three-base 2.0.142 → 2.0.145

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (227) hide show
  1. package/DirectionalLight.d.ts +6 -6
  2. package/DirectionalLight.js +68 -68
  3. package/EventMap.d.ts +103 -103
  4. package/EventMap.js +17 -17
  5. package/EventMesh.d.ts +18 -18
  6. package/EventMesh.js +59 -59
  7. package/ExtrudeMesh.d.ts +112 -112
  8. package/ExtrudeMesh.js +342 -342
  9. package/FPScontrol.d.ts +511 -511
  10. package/FPScontrol.js +1799 -1799
  11. package/FPScontrol.ts +1855 -1855
  12. package/Fire/Fire.d.ts +28 -28
  13. package/Fire/Fire.js +80 -80
  14. package/Fire/FireGeometry.d.ts +9 -9
  15. package/Fire/FireGeometry.js +40 -40
  16. package/Fire/FireMaterial.d.ts +28 -28
  17. package/Fire/FireMaterial.js +32 -32
  18. package/Fire/index.d.ts +2 -2
  19. package/Fire/index.js +2 -2
  20. package/GLTFDracoLoader.d.ts +71 -71
  21. package/GLTFDracoLoader.js +266 -266
  22. package/GLTFDracoLoader.ts +1 -1
  23. package/Grass/Grass.d.ts +8 -8
  24. package/Grass/Grass.js +38 -38
  25. package/Grass/index.d.ts +2 -2
  26. package/Grass/index.js +2 -2
  27. package/Ground1/Ground1.d.ts +10 -10
  28. package/Ground1/Ground1.js +71 -71
  29. package/Ground1/index.d.ts +1 -1
  30. package/Ground1/index.js +1 -1
  31. package/MeshBase.d.ts +36 -36
  32. package/MeshBase.js +247 -247
  33. package/Mirror.d.ts +25 -25
  34. package/Mirror.js +143 -143
  35. package/MktMesh/MktMesh.d.ts +301 -291
  36. package/MktMesh/MktMesh.js +1064 -1045
  37. package/MktMesh/MktMesh.ts +32 -11
  38. package/MktMesh/assetUrl.d.ts +59 -35
  39. package/MktMesh/assetUrl.js +154 -69
  40. package/MktMesh/assetUrl.ts +155 -69
  41. package/MktMesh/mktMappingShader.d.ts +12 -12
  42. package/MktMesh/mktMappingShader.js +138 -138
  43. package/MktMesh/mktMappingShader.ts +139 -139
  44. package/MktMesh/mktShader.d.ts +263 -263
  45. package/MktMesh/mktShader.js +1236 -1236
  46. package/MktMesh/mktShader.ts +1344 -1344
  47. package/MktMesh/panoBitmapTexture.d.ts +25 -25
  48. package/MktMesh/panoBitmapTexture.js +52 -52
  49. package/MktMesh/panoBitmapTexture.ts +54 -54
  50. package/MktMesh/panoSlotBind.d.ts +29 -29
  51. package/MktMesh/panoSlotBind.js +106 -106
  52. package/MktMesh/panoSlotBind.ts +153 -153
  53. package/MktMesh/panoSlotRoster.d.ts +22 -22
  54. package/MktMesh/panoSlotRoster.js +169 -169
  55. package/MktMesh/panoSlotRoster.ts +168 -168
  56. package/MktMesh/panoTileStitcher.d.ts +119 -119
  57. package/MktMesh/panoTileStitcher.js +529 -529
  58. package/MktMesh/panoTileStitcher.ts +616 -616
  59. package/MktMesh/types.d.ts +75 -75
  60. package/MktMesh/types.js +1 -1
  61. package/MktMesh/wallCapTessellate.d.ts +33 -33
  62. package/MktMesh/wallCapTessellate.js +97 -97
  63. package/MktMesh/wallCapTessellate.ts +68 -68
  64. package/PipeMesh/PathGeometry.d.ts +44 -44
  65. package/PipeMesh/PathGeometry.js +307 -307
  66. package/PipeMesh/PathPoint.d.ts +17 -17
  67. package/PipeMesh/PathPoint.js +31 -31
  68. package/PipeMesh/PathPointList.d.ts +31 -31
  69. package/PipeMesh/PathPointList.js +209 -209
  70. package/PipeMesh/PathTubeGeometry.d.ts +25 -25
  71. package/PipeMesh/PathTubeGeometry.js +130 -130
  72. package/PipeMesh/PipeMesh.d.ts +25 -25
  73. package/PipeMesh/PipeMesh.js +63 -63
  74. package/PipeMesh/index.d.ts +1 -1
  75. package/PipeMesh/index.js +1 -1
  76. package/PlaneModal1/PlaneModal1.d.ts +34 -34
  77. package/PlaneModal1/PlaneModal1.js +109 -109
  78. package/PlaneModal1/index.d.ts +1 -1
  79. package/PlaneModal1/index.js +1 -1
  80. package/PlaneText.d.ts +27 -27
  81. package/PlaneText.js +35 -35
  82. package/PlaneWithHtml.d.ts +13 -13
  83. package/PlaneWithHtml.js +64 -64
  84. package/PointLight/PointLight.d.ts +13 -13
  85. package/PointLight/PointLight.js +64 -64
  86. package/PointLight/index.d.ts +1 -1
  87. package/PointLight/index.js +1 -1
  88. package/Sky/Sky.d.ts +9 -9
  89. package/Sky/Sky.js +22 -22
  90. package/Sky/index.d.ts +1 -1
  91. package/Sky/index.js +1 -1
  92. package/SpotLight.d.ts +6 -6
  93. package/SpotLight.js +72 -72
  94. package/SpriteModal1/SpriteModal1.d.ts +33 -33
  95. package/SpriteModal1/SpriteModal1.js +116 -116
  96. package/SpriteModal1/index.d.ts +1 -1
  97. package/SpriteModal1/index.js +1 -1
  98. package/SpriteText.d.ts +46 -46
  99. package/SpriteText.js +176 -176
  100. package/SpriteText2.d.ts +48 -48
  101. package/SpriteText2.js +223 -223
  102. package/SpriteText3.d.ts +86 -86
  103. package/SpriteText3.js +517 -517
  104. package/SpriteText3.ts +599 -599
  105. package/SpriteWithHtml.d.ts +9 -9
  106. package/SpriteWithHtml.js +44 -44
  107. package/Text3D/Text3D.d.ts +20 -20
  108. package/Text3D/Text3D.js +58 -58
  109. package/Text3D/fontController.d.ts +60 -60
  110. package/Text3D/fontController.js +166 -166
  111. package/Text3D/index.d.ts +1 -1
  112. package/Text3D/index.js +1 -1
  113. package/ThreeBase.d.ts +248 -248
  114. package/ThreeBase.js +1202 -1202
  115. package/ThreeBase.ts +1314 -1314
  116. package/ThreeMouse.d.ts +53 -54
  117. package/ThreeMouse.js +453 -453
  118. package/Transition.d.ts +45 -45
  119. package/Transition.js +209 -209
  120. package/Water/Water.d.ts +5 -5
  121. package/Water/Water.js +44 -44
  122. package/Water/index.d.ts +2 -2
  123. package/Water/index.js +2 -2
  124. package/WaterFlow/FloatingDuck.d.ts +88 -88
  125. package/WaterFlow/FloatingDuck.js +307 -307
  126. package/WaterFlow/FloatingDuck.ts +445 -445
  127. package/WaterFlow/GPUFluidSimulation.d.ts +56 -56
  128. package/WaterFlow/GPUFluidSimulation.js +781 -781
  129. package/WaterFlow/GPUFluidSimulation.ts +827 -827
  130. package/WaterFlow/ReservoirSystem.d.ts +106 -106
  131. package/WaterFlow/ReservoirSystem.js +611 -611
  132. package/WaterFlow/ReservoirSystem.ts +754 -754
  133. package/WaterFlow/SPHField.d.ts +35 -35
  134. package/WaterFlow/SPHField.js +203 -203
  135. package/WaterFlow/SPHField.ts +228 -228
  136. package/WaterFlow/ScreenSpaceFluidPass.d.ts +69 -69
  137. package/WaterFlow/ScreenSpaceFluidPass.js +728 -728
  138. package/WaterFlow/ScreenSpaceFluidPass.ts +789 -789
  139. package/WaterFlow/ShallowWaterSurface.d.ts +45 -45
  140. package/WaterFlow/ShallowWaterSurface.js +438 -438
  141. package/WaterFlow/ShallowWaterSurface.ts +510 -510
  142. package/WaterFlow/WaterFlow.d.ts +87 -87
  143. package/WaterFlow/WaterFlow.js +171 -171
  144. package/WaterFlow/WaterFlow.ts +219 -219
  145. package/WaterFlow/types.d.ts +161 -161
  146. package/WaterFlow/types.js +1 -1
  147. package/WaterFlow/types.ts +189 -189
  148. package/WaterFlow/waterFlowPass.d.ts +3 -3
  149. package/WaterFlow/waterFlowPass.js +20 -20
  150. package/WaterFlow/waterFlowPass.ts +40 -40
  151. package/collisionOBB.d.ts +42 -42
  152. package/collisionOBB.js +112 -112
  153. package/collisionOBB.ts +108 -108
  154. package/declare.d.ts +25 -25
  155. package/index.d.ts +1 -1
  156. package/index.js +1 -1
  157. package/instanced/InstancedBatch.d.ts +57 -57
  158. package/instanced/InstancedBatch.js +348 -348
  159. package/instanced/InstancedBatch.ts +406 -406
  160. package/instanced/InstancedChild.d.ts +174 -174
  161. package/instanced/InstancedChild.js +264 -264
  162. package/instanced/InstancedChild.ts +327 -327
  163. package/instanced/InstancedGroup.d.ts +43 -43
  164. package/instanced/InstancedGroup.js +126 -126
  165. package/instanced/InstancedGroup.ts +137 -137
  166. package/instanced/InstancedMeshBase.d.ts +38 -38
  167. package/instanced/InstancedMeshBase.js +282 -282
  168. package/instanced/InstancedTemplate.d.ts +46 -46
  169. package/instanced/InstancedTemplate.js +125 -125
  170. package/keyControls.d.ts +29 -30
  171. package/keyControls.js +101 -101
  172. package/lensflare/LensFlare.d.ts +39 -39
  173. package/lensflare/LensFlare.js +284 -284
  174. package/package.json +6 -1
  175. package/pass/bloom_pass.d.ts +2 -2
  176. package/pass/bloom_pass.js +174 -174
  177. package/pass/cover_pass.d.ts +2 -2
  178. package/pass/cover_pass.js +108 -108
  179. package/pass/debugDepthState.d.ts +20 -20
  180. package/pass/debugDepthState.js +9 -9
  181. package/pass/debugDepthState.ts +22 -22
  182. package/pass/depthTestBias.d.ts +12 -12
  183. package/pass/depthTestBias.js +12 -12
  184. package/pass/depthTestBias.ts +12 -12
  185. package/pass/effectUserData.d.ts +27 -27
  186. package/pass/effectUserData.js +1 -1
  187. package/pass/effectUserData.ts +46 -46
  188. package/pass/frosted_glass_pass.d.ts +2 -2
  189. package/pass/frosted_glass_pass.js +150 -150
  190. package/pass/fsQuad.d.ts +9 -9
  191. package/pass/fsQuad.js +28 -28
  192. package/pass/glow_pass.d.ts +2 -2
  193. package/pass/glow_pass.js +350 -350
  194. package/pass/screen_bloom_pass.d.ts +11 -11
  195. package/pass/screen_bloom_pass.js +136 -136
  196. package/pass/ssao_pass.d.ts +88 -88
  197. package/pass/ssao_pass.js +799 -799
  198. package/pass/ssao_pass.ts +918 -918
  199. package/pass/stroke_pass.d.ts +2 -2
  200. package/pass/stroke_pass.js +267 -267
  201. package/shaders/FXAAShaderMaterial.d.ts +3 -3
  202. package/shaders/FXAAShaderMaterial.js +20 -20
  203. package/shaders/baseDepthVisShader.d.ts +11 -11
  204. package/shaders/baseDepthVisShader.js +36 -36
  205. package/shaders/baseDepthVisShader.ts +36 -36
  206. package/shaders/baseTextureToRenderShader.d.ts +2 -2
  207. package/shaders/baseTextureToRenderShader.js +19 -19
  208. package/shaders/copyShader.d.ts +3 -3
  209. package/shaders/copyShader.js +13 -13
  210. package/shaders/debugDepthCompareShader.d.ts +14 -14
  211. package/shaders/debugDepthCompareShader.js +49 -49
  212. package/shaders/debugDepthCompareShader.ts +50 -50
  213. package/shaders/debugDepthJudgeShader.d.ts +18 -18
  214. package/shaders/debugDepthJudgeShader.js +52 -52
  215. package/shaders/debugDepthJudgeShader.ts +54 -54
  216. package/shaders/debugExcludedShader.d.ts +13 -13
  217. package/shaders/debugExcludedShader.js +46 -46
  218. package/shaders/debugExcludedShader.ts +48 -48
  219. package/spriteTextBg.d.ts +1 -1
  220. package/spriteTextBg.js +20 -20
  221. package/spriteTextBg.ts +20 -20
  222. package/threeLoader.d.ts +35 -35
  223. package/threeLoader.js +518 -518
  224. package/threeUtils.d.ts +53 -53
  225. package/threeUtils.js +329 -329
  226. package/types.d.ts +2 -2
  227. package/types.js +6 -6
