feat: add PostFX component and webgl PostFxPasses for enhanced visual effects
- Introduced PostFX component for managing post-processing effects including LensFlarePass and FluidPushPass. - Implemented LensFlarePass for star streak effects on bright pixels with customizable parameters. - Developed FluidPushPass for GPU fluid simulation, allowing pointer-driven splats and visual refractive effects. - Added utility functions for motion reduction detection and viewport section visibility checks. - Established a comprehensive rendering pipeline with dynamic adjustments based on device metrics and user interactions.
This commit is contained in:
@@ -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<any>(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 (
|
||||
<EffectComposer
|
||||
ref={composerRef}
|
||||
multisampling={0}
|
||||
autoClear
|
||||
renderPriority={998}
|
||||
frameBufferType={UnsignedByteType}
|
||||
>
|
||||
<primitive object={flarePass} />
|
||||
<primitive object={fluidPass} />
|
||||
</EffectComposer>
|
||||
);
|
||||
}
|
||||
|
||||
export default PostFX;
|
||||
Reference in New Issue
Block a user