[Bf-blender-cvs] [861616b] master: Full Inverse-Quadratic-Equation Lamp Falloff
Jack Andersen
noreply at git.blender.org
Sun Mar 13 02:06:02 CET 2016
Commit: 861616bf693b78b070ada6cbc6aa79eb807fdde8
Author: Jack Andersen
Date: Sun Mar 13 02:00:12 2016 +0100
Branches: master
https://developer.blender.org/rB861616bf693b78b070ada6cbc6aa79eb807fdde8
Full Inverse-Quadratic-Equation Lamp Falloff
This patch adds a new `falloff_type` ('Inverse Coefficients') for Lamps in
Blender-Internal and GLSL.
The current falloff modes use a formula like this inverse-square one:
`I = E × (D^2 / (D^2 + Q × r^2))`
While such a formula is simple for 3D-artists to use, it's algebraically
cumbersome to work with. Game-designers authoring their own shaders
could benefit much more by having direct control of falloff-coefficients:
`I = E × (1.0 / (coefC + coefL × r + coefQ × r^2))`
In this mode, the `distance` parameter is unused (except for 'Sphere'
mode); instead relying on the designer to mathematically-model the
falloff-behavior.
The UI has been patched like so:
{F153843}
Reviewers: brecht, psy-fi
Reviewed By: psy-fi
Subscribers: brita_, antidote, campbellbarton, psy-fi
Differential Revision: https://developer.blender.org/D1194
===================================================================
M release/scripts/startup/bl_ui/properties_data_lamp.py
M source/blender/blenkernel/intern/lamp.c
M source/blender/gpu/GPU_material.h
M source/blender/gpu/intern/gpu_material.c
M source/blender/gpu/shaders/gpu_shader_material.glsl
M source/blender/makesdna/DNA_lamp_types.h
M source/blender/makesrna/intern/rna_lamp.c
M source/blender/render/intern/include/render_types.h
M source/blender/render/intern/source/convertblender.c
M source/blender/render/intern/source/shadeoutput.c
M source/gameengine/Converter/BL_BlenderDataConversion.cpp
M source/gameengine/Rasterizer/RAS_ILightObject.h
M source/gameengine/Rasterizer/RAS_OpenGLRasterizer/RAS_OpenGLLight.cpp
===================================================================
diff --git a/release/scripts/startup/bl_ui/properties_data_lamp.py b/release/scripts/startup/bl_ui/properties_data_lamp.py
index 0121ad4..30cd39d 100644
--- a/release/scripts/startup/bl_ui/properties_data_lamp.py
+++ b/release/scripts/startup/bl_ui/properties_data_lamp.py
@@ -103,6 +103,13 @@ class DATA_PT_lamp(DataButtonsPanel, Panel):
sub.prop(lamp, "linear_attenuation", slider=True, text="Linear")
sub.prop(lamp, "quadratic_attenuation", slider=True, text="Quadratic")
+ elif lamp.falloff_type == 'INVERSE_COEFFICIENTS':
+ col.label(text="Inverse Coefficients:")
+ sub = col.column(align=True)
+ sub.prop(lamp, "constant_coefficient", text="Constant")
+ sub.prop(lamp, "linear_coefficient", text="Linear")
+ sub.prop(lamp, "quadratic_coefficient", text="Quadratic")
+
col.prop(lamp, "use_sphere")
if lamp.type == 'AREA':
diff --git a/source/blender/blenkernel/intern/lamp.c b/source/blender/blenkernel/intern/lamp.c
index 9601a93..49a5734 100644
--- a/source/blender/blenkernel/intern/lamp.c
+++ b/source/blender/blenkernel/intern/lamp.c
@@ -79,6 +79,9 @@ void BKE_lamp_init(Lamp *la)
la->adapt_thresh = 0.001f;
la->preview = NULL;
la->falloff_type = LA_FALLOFF_INVSQUARE;
+ la->coeff_const = 1.0f;
+ la->coeff_lin = 0.0f;
+ la->coeff_quad = 0.0f;
la->curfalloff = curvemapping_add(1, 0.0f, 1.0f, 1.0f, 0.0f);
la->sun_effect_type = 0;
la->horizon_brightness = 1.0;
diff --git a/source/blender/gpu/GPU_material.h b/source/blender/gpu/GPU_material.h
index 3bcc7e2..f150b72 100644
--- a/source/blender/gpu/GPU_material.h
+++ b/source/blender/gpu/GPU_material.h
@@ -166,6 +166,9 @@ typedef enum GPUDynamicType {
GPU_DYNAMIC_LAMP_SPOTSIZE = 10 | GPU_DYNAMIC_GROUP_LAMP,
GPU_DYNAMIC_LAMP_SPOTBLEND = 11 | GPU_DYNAMIC_GROUP_LAMP,
GPU_DYNAMIC_LAMP_SPOTSCALE = 12 | GPU_DYNAMIC_GROUP_LAMP,
+ GPU_DYNAMIC_LAMP_COEFFCONST = 13 | GPU_DYNAMIC_GROUP_LAMP,
+ GPU_DYNAMIC_LAMP_COEFFLIN = 14 | GPU_DYNAMIC_GROUP_LAMP,
+ GPU_DYNAMIC_LAMP_COEFFQUAD = 15 | GPU_DYNAMIC_GROUP_LAMP,
GPU_DYNAMIC_SAMPLER_2DBUFFER = 1 | GPU_DYNAMIC_GROUP_SAMPLER,
GPU_DYNAMIC_SAMPLER_2DIMAGE = 2 | GPU_DYNAMIC_GROUP_SAMPLER,
@@ -318,7 +321,8 @@ float *GPU_lamp_dynpersmat(GPULamp *lamp);
void GPU_lamp_update(GPULamp *lamp, int lay, int hide, float obmat[4][4]);
void GPU_lamp_update_colors(GPULamp *lamp, float r, float g, float b, float energy);
-void GPU_lamp_update_distance(GPULamp *lamp, float distance, float att1, float att2);
+void GPU_lamp_update_distance(GPULamp *lamp, float distance, float att1, float att2,
+ float coeff_const, float coeff_lin, float coeff_quad);
void GPU_lamp_update_spot(GPULamp *lamp, float spotsize, float spotblend);
int GPU_lamp_shadow_layer(GPULamp *lamp);
GPUNodeLink *GPU_lamp_get_data(
diff --git a/source/blender/gpu/intern/gpu_material.c b/source/blender/gpu/intern/gpu_material.c
index cc401dc..c323268 100644
--- a/source/blender/gpu/intern/gpu_material.c
+++ b/source/blender/gpu/intern/gpu_material.c
@@ -148,6 +148,7 @@ struct GPULamp {
float spotvec[2];
float dyndist, dynatt1, dynatt2;
float dist, att1, att2;
+ float coeff_const, coeff_lin, coeff_quad;
float shadow_color[3];
float bias, d, clipend;
@@ -550,6 +551,12 @@ static GPUNodeLink *lamp_get_visibility(GPUMaterial *mat, GPULamp *lamp, GPUNode
GPU_dynamic_uniform(&lamp->att1, GPU_DYNAMIC_LAMP_ATT1, lamp->ob),
GPU_dynamic_uniform(&lamp->att2, GPU_DYNAMIC_LAMP_ATT2, lamp->ob), *dist, &visifac);
break;
+ case LA_FALLOFF_INVCOEFFICIENTS:
+ GPU_link(mat, "lamp_falloff_invcoefficients",
+ GPU_dynamic_uniform(&lamp->coeff_const, GPU_DYNAMIC_LAMP_COEFFCONST, lamp->ob),
+ GPU_dynamic_uniform(&lamp->coeff_lin, GPU_DYNAMIC_LAMP_COEFFLIN, lamp->ob),
+ GPU_dynamic_uniform(&lamp->coeff_quad, GPU_DYNAMIC_LAMP_COEFFQUAD, lamp->ob), *dist, &visifac);
+ break;
case LA_FALLOFF_CURVE:
{
float *array;
@@ -2209,11 +2216,15 @@ void GPU_lamp_update_colors(GPULamp *lamp, float r, float g, float b, float ener
lamp->col[2] = b;
}
-void GPU_lamp_update_distance(GPULamp *lamp, float distance, float att1, float att2)
+void GPU_lamp_update_distance(GPULamp *lamp, float distance, float att1, float att2,
+ float coeff_const, float coeff_lin, float coeff_quad)
{
lamp->dist = distance;
lamp->att1 = att1;
lamp->att2 = att2;
+ lamp->coeff_const = coeff_const;
+ lamp->coeff_lin = coeff_lin;
+ lamp->coeff_quad = coeff_quad;
}
void GPU_lamp_update_spot(GPULamp *lamp, float spotsize, float spotblend)
@@ -2254,6 +2265,9 @@ static void gpu_lamp_from_blender(Scene *scene, Object *ob, Object *par, Lamp *l
lamp->falloff_type = la->falloff_type;
lamp->att1 = la->att1;
lamp->att2 = la->att2;
+ lamp->coeff_const = la->coeff_const;
+ lamp->coeff_lin = la->coeff_lin;
+ lamp->coeff_quad = la->coeff_quad;
lamp->curfalloff = la->curfalloff;
/* initshadowbuf */
diff --git a/source/blender/gpu/shaders/gpu_shader_material.glsl b/source/blender/gpu/shaders/gpu_shader_material.glsl
index 51d4706..05853c3 100644
--- a/source/blender/gpu/shaders/gpu_shader_material.glsl
+++ b/source/blender/gpu/shaders/gpu_shader_material.glsl
@@ -1627,6 +1627,17 @@ void lamp_falloff_sliders(float lampdist, float ld1, float ld2, float dist, out
visifac *= lampdistkw/(lampdistkw + ld2*dist*dist);
}
+void lamp_falloff_invcoefficients(float coeff_const, float coeff_lin, float coeff_quad, float dist, out float visifac)
+{
+ vec3 coeff = vec3(coeff_const, coeff_lin, coeff_quad);
+ vec3 d_coeff = vec3(1.0, dist, dist*dist);
+ float visifac_r = dot(coeff, d_coeff);
+ if (visifac_r > 0.0)
+ visifac = 1.0 / visifac_r;
+ else
+ visifac = 0.0;
+}
+
void lamp_falloff_curve(float lampdist, sampler2D curvemap, float dist, out float visifac)
{
visifac = texture2D(curvemap, vec2(dist/lampdist, 0.0)).x;
diff --git a/source/blender/makesdna/DNA_lamp_types.h b/source/blender/makesdna/DNA_lamp_types.h
index 8f01122..a2e39f9 100644
--- a/source/blender/makesdna/DNA_lamp_types.h
+++ b/source/blender/makesdna/DNA_lamp_types.h
@@ -61,6 +61,7 @@ typedef struct Lamp {
float att1, att2; /* Quad1 and Quad2 attenuation */
+ float coeff_const, coeff_lin, coeff_quad, coeff_pad;
struct CurveMapping *curfalloff;
short falloff_type;
short pad2;
@@ -161,11 +162,12 @@ typedef struct Lamp {
#define LA_SUN_EFFECT_AP 2
/* falloff_type */
-#define LA_FALLOFF_CONSTANT 0
+#define LA_FALLOFF_CONSTANT 0
#define LA_FALLOFF_INVLINEAR 1
-#define LA_FALLOFF_INVSQUARE 2
-#define LA_FALLOFF_CURVE 3
-#define LA_FALLOFF_SLIDERS 4
+#define LA_FALLOFF_INVSQUARE 2
+#define LA_FALLOFF_CURVE 3
+#define LA_FALLOFF_SLIDERS 4
+#define LA_FALLOFF_INVCOEFFICIENTS 5
/* buftype, no flag */
diff --git a/source/blender/makesrna/intern/rna_lamp.c b/source/blender/makesrna/intern/rna_lamp.c
index 85a2623..b6b97d3 100644
--- a/source/blender/makesrna/intern/rna_lamp.c
+++ b/source/blender/makesrna/intern/rna_lamp.c
@@ -420,6 +420,7 @@ static void rna_def_lamp_falloff(StructRNA *srna)
{LA_FALLOFF_CONSTANT, "CONSTANT", 0, "Constant", ""},
{LA_FALLOFF_INVLINEAR, "INVERSE_LINEAR", 0, "Inverse Linear", ""},
{LA_FALLOFF_INVSQUARE, "INVERSE_SQUARE", 0, "Inverse Square", ""},
+ {LA_FALLOFF_INVCOEFFICIENTS, "INVERSE_COEFFICIENTS", 0, "Inverse Coefficients", ""},
{LA_FALLOFF_CURVE, "CUSTOM_CURVE", 0, "Custom Curve", ""},
{LA_FALLOFF_SLIDERS, "LINEAR_QUADRATIC_WEIGHTED", 0, "Lin/Quad Weighted", ""},
{0, NULL, 0, NULL, NULL}
@@ -451,6 +452,27 @@ static void rna_def_lamp_falloff(StructRNA *srna)
RNA_def_property_range(prop, 0.0f, 1.0f);
RNA_def_property_ui_text(prop, "Quadratic Attenuation", "Quadratic distance attenuation");
RNA_def_property_update(prop, 0, "rna_Lamp_draw_update");
+
+ prop = RNA_def_property(srna, "constant_coefficient", PROP_FLOAT, PROP_NONE);
+ RNA_def_property_float_sdna(prop, NULL, "coeff_const");
+ RNA_def_property_range(prop, 0.0f, FLT_MAX);
+ RNA_def_property_ui_text(prop, "Constant Coefficient",
+ "Constant distance attenuation coefficient");
+ RNA_def_property_update(prop, 0, "rna_Lamp_draw_update");
+
+ prop = RNA_def_property(srna, "linear_coefficient", PROP_FLOAT, PROP_NONE);
+ RNA_def_property_float_sdna(prop, NULL, "coeff_lin");
+ RNA_def_property_range(prop, 0.0f, FLT_MAX);
+ RNA_def_property_ui_text(prop, "Linear Coefficient",
+ "Linear distance attenuation coefficient");
+ RNA_def_property_update(prop, 0, "rna_Lamp_draw_update");
+
+ prop = RNA_def_property(srna, "quadratic_coefficient", PROP_FLOAT, PROP_NONE);
+ RNA_def_property_float_sdna(prop, NULL, "coeff_quad");
+ RNA_def_property_range(prop, 0.0f, FLT_MAX);
+ RNA_def_property_ui_text(prop, "Quadratic Coefficient",
+ "Quadratic distance attenuation coefficient");
+ RNA_def_property_update(prop, 0, "rna_Lamp_draw_update");
}
static void rna_def_lamp_shadow(StructRNA *srna, int spot, int area)
diff --git a/source/blender/render/intern/include/render_types.h b/source/blender/render/intern/include/render_types.h
index ed83cfe..cef3a07 100644
--- a/source/blender/render/intern/include/render_types.h
+++ b/source/blender/render/intern/include/render_types.h
@@ -569,6 +569,7 @@ typedef struct LampRen {
short falloff_type;
float ld1, ld2;
+ float coeff_const, coeff_lin, coeff_quad;
struct CurveMapping *curfalloff;
/* copied from Lamp, to decouple more rendering stuff */
diff --git a/source/blender/render/intern/source/convertblender.c b/source/blender/render/intern/source/convertblender.c
index 64fd567..ccf54cb 100644
--- a/source/blender/render/intern/source/convertblender.c
+++ b/source/blender/render/intern/source/convertblender.c
@@ -3801,6 +3801,9 @@ static GroupObject *add_render_lamp(Render *re, Object *ob)
lar->falloff_type = la->falloff_type;
lar->ld1= la->att1;
lar->ld2= la->att2;
+ lar->coeff_const= la->coeff_const;
+ lar->coeff_lin= la->coeff_lin;
+ lar->coeff_quad= la->coeff_quad;
lar->curfalloff = curvemapping_copy(la->curfalloff);
if (lar->curfalloff) {
diff --git a/sourc
@@ Diff output truncated at 10240 characters. @@
More information about the Bf-blender-cvs
mailing list