Water2.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. ( function () {
  2. /**
  3. * References:
  4. * https://alex.vlachos.com/graphics/Vlachos-SIGGRAPH10-WaterFlow.pdf
  5. * http://graphicsrunner.blogspot.de/2010/08/water-using-flow-maps.html
  6. *
  7. */
  8. class Water extends THREE.Mesh {
  9. constructor( geometry, options = {} ) {
  10. super( geometry );
  11. this.type = 'Water';
  12. const scope = this;
  13. const color = options.color !== undefined ? new THREE.Color( options.color ) : new THREE.Color( 0xFFFFFF );
  14. const textureWidth = options.textureWidth || 512;
  15. const textureHeight = options.textureHeight || 512;
  16. const clipBias = options.clipBias || 0;
  17. const flowDirection = options.flowDirection || new THREE.Vector2( 1, 0 );
  18. const flowSpeed = options.flowSpeed || 0.03;
  19. const reflectivity = options.reflectivity || 0.02;
  20. const scale = options.scale || 1;
  21. const shader = options.shader || Water.WaterShader;
  22. const textureLoader = new THREE.TextureLoader();
  23. const flowMap = options.flowMap || undefined;
  24. const normalMap0 = options.normalMap0 || textureLoader.load( 'textures/water/Water_1_M_Normal.jpg' );
  25. const normalMap1 = options.normalMap1 || textureLoader.load( 'textures/water/Water_2_M_Normal.jpg' );
  26. const cycle = 0.15; // a cycle of a flow map phase
  27. const halfCycle = cycle * 0.5;
  28. const textureMatrix = new THREE.Matrix4();
  29. const clock = new THREE.Clock(); // internal components
  30. if ( THREE.Reflector === undefined ) {
  31. console.error( 'THREE.Water: Required component THREE.Reflector not found.' );
  32. return;
  33. }
  34. if ( THREE.Refractor === undefined ) {
  35. console.error( 'THREE.Water: Required component THREE.Refractor not found.' );
  36. return;
  37. }
  38. const reflector = new THREE.Reflector( geometry, {
  39. textureWidth: textureWidth,
  40. textureHeight: textureHeight,
  41. clipBias: clipBias
  42. } );
  43. const refractor = new THREE.Refractor( geometry, {
  44. textureWidth: textureWidth,
  45. textureHeight: textureHeight,
  46. clipBias: clipBias
  47. } );
  48. reflector.matrixAutoUpdate = false;
  49. refractor.matrixAutoUpdate = false; // material
  50. this.material = new THREE.ShaderMaterial( {
  51. uniforms: THREE.UniformsUtils.merge( [ THREE.UniformsLib[ 'fog' ], shader.uniforms ] ),
  52. vertexShader: shader.vertexShader,
  53. fragmentShader: shader.fragmentShader,
  54. transparent: true,
  55. fog: true
  56. } );
  57. if ( flowMap !== undefined ) {
  58. this.material.defines.USE_FLOWMAP = '';
  59. this.material.uniforms[ 'tFlowMap' ] = {
  60. type: 't',
  61. value: flowMap
  62. };
  63. } else {
  64. this.material.uniforms[ 'flowDirection' ] = {
  65. type: 'v2',
  66. value: flowDirection
  67. };
  68. } // maps
  69. normalMap0.wrapS = normalMap0.wrapT = THREE.RepeatWrapping;
  70. normalMap1.wrapS = normalMap1.wrapT = THREE.RepeatWrapping;
  71. this.material.uniforms[ 'tReflectionMap' ].value = reflector.getRenderTarget().texture;
  72. this.material.uniforms[ 'tRefractionMap' ].value = refractor.getRenderTarget().texture;
  73. this.material.uniforms[ 'tNormalMap0' ].value = normalMap0;
  74. this.material.uniforms[ 'tNormalMap1' ].value = normalMap1; // water
  75. this.material.uniforms[ 'color' ].value = color;
  76. this.material.uniforms[ 'reflectivity' ].value = reflectivity;
  77. this.material.uniforms[ 'textureMatrix' ].value = textureMatrix; // inital values
  78. this.material.uniforms[ 'config' ].value.x = 0; // flowMapOffset0
  79. this.material.uniforms[ 'config' ].value.y = halfCycle; // flowMapOffset1
  80. this.material.uniforms[ 'config' ].value.z = halfCycle; // halfCycle
  81. this.material.uniforms[ 'config' ].value.w = scale; // scale
  82. // functions
  83. function updateTextureMatrix( camera ) {
  84. textureMatrix.set( 0.5, 0.0, 0.0, 0.5, 0.0, 0.5, 0.0, 0.5, 0.0, 0.0, 0.5, 0.5, 0.0, 0.0, 0.0, 1.0 );
  85. textureMatrix.multiply( camera.projectionMatrix );
  86. textureMatrix.multiply( camera.matrixWorldInverse );
  87. textureMatrix.multiply( scope.matrixWorld );
  88. }
  89. function updateFlow() {
  90. const delta = clock.getDelta();
  91. const config = scope.material.uniforms[ 'config' ];
  92. config.value.x += flowSpeed * delta; // flowMapOffset0
  93. config.value.y = config.value.x + halfCycle; // flowMapOffset1
  94. // Important: The distance between offsets should be always the value of "halfCycle".
  95. // Moreover, both offsets should be in the range of [ 0, cycle ].
  96. // This approach ensures a smooth water flow and avoids "reset" effects.
  97. if ( config.value.x >= cycle ) {
  98. config.value.x = 0;
  99. config.value.y = halfCycle;
  100. } else if ( config.value.y >= cycle ) {
  101. config.value.y = config.value.y - cycle;
  102. }
  103. } //
  104. this.onBeforeRender = function ( renderer, scene, camera ) {
  105. updateTextureMatrix( camera );
  106. updateFlow();
  107. scope.visible = false;
  108. reflector.matrixWorld.copy( scope.matrixWorld );
  109. refractor.matrixWorld.copy( scope.matrixWorld );
  110. reflector.onBeforeRender( renderer, scene, camera );
  111. refractor.onBeforeRender( renderer, scene, camera );
  112. scope.visible = true;
  113. };
  114. }
  115. }
  116. Water.prototype.isWater = true;
  117. Water.WaterShader = {
  118. uniforms: {
  119. 'color': {
  120. type: 'c',
  121. value: null
  122. },
  123. 'reflectivity': {
  124. type: 'f',
  125. value: 0
  126. },
  127. 'tReflectionMap': {
  128. type: 't',
  129. value: null
  130. },
  131. 'tRefractionMap': {
  132. type: 't',
  133. value: null
  134. },
  135. 'tNormalMap0': {
  136. type: 't',
  137. value: null
  138. },
  139. 'tNormalMap1': {
  140. type: 't',
  141. value: null
  142. },
  143. 'textureMatrix': {
  144. type: 'm4',
  145. value: null
  146. },
  147. 'config': {
  148. type: 'v4',
  149. value: new THREE.Vector4()
  150. }
  151. },
  152. vertexShader:
  153. /* glsl */
  154. `
  155. #include <common>
  156. #include <fog_pars_vertex>
  157. #include <logdepthbuf_pars_vertex>
  158. uniform mat4 textureMatrix;
  159. varying vec4 vCoord;
  160. varying vec2 vUv;
  161. varying vec3 vToEye;
  162. void main() {
  163. vUv = uv;
  164. vCoord = textureMatrix * vec4( position, 1.0 );
  165. vec4 worldPosition = modelMatrix * vec4( position, 1.0 );
  166. vToEye = cameraPosition - worldPosition.xyz;
  167. vec4 mvPosition = viewMatrix * worldPosition; // used in fog_vertex
  168. gl_Position = projectionMatrix * mvPosition;
  169. #include <logdepthbuf_vertex>
  170. #include <fog_vertex>
  171. }`,
  172. fragmentShader:
  173. /* glsl */
  174. `
  175. #include <common>
  176. #include <fog_pars_fragment>
  177. #include <logdepthbuf_pars_fragment>
  178. uniform sampler2D tReflectionMap;
  179. uniform sampler2D tRefractionMap;
  180. uniform sampler2D tNormalMap0;
  181. uniform sampler2D tNormalMap1;
  182. #ifdef USE_FLOWMAP
  183. uniform sampler2D tFlowMap;
  184. #else
  185. uniform vec2 flowDirection;
  186. #endif
  187. uniform vec3 color;
  188. uniform float reflectivity;
  189. uniform vec4 config;
  190. varying vec4 vCoord;
  191. varying vec2 vUv;
  192. varying vec3 vToEye;
  193. void main() {
  194. #include <logdepthbuf_fragment>
  195. float flowMapOffset0 = config.x;
  196. float flowMapOffset1 = config.y;
  197. float halfCycle = config.z;
  198. float scale = config.w;
  199. vec3 toEye = normalize( vToEye );
  200. // determine flow direction
  201. vec2 flow;
  202. #ifdef USE_FLOWMAP
  203. flow = texture2D( tFlowMap, vUv ).rg * 2.0 - 1.0;
  204. #else
  205. flow = flowDirection;
  206. #endif
  207. flow.x *= - 1.0;
  208. // sample normal maps (distort uvs with flowdata)
  209. vec4 normalColor0 = texture2D( tNormalMap0, ( vUv * scale ) + flow * flowMapOffset0 );
  210. vec4 normalColor1 = texture2D( tNormalMap1, ( vUv * scale ) + flow * flowMapOffset1 );
  211. // linear interpolate to get the final normal color
  212. float flowLerp = abs( halfCycle - flowMapOffset0 ) / halfCycle;
  213. vec4 normalColor = mix( normalColor0, normalColor1, flowLerp );
  214. // calculate normal vector
  215. vec3 normal = normalize( vec3( normalColor.r * 2.0 - 1.0, normalColor.b, normalColor.g * 2.0 - 1.0 ) );
  216. // calculate the fresnel term to blend reflection and refraction maps
  217. float theta = max( dot( toEye, normal ), 0.0 );
  218. float reflectance = reflectivity + ( 1.0 - reflectivity ) * pow( ( 1.0 - theta ), 5.0 );
  219. // calculate final uv coords
  220. vec3 coord = vCoord.xyz / vCoord.w;
  221. vec2 uv = coord.xy + coord.z * normal.xz * 0.05;
  222. vec4 reflectColor = texture2D( tReflectionMap, vec2( 1.0 - uv.x, uv.y ) );
  223. vec4 refractColor = texture2D( tRefractionMap, uv );
  224. // multiply water color with the mix of both textures
  225. gl_FragColor = vec4( color, 1.0 ) * mix( refractColor, reflectColor, reflectance );
  226. #include <tonemapping_fragment>
  227. #include <encodings_fragment>
  228. #include <fog_fragment>
  229. }`
  230. };
  231. THREE.Water = Water;
  232. } )();