Browse Source

Shaders code clean up.

Mr.doob 11 years ago
parent
commit
4cf4ff20d0
2 changed files with 82 additions and 73 deletions
  1. 44 41
      src/renderers/shaders/ShaderChunk.js
  2. 38 32
      src/renderers/shaders/ShaderLib.js

+ 44 - 41
src/renderers/shaders/ShaderChunk.js

@@ -406,7 +406,7 @@ THREE.ShaderChunk = {
 		"		vec2 st0 = dFdx( vUv.st );",
 		"		vec2 st1 = dFdy( vUv.st );",
 
-		"		vec3 S = normalize(  q0 * st1.t - q1 * st0.t );",
+		"		vec3 S = normalize( q0 * st1.t - q1 * st0.t );",
 		"		vec3 T = normalize( -q0 * st1.s + q1 * st0.s );",
 		"		vec3 N = normalize( surf_norm );",
 
@@ -812,7 +812,7 @@ THREE.ShaderChunk = {
 
 		"#if MAX_POINT_LIGHTS > 0",
 
-		"	vec3 pointDiffuse  = vec3( 0.0 );",
+		"	vec3 pointDiffuse = vec3( 0.0 );",
 		"	vec3 pointSpecular = vec3( 0.0 );",
 
 		"	for ( int i = 0; i < MAX_POINT_LIGHTS; i ++ ) {",
@@ -843,7 +843,7 @@ THREE.ShaderChunk = {
 
 		"		#endif",
 
-		"		pointDiffuse  += diffuse * pointLightColor[ i ] * pointDiffuseWeight * lDistance;",
+		"		pointDiffuse += diffuse * pointLightColor[ i ] * pointDiffuseWeight * lDistance;",
 
 				// specular
 
@@ -864,7 +864,7 @@ THREE.ShaderChunk = {
 
 		"#if MAX_SPOT_LIGHTS > 0",
 
-		"	vec3 spotDiffuse  = vec3( 0.0 );",
+		"	vec3 spotDiffuse = vec3( 0.0 );",
 		"	vec3 spotSpecular = vec3( 0.0 );",
 
 		"	for ( int i = 0; i < MAX_SPOT_LIGHTS; i ++ ) {",
@@ -924,7 +924,7 @@ THREE.ShaderChunk = {
 
 		"#if MAX_DIR_LIGHTS > 0",
 
-		"	vec3 dirDiffuse  = vec3( 0.0 );",
+		"	vec3 dirDiffuse = vec3( 0.0 );",
 		"	vec3 dirSpecular = vec3( 0.0 );" ,
 
 		"	for( int i = 0; i < MAX_DIR_LIGHTS; i ++ ) {",
@@ -949,38 +949,38 @@ THREE.ShaderChunk = {
 
 		"		#endif",
 
-		"		dirDiffuse  += diffuse * directionalLightColor[ i ] * dirDiffuseWeight;",
+		"		dirDiffuse += diffuse * directionalLightColor[ i ] * dirDiffuseWeight;",
 
-				// specular
+		// specular
 
 		"		vec3 dirHalfVector = normalize( dirVector + viewPosition );",
 		"		float dirDotNormalHalf = max( dot( normal, dirHalfVector ), 0.0 );",
 		"		float dirSpecularWeight = specularStrength * max( pow( dirDotNormalHalf, shininess ), 0.0 );",
 
-					/*
-				// fresnel term from skin shader
+		/*
+		// fresnel term from skin shader
 		"		const float F0 = 0.128;",
 
 		"		float base = 1.0 - dot( viewPosition, dirHalfVector );",
 		"		float exponential = pow( base, 5.0 );",
 
 		"		float fresnel = exponential + F0 * ( 1.0 - exponential );",
-				*/
+		*/
 
-				/*
-				// fresnel term from fresnel shader
+		/*
+		// fresnel term from fresnel shader
 		"		const float mFresnelBias = 0.08;",
 		"		const float mFresnelScale = 0.3;",
 		"		const float mFresnelPower = 5.0;",
 
 		"		float fresnel = mFresnelBias + mFresnelScale * pow( 1.0 + dot( normalize( -viewPosition ), normal ), mFresnelPower );",
-				*/
+		*/
 
-				// 2.0 => 2.0001 is hack to work around ANGLE bug
+		// 2.0 => 2.0001 is hack to work around ANGLE bug
 
 		"		float specularNormalization = ( shininess + 2.0001 ) / 8.0;",
 
-				//"dirSpecular += specular * directionalLightColor[ i ] * dirSpecularWeight * dirDiffuseWeight * specularNormalization * fresnel;",
+		// "		dirSpecular += specular * directionalLightColor[ i ] * dirSpecularWeight * dirDiffuseWeight * specularNormalization * fresnel;",
 
 		"		vec3 schlick = specular + vec3( 1.0 - specular ) * pow( max( 1.0 - dot( dirVector, dirHalfVector ), 0.0 ), 5.0 );",
 		"		dirSpecular += schlick * directionalLightColor[ i ] * dirSpecularWeight * dirDiffuseWeight * specularNormalization;",
@@ -992,7 +992,7 @@ THREE.ShaderChunk = {
 
 		"#if MAX_HEMI_LIGHTS > 0",
 
-		"	vec3 hemiDiffuse  = vec3( 0.0 );",
+		"	vec3 hemiDiffuse = vec3( 0.0 );",
 		"	vec3 hemiSpecular = vec3( 0.0 );" ,
 
 		"	for( int i = 0; i < MAX_HEMI_LIGHTS; i ++ ) {",
@@ -1000,7 +1000,7 @@ THREE.ShaderChunk = {
 		"		vec4 lDirection = viewMatrix * vec4( hemisphereLightDirection[ i ], 0.0 );",
 		"		vec3 lVector = normalize( lDirection.xyz );",
 
-				// diffuse
+		// diffuse
 
 		"		float dotProduct = dot( normal, lVector );",
 		"		float hemiDiffuseWeight = 0.5 * dotProduct + 0.5;",
@@ -1009,13 +1009,13 @@ THREE.ShaderChunk = {
 
 		"		hemiDiffuse += diffuse * hemiColor;",
 
-				// specular (sky light)
+		// specular (sky light)
 
 		"		vec3 hemiHalfVectorSky = normalize( lVector + viewPosition );",
 		"		float hemiDotNormalHalfSky = 0.5 * dot( normal, hemiHalfVectorSky ) + 0.5;",
 		"		float hemiSpecularWeightSky = specularStrength * max( pow( hemiDotNormalHalfSky, shininess ), 0.0 );",
 
-				// specular (ground light)
+		// specular (ground light)
 
 		"		vec3 lVectorGround = -lVector;",
 
@@ -1025,7 +1025,7 @@ THREE.ShaderChunk = {
 
 		"		float dotProductGround = dot( normal, lVectorGround );",
 
-				// 2.0 => 2.0001 is hack to work around ANGLE bug
+		// 2.0 => 2.0001 is hack to work around ANGLE bug
 
 		"		float specularNormalization = ( shininess + 2.0001 ) / 8.0;",
 
@@ -1210,10 +1210,11 @@ THREE.ShaderChunk = {
 
 		"	#endif",
 
-		"	vec4 skinned  = boneMatX * skinVertex * skinWeight.x;",
-		"	skinned      += boneMatY * skinVertex * skinWeight.y;",
-		"	skinned      += boneMatZ * skinVertex * skinWeight.z;",
-		"	skinned      += boneMatW * skinVertex * skinWeight.w;",
+		"	vec4 skinned = vec4( 0.0 )'",
+		"	skinned += boneMatX * skinVertex * skinWeight.x;",
+		"	skinned += boneMatY * skinVertex * skinWeight.y;",
+		"	skinned += boneMatZ * skinVertex * skinWeight.z;",
+		"	skinned += boneMatW * skinVertex * skinWeight.w;",
 
 		"#endif"
 
@@ -1296,10 +1297,10 @@ THREE.ShaderChunk = {
 
 		"	vec3 morphedNormal = vec3( 0.0 );",
 
-		"	morphedNormal +=  ( morphNormal0 - normal ) * morphTargetInfluences[ 0 ];",
-		"	morphedNormal +=  ( morphNormal1 - normal ) * morphTargetInfluences[ 1 ];",
-		"	morphedNormal +=  ( morphNormal2 - normal ) * morphTargetInfluences[ 2 ];",
-		"	morphedNormal +=  ( morphNormal3 - normal ) * morphTargetInfluences[ 3 ];",
+		"	morphedNormal += ( morphNormal0 - normal ) * morphTargetInfluences[ 0 ];",
+		"	morphedNormal += ( morphNormal1 - normal ) * morphTargetInfluences[ 1 ];",
+		"	morphedNormal += ( morphNormal2 - normal ) * morphTargetInfluences[ 2 ];",
+		"	morphedNormal += ( morphNormal3 - normal ) * morphTargetInfluences[ 3 ];",
 
 		"	morphedNormal += normal;",
 
@@ -1311,10 +1312,11 @@ THREE.ShaderChunk = {
 
 		"#ifdef USE_SKINNING",
 
-		"	mat4 skinMatrix = skinWeight.x * boneMatX;",
-		"	skinMatrix 	+= skinWeight.y * boneMatY;",
-		"	skinMatrix 	+= skinWeight.z * boneMatZ;",
-		"	skinMatrix 	+= skinWeight.w * boneMatW;",
+		"	mat4 skinMatrix = mat4( 0.0 );",
+		"	skinMatrix += skinWeight.x * boneMatX;",
+		"	skinMatrix += skinWeight.y * boneMatY;",
+		"	skinMatrix += skinWeight.z * boneMatZ;",
+		"	skinMatrix += skinWeight.w * boneMatW;",
 
 		"	#ifdef USE_MORPHNORMALS",
 
@@ -1365,8 +1367,8 @@ THREE.ShaderChunk = {
 	// SHADOW MAP
 
 	// based on SpiderGL shadow map and Fabien Sanglard's GLSL shadow mapping examples
-	//  http://spidergl.org/example.php?id=6
-	// 	http://fabiensanglard.net/shadowmapping
+	// http://spidergl.org/example.php?id=6
+	// http://fabiensanglard.net/shadowmapping
 
 	shadowmap_pars_fragment: [
 
@@ -1418,8 +1420,8 @@ THREE.ShaderChunk = {
 
 		"		vec3 shadowCoord = vShadowCoord[ i ].xyz / vShadowCoord[ i ].w;",
 
-				// "if ( something && something )" 		 breaks ATI OpenGL shader compiler
-				// "if ( all( something, something ) )"  using this instead
+				// "if ( something && something )" breaks ATI OpenGL shader compiler
+				// "if ( all( something, something ) )" using this instead
 
 		"		bvec4 inFrustumVec = bvec4 ( shadowCoord.x >= 0.0, shadowCoord.x <= 1.0, shadowCoord.y >= 0.0, shadowCoord.y <= 1.0 );",
 		"		bool inFrustum = all( inFrustumVec );",
@@ -1453,7 +1455,7 @@ THREE.ShaderChunk = {
 
 		"				float shadow = 0.0;",
 
-						/*
+		/*
 						// nested loops breaks shader compiler / validator on some ATI cards when using OpenGL
 						// must enroll loop manually
 
@@ -1474,7 +1476,7 @@ THREE.ShaderChunk = {
 
 		"				shadow /= 9.0;",
 
-						*/
+		*/
 
 		"				const float shadowDelta = 1.0 / 9.0;",
 
@@ -1576,13 +1578,13 @@ THREE.ShaderChunk = {
 
 		"				if ( fDepth < shadowCoord.z )",
 
-							// spot with multiple shadows is darker
+		// spot with multiple shadows is darker
 
 		"					shadowColor = shadowColor * vec3( 1.0 - shadowDarkness[ i ] );",
 
-							// spot with multiple shadows has the same color as single shadow spot
+		// spot with multiple shadows has the same color as single shadow spot
 
-							//"shadowColor = min( shadowColor, vec3( shadowDarkness[ i ] ) );",
+		// "					shadowColor = min( shadowColor, vec3( shadowDarkness[ i ] ) );",
 
 		"			#endif",
 
@@ -1728,6 +1730,7 @@ THREE.ShaderChunk = {
 	].join('\n'),
 
 	logdepthbuf_fragment: [
+
 		"#if defined(USE_LOGDEPTHBUF) && defined(USE_LOGDEPTHBUF_EXT)",
 
 		"	gl_FragDepthEXT = log2(vFragDepth) * logDepthBufFC * 0.5;",

+ 38 - 32
src/renderers/shaders/ShaderLib.js

@@ -337,7 +337,7 @@ THREE.ShaderLib = {
 
 	'particle_basic': {
 
-		uniforms:  THREE.UniformsUtils.merge( [
+		uniforms: THREE.UniformsUtils.merge( [
 
 			THREE.UniformsLib[ "particle" ],
 			THREE.UniformsLib[ "shadowmap" ]
@@ -411,8 +411,8 @@ THREE.ShaderLib = {
 			THREE.UniformsLib[ "fog" ],
 
 			{
-				"scale":     { type: "f", value: 1 },
-				"dashSize":  { type: "f", value: 1 },
+				"scale"    : { type: "f", value: 1 },
+				"dashSize" : { type: "f", value: 1 },
 				"totalSize": { type: "f", value: 2 }
 			}
 
@@ -596,18 +596,18 @@ THREE.ShaderLib = {
 
 			{
 
-			"enableAO"		  : { type: "i", value: 0 },
-			"enableDiffuse"	  : { type: "i", value: 0 },
-			"enableSpecular"  : { type: "i", value: 0 },
-			"enableReflection": { type: "i", value: 0 },
+			"enableAO"          : { type: "i", value: 0 },
+			"enableDiffuse"     : { type: "i", value: 0 },
+			"enableSpecular"    : { type: "i", value: 0 },
+			"enableReflection"  : { type: "i", value: 0 },
 			"enableDisplacement": { type: "i", value: 0 },
 
 			"tDisplacement": { type: "t", value: null }, // must go first as this is vertex texture
-			"tDiffuse"	   : { type: "t", value: null },
-			"tCube"		   : { type: "t", value: null },
-			"tNormal"	   : { type: "t", value: null },
-			"tSpecular"	   : { type: "t", value: null },
-			"tAO"		   : { type: "t", value: null },
+			"tDiffuse"     : { type: "t", value: null },
+			"tCube"        : { type: "t", value: null },
+			"tNormal"      : { type: "t", value: null },
+			"tSpecular"    : { type: "t", value: null },
+			"tAO"          : { type: "t", value: null },
 
 			"uNormalScale": { type: "v2", value: new THREE.Vector2( 1, 1 ) },
 
@@ -627,7 +627,7 @@ THREE.ShaderLib = {
 			"uOffset" : { type: "v2", value: new THREE.Vector2( 0, 0 ) },
 			"uRepeat" : { type: "v2", value: new THREE.Vector2( 1, 1 ) },
 
-			"wrapRGB"  : { type: "v3", value: new THREE.Vector3( 1, 1, 1 ) }
+			"wrapRGB" : { type: "v3", value: new THREE.Vector3( 1, 1, 1 ) }
 
 			}
 
@@ -935,7 +935,7 @@ THREE.ShaderLib = {
 
 			"	#if MAX_HEMI_LIGHTS > 0",
 
-			"		vec3 hemiDiffuse  = vec3( 0.0 );",
+			"		vec3 hemiDiffuse = vec3( 0.0 );",
 			"		vec3 hemiSpecular = vec3( 0.0 );" ,
 
 			"		for( int i = 0; i < MAX_HEMI_LIGHTS; i ++ ) {",
@@ -1131,12 +1131,13 @@ THREE.ShaderLib = {
 
 			"				vec4 skinVertex = vec4( position, 1.0 );",
 
-			"				vec4 skinned  = boneMatX * skinVertex * skinWeight.x;",
-			"				skinned 	  += boneMatY * skinVertex * skinWeight.y;",
-			"				skinned 	  += boneMatZ * skinVertex * skinWeight.z;",
-			"				skinned 	  += boneMatW * skinVertex * skinWeight.w;",
+			"				vec4 skinned = vec4( 0.0 );",
+			"				skinned += boneMatX * skinVertex * skinWeight.x;",
+			"				skinned += boneMatY * skinVertex * skinWeight.y;",
+			"				skinned += boneMatZ * skinVertex * skinWeight.z;",
+			"				skinned += boneMatW * skinVertex * skinWeight.w;",
 
-			"				displacedPosition  = skinned.xyz;",
+			"				displacedPosition = skinned.xyz;",
 
 			"			#else",
 
@@ -1152,12 +1153,13 @@ THREE.ShaderLib = {
 
 			"			vec4 skinVertex = vec4( position, 1.0 );",
 
-			"			vec4 skinned  = boneMatX * skinVertex * skinWeight.x;",
-			"			skinned 	  += boneMatY * skinVertex * skinWeight.y;",
-			"			skinned 	  += boneMatZ * skinVertex * skinWeight.z;",
-			"			skinned 	  += boneMatW * skinVertex * skinWeight.w;",
+			"			vec4 skinned = vec4( 0.0 );",
+			"			skinned += boneMatX * skinVertex * skinWeight.x;",
+			"			skinned += boneMatY * skinVertex * skinWeight.y;",
+			"			skinned += boneMatZ * skinVertex * skinWeight.z;",
+			"			skinned += boneMatW * skinVertex * skinWeight.w;",
 
-			"			displacedPosition  = skinned.xyz;",
+			"			displacedPosition = skinned.xyz;",
 
 			"		#else",
 
@@ -1248,13 +1250,17 @@ THREE.ShaderLib = {
 
 	},
 
-	// Depth encoding into RGBA texture
-	// 	based on SpiderGL shadow map example
-	// 		http://spidergl.org/example.php?id=6
-	// 	originally from
-	//		http://www.gamedev.net/topic/442138-packing-a-float-into-a-a8r8g8b8-texture-shader/page__whichpage__1%25EF%25BF%25BD
-	// 	see also here:
-	//		http://aras-p.info/blog/2009/07/30/encoding-floats-to-rgba-the-final/
+	/* Depth encoding into RGBA texture
+	 *
+	 * based on SpiderGL shadow map example
+	 * http://spidergl.org/example.php?id=6
+	 *
+	 * originally from
+	 * http://www.gamedev.net/topic/442138-packing-a-float-into-a-a8r8g8b8-texture-shader/page__whichpage__1%25EF%25BF%25BD
+	 *
+	 * see also
+	 * http://aras-p.info/blog/2009/07/30/encoding-floats-to-rgba-the-final/
+	 */
 
 	'depthRGBA': {
 
@@ -1285,7 +1291,7 @@ THREE.ShaderLib = {
 			"vec4 pack_depth( const in float depth ) {",
 
 			"	const vec4 bit_shift = vec4( 256.0 * 256.0 * 256.0, 256.0 * 256.0, 256.0, 1.0 );",
-			"	const vec4 bit_mask  = vec4( 0.0, 1.0 / 256.0, 1.0 / 256.0, 1.0 / 256.0 );",
+			"	const vec4 bit_mask = vec4( 0.0, 1.0 / 256.0, 1.0 / 256.0, 1.0 / 256.0 );",
 			"	vec4 res = mod( depth * bit_shift * vec4( 255 ), vec4( 256 ) ) / vec4( 255 );", // "	vec4 res = fract( depth * bit_shift );",
 			"	res -= res.xxyz * bit_mask;",
 			"	return res;",