Water2Mesh.js 4.5 KB

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