Water2.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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 ) !== undefined ? new THREE.Color( options.color ) : new THREE.Color( 0x7F7F7F );
  15. var textureWidth = options.textureWidth || 512;
  16. var textureHeight = options.textureHeight || 512;
  17. var clipBias = options.clipBias || 0;
  18. var speed = options.speed || 0.03; // water flow speed
  19. var reflectivity = options.reflectivity || 0.02; // water reflectivity
  20. var scale = options.scale || 1;
  21. var shader = options.shader || THREE.Water.WaterShader;
  22. var textureLoader = new THREE.TextureLoader();
  23. var flowMap = options.flowMap || textureLoader.load( 'textures/water/Water_1_M_Flow.jpg' );
  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. var mirror = new THREE.Mirror( width, height, {
  32. color: color,
  33. textureWidth: textureWidth,
  34. textureHeight: textureHeight,
  35. clipBias: clipBias
  36. } );
  37. var refractor = new THREE.Refractor( width, height, {
  38. color: color,
  39. textureWidth: textureWidth,
  40. textureHeight: textureHeight,
  41. clipBias: clipBias
  42. } );
  43. mirror.matrixAutoUpdate = false;
  44. refractor.matrixAutoUpdate = false;
  45. // material
  46. this.material = new THREE.ShaderMaterial( {
  47. uniforms: THREE.UniformsUtils.clone( shader.uniforms ),
  48. vertexShader: shader.vertexShader,
  49. fragmentShader: shader.fragmentShader,
  50. transparent: true
  51. } );
  52. // maps
  53. normalMap0.wrapS = normalMap0.wrapT = THREE.RepeatWrapping;
  54. normalMap1.wrapS = normalMap1.wrapT = THREE.RepeatWrapping;
  55. this.material.uniforms.tReflectionMap.value = mirror.getRenderTarget().texture;
  56. this.material.uniforms.tRefractionMap.value = refractor.getRenderTarget().texture;
  57. this.material.uniforms.tFlowMap.value = flowMap;
  58. this.material.uniforms.tNormalMap0.value = normalMap0;
  59. this.material.uniforms.tNormalMap1.value = normalMap1;
  60. // water
  61. this.material.uniforms.color.value = color;
  62. this.material.uniforms.reflectivity.value = reflectivity;
  63. this.material.uniforms.textureMatrix.value = textureMatrix;
  64. // inital values
  65. this.material.uniforms.config.value.x = 0; // flowMapOffset0
  66. this.material.uniforms.config.value.y = halfCycle; // flowMapOffset1
  67. this.material.uniforms.config.value.z = halfCycle; // halfCycle
  68. this.material.uniforms.config.value.w = scale; // scale
  69. // functions
  70. function updateTextureMatrix( camera ) {
  71. textureMatrix.set(
  72. 0.5, 0.0, 0.0, 0.5,
  73. 0.0, 0.5, 0.0, 0.5,
  74. 0.0, 0.0, 0.5, 0.5,
  75. 0.0, 0.0, 0.0, 1.0
  76. );
  77. textureMatrix.multiply( camera.projectionMatrix );
  78. textureMatrix.multiply( camera.matrixWorldInverse );
  79. textureMatrix.multiply( scope.matrixWorld );
  80. }
  81. function updateFlow( delta ) {
  82. var config = scope.material.uniforms.config;
  83. config.value.x += speed * delta; // flowMapOffset0
  84. config.value.y += speed * delta; // flowMapOffset1
  85. // reset properties if necessary
  86. if ( config.value.x >= cycle ) {
  87. config.value.x = 0;
  88. // avoid 'reset' effect when both offset are set to zero
  89. if ( config.value.y >= cycle ) {
  90. config.value.y = halfCycle;
  91. return;
  92. }
  93. }
  94. if ( config.value.y >= cycle ) {
  95. config.value.y = 0;
  96. }
  97. }
  98. //
  99. this.onBeforeRender = function ( renderer, scene, camera ) {
  100. var delta = clock.getDelta();
  101. updateTextureMatrix( camera );
  102. updateFlow( delta );
  103. scope.visible = false;
  104. mirror.matrixWorld.copy( scope.matrixWorld );
  105. refractor.matrixWorld.copy( scope.matrixWorld );
  106. mirror.onBeforeRender( renderer, scene, camera );
  107. refractor.onBeforeRender( renderer, scene, camera );
  108. scope.visible = true;
  109. };
  110. };
  111. THREE.Water.prototype = Object.create( THREE.Mesh.prototype );
  112. THREE.Water.prototype.constructor = THREE.Water;
  113. THREE.Water.WaterShader = {
  114. uniforms: {
  115. 'color': {
  116. type: 'c',
  117. value: null
  118. },
  119. 'reflectivity': {
  120. type: 'f',
  121. value: 0
  122. },
  123. 'tReflectionMap': {
  124. type: 't',
  125. value: null
  126. },
  127. 'tRefractionMap': {
  128. type: 't',
  129. value: null
  130. },
  131. 'tFlowMap': {
  132. type: 't',
  133. value: null
  134. },
  135. 'tNormalMap0': {
  136. type: 't',
  137. value: null
  138. },
  139. 'tNormalMap1': {
  140. type: 't',
  141. value: null
  142. },
  143. 'config': {
  144. type: 'v4',
  145. value: new THREE.Vector4()
  146. },
  147. 'textureMatrix': {
  148. type: 'm4',
  149. value: null
  150. }
  151. },
  152. vertexShader: [
  153. 'uniform mat4 textureMatrix;',
  154. 'varying vec4 vCoord;',
  155. 'varying vec2 vUv;',
  156. 'varying vec3 vToEye;',
  157. 'void main() {',
  158. ' vUv = uv;',
  159. ' vCoord = textureMatrix * vec4( position, 1.0 );',
  160. ' vec4 worldPosition = modelMatrix * vec4( position, 1.0 );',
  161. ' vToEye = cameraPosition - worldPosition.xyz;',
  162. ' gl_Position = projectionMatrix * viewMatrix * worldPosition;',
  163. '}'
  164. ].join( '\n' ),
  165. fragmentShader: [
  166. 'uniform sampler2D tReflectionMap;',
  167. 'uniform sampler2D tRefractionMap;',
  168. 'uniform sampler2D tFlowMap;',
  169. 'uniform sampler2D tNormalMap0;',
  170. 'uniform sampler2D tNormalMap1;',
  171. 'uniform vec3 color;',
  172. 'uniform float reflectivity;',
  173. 'uniform vec4 config;',
  174. 'varying vec4 vCoord;',
  175. 'varying vec2 vUv;',
  176. 'varying vec3 vToEye;',
  177. 'void main() {',
  178. ' float flowMapOffset0 = config.x;',
  179. ' float flowMapOffset1 = config.y;',
  180. ' float halfCycle = config.z;',
  181. ' float scale = config.w;',
  182. ' vec3 toEye = normalize( vToEye );',
  183. // sample flow map
  184. ' vec2 flow = texture2D( tFlowMap, vUv ).rg * 2.0 - 1.0;',
  185. // sample normal maps
  186. ' vec4 normalColor0 = texture2D( tNormalMap0, ( vUv * scale ) + flow * flowMapOffset0 );',
  187. ' vec4 normalColor1 = texture2D( tNormalMap1, ( vUv * scale ) + flow * flowMapOffset1 );',
  188. // linear interpolate to get the final normal color
  189. ' float flowLerp = abs( halfCycle - flowMapOffset0 ) / halfCycle;',
  190. ' vec4 normalColor = mix( normalColor0, normalColor1, flowLerp );',
  191. // calculate normal vector
  192. ' vec3 normal = normalize( vec3( normalColor.r * 2.0 - 1.0, normalColor.b, normalColor.g * 2.0 - 1.0 ) );',
  193. // fresnel effect
  194. ' float theta = max( dot( toEye, normal ), 0.0 );',
  195. ' float reflectance = reflectivity + ( 1.0 - reflectivity ) * pow( ( 1.0 - theta ), 5.0 );',
  196. // sample textures
  197. ' vec3 coord = vCoord.xyz / vCoord.w;',
  198. ' vec2 uv = coord.xy + coord.z * normal.xz * 0.05;',
  199. ' vec4 reflectColor = texture2D( tReflectionMap, uv );',
  200. ' vec4 refractColor = texture2D( tRefractionMap, uv );',
  201. // multiply water color with the mix of both textures. then add lighting
  202. ' gl_FragColor = vec4( color, 1.0 ) * mix( refractColor, reflectColor, reflectance );',
  203. '}'
  204. ].join( '\n' )
  205. };