Browse Source

separation of light accumulators into diffuse/specular, direct/indirect

Ben Houston 9 years ago
parent
commit
faa1967ef4

+ 13 - 12
src/renderers/shaders/ShaderChunk/common.glsl

@@ -82,10 +82,11 @@ vec3 linearToOutput( in vec3 a ) {
 }
 }
 
 
 
 
-vec3 BRDF_Lambert( const in vec3 incomingLight, const in vec3 diffuseColor, const in vec3 normal, const in vec3 lightDir ) {
+vec3 BRDF_Lambert( const in vec3 lightColor, const in vec3 lightDir, const in vec3 normal, const in vec3 diffuseColor ) {
 
 
-	return incomingLight * diffuseColor * ( saturate( dot( normal, lightDir ) ) * RECIPROCAL_PI );
+	return lightColor * diffuseColor * ( saturate( dot( normal, lightDir ) ) );
 
 
+	// the above should be scaled by '' * RECIPROCAL_PI'
 }
 }
 
 
 vec3 F_Schlick( const in vec3 F0, const in float dotLH ) {
 vec3 F_Schlick( const in vec3 F0, const in float dotLH ) {
@@ -116,9 +117,9 @@ float D_BlinnPhong( const in float shininess, const in float dotNH ) {
 
 
 }
 }
 
 
-vec3 BRDF_BlinnPhong( const in vec3 incomingLight, const in vec3 specularColor, const in float shininess, const in vec3 normal, const in vec3 lightDir, const in vec3 viewDir ) {
+vec3 BRDF_BlinnPhong( const in vec3 lightColor, const in vec3 lightDir, const in vec3 normal, const in vec3 viewDir, const in vec3 specularColor, const in float shininess ) {
 
 
-	vec3 halfDir = normalize( lightDir + viewDir ) * singleTestPointLight.distance;
+	vec3 halfDir = normalize( lightDir + viewDir );
 	float dotNH = saturate( dot( normal, halfDir ) );
 	float dotNH = saturate( dot( normal, halfDir ) );
 	float dotLH = saturate( dot( lightDir, halfDir ) );
 	float dotLH = saturate( dot( lightDir, halfDir ) );
 
 
@@ -126,7 +127,7 @@ vec3 BRDF_BlinnPhong( const in vec3 incomingLight, const in vec3 specularColor,
 	float G = G_BlinnPhong_Implicit( /* dotNL, dotNV */ );
 	float G = G_BlinnPhong_Implicit( /* dotNL, dotNV */ );
 	float D = D_BlinnPhong( shininess, dotNH );
 	float D = D_BlinnPhong( shininess, dotNH );
 
 
-	return incomingLight * F * ( G * D );
+	return lightColor * F * ( G * D );
 
 
 }
 }
 
 
@@ -141,7 +142,7 @@ vec3 BRDF_BlinnPhong( const in vec3 incomingLight, const in vec3 specularColor,
 
 
 	uniform DirectionalLight directionalLights[ MAX_DIR_LIGHTS ];
 	uniform DirectionalLight directionalLights[ MAX_DIR_LIGHTS ];
 
 
-	void getDirLightDirect( const in DirectionalLight directionalLight, out vec3 lightDir, out vec3 lightColor ) { 
+	void getDirLightDirect( const in DirectionalLight directionalLight, out vec3 lightColor, out vec3 lightDir ) { 
 	
 	
 		lightDir = directionalLight.direction; 
 		lightDir = directionalLight.direction; 
 		lightColor = directionalLight.color;
 		lightColor = directionalLight.color;
@@ -159,11 +160,9 @@ vec3 BRDF_BlinnPhong( const in vec3 incomingLight, const in vec3 specularColor,
 	  float decay;
 	  float decay;
 	};
 	};
 
 
-	uniform PointLight singleTestPointLight;
-
 	uniform PointLight pointLights[ MAX_POINT_LIGHTS ];
 	uniform PointLight pointLights[ MAX_POINT_LIGHTS ];
 
 
-	void getPointLightDirect( const in PointLight pointLight, const in vec3 position, out vec3 lightDir, out vec3 lightColor ) { 
+	void getPointLightDirect( const in PointLight pointLight, const in vec3 position, out vec3 lightColor, out vec3 lightDir ) { 
 	
 	
 		vec3 lightPosition = pointLight.position; 
 		vec3 lightPosition = pointLight.position; 
 	
 	
@@ -191,7 +190,7 @@ vec3 BRDF_BlinnPhong( const in vec3 incomingLight, const in vec3 specularColor,
 
 
 	uniform SpotLight spotLights[ MAX_SPOT_LIGHTS ];
 	uniform SpotLight spotLights[ MAX_SPOT_LIGHTS ];
 
 
-	void getSpotLightDirect( const in SpotLight spotLight, const in vec3 position, out vec3 lightDir, out vec3 lightColor ) {
+	void getSpotLightDirect( const in SpotLight spotLight, const in vec3 position, out vec3 lightColor, out vec3 lightDir ) {
 	
 	
 		vec3 lightPosition = spotLight.position;
 		vec3 lightPosition = spotLight.position;
 	
 	
@@ -219,13 +218,15 @@ vec3 BRDF_BlinnPhong( const in vec3 incomingLight, const in vec3 specularColor,
 
 
 	uniform HemisphereLight hemisphereLights[ MAX_HEMI_LIGHTS ];
 	uniform HemisphereLight hemisphereLights[ MAX_HEMI_LIGHTS ];
 
 
-	vec3 getHemisphereLightIndirect( const in HemisphereLight hemiLight, in vec3 normal ) { 
+	void getHemisphereLightIndirect( const in HemisphereLight hemiLight, const in vec3 normal, out vec3 lightColor, out vec3 lightDir ) { 
 	
 	
 		float dotNL = dot( normal, hemiLight.direction );
 		float dotNL = dot( normal, hemiLight.direction );
 
 
 		float hemiDiffuseWeight = 0.5 * dotNL + 0.5;
 		float hemiDiffuseWeight = 0.5 * dotNL + 0.5;
 
 
-		return mix( hemiLight.groundColor, hemiLight.skyColor, hemiDiffuseWeight );
+		lightColor = mix( hemiLight.groundColor, hemiLight.skyColor, hemiDiffuseWeight );
+
+		lightDir = normal;
 
 
 	}
 	}
 
 

+ 0 - 18
src/renderers/shaders/ShaderChunk/hemilight_fragment.glsl

@@ -1,18 +0,0 @@
-#if MAX_HEMI_LIGHTS > 0
-
-	for( int i = 0; i < MAX_HEMI_LIGHTS; i ++ ) {
-
-		vec3 lightDir = hemisphereLightDirection[ i ];
-
-		float dotProduct = dot( normal, lightDir );
-
-		float hemiDiffuseWeight = 0.5 * dotProduct + 0.5;
-
-		vec3 lightColor = mix( hemisphereLightGroundColor[ i ], hemisphereLightSkyColor[ i ], hemiDiffuseWeight );
-
-		totalAmbientLight += lightColor;
-
-	}
-
-#endif
-

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

@@ -7,19 +7,20 @@ vLightFront = vec3( 0.0 );
 #endif
 #endif
 
 
 vec3 normal = normalize( transformedNormal );
 vec3 normal = normalize( transformedNormal );
+vec3 diffuse = vec3( 1.0 );
 
 
 #if MAX_POINT_LIGHTS > 0
 #if MAX_POINT_LIGHTS > 0
 
 
 	for ( int i = 0; i < MAX_POINT_LIGHTS; i ++ ) {
 	for ( int i = 0; i < MAX_POINT_LIGHTS; i ++ ) {
 
 
-		vec3 lightDir, lightColor;
-		getPointLightDirect( pointLights[i], mvPosition.xyz, lightDir, lightColor );
+		vec3 lightColor, lightDir;
+		getPointLightDirect( pointLights[i], mvPosition.xyz, lightColor, lightDir );
 
 
-		vLightFront += BRDF_Lambert( lightColor, vec3( 1.0 ), normal, lightDir );
+		vLightFront += BRDF_Lambert( lightColor, lightDir, normal, diffuse );
 
 
 		#ifdef DOUBLE_SIDED
 		#ifdef DOUBLE_SIDED
 
 
-			vLightBack += BRDF_Lambert( lightColor, vec3( 1.0 ), -normal, lightDir );
+			vLightBack += BRDF_Lambert( lightColor, lightDir, -normal, diffuse );
 
 
 		#endif
 		#endif
 
 
@@ -31,14 +32,14 @@ vec3 normal = normalize( transformedNormal );
 
 
 	for ( int i = 0; i < MAX_SPOT_LIGHTS; i ++ ) {
 	for ( int i = 0; i < MAX_SPOT_LIGHTS; i ++ ) {
 
 
-		vec3 lightDir, lightIntensity;
-		getSpotLightDirect( spotLights[i], mvPosition.xyz, lightDir, lightIntensity );
+		vec3 lightColor, lightDir;
+		getSpotLightDirect( spotLights[i], mvPosition.xyz, lightColor, lightDir );
 
 
-		vLightFront += BRDF_Lambert( lightColor, vec3( 1.0 ), normal, lightDir );
+		vLightFront += BRDF_Lambert( lightColor, lightDir, normal, diffuse );
 
 
 		#ifdef DOUBLE_SIDED
 		#ifdef DOUBLE_SIDED
 
 
-			vLightBack += BRDF_Lambert( lightColor, vec3( 1.0 ), -normal, lightDir );
+			vLightBack += BRDF_Lambert( lightColor, lightDir, -normal, diffuse );
 
 
 		#endif
 		#endif
 
 
@@ -50,14 +51,14 @@ vec3 normal = normalize( transformedNormal );
 
 
 	for ( int i = 0; i < MAX_DIR_LIGHTS; i ++ ) {
 	for ( int i = 0; i < MAX_DIR_LIGHTS; i ++ ) {
 
 
-		vec3 lightDir, lightIntensity;
-		getDirLightDirect( directionalLights[i], lightDir, lightIntensity );
+		vec3 lightColor, lightDir;
+		getDirLightDirect( directionalLights[i], lightColor, lightDir );
 
 
-		vLightFront += BRDF_Lambert( lightColor, vec3( 1.0 ), normal, lightDir );
+		vLightFront += BRDF_Lambert( lightColor, lightDir, normal, diffuse );
 
 
 		#ifdef DOUBLE_SIDED
 		#ifdef DOUBLE_SIDED
 
 
-			vLightBack += BRDF_Lambert( lightColor, vec3( 1.0 ), -normal, lightDir );
+			vLightBack += BRDF_Lambert( lightColor, lightDir, -normal, diffuse );
 
 
 		#endif
 		#endif
 
 
@@ -69,11 +70,16 @@ vec3 normal = normalize( transformedNormal );
 
 
 	for ( int i = 0; i < MAX_HEMI_LIGHTS; i ++ ) {
 	for ( int i = 0; i < MAX_HEMI_LIGHTS; i ++ ) {
 
 
-		vLightFront += getHemisphereLightIndirect( hemisphereLights[ i ], normal ) * RECIPROCAL_PI;
+		vec3 lightColor, lightDir;
+		getHemisphereLightIndirect( hemisphereLights[ i ], normal, lightColor, lightDir );
+
+		vLightFront += BRDF_Lambert( lightColor, lightDir, normal, diffuse );
 
 
 		#ifdef DOUBLE_SIDED
 		#ifdef DOUBLE_SIDED
 	
 	
-			vLightBack += getHemisphereLightIndirect( hemisphereLights[ i ], -normal ) * RECIPROCAL_PI;
+			getHemisphereLightIndirect( hemisphereLights[ i ], -normal, lightColor, lightDir );
+
+			vLightBack += BRDF_Lambert( lightColor, lightDir, -normal, diffuse );
 
 
 		#endif
 		#endif
 
 

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

@@ -1,6 +1,9 @@
 vec3 viewDir = normalize( vViewPosition );
 vec3 viewDir = normalize( vViewPosition );
 
 
-vec3 totalReflectedLight = vec3( 0.0 );
+vec3 totalDirectReflectedSpecular = vec3( 0.0 );
+vec3 totalDirectReflectedDiffuse = vec3( 0.0 );
+vec3 totalIndirectReflectedSpecular = vec3( 0.0 );
+vec3 totalIndirectReflectedDiffuse = vec3( 0.0 );
 
 
 vec3 diffuse = diffuseColor.rgb;
 vec3 diffuse = diffuseColor.rgb;
 
 
@@ -14,12 +17,14 @@ vec3 diffuse = diffuseColor.rgb;
 
 
 	for ( int i = 0; i < MAX_POINT_LIGHTS; i ++ ) {
 	for ( int i = 0; i < MAX_POINT_LIGHTS; i ++ ) {
 
 
-		vec3 lightDir, lightColor;
-		getPointLightDirect( pointLights[i], -vViewPosition, lightDir, lightColor );
+		vec3 lightColor, lightDir;
+		getPointLightDirect( pointLights[i], -vViewPosition, lightColor, lightDir );
 
 
-		totalReflectedLight +=
-			BRDF_Lambert( lightColor, diffuse, normal, lightDir ) +
-			BRDF_BlinnPhong( lightColor, specular, shininess, normal, lightDir, viewDir );
+		totalDirectReflectedDiffuse +=
+			BRDF_Lambert( lightColor, lightDir, normal, diffuse );
+
+		totalDirectReflectedSpecular +=
+			BRDF_BlinnPhong( lightColor, lightDir, normal, viewDir, specular, shininess );
 
 
 	}
 	}
 
 
@@ -29,12 +34,14 @@ vec3 diffuse = diffuseColor.rgb;
 
 
 	for ( int i = 0; i < MAX_SPOT_LIGHTS; i ++ ) {
 	for ( int i = 0; i < MAX_SPOT_LIGHTS; i ++ ) {
 
 
-		vec3 lightDir, lightColor;
-		getSpotLightDirect( spotLights[i], -vViewPosition, lightDir, lightColor );
+		vec3 lightColor, lightDir;
+		getSpotLightDirect( spotLights[i], -vViewPosition, lightColor, lightDir );
+
+		totalDirectReflectedDiffuse +=
+			BRDF_Lambert( lightColor, lightDir, normal, diffuse );
 
 
-		totalReflectedLight +=
-			BRDF_Lambert( lightColor, diffuse, normal, lightDir ) +
-			BRDF_BlinnPhong( lightColor, specular, shininess, normal, lightDir, viewDir );
+		totalDirectReflectedSpecular +=
+			BRDF_BlinnPhong( lightColor, lightDir, normal, viewDir, specular, shininess );
 
 
 	}
 	}
 
 
@@ -44,15 +51,37 @@ vec3 diffuse = diffuseColor.rgb;
 
 
 	for( int i = 0; i < MAX_DIR_LIGHTS; i ++ ) {
 	for( int i = 0; i < MAX_DIR_LIGHTS; i ++ ) {
 
 
-		vec3 lightDir, lightIntensity;
-		getDirLightDirect( directionalLights[i], lightDir, lightIntensity );
+		vec3 lightColor, lightDir;
+		getDirLightDirect( directionalLights[i], lightColor, lightDir );
+
+		totalDirectReflectedDiffuse +=
+			BRDF_Lambert( lightColor, lightDir, normal, diffuse );
+
+		totalDirectReflectedSpecular +=
+			BRDF_BlinnPhong( lightColor, lightDir, normal, viewDir, specular, shininess );
+
+	}
+
+#endif
+
+#if MAX_HEMI_LIGHTS > 0
 
 
-		totalReflectedLight +=
-			BRDF_Lambert( lightColor, diffuse, normal, lightDir ) +
-			BRDF_BlinnPhong( lightColor, specular, shininess, normal, lightDir, viewDir );
+	for( int i = 0; i < MAX_HEMI_LIGHTS; i ++ ) {
+
+		vec3 lightColor, lightDir;
+		getHemisphereLightIndirect( hemisphereLights[ i ], normal, lightColor, lightDir );
+
+		totalIndirectReflectedDiffuse +=
+			BRDF_Lambert( lightColor, lightDir, normal, diffuse );
 
 
 	}
 	}
 
 
 #endif
 #endif
 
 
-outgoingLight += totalReflectedLight + totalEmissiveLight;
+
+outgoingLight +=
+	totalDirectReflectedDiffuse +
+	totalDirectReflectedSpecular +
+	totalIndirectReflectedDiffuse +
+	totalIndirectReflectedSpecular +
+	totalEmissiveLight;

+ 0 - 2
src/renderers/shaders/ShaderLib.js

@@ -143,7 +143,6 @@ THREE.ShaderLib = {
 			THREE.ShaderChunk[ "uv2_pars_vertex" ],
 			THREE.ShaderChunk[ "uv2_pars_vertex" ],
 			THREE.ShaderChunk[ "envmap_pars_vertex" ],
 			THREE.ShaderChunk[ "envmap_pars_vertex" ],
 			THREE.ShaderChunk[ "lights_lambert_pars_vertex" ],
 			THREE.ShaderChunk[ "lights_lambert_pars_vertex" ],
-			THREE.ShaderChunk[ "lights_pars" ],
 			THREE.ShaderChunk[ "color_pars_vertex" ],
 			THREE.ShaderChunk[ "color_pars_vertex" ],
 			THREE.ShaderChunk[ "morphtarget_pars_vertex" ],
 			THREE.ShaderChunk[ "morphtarget_pars_vertex" ],
 			THREE.ShaderChunk[ "skinning_pars_vertex" ],
 			THREE.ShaderChunk[ "skinning_pars_vertex" ],
@@ -348,7 +347,6 @@ THREE.ShaderLib = {
 			THREE.ShaderChunk[ "envmap_pars_fragment" ],
 			THREE.ShaderChunk[ "envmap_pars_fragment" ],
 			THREE.ShaderChunk[ "fog_pars_fragment" ],
 			THREE.ShaderChunk[ "fog_pars_fragment" ],
 			THREE.ShaderChunk[ "lights_phong_pars_fragment" ],
 			THREE.ShaderChunk[ "lights_phong_pars_fragment" ],
-			THREE.ShaderChunk[ "lights_pars" ],
 			THREE.ShaderChunk[ "shadowmap_pars_fragment" ],
 			THREE.ShaderChunk[ "shadowmap_pars_fragment" ],
 			THREE.ShaderChunk[ "bumpmap_pars_fragment" ],
 			THREE.ShaderChunk[ "bumpmap_pars_fragment" ],
 			THREE.ShaderChunk[ "normalmap_pars_fragment" ],
 			THREE.ShaderChunk[ "normalmap_pars_fragment" ],