Browse Source

lambert lighting works, defining light helpers only once in common.glsl

Ben Houston 9 years ago
parent
commit
28754cf13c

+ 0 - 2
src/renderers/WebGLRenderer.js

@@ -2137,7 +2137,6 @@ THREE.WebGLRenderer = function ( parameters ) {
 						};
 
 					}
-					debugger;
 
 					break;
 
@@ -2176,7 +2175,6 @@ THREE.WebGLRenderer = function ( parameters ) {
 						}
 
 					}
-					debugger;
 					
 					break;
 

+ 101 - 1
src/renderers/shaders/ShaderChunk/common.glsl

@@ -124,4 +124,104 @@ vec3 BRDF_BlinnPhong( in vec3 specularColor, in float shininess, in float dotNH,
 
 	return F * G * D;
 
-}
+}
+
+#if MAX_DIR_LIGHTS > 0
+
+	struct DirectionalLight {
+	  vec3 direction;
+	  vec3 color;
+	};
+
+	uniform DirectionalLight singleTestDirLight;
+
+	uniform DirectionalLight directionalLights[ MAX_DIR_LIGHTS ];
+
+	void getDirLightDirect( const in DirectionalLight directionalLight, out vec3 lightDir, out vec3 lightColor ) { 
+	
+		lightDir = directionalLight.direction; 
+		lightColor = directionalLight.color;
+
+	}
+
+#endif
+
+#if MAX_POINT_LIGHTS > 0
+
+	struct PointLight {
+	  vec3 position;
+	  vec3 color;
+	  float distance;
+	  float decay;
+	};
+
+	uniform PointLight singleTestPointLight;
+
+	uniform PointLight pointLights[ MAX_POINT_LIGHTS ];
+
+	void getPointLightDirect( const in PointLight pointLight, const in vec3 position, out vec3 lightDir, out vec3 lightColor ) { 
+	
+		vec3 lightPosition = pointLight.position; 
+	
+		vec3 lVector = lightPosition - position; 
+		lightDir = normalize( lVector ); 
+	
+		lightColor = pointLight.color; 
+		lightColor *= calcLightAttenuation( length( lVector ), pointLight.distance, pointLight.decay ); 
+	
+	}
+
+#endif
+
+#if MAX_SPOT_LIGHTS > 0
+
+	struct SpotLight {
+	  vec3 position;
+	  vec3 direction;
+	  vec3 color;
+	  float distance;
+	  float decay;
+	  float angleCos;
+	  float exponent;
+	};
+
+	uniform SpotLight spotLights[ MAX_SPOT_LIGHTS ];
+
+	void getSpotLightDirect( const in SpotLight spotLight, const in vec3 position, out vec3 lightDir, out vec3 lightColor ) {
+	
+		vec3 lightPosition = spotLight.position;
+	
+		vec3 lVector = lightPosition - position;
+		lightDir = normalize( lVector );
+	
+		float spotEffect = dot( spotLight.direction, lightDir );
+		spotEffect = saturate( pow( saturate( spotEffect ), spotLight.exponent ) );
+	
+		lightColor = spotLight.color;
+		lightColor *= ( spotEffect * calcLightAttenuation( length( lVector ), spotLight.distance, spotLight.decay ) );
+
+	}
+
+#endif
+
+
+#if MAX_HEMI_LIGHTS > 0
+
+	struct HemisphereLight {
+	  vec3 direction;
+	  vec3 skyColor;
+	  vec3 groundColor;
+	};
+
+	uniform HemisphereLight hemisphereLights[ MAX_HEMI_LIGHTS ];
+
+	vec3 getHemisphereLightIndirect( const in HemisphereLight hemiLight, in vec3 normal ) { 
+	
+		float dotNL = dot( normal, hemiLight.direction );
+
+		float hemiDiffuseWeight = 0.5 * dotNL + 0.5;
+
+		return mix( hemiLight.groundColor, hemiLight.skyColor, hemiDiffuseWeight );
+	}
+
+#endif

+ 0 - 112
src/renderers/shaders/ShaderChunk/lights_lambert_pars_vertex.glsl

@@ -1,113 +1 @@
 uniform vec3 ambientLightColor;
