Water2.js 8.7 KB

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