Browse Source

introducing the GeometricContext struct, broken hemisphere lights.

Ben Houston 9 years ago
parent
commit
ea903ed02f

+ 2 - 0
examples/webgl_animation_skinning_morph.html

@@ -158,6 +158,8 @@
 				var loader = new THREE.JSONLoader();
 				loader.load( "models/skinned/knight.js", function ( geometry, materials ) {
 
+					console.log( 'materials', materials );
+
 					createScene( geometry, materials, 0, FLOOR, -300, 60 )
 
 				} );

+ 1 - 1
examples/webgl_lights_pointlights.html

@@ -72,7 +72,7 @@
 
 				var callback = function( geometry ) {
 
-					object = new THREE.Mesh( geometry, new THREE.MeshPhongMaterial( { color: 0x555555, specular: 0xffffff, shininess: 50 }  )  );
+					object = new THREE.Mesh( geometry, new THREE.MeshPhongMaterial( { color: 0x555555, specular: 0x111111, shininess: 50 }  )  );
 					object.scale.x = object.scale.y = object.scale.z = 0.80;
 					scene.add( object );
 

+ 1 - 1
examples/webgl_morphnormals.html

@@ -114,7 +114,7 @@
 					geometry.computeVertexNormals();
 					geometry.computeMorphNormals();
 
-					var material = new THREE.MeshPhongMaterial( { color: 0xffffff, specular: 0xffffff, shininess: 20, morphTargets: true, morphNormals: true, vertexColors: THREE.FaceColors, shading: THREE.SmoothShading } );
+					var material = new THREE.MeshPhongMaterial( { color: 0xffffff, specular: 0x111111, shininess: 20, morphTargets: true, morphNormals: true, vertexColors: THREE.FaceColors, shading: THREE.SmoothShading } );
 					var mesh = new THREE.Mesh( geometry, material );
 					
 					mesh.scale.set( 1.5, 1.5, 1.5 );

+ 5 - 4
src/renderers/WebGLRenderer.js

@@ -2518,7 +2518,7 @@ THREE.WebGLRenderer = function ( parameters ) {
 
 				if( ! light.__webglUniforms ) {
 					light.__webglUniforms = {
-						position: new THREE.Vector3(),
+						direction: new THREE.Vector3(),
 						skyColor: new THREE.Color(),
 						groundColor: new THREE.Color()
 					}
@@ -2532,10 +2532,11 @@ THREE.WebGLRenderer = function ( parameters ) {
 					continue;
 				}
 
-				lightUniforms.position.setFromMatrixPosition( light.matrixWorld );
-				lightUniforms.position.transformDirection( viewMatrix );
+				lightUniforms.direction.setFromMatrixPosition( light.matrixWorld );
+				lightUniforms.direction.transformDirection( viewMatrix );
+				lightUniforms.direction.normalize();
 
-				lightUniforms.skyColor.copy(  light.color ).multiplyScalar( intensity );
+				lightUniforms.skyColor.copy( light.color ).multiplyScalar( intensity );
 				lightUniforms.groundColor.copy( light.groundColor ).multiplyScalar( intensity );
 
 			}

+ 25 - 19
src/renderers/shaders/ShaderChunk/common.glsl

@@ -102,18 +102,24 @@ struct ReflectedLight {
  	vec3 diffuse;
 };
 
-void BRDF_Lambert( const in IncidentLight incidentLight, const in vec3 normal, const in vec3 diffuseColor, inout ReflectedLight reflectedLight ) {
+struct GeometricContext {
+	vec3 position;
+	vec3 normal;
+	vec3 viewDir;
+};
+
+void BRDF_Lambert( const in IncidentLight incidentLight, const in GeometricContext geometryContext, const in vec3 diffuseColor, inout ReflectedLight reflectedLight ) {
 
-	reflectedLight.diffuse += incidentLight.color * diffuseColor * ( saturate( dot( normal, incidentLight.direction ) ) * RECIPROCAL_PI );
+	reflectedLight.diffuse += incidentLight.color * diffuseColor * ( saturate( dot( geometryContext.normal, incidentLight.direction ) ) * RECIPROCAL_PI );
 
 }
 
-void BRDF_OrenNayar( const in IncidentLight incidentLight, const in vec3 normal, const in vec3 viewDir, const in vec3 diffuse, const in float roughness, inout ReflectedLight reflectedLight ) {
+void BRDF_OrenNayar( const in IncidentLight incidentLight, const in GeometricContext geometryContext, const in vec3 diffuse, const in float roughness, inout ReflectedLight reflectedLight ) {
 
-	vec3 halfDir = normalize( incidentLight.direction + viewDir );
-	float dotVH = saturate( dot( viewDir, halfDir ) );
-	float dotNV = saturate( dot( normal, viewDir ) );
-	float dotNL = saturate( dot( normal, incidentLight.direction ) );
+	vec3 halfDir = normalize( incidentLight.direction + geometryContext.viewDir );
+	float dotVH = saturate( dot( geometryContext.viewDir, halfDir ) );
+	float dotNV = saturate( dot( geometryContext.normal, geometryContext.viewDir ) );
+	float dotNL = saturate( dot( geometryContext.normal, incidentLight.direction ) );
 
 	float m2 = roughness * roughness;
 	float termA = 1.0 - 0.5 * m2 / (m2 + 0.33);
@@ -153,10 +159,10 @@ float D_BlinnPhong( const in float shininess, const in float dotNH ) {
 
 }
 
-void BRDF_BlinnPhong( const in IncidentLight incidentLight, const in vec3 normal, const in vec3 viewDir, const in vec3 specularColor, const in float shininess, inout ReflectedLight reflectedLight ) {
+void BRDF_BlinnPhong( const in IncidentLight incidentLight, const in GeometricContext geometryContext, const in vec3 specularColor, const in float shininess, inout ReflectedLight reflectedLight ) {
 
-	vec3 halfDir = normalize( incidentLight.direction + viewDir );
-	float dotNH = saturate( dot( normal, halfDir ) );
+	vec3 halfDir = normalize( incidentLight.direction + geometryContext.viewDir );
+	float dotNH = saturate( dot( geometryContext.normal, halfDir ) );
 	float dotLH = saturate( dot( incidentLight.direction, halfDir ) );
 
 	vec3 F = F_Schlick( specularColor, dotLH );
@@ -176,7 +182,7 @@ void BRDF_BlinnPhong( const in IncidentLight incidentLight, const in vec3 normal
 
 	uniform DirectionalLight directionalLights[ MAX_DIR_LIGHTS ];
 
-	void getDirIncidentLight( const in DirectionalLight directionalLight, out IncidentLight incidentLight ) { 
+	void getDirIncidentLight( const in DirectionalLight directionalLight, const in GeometricContext geometryContext, out IncidentLight incidentLight ) { 
 	
 		incidentLight.color = directionalLight.color;
 		incidentLight.direction = directionalLight.direction; 
@@ -196,11 +202,11 @@ void BRDF_BlinnPhong( const in IncidentLight incidentLight, const in vec3 normal
 
 	uniform PointLight pointLights[ MAX_POINT_LIGHTS ];
 
-	void getPointIncidentLight( const in PointLight pointLight, const in vec3 position, out IncidentLight incidentLight ) { 
+	void getPointIncidentLight( const in PointLight pointLight, const in GeometricContext geometryContext, out IncidentLight incidentLight ) { 
 	
 		vec3 lightPosition = pointLight.position; 
 	
-		vec3 lVector = lightPosition - position; 
+		vec3 lVector = lightPosition - geometryContext.position; 
 		incidentLight.direction = normalize( lVector ); 
 	
 		incidentLight.color = pointLight.color; 
@@ -224,11 +230,11 @@ void BRDF_BlinnPhong( const in IncidentLight incidentLight, const in vec3 normal
 
 	uniform SpotLight spotLights[ MAX_SPOT_LIGHTS ];
 
-	void getSpotIncidentLight( const in SpotLight spotLight, const in vec3 position, out IncidentLight incidentLight ) {
+	void getSpotIncidentLight( const in SpotLight spotLight, const in GeometricContext geometryContext, out IncidentLight incidentLight ) {
 	
 		vec3 lightPosition = spotLight.position;
 	
-		vec3 lVector = lightPosition - position;
+		vec3 lVector = lightPosition - geometryContext.position;
 		incidentLight.direction = normalize( lVector );
 	
 		float spotEffect = dot( spotLight.direction, incidentLight.direction );
@@ -250,17 +256,17 @@ void BRDF_BlinnPhong( const in IncidentLight incidentLight, const in vec3 normal
 	  vec3 groundColor;
 	};
 
-	layout(packed)uniform HemisphereLight hemisphereLights[ MAX_HEMI_LIGHTS ];
+	uniform HemisphereLight hemisphereLights[ MAX_HEMI_LIGHTS ];
 
-	void getHemisphereIncidentLight( const in HemisphereLight hemiLight, const in vec3 normal, out IncidentLight incidentLight ) { 
+	void getHemisphereIncidentLight( const in HemisphereLight hemiLight, const in GeometricContext geometryContext, out IncidentLight incidentLight ) { 
 	
-		float dotNL = dot( normal, hemiLight.direction );
+		float dotNL = dot( geometryContext.normal, hemiLight.direction );
 
 		float hemiDiffuseWeight = 0.5 * dotNL + 0.5;
 
 		incidentLight.color = mix( hemiLight.groundColor, hemiLight.skyColor, hemiDiffuseWeight );
 
-		incidentLight.direction = normal;
+		incidentLight.direction = geometryContext.normal;
 
 	}
 

+ 16 - 14
src/renderers/shaders/ShaderChunk/lights_lambert_vertex.glsl

@@ -1,5 +1,5 @@
+vec3 viewDir = normalize( -mvPosition.xyz );
 vec3 normal = normalize( transformedNormal );
-vec3 backNormal = -normal;
 
 vec3 diffuse = vec3( 1.0 );
 
@@ -7,18 +7,20 @@ IncidentLight incidentLight;
 ReflectedLight frontReflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 ) );
 ReflectedLight backReflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 ) );
 
+GeometricContext geometricContext = GeometricContext( mvPosition.xyz, normal, viewDir );
+GeometricContext backGeometricContext = GeometricContext( mvPosition.xyz, -normal, viewDir );
 
 #if MAX_POINT_LIGHTS > 0
 
 	for ( int i = 0; i < MAX_POINT_LIGHTS; i ++ ) {
 
-		getPointIncidentLight( pointLights[ i ], mvPosition.xyz, incidentLight );
+		getPointIncidentLight( pointLights[ i ], geometricContext, incidentLight );
 
-		BRDF_Lambert( incidentLight, normal, diffuse, frontReflectedLight );
+		BRDF_Lambert( incidentLight, geometricContext, diffuse, frontReflectedLight );
 
 		#ifdef DOUBLE_SIDED
 
-			BRDF_Lambert( incidentLight, backNormal, diffuse, backReflectedLight );
+			BRDF_Lambert( incidentLight, backGeometricContext, diffuse, backReflectedLight );
 
 		#endif
 
@@ -30,13 +32,13 @@ ReflectedLight backReflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 ) );
 
 	for ( int i = 0; i < MAX_SPOT_LIGHTS; i ++ ) {
 
-		getSpotIncidentLight( spotLights[ i ], mvPosition.xyz, incidentLight );
+		getSpotIncidentLight( spotLights[ i ], geometricContext, incidentLight );
 
-		BRDF_Lambert( incidentLight, normal, diffuse, frontReflectedLight );
+		BRDF_Lambert( incidentLight, geometricContext, diffuse, frontReflectedLight );
 
 		#ifdef DOUBLE_SIDED
 
-			BRDF_Lambert( incidentLight, backNormal, diffuse, backReflectedLight );
+			BRDF_Lambert( incidentLight, backGeometricContext, diffuse, backReflectedLight );
 
 		#endif
 
@@ -48,13 +50,13 @@ ReflectedLight backReflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 ) );
 
 	for ( int i = 0; i < MAX_DIR_LIGHTS; i ++ ) {
 
-		getDirIncidentLight( directionalLights[ i ], incidentLight );
+		getDirIncidentLight( directionalLights[ i ], geometricContext, incidentLight );
 
-		BRDF_Lambert( incidentLight, normal, diffuse, frontReflectedLight );
+		BRDF_Lambert( incidentLight, geometricContext, diffuse, frontReflectedLight );
 
 		#ifdef DOUBLE_SIDED
 
-			BRDF_Lambert( incidentLight, backNormal, diffuse, backReflectedLight );
+			BRDF_Lambert( incidentLight, backGeometricContext, diffuse, backReflectedLight );
 
 		#endif
 
@@ -66,15 +68,15 @@ ReflectedLight backReflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 ) );
 
 	for ( int i = 0; i < MAX_HEMI_LIGHTS; i ++ ) {
 
-		getHemisphereLightIndirect( hemisphereLights[ i ], normal, incidentLight );
+		getHemisphereLightIndirect( hemisphereLights[ i ], geometricContext, , incidentLight );
 
-		BRDF_Lambert( incidentLight, normal, diffuse, frontReflectedLight );
+		BRDF_Lambert( incidentLight, geometricContext, , diffuse, frontReflectedLight );
 
 		#ifdef DOUBLE_SIDED
 	
-			getHemisphereIncidentLight( hemisphereLights[ i ], backNormal, incidentLight );
+			getHemisphereIncidentLight( hemisphereLights[ i ], backGeometricContext, incidentLight );
 
-			BRDF_Lambert( incidentLight, backNormal, diffuse, backReflectedLight );
+			BRDF_Lambert( incidentLight, backGeometricContext, diffuse, backReflectedLight );
 
 		#endif
 

+ 17 - 15
src/renderers/shaders/ShaderChunk/lights_phong_fragment.glsl

@@ -24,16 +24,18 @@ IncidentLight incidentLight;
 ReflectedLight directReflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 ) );
 ReflectedLight indirectReflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 ) );
 
+GeometricContext geometricContext = GeometricContext( -vViewPosition, normal, viewDir );
+
 #if MAX_POINT_LIGHTS > 0
 
 	for ( int i = 0; i < MAX_POINT_LIGHTS; i ++ ) {
 
-		getPointIncidentLight( pointLights[ i ], -vViewPosition, incidentLight );
+		getPointIncidentLight( pointLights[ i ], geometricContext, incidentLight );
 
-		BRDF_Lambert( incidentLight, normal, diffuse, directReflectedLight );
-		//BRDF_OrenNayar( incidentLight, normal, viewDir, diffuse, 0.5, directReflectedLight );
+		BRDF_Lambert( incidentLight, geometricContext, diffuse, directReflectedLight );
+		//BRDF_OrenNayar( incidentLight, geometricContext, diffuse, 0.5, directReflectedLight );
 
-		BRDF_BlinnPhong( incidentLight, normal, viewDir, specular, shininess, directReflectedLight );
+		BRDF_BlinnPhong( incidentLight, geometricContext, specular, shininess, directReflectedLight );
 
 	}
 
@@ -43,12 +45,12 @@ ReflectedLight indirectReflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 )
 
 	for ( int i = 0; i < MAX_SPOT_LIGHTS; i ++ ) {
 
-		getSpotIncidentLight( spotLights[ i ], -vViewPosition, incidentLight );
+		getSpotIncidentLight( spotLights[ i ], geometricContext, incidentLight );
 
-		BRDF_Lambert( incidentLight, normal, diffuse, directReflectedLight );
-		//BRDF_OrenNayar( incidentLight, normal, viewDir, diffuse, 0.5, directReflectedLight );
+		BRDF_Lambert( incidentLight, geometricContext, diffuse, directReflectedLight );
+		//BRDF_OrenNayar( incidentLight, geometricContext, diffuse, 0.5, directReflectedLight );
 
-		BRDF_BlinnPhong( incidentLight, normal, viewDir, specular, shininess, directReflectedLight );
+		BRDF_BlinnPhong( incidentLight, geometricContext, specular, shininess, directReflectedLight );
 
 	}
 
@@ -58,12 +60,12 @@ ReflectedLight indirectReflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 )
 
 	for( int i = 0; i < MAX_DIR_LIGHTS; i ++ ) {
 
-		getDirIncidentLight( directionalLights[ i ], incidentLight );
+		getDirIncidentLight( directionalLights[ i ], geometricContext, incidentLight );
 
-		BRDF_Lambert( incidentLight, normal, diffuse, directReflectedLight );
-		//BRDF_OrenNayar( incidentLight, normal, viewDir, diffuse, 0.5, directReflectedLight );
+		BRDF_Lambert( incidentLight, geometricContext, diffuse, directReflectedLight );
+		//BRDF_OrenNayar( incidentLight, geometricContext, diffuse, 0.5, directReflectedLight );
 
-		BRDF_BlinnPhong( incidentLight, normal, viewDir, specular, shininess, directReflectedLight );
+		BRDF_BlinnPhong( incidentLight, geometricContext, specular, shininess, directReflectedLight );
 
 	}
 
@@ -73,10 +75,10 @@ ReflectedLight indirectReflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 )
 
 	for( int i = 0; i < MAX_HEMI_LIGHTS; i ++ ) {
 
-		getHemisphereIncidentLight( hemisphereLights[ i ], normal, incidentLight );
+		getHemisphereIncidentLight( hemisphereLights[ i ], geometricContext, incidentLight );
 
-		BRDF_Lambert( incidentLight, normal, diffuse, indirectReflectedLight );
-		//BRDF_OrenNayar( incidentLight, normal, viewDir, diffuse, 0.5, indirectReflectedLight );
+		BRDF_Lambert( incidentLight, geometricContext, diffuse, indirectReflectedLight );
+		//BRDF_OrenNayar( incidentLight, geometricContext, diffuse, 0.5, indirectReflectedLight );
 
 	}