-
-
-#if MAX_DIR_LIGHTS > 0
-
-	//uniform vec3 directionalLightColor[ MAX_DIR_LIGHTS ];
-	//uniform vec3 directionalLightDirection[ MAX_DIR_LIGHTS ];
-
-	struct DirectionalLight {
-	  vec3 color;
-	  vec3 direction;
-	};
-
-	uniform DirectionalLight directionalLights[ MAX_DIR_LIGHTS ];
-
-	void getDirLight( const in DirectionalLight directionalLight, out vec3 lightDir, out vec3 lightColor ) { 
-	
-		lightDir = directionalLight.direction; 
-		lightColor = directionalLight.color;
-
-	}
-
-#endif
-
-#if MAX_HEMI_LIGHTS > 0
-
-	//uniform vec3 hemisphereLightSkyColor[ MAX_HEMI_LIGHTS ];
-	//uniform vec3 hemisphereLightGroundColor[ MAX_HEMI_LIGHTS ];
-	//uniform vec3 hemisphereLightDirection[ MAX_HEMI_LIGHTS ];
-
-	struct HemisphereLight {
-	  vec3 skyColor;
-	  vec3 groundColor;
-	  vec3 direction;
-	};
-
-	uniform HemisphereLight hemisphereLights[ MAX_HEMI_LIGHTS ];
-
-#endif
-
-#if MAX_POINT_LIGHTS > 0
-
-	//uniform vec3 pointLightColor[ MAX_POINT_LIGHTS ];
-	//uniform vec3 pointLightPosition[ MAX_POINT_LIGHTS ];
-	//uniform float pointLightDistance[ MAX_POINT_LIGHTS ];
-	//uniform float pointLightDecay[ MAX_POINT_LIGHTS ];
-
-	struct PointLight {
-	  vec3 color;
-	  vec3 position;
-	  float decay;
-	  float distance;
-	};
-
-	uniform PointLight pointLights[ MAX_POINT_LIGHTS ];
-
-	void getPointLight( const in PointLight pointLight, out vec3 lightDir, out vec3 lightColor ) { 
-	
-		vec3 lightPosition = pointLight.position; 
-	
-		vec3 lVector = lightPosition + vViewPosition.xyz; 
-		lightDir = normalize( lVector ); 
-	
-		lightColor = pointLight.color; 
-		lightColor *= calcLightAttenuation( length( lVector ), pointLight.distance, pointLight.decay ); 
-	
-	}
-
-#endif
-
-#if MAX_SPOT_LIGHTS > 0
-
-	//uniform vec3 spotLightColor[ MAX_SPOT_LIGHTS ];
-	//uniform vec3 spotLightPosition[ MAX_SPOT_LIGHTS ];
-	//uniform vec3 spotLightDirection[ MAX_SPOT_LIGHTS ];
-	//uniform float spotLightAngleCos[ MAX_SPOT_LIGHTS ];
-	//uniform float spotLightExponent[ MAX_SPOT_LIGHTS ];
-	//uniform float spotLightDistance[ MAX_SPOT_LIGHTS ];
-	//uniform float spotLightDecay[ MAX_SPOT_LIGHTS ];
-
-	struct SpotLight {
-	  vec3 color;
-	  vec3 position;
-	  vec3 direction;
-	  float angleCos;
-	  float exponent;
-	  float distance;
-	  float decay;
-	};
-
-	uniform SpotLight spotLights[ MAX_SPOT_LIGHTS ];
-
-	void getSpotLight( const in SpotLight spotLight, out vec3 lightDir, out vec3 lightColor ) {
-	
-		vec3 lightPosition = spotLight.position;
-	
-		vec3 lVector = lightPosition + vViewPosition.xyz;
-		lightDir = normalize( lVector );
-	
-		float spotEffect = dot( spotLight.direction, lightDir );
-		spotEffect = saturate( pow( saturate( spotEffect ), spotLight.exponent ) );
-	
-		lightColor = spotLight.color;
-		lightColor *= ( spotEffect * calcLightAttenuation( length( lVector ), spotLight.distance, spotLight.decay ) );
-
-	}
-
-#endif
-
-
-
-
-

+ 27 - 56
src/renderers/shaders/ShaderChunk/lights_lambert_vertex.glsl

