Water2.js 8.0 KB

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