ShaderTerrain.js 9.1 KB

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