@@ -12,26 +12,21 @@ vec3 normal = normalize( transformedNormal );
 
 	for ( int i = 0; i < MAX_POINT_LIGHTS; i ++ ) {
 
-		vec3 lightColor = pointLightColor[ i ];
+		vec3 lightDir, lightIntensity;
+		getPointLight( pointLights[i], -vViewPosition, lightDir, lightIntensity );
 
-		vec3 lVector = pointLightPosition[ i ] - mvPosition.xyz;
-		vec3 lightDir = normalize( lVector );
+		if( dot( lightIntensity, lightIntensity ) > 0.0 ) {
 
-		// attenuation
+			float dotNL = dot( normal, lightDir );
 
-		float attenuation = calcLightAttenuation( length( lVector ), pointLightDistance[ i ], pointLightDecay[ i ] );
+			vLightFront += lightIntensity * saturate( dotNL );
 
-		// diffuse
-
-		float dotProduct = dot( normal, lightDir );
-
-		vLightFront += lightColor * attenuation * saturate( dotProduct );
-
-		#ifdef DOUBLE_SIDED
+			#ifdef DOUBLE_SIDED
 
-			vLightBack += lightColor * attenuation * saturate( - dotProduct );
+				vLightBack += lightIntensity * saturate( - dotNL );
 
-		#endif
+			#endif
+		}
 
 	}
 
