Browse Source

Mild glsl improvements

Remove unneeded function calls, rearrage for fused multiply–add, remove
excessive shader whitespace
Ben Adams 10 years ago
parent
commit
50adbaa5c1

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

@@ -4,26 +4,6 @@
 #define LOG2 1.442695
 #define LOG2 1.442695
 #define EPSILON 1e-6
 #define EPSILON 1e-6
 
 
-float square( in float a ) { return a * a; }
-vec2  square( in vec2 a )  { return vec2( a.x * a.x, a.y * a.y ); }
-vec3  square( in vec3 a )  { return vec3( a.x * a.x, a.y * a.y, a.z * a.z ); }
-vec4  square( in vec4 a )  { return vec4( a.x * a.x, a.y * a.y, a.z * a.z, a.w * a.w ); }
-
-float saturate( in float a ) { return clamp( a, 0.0, 1.0 ); }
-vec2  saturate( in vec2 a )  { return clamp( a, 0.0, 1.0 ); }
-vec3  saturate( in vec3 a )  { return clamp( a, 0.0, 1.0 ); }
-vec4  saturate( in vec4 a )  { return clamp( a, 0.0, 1.0 ); }
-
-float average( in float a ) { return a; }
-float average( in vec2 a )  { return ( a.x + a.y ) * 0.5; }
-float average( in vec3 a )  { return ( a.x + a.y + a.z ) / 3.0; }
-float average( in vec4 a )  { return ( a.x + a.y + a.z + a.w ) * 0.25; }
-
-float whiteCompliment( in float a ) { return saturate( 1.0 - a ); }
-vec2  whiteCompliment( in vec2 a )  { return saturate( vec2( 1.0 ) - a ); }
-vec3  whiteCompliment( in vec3 a )  { return saturate( vec3( 1.0 ) - a ); }
-vec4  whiteCompliment( in vec4 a )  { return saturate( vec4( 1.0 ) - a ); }
-
 vec3 transformDirection( in vec3 normal, in mat4 matrix ) {
 vec3 transformDirection( in vec3 normal, in mat4 matrix ) {
 
 
 	return normalize( ( matrix * vec4( normal, 0.0 ) ).xyz );
 	return normalize( ( matrix * vec4( normal, 0.0 ) ).xyz );
@@ -39,9 +19,9 @@ vec3 inverseTransformDirection( in vec3 normal, in mat4 matrix ) {
 
 
 vec3 projectOnPlane(in vec3 point, in vec3 pointOnPlane, in vec3 planeNormal ) {
 vec3 projectOnPlane(in vec3 point, in vec3 pointOnPlane, in vec3 planeNormal ) {
 
 
-	float distance = dot( planeNormal, point-pointOnPlane );
+	float distance = dot( planeNormal, point - pointOnPlane );
 
 
-	return point - distance * planeNormal;
+	return - distance * planeNormal + point;
 
 
 }
 }
 
 
@@ -53,7 +33,7 @@ float sideOfPlane( in vec3 point, in vec3 pointOnPlane, in vec3 planeNormal ) {
 
 
 vec3 linePlaneIntersect( in vec3 pointOnLine, in vec3 lineDirection, in vec3 pointOnPlane, in vec3 planeNormal ) {
 vec3 linePlaneIntersect( in vec3 pointOnLine, in vec3 lineDirection, in vec3 pointOnPlane, in vec3 planeNormal ) {
 
 
-	return pointOnLine + lineDirection * ( dot( planeNormal, pointOnPlane - pointOnLine ) / dot( planeNormal, lineDirection ) );
+	return lineDirection * ( dot( planeNormal, pointOnPlane - pointOnLine ) / dot( planeNormal, lineDirection ) ) + pointOnLine;
 
 
 }
 }
 
 
@@ -61,7 +41,7 @@ float calcLightAttenuation( float lightDistance, float cutoffDistance, float dec
 
 
 	if ( decayExponent > 0.0 ) {
 	if ( decayExponent > 0.0 ) {
 
 
-	  return pow( saturate( 1.0 - lightDistance / cutoffDistance ), decayExponent );
+	  return pow( clamp( -lightDistance / cutoffDistance + 1.0, 0.0, 1.0 ), decayExponent );
 
 
 	}
 	}
 
 
@@ -71,7 +51,7 @@ float calcLightAttenuation( float lightDistance, float cutoffDistance, float dec
 
 
 vec3 F_Schlick( in vec3 specularColor, in float dotLH ) {
 vec3 F_Schlick( in vec3 specularColor, in float dotLH ) {
 
 
-	return specularColor + ( 1.0 - specularColor ) * pow( 1.0 - dotLH, 5.0 );
+	return ( 1.0 - specularColor ) * pow( 1.0 - dotLH, 5.0 ) + specularColor;
 
 
 }
 }
 
 

+ 2 - 2
src/renderers/shaders/ShaderChunk/envmap_fragment.glsl

@@ -24,7 +24,7 @@
 	#endif
 	#endif
 
 
 	#ifdef DOUBLE_SIDED
 	#ifdef DOUBLE_SIDED
-		float flipNormal = ( -1.0 + 2.0 * float( gl_FrontFacing ) );
+		float flipNormal = ( float( gl_FrontFacing ) * 2.0 - 1.0 );
 	#else
 	#else
 		float flipNormal = 1.0;
 		float flipNormal = 1.0;
 	#endif
 	#endif
@@ -34,7 +34,7 @@
 
 
 	#elif defined( ENVMAP_TYPE_EQUIREC )
 	#elif defined( ENVMAP_TYPE_EQUIREC )
 		vec2 sampleUV;
 		vec2 sampleUV;
-		sampleUV.y = saturate( flipNormal * reflectVec.y * 0.5 + 0.5 );
+		sampleUV.y = clamp( flipNormal * reflectVec.y * 0.5 + 0.5, 0.0, 1.0 );
 		sampleUV.x = atan( flipNormal * reflectVec.z, flipNormal * reflectVec.x ) * RECIPROCAL_PI2 + 0.5;
 		sampleUV.x = atan( flipNormal * reflectVec.z, flipNormal * reflectVec.x ) * RECIPROCAL_PI2 + 0.5;
 		vec4 envColor = texture2D( envMap, sampleUV );
 		vec4 envColor = texture2D( envMap, sampleUV );
 
 

+ 1 - 2
src/renderers/shaders/ShaderChunk/fog_fragment.glsl

@@ -12,8 +12,7 @@
 
 
 	#ifdef FOG_EXP2
 	#ifdef FOG_EXP2
 
 
-		float fogFactor = exp2( - square( fogDensity ) * square( depth ) * LOG2 );
-		fogFactor = whiteCompliment( fogFactor );
+		float fogFactor = 1.0 - clamp( exp2( - fogDensity * fogDensity * depth * depth * LOG2 ), 0.0, 1.0 );
 
 
 	#else
 	#else
 
 

+ 16 - 0
src/renderers/shaders/ShaderChunk/helper_funcs.glsl

@@ -0,0 +1,16 @@
+float square( in float a ) { return a * a; }
+vec2  square( in vec2 a )  { return a * a; }
+vec3  square( in vec3 a )  { return a * a; }
+vec4  square( in vec4 a )  { return a * a; }
+float saturate( in float a ) { return clamp( a, 0.0, 1.0 ); }
+vec2  saturate( in vec2 a )  { return clamp( a, 0.0, 1.0 ); }
+vec3  saturate( in vec3 a )  { return clamp( a, 0.0, 1.0 ); }
+vec4  saturate( in vec4 a )  { return clamp( a, 0.0, 1.0 ); }
+float average( in float a ) { return a; }
+float average( in vec2 a )  { return ( a.x + a.y) * 0.5; }
+float average( in vec3 a )  { return ( a.x + a.y + a.z) / 3.0; }
+float average( in vec4 a )  { return ( a.x + a.y + a.z + a.w) * 0.25; }
+float whiteCompliment( in float a ) { return saturate( 1.0 - a ); }
+vec2  whiteCompliment( in vec2 a )  { return saturate( 1.0 - a ); }
+vec3  whiteCompliment( in vec3 a )  { return saturate( 1.0 - a ); }
+vec4  whiteCompliment( in vec4 a )  { return saturate( 1.0 - a ); }

+ 13 - 2
src/renderers/webgl/WebGLProgram.js

@@ -49,6 +49,17 @@ THREE.WebGLProgram = ( function () {
 		return attributes;
 		return attributes;
 
 
 	}
 	}
+	
+	function programArrayToString ( previousValue, currentValue, index, array ) {
+	
+		if ( currentValue !== '' && currentValue !== undefined && currentValue !== null ) {
+		
+			return previousValue + currentValue + '\n';
+			
+		}
+		
+		return previousValue;
+	}
 
 
 	return function ( renderer, code, material, parameters ) {
 	return function ( renderer, code, material, parameters ) {
 
 
@@ -261,7 +272,7 @@ THREE.WebGLProgram = ( function () {
 
 
 				''
 				''
 
 
-			].join( '\n' );
+			].reduce( programArrayToString, '' );
 
 
 			prefix_fragment = [
 			prefix_fragment = [
 
 
@@ -320,7 +331,7 @@ THREE.WebGLProgram = ( function () {
 				'uniform vec3 cameraPosition;',
 				'uniform vec3 cameraPosition;',
 				''
 				''
 
 
-			].join( '\n' );
+			].reduce( programArrayToString, '' );
 
 
 		}
 		}
 
 

+ 1 - 0
utils/build/includes/common.json

@@ -108,6 +108,7 @@
 	"src/renderers/shaders/ShaderChunk/envmap_vertex.glsl",
 	"src/renderers/shaders/ShaderChunk/envmap_vertex.glsl",
 	"src/renderers/shaders/ShaderChunk/fog_fragment.glsl",
 	"src/renderers/shaders/ShaderChunk/fog_fragment.glsl",
 	"src/renderers/shaders/ShaderChunk/fog_pars_fragment.glsl",
 	"src/renderers/shaders/ShaderChunk/fog_pars_fragment.glsl",
+	"src/renderers/shaders/ShaderChunk/helper_funcs.glsl",
 	"src/renderers/shaders/ShaderChunk/lightmap_fragment.glsl",
 	"src/renderers/shaders/ShaderChunk/lightmap_fragment.glsl",
 	"src/renderers/shaders/ShaderChunk/lightmap_pars_fragment.glsl",
 	"src/renderers/shaders/ShaderChunk/lightmap_pars_fragment.glsl",
 	"src/renderers/shaders/ShaderChunk/lights_lambert_pars_vertex.glsl",
 	"src/renderers/shaders/ShaderChunk/lights_lambert_pars_vertex.glsl",