Water2.js 8.6 KB

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