three-base 2.0.142 → 2.0.145

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (227) hide show
  1. package/DirectionalLight.d.ts +6 -6
  2. package/DirectionalLight.js +68 -68
  3. package/EventMap.d.ts +103 -103
  4. package/EventMap.js +17 -17
  5. package/EventMesh.d.ts +18 -18
  6. package/EventMesh.js +59 -59
  7. package/ExtrudeMesh.d.ts +112 -112
  8. package/ExtrudeMesh.js +342 -342
  9. package/FPScontrol.d.ts +511 -511
  10. package/FPScontrol.js +1799 -1799
  11. package/FPScontrol.ts +1855 -1855
  12. package/Fire/Fire.d.ts +28 -28
  13. package/Fire/Fire.js +80 -80
  14. package/Fire/FireGeometry.d.ts +9 -9
  15. package/Fire/FireGeometry.js +40 -40
  16. package/Fire/FireMaterial.d.ts +28 -28
  17. package/Fire/FireMaterial.js +32 -32
  18. package/Fire/index.d.ts +2 -2
  19. package/Fire/index.js +2 -2
  20. package/GLTFDracoLoader.d.ts +71 -71
  21. package/GLTFDracoLoader.js +266 -266
  22. package/GLTFDracoLoader.ts +1 -1
  23. package/Grass/Grass.d.ts +8 -8
  24. package/Grass/Grass.js +38 -38
  25. package/Grass/index.d.ts +2 -2
  26. package/Grass/index.js +2 -2
  27. package/Ground1/Ground1.d.ts +10 -10
  28. package/Ground1/Ground1.js +71 -71
  29. package/Ground1/index.d.ts +1 -1
  30. package/Ground1/index.js +1 -1
  31. package/MeshBase.d.ts +36 -36
  32. package/MeshBase.js +247 -247
  33. package/Mirror.d.ts +25 -25
  34. package/Mirror.js +143 -143
  35. package/MktMesh/MktMesh.d.ts +301 -291
  36. package/MktMesh/MktMesh.js +1064 -1045
  37. package/MktMesh/MktMesh.ts +32 -11
  38. package/MktMesh/assetUrl.d.ts +59 -35
  39. package/MktMesh/assetUrl.js +154 -69
  40. package/MktMesh/assetUrl.ts +155 -69
  41. package/MktMesh/mktMappingShader.d.ts +12 -12
  42. package/MktMesh/mktMappingShader.js +138 -138
  43. package/MktMesh/mktMappingShader.ts +139 -139
  44. package/MktMesh/mktShader.d.ts +263 -263
  45. package/MktMesh/mktShader.js +1236 -1236
  46. package/MktMesh/mktShader.ts +1344 -1344
  47. package/MktMesh/panoBitmapTexture.d.ts +25 -25
  48. package/MktMesh/panoBitmapTexture.js +52 -52
  49. package/MktMesh/panoBitmapTexture.ts +54 -54
  50. package/MktMesh/panoSlotBind.d.ts +29 -29
  51. package/MktMesh/panoSlotBind.js +106 -106
  52. package/MktMesh/panoSlotBind.ts +153 -153
  53. package/MktMesh/panoSlotRoster.d.ts +22 -22
  54. package/MktMesh/panoSlotRoster.js +169 -169
  55. package/MktMesh/panoSlotRoster.ts +168 -168
  56. package/MktMesh/panoTileStitcher.d.ts +119 -119
  57. package/MktMesh/panoTileStitcher.js +529 -529
  58. package/MktMesh/panoTileStitcher.ts +616 -616
  59. package/MktMesh/types.d.ts +75 -75
  60. package/MktMesh/types.js +1 -1
  61. package/MktMesh/wallCapTessellate.d.ts +33 -33
  62. package/MktMesh/wallCapTessellate.js +97 -97
  63. package/MktMesh/wallCapTessellate.ts +68 -68
  64. package/PipeMesh/PathGeometry.d.ts +44 -44
  65. package/PipeMesh/PathGeometry.js +307 -307
  66. package/PipeMesh/PathPoint.d.ts +17 -17
  67. package/PipeMesh/PathPoint.js +31 -31
  68. package/PipeMesh/PathPointList.d.ts +31 -31
  69. package/PipeMesh/PathPointList.js +209 -209
  70. package/PipeMesh/PathTubeGeometry.d.ts +25 -25
  71. package/PipeMesh/PathTubeGeometry.js +130 -130
  72. package/PipeMesh/PipeMesh.d.ts +25 -25
  73. package/PipeMesh/PipeMesh.js +63 -63
  74. package/PipeMesh/index.d.ts +1 -1
  75. package/PipeMesh/index.js +1 -1
  76. package/PlaneModal1/PlaneModal1.d.ts +34 -34
  77. package/PlaneModal1/PlaneModal1.js +109 -109
  78. package/PlaneModal1/index.d.ts +1 -1
  79. package/PlaneModal1/index.js +1 -1
  80. package/PlaneText.d.ts +27 -27
  81. package/PlaneText.js +35 -35
  82. package/PlaneWithHtml.d.ts +13 -13
  83. package/PlaneWithHtml.js +64 -64
  84. package/PointLight/PointLight.d.ts +13 -13
  85. package/PointLight/PointLight.js +64 -64
  86. package/PointLight/index.d.ts +1 -1
  87. package/PointLight/index.js +1 -1
  88. package/Sky/Sky.d.ts +9 -9
  89. package/Sky/Sky.js +22 -22
  90. package/Sky/index.d.ts +1 -1
  91. package/Sky/index.js +1 -1
  92. package/SpotLight.d.ts +6 -6
  93. package/SpotLight.js +72 -72
  94. package/SpriteModal1/SpriteModal1.d.ts +33 -33
  95. package/SpriteModal1/SpriteModal1.js +116 -116
  96. package/SpriteModal1/index.d.ts +1 -1
  97. package/SpriteModal1/index.js +1 -1
  98. package/SpriteText.d.ts +46 -46
  99. package/SpriteText.js +176 -176
  100. package/SpriteText2.d.ts +48 -48
  101. package/SpriteText2.js +223 -223
  102. package/SpriteText3.d.ts +86 -86
  103. package/SpriteText3.js +517 -517
  104. package/SpriteText3.ts +599 -599
  105. package/SpriteWithHtml.d.ts +9 -9
  106. package/SpriteWithHtml.js +44 -44
  107. package/Text3D/Text3D.d.ts +20 -20
  108. package/Text3D/Text3D.js +58 -58
  109. package/Text3D/fontController.d.ts +60 -60
  110. package/Text3D/fontController.js +166 -166
  111. package/Text3D/index.d.ts +1 -1
  112. package/Text3D/index.js +1 -1
  113. package/ThreeBase.d.ts +248 -248
  114. package/ThreeBase.js +1202 -1202
  115. package/ThreeBase.ts +1314 -1314
  116. package/ThreeMouse.d.ts +53 -54
  117. package/ThreeMouse.js +453 -453
  118. package/Transition.d.ts +45 -45
  119. package/Transition.js +209 -209
  120. package/Water/Water.d.ts +5 -5
  121. package/Water/Water.js +44 -44
  122. package/Water/index.d.ts +2 -2
  123. package/Water/index.js +2 -2
  124. package/WaterFlow/FloatingDuck.d.ts +88 -88
  125. package/WaterFlow/FloatingDuck.js +307 -307
  126. package/WaterFlow/FloatingDuck.ts +445 -445
  127. package/WaterFlow/GPUFluidSimulation.d.ts +56 -56
  128. package/WaterFlow/GPUFluidSimulation.js +781 -781
  129. package/WaterFlow/GPUFluidSimulation.ts +827 -827
  130. package/WaterFlow/ReservoirSystem.d.ts +106 -106
  131. package/WaterFlow/ReservoirSystem.js +611 -611
  132. package/WaterFlow/ReservoirSystem.ts +754 -754
  133. package/WaterFlow/SPHField.d.ts +35 -35
  134. package/WaterFlow/SPHField.js +203 -203
  135. package/WaterFlow/SPHField.ts +228 -228
  136. package/WaterFlow/ScreenSpaceFluidPass.d.ts +69 -69
  137. package/WaterFlow/ScreenSpaceFluidPass.js +728 -728
  138. package/WaterFlow/ScreenSpaceFluidPass.ts +789 -789
  139. package/WaterFlow/ShallowWaterSurface.d.ts +45 -45
  140. package/WaterFlow/ShallowWaterSurface.js +438 -438
  141. package/WaterFlow/ShallowWaterSurface.ts +510 -510
  142. package/WaterFlow/WaterFlow.d.ts +87 -87
  143. package/WaterFlow/WaterFlow.js +171 -171
  144. package/WaterFlow/WaterFlow.ts +219 -219
  145. package/WaterFlow/types.d.ts +161 -161
  146. package/WaterFlow/types.js +1 -1
  147. package/WaterFlow/types.ts +189 -189
  148. package/WaterFlow/waterFlowPass.d.ts +3 -3
  149. package/WaterFlow/waterFlowPass.js +20 -20
  150. package/WaterFlow/waterFlowPass.ts +40 -40
  151. package/collisionOBB.d.ts +42 -42
  152. package/collisionOBB.js +112 -112
  153. package/collisionOBB.ts +108 -108
  154. package/declare.d.ts +25 -25
  155. package/index.d.ts +1 -1
  156. package/index.js +1 -1
  157. package/instanced/InstancedBatch.d.ts +57 -57
  158. package/instanced/InstancedBatch.js +348 -348
  159. package/instanced/InstancedBatch.ts +406 -406
  160. package/instanced/InstancedChild.d.ts +174 -174
  161. package/instanced/InstancedChild.js +264 -264
  162. package/instanced/InstancedChild.ts +327 -327
  163. package/instanced/InstancedGroup.d.ts +43 -43
  164. package/instanced/InstancedGroup.js +126 -126
  165. package/instanced/InstancedGroup.ts +137 -137
  166. package/instanced/InstancedMeshBase.d.ts +38 -38
  167. package/instanced/InstancedMeshBase.js +282 -282
  168. package/instanced/InstancedTemplate.d.ts +46 -46
  169. package/instanced/InstancedTemplate.js +125 -125
  170. package/keyControls.d.ts +29 -30
  171. package/keyControls.js +101 -101
  172. package/lensflare/LensFlare.d.ts +39 -39
  173. package/lensflare/LensFlare.js +284 -284
  174. package/package.json +6 -1
  175. package/pass/bloom_pass.d.ts +2 -2
  176. package/pass/bloom_pass.js +174 -174
  177. package/pass/cover_pass.d.ts +2 -2
  178. package/pass/cover_pass.js +108 -108
  179. package/pass/debugDepthState.d.ts +20 -20
  180. package/pass/debugDepthState.js +9 -9
  181. package/pass/debugDepthState.ts +22 -22
  182. package/pass/depthTestBias.d.ts +12 -12
  183. package/pass/depthTestBias.js +12 -12
  184. package/pass/depthTestBias.ts +12 -12
  185. package/pass/effectUserData.d.ts +27 -27
  186. package/pass/effectUserData.js +1 -1
  187. package/pass/effectUserData.ts +46 -46
  188. package/pass/frosted_glass_pass.d.ts +2 -2
  189. package/pass/frosted_glass_pass.js +150 -150
  190. package/pass/fsQuad.d.ts +9 -9
  191. package/pass/fsQuad.js +28 -28
  192. package/pass/glow_pass.d.ts +2 -2
  193. package/pass/glow_pass.js +350 -350
  194. package/pass/screen_bloom_pass.d.ts +11 -11
  195. package/pass/screen_bloom_pass.js +136 -136
  196. package/pass/ssao_pass.d.ts +88 -88
  197. package/pass/ssao_pass.js +799 -799
  198. package/pass/ssao_pass.ts +918 -918
  199. package/pass/stroke_pass.d.ts +2 -2
  200. package/pass/stroke_pass.js +267 -267
  201. package/shaders/FXAAShaderMaterial.d.ts +3 -3
  202. package/shaders/FXAAShaderMaterial.js +20 -20
  203. package/shaders/baseDepthVisShader.d.ts +11 -11
  204. package/shaders/baseDepthVisShader.js +36 -36
  205. package/shaders/baseDepthVisShader.ts +36 -36
  206. package/shaders/baseTextureToRenderShader.d.ts +2 -2
  207. package/shaders/baseTextureToRenderShader.js +19 -19
  208. package/shaders/copyShader.d.ts +3 -3
  209. package/shaders/copyShader.js +13 -13
  210. package/shaders/debugDepthCompareShader.d.ts +14 -14
  211. package/shaders/debugDepthCompareShader.js +49 -49
  212. package/shaders/debugDepthCompareShader.ts +50 -50
  213. package/shaders/debugDepthJudgeShader.d.ts +18 -18
  214. package/shaders/debugDepthJudgeShader.js +52 -52
  215. package/shaders/debugDepthJudgeShader.ts +54 -54
  216. package/shaders/debugExcludedShader.d.ts +13 -13
  217. package/shaders/debugExcludedShader.js +46 -46
  218. package/shaders/debugExcludedShader.ts +48 -48
  219. package/spriteTextBg.d.ts +1 -1
  220. package/spriteTextBg.js +20 -20
  221. package/spriteTextBg.ts +20 -20
  222. package/threeLoader.d.ts +35 -35
  223. package/threeLoader.js +518 -518
  224. package/threeUtils.d.ts +53 -53
  225. package/threeUtils.js +329 -329
  226. package/types.d.ts +2 -2
  227. package/types.js +6 -6
