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