瀏覽代碼

Made normal map shader consistent with Phong shader.

Transplanted hemisphere specular from Phong. Also had to go back to using varying for view position as computing it in the fragment shader wasn't really working :S
alteredq 13 年之前
父節點
當前提交
0faca8f468
共有 2 個文件被更改,包括 12 次插入9 次删除
  1. 0 1
      build/three.min.js
  2. 12 8
      src/extras/ShaderUtils.js

文件差異過大導致無法顯示
+ 0 - 1
build/three.min.js


+ 12 - 8
src/extras/ShaderUtils.js

@@ -219,14 +219,13 @@ THREE.ShaderUtils = {
 				"#endif",
 
 				"varying vec3 vWorldPosition;",
+				"varying vec3 vViewPosition;",
 
 				THREE.ShaderChunk[ "shadowmap_pars_fragment" ],
 				THREE.ShaderChunk[ "fog_pars_fragment" ],
 
 				"void main() {",
 
-					"vec3 vViewPosition = cameraPosition - vWorldPosition;",
-
 					"gl_FragColor = vec4( vec3( 1.0 ), uOpacity );",
 
 					"vec3 specularTex = vec3( 1.0 );",
@@ -483,15 +482,16 @@ THREE.ShaderUtils = {
 							"float dotProduct = dot( normal, lVector );",
 							"float hemiDiffuseWeight = 0.5 * dotProduct + 0.5;",
 
-							"hemiDiffuse += uDiffuseColor * mix( hemisphereLightGroundColor[ i ], hemisphereLightSkyColor[ i ], hemiDiffuseWeight );",
+							"vec3 hemiColor = mix( hemisphereLightGroundColor[ i ], hemisphereLightSkyColor[ i ], hemiDiffuseWeight );",
+
+							"hemiDiffuse += uDiffuseColor * hemiColor;",
 
 							// specular (sky light)
 
-							"float hemiSpecularWeight = 0.0;",
 
 							"vec3 hemiHalfVectorSky = normalize( lVector + viewPosition );",
 							"float hemiDotNormalHalfSky = 0.5 * dot( normal, hemiHalfVectorSky ) + 0.5;",
-							"hemiSpecularWeight += specularTex.r * max( pow( hemiDotNormalHalfSky, uShininess ), 0.0 );",
+							"float hemiSpecularWeightSky = specularTex.r * max( pow( hemiDotNormalHalfSky, uShininess ), 0.0 );",
 
 							// specular (ground light)
 
@@ -499,21 +499,23 @@ THREE.ShaderUtils = {
 
 							"vec3 hemiHalfVectorGround = normalize( lVectorGround + viewPosition );",
 							"float hemiDotNormalHalfGround = 0.5 * dot( normal, hemiHalfVectorGround ) + 0.5;",
-							"hemiSpecularWeight += specularTex.r * max( pow( hemiDotNormalHalfGround, uShininess ), 0.0 );",
+							"float hemiSpecularWeightGround = specularTex.r * max( pow( hemiDotNormalHalfGround, uShininess ), 0.0 );",
 
 							"#ifdef PHYSICALLY_BASED_SHADING",
 
+								"float dotProductGround = dot( normal, lVectorGround );",
+
 								// 2.0 => 2.0001 is hack to work around ANGLE bug
 
 								"float specularNormalization = ( uShininess + 2.0001 ) / 8.0;",
 
 								"vec3 schlickSky = uSpecularColor + vec3( 1.0 - uSpecularColor ) * pow( 1.0 - dot( lVector, hemiHalfVectorSky ), 5.0 );",
 								"vec3 schlickGround = uSpecularColor + vec3( 1.0 - uSpecularColor ) * pow( 1.0 - dot( lVectorGround, hemiHalfVectorGround ), 5.0 );",
-								"hemiSpecular += ( schlickSky + schlickGround ) * mix( hemisphereLightGroundColor[ i ], hemisphereLightSkyColor[ i ], hemiDiffuseWeight ) * hemiSpecularWeight * hemiDiffuseWeight * specularNormalization;",
+								"hemiSpecular += hemiColor * specularNormalization * ( schlickSky * hemiSpecularWeightSky * max( dotProduct, 0.0 ) + schlickGround * hemiSpecularWeightGround * max( dotProductGround, 0.0 ) );",
 
 							"#else",
 
-								"hemiSpecular += uSpecularColor * mix( hemisphereLightGroundColor[ i ], hemisphereLightSkyColor[ i ], hemiDiffuseWeight ) * hemiSpecularWeight * hemiDiffuseWeight;",
+								"hemiSpecular += uSpecularColor * hemiColor * ( hemiSpecularWeightSky + hemiSpecularWeightGround ) * hemiDiffuseWeight;",
 
 							"#endif",
 
@@ -622,6 +624,7 @@ THREE.ShaderUtils = {
 				"varying vec2 vUv;",
 
 				"varying vec3 vWorldPosition;",
+				"varying vec3 vViewPosition;",
 
 				THREE.ShaderChunk[ "skinning_pars_vertex" ],
 				THREE.ShaderChunk[ "shadowmap_pars_vertex" ],
@@ -711,6 +714,7 @@ THREE.ShaderUtils = {
 					//
 
 					"vWorldPosition = mPosition.xyz;",
+					"vViewPosition = -mvPosition.xyz;",
 
 					// shadows
 

部分文件因文件數量過多而無法顯示