TerrainShader.js 8.9 KB

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