ShaderTerrain.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /**
  2. * @author alteredq / http://alteredqualia.com/
  3. *
  4. */
  5. THREE.ShaderTerrain = {
  6. /* -------------------------------------------------------------------------
  7. // Dynamic terrain shader
  8. // - Blinn-Phong
  9. // - height + normal + diffuse1 + diffuse2 + specular + detail maps
  10. // - point, directional and hemisphere lights (use with "lights: true" material option)
  11. // - shadow maps receiving
  12. ------------------------------------------------------------------------- */
  13. 'terrain' : {
  14. uniforms: THREE.UniformsUtils.merge( [
  15. THREE.UniformsLib[ "fog" ],
  16. THREE.UniformsLib[ "lights" ],
  17. THREE.UniformsLib[ "shadowmap" ],
  18. {
  19. "enableDiffuse1" : { type: "i", value: 0 },
  20. "enableDiffuse2" : { type: "i", value: 0 },
  21. "enableSpecular" : { type: "i", value: 0 },
  22. "enableReflection": { type: "i", value: 0 },
  23. "tDiffuse1" : { type: "t", value: null },
  24. "tDiffuse2" : { type: "t", value: null },
  25. "tDetail" : { type: "t", value: null },
  26. "tNormal" : { type: "t", value: null },
  27. "tSpecular" : { type: "t", value: null },
  28. "tDisplacement": { type: "t", value: null },
  29. "uNormalScale": { type: "f", value: 1.0 },
  30. "uDisplacementBias": { type: "f", value: 0.0 },
  31. "uDisplacementScale": { type: "f", value: 1.0 },
  32. "diffuse": { type: "c", value: new THREE.Color( 0xeeeeee ) },
  33. "specular": { type: "c", value: new THREE.Color( 0x111111 ) },
  34. "shininess": { type: "f", value: 30 },
  35. "opacity": { type: "f", value: 1 },
  36. "uRepeatBase" : { type: "v2", value: new THREE.Vector2( 1, 1 ) },
  37. "uRepeatOverlay" : { type: "v2", value: new THREE.Vector2( 1, 1 ) },
  38. "uOffset" : { type: "v2", value: new THREE.Vector2( 0, 0 ) }
  39. }
  40. ] ),
  41. fragmentShader: [
  42. "uniform vec3 diffuse;",
  43. "uniform vec3 specular;",
  44. "uniform float shininess;",
  45. "uniform float opacity;",
  46. "uniform bool enableDiffuse1;",
  47. "uniform bool enableDiffuse2;",
  48. "uniform bool enableSpecular;",
  49. "uniform sampler2D tDiffuse1;",
  50. "uniform sampler2D tDiffuse2;",
  51. "uniform sampler2D tDetail;",
  52. "uniform sampler2D tNormal;",
  53. "uniform sampler2D tSpecular;",
  54. "uniform sampler2D tDisplacement;",
  55. "uniform float uNormalScale;",
  56. "uniform vec2 uRepeatOverlay;",
  57. "uniform vec2 uRepeatBase;",
  58. "uniform vec2 uOffset;",
  59. "varying vec3 vTangent;",
  60. "varying vec3 vBinormal;",
  61. "varying vec3 vNormal;",
  62. "varying vec2 vUv;",
  63. "uniform vec3 ambientLightColor;",
  64. "varying vec3 vViewPosition;",
  65. THREE.ShaderChunk[ "common" ],
  66. THREE.ShaderChunk[ "bsdfs" ],
  67. THREE.ShaderChunk[ "lights_pars" ],
  68. THREE.ShaderChunk[ "shadowmap_pars_fragment" ],
  69. THREE.ShaderChunk[ "fog_pars_fragment" ],
  70. "void main() {",
  71. "vec3 outgoingLight = vec3( 0.0 );", // outgoing light does not have an alpha, the surface does
  72. "vec4 diffuseColor = vec4( diffuse, opacity );",
  73. "vec3 specularTex = vec3( 1.0 );",
  74. "vec2 uvOverlay = uRepeatOverlay * vUv + uOffset;",
  75. "vec2 uvBase = uRepeatBase * vUv;",
  76. "vec3 normalTex = texture2D( tDetail, uvOverlay ).xyz * 2.0 - 1.0;",
  77. "normalTex.xy *= uNormalScale;",
  78. "normalTex = normalize( normalTex );",
  79. "if( enableDiffuse1 && enableDiffuse2 ) {",
  80. "vec4 colDiffuse1 = texture2D( tDiffuse1, uvOverlay );",
  81. "vec4 colDiffuse2 = texture2D( tDiffuse2, uvOverlay );",
  82. "colDiffuse1.xyz = inputToLinear( colDiffuse1.xyz );",
  83. "colDiffuse2.xyz = inputToLinear( colDiffuse2.xyz );",
  84. "diffuseColor *= mix ( colDiffuse1, colDiffuse2, 1.0 - texture2D( tDisplacement, uvBase ) );",
  85. " } else if( enableDiffuse1 ) {",
  86. "diffuseColor *= texture2D( tDiffuse1, uvOverlay );",
  87. "} else if( enableDiffuse2 ) {",
  88. "diffuseColor *= texture2D( tDiffuse2, uvOverlay );",
  89. "}",
  90. "if( enableSpecular )",
  91. "specularTex = texture2D( tSpecular, uvOverlay ).xyz;",
  92. "mat3 tsb = mat3( vTangent, vBinormal, vNormal );",
  93. "vec3 finalNormal = tsb * normalTex;",
  94. "vec3 normal = normalize( finalNormal );",
  95. "vec3 viewPosition = normalize( vViewPosition );",
  96. "vec3 totalDiffuseLight = vec3( 0.0 );",
  97. "vec3 totalSpecularLight = vec3( 0.0 );",
  98. // point lights
  99. "#if NUM_POINT_LIGHTS > 0",
  100. "for ( int i = 0; i < NUM_POINT_LIGHTS; i ++ ) {",
  101. "vec3 lVector = pointLights[ i ].position + vViewPosition.xyz;",
  102. "float attenuation = calcLightAttenuation( length( lVector ), pointLights[ i ].distance, pointLights[ i ].decay );",
  103. "lVector = normalize( lVector );",
  104. "vec3 pointHalfVector = normalize( lVector + viewPosition );",
  105. "float pointDotNormalHalf = max( dot( normal, pointHalfVector ), 0.0 );",
  106. "float pointDiffuseWeight = max( dot( normal, lVector ), 0.0 );",
  107. "float pointSpecularWeight = specularTex.r * max( pow( pointDotNormalHalf, shininess ), 0.0 );",
  108. "totalDiffuseLight += attenuation * pointLights[ i ].color * pointDiffuseWeight;",
  109. "totalSpecularLight += attenuation * pointLights[ i ].color * specular * pointSpecularWeight * pointDiffuseWeight;",
  110. "}",
  111. "#endif",
  112. // directional lights
  113. "#if NUM_DIR_LIGHTS > 0",
  114. "vec3 dirDiffuse = vec3( 0.0 );",
  115. "vec3 dirSpecular = vec3( 0.0 );",
  116. "for( int i = 0; i < NUM_DIR_LIGHTS; i++ ) {",
  117. "vec3 dirVector = directionalLights[ i ].direction;",
  118. "vec3 dirHalfVector = normalize( dirVector + viewPosition );",
  119. "float dirDotNormalHalf = max( dot( normal, dirHalfVector ), 0.0 );",
  120. "float dirDiffuseWeight = max( dot( normal, dirVector ), 0.0 );",
  121. "float dirSpecularWeight = specularTex.r * max( pow( dirDotNormalHalf, shininess ), 0.0 );",
  122. "totalDiffuseLight += directionalLights[ i ].color * dirDiffuseWeight;",
  123. "totalSpecularLight += directionalLights[ i ].color * specular * dirSpecularWeight * dirDiffuseWeight;",
  124. "}",
  125. "#endif",
  126. // hemisphere lights
  127. "#if NUM_HEMI_LIGHTS > 0",
  128. "vec3 hemiDiffuse = vec3( 0.0 );",
  129. "vec3 hemiSpecular = vec3( 0.0 );",
  130. "for( int i = 0; i < NUM_HEMI_LIGHTS; i ++ ) {",
  131. "vec3 lVector = hemisphereLightDirection[ i ];",
  132. // diffuse
  133. "float dotProduct = dot( normal, lVector );",
  134. "float hemiDiffuseWeight = 0.5 * dotProduct + 0.5;",
  135. "totalDiffuseLight += mix( hemisphereLights[ i ].groundColor, hemisphereLights[ i ].skyColor, hemiDiffuseWeight );",
  136. // specular (sky light)
  137. "float hemiSpecularWeight = 0.0;",
  138. "vec3 hemiHalfVectorSky = normalize( lVector + viewPosition );",
  139. "float hemiDotNormalHalfSky = 0.5 * dot( normal, hemiHalfVectorSky ) + 0.5;",
  140. "hemiSpecularWeight += specularTex.r * max( pow( hemiDotNormalHalfSky, shininess ), 0.0 );",
  141. // specular (ground light)
  142. "vec3 lVectorGround = -lVector;",
  143. "vec3 hemiHalfVectorGround = normalize( lVectorGround + viewPosition );",
  144. "float hemiDotNormalHalfGround = 0.5 * dot( normal, hemiHalfVectorGround ) + 0.5;",
  145. "hemiSpecularWeight += specularTex.r * max( pow( hemiDotNormalHalfGround, shininess ), 0.0 );",
  146. "totalSpecularLight += specular * mix( hemisphereLights[ i ].groundColor, hemisphereLights[ i ].skyColor, hemiDiffuseWeight ) * hemiSpecularWeight * hemiDiffuseWeight;",
  147. "}",
  148. "#endif",
  149. "outgoingLight += diffuseColor.xyz * ( totalDiffuseLight + ambientLightColor + totalSpecularLight );",
  150. THREE.ShaderChunk[ "shadowmap_fragment" ],
  151. THREE.ShaderChunk[ "linear_to_gamma_fragment" ],
  152. THREE.ShaderChunk[ "fog_fragment" ],
  153. "gl_FragColor = vec4( outgoingLight, diffuseColor.a );", // TODO, this should be pre-multiplied to allow for bright highlights on very transparent objects
  154. "}"
  155. ].join( "\n" ),
  156. vertexShader: [
  157. "attribute vec4 tangent;",
  158. "uniform vec2 uRepeatBase;",
  159. "uniform sampler2D tNormal;",
  160. "#ifdef VERTEX_TEXTURES",
  161. "uniform sampler2D tDisplacement;",
  162. "uniform float uDisplacementScale;",
  163. "uniform float uDisplacementBias;",
  164. "#endif",
  165. "varying vec3 vTangent;",
  166. "varying vec3 vBinormal;",
  167. "varying vec3 vNormal;",
  168. "varying vec2 vUv;",
  169. "varying vec3 vViewPosition;",
  170. THREE.ShaderChunk[ "shadowmap_pars_vertex" ],
  171. "void main() {",
  172. "vNormal = normalize( normalMatrix * normal );",
  173. // tangent and binormal vectors
  174. "vTangent = normalize( normalMatrix * tangent.xyz );",
  175. "vBinormal = cross( vNormal, vTangent ) * tangent.w;",
  176. "vBinormal = normalize( vBinormal );",
  177. // texture coordinates
  178. "vUv = uv;",
  179. "vec2 uvBase = uv * uRepeatBase;",
  180. // displacement mapping
  181. "#ifdef VERTEX_TEXTURES",
  182. "vec3 dv = texture2D( tDisplacement, uvBase ).xyz;",
  183. "float df = uDisplacementScale * dv.x + uDisplacementBias;",
  184. "vec3 displacedPosition = normal * df + position;",
  185. "vec4 worldPosition = modelMatrix * vec4( displacedPosition, 1.0 );",
  186. "vec4 mvPosition = modelViewMatrix * vec4( displacedPosition, 1.0 );",
  187. "#else",
  188. "vec4 worldPosition = modelMatrix * vec4( position, 1.0 );",
  189. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  190. "#endif",
  191. "gl_Position = projectionMatrix * mvPosition;",
  192. "vViewPosition = -mvPosition.xyz;",
  193. "vec3 normalTex = texture2D( tNormal, uvBase ).xyz * 2.0 - 1.0;",
  194. "vNormal = normalMatrix * normalTex;",
  195. THREE.ShaderChunk[ "shadowmap_vertex" ],
  196. "}"
  197. ].join( "\n" )
  198. }
  199. };