Browse Source

further cleanup to simplify usage, avoiding premature optimization.

Ben Houston 9 years ago
parent
commit
e21dd086f8

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

@@ -4,7 +4,7 @@
 #define RECIPROCAL_PI2 0.15915494
 #define LOG2 1.442695
 #define EPSILON 1e-6
-
+#define PHYSICALLY_BASED_RENDERING
 #define saturate(a) clamp( a, 0.0, 1.0 )
 #define whiteCompliment(a) ( 1.0 - saturate( a ) )
 
@@ -82,13 +82,13 @@ vec3 linearToOutput( in vec3 a ) {
 }
 
 
-vec3 BRDF_Lambert( in vec3 diffuseColor, in float dotLN ) {
+vec3 BRDF_Lambert( const in vec3 incomingLight, const in vec3 diffuseColor, const in vec3 normal, const in vec3 lightDir ) {
 
-	return diffuseColor * ( dotLN * RECIPROCAL_PI );
+	return incomingLight * diffuseColor * ( saturate( dot( normal, lightDir ) ) * RECIPROCAL_PI );
 
 }
 
-vec3 F_Schlick( in vec3 specularColor, in float dotLH ) {
+vec3 F_Schlick( const in vec3 F0, const in float dotLH ) {
 
 	// Original approximation by Christophe Schlick '94
 	//;float fresnel = pow( 1.0 - dotLH, 5.0 );
@@ -96,7 +96,7 @@ vec3 F_Schlick( in vec3 specularColor, in float dotLH ) {
 	// Optimized variant (presented by Epic at SIGGRAPH '13)
 	float fresnel = exp2( ( -5.55437 * dotLH - 6.98316 ) * dotLH );
 
-	return ( 1.0 - specularColor ) * fresnel + specularColor;
+	return F0 + ( 1.0 - F0 ) * fresnel;
 
 }
 
@@ -108,7 +108,7 @@ float G_BlinnPhong_Implicit( /* in float dotNL, in float dotNV */ ) {
 
 }
 
-float D_BlinnPhong( in float shininess, in float dotNH ) {
+float D_BlinnPhong( const in float shininess, const in float dotNH ) {
 
 	// factor of 1/PI in distribution term omitted
 
@@ -116,13 +116,17 @@ float D_BlinnPhong( in float shininess, in float dotNH ) {
 
 }
 
-vec3 BRDF_BlinnPhong( in vec3 specularColor, in float shininess, in float dotNH, in float dotLH ) {
+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 halfDir = normalize( lightDir + viewDir ) * singleTestPointLight.distance;
+	float dotNH = saturate( dot( normal, halfDir ) );
+	float dotLH = saturate( dot( lightDir, halfDir ) );
 
 	vec3 F = F_Schlick( specularColor, dotLH );
 	float G = G_BlinnPhong_Implicit( /* dotNL, dotNV */ );
 	float D = D_BlinnPhong( shininess, dotNH );
 
-	return F * G * D;
+	return incomingLight * F * ( G * D );
 
 }
 
@@ -222,6 +226,7 @@ vec3 BRDF_BlinnPhong( in vec3 specularColor, in float shininess, in float dotNH,
 		float hemiDiffuseWeight = 0.5 * dotNL + 0.5;
 
 		return mix( hemiLight.groundColor, hemiLight.skyColor, hemiDiffuseWeight );
+
 	}
 
 #endif

+ 17 - 34
src/renderers/shaders/ShaderChunk/lights_lambert_vertex.glsl

@@ -12,21 +12,16 @@ vec3 normal = normalize( transformedNormal );
 
 	for ( int i = 0; i < MAX_POINT_LIGHTS; i ++ ) {
 
-		vec3 lightDir, lightIntensity;
-		getPointLight( pointLights[i], -vViewPosition, lightDir, lightIntensity );
-
-		if( dot( lightIntensity, lightIntensity ) > 0.0 ) {
+		vec3 lightDir, lightColor;
+		getPointLightDirect( pointLights[i], mvPosition.xyz, lightDir, lightColor );
 
-			float dotNL = dot( normal, lightDir );
+		vLightFront += BRDF_Lambert( lightColor, vec3( 1.0 ), normal, lightDir );
 
-			vLightFront += lightIntensity * saturate( dotNL );
-
-			#ifdef DOUBLE_SIDED
+		#ifdef DOUBLE_SIDED
 
-				vLightBack += lightIntensity * saturate( - dotNL );
+			vLightBack += BRDF_Lambert( lightColor, vec3( 1.0 ), -normal, lightDir );
 
-			#endif
-		}
+		#endif
 
 	}
 
@@ -37,21 +32,15 @@ vec3 normal = normalize( transformedNormal );
 	for ( int i = 0; i < MAX_SPOT_LIGHTS; i ++ ) {
 
 		vec3 lightDir, lightIntensity;
-		getSpotLight( spotLights[i], -vViewPosition, lightDir, lightIntensity );
-
-		if( dot( lightIntensity, lightIntensity ) > 0.0 ) {
-
-			float dotNL = saturate( dot( normal, lightDir ) );
-
-			vLightFront += lightIntensity * saturate( dotNL );
+		getSpotLightDirect( spotLights[i], mvPosition.xyz, lightDir, lightIntensity );
 
-			#ifdef DOUBLE_SIDED
+		vLightFront += BRDF_Lambert( lightColor, vec3( 1.0 ), normal, lightDir );
 
-				vLightBack += lightIntensity * saturate( - dotNL );
+		#ifdef DOUBLE_SIDED
 
-			#endif
+			vLightBack += BRDF_Lambert( lightColor, vec3( 1.0 ), -normal, lightDir );
 
-		}
+		#endif
 
 	}
 
@@ -64,19 +53,13 @@ vec3 normal = normalize( transformedNormal );
 		vec3 lightDir, lightIntensity;
 		getDirLightDirect( directionalLights[i], lightDir, lightIntensity );
 
-		if( dot( lightIntensity, lightIntensity ) > 0.0 ) {
-
-			float dotNL = dot( normal, lightDir );
-
-			vLightFront += lightIntensity * saturate( dotNL );
+		vLightFront += BRDF_Lambert( lightColor, vec3( 1.0 ), normal, lightDir );
 
-			#ifdef DOUBLE_SIDED
-
-				vLightBack += lightIntensity * saturate( - dotNL );
+		#ifdef DOUBLE_SIDED
 
-			#endif
+			vLightBack += BRDF_Lambert( lightColor, vec3( 1.0 ), -normal, lightDir );
 
-		}
+		#endif
 
 	}
 
@@ -86,11 +69,11 @@ vec3 normal = normalize( transformedNormal );
 
 	for ( int i = 0; i < MAX_HEMI_LIGHTS; i ++ ) {
 
-		vLightFront += getHemisphereLightIndirect( hemisphereLights[ i ], normal );
+		vLightFront += getHemisphereLightIndirect( hemisphereLights[ i ], normal ) * RECIPROCAL_PI;
 
 		#ifdef DOUBLE_SIDED
 	
-			vLightBack += getHemisphereLightIndirect( hemisphereLights[ i ], -normal );
+			vLightBack += getHemisphereLightIndirect( hemisphereLights[ i ], -normal ) * RECIPROCAL_PI;
 
 		#endif
 

+ 13 - 44
src/renderers/shaders/ShaderChunk/lights_phong_fragment.glsl

@@ -14,23 +14,12 @@ vec3 diffuse = diffuseColor.rgb;
 
 	for ( int i = 0; i < MAX_POINT_LIGHTS; i ++ ) {
 
-		vec3 lightDir, lightIntensity;
-		getPointLightDirect( pointLights[i], -vViewPosition, lightDir, lightIntensity );
-
-		if( dot( lightIntensity, lightIntensity ) > 0.0 ) {
-
+		vec3 lightDir, lightColor;
+		getPointLightDirect( pointLights[i], -vViewPosition, lightDir, lightColor );
 
-			vec3 halfDir = normalize( lightDir + viewDir ) * singleTestPointLight.distance;
-			float dotNL = saturate( dot( normal, lightDir ) );
-			float dotNH = saturate( dot( normal, halfDir ) );
-			float dotLH = saturate( dot( lightDir, halfDir ) );
-
-			totalReflectedLight += (
-				BRDF_Lambert( diffuse, dotNL ) +
-				BRDF_BlinnPhong( specular, shininess, dotNH, dotLH )
-				) * lightIntensity;
-
-		}
+		totalReflectedLight +=
+			BRDF_Lambert( lightColor, diffuse, normal, lightDir ) +
+			BRDF_BlinnPhong( lightColor, specular, shininess, normal, lightDir, viewDir );
 
 	}
 
@@ -40,22 +29,12 @@ vec3 diffuse = diffuseColor.rgb;
 
 	for ( int i = 0; i < MAX_SPOT_LIGHTS; i ++ ) {
 
-		vec3 lightDir, lightIntensity;
-		getSpotLightDirect( spotLights[i], -vViewPosition, lightDir, lightIntensity );
-
-		if( dot( lightIntensity, lightIntensity ) > 0.0 ) {
-
-			vec3 halfDir = normalize( lightDir + viewDir );
-			float dotNL = saturate( dot( normal, lightDir ) );
-			float dotNH = saturate( dot( normal, halfDir ) );
-			float dotLH = saturate( dot( lightDir, halfDir ) );
-
-			totalReflectedLight += (
-				BRDF_Lambert( diffuse, dotNL ) +
-				BRDF_BlinnPhong( specular, shininess, dotNH, dotLH )
-				) * lightIntensity;
+		vec3 lightDir, lightColor;
+		getSpotLightDirect( spotLights[i], -vViewPosition, lightDir, lightColor );
 
-		}
+		totalReflectedLight +=
+			BRDF_Lambert( lightColor, diffuse, normal, lightDir ) +
+			BRDF_BlinnPhong( lightColor, specular, shininess, normal, lightDir, viewDir );
 
 	}
 
@@ -68,19 +47,9 @@ vec3 diffuse = diffuseColor.rgb;
 		vec3 lightDir, lightIntensity;
 		getDirLightDirect( directionalLights[i], lightDir, lightIntensity );
 
-		if( dot( lightIntensity, lightIntensity ) > 0.0 ) {
-
-			vec3 halfDir = normalize( lightDir + viewDir );
-			float dotNL = saturate( dot( normal, lightDir ) );
-			float dotNH = saturate( dot( normal, halfDir ) );
-			float dotLH = saturate( dot( lightDir, halfDir ) );
-
-			totalReflectedLight += (
-				BRDF_Lambert( diffuse, dotNL ) +
-				BRDF_BlinnPhong( specular, shininess, dotNH, dotLH )
-				) * lightIntensity;
-
-		}
+		totalReflectedLight +=
+			BRDF_Lambert( lightColor, diffuse, normal, lightDir ) +
+			BRDF_BlinnPhong( lightColor, specular, shininess, normal, lightDir, viewDir );
 
 	}
 

+ 1 - 1
src/renderers/shaders/UniformsLib.js

@@ -91,7 +91,7 @@ THREE.UniformsLib = {
 		"pointLights" : { type: "sa", value: [], properties: {
 			"color": { type: "c" },
 			"position": { type: "v3" },
-			"direction": { type: "v3" },
+			"decay": { type: "f" },
 			"distance": { type: "f" }
 		} },