@@ -41,33 +36,18 @@ vec3 normal = normalize( transformedNormal );
 
 	for ( int i = 0; i < MAX_SPOT_LIGHTS; i ++ ) {
 
-		vec3 lightColor = spotLightColor[ i ];
-
-		vec3 lightPosition = spotLightPosition[ i ];
-		vec3 lVector = lightPosition - mvPosition.xyz;
-		vec3 lightDir = normalize( lVector );
-
-		float spotEffect = dot( spotLightDirection[ i ], lightDir );
-
-		if ( spotEffect > spotLightAngleCos[ i ] ) {
-
-			spotEffect = saturate( pow( saturate( spotEffect ), spotLightExponent[ i ] ) );
-
-			// attenuation
+		vec3 lightDir, lightIntensity;
+		getSpotLight( spotLights[i], -vViewPosition, lightDir, lightIntensity );
 
-			float attenuation = calcLightAttenuation( length( lVector ), spotLightDistance[ i ], spotLightDecay[ i ] );
+		if( dot( lightIntensity, lightIntensity ) > 0.0 ) {
 
-			attenuation *= spotEffect;
+			float dotNL = saturate( dot( normal, lightDir ) );
 
-			// diffuse
-
-			float dotProduct = dot( normal, lightDir );
-
-			vLightFront += lightColor * attenuation * saturate( dotProduct );
+			vLightFront += lightIntensity * saturate( dotNL );
 
 			#ifdef DOUBLE_SIDED
 
-				vLightBack += lightColor * attenuation * saturate( - dotProduct );
+				vLightBack += lightIntensity * saturate( - dotNL );
 
 			#endif
 
@@ -81,21 +61,22 @@ vec3 normal = normalize( transformedNormal );
 
 	for ( int i = 0; i < MAX_DIR_LIGHTS; i ++ ) {
 
-		vec3 lightColor = directionalLightColor[ i ];
+		vec3 lightDir, lightIntensity;
+		getDirLightDirect( directionalLights[i], lightDir, lightIntensity );
 
-		vec3 lightDir = directionalLightDirection[ i ];
+		if( dot( lightIntensity, lightIntensity ) > 0.0 ) {
 
-		// diffuse
+			float dotNL = dot( normal, lightDir );
 
-		float dotProduct = dot( normal, lightDir );
+			vLightFront += lightIntensity * saturate( dotNL );
 
-		vLightFront += lightColor * saturate( dotProduct );
+			#ifdef DOUBLE_SIDED
 
-		#ifdef DOUBLE_SIDED
+				vLightBack += lightIntensity * saturate( - dotNL );
 
-			vLightBack += lightColor * saturate( - dotProduct );
+			#endif
 
-		#endif
+		}
 
 	}
 
@@ -105,21 +86,11 @@ vec3 normal = normalize( transformedNormal );
 
 	for ( int i = 0; i < MAX_HEMI_LIGHTS; i ++ ) {
 
-		vec3 lightDir = hemisphereLightDirection[ i ];
-
-		// diffuse
-
-		float dotProduct = dot( normal, lightDir );
-
-		float hemiDiffuseWeight = 0.5 * dotProduct + 0.5;
-
-		vLightFront += mix( hemisphereLightGroundColor[ i ], hemisphereLightSkyColor[ i ], hemiDiffuseWeight );
+		vLightFront += getHemisphereLightIndirect( hemisphereLights[ i ], normal );
 
 		#ifdef DOUBLE_SIDED
-
-			float hemiDiffuseWeightBack = - 0.5 * dotProduct + 0.5;
-
-			vLightBack += mix( hemisphereLightGroundColor[ i ], hemisphereLightSkyColor[ i ], hemiDiffuseWeightBack );
+	
+			vLightBack += getHemisphereLightIndirect( hemisphereLights[ i ], -normal );
 
 		#endif
 

+ 3 - 3
src/renderers/shaders/ShaderChunk/lights_phong_fragment.glsl

@@ -15,7 +15,7 @@ vec3 diffuse = diffuseColor.rgb;
 	for ( int i = 0; i < MAX_POINT_LIGHTS; i ++ ) {
 
 		vec3 lightDir, lightIntensity;
-		getPointLight( pointLights[i], lightDir, lightIntensity );
+		getPointLightDirect( pointLights[i], -vViewPosition, lightDir, lightIntensity );
 
 		if( dot( lightIntensity, lightIntensity ) > 0.0 ) {
 
@@ -41,7 +41,7 @@ vec3 diffuse = diffuseColor.rgb;
 	for ( int i = 0; i < MAX_SPOT_LIGHTS; i ++ ) {
 
 		vec3 lightDir, lightIntensity;
-		getSpotLight( spotLights[i], lightDir, lightIntensity );
+		getSpotLightDirect( spotLights[i], -vViewPosition, lightDir, lightIntensity );
 
 		if( dot( lightIntensity, lightIntensity ) > 0.0 ) {
 
@@ -66,7 +66,7 @@ vec3 diffuse = diffuseColor.rgb;
 	for( int i = 0; i < MAX_DIR_LIGHTS; i ++ ) {
 
 		vec3 lightDir, lightIntensity;
-		getDirLight( directionalLights[i], lightDir, lightIntensity );
+		getDirLightDirect( directionalLights[i], lightDir, lightIntensity );
 
 		if( dot( lightIntensity, lightIntensity ) > 0.0 ) {
 

+ 0 - 95
src/renderers/shaders/ShaderChunk/lights_phong_pars_fragment.glsl

@@ -14,98 +14,3 @@ varying vec3 vViewPosition;
 
 #endif
 
-
-#if MAX_DIR_LIGHTS > 0
-
-	struct DirectionalLight {
-	  vec3 direction;
-	  vec3 color;
-	};
-
-	uniform DirectionalLight singleTestDirLight;
-
-	uniform DirectionalLight directionalLights[ MAX_DIR_LIGHTS ];
-
-	void getDirLight( const in DirectionalLight directionalLight, out vec3 lightDir, out vec3 lightColor ) { 
-	
-		lightDir = directionalLight.direction; 
-		lightColor = directionalLight.color;
-
-	}
-
-#endif
-
-#if MAX_HEMI_LIGHTS > 0
-
-	struct HemisphereLight {
-	  vec3 direction;
-	  vec3 skyColor;
-	  vec3 groundColor;
-	};
-
-	uniform HemisphereLight hemisphereLights[ MAX_HEMI_LIGHTS ];
-
-#endif
-
-#if MAX_POINT_LIGHTS > 0
-
-	struct PointLight {
-	  vec3 position;
-	  vec3 color;
-	  float distance;
-	  float decay;
-	};
-
-	uniform PointLight singleTestPointLight;
-
-	uniform PointLight pointLights[ MAX_POINT_LIGHTS ];
-
-	void getPointLight( const in PointLight pointLight, out vec3 lightDir, out vec3 lightColor ) { 
-	
-		vec3 lightPosition = pointLight.position; 
-	
-		vec3 lVector = lightPosition + vViewPosition.xyz; 
-		lightDir = normalize( lVector ); 
-	
-		lightColor = pointLight.color; 
-		lightColor *= calcLightAttenuation( length( lVector ), pointLight.distance, pointLight.decay ); 
-	
-	}
-
-#endif
-
-#if MAX_SPOT_LIGHTS > 0
-
-	struct SpotLight {
-	  vec3 position;
-	  vec3 direction;
-	  vec3 color;
-	  float distance;
-	  float decay;
-	  float angleCos;
-	  float exponent;
-	};
-
-	uniform SpotLight spotLights[ MAX_SPOT_LIGHTS ];
-
-	void getSpotLight( const in SpotLight spotLight, out vec3 lightDir, out vec3 lightColor ) {
-	
-		vec3 lightPosition = spotLight.position;
-	
-		vec3 lVector = lightPosition + vViewPosition.xyz;
-		lightDir = normalize( lVector );
-	
-		float spotEffect = dot( spotLight.direction, lightDir );
-		spotEffect = saturate( pow( saturate( spotEffect ), spotLight.exponent ) );
-	
-		lightColor = spotLight.color;
-		lightColor *= ( spotEffect * calcLightAttenuation( length( lVector ), spotLight.distance, spotLight.decay ) );
-
-	}
-
-#endif
-
-
-
-
-