@@ -1,789 +1,789 @@
1
- import * as THREE from 'three';
2
- import { Pass, FullScreenQuad } from 'three/examples/jsm/postprocessing/Pass.js';
3
- import type {
4
- ExternalBaseTextures,
5
- FluidQuality,
6
- PartialBaseTextures,
7
- ScreenSpaceFluidPassOptions,
8
- ScreenSpaceFluidSource,
9
- WaterFlowRenderOptions,
10
- } from './types';
11
-
12
- const fullscreenVertex = /* glsl */`
13
- out vec2 vUv;
14
- void main() {
15
- vUv = uv;
16
- gl_Position = vec4(position, 1.0);
17
- }
18
- `;
19
-
20
- function createColorDepthTarget(width: number, height: number): THREE.WebGLRenderTarget {
21
- const target = new THREE.WebGLRenderTarget(width, height, {
22
- type: THREE.HalfFloatType,
23
- format: THREE.RGBAFormat,
24
- minFilter: THREE.LinearFilter,
25
- magFilter: THREE.LinearFilter,
26
- depthBuffer: true,
27
- });
28
- target.depthTexture = new THREE.DepthTexture(width, height, THREE.UnsignedIntType);
29
- target.depthTexture.minFilter = THREE.NearestFilter;
30
- target.depthTexture.magFilter = THREE.NearestFilter;
31
- return target;
32
- }
33
-
34
- function createFluidTarget(width: number, height: number, depthBuffer = false): THREE.WebGLRenderTarget {
35
- return new THREE.WebGLRenderTarget(width, height, {
36
- type: THREE.HalfFloatType,
37
- format: THREE.RGBAFormat,
38
- minFilter: THREE.LinearFilter,
39
- magFilter: THREE.LinearFilter,
40
- wrapS: THREE.ClampToEdgeWrapping,
41
- wrapT: THREE.ClampToEdgeWrapping,
42
- depthBuffer,
43
- stencilBuffer: false,
44
- });
45
- }
46
-
47
- function createLookupGeometry(size: number): THREE.BufferGeometry {
48
- const count = size * size;
49
- const positions = new Float32Array(count * 3);
50
- const lookup = new Float32Array(count * 2);
51
- for (let y = 0; y < size; y++) {
52
- for (let x = 0; x < size; x++) {
53
- const i = y * size + x;
54
- lookup[i * 2] = (x + 0.5) / size;
55
- lookup[i * 2 + 1] = (y + 0.5) / size;
56
- }
57
- }
58
- const geometry = new THREE.BufferGeometry();
59
- geometry.setAttribute('position', new THREE.BufferAttribute(positions, 3));
60
- geometry.setAttribute('aLookup', new THREE.BufferAttribute(lookup, 2));
61
- return geometry;
62
- }
63
-
64
- const particleVertex = /* glsl */`
65
- precision highp float;
66
- uniform sampler2D uPosition;
67
- uniform sampler2D uVelocity;
68
- uniform float uParticleRadius;
69
- uniform float uRetainedRadiusScale;
70
- uniform float uShowRetained;
71
- uniform float uPointScale;
72
- in vec2 aLookup;
73
- out vec3 vViewCenter;
74
- out float vRadius;
75
- out float vImpact;
76
- out float vRetained;
77
- out float vAlive;
78
-
79
- void main() {
80
- vec4 particle = texture(uPosition, aLookup);
81
- vec4 velocity = texture(uVelocity, aLookup);
82
- float retained = 1.0 - step(-0.5, velocity.w);
83
- vRetained = retained;
84
- vAlive = step(0.0, particle.w) * mix(1.0, uShowRetained, retained);
85
- vImpact = velocity.w;
86
- float jetRadius = mix(uParticleRadius, uParticleRadius * 0.56, smoothstep(0.05, 0.7, velocity.w));
87
- vRadius = mix(jetRadius, uParticleRadius * uRetainedRadiusScale, retained);
88
- vec4 mv = viewMatrix * vec4(particle.xyz, 1.0);
89
- vViewCenter = mv.xyz;
90
- gl_Position = projectionMatrix * mv;
91
- gl_PointSize = vAlive * max(0.0, vRadius * uPointScale / max(0.2, -mv.z));
92
- if (vAlive < 0.5) gl_Position = vec4(2.0, 2.0, 2.0, 1.0);
93
- }
94
- `;
95
-
96
- export class ScreenSpaceFluidPass extends Pass {
97
- /**
98
- * 字段集中声明。
99
- *
100
- * 构造器在 `if (!this.supported) return;` 处提前返回,因此下面标注 `T | null` 的这批资源字段
101
- * 在「不支持 WebGL2 / 浮点纹理」的空壳实例上**根本没被赋值**(运行时是 undefined,不是 null)。
102
- * 一律建模为 `T | null` 而不是用 `!` 硬压,且所有读取点都在 `if (!this.supported) return;`
103
- * 守卫之后(setSize/dispose 里原有的 `?.` 也照留),语义与原实现逐字节一致。
104
- */
105
- private readonly scene: THREE.Scene;
106
- /** setSize 要读 camera.fov 算 pointScale,故按透视相机建模。 */
107
- private readonly camera: THREE.PerspectiveCamera;
108
- private readonly simulation: ScreenSpaceFluidSource;
109
- private readonly supported: boolean;
110
- private scale: number;
111
- private airFraction: number;
112
- private activeCount: number;
113
- /** 外部注入的基础纹理;只在 renderInto 里被赋值,此前为 undefined,setBaseTextures 的 falsy 守卫会直接返回。 */
114
- private externalBase: ExternalBaseTextures | null;
115
- /** 由 setSize 写入(构造器 supported 分支末尾调用过),空壳实例上为 undefined。 */
116
- private width: number | null;
117
- private height: number | null;
118
- private readonly sceneTarget: THREE.WebGLRenderTarget | null;
119
- private readonly depthTarget: THREE.WebGLRenderTarget | null;
120
- private readonly thicknessTarget: THREE.WebGLRenderTarget | null;
121
- private readonly blurA: THREE.WebGLRenderTarget | null;
122
- private readonly blurB: THREE.WebGLRenderTarget | null;
123
- private readonly thicknessBlurA: THREE.WebGLRenderTarget | null;
124
- private readonly thicknessBlurB: THREE.WebGLRenderTarget | null;
125
- private readonly compositeTarget: THREE.WebGLRenderTarget | null;
126
- private readonly lookupGeometry: THREE.BufferGeometry | null;
127
- private readonly depthMaterial: THREE.ShaderMaterial | null;
128
- private readonly thicknessMaterial: THREE.ShaderMaterial | null;
129
- private readonly sprayMaterial: THREE.ShaderMaterial | null;
130
- private readonly depthBlurMaterial: THREE.ShaderMaterial | null;
131
- private readonly thicknessBlurMaterial: THREE.ShaderMaterial | null;
132
- private readonly compositeMaterial: THREE.ShaderMaterial | null;
133
- private readonly copyMaterial: THREE.ShaderMaterial | null;
134
- private readonly particleScene: THREE.Scene | null;
135
- private readonly sprayScene: THREE.Scene | null;
136
- private readonly depthPoints: THREE.Points<THREE.BufferGeometry, THREE.ShaderMaterial> | null;
137
- private readonly sprayPoints: THREE.Points<THREE.BufferGeometry, THREE.ShaderMaterial> | null;
138
- private readonly fsQuad: FullScreenQuad | null;
139
-
140
- constructor(
141
- scene: THREE.Scene,
142
- camera: THREE.PerspectiveCamera,
143
- simulation: ScreenSpaceFluidSource,
144
- { width = 1, height = 1, scale = 0.75, activeCount = simulation.count }: ScreenSpaceFluidPassOptions = {},
145
- ) {
146
- super();
147
- this.scene = scene;
148
- this.camera = camera;
149
- this.simulation = simulation;
150
- this.scale = scale;
151
- this.airFraction = 0.18;
152
- this.activeCount = Math.min(activeCount, simulation.count);
153
- this.supported = simulation.supported;
154
- this.needsSwap = true;
155
-
156
- if (!this.supported) return;
157
-
158
- this.sceneTarget = createColorDepthTarget(width, height);
159
- this.depthTarget = createFluidTarget(Math.ceil(width * scale), Math.ceil(height * scale), true);
160
- this.thicknessTarget = createFluidTarget(Math.ceil(width * scale), Math.ceil(height * scale));
161
- this.blurA = createFluidTarget(Math.ceil(width * scale), Math.ceil(height * scale));
162
- this.blurB = createFluidTarget(Math.ceil(width * scale), Math.ceil(height * scale));
163
- this.thicknessBlurA = createFluidTarget(Math.ceil(width * scale), Math.ceil(height * scale));
164
- this.thicknessBlurB = createFluidTarget(Math.ceil(width * scale), Math.ceil(height * scale));
165
-
166
- this.lookupGeometry = createLookupGeometry(simulation.size);
167
- this.lookupGeometry.setDrawRange(0, this.activeCount);
168
-
169
- this.depthMaterial = new THREE.ShaderMaterial({
170
- glslVersion: THREE.GLSL3,
171
- uniforms: {
172
- uPosition: { value: simulation.positionTexture },
173
- uVelocity: { value: simulation.velocityTexture },
174
- uParticleRadius: { value: 0.037 },
175
- uRetainedRadiusScale: { value: 1.55 },
176
- uShowRetained: { value: 0 },
177
- uPointScale: { value: 1 },
178
- uProjectionMatrix: { value: camera.projectionMatrix.clone() },
179
- },
180
- vertexShader: particleVertex,
181
- fragmentShader: /* glsl */`
182
- precision highp float;
183
- uniform mat4 uProjectionMatrix;
184
- in vec3 vViewCenter;
185
- in float vRadius;
186
- in float vImpact;
187
- in float vRetained;
188
- in float vAlive;
189
- out vec4 outColor;
190
- void main() {
191
- vec2 circle = gl_PointCoord * 2.0 - 1.0;
192
- float radiusSq = dot(circle, circle);
193
- if (radiusSq > 1.0 || vAlive < 0.5) discard;
194
- float sphereZ = sqrt(max(0.0, 1.0 - radiusSq));
195
- vec3 surfacePosition = vViewCenter + vec3(circle * vRadius, sphereZ * vRadius);
196
- vec4 clip = uProjectionMatrix * vec4(surfacePosition, 1.0);
197
- float ndcDepth = clip.z / clip.w;
198
- gl_FragDepth = ndcDepth * 0.5 + 0.5;
199
- outColor = vec4(-surfacePosition.z, max(vImpact, 0.0), vRetained, 1.0);
200
- }
201
- `,
202
- depthTest: true,
203
- depthWrite: true,
204
- blending: THREE.NoBlending,
205
- toneMapped: false,
206
- });
207
-
208
- this.thicknessMaterial = new THREE.ShaderMaterial({
209
- glslVersion: THREE.GLSL3,
210
- uniforms: {
211
- uPosition: { value: simulation.positionTexture },
212
- uVelocity: { value: simulation.velocityTexture },
213
- uParticleRadius: { value: 0.037 },
214
- uRetainedRadiusScale: { value: 1.55 },
215
- uShowRetained: { value: 0 },
216
- uPointScale: { value: 1 },
217
- },
218
- vertexShader: particleVertex,
219
- fragmentShader: /* glsl */`
220
- precision highp float;
221
- in vec3 vViewCenter;
222
- in float vRadius;
223
- in float vImpact;
224
- in float vRetained;
225
- in float vAlive;
226
- out vec4 outColor;
227
- void main() {
228
- vec2 circle = gl_PointCoord * 2.0 - 1.0;
229
- float radiusSq = dot(circle, circle);
230
- if (radiusSq > 1.0 || vAlive < 0.5) discard;
231
- float chord = sqrt(max(0.0, 1.0 - radiusSq)) * vRadius * 2.0;
232
- float softEdge = smoothstep(1.0, 0.72, radiusSq);
233
- outColor = vec4(
234
- chord * softEdge,
235
- chord * max(vImpact, 0.0) * softEdge,
236
- chord * vRetained * softEdge,
237
- softEdge
238
- );
239
- }
240
- `,
241
- transparent: true,
242
- depthTest: false,
243
- depthWrite: false,
244
- blending: THREE.AdditiveBlending,
245
- toneMapped: false,
246
- });
247
-
248
- this.particleScene = new THREE.Scene();
249
- this.depthPoints = new THREE.Points(this.lookupGeometry, this.depthMaterial);
250
- this.depthPoints.frustumCulled = false;
251
- this.particleScene.add(this.depthPoints);
252
-
253
- this.sprayMaterial = new THREE.ShaderMaterial({
254
- glslVersion: THREE.GLSL3,
255
- uniforms: {
256
- uPosition: { value: simulation.positionTexture },
257
- uVelocity: { value: simulation.velocityTexture },
258
- uPixelRatio: { value: 1 },
259
- uAirFraction: { value: this.airFraction },
260
- },
261
- vertexShader: /* glsl */`
262
- precision highp float;
263
- uniform sampler2D uPosition;
264
- uniform sampler2D uVelocity;
265
- uniform float uPixelRatio;
266
- in vec2 aLookup;
267
- out float vImpact;
268
- void main() {
269
- vec4 particle = texture(uPosition, aLookup);
270
- vec4 velocity = texture(uVelocity, aLookup);
271
- vImpact = velocity.w;
272
- vec4 mv = viewMatrix * vec4(particle.xyz, 1.0);
273
- gl_Position = projectionMatrix * mv;
274
- gl_PointSize = min(6.5, (1.2 + velocity.w * 3.4) * uPixelRatio * (5.5 / max(2.0, -mv.z)));
275
- if (particle.w < 0.0 || velocity.w < 0.045) gl_Position = vec4(2.0, 2.0, 2.0, 1.0);
276
- }
277
- `,
278
- fragmentShader: /* glsl */`
279
- precision highp float;
280
- uniform float uAirFraction;
281
- in float vImpact;
282
- out vec4 outColor;
283
- void main() {
284
- vec2 p = gl_PointCoord - 0.5;
285
- float d = length(p);
286
- if (d > 0.5) discard;
287
- float glint = 1.0 - smoothstep(0.02, 0.15, distance(gl_PointCoord, vec2(0.34, 0.3)));
288
- float edge = 1.0 - smoothstep(0.34, 0.5, d);
289
- vec3 color = mix(vec3(0.18, 0.68, 0.78), vec3(0.86, 1.0, 1.0), glint);
290
- outColor = vec4(color, edge * vImpact * (0.12 + uAirFraction * 0.34));
291
- }
292
- `,
293
- transparent: true,
294
- depthTest: false,
295
- depthWrite: false,
296
- blending: THREE.AdditiveBlending,
297
- toneMapped: false,
298
- });
299
- this.sprayScene = new THREE.Scene();
300
- this.sprayPoints = new THREE.Points(this.lookupGeometry, this.sprayMaterial);
301
- this.sprayPoints.frustumCulled = false;
302
- this.sprayScene.add(this.sprayPoints);
303
-
304
- this.depthBlurMaterial = new THREE.ShaderMaterial({
305
- glslVersion: THREE.GLSL3,
306
- uniforms: {
307
- uInput: { value: this.depthTarget.texture },
308
- uTexel: { value: new THREE.Vector2() },
309
- uDirection: { value: new THREE.Vector2(1, 0) },
310
- uRadius: { value: 5 },
311
- uRetainedOnly: { value: 0 },
312
- },
313
- vertexShader: fullscreenVertex,
314
- fragmentShader: /* glsl */`
315
- precision highp float;
316
- uniform sampler2D uInput;
317
- uniform vec2 uTexel;
318
- uniform vec2 uDirection;
319
- uniform int uRadius;
320
- uniform float uRetainedOnly;
321
- in vec2 vUv;
322
- out vec4 outColor;
323
-
324
- void main() {
325
- vec4 centerSample = texture(uInput, vUv);
326
- if (uRetainedOnly > 0.5 && centerSample.b < 0.35) {
327
- outColor = centerSample;
328
- return;
329
- }
330
- float center = centerSample.r;
331
- if (center <= 0.0) {
332
- float nearest = 1e6;
333
- vec4 nearestSample = vec4(0.0);
334
- for (int i = -5; i <= 5; i++) {
335
- if (abs(i) > uRadius) continue;
336
- vec4 candidate = texture(uInput, vUv + uDirection * uTexel * float(i));
337
- if (candidate.r > 0.0 && candidate.r < nearest) {
338
- nearest = candidate.r;
339
- nearestSample = candidate;
340
- }
341
- }
342
- if (nearest > 1e5) { outColor = vec4(0.0); return; }
343
- center = nearest;
344
- centerSample = nearestSample;
345
- }
346
-
347
- float total = 0.0;
348
- float retainedTotal = 0.0;
349
- float weightSum = 0.0;
350
- float centerRetained = step(0.45, centerSample.b);
351
- for (int i = -5; i <= 5; i++) {
352
- if (abs(i) > uRadius) continue;
353
- vec4 sampleValue = texture(uInput, vUv + uDirection * uTexel * float(i));
354
- if (sampleValue.r <= 0.0) continue;
355
- float retained = max(centerRetained, step(0.45, sampleValue.b));
356
- float gaussian = exp(-float(i * i) * mix(0.11, 0.055, retained));
357
- float bilateral = exp(-abs(sampleValue.r - center) * mix(18.0, 3.2, retained));
358
- float weight = gaussian * bilateral;
359
- total += sampleValue.r * weight;
360
- retainedTotal += sampleValue.b * weight;
361
- weightSum += weight;
362
- }
363
- outColor = vec4(
364
- total / max(weightSum, 0.0001),
365
- 0.0,
366
- retainedTotal / max(weightSum, 0.0001),
367
- 1.0
368
- );
369
- }
370
- `,
371
- depthTest: false,
372
- depthWrite: false,
373
- toneMapped: false,
374
- });
375
-
376
- this.thicknessBlurMaterial = new THREE.ShaderMaterial({
377
- glslVersion: THREE.GLSL3,
378
- uniforms: {
379
- uInput: { value: this.thicknessTarget.texture },
380
- uTexel: { value: new THREE.Vector2() },
381
- uDirection: { value: new THREE.Vector2(1, 0) },
382
- },
383
- vertexShader: fullscreenVertex,
384
- fragmentShader: /* glsl */`
385
- precision highp float;
386
- uniform sampler2D uInput;
387
- uniform vec2 uTexel;
388
- uniform vec2 uDirection;
389
- in vec2 vUv;
390
- out vec4 outColor;
391
- void main() {
392
- vec4 result = texture(uInput, vUv) * 0.227027;
393
- result += texture(uInput, vUv + uDirection * uTexel * 1.384615) * 0.316216;
394
- result += texture(uInput, vUv - uDirection * uTexel * 1.384615) * 0.316216;
395
- result += texture(uInput, vUv + uDirection * uTexel * 3.230769) * 0.070270;
396
- result += texture(uInput, vUv - uDirection * uTexel * 3.230769) * 0.070270;
397
- outColor = result;
398
- }
399
- `,
400
- depthTest: false,
401
- depthWrite: false,
402
- toneMapped: false,
403
- });
404
-
405
- this.compositeMaterial = new THREE.ShaderMaterial({
406
- glslVersion: THREE.GLSL3,
407
- uniforms: {
408
- uSceneColor: { value: this.externalBase ? this.externalBase.colorTexture : this.sceneTarget.texture },
409
- uSceneDepth: { value: this.externalBase ? this.externalBase.depthTexture : this.sceneTarget.depthTexture },
410
- uDepthMode: { value: this.externalBase && this.externalBase.depthMode === 'logarithmic' ? 1.0 : 0.0 },
411
- uFluidDepth: { value: this.blurB.texture },
412
- uThickness: { value: this.thicknessBlurB.texture },
413
- uResolution: { value: new THREE.Vector2(width, height) },
414
- uInverseProjection: { value: camera.projectionMatrixInverse },
415
- uCameraNear: { value: camera.near },
416
- uCameraFar: { value: camera.far },
417
- uLightDirection: { value: new THREE.Vector3(-0.2, 0.7, 0.6).normalize() },
418
- uAirFraction: { value: this.airFraction },
419
- },
420
- vertexShader: fullscreenVertex,
421
- fragmentShader: /* glsl */`
422
- precision highp float;
423
- uniform sampler2D uSceneColor;
424
- uniform sampler2D uSceneDepth;
425
- uniform sampler2D uFluidDepth;
426
- uniform sampler2D uThickness;
427
- uniform vec2 uResolution;
428
- uniform mat4 uInverseProjection;
429
- uniform float uCameraNear;
430
- uniform float uCameraFar;
431
- uniform vec3 uLightDirection;
432
- uniform float uAirFraction;
433
- in vec2 vUv;
434
- out vec4 outColor;
435
-
436
- uniform float uDepthMode;
437
- float sceneDepthToViewDistance(float depth) {
438
- if (uDepthMode > 0.5) {
439
- // 对数深度(three-base logarithmicDepthBuffer):与 frosted_glass_pass.ts:146 对齐
440
- return pow(uCameraFar + 1.0, depth) - 1.0;
441
- }
442
- float viewZ = (uCameraNear * uCameraFar) / ((uCameraFar - uCameraNear) * depth - uCameraFar);
443
- return -viewZ;
444
- }
445
-
446
- vec3 viewPosition(vec2 uv, float distanceToCamera) {
447
- vec4 ray = uInverseProjection * vec4(uv * 2.0 - 1.0, 1.0, 1.0);
448
- vec3 direction = ray.xyz / ray.w;
449
- return direction * (-distanceToCamera / direction.z);
450
- }
451
-
452
- void main() {
453
- vec3 sceneColor = texture(uSceneColor, vUv).rgb;
454
- float depth = texture(uFluidDepth, vUv).r;
455
- if (depth <= 0.0) { outColor = vec4(sceneColor, 1.0); return; }
456
-
457
- float sceneDistance = sceneDepthToViewDistance(texture(uSceneDepth, vUv).r);
458
- if (depth > sceneDistance + 0.018) { outColor = vec4(sceneColor, 1.0); return; }
459
-
460
- vec4 thicknessData = texture(uThickness, vUv);
461
- float retainedPreview = clamp(
462
- thicknessData.b / max(thicknessData.r, 0.0001),
463
- 0.0,
464
- 1.0
465
- );
466
- vec2 texel = 1.0 / uResolution;
467
- float normalSpan = mix(1.0, 2.35, retainedPreview);
468
- float dL = texture(uFluidDepth, vUv - vec2(texel.x * normalSpan, 0.0)).r;
469
- float dR = texture(uFluidDepth, vUv + vec2(texel.x * normalSpan, 0.0)).r;
470
- float dD = texture(uFluidDepth, vUv - vec2(0.0, texel.y * normalSpan)).r;
471
- float dU = texture(uFluidDepth, vUv + vec2(0.0, texel.y * normalSpan)).r;
472
- dL = dL <= 0.0 ? depth : dL;
473
- dR = dR <= 0.0 ? depth : dR;
474
- dD = dD <= 0.0 ? depth : dD;
475
- dU = dU <= 0.0 ? depth : dU;
476
-
477
- vec3 pL = viewPosition(vUv - vec2(texel.x * normalSpan, 0.0), dL);
478
- vec3 pR = viewPosition(vUv + vec2(texel.x * normalSpan, 0.0), dR);
479
- vec3 pD = viewPosition(vUv - vec2(0.0, texel.y * normalSpan), dD);
480
- vec3 pU = viewPosition(vUv + vec2(0.0, texel.y * normalSpan), dU);
481
- vec3 normal = normalize(cross(pR - pL, pU - pD));
482
- if (normal.z < 0.0) normal = -normal;
483
-
484
- float thickness = max(0.0, thicknessData.r * 1.28);
485
- float impactFoam = thicknessData.g / max(thicknessData.r, 0.0001);
486
- float retainedPhase = clamp(thicknessData.b / max(thicknessData.r, 0.0001), 0.0, 1.0);
487
- float curvature = abs(dL + dR + dD + dU - 4.0 * depth) / max(depth, 0.1);
488
- float foam = smoothstep(0.22, 0.8, impactFoam) * (0.08 + uAirFraction * 0.5)
489
- + smoothstep(0.004, 0.02, curvature) * uAirFraction * 0.2
490
- * mix(1.0, 0.1, retainedPhase);
491
- foam = clamp(foam, 0.0, 0.55);
492
-
493
- vec2 refractOffset = normal.xy * (0.004 + min(thickness, 0.8) * 0.012) * vec2(1.0, -1.0);
494
- vec2 refractUv = clamp(vUv + refractOffset, vec2(0.002), vec2(0.998));
495
- vec3 refracted = texture(uSceneColor, refractUv).rgb;
496
- vec3 absorption = vec3(0.09, 0.032, 0.017);
497
- vec3 transmission = exp(-absorption * thickness * mix(3.8, 2.45, retainedPhase));
498
- vec3 waterTint = vec3(0.01, 0.145, 0.18);
499
- vec3 body = refracted * transmission + waterTint * (1.0 - transmission);
500
-
501
- vec3 viewDir = normalize(-viewPosition(vUv, depth));
502
- float fresnel = 0.0204 + 0.9796 * pow(1.0 - max(dot(normal, viewDir), 0.0), 5.0);
503
- vec3 lightDir = normalize(uLightDirection);
504
- vec3 halfDir = normalize(lightDir + viewDir);
505
- float specular = pow(max(dot(normal, halfDir), 0.0), 132.0);
506
- vec3 reflection = mix(vec3(0.03, 0.09, 0.11), vec3(0.34, 0.56, 0.62), max(normal.y * 0.5 + 0.5, fresnel));
507
- vec3 waterColor = mix(body, reflection, fresnel * 0.5);
508
- waterColor += vec3(0.84, 0.98, 1.0) * specular * 1.1;
509
- waterColor = mix(waterColor, vec3(0.72, 0.94, 0.96), foam);
510
-
511
- float coverage = smoothstep(0.002, 0.06, thickness);
512
- float fluidOpacity = mix(0.76, 0.59, retainedPhase) + fresnel * 0.22;
513
- vec3 result = mix(sceneColor, waterColor, coverage * fluidOpacity);
514
- outColor = vec4(result, 1.0);
515
- }
516
- `,
517
- depthTest: false,
518
- depthWrite: false,
519
- toneMapped: false,
520
- });
521
-
522
- this.copyMaterial = new THREE.ShaderMaterial({
523
- glslVersion: THREE.GLSL3,
524
- uniforms: { tDiffuse: { value: null } },
525
- vertexShader: fullscreenVertex,
526
- fragmentShader: /* glsl */`
527
- precision highp float;
528
- uniform sampler2D tDiffuse;
529
- in vec2 vUv;
530
- out vec4 outColor;
531
- void main() { outColor = texture(tDiffuse, vUv); }
532
- `,
533
- depthTest: false,
534
- depthWrite: false,
535
- toneMapped: false,
536
- });
537
- // 函数式 renderInto 的中间合成目标:composite 先写到它,再拷回 outputTarget,
538
- // 避免「读当前目标写当前目标」的反馈未定义行为(与 frosted_glass_pass 同构)。
539
- // 与 outputTarget 同分辨率(全分辨率),保证合成回屏幕时不丢清晰度(水本身是软的,但背景不能糊)。
540
- this.compositeTarget = createFluidTarget(width, height, false);
541
-
542
- this.fsQuad = new FullScreenQuad(this.compositeMaterial);
543
- this.setSize(width, height);
544
- }
545
-
546
- setAirFraction(value: number): void {
547
- if (!this.supported) return;
548
- this.airFraction = value;
549
- this.compositeMaterial.uniforms.uAirFraction.value = value;
550
- this.sprayMaterial.uniforms.uAirFraction.value = value;
551
- }
552
-
553
- setParticleRadius(value: number): void {
554
- if (!this.supported) return;
555
- this.depthMaterial.uniforms.uParticleRadius.value = value;
556
- this.thicknessMaterial.uniforms.uParticleRadius.value = value;
557
- }
558
-
559
- setRetainedFluidVisible(visible: boolean): void {
560
- if (!this.supported) return;
561
- const value = visible ? 1 : 0;
562
- this.depthMaterial.uniforms.uShowRetained.value = value;
563
- this.thicknessMaterial.uniforms.uShowRetained.value = value;
564
- }
565
-
566
- setQuality({ scale, activeCount, blurRadius }: FluidQuality): void {
567
- if (!this.supported) return;
568
- this.scale = scale;
569
- this.activeCount = Math.min(activeCount, this.simulation.count);
570
- this.lookupGeometry.setDrawRange(0, this.activeCount);
571
- const retainedRadiusScale = 1.58 * Math.cbrt(40000 / Math.max(this.activeCount, 1));
572
- this.depthMaterial.uniforms.uRetainedRadiusScale.value = retainedRadiusScale;
573
- this.thicknessMaterial.uniforms.uRetainedRadiusScale.value = retainedRadiusScale;
574
- this.depthBlurMaterial.uniforms.uRadius.value = blurRadius;
575
- this.setSize(this.width, this.height);
576
- }
577
-
578
- /** three-base 等外部调用方在 resize / 切换 baseRT 时刷新注入的基础纹理。 */
579
- setBaseTextures({ colorTexture, depthTexture, depthMode }: PartialBaseTextures = {}): void {
580
- if (!this.supported || !this.externalBase) return;
581
- if (colorTexture) {
582
- this.externalBase.colorTexture = colorTexture;
583
- this.compositeMaterial.uniforms.uSceneColor.value = colorTexture;
584
- }
585
- if (depthTexture) {
586
- this.externalBase.depthTexture = depthTexture;
587
- this.compositeMaterial.uniforms.uSceneDepth.value = depthTexture;
588
- }
589
- if (depthMode) {
590
- this.externalBase.depthMode = depthMode === 'logarithmic' ? 'logarithmic' : 'standard';
591
- this.compositeMaterial.uniforms.uDepthMode.value = this.externalBase.depthMode === 'logarithmic' ? 1.0 : 0.0;
592
- }
593
- }
594
-
595
- setSize(width: number, height: number): void {
596
- if (!this.supported) return;
597
- this.width = Math.max(1, Math.floor(width));
598
- this.height = Math.max(1, Math.floor(height));
599
- const fluidWidth = Math.max(1, Math.ceil(this.width * this.scale));
600
- const fluidHeight = Math.max(1, Math.ceil(this.height * this.scale));
601
- this.sceneTarget?.setSize(this.width, this.height);
602
- this.depthTarget.setSize(fluidWidth, fluidHeight);
603
- this.thicknessTarget.setSize(fluidWidth, fluidHeight);
604
- this.blurA.setSize(fluidWidth, fluidHeight);
605
- this.blurB.setSize(fluidWidth, fluidHeight);
606
- this.thicknessBlurA.setSize(fluidWidth, fluidHeight);
607
- this.thicknessBlurB.setSize(fluidWidth, fluidHeight);
608
- this.compositeTarget.setSize(this.width, this.height);
609
- this.depthBlurMaterial.uniforms.uTexel.value.set(1 / fluidWidth, 1 / fluidHeight);
610
- this.thicknessBlurMaterial.uniforms.uTexel.value.set(1 / fluidWidth, 1 / fluidHeight);
611
- this.compositeMaterial.uniforms.uResolution.value.set(fluidWidth, fluidHeight);
612
- const pointScale = fluidHeight / Math.tan(THREE.MathUtils.degToRad(this.camera.fov * 0.5));
613
- this.depthMaterial.uniforms.uPointScale.value = pointScale;
614
- this.thicknessMaterial.uniforms.uPointScale.value = pointScale;
615
- this.sprayMaterial.uniforms.uPixelRatio.value = Math.min(window.devicePixelRatio, 2);
616
- }
617
-
618
- /** 渲染粒子深度/厚度 + 两轮分离模糊 + 厚度模糊(不输出合成、不渲染 spray)。render/renderInto 共用。 */
619
- renderFluidTextures(renderer: THREE.WebGLRenderer, camera?: THREE.Camera): void {
620
- if (!this.supported) return;
621
- const cam = camera || this.camera;
622
- this.depthMaterial.uniforms.uPosition.value = this.simulation.positionTexture;
623
- this.depthMaterial.uniforms.uVelocity.value = this.simulation.velocityTexture;
624
- this.thicknessMaterial.uniforms.uPosition.value = this.simulation.positionTexture;
625
- this.thicknessMaterial.uniforms.uVelocity.value = this.simulation.velocityTexture;
626
- this.sprayMaterial.uniforms.uPosition.value = this.simulation.positionTexture;
627
- this.sprayMaterial.uniforms.uVelocity.value = this.simulation.velocityTexture;
628
- this.compositeMaterial.uniforms.uInverseProjection.value.copy(cam.projectionMatrixInverse);
629
- this.depthMaterial.uniforms.uProjectionMatrix.value.copy(cam.projectionMatrix);
630
-
631
- renderer.setClearColor(0x000000, 0);
632
- this.depthPoints.material = this.depthMaterial;
633
- renderer.setRenderTarget(this.depthTarget);
634
- renderer.clear(true, true, false);
635
- renderer.render(this.particleScene, cam);
636
-
637
- this.depthPoints.material = this.thicknessMaterial;
638
- renderer.setRenderTarget(this.thicknessTarget);
639
- renderer.clear(true, false, false);
640
- renderer.render(this.particleScene, cam);
641
-
642
- this.fsQuad.material = this.depthBlurMaterial;
643
- this.depthBlurMaterial.uniforms.uInput.value = this.depthTarget.texture;
644
- this.depthBlurMaterial.uniforms.uRetainedOnly.value = 0;
645
- this.depthBlurMaterial.uniforms.uDirection.value.set(1, 0);
646
- renderer.setRenderTarget(this.blurA);
647
- renderer.clear();
648
- this.fsQuad.render(renderer);
649
- this.depthBlurMaterial.uniforms.uInput.value = this.blurA.texture;
650
- this.depthBlurMaterial.uniforms.uDirection.value.set(0, 1);
651
- renderer.setRenderTarget(this.blurB);
652
- renderer.clear();
653
- this.fsQuad.render(renderer);
654
-
655
- // A second phase-aware relaxation removes the individual SPH sphere imprint
656
- // from stored water without softening the narrow free jet.
657
- this.depthBlurMaterial.uniforms.uRetainedOnly.value = 1;
658
- this.depthBlurMaterial.uniforms.uInput.value = this.blurB.texture;
659
- this.depthBlurMaterial.uniforms.uDirection.value.set(1, 0);
660
- renderer.setRenderTarget(this.blurA);
661
- renderer.clear();
662
- this.fsQuad.render(renderer);
663
- this.depthBlurMaterial.uniforms.uInput.value = this.blurA.texture;
664
- this.depthBlurMaterial.uniforms.uDirection.value.set(0, 1);
665
- renderer.setRenderTarget(this.blurB);
666
- renderer.clear();
667
- this.fsQuad.render(renderer);
668
- this.depthBlurMaterial.uniforms.uRetainedOnly.value = 0;
669
-
670
- this.fsQuad.material = this.thicknessBlurMaterial;
671
- this.thicknessBlurMaterial.uniforms.uInput.value = this.thicknessTarget.texture;
672
- this.thicknessBlurMaterial.uniforms.uDirection.value.set(1, 0);
673
- renderer.setRenderTarget(this.thicknessBlurA);
674
- renderer.clear();
675
- this.fsQuad.render(renderer);
676
- this.thicknessBlurMaterial.uniforms.uInput.value = this.thicknessBlurA.texture;
677
- this.thicknessBlurMaterial.uniforms.uDirection.value.set(0, 1);
678
- renderer.setRenderTarget(this.thicknessBlurB);
679
- renderer.clear();
680
- this.fsQuad.render(renderer);
681
- }
682
-
683
- /** EffectComposer 链入口(自渲染模式 / base 纹理注入模式通用,保持原行为不变)。 */
684
- render(renderer: THREE.WebGLRenderer, writeBuffer: THREE.WebGLRenderTarget): void {
685
- if (!this.supported) return;
686
- const previousTarget = renderer.getRenderTarget();
687
- const previousColor = renderer.getClearColor(new THREE.Color());
688
- const previousAlpha = renderer.getClearAlpha();
689
- const previousAutoClear = renderer.autoClear;
690
-
691
- if (!this.externalBase) {
692
- renderer.autoClear = true;
693
- renderer.setClearColor(0x061017, 1);
694
- renderer.setRenderTarget(this.sceneTarget);
695
- renderer.clear(true, true, true);
696
- renderer.render(this.scene, this.camera);
697
- }
698
-
699
- this.renderFluidTextures(renderer, this.camera);
700
-
701
- this.fsQuad.material = this.compositeMaterial;
702
- this.compositeMaterial.uniforms.uFluidDepth.value = this.blurB.texture;
703
- this.compositeMaterial.uniforms.uThickness.value = this.thicknessBlurB.texture;
704
- renderer.setRenderTarget(this.renderToScreen ? null : writeBuffer);
705
- renderer.clear();
706
- this.fsQuad.render(renderer);
707
-
708
- renderer.autoClear = false;
709
- renderer.render(this.sprayScene, this.camera);
710
- renderer.autoClear = previousAutoClear;
711
- renderer.setRenderTarget(previousTarget);
712
- renderer.setClearColor(previousColor, previousAlpha);
713
- }
714
-
715
- /**
716
- * 函数式入口:直接消费调用方已有的基础颜色/深度纹理,把水面合成到指定目标。
717
- * 与 three-base 的手写 fsQuad 后处理链(frosted_glass_pass 等)形态一致:
718
- * 不创建/自渲染主场景,composite 先写到内部 compositeTarget,再拷回 outputTarget,
719
- * 避免「读 baseRenderTarget 写 baseRenderTarget」的反馈未定义行为。
720
- * @param {THREE.WebGLRenderer} renderer
721
- * @param {{colorTexture:THREE.Texture,depthTexture:THREE.Texture,depthMode?:'standard'|'logarithmic',outputTarget:THREE.WebGLRenderTarget,camera:THREE.Camera}} opts
722
- */
723
- renderInto(renderer: THREE.WebGLRenderer, opts: WaterFlowRenderOptions): void {
724
- if (!this.supported || !opts) return;
725
- const {
726
- colorTexture,
727
- depthTexture,
728
- depthMode = 'logarithmic',
729
- outputTarget,
730
- camera,
731
- } = opts;
732
-
733
- if (colorTexture) {
734
- this.externalBase = { colorTexture, depthTexture, depthMode };
735
- this.compositeMaterial.uniforms.uSceneColor.value = colorTexture;
736
- this.compositeMaterial.uniforms.uSceneDepth.value = depthTexture;
737
- this.compositeMaterial.uniforms.uDepthMode.value = depthMode === 'logarithmic' ? 1.0 : 0.0;
738
- }
739
-
740
- const previousTarget = renderer.getRenderTarget();
741
- const previousColor = renderer.getClearColor(new THREE.Color());
742
- const previousAlpha = renderer.getClearAlpha();
743
- const previousAutoClear = renderer.autoClear;
744
-
745
- this.renderFluidTextures(renderer, camera);
746
-
747
- // composite → 独立 compositeTarget(读 base 写 compositeTarget,无 feedback)
748
- this.fsQuad.material = this.compositeMaterial;
749
- this.compositeMaterial.uniforms.uFluidDepth.value = this.blurB.texture;
750
- this.compositeMaterial.uniforms.uThickness.value = this.thicknessBlurB.texture;
751
- renderer.setRenderTarget(this.compositeTarget);
752
- renderer.clear();
753
- this.fsQuad.render(renderer);
754
-
755
- // 拷贝 compositeTarget → outputTarget(如 baseRenderTarget)
756
- this.fsQuad.material = this.copyMaterial;
757
- this.copyMaterial.uniforms.tDiffuse.value = this.compositeTarget.texture;
758
- renderer.setRenderTarget(outputTarget);
759
- renderer.clear();
760
- this.fsQuad.render(renderer);
761
-
762
- // spray 飞沫叠加到 outputTarget
763
- renderer.autoClear = false;
764
- renderer.render(this.sprayScene, camera);
765
- renderer.autoClear = previousAutoClear;
766
-
767
- renderer.setRenderTarget(previousTarget);
768
- renderer.setClearColor(previousColor, previousAlpha);
769
- }
770
-
771
- dispose(): void {
772
- if (!this.supported) return;
773
- this.sceneTarget?.dispose();
774
- this.depthTarget.dispose();
775
- this.thicknessTarget.dispose();
776
- this.blurA.dispose();
777
- this.blurB.dispose();
778
- this.thicknessBlurA.dispose();
779
- this.thicknessBlurB.dispose();
780
- this.lookupGeometry.dispose();
781
- this.depthMaterial.dispose();
782
- this.thicknessMaterial.dispose();
783
- this.sprayMaterial.dispose();
784
- this.depthBlurMaterial.dispose();
785
- this.thicknessBlurMaterial.dispose();
786
- this.compositeMaterial.dispose();
787
- this.fsQuad.dispose();
788
- }
789
- }
1
+ import * as THREE from 'three';
2
+ import { Pass, FullScreenQuad } from 'three/examples/jsm/postprocessing/Pass.js';
3
+ import type {
4
+ ExternalBaseTextures,
5
+ FluidQuality,
6
+ PartialBaseTextures,
7
+ ScreenSpaceFluidPassOptions,
8
+ ScreenSpaceFluidSource,
9
+ WaterFlowRenderOptions,
10
+ } from './types';
11
+
12
+ const fullscreenVertex = /* glsl */`
13
+ out vec2 vUv;
14
+ void main() {
15
+ vUv = uv;
16
+ gl_Position = vec4(position, 1.0);
17
+ }
18
+ `;
19
+
20
+ function createColorDepthTarget(width: number, height: number): THREE.WebGLRenderTarget {
21
+ const target = new THREE.WebGLRenderTarget(width, height, {
22
+ type: THREE.HalfFloatType,
23
+ format: THREE.RGBAFormat,
24
+ minFilter: THREE.LinearFilter,
25
+ magFilter: THREE.LinearFilter,
26
+ depthBuffer: true,
27
+ });
28
+ target.depthTexture = new THREE.DepthTexture(width, height, THREE.UnsignedIntType);
29
+ target.depthTexture.minFilter = THREE.NearestFilter;
30
+ target.depthTexture.magFilter = THREE.NearestFilter;
31
+ return target;
32
+ }
33
+
34
+ function createFluidTarget(width: number, height: number, depthBuffer = false): THREE.WebGLRenderTarget {
35
+ return new THREE.WebGLRenderTarget(width, height, {
36
+ type: THREE.HalfFloatType,
37
+ format: THREE.RGBAFormat,
38
+ minFilter: THREE.LinearFilter,
39
+ magFilter: THREE.LinearFilter,
40
+ wrapS: THREE.ClampToEdgeWrapping,
41
+ wrapT: THREE.ClampToEdgeWrapping,
42
+ depthBuffer,
43
+ stencilBuffer: false,
44
+ });
45
+ }
46
+
47
+ function createLookupGeometry(size: number): THREE.BufferGeometry {
48
+ const count = size * size;
49
+ const positions = new Float32Array(count * 3);
50
+ const lookup = new Float32Array(count * 2);
51
+ for (let y = 0; y < size; y++) {
52
+ for (let x = 0; x < size; x++) {
53
+ const i = y * size + x;
54
+ lookup[i * 2] = (x + 0.5) / size;
55
+ lookup[i * 2 + 1] = (y + 0.5) / size;
56
+ }
57
+ }
58
+ const geometry = new THREE.BufferGeometry();
59
+ geometry.setAttribute('position', new THREE.BufferAttribute(positions, 3));
60
+ geometry.setAttribute('aLookup', new THREE.BufferAttribute(lookup, 2));
61
+ return geometry;
62
+ }
63
+
64
+ const particleVertex = /* glsl */`
65
+ precision highp float;
66
+ uniform sampler2D uPosition;
67
+ uniform sampler2D uVelocity;
68
+ uniform float uParticleRadius;
69
+ uniform float uRetainedRadiusScale;
70
+ uniform float uShowRetained;
71
+ uniform float uPointScale;
72
+ in vec2 aLookup;
73
+ out vec3 vViewCenter;
74
+ out float vRadius;
75
+ out float vImpact;
76
+ out float vRetained;
77
+ out float vAlive;
78
+
79
+ void main() {
80
+ vec4 particle = texture(uPosition, aLookup);
81
+ vec4 velocity = texture(uVelocity, aLookup);
82
+ float retained = 1.0 - step(-0.5, velocity.w);
83
+ vRetained = retained;
84
+ vAlive = step(0.0, particle.w) * mix(1.0, uShowRetained, retained);
85
+ vImpact = velocity.w;
86
+ float jetRadius = mix(uParticleRadius, uParticleRadius * 0.56, smoothstep(0.05, 0.7, velocity.w));
87
+ vRadius = mix(jetRadius, uParticleRadius * uRetainedRadiusScale, retained);
88
+ vec4 mv = viewMatrix * vec4(particle.xyz, 1.0);
89
+ vViewCenter = mv.xyz;
90
+ gl_Position = projectionMatrix * mv;
91
+ gl_PointSize = vAlive * max(0.0, vRadius * uPointScale / max(0.2, -mv.z));
92
+ if (vAlive < 0.5) gl_Position = vec4(2.0, 2.0, 2.0, 1.0);
93
+ }
94
+ `;
95
+
96
+ export class ScreenSpaceFluidPass extends Pass {
97
+ /**
98
+ * 字段集中声明。
99
+ *
100
+ * 构造器在 `if (!this.supported) return;` 处提前返回,因此下面标注 `T | null` 的这批资源字段
101
+ * 在「不支持 WebGL2 / 浮点纹理」的空壳实例上**根本没被赋值**(运行时是 undefined,不是 null)。
102
+ * 一律建模为 `T | null` 而不是用 `!` 硬压,且所有读取点都在 `if (!this.supported) return;`
103
+ * 守卫之后(setSize/dispose 里原有的 `?.` 也照留),语义与原实现逐字节一致。
104
+ */
105
+ private readonly scene: THREE.Scene;
106
+ /** setSize 要读 camera.fov 算 pointScale,故按透视相机建模。 */
107
+ private readonly camera: THREE.PerspectiveCamera;
108
+ private readonly simulation: ScreenSpaceFluidSource;
109
+ private readonly supported: boolean;
110
+ private scale: number;
111
+ private airFraction: number;
112
+ private activeCount: number;
113
+ /** 外部注入的基础纹理;只在 renderInto 里被赋值,此前为 undefined,setBaseTextures 的 falsy 守卫会直接返回。 */
114
+ private externalBase: ExternalBaseTextures | null;
115
+ /** 由 setSize 写入(构造器 supported 分支末尾调用过),空壳实例上为 undefined。 */
116
+ private width: number | null;
117
+ private height: number | null;
118
+ private readonly sceneTarget: THREE.WebGLRenderTarget | null;
119
+ private readonly depthTarget: THREE.WebGLRenderTarget | null;
120
+ private readonly thicknessTarget: THREE.WebGLRenderTarget | null;
121
+ private readonly blurA: THREE.WebGLRenderTarget | null;
122
+ private readonly blurB: THREE.WebGLRenderTarget | null;
123
+ private readonly thicknessBlurA: THREE.WebGLRenderTarget | null;
124
+ private readonly thicknessBlurB: THREE.WebGLRenderTarget | null;
125
+ private readonly compositeTarget: THREE.WebGLRenderTarget | null;
126
+ private readonly lookupGeometry: THREE.BufferGeometry | null;
127
+ private readonly depthMaterial: THREE.ShaderMaterial | null;
128
+ private readonly thicknessMaterial: THREE.ShaderMaterial | null;
129
+ private readonly sprayMaterial: THREE.ShaderMaterial | null;
130
+ private readonly depthBlurMaterial: THREE.ShaderMaterial | null;
131
+ private readonly thicknessBlurMaterial: THREE.ShaderMaterial | null;
132
+ private readonly compositeMaterial: THREE.ShaderMaterial | null;
133
+ private readonly copyMaterial: THREE.ShaderMaterial | null;
134
+ private readonly particleScene: THREE.Scene | null;
135
+ private readonly sprayScene: THREE.Scene | null;
136
+ private readonly depthPoints: THREE.Points<THREE.BufferGeometry, THREE.ShaderMaterial> | null;
137
+ private readonly sprayPoints: THREE.Points<THREE.BufferGeometry, THREE.ShaderMaterial> | null;
138
+ private readonly fsQuad: FullScreenQuad | null;
139
+
140
+ constructor(
141
+ scene: THREE.Scene,
142
+ camera: THREE.PerspectiveCamera,
143
+ simulation: ScreenSpaceFluidSource,
144
+ { width = 1, height = 1, scale = 0.75, activeCount = simulation.count }: ScreenSpaceFluidPassOptions = {},
145
+ ) {
146
+ super();
147
+ this.scene = scene;
148
+ this.camera = camera;
149
+ this.simulation = simulation;
150
+ this.scale = scale;
151
+ this.airFraction = 0.18;
152
+ this.activeCount = Math.min(activeCount, simulation.count);
153
+ this.supported = simulation.supported;
154
+ this.needsSwap = true;
155
+
156
+ if (!this.supported) return;
157
+
158
+ this.sceneTarget = createColorDepthTarget(width, height);
159
+ this.depthTarget = createFluidTarget(Math.ceil(width * scale), Math.ceil(height * scale), true);
160
+ this.thicknessTarget = createFluidTarget(Math.ceil(width * scale), Math.ceil(height * scale));
161
+ this.blurA = createFluidTarget(Math.ceil(width * scale), Math.ceil(height * scale));
162
+ this.blurB = createFluidTarget(Math.ceil(width * scale), Math.ceil(height * scale));
163
+ this.thicknessBlurA = createFluidTarget(Math.ceil(width * scale), Math.ceil(height * scale));
164
+ this.thicknessBlurB = createFluidTarget(Math.ceil(width * scale), Math.ceil(height * scale));
165
+
166
+ this.lookupGeometry = createLookupGeometry(simulation.size);
167
+ this.lookupGeometry.setDrawRange(0, this.activeCount);
168
+
169
+ this.depthMaterial = new THREE.ShaderMaterial({
170
+ glslVersion: THREE.GLSL3,
171
+ uniforms: {
172
+ uPosition: { value: simulation.positionTexture },
173
+ uVelocity: { value: simulation.velocityTexture },
174
+ uParticleRadius: { value: 0.037 },
175
+ uRetainedRadiusScale: { value: 1.55 },
176
+ uShowRetained: { value: 0 },
177
+ uPointScale: { value: 1 },
178
+ uProjectionMatrix: { value: camera.projectionMatrix.clone() },
179
+ },
180
+ vertexShader: particleVertex,
181
+ fragmentShader: /* glsl */`
182
+ precision highp float;
183
+ uniform mat4 uProjectionMatrix;
184
+ in vec3 vViewCenter;
185
+ in float vRadius;
186
+ in float vImpact;
187
+ in float vRetained;
188
+ in float vAlive;
189
+ out vec4 outColor;
190
+ void main() {
191
+ vec2 circle = gl_PointCoord * 2.0 - 1.0;
192
+ float radiusSq = dot(circle, circle);
193
+ if (radiusSq > 1.0 || vAlive < 0.5) discard;
194
+ float sphereZ = sqrt(max(0.0, 1.0 - radiusSq));
195
+ vec3 surfacePosition = vViewCenter + vec3(circle * vRadius, sphereZ * vRadius);
196
+ vec4 clip = uProjectionMatrix * vec4(surfacePosition, 1.0);
197
+ float ndcDepth = clip.z / clip.w;
198
+ gl_FragDepth = ndcDepth * 0.5 + 0.5;
199
+ outColor = vec4(-surfacePosition.z, max(vImpact, 0.0), vRetained, 1.0);
200
+ }
201
+ `,
202
+ depthTest: true,
203
+ depthWrite: true,
204
+ blending: THREE.NoBlending,
205
+ toneMapped: false,
206
+ });
207
+
208
+ this.thicknessMaterial = new THREE.ShaderMaterial({
209
+ glslVersion: THREE.GLSL3,
210
+ uniforms: {
211
+ uPosition: { value: simulation.positionTexture },
212
+ uVelocity: { value: simulation.velocityTexture },
213
+ uParticleRadius: { value: 0.037 },
214
+ uRetainedRadiusScale: { value: 1.55 },
215
+ uShowRetained: { value: 0 },
216
+ uPointScale: { value: 1 },
217
+ },
218
+ vertexShader: particleVertex,
219
+ fragmentShader: /* glsl */`
220
+ precision highp float;
221
+ in vec3 vViewCenter;
222
+ in float vRadius;
223
+ in float vImpact;
224
+ in float vRetained;
225
+ in float vAlive;
226
+ out vec4 outColor;
227
+ void main() {
228
+ vec2 circle = gl_PointCoord * 2.0 - 1.0;
229
+ float radiusSq = dot(circle, circle);
230
+ if (radiusSq > 1.0 || vAlive < 0.5) discard;
231
+ float chord = sqrt(max(0.0, 1.0 - radiusSq)) * vRadius * 2.0;
232
+ float softEdge = smoothstep(1.0, 0.72, radiusSq);
233
+ outColor = vec4(
234
+ chord * softEdge,
235
+ chord * max(vImpact, 0.0) * softEdge,
236
+ chord * vRetained * softEdge,
237
+ softEdge
238
+ );
239
+ }
240
+ `,
241
+ transparent: true,
242
+ depthTest: false,
243
+ depthWrite: false,
244
+ blending: THREE.AdditiveBlending,
245
+ toneMapped: false,
246
+ });
247
+
248
+ this.particleScene = new THREE.Scene();
249
+ this.depthPoints = new THREE.Points(this.lookupGeometry, this.depthMaterial);
250
+ this.depthPoints.frustumCulled = false;
251
+ this.particleScene.add(this.depthPoints);
252
+
253
+ this.sprayMaterial = new THREE.ShaderMaterial({
254
+ glslVersion: THREE.GLSL3,
255
+ uniforms: {
256
+ uPosition: { value: simulation.positionTexture },
257
+ uVelocity: { value: simulation.velocityTexture },
258
+ uPixelRatio: { value: 1 },
259
+ uAirFraction: { value: this.airFraction },
260
+ },
261
+ vertexShader: /* glsl */`
262
+ precision highp float;
263
+ uniform sampler2D uPosition;
264
+ uniform sampler2D uVelocity;
265
+ uniform float uPixelRatio;
266
+ in vec2 aLookup;
267
+ out float vImpact;
268
+ void main() {
269
+ vec4 particle = texture(uPosition, aLookup);
270
+ vec4 velocity = texture(uVelocity, aLookup);
271
+ vImpact = velocity.w;
272
+ vec4 mv = viewMatrix * vec4(particle.xyz, 1.0);
273
+ gl_Position = projectionMatrix * mv;
274
+ gl_PointSize = min(6.5, (1.2 + velocity.w * 3.4) * uPixelRatio * (5.5 / max(2.0, -mv.z)));
275
+ if (particle.w < 0.0 || velocity.w < 0.045) gl_Position = vec4(2.0, 2.0, 2.0, 1.0);
276
+ }
277
+ `,
278
+ fragmentShader: /* glsl */`
279
+ precision highp float;
280
+ uniform float uAirFraction;
281
+ in float vImpact;
282
+ out vec4 outColor;
283
+ void main() {
284
+ vec2 p = gl_PointCoord - 0.5;
285
+ float d = length(p);
286
+ if (d > 0.5) discard;
287
+ float glint = 1.0 - smoothstep(0.02, 0.15, distance(gl_PointCoord, vec2(0.34, 0.3)));
288
+ float edge = 1.0 - smoothstep(0.34, 0.5, d);
289
+ vec3 color = mix(vec3(0.18, 0.68, 0.78), vec3(0.86, 1.0, 1.0), glint);
290
+ outColor = vec4(color, edge * vImpact * (0.12 + uAirFraction * 0.34));
291
+ }
292
+ `,
293
+ transparent: true,
294
+ depthTest: false,
295
+ depthWrite: false,
296
+ blending: THREE.AdditiveBlending,
297
+ toneMapped: false,
298
+ });
299
+ this.sprayScene = new THREE.Scene();
300
+ this.sprayPoints = new THREE.Points(this.lookupGeometry, this.sprayMaterial);
301
+ this.sprayPoints.frustumCulled = false;
302
+ this.sprayScene.add(this.sprayPoints);
303
+
304
+ this.depthBlurMaterial = new THREE.ShaderMaterial({
305
+ glslVersion: THREE.GLSL3,
306
+ uniforms: {
307
+ uInput: { value: this.depthTarget.texture },
308
+ uTexel: { value: new THREE.Vector2() },
309
+ uDirection: { value: new THREE.Vector2(1, 0) },
310
+ uRadius: { value: 5 },
311
+ uRetainedOnly: { value: 0 },
312
+ },
313
+ vertexShader: fullscreenVertex,
314
+ fragmentShader: /* glsl */`
315
+ precision highp float;
316
+ uniform sampler2D uInput;
317
+ uniform vec2 uTexel;
318
+ uniform vec2 uDirection;
319
+ uniform int uRadius;
320
+ uniform float uRetainedOnly;
321
+ in vec2 vUv;
322
+ out vec4 outColor;
323
+
324
+ void main() {
325
+ vec4 centerSample = texture(uInput, vUv);
326
+ if (uRetainedOnly > 0.5 && centerSample.b < 0.35) {
327
+ outColor = centerSample;
328
+ return;
329
+ }
330
+ float center = centerSample.r;
331
+ if (center <= 0.0) {
332
+ float nearest = 1e6;
333
+ vec4 nearestSample = vec4(0.0);
334
+ for (int i = -5; i <= 5; i++) {
335
+ if (abs(i) > uRadius) continue;
336
+ vec4 candidate = texture(uInput, vUv + uDirection * uTexel * float(i));
337
+ if (candidate.r > 0.0 && candidate.r < nearest) {
338
+ nearest = candidate.r;
339
+ nearestSample = candidate;
340
+ }
341
+ }
342
+ if (nearest > 1e5) { outColor = vec4(0.0); return; }
343
+ center = nearest;
344
+ centerSample = nearestSample;
345
+ }
346
+
347
+ float total = 0.0;
348
+ float retainedTotal = 0.0;
349
+ float weightSum = 0.0;
350
+ float centerRetained = step(0.45, centerSample.b);
351
+ for (int i = -5; i <= 5; i++) {
352
+ if (abs(i) > uRadius) continue;
353
+ vec4 sampleValue = texture(uInput, vUv + uDirection * uTexel * float(i));
354
+ if (sampleValue.r <= 0.0) continue;
355
+ float retained = max(centerRetained, step(0.45, sampleValue.b));
356
+ float gaussian = exp(-float(i * i) * mix(0.11, 0.055, retained));
357
+ float bilateral = exp(-abs(sampleValue.r - center) * mix(18.0, 3.2, retained));
358
+ float weight = gaussian * bilateral;
359
+ total += sampleValue.r * weight;
360
+ retainedTotal += sampleValue.b * weight;
361
+ weightSum += weight;
362
+ }
363
+ outColor = vec4(
364
+ total / max(weightSum, 0.0001),
365
+ 0.0,
366
+ retainedTotal / max(weightSum, 0.0001),
367
+ 1.0
368
+ );
369
+ }
370
+ `,
371
+ depthTest: false,
372
+ depthWrite: false,
373
+ toneMapped: false,
374
+ });
375
+
376
+ this.thicknessBlurMaterial = new THREE.ShaderMaterial({
377
+ glslVersion: THREE.GLSL3,
378
+ uniforms: {
379
+ uInput: { value: this.thicknessTarget.texture },
380
+ uTexel: { value: new THREE.Vector2() },
381
+ uDirection: { value: new THREE.Vector2(1, 0) },
382
+ },
383
+ vertexShader: fullscreenVertex,
384
+ fragmentShader: /* glsl */`
385
+ precision highp float;
386
+ uniform sampler2D uInput;
387
+ uniform vec2 uTexel;
388
+ uniform vec2 uDirection;
389
+ in vec2 vUv;
390
+ out vec4 outColor;
391
+ void main() {
392
+ vec4 result = texture(uInput, vUv) * 0.227027;
393
+ result += texture(uInput, vUv + uDirection * uTexel * 1.384615) * 0.316216;
394
+ result += texture(uInput, vUv - uDirection * uTexel * 1.384615) * 0.316216;
395
+ result += texture(uInput, vUv + uDirection * uTexel * 3.230769) * 0.070270;
396
+ result += texture(uInput, vUv - uDirection * uTexel * 3.230769) * 0.070270;
397
+ outColor = result;
398
+ }
399
+ `,
400
+ depthTest: false,
401
+ depthWrite: false,
402
+ toneMapped: false,
403
+ });
404
+
405
+ this.compositeMaterial = new THREE.ShaderMaterial({
406
+ glslVersion: THREE.GLSL3,
407
+ uniforms: {
408
+ uSceneColor: { value: this.externalBase ? this.externalBase.colorTexture : this.sceneTarget.texture },
409
+ uSceneDepth: { value: this.externalBase ? this.externalBase.depthTexture : this.sceneTarget.depthTexture },
410
+ uDepthMode: { value: this.externalBase && this.externalBase.depthMode === 'logarithmic' ? 1.0 : 0.0 },
411
+ uFluidDepth: { value: this.blurB.texture },
412
+ uThickness: { value: this.thicknessBlurB.texture },
413
+ uResolution: { value: new THREE.Vector2(width, height) },
414
+ uInverseProjection: { value: camera.projectionMatrixInverse },
415
+ uCameraNear: { value: camera.near },
416
+ uCameraFar: { value: camera.far },
417
+ uLightDirection: { value: new THREE.Vector3(-0.2, 0.7, 0.6).normalize() },
418
+ uAirFraction: { value: this.airFraction },
419
+ },
420
+ vertexShader: fullscreenVertex,
421
+ fragmentShader: /* glsl */`
422
+ precision highp float;
423
+ uniform sampler2D uSceneColor;
424
+ uniform sampler2D uSceneDepth;
425
+ uniform sampler2D uFluidDepth;
426
+ uniform sampler2D uThickness;
427
+ uniform vec2 uResolution;
428
+ uniform mat4 uInverseProjection;
429
+ uniform float uCameraNear;
430
+ uniform float uCameraFar;
431
+ uniform vec3 uLightDirection;
432
+ uniform float uAirFraction;
433
+ in vec2 vUv;
434
+ out vec4 outColor;
435
+
436
+ uniform float uDepthMode;
437
+ float sceneDepthToViewDistance(float depth) {
438
+ if (uDepthMode > 0.5) {
439
+ // 对数深度(three-base logarithmicDepthBuffer):与 frosted_glass_pass.ts:146 对齐
440
+ return pow(uCameraFar + 1.0, depth) - 1.0;
441
+ }
442
+ float viewZ = (uCameraNear * uCameraFar) / ((uCameraFar - uCameraNear) * depth - uCameraFar);
443
+ return -viewZ;
444
+ }
445
+
446
+ vec3 viewPosition(vec2 uv, float distanceToCamera) {
447
+ vec4 ray = uInverseProjection * vec4(uv * 2.0 - 1.0, 1.0, 1.0);
448
+ vec3 direction = ray.xyz / ray.w;
449
+ return direction * (-distanceToCamera / direction.z);
450
+ }
451
+
452
+ void main() {
453
+ vec3 sceneColor = texture(uSceneColor, vUv).rgb;
454
+ float depth = texture(uFluidDepth, vUv).r;
455
+ if (depth <= 0.0) { outColor = vec4(sceneColor, 1.0); return; }
456
+
457
+ float sceneDistance = sceneDepthToViewDistance(texture(uSceneDepth, vUv).r);
458
+ if (depth > sceneDistance + 0.018) { outColor = vec4(sceneColor, 1.0); return; }
459
+
460
+ vec4 thicknessData = texture(uThickness, vUv);
461
+ float retainedPreview = clamp(
462
+ thicknessData.b / max(thicknessData.r, 0.0001),
463
+ 0.0,
464
+ 1.0
465
+ );
466
+ vec2 texel = 1.0 / uResolution;
467
+ float normalSpan = mix(1.0, 2.35, retainedPreview);
468
+ float dL = texture(uFluidDepth, vUv - vec2(texel.x * normalSpan, 0.0)).r;
469
+ float dR = texture(uFluidDepth, vUv + vec2(texel.x * normalSpan, 0.0)).r;
470
+ float dD = texture(uFluidDepth, vUv - vec2(0.0, texel.y * normalSpan)).r;
471
+ float dU = texture(uFluidDepth, vUv + vec2(0.0, texel.y * normalSpan)).r;
472
+ dL = dL <= 0.0 ? depth : dL;
473
+ dR = dR <= 0.0 ? depth : dR;
474
+ dD = dD <= 0.0 ? depth : dD;
475
+ dU = dU <= 0.0 ? depth : dU;
476
+
477
+ vec3 pL = viewPosition(vUv - vec2(texel.x * normalSpan, 0.0), dL);
478
+ vec3 pR = viewPosition(vUv + vec2(texel.x * normalSpan, 0.0), dR);
479
+ vec3 pD = viewPosition(vUv - vec2(0.0, texel.y * normalSpan), dD);
480
+ vec3 pU = viewPosition(vUv + vec2(0.0, texel.y * normalSpan), dU);
481
+ vec3 normal = normalize(cross(pR - pL, pU - pD));
482
+ if (normal.z < 0.0) normal = -normal;
483
+
484
+ float thickness = max(0.0, thicknessData.r * 1.28);
485
+ float impactFoam = thicknessData.g / max(thicknessData.r, 0.0001);
486
+ float retainedPhase = clamp(thicknessData.b / max(thicknessData.r, 0.0001), 0.0, 1.0);
487
+ float curvature = abs(dL + dR + dD + dU - 4.0 * depth) / max(depth, 0.1);
488
+ float foam = smoothstep(0.22, 0.8, impactFoam) * (0.08 + uAirFraction * 0.5)
489
+ + smoothstep(0.004, 0.02, curvature) * uAirFraction * 0.2
490
+ * mix(1.0, 0.1, retainedPhase);
491
+ foam = clamp(foam, 0.0, 0.55);
492
+
493
+ vec2 refractOffset = normal.xy * (0.004 + min(thickness, 0.8) * 0.012) * vec2(1.0, -1.0);
494
+ vec2 refractUv = clamp(vUv + refractOffset, vec2(0.002), vec2(0.998));
495
+ vec3 refracted = texture(uSceneColor, refractUv).rgb;
496
+ vec3 absorption = vec3(0.09, 0.032, 0.017);
497
+ vec3 transmission = exp(-absorption * thickness * mix(3.8, 2.45, retainedPhase));
498
+ vec3 waterTint = vec3(0.01, 0.145, 0.18);
499
+ vec3 body = refracted * transmission + waterTint * (1.0 - transmission);
500
+
501
+ vec3 viewDir = normalize(-viewPosition(vUv, depth));
502
+ float fresnel = 0.0204 + 0.9796 * pow(1.0 - max(dot(normal, viewDir), 0.0), 5.0);
503
+ vec3 lightDir = normalize(uLightDirection);
504
+ vec3 halfDir = normalize(lightDir + viewDir);
505
+ float specular = pow(max(dot(normal, halfDir), 0.0), 132.0);
506
+ vec3 reflection = mix(vec3(0.03, 0.09, 0.11), vec3(0.34, 0.56, 0.62), max(normal.y * 0.5 + 0.5, fresnel));
507
+ vec3 waterColor = mix(body, reflection, fresnel * 0.5);
508
+ waterColor += vec3(0.84, 0.98, 1.0) * specular * 1.1;
509
+ waterColor = mix(waterColor, vec3(0.72, 0.94, 0.96), foam);
510
+
511
+ float coverage = smoothstep(0.002, 0.06, thickness);
512
+ float fluidOpacity = mix(0.76, 0.59, retainedPhase) + fresnel * 0.22;
513
+ vec3 result = mix(sceneColor, waterColor, coverage * fluidOpacity);
514
+ outColor = vec4(result, 1.0);
515
+ }
516
+ `,
517
+ depthTest: false,
518
+ depthWrite: false,
519
+ toneMapped: false,
520
+ });
521
+
522
+ this.copyMaterial = new THREE.ShaderMaterial({
523
+ glslVersion: THREE.GLSL3,
524
+ uniforms: { tDiffuse: { value: null } },
525
+ vertexShader: fullscreenVertex,
526
+ fragmentShader: /* glsl */`
527
+ precision highp float;
528
+ uniform sampler2D tDiffuse;
529
+ in vec2 vUv;
530
+ out vec4 outColor;
531
+ void main() { outColor = texture(tDiffuse, vUv); }
532
+ `,
533
+ depthTest: false,
534
+ depthWrite: false,
535
+ toneMapped: false,
536
+ });
537
+ // 函数式 renderInto 的中间合成目标:composite 先写到它,再拷回 outputTarget,
538
+ // 避免「读当前目标写当前目标」的反馈未定义行为(与 frosted_glass_pass 同构)。
539
+ // 与 outputTarget 同分辨率(全分辨率),保证合成回屏幕时不丢清晰度(水本身是软的,但背景不能糊)。
540
+ this.compositeTarget = createFluidTarget(width, height, false);
541
+
542
+ this.fsQuad = new FullScreenQuad(this.compositeMaterial);
543
+ this.setSize(width, height);
544
+ }
545
+
546
+ setAirFraction(value: number): void {
547
+ if (!this.supported) return;
548
+ this.airFraction = value;
549
+ this.compositeMaterial.uniforms.uAirFraction.value = value;
550
+ this.sprayMaterial.uniforms.uAirFraction.value = value;
551
+ }
552
+
553
+ setParticleRadius(value: number): void {
554
+ if (!this.supported) return;
555
+ this.depthMaterial.uniforms.uParticleRadius.value = value;
556
+ this.thicknessMaterial.uniforms.uParticleRadius.value = value;
557
+ }
558
+
559
+ setRetainedFluidVisible(visible: boolean): void {
560
+ if (!this.supported) return;
561
+ const value = visible ? 1 : 0;
562
+ this.depthMaterial.uniforms.uShowRetained.value = value;
563
+ this.thicknessMaterial.uniforms.uShowRetained.value = value;
564
+ }
565
+
566
+ setQuality({ scale, activeCount, blurRadius }: FluidQuality): void {
567
+ if (!this.supported) return;
568
+ this.scale = scale;
569
+ this.activeCount = Math.min(activeCount, this.simulation.count);
570
+ this.lookupGeometry.setDrawRange(0, this.activeCount);
571
+ const retainedRadiusScale = 1.58 * Math.cbrt(40000 / Math.max(this.activeCount, 1));
572
+ this.depthMaterial.uniforms.uRetainedRadiusScale.value = retainedRadiusScale;
573
+ this.thicknessMaterial.uniforms.uRetainedRadiusScale.value = retainedRadiusScale;
574
+ this.depthBlurMaterial.uniforms.uRadius.value = blurRadius;
575
+ this.setSize(this.width, this.height);
576
+ }
577
+
578
+ /** three-base 等外部调用方在 resize / 切换 baseRT 时刷新注入的基础纹理。 */
579
+ setBaseTextures({ colorTexture, depthTexture, depthMode }: PartialBaseTextures = {}): void {
580
+ if (!this.supported || !this.externalBase) return;
581
+ if (colorTexture) {
582
+ this.externalBase.colorTexture = colorTexture;
583
+ this.compositeMaterial.uniforms.uSceneColor.value = colorTexture;
584
+ }
585
+ if (depthTexture) {
586
+ this.externalBase.depthTexture = depthTexture;
587
+ this.compositeMaterial.uniforms.uSceneDepth.value = depthTexture;
588
+ }
589
+ if (depthMode) {
590
+ this.externalBase.depthMode = depthMode === 'logarithmic' ? 'logarithmic' : 'standard';
591
+ this.compositeMaterial.uniforms.uDepthMode.value = this.externalBase.depthMode === 'logarithmic' ? 1.0 : 0.0;
592
+ }
593
+ }
594
+
595
+ setSize(width: number, height: number): void {
596
+ if (!this.supported) return;
597
+ this.width = Math.max(1, Math.floor(width));
598
+ this.height = Math.max(1, Math.floor(height));
599
+ const fluidWidth = Math.max(1, Math.ceil(this.width * this.scale));
600
+ const fluidHeight = Math.max(1, Math.ceil(this.height * this.scale));
601
+ this.sceneTarget?.setSize(this.width, this.height);
602
+ this.depthTarget.setSize(fluidWidth, fluidHeight);
603
+ this.thicknessTarget.setSize(fluidWidth, fluidHeight);
604
+ this.blurA.setSize(fluidWidth, fluidHeight);
605
+ this.blurB.setSize(fluidWidth, fluidHeight);
606
+ this.thicknessBlurA.setSize(fluidWidth, fluidHeight);
607
+ this.thicknessBlurB.setSize(fluidWidth, fluidHeight);
608
+ this.compositeTarget.setSize(this.width, this.height);
609
+ this.depthBlurMaterial.uniforms.uTexel.value.set(1 / fluidWidth, 1 / fluidHeight);
610
+ this.thicknessBlurMaterial.uniforms.uTexel.value.set(1 / fluidWidth, 1 / fluidHeight);
611
+ this.compositeMaterial.uniforms.uResolution.value.set(fluidWidth, fluidHeight);
612
+ const pointScale = fluidHeight / Math.tan(THREE.MathUtils.degToRad(this.camera.fov * 0.5));
613
+ this.depthMaterial.uniforms.uPointScale.value = pointScale;
614
+ this.thicknessMaterial.uniforms.uPointScale.value = pointScale;
615
+ this.sprayMaterial.uniforms.uPixelRatio.value = Math.min(window.devicePixelRatio, 2);
616
+ }
617
+
618
+ /** 渲染粒子深度/厚度 + 两轮分离模糊 + 厚度模糊(不输出合成、不渲染 spray)。render/renderInto 共用。 */
619
+ renderFluidTextures(renderer: THREE.WebGLRenderer, camera?: THREE.Camera): void {
620
+ if (!this.supported) return;
621
+ const cam = camera || this.camera;
622
+ this.depthMaterial.uniforms.uPosition.value = this.simulation.positionTexture;
623
+ this.depthMaterial.uniforms.uVelocity.value = this.simulation.velocityTexture;
624
+ this.thicknessMaterial.uniforms.uPosition.value = this.simulation.positionTexture;
625
+ this.thicknessMaterial.uniforms.uVelocity.value = this.simulation.velocityTexture;
626
+ this.sprayMaterial.uniforms.uPosition.value = this.simulation.positionTexture;
627
+ this.sprayMaterial.uniforms.uVelocity.value = this.simulation.velocityTexture;
628
+ this.compositeMaterial.uniforms.uInverseProjection.value.copy(cam.projectionMatrixInverse);
629
+ this.depthMaterial.uniforms.uProjectionMatrix.value.copy(cam.projectionMatrix);
630
+
631
+ renderer.setClearColor(0x000000, 0);
632
+ this.depthPoints.material = this.depthMaterial;
633
+ renderer.setRenderTarget(this.depthTarget);
634
+ renderer.clear(true, true, false);
635
+ renderer.render(this.particleScene, cam);
636
+
637
+ this.depthPoints.material = this.thicknessMaterial;
638
+ renderer.setRenderTarget(this.thicknessTarget);
639
+ renderer.clear(true, false, false);
640
+ renderer.render(this.particleScene, cam);
641
+
642
+ this.fsQuad.material = this.depthBlurMaterial;
643
+ this.depthBlurMaterial.uniforms.uInput.value = this.depthTarget.texture;
644
+ this.depthBlurMaterial.uniforms.uRetainedOnly.value = 0;
645
+ this.depthBlurMaterial.uniforms.uDirection.value.set(1, 0);
646
+ renderer.setRenderTarget(this.blurA);
647
+ renderer.clear();
648
+ this.fsQuad.render(renderer);
649
+ this.depthBlurMaterial.uniforms.uInput.value = this.blurA.texture;
650
+ this.depthBlurMaterial.uniforms.uDirection.value.set(0, 1);
651
+ renderer.setRenderTarget(this.blurB);
652
+ renderer.clear();
653
+ this.fsQuad.render(renderer);
654
+
655
+ // A second phase-aware relaxation removes the individual SPH sphere imprint
656
+ // from stored water without softening the narrow free jet.
657
+ this.depthBlurMaterial.uniforms.uRetainedOnly.value = 1;
658
+ this.depthBlurMaterial.uniforms.uInput.value = this.blurB.texture;
659
+ this.depthBlurMaterial.uniforms.uDirection.value.set(1, 0);
660
+ renderer.setRenderTarget(this.blurA);
661
+ renderer.clear();
662
+ this.fsQuad.render(renderer);
663
+ this.depthBlurMaterial.uniforms.uInput.value = this.blurA.texture;
664
+ this.depthBlurMaterial.uniforms.uDirection.value.set(0, 1);
665
+ renderer.setRenderTarget(this.blurB);
666
+ renderer.clear();
667
+ this.fsQuad.render(renderer);
668
+ this.depthBlurMaterial.uniforms.uRetainedOnly.value = 0;
669
+
670
+ this.fsQuad.material = this.thicknessBlurMaterial;
671
+ this.thicknessBlurMaterial.uniforms.uInput.value = this.thicknessTarget.texture;
672
+ this.thicknessBlurMaterial.uniforms.uDirection.value.set(1, 0);
673
+ renderer.setRenderTarget(this.thicknessBlurA);
674
+ renderer.clear();
675
+ this.fsQuad.render(renderer);
676
+ this.thicknessBlurMaterial.uniforms.uInput.value = this.thicknessBlurA.texture;
677
+ this.thicknessBlurMaterial.uniforms.uDirection.value.set(0, 1);
678
+ renderer.setRenderTarget(this.thicknessBlurB);
679
+ renderer.clear();
680
+ this.fsQuad.render(renderer);
681
+ }
682
+
683
+ /** EffectComposer 链入口(自渲染模式 / base 纹理注入模式通用,保持原行为不变)。 */
684
+ render(renderer: THREE.WebGLRenderer, writeBuffer: THREE.WebGLRenderTarget): void {
685
+ if (!this.supported) return;
686
+ const previousTarget = renderer.getRenderTarget();
687
+ const previousColor = renderer.getClearColor(new THREE.Color());
688
+ const previousAlpha = renderer.getClearAlpha();
689
+ const previousAutoClear = renderer.autoClear;
690
+
691
+ if (!this.externalBase) {
692
+ renderer.autoClear = true;
693
+ renderer.setClearColor(0x061017, 1);
694
+ renderer.setRenderTarget(this.sceneTarget);
695
+ renderer.clear(true, true, true);
696
+ renderer.render(this.scene, this.camera);
697
+ }
698
+
699
+ this.renderFluidTextures(renderer, this.camera);
700
+
701
+ this.fsQuad.material = this.compositeMaterial;
702
+ this.compositeMaterial.uniforms.uFluidDepth.value = this.blurB.texture;
703
+ this.compositeMaterial.uniforms.uThickness.value = this.thicknessBlurB.texture;
704
+ renderer.setRenderTarget(this.renderToScreen ? null : writeBuffer);
705
+ renderer.clear();
706
+ this.fsQuad.render(renderer);
707
+
708
+ renderer.autoClear = false;
709
+ renderer.render(this.sprayScene, this.camera);
710
+ renderer.autoClear = previousAutoClear;
711
+ renderer.setRenderTarget(previousTarget);
712
+ renderer.setClearColor(previousColor, previousAlpha);
713
+ }
714
+
715
+ /**
716
+ * 函数式入口:直接消费调用方已有的基础颜色/深度纹理,把水面合成到指定目标。
717
+ * 与 three-base 的手写 fsQuad 后处理链(frosted_glass_pass 等)形态一致:
718
+ * 不创建/自渲染主场景,composite 先写到内部 compositeTarget,再拷回 outputTarget,
719
+ * 避免「读 baseRenderTarget 写 baseRenderTarget」的反馈未定义行为。
720
+ * @param {THREE.WebGLRenderer} renderer
721
+ * @param {{colorTexture:THREE.Texture,depthTexture:THREE.Texture,depthMode?:'standard'|'logarithmic',outputTarget:THREE.WebGLRenderTarget,camera:THREE.Camera}} opts
722
+ */
723
+ renderInto(renderer: THREE.WebGLRenderer, opts: WaterFlowRenderOptions): void {
724
+ if (!this.supported || !opts) return;
725
+ const {
726
+ colorTexture,
727
+ depthTexture,
728
+ depthMode = 'logarithmic',
729
+ outputTarget,
730
+ camera,
731
+ } = opts;
732
+
733
+ if (colorTexture) {
734
+ this.externalBase = { colorTexture, depthTexture, depthMode };
735
+ this.compositeMaterial.uniforms.uSceneColor.value = colorTexture;
736
+ this.compositeMaterial.uniforms.uSceneDepth.value = depthTexture;
737
+ this.compositeMaterial.uniforms.uDepthMode.value = depthMode === 'logarithmic' ? 1.0 : 0.0;
738
+ }
739
+
740
+ const previousTarget = renderer.getRenderTarget();
741
+ const previousColor = renderer.getClearColor(new THREE.Color());
742
+ const previousAlpha = renderer.getClearAlpha();
743
+ const previousAutoClear = renderer.autoClear;
744
+
745
+ this.renderFluidTextures(renderer, camera);
746
+
747
+ // composite → 独立 compositeTarget(读 base 写 compositeTarget,无 feedback)
748
+ this.fsQuad.material = this.compositeMaterial;
749
+ this.compositeMaterial.uniforms.uFluidDepth.value = this.blurB.texture;
750
+ this.compositeMaterial.uniforms.uThickness.value = this.thicknessBlurB.texture;
751
+ renderer.setRenderTarget(this.compositeTarget);
752
+ renderer.clear();
753
+ this.fsQuad.render(renderer);
754
+
755
+ // 拷贝 compositeTarget → outputTarget(如 baseRenderTarget)
756
+ this.fsQuad.material = this.copyMaterial;
757
+ this.copyMaterial.uniforms.tDiffuse.value = this.compositeTarget.texture;
758
+ renderer.setRenderTarget(outputTarget);
759
+ renderer.clear();
760
+ this.fsQuad.render(renderer);
761
+
762
+ // spray 飞沫叠加到 outputTarget
763
+ renderer.autoClear = false;
764
+ renderer.render(this.sprayScene, camera);
765
+ renderer.autoClear = previousAutoClear;
766
+
767
+ renderer.setRenderTarget(previousTarget);
768
+ renderer.setClearColor(previousColor, previousAlpha);
769
+ }
770
+
771
+ dispose(): void {
772
+ if (!this.supported) return;
773
+ this.sceneTarget?.dispose();
774
+ this.depthTarget.dispose();
775
+ this.thicknessTarget.dispose();
776
+ this.blurA.dispose();
777
+ this.blurB.dispose();
778
+ this.thicknessBlurA.dispose();
779
+ this.thicknessBlurB.dispose();
780
+ this.lookupGeometry.dispose();
781
+ this.depthMaterial.dispose();
782
+ this.thicknessMaterial.dispose();
783
+ this.sprayMaterial.dispose();
784
+ this.depthBlurMaterial.dispose();
785
+ this.thicknessBlurMaterial.dispose();
786
+ this.compositeMaterial.dispose();
787
+ this.fsQuad.dispose();
788
+ }
789
+ }