ShaderTerrain.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. "#if MAX_DIR_LIGHTS > 0",
  65. "uniform vec3 directionalLightColor[ MAX_DIR_LIGHTS ];",
  66. "uniform vec3 directionalLightDirection[ MAX_DIR_LIGHTS ];",
  67. "#endif",
  68. "#if MAX_HEMI_LIGHTS > 0",
  69. "uniform vec3 hemisphereLightSkyColor[ MAX_HEMI_LIGHTS ];",
  70. "uniform vec3 hemisphereLightGroundColor[ MAX_HEMI_LIGHTS ];",
  71. "uniform vec3 hemisphereLightDirection[ MAX_HEMI_LIGHTS ];",
  72. "#endif",
  73. "#if MAX_POINT_LIGHTS > 0",
  74. "uniform vec3 pointLightColor[ MAX_POINT_LIGHTS ];",
  75. "uniform vec3 pointLightPosition[ MAX_POINT_LIGHTS ];",
  76. "uniform float pointLightDistance[ MAX_POINT_LIGHTS ];",
  77. "uniform float pointLightDecay[ MAX_POINT_LIGHTS ];",
  78. "#endif",
  79. "varying vec3 vViewPosition;",
  80. THREE.ShaderChunk[ "common" ],
  81. THREE.ShaderChunk[ "shadowmap_pars_fragment" ],
  82. THREE.ShaderChunk[ "fog_pars_fragment" ],
  83. "void main() {",
  84. "gl_FragColor = vec4( vec3( 1.0 ), opacity );",
  85. "vec3 specularTex = vec3( 1.0 );",
  86. "vec2 uvOverlay = uRepeatOverlay * vUv + uOffset;",
  87. "vec2 uvBase = uRepeatBase * vUv;",
  88. "vec3 normalTex = texture2D( tDetail, uvOverlay ).xyz * 2.0 - 1.0;",
  89. "normalTex.xy *= uNormalScale;",
  90. "normalTex = normalize( normalTex );",
  91. "if( enableDiffuse1 && enableDiffuse2 ) {",
  92. "vec4 colDiffuse1 = texture2D( tDiffuse1, uvOverlay );",
  93. "vec4 colDiffuse2 = texture2D( tDiffuse2, uvOverlay );",
  94. "colDiffuse1.xyz = inputToLinear( colDiffuse1.xyz );",
  95. "colDiffuse2.xyz = inputToLinear( colDiffuse2.xyz );",
  96. "gl_FragColor = gl_FragColor * mix ( colDiffuse1, colDiffuse2, 1.0 - texture2D( tDisplacement, uvBase ) );",
  97. " } else if( enableDiffuse1 ) {",
  98. "gl_FragColor = gl_FragColor * texture2D( tDiffuse1, uvOverlay );",
  99. "} else if( enableDiffuse2 ) {",
  100. "gl_FragColor = gl_FragColor * texture2D( tDiffuse2, uvOverlay );",
  101. "}",
  102. "if( enableSpecular )",
  103. "specularTex = texture2D( tSpecular, uvOverlay ).xyz;",
  104. "mat3 tsb = mat3( vTangent, vBinormal, vNormal );",
  105. "vec3 finalNormal = tsb * normalTex;",
  106. "vec3 normal = normalize( finalNormal );",
  107. "vec3 viewPosition = normalize( vViewPosition );",
  108. // point lights
  109. "#if MAX_POINT_LIGHTS > 0",
  110. "vec3 pointDiffuse = vec3( 0.0 );",
  111. "vec3 pointSpecular = vec3( 0.0 );",
  112. "for ( int i = 0; i < MAX_POINT_LIGHTS; i ++ ) {",
  113. "vec4 lPosition = viewMatrix * vec4( pointLightPosition[ i ], 1.0 );",
  114. "vec3 lVector = lPosition.xyz + vViewPosition.xyz;",
  115. "float attenuation = calcLightAttenuation( length( lVector ), pointLightDistance[ i ], pointLightDecay[i] );",
  116. "lVector = normalize( lVector );",
  117. "vec3 pointHalfVector = normalize( lVector + viewPosition );",
  118. "float pointDotNormalHalf = max( dot( normal, pointHalfVector ), 0.0 );",
  119. "float pointDiffuseWeight = max( dot( normal, lVector ), 0.0 );",
  120. "float pointSpecularWeight = specularTex.r * max( pow( pointDotNormalHalf, shininess ), 0.0 );",
  121. "pointDiffuse += attenuation * pointLightColor[ i ] * diffuse * pointDiffuseWeight;",
  122. "pointSpecular += attenuation * pointLightColor[ i ] * specular * pointSpecularWeight * pointDiffuseWeight;",
  123. "}",
  124. "#endif",
  125. // directional lights
  126. "#if MAX_DIR_LIGHTS > 0",
  127. "vec3 dirDiffuse = vec3( 0.0 );",
  128. "vec3 dirSpecular = vec3( 0.0 );",
  129. "for( int i = 0; i < MAX_DIR_LIGHTS; i++ ) {",
  130. "vec3 dirVector = transformDirection( directionalLightDirection[ i ], viewMatrix );",
  131. "vec3 dirHalfVector = normalize( dirVector + viewPosition );",
  132. "float dirDotNormalHalf = max( dot( normal, dirHalfVector ), 0.0 );",
  133. "float dirDiffuseWeight = max( dot( normal, dirVector ), 0.0 );",
  134. "float dirSpecularWeight = specularTex.r * max( pow( dirDotNormalHalf, shininess ), 0.0 );",
  135. "dirDiffuse += directionalLightColor[ i ] * diffuse * dirDiffuseWeight;",
  136. "dirSpecular += directionalLightColor[ i ] * specular * dirSpecularWeight * dirDiffuseWeight;",
  137. "}",
  138. "#endif",
  139. // hemisphere lights
  140. "#if MAX_HEMI_LIGHTS > 0",
  141. "vec3 hemiDiffuse = vec3( 0.0 );",
  142. "vec3 hemiSpecular = vec3( 0.0 );",
  143. "for( int i = 0; i < MAX_HEMI_LIGHTS; i ++ ) {",
  144. "vec3 lVector = transformDirection( hemisphereLightDirection[ i ], viewMatrix );",
  145. // diffuse
  146. "float dotProduct = dot( normal, lVector );",
  147. "float hemiDiffuseWeight = 0.5 * dotProduct + 0.5;",
  148. "hemiDiffuse += diffuse * mix( hemisphereLightGroundColor[ i ], hemisphereLightSkyColor[ i ], hemiDiffuseWeight );",
  149. // specular (sky light)
  150. "float hemiSpecularWeight = 0.0;",
  151. "vec3 hemiHalfVectorSky = normalize( lVector + viewPosition );",
  152. "float hemiDotNormalHalfSky = 0.5 * dot( normal, hemiHalfVectorSky ) + 0.5;",
  153. "hemiSpecularWeight += specularTex.r * max( pow( hemiDotNormalHalfSky, shininess ), 0.0 );",
  154. // specular (ground light)
  155. "vec3 lVectorGround = -lVector;",
  156. "vec3 hemiHalfVectorGround = normalize( lVectorGround + viewPosition );",
  157. "float hemiDotNormalHalfGround = 0.5 * dot( normal, hemiHalfVectorGround ) + 0.5;",
  158. "hemiSpecularWeight += specularTex.r * max( pow( hemiDotNormalHalfGround, shininess ), 0.0 );",
  159. "hemiSpecular += specular * mix( hemisphereLightGroundColor[ i ], hemisphereLightSkyColor[ i ], hemiDiffuseWeight ) * hemiSpecularWeight * hemiDiffuseWeight;",
  160. "}",
  161. "#endif",
  162. // all lights contribution summation
  163. "vec3 totalDiffuse = vec3( 0.0 );",
  164. "vec3 totalSpecular = vec3( 0.0 );",
  165. "#if MAX_DIR_LIGHTS > 0",
  166. "totalDiffuse += dirDiffuse;",
  167. "totalSpecular += dirSpecular;",
  168. "#endif",
  169. "#if MAX_HEMI_LIGHTS > 0",
  170. "totalDiffuse += hemiDiffuse;",
  171. "totalSpecular += hemiSpecular;",
  172. "#endif",
  173. "#if MAX_POINT_LIGHTS > 0",
  174. "totalDiffuse += pointDiffuse;",
  175. "totalSpecular += pointSpecular;",
  176. "#endif",
  177. //"gl_FragColor.xyz = gl_FragColor.xyz * ( totalDiffuse + ambientLightColor * diffuse ) + totalSpecular;",
  178. "gl_FragColor.xyz = gl_FragColor.xyz * ( totalDiffuse + ambientLightColor * diffuse + totalSpecular );",
  179. THREE.ShaderChunk[ "shadowmap_fragment" ],
  180. THREE.ShaderChunk[ "linear_to_gamma_fragment" ],
  181. THREE.ShaderChunk[ "fog_fragment" ],
  182. "}"
  183. ].join("\n"),
  184. vertexShader: [
  185. "attribute vec4 tangent;",
  186. "uniform vec2 uRepeatBase;",
  187. "uniform sampler2D tNormal;",
  188. "#ifdef VERTEX_TEXTURES",
  189. "uniform sampler2D tDisplacement;",
  190. "uniform float uDisplacementScale;",
  191. "uniform float uDisplacementBias;",
  192. "#endif",
  193. "varying vec3 vTangent;",
  194. "varying vec3 vBinormal;",
  195. "varying vec3 vNormal;",
  196. "varying vec2 vUv;",
  197. "varying vec3 vViewPosition;",
  198. THREE.ShaderChunk[ "shadowmap_pars_vertex" ],
  199. "void main() {",
  200. "vNormal = normalize( normalMatrix * normal );",
  201. // tangent and binormal vectors
  202. "vTangent = normalize( normalMatrix * tangent.xyz );",
  203. "vBinormal = cross( vNormal, vTangent ) * tangent.w;",
  204. "vBinormal = normalize( vBinormal );",
  205. // texture coordinates
  206. "vUv = uv;",
  207. "vec2 uvBase = uv * uRepeatBase;",
  208. // displacement mapping
  209. "#ifdef VERTEX_TEXTURES",
  210. "vec3 dv = texture2D( tDisplacement, uvBase ).xyz;",
  211. "float df = uDisplacementScale * dv.x + uDisplacementBias;",
  212. "vec3 displacedPosition = normal * df + position;",
  213. "vec4 worldPosition = modelMatrix * vec4( displacedPosition, 1.0 );",
  214. "vec4 mvPosition = modelViewMatrix * vec4( displacedPosition, 1.0 );",
  215. "#else",
  216. "vec4 worldPosition = modelMatrix * vec4( position, 1.0 );",
  217. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  218. "#endif",
  219. "gl_Position = projectionMatrix * mvPosition;",
  220. "vViewPosition = -mvPosition.xyz;",
  221. "vec3 normalTex = texture2D( tNormal, uvBase ).xyz * 2.0 - 1.0;",
  222. "vNormal = normalMatrix * normalTex;",
  223. THREE.ShaderChunk[ "shadowmap_vertex" ],
  224. "}"
  225. ].join("\n")
  226. }
  227. };