Water2.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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 ( width, height, options ) {
  10. THREE.Mesh.call( this, new THREE.PlaneBufferGeometry( width, height ) );
  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.Mirror === undefined ) {
  33. console.error( 'THREE.Water: Required component THREE.Mirror 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 mirror = new THREE.Mirror( width, height, {
  41. textureWidth: textureWidth,
  42. textureHeight: textureHeight,
  43. clipBias: clipBias
  44. } );
  45. var refractor = new THREE.Refractor( width, height, {
  46. textureWidth: textureWidth,
  47. textureHeight: textureHeight,
  48. clipBias: clipBias
  49. } );
  50. mirror.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 = mirror.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( delta ) {
  104. var config = scope.material.uniforms.config;
  105. config.value.x += flowSpeed * delta; // flowMapOffset0
  106. config.value.y += flowSpeed * delta; // flowMapOffset1
  107. // reset properties if necessary
  108. if ( config.value.x >= cycle ) {
  109. config.value.x = 0;
  110. // avoid 'reset' effect when both offset are set to zero
  111. if ( config.value.y >= cycle ) {
  112. config.value.y = halfCycle;
  113. return;
  114. }
  115. }
  116. if ( config.value.y >= cycle ) {
  117. config.value.y = 0;
  118. }
  119. }
  120. //
  121. this.onBeforeRender = function ( renderer, scene, camera ) {
  122. var delta = clock.getDelta();
  123. updateTextureMatrix( camera );
  124. updateFlow( delta );
  125. scope.visible = false;
  126. mirror.matrixWorld.copy( scope.matrixWorld );
  127. refractor.matrixWorld.copy( scope.matrixWorld );
  128. mirror.onBeforeRender( renderer, scene, camera );
  129. refractor.onBeforeRender( renderer, scene, camera );
  130. scope.visible = true;
  131. };
  132. };
  133. THREE.Water.prototype = Object.create( THREE.Mesh.prototype );
  134. THREE.Water.prototype.constructor = THREE.Water;
  135. THREE.Water.WaterShader = {
  136. uniforms: {
  137. 'color': {
  138. type: 'c',
  139. value: null
  140. },
  141. 'reflectivity': {
  142. type: 'f',
  143. value: 0
  144. },
  145. 'tReflectionMap': {
  146. type: 't',
  147. value: null
  148. },
  149. 'tRefractionMap': {
  150. type: 't',
  151. value: null
  152. },
  153. 'tNormalMap0': {
  154. type: 't',
  155. value: null
  156. },
  157. 'tNormalMap1': {
  158. type: 't',
  159. value: null
  160. },
  161. 'textureMatrix': {
  162. type: 'm4',
  163. value: null
  164. },
  165. 'config': {
  166. type: 'v4',
  167. value: new THREE.Vector4()
  168. }
  169. },
  170. vertexShader: [
  171. '#include <fog_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 <fog_vertex>',
  184. '}'
  185. ].join( '\n' ),
  186. fragmentShader: [
  187. '#include <fog_pars_fragment>',
  188. 'uniform sampler2D tReflectionMap;',
  189. 'uniform sampler2D tRefractionMap;',
  190. 'uniform sampler2D tNormalMap0;',
  191. 'uniform sampler2D tNormalMap1;',
  192. '#ifdef USE_FLOWMAP',
  193. ' uniform sampler2D tFlowMap;',
  194. '#else',
  195. ' uniform vec2 flowDirection;',
  196. '#endif',
  197. 'uniform vec3 color;',
  198. 'uniform float reflectivity;',
  199. 'uniform vec4 config;',
  200. 'varying vec4 vCoord;',
  201. 'varying vec2 vUv;',
  202. 'varying vec3 vToEye;',
  203. 'void main() {',
  204. ' float flowMapOffset0 = config.x;',
  205. ' float flowMapOffset1 = config.y;',
  206. ' float halfCycle = config.z;',
  207. ' float scale = config.w;',
  208. ' vec3 toEye = normalize( vToEye );',
  209. // determine flow direction
  210. ' vec2 flow;',
  211. ' #ifdef USE_FLOWMAP',
  212. ' flow = texture2D( tFlowMap, vUv ).rg * 2.0 - 1.0;',
  213. ' #else',
  214. ' flow = flowDirection;',
  215. ' #endif',
  216. ' flow.x *= - 1.0;',
  217. // sample normal maps (distort uvs with flowdata)
  218. ' vec4 normalColor0 = texture2D( tNormalMap0, ( vUv * scale ) + flow * flowMapOffset0 );',
  219. ' vec4 normalColor1 = texture2D( tNormalMap1, ( vUv * scale ) + flow * flowMapOffset1 );',
  220. // linear interpolate to get the final normal color
  221. ' float flowLerp = abs( halfCycle - flowMapOffset0 ) / halfCycle;',
  222. ' vec4 normalColor = mix( normalColor0, normalColor1, flowLerp );',
  223. // calculate normal vector
  224. ' vec3 normal = normalize( vec3( normalColor.r * 2.0 - 1.0, normalColor.b, normalColor.g * 2.0 - 1.0 ) );',
  225. // calculate the fresnel term to blend reflection and refraction maps
  226. ' float theta = max( dot( toEye, normal ), 0.0 );',
  227. ' float reflectance = reflectivity + ( 1.0 - reflectivity ) * pow( ( 1.0 - theta ), 5.0 );',
  228. // calculate final uv coords
  229. ' vec3 coord = vCoord.xyz / vCoord.w;',
  230. ' vec2 uv = coord.xy + coord.z * normal.xz * 0.05;',
  231. ' vec4 reflectColor = texture2D( tReflectionMap, uv );',
  232. ' vec4 refractColor = texture2D( tRefractionMap, uv );',
  233. // multiply water color with the mix of both textures
  234. ' gl_FragColor = vec4( color, 1.0 ) * mix( refractColor, reflectColor, reflectance );',
  235. '#include <tonemapping_fragment>',
  236. '#include <encodings_fragment>',
  237. '#include <fog_fragment>',
  238. '}'
  239. ].join( '\n' )
  240. };