diff --git a/components/canvas/HeroScene.tsx b/components/canvas/HeroScene.tsx
index 7ba14ed..75b0e32 100644
--- a/components/canvas/HeroScene.tsx
+++ b/components/canvas/HeroScene.tsx
@@ -8,6 +8,7 @@ import { HelloModel, CursorStar } from "@/components/canvas/GlassModels";
import ImageLayers from "@/components/canvas/ImageLayers";
import BackgroundFX from "@/components/canvas/BackgroundFX";
import DotOverlay from "@/components/canvas/DotOverlay";
+import PostFX from "@/components/canvas/PostFX";
import { useAppReady } from "@/components/dom/LoadingOverlay";
import { useIsMobile } from "@/lib/useIsMobile";
@@ -61,6 +62,10 @@ export function HeroScene() {
{/* Glass hero models (hello + blue cursor star), refractive tint shader */}
+
+ {/* Post FX (original cG): lens-flare star streaks + pointer-driven
+ fluid refraction + lime dot-trail indicator. */}
+
);
diff --git a/components/canvas/PostFX.tsx b/components/canvas/PostFX.tsx
new file mode 100644
index 0000000..72ff8c2
--- /dev/null
+++ b/components/canvas/PostFX.tsx
@@ -0,0 +1,226 @@
+"use client";
+
+import { useEffect, useMemo, useRef, useState } from "react";
+import { useFrame, useThree } from "@react-three/fiber";
+import { EffectComposer } from "@react-three/postprocessing";
+import { UnsignedByteType, Vector2 } from "three";
+import { usePointer } from "@/lib/pointer";
+import { scrollBus } from "@/lib/scrollBus";
+import {
+ FluidPushPass,
+ LensFlarePass,
+} from "@/components/webgl/PostFxPasses";
+
+/**
+ * Hero post FX controller — 1:1 port of the original `cG` (chunk
+ * 4d3f3b68dbbde33a.js): an EffectComposer (renderPriority 998) with
+ *
+ * LensFlarePass (star streaks on bright pixels, downsampled .5 target,
+ * rendered every 2nd frame) →
+ * FluidPushPass (GPU fluid sim at simResolution 160 driven by pointer
+ * splats; display refracts the frame by the velocity field and draws
+ * the lime #c0fe04 16px-cell pointer trail).
+ *
+ * Gating (original useFrame):
+ * - flare enabled = banner/footer section visible && NOT covered by the
+ * dot overlay (sD hysteresis on sT(sP), threshold .9/.82)
+ * - fluid effect enabled = !mobile && !reducedMotion && !covered &&
+ * pointer moved within 600ms
+ * - while covered (mid-page), the fluid pass renders to screen so the
+ * lime trail acts as a pointer indicator over the dot curtain
+ * - params: starRays 6, intensity .7, threshold .99, streakScale
+ * 8·(w/1920)·(mobile?2:1), hotspotPower 32, gate .88,
+ * tailColorLight #ffa300
+ */
+
+const BRIGHT_SOURCE_SECTIONS = ["banner", "footer"];
+const clamp01 = (v: number) => Math.min(1, Math.max(0, v));
+const sT = (e?: number) => (e === undefined ? 0 : clamp01(e > 1 ? e / 100 : e));
+
+/** Original sD hysteresis comparator. */
+function sD(e: boolean, t: number, opaqueThreshold: number, opaqueTolerance: number, hysteresis: number) {
+ const l = Math.min(1, Math.max(0, opaqueThreshold - opaqueTolerance));
+ const s = Math.min(1, Math.max(0, l - hysteresis));
+ const n = sT(t);
+ return !e && n >= l ? true : (!e || !(n <= s) ? e : false);
+}
+
+const SOLID_EFFECT = { opaqueThreshold: 0.9, opaqueTolerance: 0, hysteresis: 0.9 - 0.82 };
+
+function useReducedMotion() {
+ const [reduced, setReduced] = useState(false);
+ useEffect(() => {
+ const mq = window.matchMedia("(prefers-reduced-motion: reduce)");
+ const update = () => setReduced(mq.matches);
+ update();
+ mq.addEventListener("change", update);
+ return () => mq.removeEventListener("change", update);
+ }, []);
+ return reduced;
+}
+
+/** Original s0.anySectionInViewport over data-section DOM anchors. */
+function anySectionInViewport(names: string[]): boolean {
+ if (typeof document === "undefined" || names.length === 0) return true;
+ const vh = Math.max(1, window.innerHeight);
+ let found = false;
+ for (const name of names) {
+ const el = document.querySelector(`[data-section="${name}"]`);
+ if (!el) continue;
+ found = true;
+ const r = el.getBoundingClientRect();
+ // sq: bottomDocY - scrollTop > 0 && topDocY - scrollTop < vh
+ if (r.bottom > 0 && r.top < vh) return true;
+ }
+ return !found ? true : false;
+}
+
+export function PostFX() {
+ const size = useThree((s) => s.size);
+ const gl = useThree((s) => s.gl);
+ const { uv: pointerUv, insideRef } = usePointer();
+ const isMobile = size.width < 1024;
+ const reducedMotion = useReducedMotion();
+
+ const composerRef = useRef(null);
+
+ const flarePass = useMemo(
+ () =>
+ new LensFlarePass({
+ flareDownsample: 0.5,
+ flareStride: 2,
+ starRays: 6,
+ intensity: 0.7,
+ threshold: 0.99,
+ streakScale: 8,
+ hotspotPower: 32,
+ gate: 0.88,
+ tailColor: "#ffa300",
+ }),
+ []
+ );
+ const fluidPass = useMemo(() => new FluidPushPass(), []);
+
+ useEffect(() => {
+ fluidPass.setPointerColor("#c0fe04");
+ fluidPass.setPointerPixelSize(16);
+ }, [fluidPass]);
+
+ useEffect(
+ () => () => {
+ flarePass.dispose();
+ fluidPass.dispose();
+ },
+ [flarePass, fluidPass]
+ );
+
+ // Params + metrics (original effects).
+ const streakScale = 8 * (Math.max(1, size.width) / 1920) * (isMobile ? 2 : 1);
+ useEffect(() => {
+ flarePass.setParams({
+ starRays: 6,
+ intensity: 0.7,
+ threshold: 0.99,
+ streakScale,
+ hotspotPower: 32,
+ gate: 0.88,
+ tailColor: "#ffa300",
+ });
+ }, [flarePass, streakScale]);
+
+ const dprRef = useRef(1);
+ useEffect(() => {
+ const composer = composerRef.current;
+ if (!composer) return;
+ const t = Math.min(gl.getPixelRatio(), 2);
+ dprRef.current = t;
+ const r = Math.max(1, Math.floor(size.width * t));
+ const n = Math.max(1, Math.floor(size.height * t));
+ composer.inputBuffer.setSize(r, n);
+ composer.outputBuffer.setSize(r, n);
+ for (const pass of composer.passes) pass.setSize(r, n);
+ fluidPass.setDisplayMetrics(size.width, size.height, t);
+ }, [gl, size.width, size.height, composerRef, fluidPass]);
+
+ // Gating loop (original useFrame priority 0).
+ const coveredRef = useRef(false);
+ const flareWasEnabledRef = useRef(true);
+ const pointerDeltaRef = useRef(new Vector2(0, 0));
+ const pointerDevRef = useRef(new Vector2(-1, -1));
+ const trailUvRef = useRef(new Vector2(-1, -1));
+ const lastMovedAtRef = useRef(0);
+
+ useFrame((_state, delta) => {
+ const composer = composerRef.current;
+ if (!composer) return;
+
+ const r = sT(scrollBus.sP);
+ coveredRef.current = sD(
+ coveredRef.current,
+ r,
+ SOLID_EFFECT.opaqueThreshold,
+ SOLID_EFFECT.opaqueTolerance,
+ SOLID_EFFECT.hysteresis
+ );
+ const covered = coveredRef.current;
+
+ const sectionVisible = anySectionInViewport(BRIGHT_SOURCE_SECTIONS);
+ const flareEnabled = !covered && sectionVisible;
+ if (!flareWasEnabledRef.current && flareEnabled) flarePass.resetFlareCadence();
+ flareWasEnabledRef.current = flareEnabled;
+ flarePass.enabled = flareEnabled;
+
+ const pointerActive = insideRef.current && !isMobile && !reducedMotion;
+ const dpr = dprRef.current;
+ if (pointerActive) {
+ const e = pointerUv.x * size.width * dpr;
+ const t = pointerUv.y * size.height * dpr;
+ if (pointerDevRef.current.x >= 0 && pointerDevRef.current.y >= 0) {
+ pointerDeltaRef.current.set(e - pointerDevRef.current.x, t - pointerDevRef.current.y);
+ } else {
+ pointerDeltaRef.current.set(0, 0);
+ }
+ pointerDevRef.current.set(e, t);
+ } else {
+ pointerDeltaRef.current.multiplyScalar(0.9);
+ }
+
+ const movedSq = pointerActive ? pointerDeltaRef.current.lengthSq() : 0;
+ const now = performance.now();
+ if (movedSq > 1) lastMovedAtRef.current = now;
+ const idle = now - lastMovedAtRef.current > 600;
+ const fluidEnabled = !isMobile && !reducedMotion && !covered && !idle;
+ const trailEnabled = covered;
+
+ if (!flareEnabled || fluidEnabled || trailEnabled) {
+ flarePass.renderToScreen = false;
+ fluidPass.enabled = true;
+ fluidPass.renderToScreen = true;
+ } else {
+ flarePass.renderToScreen = true;
+ fluidPass.enabled = false;
+ }
+ fluidPass.setEffectEnabled(fluidEnabled);
+ fluidPass.setPointerOverlayEnabled(trailEnabled);
+ fluidPass.setPointer(pointerDevRef.current);
+ fluidPass.setPointerDelta(pointerDeltaRef.current);
+ if (pointerActive) trailUvRef.current.set(pointerUv.x, pointerUv.y);
+ else trailUvRef.current.set(-1, -1);
+ fluidPass.updatePointer(trailUvRef.current, pointerActive && trailEnabled, delta);
+ });
+
+ return (
+
+
+
+
+ );
+}
+
+export default PostFX;
diff --git a/components/webgl/PostFxPasses.ts b/components/webgl/PostFxPasses.ts
new file mode 100644
index 0000000..bcbed92
--- /dev/null
+++ b/components/webgl/PostFxPasses.ts
@@ -0,0 +1,869 @@
+import {
+ HalfFloatType,
+ LinearFilter,
+ LinearSRGBColorSpace,
+ RGBAFormat,
+ ShaderMaterial,
+ UnsignedByteType,
+ Vector2,
+ WebGLRenderTarget,
+ Color,
+ MathUtils,
+} from "three";
+import { Pass } from "postprocessing";
+
+/**
+ * Hero post FX passes — 1:1 port of the original classes (chunk
+ * 4d3f3b68dbbde33a.js):
+ *
+ * - `cT` → LensFlarePass: bright-pass star streaks rendered into a
+ * downsampled target every `flareStride` frames, composited as
+ * base + flare.
+ * - `cI` → PointerTrailState: 16-cell pointer trail state (14 segments
+ * active, strength decays with damp 2).
+ * - `cO` → FluidPushPass: GPU fluid sim (curl → vorticity+pointer splat →
+ * divergence → pressure Jacobi → gradient subtract → advection) whose
+ * velocity field refracts the composed frame (4-tap chromatic
+ * displacement + spectral highlight) and draws the pointer dot trail.
+ * - `cP`/`ck`/`cS`: RT factory, clip-space material factory, shared vertex.
+ */
+
+const cS = `varying vec2 vUv;void main(){vUv=position.xy*0.5+0.5;gl_Position=vec4(position.xy,1.0,1.0);}`;
+
+function cP(e: number, t: number) {
+ return new WebGLRenderTarget(e, t, {
+ depthBuffer: false,
+ stencilBuffer: false,
+ minFilter: LinearFilter,
+ magFilter: LinearFilter,
+ format: RGBAFormat,
+ type: HalfFloatType,
+ });
+}
+
+function ck(fragmentShader: string, uniforms: Record) {
+ return new ShaderMaterial({
+ uniforms,
+ vertexShader: `varying vec2 vUv;
+
+void main() {
+ vUv = position.xy * 0.5 + 0.5;
+ gl_Position = vec4(position.xy, 1.0, 1.0);
+}`,
+ fragmentShader,
+ depthTest: false,
+ depthWrite: false,
+ transparent: false,
+ toneMapped: false,
+ });
+}
+
+const FLARE_FRAG_BASE = `uniform sampler2D tDiffuse;
+uniform vec2 uResolution;
+uniform float uEnabled;
+uniform float uIntensity;
+uniform float uThreshold;
+uniform float uStreakScale;
+uniform float uHotspotPower;
+uniform float uGate;
+uniform float uStarRays;
+
+uniform vec3 uTailColor;
+
+const float TAIL_MIX = 1.0;
+const float TAIL_START = 0.0;
+const float TAIL_FALLOFF = 0.5;
+
+varying vec2 vUv;
+
+float luma(vec3 c) {
+ return dot(c, vec3(0.2126, 0.7152, 0.0722));
+}
+
+float brightMask(float lum) {
+ // 适配 LDR/sRGB:阈值一般在 0.8~0.95
+ float x = max(lum - uThreshold, 0.0);
+ float m = x / max(1.0 - uThreshold, 1e-5);
+ // smoothstep(0,1,m) 的多项式形式(避免多余 clamp;smoothstep 本身会夹紧)
+ m = clamp(m, 0.0, 1.0);
+ m = m * m * (3.0 - 2.0 * m);
+
+ float hp = max(uHotspotPower, 1.0);
+ if (hp > 1.01) {
+ m = pow(m, hp);
+ }
+
+ // 线性软门控:减少断裂感且便宜
+ float gate = clamp(uGate, 0.0, 1.0);
+ float gm = (m - gate) / max(1.0 - gate, 1e-5);
+ gm = clamp(gm, 0.0, 1.0);
+ return m * gm;
+}
+
+vec3 sampleBright(vec2 uv) {
+ vec3 c = texture2D(tDiffuse, uv).rgb;
+ return c * brightMask(luma(c));
+}
+
+vec3 streak(vec2 dirPx) {
+ vec3 acc = vec3(0.0);
+ // 固定 8 次采样:用“半步相位”打散离散采样间隙(更像连续直线),且不引入 sin/exp
+ vec2 pixel = floor(vUv * uResolution);
+ float h = fract(52.9829189 * fract(dot(pixel, vec2(0.06711056, 0.00583715))));
+ float phase = step(0.5, h) * 0.5; // 0 或 0.5(半步)
+ for (int i = 1; i <= 8; i++) {
+ float fi = float(i);
+ // 增加步长以维持拖尾长度 (原 1 -> 1.5)
+ float dist = fi * 1.5 + phase;
+ // 放缓衰减:视觉上更“长”,同时保持成本低
+ float w = 1.0 / (1.0 + dist * 0.22);
+ w *= w;
+
+ // 末端颜色:中心偏白,越靠近末端越靠近 uTailColor
+ float t = clamp(dist / 8.0, 0.0, 1.0);
+ float start = clamp(TAIL_START, 0.0, 0.95);
+ float tt = clamp((t - start) / max(1.0 - start, 1e-5), 0.0, 1.0);
+ tt = pow(tt, max(TAIL_FALLOFF, 0.001));
+ vec3 ramp = mix(vec3(1.0), uTailColor, clamp(TAIL_MIX, 0.0, 1.0) * tt);
+
+ vec2 o = dirPx * dist;
+ acc += sampleBright(vUv + o) * (w * ramp);
+ acc += sampleBright(vUv - o) * (w * ramp);
+ }
+ return acc;
+}
+
+void main() {
+ vec3 flare = vec3(0.0);
+ if (uEnabled >= 0.5 && uIntensity > 0.0001) {
+ vec3 base = texture2D(tDiffuse, vUv).rgb;
+ vec2 px = (1.0 / max(uResolution, vec2(1.0))) * uStreakScale;
+
+ // 中心热点(让亮点像“星芒”有核心)
+ flare += base * brightMask(luma(base)) * 1.2;
+ // 4/6/8 芒:每条 streak 是“一条线”(包含正反两个方向)
+ // 4 芒:0° + 90°(十字)
+ // 6 芒:0° + (+60°) + (-60°)(三条线)
+ // 8 芒:0° + 90° + (+45°) + (-45°)
+ if (uStarRays >= 7.5) {
+ // 8 rays
+ flare += streak(vec2(px.x, 0.0));
+ flare += streak(vec2(0.0, px.y));
+ const float c45 = 0.70710678;
+ flare += streak(vec2(px.x * c45, px.y * c45));
+ flare += streak(vec2(px.x * c45, -px.y * c45));
+ } else if (uStarRays >= 5.5) {
+ // 6 rays(整体旋转 30°,保证有一根竖线):90° + (+30°) + (-30°)
+ flare += streak(vec2(0.0, px.y));
+ const float c30 = 0.8660254;
+ const float s30 = 0.5;
+ flare += streak(vec2(px.x * c30, px.y * s30));
+ flare += streak(vec2(px.x * c30, -px.y * s30));
+ } else {
+ // 4 rays
+ flare += streak(vec2(px.x, 0.0));
+ flare += streak(vec2(0.0, px.y));
+ }
+
+ // 保持核心尽量中性(末端颜色由 streak 内的 ramp 控制)
+ }
+
+ flare *= (uIntensity * 0.75);
+ gl_FragColor = vec4(flare, 1.0);
+}`;
+
+const FLARE_COMPOSITE_FRAG = `uniform sampler2D tBase;
+uniform sampler2D tFlare;
+
+varying vec2 vUv;
+
+void main() {
+ vec3 base = texture2D(tBase, vUv).rgb;
+ vec3 flare = texture2D(tFlare, vUv).rgb;
+
+ gl_FragColor = vec4(base + flare, 1.0);
+}`;
+
+export class LensFlarePass extends Pass {
+ flareScale: number;
+ flareStride: number;
+ flareFrame = 0;
+ width = 1;
+ height = 1;
+ flareTarget: WebGLRenderTarget;
+ flareMaterial: ShaderMaterial;
+ compositeMaterial: ShaderMaterial;
+
+ constructor({
+ flareDownsample = 0.5,
+ flareStride = 2,
+ starRays,
+ intensity,
+ threshold,
+ streakScale,
+ hotspotPower,
+ gate,
+ tailColor,
+ }: {
+ flareDownsample?: number;
+ flareStride?: number;
+ starRays: number;
+ intensity: number;
+ threshold: number;
+ streakScale: number;
+ hotspotPower: number;
+ gate: number;
+ tailColor: string;
+ }) {
+ super("LensFlarePass");
+ this.flareScale = Math.min(1, Math.max(0.2, flareDownsample));
+ this.flareStride = Math.max(1, Math.floor(flareStride));
+ this.flareTarget = new WebGLRenderTarget(1, 1, {
+ minFilter: LinearFilter,
+ magFilter: LinearFilter,
+ format: RGBAFormat,
+ type: UnsignedByteType,
+ depthBuffer: false,
+ stencilBuffer: false,
+ generateMipmaps: false,
+ });
+ this.flareTarget.texture.colorSpace = LinearSRGBColorSpace;
+ this.flareTarget.texture.generateMipmaps = false;
+ this.flareTarget.texture.name = "LensFlarePass.Target";
+ this.flareMaterial = new ShaderMaterial({
+ uniforms: {
+ tDiffuse: { value: null },
+ uResolution: { value: new Vector2(1, 1) },
+ uEnabled: { value: 1 },
+ uStarRays: { value: starRays },
+ uIntensity: { value: intensity },
+ uThreshold: { value: threshold },
+ uStreakScale: { value: streakScale },
+ uHotspotPower: { value: hotspotPower },
+ uGate: { value: gate },
+ uTailColor: { value: new Color(tailColor) },
+ },
+ vertexShader: cS,
+ fragmentShader: FLARE_FRAG_BASE,
+ depthTest: false,
+ depthWrite: false,
+ transparent: false,
+ toneMapped: false,
+ });
+ this.compositeMaterial = new ShaderMaterial({
+ uniforms: {
+ tBase: { value: null },
+ tFlare: { value: null },
+ },
+ vertexShader: cS,
+ fragmentShader: FLARE_COMPOSITE_FRAG,
+ depthTest: false,
+ depthWrite: false,
+ transparent: false,
+ toneMapped: false,
+ });
+ this.fullscreenMaterial = this.compositeMaterial;
+ }
+
+ setParams({
+ starRays,
+ intensity,
+ threshold,
+ streakScale,
+ hotspotPower,
+ gate,
+ tailColor,
+ }: {
+ starRays: number;
+ intensity: number;
+ threshold: number;
+ streakScale: number;
+ hotspotPower: number;
+ gate: number;
+ tailColor: string;
+ }) {
+ this.flareMaterial.uniforms.uStarRays.value = starRays;
+ this.flareMaterial.uniforms.uIntensity.value = intensity;
+ this.flareMaterial.uniforms.uThreshold.value = threshold;
+ this.flareMaterial.uniforms.uStreakScale.value = streakScale;
+ this.flareMaterial.uniforms.uHotspotPower.value = hotspotPower;
+ this.flareMaterial.uniforms.uGate.value = gate;
+ (this.flareMaterial.uniforms.uTailColor.value as Color).set(tailColor);
+ }
+
+ setFlareDownsample(e: number) {
+ this.flareScale = Math.min(1, Math.max(0.2, e));
+ this.resizeFlareTarget();
+ }
+
+ setFlareStride(e: number) {
+ this.flareStride = Math.max(1, Math.floor(e));
+ }
+
+ resetFlareCadence() {
+ this.flareFrame = 0;
+ }
+
+ setSize(e: number, t: number) {
+ this.width = Math.max(1, Math.floor(e));
+ this.height = Math.max(1, Math.floor(t));
+ this.flareMaterial.uniforms.uResolution.value.set(this.width, this.height);
+ this.resizeFlareTarget();
+ }
+
+ resizeFlareTarget() {
+ const e = Math.max(1, Math.floor(this.width * this.flareScale));
+ const t = Math.max(1, Math.floor(this.height * this.flareScale));
+ this.flareTarget.setSize(e, t);
+ this.resetFlareCadence();
+ }
+
+ render(renderer: any, inputBuffer: any, outputBuffer: any) {
+ if (inputBuffer) {
+ if (this.flareFrame % this.flareStride === 0) {
+ this.flareMaterial.uniforms.tDiffuse.value = inputBuffer.texture;
+ this.fullscreenMaterial = this.flareMaterial;
+ renderer.setRenderTarget(this.flareTarget);
+ renderer.clear();
+ renderer.render(this.scene, this.camera);
+ }
+ this.flareFrame += 1;
+ this.compositeMaterial.uniforms.tBase.value = inputBuffer.texture;
+ this.compositeMaterial.uniforms.tFlare.value = this.flareTarget.texture;
+ this.fullscreenMaterial = this.compositeMaterial;
+ renderer.setRenderTarget(this.renderToScreen ? null : outputBuffer);
+ renderer.clear();
+ renderer.render(this.scene, this.camera);
+ }
+ }
+
+ dispose() {
+ this.flareTarget.dispose();
+ this.flareMaterial.dispose();
+ this.compositeMaterial.dispose();
+ super.dispose();
+ }
+}
+
+const POINTER_COLOR = "#c0fe04";
+
+export class PointerTrailState {
+ cssResolution = new Vector2(1, 1);
+ devicePixelRatio = 1;
+ trail = Array.from({ length: 16 }, () => new Vector2(0.5, 0.5));
+ trailStrength = Array.from({ length: 16 }, () => 0);
+ lastPointerCell = new Vector2(-1, -1);
+ pixelSize = 16;
+ uniforms = {
+ uTrail: { value: this.trail },
+ uTrailStrength: { value: this.trailStrength },
+ uTrailCount: { value: 14 },
+ uPointerColor: { value: new Color(POINTER_COLOR) },
+ uPointerOpacity: { value: 1 },
+ uPointerDotRadius: { value: 0.8 },
+ uPointerPixelSize: { value: this.pixelSize },
+ uResolution: { value: new Vector2(1, 1) },
+ uDevicePixelRatio: { value: this.devicePixelRatio },
+ };
+
+ setColor(e: string) {
+ (this.uniforms.uPointerColor.value as Color).set(e);
+ }
+
+ setPixelSize(e: number) {
+ this.pixelSize = e;
+ this.uniforms.uPointerPixelSize.value = e;
+ }
+
+ setDisplayMetrics(e: number, t: number, r: number) {
+ this.cssResolution.set(Math.max(1, e), Math.max(1, t));
+ this.devicePixelRatio = Math.max(1, r);
+ this.uniforms.uDevicePixelRatio.value = this.devicePixelRatio;
+ }
+
+ setResolution(e: number, t: number) {
+ this.uniforms.uResolution.value.set(Math.max(1, e), Math.max(1, t));
+ }
+
+ updatePointer(e: Vector2, t: boolean, r: number) {
+ const n = Math.max(this.pixelSize / Math.max(this.cssResolution.x, 1), 1e-6);
+ const i = Math.max(this.pixelSize / Math.max(this.cssResolution.y, 1), 1e-6);
+ for (let k = +!!t; k < 14; k += 1) {
+ this.trailStrength[k] = MathUtils.damp(this.trailStrength[k], 0, 2, r);
+ }
+ if (t) {
+ const cellX = Math.floor(e.x / n);
+ const cellY = Math.floor(e.y / i);
+ if (cellX !== this.lastPointerCell.x || cellY !== this.lastPointerCell.y) {
+ for (let k = 13; k > 0; k -= 1) {
+ this.trail[k].copy(this.trail[k - 1]);
+ this.trailStrength[k] = this.trailStrength[k - 1];
+ }
+ this.lastPointerCell.set(cellX, cellY);
+ }
+ this.trail[0].set(e.x, e.y);
+ this.trailStrength[0] = 1;
+ return;
+ }
+ this.lastPointerCell.set(-1, -1);
+ }
+}
+
+const CURL_FRAG = `uniform sampler2D uVelocity;
+uniform vec2 uTexelSize;
+varying vec2 vUv;
+
+void main() {
+ float left = texture2D(uVelocity, vUv - vec2(uTexelSize.x, 0.0)).y;
+ float right = texture2D(uVelocity, vUv + vec2(uTexelSize.x, 0.0)).y;
+ float top = texture2D(uVelocity, vUv + vec2(0.0, uTexelSize.y)).x;
+ float bottom = texture2D(uVelocity, vUv - vec2(0.0, uTexelSize.y)).x;
+ float value = 0.5 * (right - left - top + bottom);
+ gl_FragColor = vec4(value, 0.0, 0.0, 1.0);
+}`;
+
+const VORTICITY_FRAG = `uniform sampler2D uVelocity;
+uniform sampler2D uCurl;
+uniform vec2 uTexelSize;
+uniform vec2 uResolution;
+uniform vec2 uPointer;
+uniform vec2 uPointerDelta;
+uniform float uCurlStrength;
+uniform float uSplatRadius;
+uniform float uSplatForce;
+varying vec2 vUv;
+
+void main() {
+ float left = abs(texture2D(uCurl, vUv - vec2(uTexelSize.x, 0.0)).x);
+ float right = abs(texture2D(uCurl, vUv + vec2(uTexelSize.x, 0.0)).x);
+ float top = abs(texture2D(uCurl, vUv + vec2(0.0, uTexelSize.y)).x);
+ float bottom = abs(texture2D(uCurl, vUv - vec2(0.0, uTexelSize.y)).x);
+ float center = texture2D(uCurl, vUv).x;
+
+ vec2 force = vec2(top - bottom, right - left);
+ float forceLength = length(force);
+ if (forceLength > 0.0001) {
+ force /= forceLength;
+ } else {
+ force = vec2(0.0);
+ }
+
+ force *= uCurlStrength * center;
+ force.y *= -1.0;
+
+ vec2 velocity = texture2D(uVelocity, vUv).xy;
+ velocity += force * 0.016;
+ velocity = clamp(velocity, vec2(-1000.0), vec2(1000.0));
+
+ vec2 mouseUv = uPointer / max(uResolution, vec2(0.0001));
+ vec2 diff = vUv - mouseUv;
+ diff.x *= uResolution.x / max(uResolution.y, 0.0001);
+ float pointerMask = exp(-dot(diff, diff) / max(uSplatRadius, 0.0001));
+ velocity += (uPointerDelta / max(uResolution, vec2(0.0001))) * pointerMask * uSplatForce;
+
+ gl_FragColor = vec4(velocity, 0.0, 1.0);
+}`;
+
+const DIVERGENCE_FRAG = `uniform sampler2D uVelocity;
+uniform vec2 uTexelSize;
+varying vec2 vUv;
+
+void main() {
+ float left = texture2D(uVelocity, vUv - vec2(uTexelSize.x, 0.0)).x;
+ float right = texture2D(uVelocity, vUv + vec2(uTexelSize.x, 0.0)).x;
+ float top = texture2D(uVelocity, vUv + vec2(0.0, uTexelSize.y)).y;
+ float bottom = texture2D(uVelocity, vUv - vec2(0.0, uTexelSize.y)).y;
+ float divergence = 0.5 * (right - left + top - bottom);
+ gl_FragColor = vec4(divergence, 0.0, 0.0, 1.0);
+}`;
+
+const CLEAR_FRAG = `void main() {
+ gl_FragColor = vec4(0.0);
+}`;
+
+const PRESSURE_FRAG = `uniform sampler2D uPressure;
+uniform sampler2D uDivergence;
+uniform vec2 uTexelSize;
+varying vec2 vUv;
+
+void main() {
+ float left = texture2D(uPressure, vUv - vec2(uTexelSize.x, 0.0)).x;
+ float right = texture2D(uPressure, vUv + vec2(uTexelSize.x, 0.0)).x;
+ float top = texture2D(uPressure, vUv + vec2(0.0, uTexelSize.y)).x;
+ float bottom = texture2D(uPressure, vUv - vec2(0.0, uTexelSize.y)).x;
+ float divergence = texture2D(uDivergence, vUv).x;
+ float pressure = (left + right + top + bottom - divergence) * 0.25;
+ gl_FragColor = vec4(pressure, 0.0, 0.0, 1.0);
+}`;
+
+const GRADIENT_FRAG = `uniform sampler2D uVelocity;
+uniform sampler2D uPressure;
+uniform vec2 uTexelSize;
+varying vec2 vUv;
+
+void main() {
+ float left = texture2D(uPressure, vUv - vec2(uTexelSize.x, 0.0)).x;
+ float right = texture2D(uPressure, vUv + vec2(uTexelSize.x, 0.0)).x;
+ float top = texture2D(uPressure, vUv + vec2(0.0, uTexelSize.y)).x;
+ float bottom = texture2D(uPressure, vUv - vec2(0.0, uTexelSize.y)).x;
+ vec2 velocity = texture2D(uVelocity, vUv).xy;
+ velocity -= vec2(right - left, top - bottom);
+ gl_FragColor = vec4(velocity, 0.0, 1.0);
+}`;
+
+const ADVECT_FRAG = `uniform sampler2D uVelocity;
+uniform sampler2D uProjectedVelocity;
+uniform vec2 uTexelSize;
+uniform float uDissipation;
+varying vec2 vUv;
+
+void main() {
+ vec2 velocity = texture2D(uProjectedVelocity, vUv).xy;
+ vec2 coord = clamp(vUv - velocity * uTexelSize * 0.016, 0.0, 1.0);
+ vec2 advected = texture2D(uProjectedVelocity, coord).xy;
+ advected /= 1.0 + uDissipation * 0.016;
+ gl_FragColor = vec4(advected, 0.0, 1.0);
+}`;
+
+const DISPLAY_FRAG_HEAD = `uniform sampler2D tDiffuse;
+uniform sampler2D uVelocity;
+uniform vec2 uSimSize;
+uniform float uDisplacementStrength;
+uniform float uChromaticBoost;
+uniform float uEffectEnabled;
+
+vec3 spectrum(float x) {
+ return cos((x - vec3(0.0, 0.5, 1.0)) * vec3(0.6, 1.0, 0.5) * 3.14);
+}
+
+vec4 getFluidDisplayColor(vec2 uv) {
+ vec2 velocity = texture2D(uVelocity, uv).xy;
+ float effectEnabled = step(0.5, uEffectEnabled);
+ vec2 displacement = velocity / max(uSimSize, vec2(1.0)) * uDisplacementStrength * effectEnabled;
+ float velocityMagnitude = length(displacement);
+
+ const int samples = 4; // 采样次数
+ vec4 color = vec4(0.0);
+ vec3 weightSum = vec3(0.0);
+
+ for (int index = 0; index < samples; index++) {
+ float t = float(index) / float(samples - 1);
+ vec3 weight = max(vec3(0.0), cos((t - vec3(0.0, 0.5, 1.0)) * 3.14159 * 0.5));
+ vec4 sampleColor = texture2D(tDiffuse, clamp(uv - displacement * 0.3 * (t + 0.3) * velocityMagnitude, 0.0, 1.0));
+ color.rgb += sampleColor.rgb * weight;
+ color.a += sampleColor.a * (weight.r + weight.g + weight.b) / 3.0;
+ weightSum += weight;
+ }
+
+ color.rgb /= max(weightSum, vec3(0.0001));
+ color.a /= max((weightSum.r + weightSum.g + weightSum.b) / 3.0, 0.0001);
+
+ vec3 spectralHighlight = spectrum(sin(velocityMagnitude * 2.0) * 0.4 + 0.6);
+ color.rgb += spectralHighlight * smoothstep(0.2, 0.8, velocityMagnitude) * 0.5 * uChromaticBoost * effectEnabled;
+
+ return color;
+}
+
+uniform vec2 uTrail[16];
+uniform float uTrailStrength[16];
+uniform float uTrailCount;
+uniform vec3 uPointerColor;
+uniform float uPointerOpacity;
+uniform float uPointerDotRadius;
+uniform float uPointerPixelSize;
+uniform vec2 uResolution;
+uniform float uDevicePixelRatio;
+
+float cellEquals(vec2 a, vec2 b) {
+ vec2 d = abs(a - b);
+ return 1.0 - step(0.5, max(d.x, d.y));
+}
+
+vec4 applyPointerOverlay(vec2 uv, vec4 baseColor) {
+ float cssPixelSize = uPointerPixelSize * max(uDevicePixelRatio, 1.0);
+ vec2 normalizedPixelSize = vec2(
+ cssPixelSize / max(uResolution.x, 1.0),
+ cssPixelSize / max(uResolution.y, 1.0)
+ );
+
+ vec2 safePixelSize = max(normalizedPixelSize, vec2(1e-6));
+ vec2 cellId = floor(uv / safePixelSize);
+ vec2 cellUV = fract(uv / safePixelSize);
+
+ float highlight = 0.0;
+ for (int i = 0; i < 16; i++) {
+ float enabled = step(float(i), uTrailCount - 1.0);
+ vec2 pointerCell = floor(uTrail[i] / safePixelSize);
+ float isSame = cellEquals(cellId, pointerCell);
+ float weight = clamp(uTrailStrength[i], 0.0, 1.0);
+ highlight = max(highlight, enabled * isSame * weight);
+ }
+
+ float distToCenter = distance(cellUV, vec2(0.5));
+ float aa = fwidth(distToCenter) * 1.5;
+ float radius = clamp(uPointerDotRadius, 0.0, 1.0);
+ float circleMask = smoothstep(radius, radius - aa, distToCenter);
+ float overlayAlpha = circleMask * highlight * clamp(uPointerOpacity, 0.0, 1.0);
+ baseColor.rgb = mix(baseColor.rgb, uPointerColor, overlayAlpha);
+
+ return baseColor;
+}
+
+varying vec2 vUv;
+
+void main() {
+ vec4 color = getFluidDisplayColor(vUv);
+ gl_FragColor = applyPointerOverlay(vUv, color);
+}`;
+
+export class FluidPushPass extends Pass {
+ simResolution: number;
+ simWidth = 1;
+ simHeight = 1;
+ viewportWidth = 1;
+ viewportHeight = 1;
+ pressureIterations: number;
+ effectEnabled = true;
+ pointerOverlayEnabled = true;
+ resolution = new Vector2(1, 1);
+ pointerOverlay = new PointerTrailState();
+ pointer = new Vector2(-1, -1);
+ pointerDelta = new Vector2(0, 0);
+ velocityRead: WebGLRenderTarget = cP(1, 1);
+ velocityWrite: WebGLRenderTarget = cP(1, 1);
+ curlTarget: WebGLRenderTarget = cP(1, 1);
+ vortTarget: WebGLRenderTarget = cP(1, 1);
+ divergenceTarget: WebGLRenderTarget = cP(1, 1);
+ pressureA: WebGLRenderTarget = cP(1, 1);
+ pressureB: WebGLRenderTarget = cP(1, 1);
+ projectedVelocityTarget: WebGLRenderTarget = cP(1, 1);
+ curlMaterial: ShaderMaterial;
+ vorticityMaterial: ShaderMaterial;
+ divergenceMaterial: ShaderMaterial;
+ clearMaterial: ShaderMaterial;
+ pressureMaterial: ShaderMaterial;
+ gradientMaterial: ShaderMaterial;
+ advectMaterial: ShaderMaterial;
+ displayMaterial: ShaderMaterial;
+
+ constructor({
+ strength = 0.3,
+ radius = 1.5,
+ velocityScale = 1,
+ chromaticStrength = 0.002,
+ pressureIterations = 4,
+ curlStrength = 0,
+ velocityDissipation = 3,
+ simResolution = 160,
+ } = {}) {
+ super("FluidPushPass");
+ this.simResolution = simResolution;
+ this.pressureIterations = pressureIterations;
+ const s = new Vector2(1, 1);
+ const u = new Vector2(1, 1);
+
+ this.curlMaterial = ck(CURL_FRAG, {
+ uVelocity: { value: null },
+ uTexelSize: { value: s.clone() },
+ });
+ this.vorticityMaterial = ck(VORTICITY_FRAG, {
+ uVelocity: { value: null },
+ uCurl: { value: null },
+ uTexelSize: { value: s.clone() },
+ uResolution: { value: u.clone() },
+ uPointer: { value: this.pointer },
+ uPointerDelta: { value: this.pointerDelta },
+ uCurlStrength: { value: curlStrength },
+ uSplatRadius: { value: Math.max(0.002 * radius, 5e-4) },
+ uSplatForce: { value: Math.max(3e3 * velocityScale, 0) },
+ });
+ this.divergenceMaterial = ck(DIVERGENCE_FRAG, {
+ uVelocity: { value: null },
+ uTexelSize: { value: s.clone() },
+ });
+ this.clearMaterial = ck(CLEAR_FRAG, {});
+ this.pressureMaterial = ck(PRESSURE_FRAG, {
+ uPressure: { value: null },
+ uDivergence: { value: null },
+ uTexelSize: { value: s.clone() },
+ });
+ this.gradientMaterial = ck(GRADIENT_FRAG, {
+ uVelocity: { value: null },
+ uPressure: { value: null },
+ uTexelSize: { value: s.clone() },
+ });
+ this.advectMaterial = ck(ADVECT_FRAG, {
+ uVelocity: { value: null },
+ uProjectedVelocity: { value: null },
+ uTexelSize: { value: s.clone() },
+ uDissipation: { value: velocityDissipation },
+ });
+ this.displayMaterial = ck(
+ DISPLAY_FRAG_HEAD,
+ {
+ tDiffuse: { value: null },
+ uVelocity: { value: null },
+ uSimSize: { value: new Vector2(simResolution, simResolution) },
+ uDisplacementStrength: { value: Math.max(strength / 0.3, 0) },
+ uChromaticBoost: { value: Math.max(chromaticStrength / 0.004, 0) },
+ uEffectEnabled: { value: 1 },
+ ...this.pointerOverlay.uniforms,
+ }
+ );
+ this.fullscreenMaterial = this.displayMaterial;
+ }
+
+ setEffectEnabled(e: boolean) {
+ this.effectEnabled = e;
+ this.displayMaterial.uniforms.uEffectEnabled.value = e ? 1 : 0;
+ }
+
+ setPointerOverlayEnabled(e: boolean) {
+ this.pointerOverlayEnabled = e;
+ this.displayMaterial.uniforms.uPointerOpacity.value = e ? 1 : 0;
+ }
+
+ setPointerColor(e: string) {
+ this.pointerOverlay.setColor(e);
+ }
+
+ setPointerPixelSize(e: number) {
+ this.pointerOverlay.setPixelSize(e);
+ if (this.pointerOverlayEnabled) {
+ this.displayMaterial.uniforms.uPointerOpacity.value = 1;
+ }
+ }
+
+ setDisplayMetrics(e: number, t: number, r: number) {
+ this.pointerOverlay.setDisplayMetrics(e, t, r);
+ }
+
+ updatePointer(e: Vector2, t: boolean, r: number) {
+ this.pointerOverlay.updatePointer(e, t, r);
+ }
+
+ setPointer(e: Vector2) {
+ this.pointer.copy(e);
+ }
+
+ setPointerDelta(e: Vector2) {
+ this.pointerDelta.copy(e);
+ }
+
+ setSize(e: number, t: number) {
+ this.viewportWidth = Math.max(1, e);
+ this.viewportHeight = Math.max(1, t);
+ const r = this.simResolution;
+ const n = this.viewportWidth / this.viewportHeight;
+ if (n > 1) {
+ this.simWidth = Math.round(r * n);
+ this.simHeight = r;
+ } else {
+ this.simWidth = r;
+ this.simHeight = Math.round(r / Math.max(n, 1e-4));
+ }
+ this.resizeRenderTarget(this.velocityRead);
+ this.resizeRenderTarget(this.velocityWrite);
+ this.resizeRenderTarget(this.curlTarget);
+ this.resizeRenderTarget(this.vortTarget);
+ this.resizeRenderTarget(this.divergenceTarget);
+ this.resizeRenderTarget(this.pressureA);
+ this.resizeRenderTarget(this.pressureB);
+ this.resizeRenderTarget(this.projectedVelocityTarget);
+
+ const i = 1 / this.simWidth;
+ const o = 1 / this.simHeight;
+ const a = new Vector2(this.viewportWidth, this.viewportHeight);
+ const l = new Vector2(this.simWidth, this.simHeight);
+ this.resolution.copy(a);
+ this.pointerOverlay.setResolution(this.viewportWidth, this.viewportHeight);
+ for (const m of [
+ this.curlMaterial,
+ this.vorticityMaterial,
+ this.divergenceMaterial,
+ this.pressureMaterial,
+ this.gradientMaterial,
+ this.advectMaterial,
+ ]) {
+ if (m.uniforms.uTexelSize) (m.uniforms.uTexelSize.value as Vector2).set(i, o);
+ }
+ (this.vorticityMaterial.uniforms.uResolution.value as Vector2).copy(a);
+ (this.displayMaterial.uniforms.uSimSize.value as Vector2).copy(l);
+ }
+
+ render(renderer: any, inputBuffer: any, outputBuffer: any) {
+ if (!inputBuffer) return;
+ const n = renderer.getRenderTarget();
+ if (this.effectEnabled) {
+ this.curlMaterial.uniforms.uVelocity.value = this.velocityRead.texture;
+ this.renderMaterial(renderer, this.curlMaterial, this.curlTarget);
+ this.vorticityMaterial.uniforms.uVelocity.value = this.velocityRead.texture;
+ this.vorticityMaterial.uniforms.uCurl.value = this.curlTarget.texture;
+ this.renderMaterial(renderer, this.vorticityMaterial, this.vortTarget);
+ this.divergenceMaterial.uniforms.uVelocity.value = this.vortTarget.texture;
+ this.renderMaterial(renderer, this.divergenceMaterial, this.divergenceTarget);
+ this.renderMaterial(renderer, this.clearMaterial, this.pressureA);
+ let t = this.pressureA;
+ let r = this.pressureB;
+ for (let k = 0; k < this.pressureIterations; k += 1) {
+ this.pressureMaterial.uniforms.uPressure.value = t.texture;
+ this.pressureMaterial.uniforms.uDivergence.value = this.divergenceTarget.texture;
+ this.renderMaterial(renderer, this.pressureMaterial, r);
+ const tmp = t;
+ t = r;
+ r = tmp;
+ }
+ this.gradientMaterial.uniforms.uVelocity.value = this.vortTarget.texture;
+ this.gradientMaterial.uniforms.uPressure.value = t.texture;
+ this.renderMaterial(renderer, this.gradientMaterial, this.projectedVelocityTarget);
+ this.advectMaterial.uniforms.uVelocity.value = this.velocityRead.texture;
+ this.advectMaterial.uniforms.uProjectedVelocity.value = this.projectedVelocityTarget.texture;
+ this.renderMaterial(renderer, this.advectMaterial, this.velocityWrite);
+ this.swapVelocityTargets();
+ }
+ this.displayMaterial.uniforms.tDiffuse.value = inputBuffer.texture;
+ this.displayMaterial.uniforms.uVelocity.value = this.velocityRead.texture;
+ this.fullscreenMaterial = this.displayMaterial;
+ renderer.setRenderTarget(this.renderToScreen ? null : outputBuffer);
+ renderer.clear();
+ renderer.render(this.scene, this.camera);
+ renderer.setRenderTarget(n);
+ }
+
+ dispose() {
+ this.velocityRead.dispose();
+ this.velocityWrite.dispose();
+ this.curlTarget.dispose();
+ this.vortTarget.dispose();
+ this.divergenceTarget.dispose();
+ this.pressureA.dispose();
+ this.pressureB.dispose();
+ this.projectedVelocityTarget.dispose();
+ this.curlMaterial.dispose();
+ this.vorticityMaterial.dispose();
+ this.divergenceMaterial.dispose();
+ this.clearMaterial.dispose();
+ this.pressureMaterial.dispose();
+ this.gradientMaterial.dispose();
+ this.advectMaterial.dispose();
+ this.displayMaterial.dispose();
+ super.dispose();
+ }
+
+ private renderMaterial(renderer: any, material: ShaderMaterial, target: WebGLRenderTarget) {
+ this.fullscreenMaterial = material;
+ renderer.setRenderTarget(target);
+ renderer.clear();
+ renderer.render(this.scene, this.camera);
+ }
+
+ private resizeRenderTarget(target: WebGLRenderTarget) {
+ target.setSize(this.simWidth, this.simHeight);
+ }
+
+ private swapVelocityTargets() {
+ const e = this.velocityRead;
+ this.velocityRead = this.velocityWrite;
+ this.velocityWrite = e;
+ }
+}