three-base 2.0.137 → 2.0.142
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/DirectionalLight.ts +70 -0
- package/EventMap.d.ts +103 -0
- package/EventMap.js +17 -0
- package/EventMap.ts +139 -0
- package/EventMesh.d.ts +18 -18
- package/EventMesh.js +59 -59
- package/EventMesh.ts +60 -0
- package/ExtrudeMesh.d.ts +112 -112
- package/ExtrudeMesh.js +342 -366
- package/ExtrudeMesh.ts +553 -0
- package/FPScontrol.d.ts +511 -78
- package/FPScontrol.js +1799 -617
- package/FPScontrol.ts +1855 -0
- package/Fire/Fire.d.ts +28 -28
- package/Fire/Fire.js +80 -79
- package/Fire/Fire.ts +89 -0
- package/Fire/{FireGromtry.d.ts → FireGeometry.d.ts} +9 -9
- package/Fire/{FireGromtry.js → FireGeometry.js} +40 -40
- package/Fire/FireGeometry.ts +40 -0
- package/Fire/FireMaterial.d.ts +28 -28
- package/Fire/FireMaterial.js +32 -32
- package/Fire/FireMaterial.ts +89 -0
- package/Fire/index.d.ts +2 -1
- package/Fire/index.js +2 -1
- package/Fire/index.ts +2 -0
- package/GLTFDracoLoader.d.ts +71 -0
- package/GLTFDracoLoader.js +266 -0
- package/GLTFDracoLoader.ts +430 -0
- package/Grass/Grass.d.ts +8 -8
- package/Grass/Grass.js +38 -38
- package/Grass/Grass.ts +38 -0
- package/Grass/index.d.ts +2 -1
- package/Grass/index.js +2 -1
- package/Grass/index.ts +2 -0
- package/Ground1/Ground1.d.ts +10 -11
- package/Ground1/Ground1.js +95 -91
- package/Ground1/Ground1.ts +114 -0
- package/Ground1/index.d.ts +1 -1
- package/Ground1/index.js +1 -1
- package/Ground1/index.ts +1 -0
- package/MeshBase.d.ts +36 -35
- package/MeshBase.js +247 -223
- package/MeshBase.ts +253 -0
- package/Mirror.d.ts +25 -25
- package/Mirror.js +143 -139
- package/Mirror.ts +203 -0
- package/MktMesh/MktMesh.d.ts +291 -0
- package/MktMesh/MktMesh.js +1045 -0
- package/MktMesh/MktMesh.ts +1075 -0
- package/MktMesh/assetUrl.d.ts +35 -0
- package/MktMesh/assetUrl.js +69 -0
- package/MktMesh/assetUrl.ts +69 -0
- package/MktMesh/mktMappingShader.d.ts +12 -0
- package/MktMesh/mktMappingShader.js +139 -0
- package/MktMesh/mktMappingShader.ts +139 -0
- package/MktMesh/mktShader.d.ts +263 -0
- package/MktMesh/mktShader.js +1248 -0
- package/MktMesh/mktShader.ts +1344 -0
- package/MktMesh/panoBitmapTexture.d.ts +25 -0
- package/MktMesh/panoBitmapTexture.js +52 -0
- package/MktMesh/panoBitmapTexture.ts +54 -0
- package/MktMesh/panoSlotBind.d.ts +29 -0
- package/MktMesh/panoSlotBind.js +106 -0
- package/MktMesh/panoSlotBind.ts +153 -0
- package/MktMesh/panoSlotRoster.d.ts +22 -0
- package/MktMesh/panoSlotRoster.js +169 -0
- package/MktMesh/panoSlotRoster.ts +168 -0
- package/MktMesh/panoTileStitcher.d.ts +119 -0
- package/MktMesh/panoTileStitcher.js +533 -0
- package/MktMesh/panoTileStitcher.ts +616 -0
- package/MktMesh/types.d.ts +75 -0
- package/MktMesh/types.js +1 -0
- package/MktMesh/types.ts +87 -0
- package/MktMesh/wallCapTessellate.d.ts +33 -0
- package/MktMesh/wallCapTessellate.js +97 -0
- package/MktMesh/wallCapTessellate.ts +68 -0
- package/PipeMesh/PathGeometry.d.ts +44 -44
- package/PipeMesh/PathGeometry.js +307 -300
- package/PipeMesh/PathGeometry.ts +343 -0
- package/PipeMesh/PathPoint.d.ts +17 -17
- package/PipeMesh/PathPoint.js +31 -31
- package/PipeMesh/PathPoint.ts +36 -0
- package/PipeMesh/PathPointList.d.ts +31 -31
- package/PipeMesh/PathPointList.js +209 -208
- package/PipeMesh/PathPointList.ts +227 -0
- package/PipeMesh/PathTubeGeometry.d.ts +25 -25
- package/PipeMesh/PathTubeGeometry.js +130 -130
- package/PipeMesh/PathTubeGeometry.ts +146 -0
- package/PipeMesh/PipeMesh.d.ts +25 -25
- package/PipeMesh/PipeMesh.js +63 -63
- package/PipeMesh/PipeMesh.ts +76 -0
- package/PipeMesh/index.d.ts +1 -1
- package/PipeMesh/index.js +1 -1
- package/PipeMesh/index.ts +1 -0
- package/PlaneModal1/PlaneModal1.d.ts +34 -31
- package/PlaneModal1/PlaneModal1.js +109 -105
- package/PlaneModal1/PlaneModal1.ts +138 -0
- package/PlaneModal1/index.d.ts +1 -1
- package/PlaneModal1/index.js +1 -1
- package/PlaneModal1/index.ts +1 -0
- package/PlaneText.d.ts +27 -39
- package/PlaneText.js +35 -30
- package/PlaneText.ts +55 -0
- package/PlaneWithHtml.d.ts +13 -11
- package/PlaneWithHtml.js +64 -63
- package/PlaneWithHtml.ts +70 -0
- package/PointLight/PointLight.d.ts +13 -14
- package/PointLight/PointLight.js +64 -70
- package/PointLight/PointLight.ts +67 -0
- package/PointLight/index.d.ts +1 -1
- package/PointLight/index.js +1 -1
- package/PointLight/index.ts +1 -0
- package/README.md +110 -79
- package/Sky/Sky.d.ts +9 -9
- package/Sky/Sky.js +22 -22
- package/Sky/Sky.ts +22 -0
- package/Sky/index.d.ts +1 -1
- package/Sky/index.js +1 -1
- package/Sky/index.ts +1 -0
- package/SpotLight.d.ts +6 -6
- package/SpotLight.js +72 -72
- package/SpotLight.ts +74 -0
- package/SpriteModal1/SpriteModal1.d.ts +33 -30
- package/SpriteModal1/SpriteModal1.js +116 -110
- package/SpriteModal1/SpriteModal1.ts +146 -0
- package/SpriteModal1/index.d.ts +1 -1
- package/SpriteModal1/index.js +1 -1
- package/SpriteModal1/index.ts +1 -0
- package/SpriteText.d.ts +46 -45
- package/SpriteText.js +176 -163
- package/SpriteText.ts +205 -0
- package/SpriteText2.d.ts +48 -47
- package/SpriteText2.js +223 -198
- package/SpriteText2.ts +258 -0
- package/SpriteText3.d.ts +86 -0
- package/SpriteText3.js +519 -0
- package/SpriteText3.ts +599 -0
- package/SpriteWithHtml.d.ts +9 -7
- package/SpriteWithHtml.js +44 -44
- package/SpriteWithHtml.ts +50 -0
- package/Text3D/Text3D.d.ts +20 -20
- package/Text3D/Text3D.js +58 -62
- package/Text3D/Text3D.ts +77 -0
- package/Text3D/fontController.d.ts +60 -59
- package/Text3D/fontController.js +166 -199
- package/Text3D/fontController.ts +209 -0
- package/Text3D/index.d.ts +1 -1
- package/Text3D/index.js +1 -1
- package/Text3D/index.ts +1 -0
- package/ThreeBase.d.ts +248 -111
- package/ThreeBase.js +1202 -656
- package/ThreeBase.ts +1314 -0
- package/ThreeMouse.d.ts +54 -40
- package/ThreeMouse.js +453 -407
- package/ThreeMouse.ts +472 -0
- package/Transition.d.ts +45 -40
- package/Transition.js +209 -210
- package/Transition.ts +242 -0
- package/Water/Water.d.ts +5 -5
- package/Water/Water.js +44 -26
- package/Water/Water.ts +45 -0
- package/Water/index.d.ts +2 -0
- package/Water/index.js +2 -0
- package/Water/index.ts +2 -0
- package/WaterFlow/FloatingDuck.d.ts +88 -0
- package/WaterFlow/FloatingDuck.js +307 -0
- package/WaterFlow/FloatingDuck.ts +445 -0
- package/WaterFlow/GPUFluidSimulation.d.ts +56 -0
- package/WaterFlow/GPUFluidSimulation.js +785 -0
- package/WaterFlow/GPUFluidSimulation.ts +827 -0
- package/WaterFlow/ReservoirSystem.d.ts +106 -0
- package/WaterFlow/ReservoirSystem.js +621 -0
- package/WaterFlow/ReservoirSystem.ts +754 -0
- package/WaterFlow/SPHField.d.ts +35 -0
- package/WaterFlow/SPHField.js +204 -0
- package/WaterFlow/SPHField.ts +228 -0
- package/WaterFlow/ScreenSpaceFluidPass.d.ts +69 -0
- package/WaterFlow/ScreenSpaceFluidPass.js +736 -0
- package/WaterFlow/ScreenSpaceFluidPass.ts +789 -0
- package/WaterFlow/ShallowWaterSurface.d.ts +45 -0
- package/WaterFlow/ShallowWaterSurface.js +441 -0
- package/WaterFlow/ShallowWaterSurface.ts +510 -0
- package/WaterFlow/WaterFlow.d.ts +87 -0
- package/WaterFlow/WaterFlow.js +171 -0
- package/WaterFlow/WaterFlow.ts +219 -0
- package/WaterFlow/types.d.ts +161 -0
- package/WaterFlow/types.js +1 -0
- package/WaterFlow/types.ts +189 -0
- package/WaterFlow/waterFlowPass.d.ts +3 -0
- package/WaterFlow/waterFlowPass.js +20 -0
- package/WaterFlow/waterFlowPass.ts +40 -0
- package/collisionOBB.d.ts +42 -0
- package/collisionOBB.js +112 -0
- package/collisionOBB.ts +108 -0
- package/declare.d.ts +23 -165
- package/index.d.ts +1 -1
- package/index.js +1 -2
- package/index.ts +1 -0
- package/instanced/InstancedBatch.d.ts +57 -0
- package/instanced/InstancedBatch.js +348 -0
- package/instanced/InstancedBatch.ts +406 -0
- package/instanced/InstancedChild.d.ts +174 -0
- package/instanced/InstancedChild.js +264 -0
- package/instanced/InstancedChild.ts +327 -0
- package/instanced/InstancedGroup.d.ts +43 -0
- package/instanced/InstancedGroup.js +126 -0
- package/instanced/InstancedGroup.ts +137 -0
- package/{InstancedMeshBase.d.ts → instanced/InstancedMeshBase.d.ts} +38 -39
- package/{InstancedMeshBase.js → instanced/InstancedMeshBase.js} +282 -293
- package/instanced/InstancedMeshBase.ts +292 -0
- package/instanced/InstancedTemplate.d.ts +46 -0
- package/instanced/InstancedTemplate.js +125 -0
- package/instanced/InstancedTemplate.ts +188 -0
- package/keyControls.d.ts +30 -0
- package/{keyContorls.js → keyControls.js} +101 -98
- package/keyControls.ts +107 -0
- package/lensflare/LensFlare.d.ts +39 -0
- package/lensflare/LensFlare.js +373 -0
- package/lensflare/LensFlare.ts +448 -0
- package/lensflare/lensflare0.png +0 -0
- package/lensflare/lensflare3.png +0 -0
- package/lensflare/lensflare_custom.png +0 -0
- package/lensflare/lensflare_hex_ghost.png +0 -0
- package/lensflare/lensflare_hex_main.png +0 -0
- package/package.json +99 -25
- package/pass/bloom_pass.d.ts +2 -2
- package/pass/bloom_pass.js +188 -101
- package/pass/bloom_pass.ts +243 -0
- package/pass/cover_pass.d.ts +2 -2
- package/pass/cover_pass.js +122 -99
- package/pass/cover_pass.ts +182 -0
- package/pass/debugDepthState.d.ts +20 -0
- package/pass/debugDepthState.js +9 -0
- package/pass/debugDepthState.ts +22 -0
- package/pass/depthTestBias.d.ts +12 -0
- package/pass/depthTestBias.js +12 -0
- package/pass/depthTestBias.ts +12 -0
- package/pass/effectUserData.d.ts +27 -0
- package/pass/effectUserData.js +1 -0
- package/pass/effectUserData.ts +46 -0
- package/pass/frosted_glass_pass.d.ts +2 -2
- package/pass/frosted_glass_pass.js +167 -186
- package/pass/frosted_glass_pass.ts +288 -0
- package/pass/fsQuad.d.ts +9 -9
- package/pass/fsQuad.js +28 -28
- package/pass/fsQuad.ts +34 -0
- package/pass/glow_pass.d.ts +2 -2
- package/pass/glow_pass.js +449 -233
- package/pass/glow_pass.ts +654 -0
- package/pass/screen_bloom_pass.d.ts +11 -0
- package/pass/screen_bloom_pass.js +235 -0
- package/pass/screen_bloom_pass.ts +263 -0
- package/pass/ssao_pass.d.ts +88 -13
- package/pass/ssao_pass.js +802 -366
- package/pass/ssao_pass.ts +918 -0
- package/pass/stroke_pass.d.ts +2 -2
- package/pass/stroke_pass.js +329 -258
- package/pass/stroke_pass.ts +638 -0
- package/shaders/FXAAShaderMaterial.d.ts +3 -3
- package/shaders/FXAAShaderMaterial.js +23 -66
- package/shaders/FXAAShaderMaterial.ts +201 -0
- package/shaders/baseDepthVisShader.d.ts +11 -0
- package/shaders/baseDepthVisShader.js +36 -0
- package/shaders/baseDepthVisShader.ts +36 -0
- package/shaders/baseTextureToRenderShader.d.ts +2 -2
- package/shaders/baseTextureToRenderShader.js +55 -60
- package/shaders/baseTextureToRenderShader.ts +232 -0
- package/shaders/copyShader.d.ts +3 -3
- package/shaders/copyShader.js +13 -14
- package/shaders/copyShader.ts +32 -0
- package/shaders/debugDepthCompareShader.d.ts +14 -0
- package/shaders/debugDepthCompareShader.js +49 -0
- package/shaders/debugDepthCompareShader.ts +50 -0
- package/shaders/debugDepthJudgeShader.d.ts +18 -0
- package/shaders/debugDepthJudgeShader.js +52 -0
- package/shaders/debugDepthJudgeShader.ts +54 -0
- package/shaders/debugExcludedShader.d.ts +13 -0
- package/shaders/debugExcludedShader.js +46 -0
- package/shaders/debugExcludedShader.ts +48 -0
- package/spriteTextBg.d.ts +1 -0
- package/spriteTextBg.js +20 -0
- package/spriteTextBg.ts +20 -0
- package/threeLoader.d.ts +35 -34
- package/threeLoader.js +518 -527
- package/threeLoader.ts +525 -0
- package/threeUtils.d.ts +53 -52
- package/threeUtils.js +329 -330
- package/threeUtils.ts +373 -0
- package/types.d.ts +2 -0
- package/types.js +6 -0
- package/types.ts +8 -0
- package/ApngTexture.d.ts +0 -13
- package/ApngTexture.js +0 -61
- package/ApngTextureManage.d.ts +0 -18
- package/ApngTextureManage.js +0 -87
- package/KeyFrameAnimation.d.ts +0 -25
- package/KeyFrameAnimation.js +0 -78
- package/keyContorls.d.ts +0 -25
- package/lib/draco/draco_decoder.js +0 -34
- package/lib/draco/draco_decoder.wasm +0 -0
- package/lib/draco/draco_wasm_wrapper.js +0 -117
- package/pass/emissive_ao_pass.d.ts +0 -13
- package/pass/emissive_ao_pass.js +0 -370
- package/shaders.d.ts +0 -5
- package/shaders.js +0 -93
|
@@ -0,0 +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
|
+
}
|