WaterMesh.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. import {
  2. Color,
  3. Mesh,
  4. Vector3,
  5. MeshLambertNodeMaterial
  6. } from 'three/webgpu';
  7. import { Fn, add, cameraPosition, div, normalize, positionWorld, sub, time, texture, vec2, vec3, max, dot, reflect, pow, length, float, uniform, reflector, mul, mix, diffuseColor } from 'three/tsl';
  8. /**
  9. * A basic flat, reflective water effect.
  10. *
  11. * Note that this class can only be used with {@link WebGPURenderer}.
  12. * When using {@link WebGLRenderer}, use {@link Water}.
  13. *
  14. * References:
  15. *
  16. * - [Flat mirror for three.js]{@link https://github.com/Slayvin}
  17. * - [An implementation of water shader based on the flat mirror]{@link https://home.adelphi.edu/~stemkoski/}
  18. * - [Water shader explanations in WebGL]{@link http://29a.ch/slides/2012/webglwater/ }
  19. *
  20. * @augments Mesh
  21. */
  22. class WaterMesh extends Mesh {
  23. /**
  24. * Constructs a new water mesh.
  25. *
  26. * @param {BufferGeometry} geometry - The water mesh's geometry.
  27. * @param {WaterMesh~Options} [options] - The configuration options.
  28. */
  29. constructor( geometry, options ) {
  30. const material = new MeshLambertNodeMaterial();
  31. super( geometry, material );
  32. /**
  33. * This flag can be used for type testing.
  34. *
  35. * @type {boolean}
  36. * @readonly
  37. * @default true
  38. */
  39. this.isWaterMesh = true;
  40. /**
  41. * The effect's resolution scale.
  42. *
  43. * @type {number}
  44. * @default 0.5
  45. */
  46. this.resolution = options.resolution !== undefined ? options.resolution : 0.5;
  47. // Uniforms
  48. /**
  49. * The water's normal map.
  50. *
  51. * @type {TextureNode}
  52. */
  53. this.waterNormals = texture( options.waterNormals );
  54. /**
  55. * The alpha value.
  56. *
  57. * @type {UniformNode<float>}
  58. * @default 1
  59. */
  60. this.alpha = uniform( options.alpha !== undefined ? options.alpha : 1.0 );
  61. /**
  62. * The size value.
  63. *
  64. * @type {UniformNode<float>}
  65. * @default 1
  66. */
  67. this.size = uniform( options.size !== undefined ? options.size : 1.0 );
  68. /**
  69. * The sun color.
  70. *
  71. * @type {UniformNode<color>}
  72. * @default 0xffffff
  73. */
  74. this.sunColor = uniform( new Color( options.sunColor !== undefined ? options.sunColor : 0xffffff ) );
  75. /**
  76. * The sun direction.
  77. *
  78. * @type {UniformNode<vec3>}
  79. * @default (0.70707,0.70707,0.0)
  80. */
  81. this.sunDirection = uniform( options.sunDirection !== undefined ? options.sunDirection : new Vector3( 0.70707, 0.70707, 0.0 ) );
  82. /**
  83. * The water color.
  84. *
  85. * @type {UniformNode<color>}
  86. * @default 0x7f7f7f
  87. */
  88. this.waterColor = uniform( new Color( options.waterColor !== undefined ? options.waterColor : 0x7f7f7f ) );
  89. /**
  90. * The distortion scale.
  91. *
  92. * @type {UniformNode<float>}
  93. * @default 20
  94. */
  95. this.distortionScale = uniform( options.distortionScale !== undefined ? options.distortionScale : 20.0 );
  96. // TSL
  97. const getNoise = Fn( ( [ uv ] ) => {
  98. const offset = time;
  99. const uv0 = add( div( uv, 103 ), vec2( div( offset, 17 ), div( offset, 29 ) ) ).toVar();
  100. const uv1 = div( uv, 107 ).sub( vec2( div( offset, - 19 ), div( offset, 31 ) ) ).toVar();
  101. const uv2 = add( div( uv, vec2( 8907.0, 9803.0 ) ), vec2( div( offset, 101 ), div( offset, 97 ) ) ).toVar();
  102. const uv3 = sub( div( uv, vec2( 1091.0, 1027.0 ) ), vec2( div( offset, 109 ), div( offset, - 113 ) ) ).toVar();
  103. const sample0 = this.waterNormals.sample( uv0 );
  104. const sample1 = this.waterNormals.sample( uv1 );
  105. const sample2 = this.waterNormals.sample( uv2 );
  106. const sample3 = this.waterNormals.sample( uv3 );
  107. const noise = sample0.add( sample1 ).add( sample2 ).add( sample3 );
  108. return noise.mul( 0.5 ).sub( 1 );
  109. } );
  110. const noise = getNoise( positionWorld.xz.mul( this.size ) );
  111. const surfaceNormal = normalize( noise.xzy.mul( 1.5, 1.0, 1.5 ) );
  112. const worldToEye = cameraPosition.sub( positionWorld );
  113. const eyeDirection = normalize( worldToEye );
  114. const reflection = normalize( reflect( this.sunDirection.negate(), surfaceNormal ) );
  115. const direction = max( 0.0, dot( eyeDirection, reflection ) );
  116. const specularLight = pow( direction, 100 ).mul( this.sunColor ).mul( 2.0 );
  117. const diffuseLight = max( dot( this.sunDirection, surfaceNormal ), 0.0 ).mul( this.sunColor ).mul( 0.5 );
  118. const distance = length( worldToEye );
  119. const distortion = surfaceNormal.xz.mul( float( 0.001 ).add( float( 1.0 ).div( distance ) ) ).mul( this.distortionScale );
  120. // Material
  121. material.transparent = true;
  122. material.opacityNode = this.alpha;
  123. material.shadowPositionNode = positionWorld.add( distortion );
  124. material.setupOutgoingLight = () => diffuseColor.rgb; // backwards compatibility
  125. material.colorNode = Fn( () => {
  126. const mirrorSampler = reflector();
  127. mirrorSampler.uvNode = mirrorSampler.uvNode.add( distortion );
  128. mirrorSampler.resolution = this.resolution;
  129. this.add( mirrorSampler.target );
  130. const theta = max( dot( eyeDirection, surfaceNormal ), 0.0 );
  131. const rf0 = float( 0.3 );
  132. const reflectance = mul( pow( float( 1.0 ).sub( theta ), 5.0 ), float( 1.0 ).sub( rf0 ) ).add( rf0 );
  133. const scatter = max( 0.0, dot( surfaceNormal, eyeDirection ) ).mul( this.waterColor );
  134. const albedo = mix( this.sunColor.mul( diffuseLight ).mul( 0.3 ).add( scatter ), mirrorSampler.rgb.mul( specularLight ).add( mirrorSampler.rgb.mul( 0.9 ) ).add( vec3( 0.1 ) ), reflectance );
  135. return albedo;
  136. } )();
  137. }
  138. }
  139. /**
  140. * Constructor options of `WaterMesh`.
  141. *
  142. * @typedef {Object} WaterMesh~Options
  143. * @property {number} [resolution=0.5] - The resolution scale.
  144. * @property {?Texture} [waterNormals=null] - The water's normal map.
  145. * @property {number} [alpha=1] - The alpha value.
  146. * @property {number} [size=1] - The size value.
  147. * @property {number|Color|string} [sunColor=0xffffff] - The sun color.
  148. * @property {Vector3} [sunDirection=(0.70707,0.70707,0.0)] - The sun direction.
  149. * @property {number|Color|string} [waterColor=0x7F7F7F] - The water color.
  150. * @property {number} [distortionScale=20] - The distortion scale.
  151. **/
  152. export { WaterMesh };
粤ICP备19079148号