Water2.js 8.3 KB

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