@@ -1,441 +1,441 @@
1
- import * as THREE from 'three';
2
- const quadVertex = /* glsl */ `
3
- out vec2 vUv;
4
- void main() {
5
- vUv = uv;
6
- gl_Position = vec4(position, 1.0);
7
- }
8
- `;
9
- function createWaveTarget(width, height) {
10
- return new THREE.WebGLRenderTarget(width, height, {
11
- format: THREE.RGBAFormat,
12
- type: THREE.HalfFloatType,
13
- minFilter: THREE.LinearFilter,
14
- magFilter: THREE.LinearFilter,
15
- wrapS: THREE.ClampToEdgeWrapping,
16
- wrapT: THREE.ClampToEdgeWrapping,
17
- depthBuffer: false,
18
- });
19
- }
20
- function ambientSurfaceHeight(u, v, time) {
21
- const sourceX = 0.56 + Math.sin(time * 0.17) * 0.13;
22
- const sourceZ = 0.48 + Math.cos(time * 0.14) * 0.11;
23
- const radialX = (u - sourceX) * 1.65;
24
- const radialZ = v - sourceZ;
25
- const radialDistance = Math.hypot(radialX, radialZ);
26
- const swellA = Math.sin(u * 11.0 + v * 7.0 + time * 1.15) * 0.008;
27
- const swellB = Math.sin(-u * 8.0 + v * 13.0 - time * 0.88 + 0.9) * 0.0055;
28
- const radial = Math.sin(radialDistance * 29.0 - time * 2.05)
29
- * Math.exp(-radialDistance * 2.0) * 0.0065;
30
- return swellA + swellB + radial;
31
- }
32
- function sloshSurfaceHeight(u, v, slosh) {
33
- return (u - 0.5) * 2 * slosh.x + (v - 0.5) * 2 * slosh.y;
34
- }
35
- export class ShallowWaterSurface {
36
- renderer;
37
- width;
38
- height;
39
- basinMinX;
40
- basinMaxX;
41
- basinHalfZ;
42
- basinCenterZ;
43
- waterY;
44
- cpuSampling;
45
- cpuWidth;
46
- cpuHeight;
47
- cpuCurrent;
48
- cpuPrevious;
49
- cpuNext;
50
- cpuLastDelta;
51
- cpuImpactX;
52
- cpuImpactZ;
53
- cpuImpactEnergy;
54
- cpuEmitting;
55
- elapsed;
56
- slosh;
57
- previousSlosh;
58
- motionImpulse;
59
- /** 高度场乒乓 RT:每帧交换,故非 readonly。 */
60
- read;
61
- write;
62
- computeScene;
63
- computeCamera;
64
- computeMaterial;
65
- computeQuad;
66
- material;
67
- /** 对外可见的水面网格(ReservoirSystem 会改 renderOrder / visible)。 */
68
- mesh;
69
- constructor(renderer, { width = 256, height = 128, basinMinX = 0.25, basinMaxX = 3.85, basinHalfZ = 0.82, basinCenterZ = 0, waterY = 0.2, cpuSampling = false, } = {}) {
70
- this.renderer = renderer;
71
- this.width = width;
72
- this.height = height;
73
- this.basinMinX = basinMinX;
74
- this.basinMaxX = basinMaxX;
75
- this.basinHalfZ = basinHalfZ;
76
- this.basinCenterZ = basinCenterZ;
77
- this.waterY = waterY;
78
- this.cpuSampling = cpuSampling;
79
- this.cpuWidth = cpuSampling ? 48 : 0;
80
- this.cpuHeight = cpuSampling ? 24 : 0;
81
- this.cpuCurrent = cpuSampling ? new Float32Array(this.cpuWidth * this.cpuHeight) : null;
82
- this.cpuPrevious = cpuSampling ? new Float32Array(this.cpuWidth * this.cpuHeight) : null;
83
- this.cpuNext = cpuSampling ? new Float32Array(this.cpuWidth * this.cpuHeight) : null;
84
- this.cpuLastDelta = 1 / 60;
85
- this.cpuImpactX = (basinMinX + basinMaxX) * 0.5;
86
- this.cpuImpactZ = basinCenterZ;
87
- this.cpuImpactEnergy = 0;
88
- this.cpuEmitting = false;
89
- this.elapsed = 0;
90
- this.slosh = new THREE.Vector2();
91
- this.previousSlosh = new THREE.Vector2();
92
- this.motionImpulse = new THREE.Vector2();
93
- this.read = createWaveTarget(width, height);
94
- this.write = createWaveTarget(width, height);
95
- this.computeScene = new THREE.Scene();
96
- this.computeCamera = new THREE.OrthographicCamera(-1, 1, 1, -1, 0, 1);
97
- this.computeMaterial = new THREE.ShaderMaterial({
98
- glslVersion: THREE.GLSL3,
99
- uniforms: {
100
- uState: { value: this.read.texture },
101
- uTexel: { value: new THREE.Vector2(1 / width, 1 / height) },
102
- uImpactUv: { value: new THREE.Vector2(0.35, 0.5) },
103
- uImpulse: { value: 0 },
104
- uTime: { value: 0 },
105
- uMotionImpulse: { value: new THREE.Vector2() },
106
- },
107
- vertexShader: quadVertex,
108
- fragmentShader: /* glsl */ `
109
- precision highp float;
110
- uniform sampler2D uState;
111
- uniform vec2 uTexel;
112
- uniform vec2 uImpactUv;
113
- uniform float uImpulse;
114
- uniform float uTime;
115
- uniform vec2 uMotionImpulse;
116
- in vec2 vUv;
117
- out vec4 outColor;
118
-
119
- void main() {
120
- vec2 state = texture(uState, vUv).rg;
121
- float current = state.r;
122
- float previous = state.g;
123
- float left = texture(uState, vUv - vec2(uTexel.x, 0.0)).r;
124
- float right = texture(uState, vUv + vec2(uTexel.x, 0.0)).r;
125
- float down = texture(uState, vUv - vec2(0.0, uTexel.y)).r;
126
- float up = texture(uState, vUv + vec2(0.0, uTexel.y)).r;
127
- float laplacian = left + right + down + up - 4.0 * current;
128
- float nextHeight = (2.0 * current - previous) + laplacian * 0.235;
129
- nextHeight *= 0.9925;
130
-
131
- vec2 aspectUv = (vUv - uImpactUv) * vec2(2.2, 1.0);
132
- float impact = exp(-dot(aspectUv, aspectUv) * 420.0);
133
- float microPulse = 0.72 + 0.28 * sin(uTime * 19.0);
134
- nextHeight += impact * uImpulse * microPulse;
135
-
136
- vec2 sourceA = vec2(0.22 + sin(uTime * 0.19) * 0.08, 0.31 + cos(uTime * 0.16) * 0.09);
137
- vec2 sourceB = vec2(0.72 + cos(uTime * 0.13) * 0.09, 0.68 + sin(uTime * 0.17) * 0.08);
138
- vec2 deltaA = (vUv - sourceA) * vec2(1.75, 1.0);
139
- vec2 deltaB = (vUv - sourceB) * vec2(1.55, 1.0);
140
- float distanceA = length(deltaA);
141
- float distanceB = length(deltaB);
142
- float ambientWaves = sin(distanceA * 47.0 - uTime * 2.15) * exp(-distanceA * 3.8)
143
- + sin(distanceB * 39.0 - uTime * 1.72 + 1.4) * exp(-distanceB * 3.5);
144
- nextHeight += ambientWaves * 0.000028;
145
-
146
- vec2 centered = vUv - 0.5;
147
- float wallResponse = 0.34
148
- + 0.66 * pow(max(abs(centered.x), abs(centered.y)) * 2.0, 2.0);
149
- nextHeight += dot(centered, uMotionImpulse) * wallResponse;
150
-
151
- float edge = smoothstep(0.0, 0.025, vUv.x) * smoothstep(1.0, 0.975, vUv.x)
152
- * smoothstep(0.0, 0.045, vUv.y) * smoothstep(1.0, 0.955, vUv.y);
153
- nextHeight *= mix(0.86, 1.0, edge);
154
- outColor = vec4(nextHeight, current, 0.0, 1.0);
155
- }
156
- `,
157
- depthTest: false,
158
- depthWrite: false,
159
- toneMapped: false,
160
- });
161
- this.computeQuad = new THREE.Mesh(new THREE.PlaneGeometry(2, 2), this.computeMaterial);
162
- this.computeQuad.frustumCulled = false;
163
- this.computeScene.add(this.computeQuad);
164
- this.material = new THREE.ShaderMaterial({
165
- glslVersion: THREE.GLSL3,
166
- uniforms: {
167
- uHeightMap: { value: this.read.texture },
168
- uTexel: { value: new THREE.Vector2(1 / width, 1 / height) },
169
- uTime: { value: 0 },
170
- uSlosh: { value: this.slosh },
171
- uLightPosition: { value: new THREE.Vector3(-1, 5, 4) },
172
- },
173
- vertexShader: /* glsl */ `
174
- precision highp float;
175
- uniform sampler2D uHeightMap;
176
- uniform vec2 uTexel;
177
- uniform float uTime;
178
- uniform vec2 uSlosh;
179
- out vec2 vUv;
180
- out vec3 vWorldPosition;
181
- out vec3 vWorldNormal;
182
-
183
- float ambientHeight(vec2 sampleUv) {
184
- vec2 source = vec2(
185
- 0.56 + sin(uTime * 0.17) * 0.13,
186
- 0.48 + cos(uTime * 0.14) * 0.11
187
- );
188
- vec2 radialDelta = (sampleUv - source) * vec2(1.65, 1.0);
189
- float radialDistance = length(radialDelta);
190
- float swellA = sin(sampleUv.x * 11.0 + sampleUv.y * 7.0 + uTime * 1.15) * 0.008;
191
- float swellB = sin(-sampleUv.x * 8.0 + sampleUv.y * 13.0 - uTime * 0.88 + 0.9) * 0.0055;
192
- float radial = sin(radialDistance * 29.0 - uTime * 2.05)
193
- * exp(-radialDistance * 2.0) * 0.0065;
194
- return swellA + swellB + radial;
195
- }
196
-
197
- float surfaceHeight(vec2 sampleUv) {
198
- float sloshHeight = dot((sampleUv - 0.5) * 2.0, uSlosh);
199
- return texture(uHeightMap, sampleUv).r * 0.52 + ambientHeight(sampleUv) + sloshHeight;
200
- }
201
-
202
- void main() {
203
- vUv = uv;
204
- float h = surfaceHeight(uv);
205
- float hL = surfaceHeight(uv - vec2(uTexel.x, 0.0));
206
- float hR = surfaceHeight(uv + vec2(uTexel.x, 0.0));
207
- float hD = surfaceHeight(uv - vec2(0.0, uTexel.y));
208
- float hU = surfaceHeight(uv + vec2(0.0, uTexel.y));
209
- vec3 displaced = position;
210
- displaced.z += h;
211
- vec3 localNormal = normalize(vec3((hL - hR) * 46.0, (hD - hU) * 46.0, 1.0));
212
- vec4 world = modelMatrix * vec4(displaced, 1.0);
213
- vWorldPosition = world.xyz;
214
- vWorldNormal = normalize(mat3(modelMatrix) * localNormal);
215
- gl_Position = projectionMatrix * viewMatrix * world;
216
- }
217
- `,
218
- fragmentShader: /* glsl */ `
219
- precision highp float;
220
- uniform sampler2D uHeightMap;
221
- uniform vec2 uTexel;
222
- uniform float uTime;
223
- uniform vec3 uLightPosition;
224
- in vec2 vUv;
225
- in vec3 vWorldPosition;
226
- in vec3 vWorldNormal;
227
- out vec4 outColor;
228
-
229
- void main() {
230
- vec2 microRipple = vec2(
231
- sin(vUv.x * 54.0 + vUv.y * 19.0 + uTime * 1.35)
232
- + sin(vUv.y * 37.0 - uTime * 0.94),
233
- cos(vUv.y * 49.0 - vUv.x * 17.0 + uTime * 1.12)
234
- + cos(vUv.x * 31.0 + uTime * 0.78)
235
- ) * 0.014;
236
- vec3 normal = normalize(vWorldNormal + vec3(microRipple.x, 0.0, microRipple.y));
237
- vec3 viewDir = normalize(cameraPosition - vWorldPosition);
238
- vec3 lightDir = normalize(uLightPosition - vWorldPosition);
239
- vec3 halfDir = normalize(viewDir + lightDir);
240
- float fresnel = 0.02 + 0.98 * pow(1.0 - max(dot(viewDir, normal), 0.0), 5.0);
241
- float specular = pow(max(dot(normal, halfDir), 0.0), 118.0);
242
- float height = texture(uHeightMap, vUv).r;
243
- float slope = abs(texture(uHeightMap, vUv + vec2(uTexel.x, 0.0)).r - texture(uHeightMap, vUv - vec2(uTexel.x, 0.0)).r)
244
- + abs(texture(uHeightMap, vUv + vec2(0.0, uTexel.y)).r - texture(uHeightMap, vUv - vec2(0.0, uTexel.y)).r);
245
- float foam = smoothstep(0.018, 0.07, slope + abs(height) * 0.16);
246
- vec3 deep = vec3(0.018, 0.16, 0.2);
247
- vec3 reflection = mix(vec3(0.06, 0.2, 0.24), vec3(0.5, 0.86, 0.9), fresnel);
248
- vec3 color = mix(deep, reflection, 0.32 + fresnel * 0.68);
249
- color += vec3(0.75, 0.98, 1.0) * specular * 1.4;
250
- color = mix(color, vec3(0.72, 0.94, 0.96), foam * 0.36);
251
- outColor = vec4(color, 0.66 + fresnel * 0.22);
252
- }
253
- `,
254
- transparent: true,
255
- depthWrite: false,
256
- side: THREE.DoubleSide,
257
- });
258
- const segmentsX = width >= 200 ? 128 : 64;
259
- const segmentsY = width >= 200 ? 64 : 32;
260
- this.mesh = new THREE.Mesh(new THREE.PlaneGeometry(basinMaxX - basinMinX - 0.12, basinHalfZ * 2 - 0.12, segmentsX, segmentsY), this.material);
261
- this.mesh.rotation.x = -Math.PI / 2;
262
- this.mesh.position.set((basinMinX + basinMaxX) * 0.5, waterY, basinCenterZ);
263
- this.mesh.renderOrder = 2;
264
- this.clear();
265
- }
266
- clear() {
267
- this.cpuCurrent?.fill(0);
268
- this.cpuPrevious?.fill(0);
269
- this.cpuNext?.fill(0);
270
- const previousTarget = this.renderer.getRenderTarget();
271
- const previousColor = this.renderer.getClearColor(new THREE.Color());
272
- const previousAlpha = this.renderer.getClearAlpha();
273
- this.renderer.setClearColor(0x000000, 0);
274
- this.renderer.setRenderTarget(this.read);
275
- this.renderer.clear();
276
- this.renderer.setRenderTarget(this.write);
277
- this.renderer.clear();
278
- this.renderer.setRenderTarget(previousTarget);
279
- this.renderer.setClearColor(previousColor, previousAlpha);
280
- }
281
- setBounds({ basinMinX, basinMaxX, basinHalfZ, basinCenterZ = 0, waterY }) {
282
- this.basinMinX = basinMinX;
283
- this.basinMaxX = basinMaxX;
284
- this.basinHalfZ = basinHalfZ;
285
- this.basinCenterZ = basinCenterZ;
286
- this.waterY = waterY;
287
- this.mesh.position.set((basinMinX + basinMaxX) * 0.5, waterY, basinCenterZ);
288
- }
289
- setContainerMotion(sloshX = 0, sloshZ = 0, impulseX = 0, impulseZ = 0) {
290
- this.previousSlosh.copy(this.slosh);
291
- this.slosh.set(sloshX, sloshZ);
292
- this.motionImpulse.set(impulseX, impulseZ);
293
- this.computeMaterial.uniforms.uMotionImpulse.value.copy(this.motionImpulse);
294
- }
295
- update(delta, elapsed, impactWorldX, energy, emitting, impactWorldZ = 0) {
296
- this.elapsed = elapsed;
297
- const impactUvX = THREE.MathUtils.clamp((impactWorldX - this.basinMinX) / (this.basinMaxX - this.basinMinX), 0.03, 0.97);
298
- const impactUvZ = THREE.MathUtils.clamp((impactWorldZ - (this.basinCenterZ - this.basinHalfZ)) / (this.basinHalfZ * 2), 0.03, 0.97);
299
- const impulse = emitting ? Math.min(energy * delta * 0.00135, 0.0035) : 0;
300
- this.computeMaterial.uniforms.uState.value = this.read.texture;
301
- this.computeMaterial.uniforms.uImpactUv.value.set(impactUvX, impactUvZ);
302
- this.computeMaterial.uniforms.uImpulse.value = impulse;
303
- this.computeMaterial.uniforms.uTime.value = elapsed;
304
- if (this.cpuSampling) {
305
- this.stepCpuSampler(delta, elapsed, impactUvX, impactUvZ, impactWorldX, impactWorldZ, energy, emitting, impulse);
1
+ import * as THREE from 'three';
2
+ const quadVertex = /* glsl */ `
3
+ out vec2 vUv;
4
+ void main() {
5
+ vUv = uv;
6
+ gl_Position = vec4(position, 1.0);
7
+ }
8
+ `;
9
+ function createWaveTarget(width, height) {
10
+ return new THREE.WebGLRenderTarget(width, height, {
11
+ format: THREE.RGBAFormat,
12
+ type: THREE.HalfFloatType,
13
+ minFilter: THREE.LinearFilter,
14
+ magFilter: THREE.LinearFilter,
15
+ wrapS: THREE.ClampToEdgeWrapping,
16
+ wrapT: THREE.ClampToEdgeWrapping,
17
+ depthBuffer: false,
18
+ });
19
+ }
20
+ function ambientSurfaceHeight(u, v, time) {
21
+ const sourceX = 0.56 + Math.sin(time * 0.17) * 0.13;
22
+ const sourceZ = 0.48 + Math.cos(time * 0.14) * 0.11;
23
+ const radialX = (u - sourceX) * 1.65;
24
+ const radialZ = v - sourceZ;
25
+ const radialDistance = Math.hypot(radialX, radialZ);
26
+ const swellA = Math.sin(u * 11.0 + v * 7.0 + time * 1.15) * 0.008;
27
+ const swellB = Math.sin(-u * 8.0 + v * 13.0 - time * 0.88 + 0.9) * 0.0055;
28
+ const radial = Math.sin(radialDistance * 29.0 - time * 2.05)
29
+ * Math.exp(-radialDistance * 2.0) * 0.0065;
30
+ return swellA + swellB + radial;
31
+ }
32
+ function sloshSurfaceHeight(u, v, slosh) {
33
+ return (u - 0.5) * 2 * slosh.x + (v - 0.5) * 2 * slosh.y;
34
+ }
35
+ export class ShallowWaterSurface {
36
+ renderer;
37
+ width;
38
+ height;
39
+ basinMinX;
40
+ basinMaxX;
41
+ basinHalfZ;
42
+ basinCenterZ;
43
+ waterY;
44
+ cpuSampling;
45
+ cpuWidth;
46
+ cpuHeight;
47
+ cpuCurrent;
48
+ cpuPrevious;
49
+ cpuNext;
50
+ cpuLastDelta;
51
+ cpuImpactX;
52
+ cpuImpactZ;
53
+ cpuImpactEnergy;
54
+ cpuEmitting;
55
+ elapsed;
56
+ slosh;
57
+ previousSlosh;
58
+ motionImpulse;
59
+ /** 高度场乒乓 RT:每帧交换,故非 readonly。 */
60
+ read;
61
+ write;
62
+ computeScene;
63
+ computeCamera;
64
+ computeMaterial;
65
+ computeQuad;
66
+ material;
67
+ /** 对外可见的水面网格(ReservoirSystem 会改 renderOrder / visible)。 */
68
+ mesh;
69
+ constructor(renderer, { width = 256, height = 128, basinMinX = 0.25, basinMaxX = 3.85, basinHalfZ = 0.82, basinCenterZ = 0, waterY = 0.2, cpuSampling = false, } = {}) {
70
+ this.renderer = renderer;
71
+ this.width = width;
72
+ this.height = height;
73
+ this.basinMinX = basinMinX;
74
+ this.basinMaxX = basinMaxX;
75
+ this.basinHalfZ = basinHalfZ;
76
+ this.basinCenterZ = basinCenterZ;
77
+ this.waterY = waterY;
78
+ this.cpuSampling = cpuSampling;
79
+ this.cpuWidth = cpuSampling ? 48 : 0;
80
+ this.cpuHeight = cpuSampling ? 24 : 0;
81
+ this.cpuCurrent = cpuSampling ? new Float32Array(this.cpuWidth * this.cpuHeight) : null;
82
+ this.cpuPrevious = cpuSampling ? new Float32Array(this.cpuWidth * this.cpuHeight) : null;
83
+ this.cpuNext = cpuSampling ? new Float32Array(this.cpuWidth * this.cpuHeight) : null;
84
+ this.cpuLastDelta = 1 / 60;
85
+ this.cpuImpactX = (basinMinX + basinMaxX) * 0.5;
86
+ this.cpuImpactZ = basinCenterZ;
87
+ this.cpuImpactEnergy = 0;
88
+ this.cpuEmitting = false;
89
+ this.elapsed = 0;
90
+ this.slosh = new THREE.Vector2();
91
+ this.previousSlosh = new THREE.Vector2();
92
+ this.motionImpulse = new THREE.Vector2();
93
+ this.read = createWaveTarget(width, height);
94
+ this.write = createWaveTarget(width, height);
95
+ this.computeScene = new THREE.Scene();
96
+ this.computeCamera = new THREE.OrthographicCamera(-1, 1, 1, -1, 0, 1);
97
+ this.computeMaterial = new THREE.ShaderMaterial({
98
+ glslVersion: THREE.GLSL3,
99
+ uniforms: {
100
+ uState: { value: this.read.texture },
101
+ uTexel: { value: new THREE.Vector2(1 / width, 1 / height) },
102
+ uImpactUv: { value: new THREE.Vector2(0.35, 0.5) },
103
+ uImpulse: { value: 0 },
104
+ uTime: { value: 0 },
105
+ uMotionImpulse: { value: new THREE.Vector2() },
106
+ },
107
+ vertexShader: quadVertex,
108
+ fragmentShader: /* glsl */ `
109
+ precision highp float;
110
+ uniform sampler2D uState;
111
+ uniform vec2 uTexel;
112
+ uniform vec2 uImpactUv;
113
+ uniform float uImpulse;
114
+ uniform float uTime;
115
+ uniform vec2 uMotionImpulse;
116
+ in vec2 vUv;
117
+ out vec4 outColor;
118
+
119
+ void main() {
120
+ vec2 state = texture(uState, vUv).rg;
121
+ float current = state.r;
122
+ float previous = state.g;
123
+ float left = texture(uState, vUv - vec2(uTexel.x, 0.0)).r;
124
+ float right = texture(uState, vUv + vec2(uTexel.x, 0.0)).r;
125
+ float down = texture(uState, vUv - vec2(0.0, uTexel.y)).r;
126
+ float up = texture(uState, vUv + vec2(0.0, uTexel.y)).r;
127
+ float laplacian = left + right + down + up - 4.0 * current;
128
+ float nextHeight = (2.0 * current - previous) + laplacian * 0.235;
129
+ nextHeight *= 0.9925;
130
+
131
+ vec2 aspectUv = (vUv - uImpactUv) * vec2(2.2, 1.0);
132
+ float impact = exp(-dot(aspectUv, aspectUv) * 420.0);
133
+ float microPulse = 0.72 + 0.28 * sin(uTime * 19.0);
134
+ nextHeight += impact * uImpulse * microPulse;
135
+
136
+ vec2 sourceA = vec2(0.22 + sin(uTime * 0.19) * 0.08, 0.31 + cos(uTime * 0.16) * 0.09);
137
+ vec2 sourceB = vec2(0.72 + cos(uTime * 0.13) * 0.09, 0.68 + sin(uTime * 0.17) * 0.08);
138
+ vec2 deltaA = (vUv - sourceA) * vec2(1.75, 1.0);
139
+ vec2 deltaB = (vUv - sourceB) * vec2(1.55, 1.0);
140
+ float distanceA = length(deltaA);
141
+ float distanceB = length(deltaB);
142
+ float ambientWaves = sin(distanceA * 47.0 - uTime * 2.15) * exp(-distanceA * 3.8)
143
+ + sin(distanceB * 39.0 - uTime * 1.72 + 1.4) * exp(-distanceB * 3.5);
144
+ nextHeight += ambientWaves * 0.000028;
145
+
146
+ vec2 centered = vUv - 0.5;
147
+ float wallResponse = 0.34
148
+ + 0.66 * pow(max(abs(centered.x), abs(centered.y)) * 2.0, 2.0);
149
+ nextHeight += dot(centered, uMotionImpulse) * wallResponse;
150
+
151
+ float edge = smoothstep(0.0, 0.025, vUv.x) * smoothstep(1.0, 0.975, vUv.x)
152
+ * smoothstep(0.0, 0.045, vUv.y) * smoothstep(1.0, 0.955, vUv.y);
153
+ nextHeight *= mix(0.86, 1.0, edge);
154
+ outColor = vec4(nextHeight, current, 0.0, 1.0);
306
155
  }
307
- const previousTarget = this.renderer.getRenderTarget();
308
- this.renderer.setRenderTarget(this.write);
309
- this.renderer.render(this.computeScene, this.computeCamera);
310
- this.renderer.setRenderTarget(previousTarget);
311
- [this.read, this.write] = [this.write, this.read];
312
- this.material.uniforms.uHeightMap.value = this.read.texture;
313
- this.material.uniforms.uTime.value = elapsed;
314
- }
315
- stepCpuSampler(delta, elapsed, impactUvX, impactUvZ, impactWorldX, impactWorldZ, energy, emitting, impulse) {
316
- const width = this.cpuWidth;
317
- const height = this.cpuHeight;
318
- const current = this.cpuCurrent;
319
- const previous = this.cpuPrevious;
320
- const next = this.cpuNext;
321
- const microPulse = 0.72 + 0.28 * Math.sin(elapsed * 19);
322
- const sourceAX = 0.22 + Math.sin(elapsed * 0.19) * 0.08;
323
- const sourceAZ = 0.31 + Math.cos(elapsed * 0.16) * 0.09;
324
- const sourceBX = 0.72 + Math.cos(elapsed * 0.13) * 0.09;
325
- const sourceBZ = 0.68 + Math.sin(elapsed * 0.17) * 0.08;
326
- for (let z = 0; z < height; z++) {
327
- const uvZ = (z + 0.5) / height;
328
- const zEdge = Math.min(1, uvZ / 0.055, (1 - uvZ) / 0.055);
329
- for (let x = 0; x < width; x++) {
330
- const index = z * width + x;
331
- const uvX = (x + 0.5) / width;
332
- const left = current[z * width + Math.max(0, x - 1)];
333
- const right = current[z * width + Math.min(width - 1, x + 1)];
334
- const down = current[Math.max(0, z - 1) * width + x];
335
- const up = current[Math.min(height - 1, z + 1) * width + x];
336
- const laplacian = left + right + down + up - 4 * current[index];
337
- let nextHeight = (2 * current[index] - previous[index]) + laplacian * 0.235;
338
- nextHeight *= 0.9925;
339
- const dx = (uvX - impactUvX) * 2.2;
340
- const dz = uvZ - impactUvZ;
341
- nextHeight += Math.exp(-(dx * dx + dz * dz) * 420) * impulse * microPulse;
342
- const ambientAX = (uvX - sourceAX) * 1.75;
343
- const ambientAZ = uvZ - sourceAZ;
344
- const ambientBX = (uvX - sourceBX) * 1.55;
345
- const ambientBZ = uvZ - sourceBZ;
346
- const distanceA = Math.hypot(ambientAX, ambientAZ);
347
- const distanceB = Math.hypot(ambientBX, ambientBZ);
348
- const ambientWaves = Math.sin(distanceA * 47 - elapsed * 2.15) * Math.exp(-distanceA * 3.8)
349
- + Math.sin(distanceB * 39 - elapsed * 1.72 + 1.4) * Math.exp(-distanceB * 3.5);
350
- nextHeight += ambientWaves * 0.000028;
351
- const centeredX = uvX - 0.5;
352
- const centeredZ = uvZ - 0.5;
353
- const wallResponse = 0.34 + 0.66 * Math.pow(Math.max(Math.abs(centeredX), Math.abs(centeredZ)) * 2, 2);
354
- nextHeight += (centeredX * this.motionImpulse.x + centeredZ * this.motionImpulse.y) * wallResponse;
355
- const xEdge = Math.min(1, uvX / 0.035, (1 - uvX) / 0.035);
356
- nextHeight *= THREE.MathUtils.lerp(0.86, 1, Math.max(0, Math.min(xEdge, zEdge)));
357
- next[index] = nextHeight;
358
- }
156
+ `,
157
+ depthTest: false,
158
+ depthWrite: false,
159
+ toneMapped: false,
160
+ });
161
+ this.computeQuad = new THREE.Mesh(new THREE.PlaneGeometry(2, 2), this.computeMaterial);
162
+ this.computeQuad.frustumCulled = false;
163
+ this.computeScene.add(this.computeQuad);
164
+ this.material = new THREE.ShaderMaterial({
165
+ glslVersion: THREE.GLSL3,
166
+ uniforms: {
167
+ uHeightMap: { value: this.read.texture },
168
+ uTexel: { value: new THREE.Vector2(1 / width, 1 / height) },
169
+ uTime: { value: 0 },
170
+ uSlosh: { value: this.slosh },
171
+ uLightPosition: { value: new THREE.Vector3(-1, 5, 4) },
172
+ },
173
+ vertexShader: /* glsl */ `
174
+ precision highp float;
175
+ uniform sampler2D uHeightMap;
176
+ uniform vec2 uTexel;
177
+ uniform float uTime;
178
+ uniform vec2 uSlosh;
179
+ out vec2 vUv;
180
+ out vec3 vWorldPosition;
181
+ out vec3 vWorldNormal;
182
+
183
+ float ambientHeight(vec2 sampleUv) {
184
+ vec2 source = vec2(
185
+ 0.56 + sin(uTime * 0.17) * 0.13,
186
+ 0.48 + cos(uTime * 0.14) * 0.11
187
+ );
188
+ vec2 radialDelta = (sampleUv - source) * vec2(1.65, 1.0);
189
+ float radialDistance = length(radialDelta);
190
+ float swellA = sin(sampleUv.x * 11.0 + sampleUv.y * 7.0 + uTime * 1.15) * 0.008;
191
+ float swellB = sin(-sampleUv.x * 8.0 + sampleUv.y * 13.0 - uTime * 0.88 + 0.9) * 0.0055;
192
+ float radial = sin(radialDistance * 29.0 - uTime * 2.05)
193
+ * exp(-radialDistance * 2.0) * 0.0065;
194
+ return swellA + swellB + radial;
359
195
  }
360
- this.cpuPrevious = current;
361
- this.cpuCurrent = next;
362
- this.cpuNext = previous;
363
- this.cpuLastDelta = Math.max(delta, 1 / 120);
364
- this.cpuImpactX = impactWorldX;
365
- this.cpuImpactZ = impactWorldZ;
366
- this.cpuImpactEnergy = energy;
367
- this.cpuEmitting = emitting;
368
- }
369
- sampleSurface(worldX, worldZ, target = {}) {
370
- target.height = 0;
371
- target.velocityY = 0;
372
- target.slopeX = 0;
373
- target.slopeZ = 0;
374
- target.flowX = 0;
375
- target.flowZ = 0;
376
- target.impact = 0;
377
- if (!this.cpuSampling || !this.cpuCurrent)
378
- return target;
379
- const sample = (field, u, v) => {
380
- const x = THREE.MathUtils.clamp(u, 0, 1) * (this.cpuWidth - 1);
381
- const z = THREE.MathUtils.clamp(v, 0, 1) * (this.cpuHeight - 1);
382
- const x0 = Math.floor(x);
383
- const z0 = Math.floor(z);
384
- const x1 = Math.min(this.cpuWidth - 1, x0 + 1);
385
- const z1 = Math.min(this.cpuHeight - 1, z0 + 1);
386
- const tx = x - x0;
387
- const tz = z - z0;
388
- const a = THREE.MathUtils.lerp(field[z0 * this.cpuWidth + x0], field[z0 * this.cpuWidth + x1], tx);
389
- const b = THREE.MathUtils.lerp(field[z1 * this.cpuWidth + x0], field[z1 * this.cpuWidth + x1], tx);
390
- return THREE.MathUtils.lerp(a, b, tz);
391
- };
392
- const u = (worldX - this.basinMinX) / Math.max(this.basinMaxX - this.basinMinX, 0.001);
393
- const v = (worldZ - (this.basinCenterZ - this.basinHalfZ)) / Math.max(this.basinHalfZ * 2, 0.001);
394
- const du = 1 / Math.max(this.cpuWidth - 1, 1);
395
- const dv = 1 / Math.max(this.cpuHeight - 1, 1);
396
- const center = sample(this.cpuCurrent, u, v);
397
- const previous = sample(this.cpuPrevious, u, v);
398
- const left = sample(this.cpuCurrent, u - du, v);
399
- const right = sample(this.cpuCurrent, u + du, v);
400
- const down = sample(this.cpuCurrent, u, v - dv);
401
- const up = sample(this.cpuCurrent, u, v + dv);
402
- const cellX = (this.basinMaxX - this.basinMinX) * du;
403
- const cellZ = this.basinHalfZ * 2 * dv;
404
- const displacementScale = 0.52;
405
- const ambientCenter = ambientSurfaceHeight(u, v, this.elapsed) + sloshSurfaceHeight(u, v, this.slosh);
406
- const ambientPrevious = ambientSurfaceHeight(u, v, this.elapsed - this.cpuLastDelta)
407
- + sloshSurfaceHeight(u, v, this.previousSlosh);
408
- const ambientLeft = ambientSurfaceHeight(u - du, v, this.elapsed) + sloshSurfaceHeight(u - du, v, this.slosh);
409
- const ambientRight = ambientSurfaceHeight(u + du, v, this.elapsed) + sloshSurfaceHeight(u + du, v, this.slosh);
410
- const ambientDown = ambientSurfaceHeight(u, v - dv, this.elapsed) + sloshSurfaceHeight(u, v - dv, this.slosh);
411
- const ambientUp = ambientSurfaceHeight(u, v + dv, this.elapsed) + sloshSurfaceHeight(u, v + dv, this.slosh);
412
- target.height = center * displacementScale + ambientCenter;
413
- target.velocityY = ((center - previous) * displacementScale + ambientCenter - ambientPrevious)
414
- / this.cpuLastDelta;
415
- target.slopeX = ((right - left) * displacementScale + ambientRight - ambientLeft)
416
- / Math.max(cellX * 2, 0.001);
417
- target.slopeZ = ((up - down) * displacementScale + ambientUp - ambientDown)
418
- / Math.max(cellZ * 2, 0.001);
419
- target.flowX = -target.slopeX * 1.35;
420
- target.flowZ = -target.slopeZ * 1.35;
421
- if (this.cpuEmitting) {
422
- const dx = worldX - this.cpuImpactX;
423
- const dz = worldZ - this.cpuImpactZ;
424
- const distanceSq = dx * dx + dz * dz;
425
- const inverseDistance = 1 / Math.max(Math.sqrt(distanceSq), 0.06);
426
- const sourceStrength = Math.min(this.cpuImpactEnergy * 0.075, 1.15) * Math.exp(-distanceSq / 0.48);
427
- target.flowX += sourceStrength * (0.42 + dx * inverseDistance * 0.68);
428
- target.flowZ += sourceStrength * dz * inverseDistance * 0.92;
429
- target.impact = sourceStrength * Math.exp(-distanceSq / 0.095);
196
+
197
+ float surfaceHeight(vec2 sampleUv) {
198
+ float sloshHeight = dot((sampleUv - 0.5) * 2.0, uSlosh);
199
+ return texture(uHeightMap, sampleUv).r * 0.52 + ambientHeight(sampleUv) + sloshHeight;
430
200
  }
431
- return target;
432
- }
433
- dispose() {
434
- this.read.dispose();
435
- this.write.dispose();
436
- this.computeMaterial.dispose();
437
- this.computeQuad.geometry.dispose();
438
- this.material.dispose();
439
- this.mesh.geometry.dispose();
440
- }
441
- }
201
+
202
+ void main() {
203
+ vUv = uv;
204
+ float h = surfaceHeight(uv);
205
+ float hL = surfaceHeight(uv - vec2(uTexel.x, 0.0));
206
+ float hR = surfaceHeight(uv + vec2(uTexel.x, 0.0));
207
+ float hD = surfaceHeight(uv - vec2(0.0, uTexel.y));
208
+ float hU = surfaceHeight(uv + vec2(0.0, uTexel.y));
209
+ vec3 displaced = position;
210
+ displaced.z += h;
211
+ vec3 localNormal = normalize(vec3((hL - hR) * 46.0, (hD - hU) * 46.0, 1.0));
212
+ vec4 world = modelMatrix * vec4(displaced, 1.0);
213
+ vWorldPosition = world.xyz;
214
+ vWorldNormal = normalize(mat3(modelMatrix) * localNormal);
215
+ gl_Position = projectionMatrix * viewMatrix * world;
216
+ }
217
+ `,
218
+ fragmentShader: /* glsl */ `
219
+ precision highp float;
220
+ uniform sampler2D uHeightMap;
221
+ uniform vec2 uTexel;
222
+ uniform float uTime;
223
+ uniform vec3 uLightPosition;
224
+ in vec2 vUv;
225
+ in vec3 vWorldPosition;
226
+ in vec3 vWorldNormal;
227
+ out vec4 outColor;
228
+
229
+ void main() {
230
+ vec2 microRipple = vec2(
231
+ sin(vUv.x * 54.0 + vUv.y * 19.0 + uTime * 1.35)
232
+ + sin(vUv.y * 37.0 - uTime * 0.94),
233
+ cos(vUv.y * 49.0 - vUv.x * 17.0 + uTime * 1.12)
234
+ + cos(vUv.x * 31.0 + uTime * 0.78)
235
+ ) * 0.014;
236
+ vec3 normal = normalize(vWorldNormal + vec3(microRipple.x, 0.0, microRipple.y));
237
+ vec3 viewDir = normalize(cameraPosition - vWorldPosition);
238
+ vec3 lightDir = normalize(uLightPosition - vWorldPosition);
239
+ vec3 halfDir = normalize(viewDir + lightDir);
240
+ float fresnel = 0.02 + 0.98 * pow(1.0 - max(dot(viewDir, normal), 0.0), 5.0);
241
+ float specular = pow(max(dot(normal, halfDir), 0.0), 118.0);
242
+ float height = texture(uHeightMap, vUv).r;
243
+ float slope = abs(texture(uHeightMap, vUv + vec2(uTexel.x, 0.0)).r - texture(uHeightMap, vUv - vec2(uTexel.x, 0.0)).r)
244
+ + abs(texture(uHeightMap, vUv + vec2(0.0, uTexel.y)).r - texture(uHeightMap, vUv - vec2(0.0, uTexel.y)).r);
245
+ float foam = smoothstep(0.018, 0.07, slope + abs(height) * 0.16);
246
+ vec3 deep = vec3(0.018, 0.16, 0.2);
247
+ vec3 reflection = mix(vec3(0.06, 0.2, 0.24), vec3(0.5, 0.86, 0.9), fresnel);
248
+ vec3 color = mix(deep, reflection, 0.32 + fresnel * 0.68);
249
+ color += vec3(0.75, 0.98, 1.0) * specular * 1.4;
250
+ color = mix(color, vec3(0.72, 0.94, 0.96), foam * 0.36);
251
+ outColor = vec4(color, 0.66 + fresnel * 0.22);
252
+ }
253
+ `,
254
+ transparent: true,
255
+ depthWrite: false,
256
+ side: THREE.DoubleSide,
257
+ });
258
+ const segmentsX = width >= 200 ? 128 : 64;
259
+ const segmentsY = width >= 200 ? 64 : 32;
260
+ this.mesh = new THREE.Mesh(new THREE.PlaneGeometry(basinMaxX - basinMinX - 0.12, basinHalfZ * 2 - 0.12, segmentsX, segmentsY), this.material);
261
+ this.mesh.rotation.x = -Math.PI / 2;
262
+ this.mesh.position.set((basinMinX + basinMaxX) * 0.5, waterY, basinCenterZ);
263
+ this.mesh.renderOrder = 2;
264
+ this.clear();
265
+ }
266
+ clear() {
267
+ this.cpuCurrent?.fill(0);
268
+ this.cpuPrevious?.fill(0);
269
+ this.cpuNext?.fill(0);
270
+ const previousTarget = this.renderer.getRenderTarget();
271
+ const previousColor = this.renderer.getClearColor(new THREE.Color());
272
+ const previousAlpha = this.renderer.getClearAlpha();
273
+ this.renderer.setClearColor(0x000000, 0);
274
+ this.renderer.setRenderTarget(this.read);
275
+ this.renderer.clear();
276
+ this.renderer.setRenderTarget(this.write);
277
+ this.renderer.clear();
278
+ this.renderer.setRenderTarget(previousTarget);
279
+ this.renderer.setClearColor(previousColor, previousAlpha);
280
+ }
281
+ setBounds({ basinMinX, basinMaxX, basinHalfZ, basinCenterZ = 0, waterY }) {
282
+ this.basinMinX = basinMinX;
283
+ this.basinMaxX = basinMaxX;
284
+ this.basinHalfZ = basinHalfZ;
285
+ this.basinCenterZ = basinCenterZ;
286
+ this.waterY = waterY;
287
+ this.mesh.position.set((basinMinX + basinMaxX) * 0.5, waterY, basinCenterZ);
288
+ }
289
+ setContainerMotion(sloshX = 0, sloshZ = 0, impulseX = 0, impulseZ = 0) {
290
+ this.previousSlosh.copy(this.slosh);
291
+ this.slosh.set(sloshX, sloshZ);
292
+ this.motionImpulse.set(impulseX, impulseZ);
293
+ this.computeMaterial.uniforms.uMotionImpulse.value.copy(this.motionImpulse);
294
+ }
295
+ update(delta, elapsed, impactWorldX, energy, emitting, impactWorldZ = 0) {
296
+ this.elapsed = elapsed;
297
+ const impactUvX = THREE.MathUtils.clamp((impactWorldX - this.basinMinX) / (this.basinMaxX - this.basinMinX), 0.03, 0.97);
298
+ const impactUvZ = THREE.MathUtils.clamp((impactWorldZ - (this.basinCenterZ - this.basinHalfZ)) / (this.basinHalfZ * 2), 0.03, 0.97);
299
+ const impulse = emitting ? Math.min(energy * delta * 0.00135, 0.0035) : 0;
300
+ this.computeMaterial.uniforms.uState.value = this.read.texture;
301
+ this.computeMaterial.uniforms.uImpactUv.value.set(impactUvX, impactUvZ);
302
+ this.computeMaterial.uniforms.uImpulse.value = impulse;
303
+ this.computeMaterial.uniforms.uTime.value = elapsed;
304
+ if (this.cpuSampling) {
305
+ this.stepCpuSampler(delta, elapsed, impactUvX, impactUvZ, impactWorldX, impactWorldZ, energy, emitting, impulse);
306
+ }
307
+ const previousTarget = this.renderer.getRenderTarget();
308
+ this.renderer.setRenderTarget(this.write);
309
+ this.renderer.render(this.computeScene, this.computeCamera);
310
+ this.renderer.setRenderTarget(previousTarget);
311
+ [this.read, this.write] = [this.write, this.read];
312
+ this.material.uniforms.uHeightMap.value = this.read.texture;
313
+ this.material.uniforms.uTime.value = elapsed;
314
+ }
315
+ stepCpuSampler(delta, elapsed, impactUvX, impactUvZ, impactWorldX, impactWorldZ, energy, emitting, impulse) {
316
+ const width = this.cpuWidth;
317
+ const height = this.cpuHeight;
318
+ const current = this.cpuCurrent;
319
+ const previous = this.cpuPrevious;
320
+ const next = this.cpuNext;
321
+ const microPulse = 0.72 + 0.28 * Math.sin(elapsed * 19);
322
+ const sourceAX = 0.22 + Math.sin(elapsed * 0.19) * 0.08;
323
+ const sourceAZ = 0.31 + Math.cos(elapsed * 0.16) * 0.09;
324
+ const sourceBX = 0.72 + Math.cos(elapsed * 0.13) * 0.09;
325
+ const sourceBZ = 0.68 + Math.sin(elapsed * 0.17) * 0.08;
326
+ for (let z = 0; z < height; z++) {
327
+ const uvZ = (z + 0.5) / height;
328
+ const zEdge = Math.min(1, uvZ / 0.055, (1 - uvZ) / 0.055);
329
+ for (let x = 0; x < width; x++) {
330
+ const index = z * width + x;
331
+ const uvX = (x + 0.5) / width;
332
+ const left = current[z * width + Math.max(0, x - 1)];
333
+ const right = current[z * width + Math.min(width - 1, x + 1)];
334
+ const down = current[Math.max(0, z - 1) * width + x];
335
+ const up = current[Math.min(height - 1, z + 1) * width + x];
336
+ const laplacian = left + right + down + up - 4 * current[index];
337
+ let nextHeight = (2 * current[index] - previous[index]) + laplacian * 0.235;
338
+ nextHeight *= 0.9925;
339
+ const dx = (uvX - impactUvX) * 2.2;
340
+ const dz = uvZ - impactUvZ;
341
+ nextHeight += Math.exp(-(dx * dx + dz * dz) * 420) * impulse * microPulse;
342
+ const ambientAX = (uvX - sourceAX) * 1.75;
343
+ const ambientAZ = uvZ - sourceAZ;
344
+ const ambientBX = (uvX - sourceBX) * 1.55;
345
+ const ambientBZ = uvZ - sourceBZ;
346
+ const distanceA = Math.hypot(ambientAX, ambientAZ);
347
+ const distanceB = Math.hypot(ambientBX, ambientBZ);
348
+ const ambientWaves = Math.sin(distanceA * 47 - elapsed * 2.15) * Math.exp(-distanceA * 3.8)
349
+ + Math.sin(distanceB * 39 - elapsed * 1.72 + 1.4) * Math.exp(-distanceB * 3.5);
350
+ nextHeight += ambientWaves * 0.000028;
351
+ const centeredX = uvX - 0.5;
352
+ const centeredZ = uvZ - 0.5;
353
+ const wallResponse = 0.34 + 0.66 * Math.pow(Math.max(Math.abs(centeredX), Math.abs(centeredZ)) * 2, 2);
354
+ nextHeight += (centeredX * this.motionImpulse.x + centeredZ * this.motionImpulse.y) * wallResponse;
355
+ const xEdge = Math.min(1, uvX / 0.035, (1 - uvX) / 0.035);
356
+ nextHeight *= THREE.MathUtils.lerp(0.86, 1, Math.max(0, Math.min(xEdge, zEdge)));
357
+ next[index] = nextHeight;
358
+ }
359
+ }
360
+ this.cpuPrevious = current;
361
+ this.cpuCurrent = next;
362
+ this.cpuNext = previous;
363
+ this.cpuLastDelta = Math.max(delta, 1 / 120);
364
+ this.cpuImpactX = impactWorldX;
365
+ this.cpuImpactZ = impactWorldZ;
366
+ this.cpuImpactEnergy = energy;
367
+ this.cpuEmitting = emitting;
368
+ }
369
+ sampleSurface(worldX, worldZ, target = {}) {
370
+ target.height = 0;
371
+ target.velocityY = 0;
372
+ target.slopeX = 0;
373
+ target.slopeZ = 0;
374
+ target.flowX = 0;
375
+ target.flowZ = 0;
376
+ target.impact = 0;
377
+ if (!this.cpuSampling || !this.cpuCurrent)
378
+ return target;
379
+ const sample = (field, u, v) => {
380
+ const x = THREE.MathUtils.clamp(u, 0, 1) * (this.cpuWidth - 1);
381
+ const z = THREE.MathUtils.clamp(v, 0, 1) * (this.cpuHeight - 1);
382
+ const x0 = Math.floor(x);
383
+ const z0 = Math.floor(z);
384
+ const x1 = Math.min(this.cpuWidth - 1, x0 + 1);
385
+ const z1 = Math.min(this.cpuHeight - 1, z0 + 1);
386
+ const tx = x - x0;
387
+ const tz = z - z0;
388
+ const a = THREE.MathUtils.lerp(field[z0 * this.cpuWidth + x0], field[z0 * this.cpuWidth + x1], tx);
389
+ const b = THREE.MathUtils.lerp(field[z1 * this.cpuWidth + x0], field[z1 * this.cpuWidth + x1], tx);
390
+ return THREE.MathUtils.lerp(a, b, tz);
391
+ };
392
+ const u = (worldX - this.basinMinX) / Math.max(this.basinMaxX - this.basinMinX, 0.001);
393
+ const v = (worldZ - (this.basinCenterZ - this.basinHalfZ)) / Math.max(this.basinHalfZ * 2, 0.001);
394
+ const du = 1 / Math.max(this.cpuWidth - 1, 1);
395
+ const dv = 1 / Math.max(this.cpuHeight - 1, 1);
396
+ const center = sample(this.cpuCurrent, u, v);
397
+ const previous = sample(this.cpuPrevious, u, v);
398
+ const left = sample(this.cpuCurrent, u - du, v);
399
+ const right = sample(this.cpuCurrent, u + du, v);
400
+ const down = sample(this.cpuCurrent, u, v - dv);
401
+ const up = sample(this.cpuCurrent, u, v + dv);
402
+ const cellX = (this.basinMaxX - this.basinMinX) * du;
403
+ const cellZ = this.basinHalfZ * 2 * dv;
404
+ const displacementScale = 0.52;
405
+ const ambientCenter = ambientSurfaceHeight(u, v, this.elapsed) + sloshSurfaceHeight(u, v, this.slosh);
406
+ const ambientPrevious = ambientSurfaceHeight(u, v, this.elapsed - this.cpuLastDelta)
407
+ + sloshSurfaceHeight(u, v, this.previousSlosh);
408
+ const ambientLeft = ambientSurfaceHeight(u - du, v, this.elapsed) + sloshSurfaceHeight(u - du, v, this.slosh);
409
+ const ambientRight = ambientSurfaceHeight(u + du, v, this.elapsed) + sloshSurfaceHeight(u + du, v, this.slosh);
410
+ const ambientDown = ambientSurfaceHeight(u, v - dv, this.elapsed) + sloshSurfaceHeight(u, v - dv, this.slosh);
411
+ const ambientUp = ambientSurfaceHeight(u, v + dv, this.elapsed) + sloshSurfaceHeight(u, v + dv, this.slosh);
412
+ target.height = center * displacementScale + ambientCenter;
413
+ target.velocityY = ((center - previous) * displacementScale + ambientCenter - ambientPrevious)
414
+ / this.cpuLastDelta;
415
+ target.slopeX = ((right - left) * displacementScale + ambientRight - ambientLeft)
416
+ / Math.max(cellX * 2, 0.001);
417
+ target.slopeZ = ((up - down) * displacementScale + ambientUp - ambientDown)
418
+ / Math.max(cellZ * 2, 0.001);
419
+ target.flowX = -target.slopeX * 1.35;
420
+ target.flowZ = -target.slopeZ * 1.35;
421
+ if (this.cpuEmitting) {
422
+ const dx = worldX - this.cpuImpactX;
423
+ const dz = worldZ - this.cpuImpactZ;
424
+ const distanceSq = dx * dx + dz * dz;
425
+ const inverseDistance = 1 / Math.max(Math.sqrt(distanceSq), 0.06);
426
+ const sourceStrength = Math.min(this.cpuImpactEnergy * 0.075, 1.15) * Math.exp(-distanceSq / 0.48);
427
+ target.flowX += sourceStrength * (0.42 + dx * inverseDistance * 0.68);
428
+ target.flowZ += sourceStrength * dz * inverseDistance * 0.92;
429
+ target.impact = sourceStrength * Math.exp(-distanceSq / 0.095);
430
+ }
431
+ return target;
432
+ }
433
+ dispose() {
434
+ this.read.dispose();
435
+ this.write.dispose();
436
+ this.computeMaterial.dispose();
437
+ this.computeQuad.geometry.dispose();
438
+ this.material.dispose();
439
+ this.mesh.geometry.dispose();
440
+ }
441
+ }