ShaderTerrain.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. "ambient": { type: "c", value: new THREE.Color( 0x050505 ) },
  35. "shininess": { type: "f", value: 30 },
  36. "opacity": { type: "f", value: 1 },
  37. "uRepeatBase" : { type: "v2", value: new THREE.Vector2( 1, 1 ) },
  38. "uRepeatOverlay" : { type: "v2", value: new THREE.Vector2( 1, 1 ) },
  39. "uOffset" : { type: "v2", value: new THREE.Vector2( 0, 0 ) }
  40. }
  41. ] ),
  42. fragmentShader: [
  43. "uniform vec3 ambient;",
  44. "uniform vec3 diffuse;",
  45. "uniform vec3 specular;",
  46. "uniform float shininess;",
  47. "uniform float opacity;",
  48. "uniform bool enableDiffuse1;",
  49. "uniform bool enableDiffuse2;",
  50. "uniform bool enableSpecular;",
  51. "uniform sampler2D tDiffuse1;",
  52. "uniform sampler2D tDiffuse2;",
  53. "uniform sampler2D tDetail;",
  54. "uniform sampler2D tNormal;",
  55. "uniform sampler2D tSpecular;",
  56. "uniform sampler2D tDisplacement;",
  57. "uniform float uNormalScale;",
  58. "uniform vec2 uRepeatOverlay;",
  59. "uniform vec2 uRepeatBase;",
  60. "uniform vec2 uOffset;",
  61. "varying vec3 vTangent;",
  62. "varying vec3 vBinormal;",
  63. "varying vec3 vNormal;",
  64. "varying vec2 vUv;",
  65. "uniform vec3 ambientLightColor;",
  66. "#if MAX_DIR_LIGHTS > 0",
  67. "uniform vec3 directionalLightColor[ MAX_DIR_LIGHTS ];",
  68. "uniform vec3 directionalLightDirection[ MAX_DIR_LIGHTS ];",
  69. "#endif",
  70. "#if MAX_HEMI_LIGHTS > 0",
  71. "uniform vec3 hemisphereLightSkyColor[ MAX_HEMI_LIGHTS ];",
  72. "uniform vec3 hemisphereLightGroundColor[ MAX_HEMI_LIGHTS ];",
  73. "uniform vec3 hemisphereLightDirection[ MAX_HEMI_LIGHTS ];",
  74. "#endif",
  75. "#if MAX_POINT_LIGHTS > 0",
  76. "uniform vec3 pointLightColor[ MAX_POINT_LIGHTS ];",
  77. "uniform vec3 pointLightPosition[ MAX_POINT_LIGHTS ];",
  78. "uniform float pointLightDistance[ MAX_POINT_LIGHTS ];",
  79. "#endif",
  80. "varying vec3 vViewPosition;",
  81. THREE.ShaderChunk[ "shadowmap_pars_fragment" ],
  82. THREE.ShaderChunk[ "fog_pars_fragment" ],
  83. "void main() {",
  84. " vec3 outgoingLight = vec3( 0.0, 0.0, 0.0 );", // outgoing light does not have an alpha, the surface does
  85. " vec4 diffuseColor = vec4( diffuse, opacity );",
  86. "vec3 specularTex = vec3( 1.0 );",
  87. "vec2 uvOverlay = uRepeatOverlay * vUv + uOffset;",
  88. "vec2 uvBase = uRepeatBase * vUv;",
  89. "vec3 normalTex = texture2D( tDetail, uvOverlay ).xyz * 2.0 - 1.0;",
  90. "normalTex.xy *= uNormalScale;",
  91. "normalTex = normalize( normalTex );",
  92. "if( enableDiffuse1 && enableDiffuse2 ) {",
  93. "vec4 colDiffuse1 = texture2D( tDiffuse1, uvOverlay );",
  94. "vec4 colDiffuse2 = texture2D( tDiffuse2, uvOverlay );",
  95. "#ifdef GAMMA_INPUT",
  96. "colDiffuse1.xyz *= colDiffuse1.xyz;",
  97. "colDiffuse2.xyz *= colDiffuse2.xyz;",
  98. "#endif",
  99. "diffuseColor *= mix ( colDiffuse1, colDiffuse2, 1.0 - texture2D( tDisplacement, uvBase ) );",
  100. " } else if( enableDiffuse1 ) {",
  101. "diffuseColor *= texture2D( tDiffuse1, uvOverlay );",
  102. "} else if( enableDiffuse2 ) {",
  103. "diffuseColor *= texture2D( tDiffuse2, uvOverlay );",
  104. "}",
  105. "if( enableSpecular )",
  106. "specularTex = texture2D( tSpecular, uvOverlay ).xyz;",
  107. "mat3 tsb = mat3( vTangent, vBinormal, vNormal );",
  108. "vec3 finalNormal = tsb * normalTex;",
  109. "vec3 normal = normalize( finalNormal );",
  110. "vec3 viewPosition = normalize( vViewPosition );",
  111. "vec3 totalDiffuseLight = vec3( 0.0 );",
  112. "vec3 totalSpecularLight = vec3( 0.0 );",
  113. // point lights
  114. "#if MAX_POINT_LIGHTS > 0",
  115. "for ( int i = 0; i < MAX_POINT_LIGHTS; i ++ ) {",
  116. "vec4 lPosition = viewMatrix * vec4( pointLightPosition[ i ], 1.0 );",
  117. "vec3 lVector = lPosition.xyz + vViewPosition.xyz;",
  118. "float lDistance = 1.0;",
  119. "if ( pointLightDistance[ i ] > 0.0 )",
  120. "lDistance = 1.0 - min( ( length( lVector ) / pointLightDistance[ i ] ), 1.0 );",
  121. "lVector = normalize( lVector );",
  122. "vec3 pointHalfVector = normalize( lVector + viewPosition );",
  123. "float pointDistance = lDistance;",
  124. "float pointDotNormalHalf = max( dot( normal, pointHalfVector ), 0.0 );",
  125. "float pointDiffuseWeight = max( dot( normal, lVector ), 0.0 );",
  126. "float pointSpecularWeight = specularTex.r * max( pow( pointDotNormalHalf, shininess ), 0.0 );",
  127. "totalDiffuseLight += pointDistance * pointLightColor[ i ] * pointDiffuseWeight;",
  128. "totalSpecularLight += pointDistance * pointLightColor[ i ] * specular * pointSpecularWeight * pointDiffuseWeight;",
  129. "}",
  130. "#endif",
  131. // directional lights
  132. "#if MAX_DIR_LIGHTS > 0",
  133. "vec3 dirDiffuse = vec3( 0.0 );",
  134. "vec3 dirSpecular = vec3( 0.0 );",
  135. "for( int i = 0; i < MAX_DIR_LIGHTS; i++ ) {",
  136. "vec4 lDirection = viewMatrix * vec4( directionalLightDirection[ i ], 0.0 );",
  137. "vec3 dirVector = normalize( lDirection.xyz );",
  138. "vec3 dirHalfVector = normalize( dirVector + viewPosition );",
  139. "float dirDotNormalHalf = max( dot( normal, dirHalfVector ), 0.0 );",
  140. "float dirDiffuseWeight = max( dot( normal, dirVector ), 0.0 );",
  141. "float dirSpecularWeight = specularTex.r * max( pow( dirDotNormalHalf, shininess ), 0.0 );",
  142. "totalDiffuseLight += directionalLightColor[ i ] * dirDiffuseWeight;",
  143. "totalSpecularLight += directionalLightColor[ i ] * specular * dirSpecularWeight * dirDiffuseWeight;",
  144. "}",
  145. "#endif",
  146. // hemisphere lights
  147. "#if MAX_HEMI_LIGHTS > 0",
  148. "vec3 hemiDiffuse = vec3( 0.0 );",
  149. "vec3 hemiSpecular = vec3( 0.0 );" ,
  150. "for( int i = 0; i < MAX_HEMI_LIGHTS; i ++ ) {",
  151. "vec4 lDirection = viewMatrix * vec4( hemisphereLightDirection[ i ], 0.0 );",
  152. "vec3 lVector = normalize( lDirection.xyz );",
  153. // diffuse
  154. "float dotProduct = dot( normal, lVector );",
  155. "float hemiDiffuseWeight = 0.5 * dotProduct + 0.5;",
  156. "totalDiffuseLight += mix( hemisphereLightGroundColor[ i ], hemisphereLightSkyColor[ i ], hemiDiffuseWeight );",
  157. // specular (sky light)
  158. "float hemiSpecularWeight = 0.0;",
  159. "vec3 hemiHalfVectorSky = normalize( lVector + viewPosition );",
  160. "float hemiDotNormalHalfSky = 0.5 * dot( normal, hemiHalfVectorSky ) + 0.5;",
  161. "hemiSpecularWeight += specularTex.r * max( pow( hemiDotNormalHalfSky, shininess ), 0.0 );",
  162. // specular (ground light)
  163. "vec3 lVectorGround = -lVector;",
  164. "vec3 hemiHalfVectorGround = normalize( lVectorGround + viewPosition );",
  165. "float hemiDotNormalHalfGround = 0.5 * dot( normal, hemiHalfVectorGround ) + 0.5;",
  166. "hemiSpecularWeight += specularTex.r * max( pow( hemiDotNormalHalfGround, shininess ), 0.0 );",
  167. "totalSpecularLight += specular * mix( hemisphereLightGroundColor[ i ], hemisphereLightSkyColor[ i ], hemiDiffuseWeight ) * hemiSpecularWeight * hemiDiffuseWeight;",
  168. "}",
  169. "#endif",
  170. //"gl_FragColor.xyz = gl_FragColor.xyz * ( totalDiffuse + ambientLightColor * ambient) + totalSpecular;",
  171. "outgoingLight.rgb += diffuseColor.xyz * ( totalDiffuseLight + ambientLightColor * ambient + totalSpecularLight );",
  172. THREE.ShaderChunk[ "shadowmap_fragment" ],
  173. THREE.ShaderChunk[ "linear_to_gamma_fragment" ],
  174. THREE.ShaderChunk[ "fog_fragment" ],
  175. " gl_FragColor = vec4( outgoingLight, diffuseColor.a );", // TODO, this should be pre-multiplied to allow for bright highlights on very transparent objects
  176. "}"
  177. ].join("\n"),
  178. vertexShader: [
  179. "attribute vec4 tangent;",
  180. "uniform vec2 uRepeatBase;",
  181. "uniform sampler2D tNormal;",
  182. "#ifdef VERTEX_TEXTURES",
  183. "uniform sampler2D tDisplacement;",
  184. "uniform float uDisplacementScale;",
  185. "uniform float uDisplacementBias;",
  186. "#endif",
  187. "varying vec3 vTangent;",
  188. "varying vec3 vBinormal;",
  189. "varying vec3 vNormal;",
  190. "varying vec2 vUv;",
  191. "varying vec3 vViewPosition;",
  192. THREE.ShaderChunk[ "shadowmap_pars_vertex" ],
  193. "void main() {",
  194. "vNormal = normalize( normalMatrix * normal );",
  195. // tangent and binormal vectors
  196. "vTangent = normalize( normalMatrix * tangent.xyz );",
  197. "vBinormal = cross( vNormal, vTangent ) * tangent.w;",
  198. "vBinormal = normalize( vBinormal );",
  199. // texture coordinates
  200. "vUv = uv;",
  201. "vec2 uvBase = uv * uRepeatBase;",
  202. // displacement mapping
  203. "#ifdef VERTEX_TEXTURES",
  204. "vec3 dv = texture2D( tDisplacement, uvBase ).xyz;",
  205. "float df = uDisplacementScale * dv.x + uDisplacementBias;",
  206. "vec3 displacedPosition = normal * df + position;",
  207. "vec4 worldPosition = modelMatrix * vec4( displacedPosition, 1.0 );",
  208. "vec4 mvPosition = modelViewMatrix * vec4( displacedPosition, 1.0 );",
  209. "#else",
  210. "vec4 worldPosition = modelMatrix * vec4( position, 1.0 );",
  211. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  212. "#endif",
  213. "gl_Position = projectionMatrix * mvPosition;",
  214. "vViewPosition = -mvPosition.xyz;",
  215. "vec3 normalTex = texture2D( tNormal, uvBase ).xyz * 2.0 - 1.0;",
  216. "vNormal = normalMatrix * normalTex;",
  217. THREE.ShaderChunk[ "shadowmap_vertex" ],
  218. "}"
  219. ].join("\n")
  220. }
  221. };