ShaderTerrain.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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 and directional lights (use with "lights: true" material option)
  11. ------------------------------------------------------------------------- */
  12. 'terrain' : {
  13. uniforms: THREE.UniformsUtils.merge( [
  14. THREE.UniformsLib[ "fog" ],
  15. THREE.UniformsLib[ "lights" ],
  16. {
  17. "enableDiffuse1" : { type: "i", value: 0 },
  18. "enableDiffuse2" : { type: "i", value: 0 },
  19. "enableSpecular" : { type: "i", value: 0 },
  20. "enableReflection": { type: "i", value: 0 },
  21. "tDiffuse1" : { type: "t", value: 0, texture: null },
  22. "tDiffuse2" : { type: "t", value: 1, texture: null },
  23. "tDetail" : { type: "t", value: 2, texture: null },
  24. "tNormal" : { type: "t", value: 3, texture: null },
  25. "tSpecular" : { type: "t", value: 4, texture: null },
  26. "tDisplacement": { type: "t", value: 5, texture: null },
  27. "uNormalScale": { type: "f", value: 1.0 },
  28. "uDisplacementBias": { type: "f", value: 0.0 },
  29. "uDisplacementScale": { type: "f", value: 1.0 },
  30. "uDiffuseColor": { type: "c", value: new THREE.Color( 0xeeeeee ) },
  31. "uSpecularColor": { type: "c", value: new THREE.Color( 0x111111 ) },
  32. "uAmbientColor": { type: "c", value: new THREE.Color( 0x050505 ) },
  33. "uShininess": { type: "f", value: 30 },
  34. "uOpacity": { type: "f", value: 1 },
  35. "uRepeatBase" : { type: "v2", value: new THREE.Vector2( 1, 1 ) },
  36. "uRepeatOverlay" : { type: "v2", value: new THREE.Vector2( 1, 1 ) },
  37. "uOffset" : { type: "v2", value: new THREE.Vector2( 0, 0 ) }
  38. }
  39. ] ),
  40. fragmentShader: [
  41. "uniform vec3 uAmbientColor;",
  42. "uniform vec3 uDiffuseColor;",
  43. "uniform vec3 uSpecularColor;",
  44. "uniform float uShininess;",
  45. "uniform float uOpacity;",
  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_POINT_LIGHTS > 0",
  69. "uniform vec3 pointLightColor[ MAX_POINT_LIGHTS ];",
  70. "uniform vec3 pointLightPosition[ MAX_POINT_LIGHTS ];",
  71. "uniform float pointLightDistance[ MAX_POINT_LIGHTS ];",
  72. "#endif",
  73. "varying vec3 vViewPosition;",
  74. THREE.ShaderChunk[ "fog_pars_fragment" ],
  75. "void main() {",
  76. "gl_FragColor = vec4( vec3( 1.0 ), uOpacity );",
  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. "#ifdef GAMMA_INPUT",
  87. "colDiffuse1.xyz *= colDiffuse1.xyz;",
  88. "colDiffuse2.xyz *= colDiffuse2.xyz;",
  89. "#endif",
  90. "gl_FragColor = gl_FragColor * mix ( colDiffuse1, colDiffuse2, 1.0 - texture2D( tDisplacement, uvBase ) );",
  91. " } else if( enableDiffuse1 ) {",
  92. "gl_FragColor = gl_FragColor * texture2D( tDiffuse1, uvOverlay );",
  93. "} else if( enableDiffuse2 ) {",
  94. "gl_FragColor = gl_FragColor * texture2D( tDiffuse2, uvOverlay );",
  95. "}",
  96. "if( enableSpecular )",
  97. "specularTex = texture2D( tSpecular, uvOverlay ).xyz;",
  98. "mat3 tsb = mat3( vTangent, vBinormal, vNormal );",
  99. "vec3 finalNormal = tsb * normalTex;",
  100. "vec3 normal = normalize( finalNormal );",
  101. "vec3 viewPosition = normalize( vViewPosition );",
  102. // point lights
  103. "#if MAX_POINT_LIGHTS > 0",
  104. "vec3 pointDiffuse = vec3( 0.0 );",
  105. "vec3 pointSpecular = vec3( 0.0 );",
  106. "for ( int i = 0; i < MAX_POINT_LIGHTS; i ++ ) {",
  107. "vec4 lPosition = viewMatrix * vec4( pointLightPosition[ i ], 1.0 );",
  108. "vec3 lVector = lPosition.xyz + vViewPosition.xyz;",
  109. "float lDistance = 1.0;",
  110. "if ( pointLightDistance[ i ] > 0.0 )",
  111. "lDistance = 1.0 - min( ( length( lVector ) / pointLightDistance[ i ] ), 1.0 );",
  112. "lVector = normalize( lVector );",
  113. "vec3 pointHalfVector = normalize( lVector + viewPosition );",
  114. "float pointDistance = lDistance;",
  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, uShininess ), 0.0 );",
  118. "pointDiffuse += pointDistance * pointLightColor[ i ] * uDiffuseColor * pointDiffuseWeight;",
  119. "pointSpecular += pointDistance * pointLightColor[ i ] * uSpecularColor * pointSpecularWeight * pointDiffuseWeight;",
  120. "}",
  121. "#endif",
  122. // directional lights
  123. "#if MAX_DIR_LIGHTS > 0",
  124. "vec3 dirDiffuse = vec3( 0.0 );",
  125. "vec3 dirSpecular = vec3( 0.0 );",
  126. "for( int i = 0; i < MAX_DIR_LIGHTS; i++ ) {",
  127. "vec4 lDirection = viewMatrix * vec4( directionalLightDirection[ i ], 0.0 );",
  128. "vec3 dirVector = normalize( lDirection.xyz );",
  129. "vec3 dirHalfVector = normalize( dirVector + viewPosition );",
  130. "float dirDotNormalHalf = max( dot( normal, dirHalfVector ), 0.0 );",
  131. "float dirDiffuseWeight = max( dot( normal, dirVector ), 0.0 );",
  132. "float dirSpecularWeight = specularTex.r * max( pow( dirDotNormalHalf, uShininess ), 0.0 );",
  133. "dirDiffuse += directionalLightColor[ i ] * uDiffuseColor * dirDiffuseWeight;",
  134. "dirSpecular += directionalLightColor[ i ] * uSpecularColor * dirSpecularWeight * dirDiffuseWeight;",
  135. "}",
  136. "#endif",
  137. // all lights contribution summation
  138. "vec3 totalDiffuse = vec3( 0.0 );",
  139. "vec3 totalSpecular = vec3( 0.0 );",
  140. "#if MAX_DIR_LIGHTS > 0",
  141. "totalDiffuse += dirDiffuse;",
  142. "totalSpecular += dirSpecular;",
  143. "#endif",
  144. "#if MAX_POINT_LIGHTS > 0",
  145. "totalDiffuse += pointDiffuse;",
  146. "totalSpecular += pointSpecular;",
  147. "#endif",
  148. //"gl_FragColor.xyz = gl_FragColor.xyz * ( totalDiffuse + ambientLightColor * uAmbientColor) + totalSpecular;",
  149. "gl_FragColor.xyz = gl_FragColor.xyz * ( totalDiffuse + ambientLightColor * uAmbientColor + totalSpecular );",
  150. THREE.ShaderChunk[ "linear_to_gamma_fragment" ],
  151. THREE.ShaderChunk[ "fog_fragment" ],
  152. "}"
  153. ].join("\n"),
  154. vertexShader: [
  155. "attribute vec4 tangent;",
  156. "uniform vec2 uRepeatBase;",
  157. "uniform sampler2D tNormal;",
  158. "#ifdef VERTEX_TEXTURES",
  159. "uniform sampler2D tDisplacement;",
  160. "uniform float uDisplacementScale;",
  161. "uniform float uDisplacementBias;",
  162. "#endif",
  163. "varying vec3 vTangent;",
  164. "varying vec3 vBinormal;",
  165. "varying vec3 vNormal;",
  166. "varying vec2 vUv;",
  167. "varying vec3 vViewPosition;",
  168. "void main() {",
  169. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  170. "vViewPosition = -mvPosition.xyz;",
  171. "vNormal = normalize( normalMatrix * normal );",
  172. // tangent and binormal vectors
  173. "vTangent = normalize( normalMatrix * tangent.xyz );",
  174. "vBinormal = cross( vNormal, vTangent ) * tangent.w;",
  175. "vBinormal = normalize( vBinormal );",
  176. // texture coordinates
  177. "vUv = uv;",
  178. "vec2 uvBase = uv * uRepeatBase;",
  179. // displacement mapping
  180. "#ifdef VERTEX_TEXTURES",
  181. "vec3 dv = texture2D( tDisplacement, uvBase ).xyz;",
  182. "float df = uDisplacementScale * dv.x + uDisplacementBias;",
  183. "vec4 displacedPosition = vec4( vNormal.xyz * df, 0.0 ) + mvPosition;",
  184. "gl_Position = projectionMatrix * displacedPosition;",
  185. "#else",
  186. "gl_Position = projectionMatrix * mvPosition;",
  187. "#endif",
  188. "vec3 normalTex = texture2D( tNormal, uvBase ).xyz * 2.0 - 1.0;",
  189. "vNormal = normalMatrix * normalTex;",
  190. "}"
  191. ].join("\n")
  192. }
  193. };