Water2.js 7.3 KB

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