three-base 2.0.142 → 2.0.166

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 (231) 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 +531 -511
  10. package/FPScontrol.js +1866 -1799
  11. package/FPScontrol.ts +1922 -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 +251 -247
  33. package/MeshBase.ts +6 -2
  34. package/Mirror.d.ts +25 -25
  35. package/Mirror.js +143 -143
  36. package/MktMesh/MktMesh.d.ts +301 -291
  37. package/MktMesh/MktMesh.js +1064 -1045
  38. package/MktMesh/MktMesh.ts +32 -11
  39. package/MktMesh/assetUrl.d.ts +59 -35
  40. package/MktMesh/assetUrl.js +165 -69
  41. package/MktMesh/assetUrl.ts +164 -69
  42. package/MktMesh/mktMappingShader.d.ts +12 -12
  43. package/MktMesh/mktMappingShader.js +138 -138
  44. package/MktMesh/mktMappingShader.ts +139 -139
  45. package/MktMesh/mktShader.d.ts +263 -263
  46. package/MktMesh/mktShader.js +1236 -1236
  47. package/MktMesh/mktShader.ts +1344 -1344
  48. package/MktMesh/panoBitmapTexture.d.ts +25 -25
  49. package/MktMesh/panoBitmapTexture.js +52 -52
  50. package/MktMesh/panoBitmapTexture.ts +54 -54
  51. package/MktMesh/panoSlotBind.d.ts +29 -29
  52. package/MktMesh/panoSlotBind.js +106 -106
  53. package/MktMesh/panoSlotBind.ts +153 -153
  54. package/MktMesh/panoSlotRoster.d.ts +22 -22
  55. package/MktMesh/panoSlotRoster.js +169 -169
  56. package/MktMesh/panoSlotRoster.ts +168 -168
  57. package/MktMesh/panoTileStitcher.d.ts +119 -119
  58. package/MktMesh/panoTileStitcher.js +529 -529
  59. package/MktMesh/panoTileStitcher.ts +616 -616
  60. package/MktMesh/types.d.ts +75 -75
  61. package/MktMesh/types.js +1 -1
  62. package/MktMesh/wallCapTessellate.d.ts +33 -33
  63. package/MktMesh/wallCapTessellate.js +97 -97
  64. package/MktMesh/wallCapTessellate.ts +68 -68
  65. package/PipeMesh/PathGeometry.d.ts +44 -44
  66. package/PipeMesh/PathGeometry.js +307 -307
  67. package/PipeMesh/PathPoint.d.ts +17 -17
  68. package/PipeMesh/PathPoint.js +31 -31
  69. package/PipeMesh/PathPointList.d.ts +31 -31
  70. package/PipeMesh/PathPointList.js +209 -209
  71. package/PipeMesh/PathTubeGeometry.d.ts +25 -25
  72. package/PipeMesh/PathTubeGeometry.js +130 -130
  73. package/PipeMesh/PipeMesh.d.ts +25 -25
  74. package/PipeMesh/PipeMesh.js +63 -63
  75. package/PipeMesh/index.d.ts +1 -1
  76. package/PipeMesh/index.js +1 -1
  77. package/PlaneModal1/PlaneModal1.d.ts +34 -34
  78. package/PlaneModal1/PlaneModal1.js +109 -109
  79. package/PlaneModal1/index.d.ts +1 -1
  80. package/PlaneModal1/index.js +1 -1
  81. package/PlaneText.d.ts +27 -27
  82. package/PlaneText.js +35 -35
  83. package/PlaneWithHtml.d.ts +13 -13
  84. package/PlaneWithHtml.js +66 -64
  85. package/PlaneWithHtml.ts +3 -1
  86. package/PointLight/PointLight.d.ts +13 -13
  87. package/PointLight/PointLight.js +64 -64
  88. package/PointLight/index.d.ts +1 -1
  89. package/PointLight/index.js +1 -1
  90. package/Sky/Sky.d.ts +9 -9
  91. package/Sky/Sky.js +22 -22
  92. package/Sky/index.d.ts +1 -1
  93. package/Sky/index.js +1 -1
  94. package/SpotLight.d.ts +6 -6
  95. package/SpotLight.js +72 -72
  96. package/SpriteModal1/SpriteModal1.d.ts +33 -33
  97. package/SpriteModal1/SpriteModal1.js +116 -116
  98. package/SpriteModal1/index.d.ts +1 -1
  99. package/SpriteModal1/index.js +1 -1
  100. package/SpriteText.d.ts +46 -46
  101. package/SpriteText.js +176 -176
  102. package/SpriteText2.d.ts +48 -48
  103. package/SpriteText2.js +223 -223
  104. package/SpriteText3.d.ts +86 -86
  105. package/SpriteText3.js +517 -517
  106. package/SpriteText3.ts +599 -599
  107. package/SpriteWithHtml.d.ts +9 -9
  108. package/SpriteWithHtml.js +46 -44
  109. package/SpriteWithHtml.ts +3 -1
  110. package/Text3D/Text3D.d.ts +20 -20
  111. package/Text3D/Text3D.js +58 -58
  112. package/Text3D/fontController.d.ts +60 -60
  113. package/Text3D/fontController.js +166 -166
  114. package/Text3D/index.d.ts +1 -1
  115. package/Text3D/index.js +1 -1
  116. package/ThreeBase.d.ts +248 -248
  117. package/ThreeBase.js +1202 -1202
  118. package/ThreeBase.ts +1314 -1314
  119. package/ThreeMouse.d.ts +53 -54
  120. package/ThreeMouse.js +453 -453
  121. package/Transition.d.ts +45 -45
  122. package/Transition.js +209 -209
  123. package/Water/Water.d.ts +5 -5
  124. package/Water/Water.js +44 -44
  125. package/Water/index.d.ts +2 -2
  126. package/Water/index.js +2 -2
  127. package/WaterFlow/FloatingDuck.d.ts +88 -88
  128. package/WaterFlow/FloatingDuck.js +307 -307
  129. package/WaterFlow/FloatingDuck.ts +445 -445
  130. package/WaterFlow/GPUFluidSimulation.d.ts +56 -56
  131. package/WaterFlow/GPUFluidSimulation.js +781 -781
  132. package/WaterFlow/GPUFluidSimulation.ts +827 -827
  133. package/WaterFlow/ReservoirSystem.d.ts +106 -106
  134. package/WaterFlow/ReservoirSystem.js +611 -611
  135. package/WaterFlow/ReservoirSystem.ts +754 -754
  136. package/WaterFlow/SPHField.d.ts +35 -35
  137. package/WaterFlow/SPHField.js +203 -203
  138. package/WaterFlow/SPHField.ts +228 -228
  139. package/WaterFlow/ScreenSpaceFluidPass.d.ts +69 -69
  140. package/WaterFlow/ScreenSpaceFluidPass.js +728 -728
  141. package/WaterFlow/ScreenSpaceFluidPass.ts +789 -789
  142. package/WaterFlow/ShallowWaterSurface.d.ts +45 -45
  143. package/WaterFlow/ShallowWaterSurface.js +438 -438
  144. package/WaterFlow/ShallowWaterSurface.ts +510 -510
  145. package/WaterFlow/WaterFlow.d.ts +87 -87
  146. package/WaterFlow/WaterFlow.js +171 -171
  147. package/WaterFlow/WaterFlow.ts +219 -219
  148. package/WaterFlow/types.d.ts +161 -161
  149. package/WaterFlow/types.js +1 -1
  150. package/WaterFlow/types.ts +189 -189
  151. package/WaterFlow/waterFlowPass.d.ts +3 -3
  152. package/WaterFlow/waterFlowPass.js +20 -20
  153. package/WaterFlow/waterFlowPass.ts +40 -40
  154. package/collisionOBB.d.ts +42 -42
  155. package/collisionOBB.js +112 -112
  156. package/collisionOBB.ts +108 -108
  157. package/declare.d.ts +25 -25
  158. package/index.d.ts +1 -1
  159. package/index.js +1 -1
  160. package/instanced/InstancedBatch.d.ts +57 -57
  161. package/instanced/InstancedBatch.js +348 -348
  162. package/instanced/InstancedBatch.ts +406 -406
  163. package/instanced/InstancedChild.d.ts +174 -174
  164. package/instanced/InstancedChild.js +264 -264
  165. package/instanced/InstancedChild.ts +327 -327
  166. package/instanced/InstancedGroup.d.ts +43 -43
  167. package/instanced/InstancedGroup.js +126 -126
  168. package/instanced/InstancedGroup.ts +137 -137
  169. package/instanced/InstancedMeshBase.d.ts +38 -38
  170. package/instanced/InstancedMeshBase.js +282 -282
  171. package/instanced/InstancedTemplate.d.ts +46 -46
  172. package/instanced/InstancedTemplate.js +125 -125
  173. package/keyControls.d.ts +29 -30
  174. package/keyControls.js +101 -101
  175. package/lensflare/LensFlare.d.ts +39 -39
  176. package/lensflare/LensFlare.js +284 -284
  177. package/package.json +6 -1
  178. package/pass/bloom_pass.d.ts +2 -2
  179. package/pass/bloom_pass.js +174 -174
  180. package/pass/cover_pass.d.ts +2 -2
  181. package/pass/cover_pass.js +108 -108
  182. package/pass/debugDepthState.d.ts +20 -20
  183. package/pass/debugDepthState.js +9 -9
  184. package/pass/debugDepthState.ts +22 -22
  185. package/pass/depthTestBias.d.ts +12 -12
  186. package/pass/depthTestBias.js +12 -12
  187. package/pass/depthTestBias.ts +12 -12
  188. package/pass/effectUserData.d.ts +27 -27
  189. package/pass/effectUserData.js +1 -1
  190. package/pass/effectUserData.ts +46 -46
  191. package/pass/frosted_glass_pass.d.ts +2 -2
  192. package/pass/frosted_glass_pass.js +150 -150
  193. package/pass/fsQuad.d.ts +9 -9
  194. package/pass/fsQuad.js +28 -28
  195. package/pass/glow_pass.d.ts +2 -2
  196. package/pass/glow_pass.js +350 -350
  197. package/pass/screen_bloom_pass.d.ts +11 -11
  198. package/pass/screen_bloom_pass.js +136 -136
  199. package/pass/ssao_pass.d.ts +88 -88
  200. package/pass/ssao_pass.js +799 -799
  201. package/pass/ssao_pass.ts +918 -918
  202. package/pass/stroke_pass.d.ts +2 -2
  203. package/pass/stroke_pass.js +267 -267
  204. package/shaders/FXAAShaderMaterial.d.ts +3 -3
  205. package/shaders/FXAAShaderMaterial.js +20 -20
  206. package/shaders/baseDepthVisShader.d.ts +11 -11
  207. package/shaders/baseDepthVisShader.js +36 -36
  208. package/shaders/baseDepthVisShader.ts +36 -36
  209. package/shaders/baseTextureToRenderShader.d.ts +2 -2
  210. package/shaders/baseTextureToRenderShader.js +19 -19
  211. package/shaders/copyShader.d.ts +3 -3
  212. package/shaders/copyShader.js +13 -13
  213. package/shaders/debugDepthCompareShader.d.ts +14 -14
  214. package/shaders/debugDepthCompareShader.js +49 -49
  215. package/shaders/debugDepthCompareShader.ts +50 -50
  216. package/shaders/debugDepthJudgeShader.d.ts +18 -18
  217. package/shaders/debugDepthJudgeShader.js +52 -52
  218. package/shaders/debugDepthJudgeShader.ts +54 -54
  219. package/shaders/debugExcludedShader.d.ts +13 -13
  220. package/shaders/debugExcludedShader.js +46 -46
  221. package/shaders/debugExcludedShader.ts +48 -48
  222. package/spriteTextBg.d.ts +1 -1
  223. package/spriteTextBg.js +20 -20
  224. package/spriteTextBg.ts +20 -20
  225. package/threeLoader.d.ts +35 -35
  226. package/threeLoader.js +518 -518
  227. package/threeUtils.d.ts +71 -53
  228. package/threeUtils.js +363 -329
  229. package/threeUtils.ts +35 -0
  230. package/types.d.ts +2 -2
  231. 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
+ }