[Bf-blender-cvs] [416b7036e12] blender2.8-workbench: Workbench: World shadows

Jeroen Bakker noreply at git.blender.org
Tue May 1 09:44:37 CEST 2018


Commit: 416b7036e123854d22b55129147b2ae3dd0efc8b
Author: Jeroen Bakker
Date:   Tue May 1 09:44:16 2018 +0200
Branches: blender2.8-workbench
https://developer.blender.org/rB416b7036e123854d22b55129147b2ae3dd0efc8b

Workbench: World shadows

===================================================================

M	source/blender/draw/engines/workbench/shaders/workbench_shadow_geom.glsl
M	source/blender/draw/engines/workbench/shaders/workbench_shadow_vert.glsl
M	source/blender/draw/engines/workbench/workbench_materials.c
M	source/blender/draw/intern/draw_manager_exec.c

===================================================================

diff --git a/source/blender/draw/engines/workbench/shaders/workbench_shadow_geom.glsl b/source/blender/draw/engines/workbench/shaders/workbench_shadow_geom.glsl
index 00caed91285..62a538666e4 100644
--- a/source/blender/draw/engines/workbench/shaders/workbench_shadow_geom.glsl
+++ b/source/blender/draw/engines/workbench/shaders/workbench_shadow_geom.glsl
@@ -1,43 +1,39 @@
 #define EPSILON 0.0000001
-
+#define INFINITE 1000.0
 layout(lines) in;
 layout(triangle_strip, max_vertices=4) out;
 
-uniform mat4 ViewProjectionMatrix;
+uniform mat4 ModelMatrix;
+uniform mat4 ModelViewProjectionMatrix;
 
-uniform vec3 lightDirection = vec3(0.57, -0.57, -0.57);
-in vec3 faceNormal1[]; 
-in vec3 faceNormal2[];
-in vec3 localPos[];
+uniform vec3 lightDirection = vec3(0.57, 0.57, -0.57);
+flat in vec3 faceNormal1[]; 
+flat in vec3 faceNormal2[];
 
