Water2Mesh.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. import {
  2. Color,
  3. Mesh,
  4. NodeMaterial,
  5. Vector2,
  6. Vector3
  7. } from 'three';
  8. import { vec2, viewportSafeUV, viewportSharedTexture, reflector, pow, float, abs, texture, uniform, TempNode, NodeUpdateType, vec4, Fn, cameraPosition, positionWorld, uv, mix, vec3, normalize, max, dot, screenUV } from 'three/tsl';
  9. /**
  10. * References:
  11. * https://alex.vlachos.com/graphics/Vlachos-SIGGRAPH10-WaterFlow.pdf
  12. * http://graphicsrunner.blogspot.de/2010/08/water-using-flow-maps.html
  13. *
  14. */
  15. class WaterMesh extends Mesh {
  16. constructor( geometry, options = {} ) {
  17. const material = new NodeMaterial();
  18. super( geometry, material );
  19. this.isWater = true;
  20. material.fragmentNode = new WaterNode( options, this );
  21. }
  22. }
  23. class WaterNode extends TempNode {
  24. constructor( options, waterBody ) {
  25. super( 'vec4' );
  26. this.waterBody = waterBody;
  27. this.normalMap0 = texture( options.normalMap0 );
  28. this.normalMap1 = texture( options.normalMap1 );
  29. this.flowMap = texture( options.flowMap !== undefined ? options.flowMap : null );
  30. this.color = uniform( options.color !== undefined ? new Color( options.color ) : new Color( 0xffffff ) );
  31. this.flowDirection = uniform( options.flowDirection !== undefined ? options.flowDirection : new Vector2( 1, 0 ) );
  32. this.flowSpeed = uniform( options.flowSpeed !== undefined ? options.flowSpeed : 0.03 );
  33. this.reflectivity = uniform( options.reflectivity !== undefined ? options.reflectivity : 0.02 );
  34. this.scale = uniform( options.scale !== undefined ? options.scale : 1 );
  35. this.flowConfig = uniform( new Vector3() );
  36. this.updateBeforeType = NodeUpdateType.RENDER;
  37. this._cycle = 0.15; // a cycle of a flow map phase
  38. this._halfCycle = this._cycle * 0.5;
  39. this._USE_FLOW = options.flowMap !== undefined;
  40. }
  41. updateFlow( delta ) {
  42. this.flowConfig.value.x += this.flowSpeed.value * delta; // flowMapOffset0
  43. this.flowConfig.value.y = this.flowConfig.value.x + this._halfCycle; // flowMapOffset1
  44. // Important: The distance between offsets should be always the value of "halfCycle".
  45. // Moreover, both offsets should be in the range of [ 0, cycle ].
  46. // This approach ensures a smooth water flow and avoids "reset" effects.
  47. if ( this.flowConfig.value.x >= this._cycle ) {
  48. this.flowConfig.value.x = 0;
  49. this.flowConfig.value.y = this._halfCycle;
  50. } else if ( this.flowConfig.value.y >= this._cycle ) {
  51. this.flowConfig.value.y = this.flowConfig.value.y - this._cycle;
  52. }
  53. this.flowConfig.value.z = this._halfCycle;
  54. }
  55. updateBefore( frame ) {
  56. this.updateFlow( frame.deltaTime );
  57. }
  58. setup() {
  59. const outputNode = Fn( () => {
  60. const flowMapOffset0 = this.flowConfig.x;
  61. const flowMapOffset1 = this.flowConfig.y;
  62. const halfCycle = this.flowConfig.z;
  63. const toEye = normalize( cameraPosition.sub( positionWorld ) );
  64. let flow;
  65. if ( this._USE_FLOW === true ) {
  66. flow = this.flowMap.rg.mul( 2 ).sub( 1 );
  67. } else {
  68. flow = vec2( this.flowDirection.x, this.flowDirection.y );
  69. }
  70. flow.x.mulAssign( - 1 );
  71. // sample normal maps (distort uvs with flowdata)
  72. const uvs = uv();
  73. const normalUv0 = uvs.mul( this.scale ).add( flow.mul( flowMapOffset0 ) );
  74. const normalUv1 = uvs.mul( this.scale ).add( flow.mul( flowMapOffset1 ) );
  75. const normalColor0 = this.normalMap0.uv( normalUv0 );
  76. const normalColor1 = this.normalMap1.uv( normalUv1 );
  77. // linear interpolate to get the final normal color
  78. const flowLerp = abs( halfCycle.sub( flowMapOffset0 ) ).div( halfCycle );
  79. const normalColor = mix( normalColor0, normalColor1, flowLerp );
  80. // calculate normal vector
  81. const normal = normalize( vec3( normalColor.r.mul( 2 ).sub( 1 ), normalColor.b, normalColor.g.mul( 2 ).sub( 1 ) ) );
  82. // calculate the fresnel term to blend reflection and refraction maps
  83. const theta = max( dot( toEye, normal ), 0 );
  84. const reflectance = pow( float( 1.0 ).sub( theta ), 5.0 ).mul( float( 1.0 ).sub( this.reflectivity ) ).add( this.reflectivity );
  85. // reflector, refractor
  86. const offset = normal.xz.mul( 0.05 ).toVar();
  87. const reflectionSampler = reflector();
  88. this.waterBody.add( reflectionSampler.target );
  89. reflectionSampler.uvNode = reflectionSampler.uvNode.add( offset );
  90. const refractorUV = screenUV.add( offset );
  91. const refractionSampler = viewportSharedTexture( viewportSafeUV( refractorUV ) );
  92. // calculate final uv coords
  93. return vec4( this.color, 1.0 ).mul( mix( refractionSampler, reflectionSampler, reflectance ) );
  94. } )();
  95. return outputNode;
  96. }
  97. }
  98. export { WaterMesh };