Browse Source

WebGLDeferredRenderer: refactored directional lights.

The same like for spotlights slso here uniform for view space light direction is enough.
alteredq 12 years ago
parent
commit
0d00644eb6
2 changed files with 15 additions and 27 deletions
  1. 10 21
      examples/js/ShaderDeferred.js
  2. 5 6
      examples/js/renderers/WebGLDeferredRenderer.js

+ 10 - 21
examples/js/ShaderDeferred.js

@@ -370,6 +370,7 @@ THREE.ShaderDeferred = {
 			matProjInverse: { type: "m4", value: new THREE.Matrix4() },
 			viewWidth: 		{ type: "f", value: 800 },
 			viewHeight: 	{ type: "f", value: 600 },
+
 			lightPos: 		{ type: "v3", value: new THREE.Vector3( 0, 0, 0 ) },
 			lightColor: 	{ type: "c", value: new THREE.Color( 0x000000 ) },
 			lightIntensity: { type: "f", value: 1.0 },
@@ -539,6 +540,7 @@ THREE.ShaderDeferred = {
 			matProjInverse: { type: "m4", value: new THREE.Matrix4() },
 			viewWidth: 		{ type: "f", value: 800 },
 			viewHeight: 	{ type: "f", value: 600 },
+
 			lightPositionVS :{ type: "v3", value: new THREE.Vector3( 0, 1, 0 ) },
 			lightDirectionVS:{ type: "v3", value: new THREE.Vector3( 0, 1, 0 ) },
 			lightColor: 	{ type: "c", value: new THREE.Color( 0x000000 ) },
@@ -689,11 +691,11 @@ THREE.ShaderDeferred = {
 
 			samplerNormalDepth: { type: "t", value: null },
 			samplerColor: 		{ type: "t", value: null },
-			matView: 		{ type: "m4", value: new THREE.Matrix4() },
 			matProjInverse: { type: "m4", value: new THREE.Matrix4() },
 			viewWidth: 		{ type: "f", value: 800 },
 			viewHeight: 	{ type: "f", value: 600 },
-			lightDir: 		{ type: "v3", value: new THREE.Vector3( 0, 1, 0 ) },
+
+			lightDirectionVS: { type: "v3", value: new THREE.Vector3( 0, 1, 0 ) },
 			lightColor: 	{ type: "c", value: new THREE.Color( 0x000000 ) },
 			lightIntensity: { type: "f", value: 1.0 }
 
@@ -701,9 +703,6 @@ THREE.ShaderDeferred = {
 
 		fragmentShader : [
 
-			"varying vec3 lightView;",
-			"varying vec4 clipPos;",
-
 			"uniform sampler2D samplerColor;",
 			"uniform sampler2D samplerNormalDepth;",
 
@@ -713,6 +712,7 @@ THREE.ShaderDeferred = {
 			"uniform float viewWidth;",
 
 			"uniform vec3 lightColor;",
+			"uniform vec3 lightDirectionVS;",
 
 			"uniform mat4 matProjInverse;",
 
@@ -745,8 +745,6 @@ THREE.ShaderDeferred = {
 				"viewPos.xyz /= viewPos.w;",
 				"viewPos.w = 1.0;",
 
-				"vec3 dirVector = normalize( lightView );",
-
 				// normal
 
 				"vec3 normal = normalDepth.xyz * 2.0 - 1.0;",
@@ -761,7 +759,7 @@ THREE.ShaderDeferred = {
 
 				"vec3 diffuse;",
 
-				"float dotProduct = dot( normal, dirVector );",
+				"float dotProduct = dot( normal, lightDirectionVS );",
 
 				"float diffuseFull = max( dotProduct, 0.0 );",
 
@@ -784,7 +782,7 @@ THREE.ShaderDeferred = {
 
 				// specular
 
-				"vec3 halfVector = normalize( dirVector - normalize( viewPos.xyz ) );",
+				"vec3 halfVector = normalize( lightDirectionVS - normalize( viewPos.xyz ) );",
 				"float dotNormalHalf = max( dot( normal, halfVector ), 0.0 );",
 
 				// simple specular
@@ -795,7 +793,7 @@ THREE.ShaderDeferred = {
 
 				"float specularNormalization = ( shininess + 2.0001 ) / 8.0;",
 
-				"vec3 schlick = specularColor + vec3( 1.0 - specularColor ) * pow( 1.0 - dot( dirVector, halfVector ), 5.0 );",
+				"vec3 schlick = specularColor + vec3( 1.0 - specularColor ) * pow( 1.0 - dot( lightDirectionVS, halfVector ), 5.0 );",
 				"vec3 specular = schlick * max( pow( dotNormalHalf, shininess ), 0.0 ) * diffuse * specularNormalization;",
 
 				// combine
@@ -809,17 +807,9 @@ THREE.ShaderDeferred = {
 
 		vertexShader : [
 
-			"varying vec3 lightView;",
-			"varying vec4 clipPos;",
-			"uniform vec3 lightDir;",
-			"uniform mat4 matView;",
-
 			"void main() { ",
 
-				"vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
-				"gl_Position = projectionMatrix * mvPosition;",
-				"lightView = vec3( matView * vec4( lightDir, 0.0 ) );",
-				"clipPos = gl_Position;",
+				"gl_Position = vec4( sign( position.xy ), 0.0, 1.0 );",
 
 			"}"
 
@@ -872,8 +862,7 @@ THREE.ShaderDeferred = {
 
 			"void main() { ",
 
-				"vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
-				"gl_Position = projectionMatrix * mvPosition;",
+				"gl_Position = vec4( sign( position.xy ), 0.0, 1.0 );",
 
 			"}"
 

+ 5 - 6
examples/js/renderers/WebGLDeferredRenderer.js

@@ -40,8 +40,6 @@ THREE.WebGLDeferredRenderer = function ( parameters ) {
 	var positionVS = new THREE.Vector3();
 	var directionVS = new THREE.Vector3();
 
-	var direction = new THREE.Vector3();
-
 	//
 
 	var geometryLightSphere = new THREE.SphereGeometry( 1, 16, 8 );
@@ -485,11 +483,12 @@ THREE.WebGLDeferredRenderer = function ( parameters ) {
 		var light = lightProxy.properties.originalLight;
 		var uniforms = lightProxy.material.uniforms;
 
-		direction.copy( light.matrixWorld.getPosition() );
-		direction.subSelf( light.target.matrixWorld.getPosition() );
-		direction.normalize();
+		directionVS.copy( light.matrixWorld.getPosition() );
+		directionVS.subSelf( light.target.matrixWorld.getPosition() );
+		directionVS.normalize();
+		camera.matrixWorldInverse.rotateAxis( directionVS );
 
-		uniforms[ "lightDir" ].value.copy( direction );
+		uniforms[ "lightDirectionVS" ].value.copy( directionVS );
 
 		// linear space colors