瀏覽代碼

Refactored normal map shader: fixed displacement mapping, made it toggleable, saved one varying.

Displacement should be computed in object space (and shouldn't need scaling).

Correct shadow casting still needs a custom depth material but at least shadow receiving should now work with displaced vertices.
alteredq 13 年之前
父節點
當前提交
13a665d444
共有 4 個文件被更改,包括 45 次插入37 次删除
  1. 0 3
      build/Three.js
  2. 0 3
      build/custom/ThreeExtras.js
  3. 3 2
      examples/webgl_materials_normalmap.html
  4. 42 29
      src/extras/ShaderUtils.js

文件差異過大導致無法顯示
+ 0 - 3
build/Three.js


文件差異過大導致無法顯示
+ 0 - 3
build/custom/ThreeExtras.js


+ 3 - 2
examples/webgl_materials_normalmap.html

@@ -163,13 +163,14 @@
 				uniforms[ "enableDiffuse" ].value = false;
 				uniforms[ "enableDiffuse" ].value = false;
 				uniforms[ "enableSpecular" ].value = false;
 				uniforms[ "enableSpecular" ].value = false;
 				uniforms[ "enableReflection" ].value = true;
 				uniforms[ "enableReflection" ].value = true;
+				uniforms[ "enableDisplacement" ].value = true;
 
 
 				uniforms[ "tNormal" ].texture = THREE.ImageUtils.loadTexture( "textures/normal/ninja/normal.jpg" );
 				uniforms[ "tNormal" ].texture = THREE.ImageUtils.loadTexture( "textures/normal/ninja/normal.jpg" );
 				uniforms[ "tAO" ].texture = THREE.ImageUtils.loadTexture( "textures/normal/ninja/ao.jpg" );
 				uniforms[ "tAO" ].texture = THREE.ImageUtils.loadTexture( "textures/normal/ninja/ao.jpg" );
 
 
 				uniforms[ "tDisplacement" ].texture = THREE.ImageUtils.loadTexture( "textures/normal/ninja/displacement.jpg" );
 				uniforms[ "tDisplacement" ].texture = THREE.ImageUtils.loadTexture( "textures/normal/ninja/displacement.jpg" );
-				uniforms[ "uDisplacementBias" ].value = - 0.428408 * scale;
-				uniforms[ "uDisplacementScale" ].value = 2.436143 * scale;
+				uniforms[ "uDisplacementBias" ].value = - 0.428408;
+				uniforms[ "uDisplacementScale" ].value = 2.436143;
 
 
 				uniforms[ "uDiffuseColor" ].value.setHex( diffuse );
 				uniforms[ "uDiffuseColor" ].value.setHex( diffuse );
 				uniforms[ "uSpecularColor" ].value.setHex( specular );
 				uniforms[ "uSpecularColor" ].value.setHex( specular );

+ 42 - 29
src/extras/ShaderUtils.js

@@ -112,6 +112,7 @@ THREE.ShaderUtils = {
 				"enableDiffuse"	  : { type: "i", value: 0 },
 				"enableDiffuse"	  : { type: "i", value: 0 },
 				"enableSpecular"  : { type: "i", value: 0 },
 				"enableSpecular"  : { type: "i", value: 0 },
 				"enableReflection": { type: "i", value: 0 },
 				"enableReflection": { type: "i", value: 0 },
+				"enableDisplacement": { type: "i", value: 0 },
 
 
 				"tDiffuse"	   : { type: "t", value: 0, texture: null },
 				"tDiffuse"	   : { type: "t", value: 0, texture: null },
 				"tCube"		   : { type: "t", value: 1, texture: null },
 				"tCube"		   : { type: "t", value: 1, texture: null },
@@ -196,8 +197,6 @@ THREE.ShaderUtils = {
 					"uniform float spotLightExponent[ MAX_SPOT_LIGHTS ];",
 					"uniform float spotLightExponent[ MAX_SPOT_LIGHTS ];",
 					"uniform float spotLightDistance[ MAX_SPOT_LIGHTS ];",
 					"uniform float spotLightDistance[ MAX_SPOT_LIGHTS ];",
 
 
-					"varying vec3 vWorldPosition;",
-
 				"#endif",
 				"#endif",
 
 
 				"#ifdef WRAP_AROUND",
 				"#ifdef WRAP_AROUND",
@@ -206,13 +205,15 @@ THREE.ShaderUtils = {
 
 
 				"#endif",
 				"#endif",
 
 
-				"varying vec3 vViewPosition;",
+				"varying vec3 vWorldPosition;",
 
 
 				THREE.ShaderChunk[ "shadowmap_pars_fragment" ],
 				THREE.ShaderChunk[ "shadowmap_pars_fragment" ],
 				THREE.ShaderChunk[ "fog_pars_fragment" ],
 				THREE.ShaderChunk[ "fog_pars_fragment" ],
 
 
 				"void main() {",
 				"void main() {",
 
 
+					"vec3 vViewPosition = cameraPosition - vWorldPosition;",
+
 					"gl_FragColor = vec4( vec3( 1.0 ), uOpacity );",
 					"gl_FragColor = vec4( vec3( 1.0 ), uOpacity );",
 
 
 					"vec3 specularTex = vec3( 1.0 );",
 					"vec3 specularTex = vec3( 1.0 );",
@@ -476,8 +477,7 @@ THREE.ShaderUtils = {
 
 
 					"if ( enableReflection ) {",
 					"if ( enableReflection ) {",
 
 
-						"vec3 wPos = cameraPosition - vViewPosition;",
-						"vec3 vReflect = reflect( normalize( wPos ), normal );",
+						"vec3 vReflect = reflect( normalize( vWorldPosition ), normal );",
 
 
 						"vec4 cubeColor = textureCube( tCube, vec3( -vReflect.x, vReflect.yz ) );",
 						"vec4 cubeColor = textureCube( tCube, vec3( -vReflect.x, vReflect.yz ) );",
 
 
@@ -506,6 +506,8 @@ THREE.ShaderUtils = {
 				"uniform vec2 uOffset;",
 				"uniform vec2 uOffset;",
 				"uniform vec2 uRepeat;",
 				"uniform vec2 uRepeat;",
 
 
+				"uniform bool enableDisplacement;",
+
 				"#ifdef VERTEX_TEXTURES",
 				"#ifdef VERTEX_TEXTURES",
 
 
 					"uniform sampler2D tDisplacement;",
 					"uniform sampler2D tDisplacement;",
@@ -519,22 +521,12 @@ THREE.ShaderUtils = {
 				"varying vec3 vNormal;",
 				"varying vec3 vNormal;",
 				"varying vec2 vUv;",
 				"varying vec2 vUv;",
 
 
-				"#if MAX_SPOT_LIGHTS > 0",
-
-					"varying vec3 vWorldPosition;",
-
-				"#endif",
-
-				"varying vec3 vViewPosition;",
+				"varying vec3 vWorldPosition;",
 
 
 				THREE.ShaderChunk[ "shadowmap_pars_vertex" ],
 				THREE.ShaderChunk[ "shadowmap_pars_vertex" ],
 
 
 				"void main() {",
 				"void main() {",
 
 
-					"vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
-
-					"vViewPosition = -mvPosition.xyz;",
-
 					// normal, tangent and binormal vectors
 					// normal, tangent and binormal vectors
 
 
 					"vNormal = normalMatrix * normal;",
 					"vNormal = normalMatrix * normal;",
@@ -543,31 +535,52 @@ THREE.ShaderUtils = {
 
 
 					"vUv = uv * uRepeat + uOffset;",
 					"vUv = uv * uRepeat + uOffset;",
 
 
-					// spot lights
+					// displacement mapping
 
 
-					"#if MAX_SPOT_LIGHTS > 0",
+					"vec3 displacedPosition;",
 
 
-						"vec4 wPosition = objectMatrix * vec4( position, 1.0 );",
-						"vWorldPosition = wPosition.xyz;",
+					"#ifdef VERTEX_TEXTURES",
 
 
-					"#endif",
+						"if ( enableDisplacement ) {",
 
 
-					// displacement mapping
+							"vec3 dv = texture2D( tDisplacement, uv ).xyz;",
+							"float df = uDisplacementScale * dv.x + uDisplacementBias;",
+							"displacedPosition = position + normalize( normal ) * df;",
 
 
-					"#ifdef VERTEX_TEXTURES",
+						"} else {",
 
 
-						"vec3 dv = texture2D( tDisplacement, uv ).xyz;",
-						"float df = uDisplacementScale * dv.x + uDisplacementBias;",
-						"vec4 displacedPosition = vec4( normalize( vNormal.xyz ) * df, 0.0 ) + mvPosition;",
-						"gl_Position = projectionMatrix * displacedPosition;",
+							"displacedPosition = position;",
+
+						"}",
 
 
 					"#else",
 					"#else",
 
 
-						"gl_Position = projectionMatrix * mvPosition;",
+						"displacedPosition = position;",
 
 
 					"#endif",
 					"#endif",
 
 
-					THREE.ShaderChunk[ "shadowmap_vertex" ],
+					//
+
+					"vec4 mvPosition = modelViewMatrix * vec4( displacedPosition, 1.0 );",
+					"vec4 wPosition = objectMatrix * vec4( displacedPosition, 1.0 );",
+
+					"gl_Position = projectionMatrix * mvPosition;",
+
+					//
+
+					"vWorldPosition = wPosition.xyz;",
+
+					// shadows
+
+					"#ifdef USE_SHADOWMAP",
+
+						"for( int i = 0; i < MAX_SHADOWS; i ++ ) {",
+
+							"vShadowCoord[ i ] = shadowMatrix[ i ] * wPosition;",
+
+						"}",
+
+					"#endif",
 
 
 				"}"
 				"}"
 
 

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