-void emit_quad(vec3 start_vertex, vec3 end_vertex, vec3 light_direction) {
-	gl_Position = ViewProjectionMatrix * vec4((start_vertex + light_direction * EPSILON), 1.0);
+void emit_quad(vec4 start_vertex, vec4 end_vertex, vec4 light_direction) {
+	gl_Position = ModelViewProjectionMatrix * (start_vertex + light_direction * EPSILON);
 	EmitVertex();
-	gl_Position = ViewProjectionMatrix * vec4((start_vertex + light_direction * 25.0), 1.0);
+	gl_Position = ModelViewProjectionMatrix * (start_vertex + light_direction * INFINITE);
 	EmitVertex();
-	gl_Position = ViewProjectionMatrix * vec4((end_vertex + light_direction * EPSILON), 1.0);
+	gl_Position = ModelViewProjectionMatrix * (end_vertex + light_direction * EPSILON);
 	EmitVertex();
-	gl_Position = ViewProjectionMatrix * vec4((end_vertex + light_direction * 25.0), 1.0);
+	gl_Position = ModelViewProjectionMatrix * (end_vertex + light_direction * INFINITE);
 	EmitVertex();
 	EndPrimitive();
 }
 void main()
 {
-	/* 
-		TODO: light is currently connected to the object.
-		multiply with inverse object matrix to get to counteracti this.
-		or fo not use the modelviewprojectionmatrix (but the ViewProjectionMatrix)
-		
-		Should the faceNormal and local pos not be done in the vertex shader (conversion to world coordinates...)
-	*/
-	vec3 light_direction = lightDirection;
-	bool front1 = dot(faceNormal1[0], normalize(light_direction)) >= 0;
-	bool front2 = dot(faceNormal2[1], normalize(light_direction)) >= 0;
+	/* light_direction: Light direction in object space TODO: Move to CPU */
+	vec3 light_direction = normalize((vec4(lightDirection, 0.0) * ModelMatrix).xyz);
+	bool front1 = dot(faceNormal1[0], light_direction) >= 0;
+	bool front2 = dot(faceNormal2[0], light_direction) >= 0;
 
-	if (!front1 && front2) {
-		emit_quad(gl_in[0].gl_Position.xyz, gl_in[1].gl_Position.xyz, light_direction);
-	}
-	else if (front1 && !front2) {
-		emit_quad(gl_in[1].gl_Position.xyz, gl_in[0].gl_Position.xyz, light_direction);
+	if (front1 != front2) {
+		if (!front1 && front2) {
+			emit_quad(gl_in[0].gl_Position, gl_in[1].gl_Position, vec4(light_direction, 0.0));
+		}
+		else if (front1 && !front2) {
+			emit_quad(gl_in[1].gl_Position, gl_in[0].gl_Position, vec4(light_direction, 0.0));
+		}
 	}
 }
\ No newline at end of file
diff --git a/source/blender/draw/engines/workbench/shaders/workbench_shadow_vert.glsl b/source/blender/draw/engines/workbench/shaders/workbench_shadow_vert.glsl
index 74ed0265690..8bb080d5873 100644
--- a/source/blender/draw/engines/workbench/shaders/workbench_shadow_vert.glsl
+++ b/source/blender/draw/engines/workbench/shaders/workbench_shadow_vert.glsl
@@ -1,10 +1,11 @@
 in vec3 pos;
 in vec3 N1;
 in vec3 N2;
-out vec3 faceNormal1;
-out vec3 faceNormal2;
+flat out vec3 faceNormal1;
+flat out vec3 faceNormal2;
 void main()
 {
+	/* Pass through, MVP calculation happens in geometry shader */
 	faceNormal1 = N1;
 	faceNormal2 = N2;
 	gl_Position = vec4(pos, 1.0);
diff --git a/source/blender/draw/engines/workbench/workbench_materials.c b/source/blender/draw/engines/workbench/workbench_materials.c
index 40b45b84b99..c3e97207515 100644
--- a/source/blender/draw/engines/workbench/workbench_materials.c
+++ b/source/blender/draw/engines/workbench/workbench_materials.c
@@ -46,9 +46,10 @@ static struct {
 	struct GPUTexture *color_buffer_tx; /* ref only, not alloced */
 	struct GPUTexture *normal_buffer_tx; /* ref only, not alloced */
 
-	int next_object_id;
+	float light_direction[3]; /* world light direction for shadows */
 	float light_multiplier;
 	float shadow_multiplier;
+	int next_object_id;
 } e_data = {NULL};
 
 /* Shaders */
@@ -307,6 +308,8 @@ void workbench_materials_cache_init(WORKBENCH_Data *vedata)
 		wpd->world_ubo = DRW_uniformbuffer_create(sizeof(WORKBENCH_UBO_World), NULL);
 		DRW_uniformbuffer_update(wpd->world_ubo, &wpd->world_data);
 
+		copy_v3_v3(e_data.light_direction, BKE_collection_engine_property_value_get_float_array(props, "light_direction"));
+
 		psl->composite_pass = DRW_pass_create("Composite", DRW_STATE_WRITE_COLOR | DRW_STATE_STENCIL_EQUAL);
 		grp = DRW_shgroup_create(wpd->composite_sh, psl->composite_pass);
 		workbench_composite_uniforms(wpd, grp);
@@ -315,10 +318,10 @@ void workbench_materials_cache_init(WORKBENCH_Data *vedata)
 		DRW_shgroup_call_add(grp, DRW_cache_fullscreen_quad_get(), NULL);
 
 		if (SHADOW_ENABLED(wpd)) {
-			psl->shadow_pass = DRW_pass_create("Shadow", DRW_STATE_DEPTH_LESS | DRW_STATE_WRITE_STENCIL);
-			// DRW_STATE_WRITE_STENCIL | DRW_STATE_STENCIL_INCR_DECR_WRAP
+			psl->shadow_pass = DRW_pass_create("Shadow", DRW_STATE_DEPTH_LESS | DRW_STATE_WRITE_STENCIL | DRW_STATE_STENCIL_INCR_DECR_WRAP);
 			grp = DRW_shgroup_create(e_data.shadow_sh, psl->shadow_pass);
-			DRW_shgroup_stencil_mask(grp, 0x01);
+			DRW_shgroup_uniform_float(grp, "lightDirection", e_data.light_direction, 3);
+			DRW_shgroup_stencil_mask(grp, 0xFF);
 			wpd->shadow_shgrp = grp;
 
 			psl->composite_shadow_pass = DRW_pass_create("Composite Shadow", DRW_STATE_WRITE_COLOR | DRW_STATE_STENCIL_NEQUAL);
diff --git a/source/blender/draw/intern/draw_manager_exec.c b/source/blender/draw/intern/draw_manager_exec.c
index 62826b9289c..4d063aedf7e 100644
--- a/source/blender/draw/intern/draw_manager_exec.c
+++ b/source/blender/draw/intern/draw_manager_exec.c
@@ -287,8 +287,8 @@ void drw_state_set(DRWState state)
 				if ((state & DRW_STATE_WRITE_STENCIL) != 0) {
 					if ((state & DRW_STATE_STENCIL_INCR_DECR_WRAP) != 0) {
 						glStencilMask(0xFF);
-						glStencilOpSeparate(GL_BACK,  GL_KEEP, GL_INCR_WRAP, GL_KEEP);
-						glStencilOpSeparate(GL_FRONT, GL_KEEP, GL_DECR_WRAP, GL_KEEP);
+						glStencilOpSeparate(GL_BACK,  GL_KEEP, GL_KEEP, GL_INCR_WRAP);
+						glStencilOpSeparate(GL_FRONT, GL_KEEP, GL_KEEP, GL_DECR_WRAP);
 					}
 					else {
 						glStencilMask(0xFF);



More information about the Bf-blender-cvs mailing list