ce08eabf62
- BackgroundFX: 1:1 移植原版 sz 多通道程序化背景(vignette→swirl→sine→shatter→bokeh→output,30% 分辨率 ping-pong,指针跟随、进度门控跳帧) - DotOverlay: 移植原版 s1 滚动点阵遮罩(renderOrder 10,uOpacity=sT(sP))——贴纸随滚动溶入页面底色的原版机制 - LoadingCurtain+LoadingOverlay: 移植原版入场流程(径向圆洞幕布 + MaskedDots 点阵化后处理 + 居中进度条,字体/重资产 5:5 计时轴) - sP 修正: f2 无 footer 时取 1(原版语义),相机 lerp(24,32,sP) 拉远恢复;视差 y 系数修正为 0.6 - 相机: ready 门控 intro dolly(lerp(A+8,A,customCubic),1.2s)+ 鼠标视差(lag .18/leave .05) - GlassModels: cursor 贴色修正为亮色主题 #009dff/#009dff;新增指针跟随高光控制器 ca() - 刷新回顶: scrollRestoration=manual + pageshow(bfcache) 双 rAF 重置 + lenis immediate - 新增 lib/pointer.ts、lib/loadingBus.ts、lib/customCubic.ts;loadingBus 打点: 模型/贴纸/about 图
518 lines
17 KiB
TypeScript
518 lines
17 KiB
TypeScript
"use client";
|
|
|
|
import { Suspense, useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
import { useFrame, useLoader, useThree } from "@react-three/fiber";
|
|
import * as THREE from "three";
|
|
import { scrollBus } from "@/lib/scrollBus";
|
|
import { useIsMobile } from "@/lib/useIsMobile";
|
|
import { imageLayerTargets } from "@/lib/imageLayers";
|
|
import { loadingBus } from "@/lib/loadingBus";
|
|
|
|
/**
|
|
* DOM-tracked WebGL image layers — 1:1 port of the original `cE`/`cw`
|
|
* (image layer), `cz` (target-rect tracker) and `cB` (texture prep) from
|
|
* chunk 4d3f3b68dbbde33a.js.
|
|
*
|
|
* Each layer is a fullscreen clip-space quad whose fragment shader samples
|
|
* the image inside the screen-space rect of a tracked DOM element:
|
|
* - photo-negative reveal: uPolarityPositive ramps 0→1 over 0.8s with
|
|
* easeInOutCubic each time the rect enters the viewport (inverted → normal)
|
|
* - scroll-velocity curl: max 6% horizontal squeeze, damped
|
|
* - hover: expanding square-dot mask (18px cell) crossfades to the hover
|
|
* texture over 0.42s, driven by pointerenter/leave on the target's parent
|
|
* - rect tracking: cached rect shifted by scroll delta, re-measured on the
|
|
* layer's measure frame (every 12th) or when potentially visible without a
|
|
* cache (original cz algorithm)
|
|
*/
|
|
|
|
const VERT = /* glsl */ `
|
|
varying vec2 vUv;
|
|
|
|
void main() {
|
|
vUv = uv;
|
|
gl_Position = vec4(position, 1.0);
|
|
}
|
|
`;
|
|
|
|
const FRAG = /* glsl */ `
|
|
uniform float uCurlStrength;
|
|
|
|
vec2 applyCurl(vec2 screenUv) {
|
|
float centered = 2.0 * screenUv.y - 1.0;
|
|
float profile = 1.0 - sqrt(max(0.0, 1.0 - centered * centered));
|
|
float uvScale = 1.0 - profile * uCurlStrength;
|
|
float distortedX = (screenUv.x - 0.5) * uvScale + 0.5;
|
|
return vec2(distortedX, screenUv.y);
|
|
}
|
|
|
|
uniform sampler2D map;
|
|
uniform sampler2D mapHover;
|
|
uniform vec4 uRect;
|
|
uniform float uPolarityPositive;
|
|
uniform float uLayerOpacity;
|
|
uniform float uRevealProgress;
|
|
uniform float uRevealSoftness;
|
|
uniform float uRevealDirection;
|
|
uniform float uHoverRevealProgress;
|
|
uniform float uDotPixelSize;
|
|
uniform vec2 uViewportPx;
|
|
varying vec2 vUv;
|
|
|
|
vec3 applyPolarity(vec3 rgb) {
|
|
float t = clamp(uPolarityPositive, 0.0, 1.0);
|
|
return mix(1.0 - rgb, rgb, t);
|
|
}
|
|
|
|
float hoverDotCoverage(vec2 screenUv) {
|
|
float hoverProgress = clamp(uHoverRevealProgress, 0.0, 1.0);
|
|
if (hoverProgress <= 0.0) return 0.0;
|
|
|
|
vec2 viewportPx = max(uViewportPx, vec2(1.0));
|
|
float dotPx = max(2.0, uDotPixelSize);
|
|
vec2 cellSizeUv = vec2(dotPx) / viewportPx;
|
|
vec2 safeCellSize = max(cellSizeUv, vec2(1.0 / 4096.0));
|
|
|
|
float rectWidthPx = max(uRect.z * uViewportPx.x, 1.0);
|
|
float rectHeightPx = max(uRect.w * uViewportPx.y, 1.0);
|
|
float rectAspect = max(rectWidthPx / rectHeightPx, 1e-5);
|
|
vec2 localUv = (screenUv - uRect.xy) / uRect.zw;
|
|
vec2 centered = localUv * 2.0 - 1.0;
|
|
centered.x *= rectAspect;
|
|
float distToCenter = length(centered);
|
|
|
|
float maxRadius = sqrt(1.0 + rectAspect * rectAspect);
|
|
float revealBand = max(length(safeCellSize) * 18.0, 0.08);
|
|
float revealRadius = hoverProgress * (maxRadius + revealBand);
|
|
float grow = clamp((revealRadius - distToCenter) / revealBand, 0.0, 1.0);
|
|
grow = smoothstep(0.0, 1.0, grow);
|
|
|
|
vec2 cellUv = fract(screenUv / safeCellSize);
|
|
vec2 cellFromCenter = abs(cellUv - vec2(0.5));
|
|
float squareExtent = mix(0.0, 0.5, grow);
|
|
float squareDist = max(cellFromCenter.x, cellFromCenter.y);
|
|
float squareAa = max(fwidth(squareDist), 0.0001) * 1.5;
|
|
if (squareExtent <= squareAa) {
|
|
return 0.0;
|
|
}
|
|
if (grow >= 0.999) {
|
|
return 1.0;
|
|
}
|
|
float squareMask = 1.0 - smoothstep(
|
|
squareExtent - squareAa,
|
|
squareExtent + squareAa,
|
|
squareDist
|
|
);
|
|
|
|
return squareMask;
|
|
}
|
|
|
|
vec4 sampleSourceRgba(vec2 localUv, float hoverCoverage) {
|
|
vec2 lu = clamp(localUv, 0.0, 1.0);
|
|
vec4 baseColor = texture2D(map, lu);
|
|
if (hoverCoverage < 0.001) return baseColor;
|
|
vec4 hoverColor = texture2D(mapHover, lu);
|
|
return mix(baseColor, hoverColor, clamp(hoverCoverage, 0.0, 1.0));
|
|
}
|
|
|
|
float edgeAaMask(vec2 uv, vec2 aaRef) {
|
|
vec2 edgeDist = min(uv, 1.0 - uv);
|
|
float xClip = smoothstep(0.0, aaRef.x, edgeDist.x);
|
|
float yClip = smoothstep(0.0, aaRef.y, edgeDist.y);
|
|
return xClip * yClip;
|
|
}
|
|
|
|
void main() {
|
|
vec2 distortedScreenUv = applyCurl(vUv);
|
|
vec2 revealLocalUv = (vUv - uRect.xy) / uRect.zw;
|
|
vec2 localUv = (distortedScreenUv - uRect.xy) / uRect.zw;
|
|
|
|
vec2 aa = max(fwidth(localUv), vec2(1e-5));
|
|
|
|
float revealProgress = clamp(uRevealProgress, 0.0, 1.0);
|
|
float revealMask = 1.0;
|
|
if (revealProgress <= 0.001) {
|
|
revealMask = 0.0;
|
|
} else if (revealProgress < 0.999) {
|
|
float revealCoord = uRevealDirection < 0.0 ? 1.0 - revealLocalUv.x : revealLocalUv.x;
|
|
float revealFeather = max(uRevealSoftness, 0.0);
|
|
revealMask = revealFeather <= 0.0
|
|
? step(revealCoord, revealProgress)
|
|
: 1.0 - smoothstep(
|
|
revealProgress - revealFeather,
|
|
revealProgress + revealFeather,
|
|
revealCoord
|
|
);
|
|
}
|
|
|
|
float hoverCov = hoverDotCoverage(vUv);
|
|
vec4 sourceColor = sampleSourceRgba(localUv, hoverCov);
|
|
float inside = edgeAaMask(localUv, aa);
|
|
float outA = sourceColor.a * inside * revealMask * clamp(uLayerOpacity, 0.0, 1.0);
|
|
if (outA < 0.001) {
|
|
discard;
|
|
}
|
|
|
|
vec3 sourcePolar = applyPolarity(sourceColor.rgb);
|
|
gl_FragColor = vec4(sourcePolar, outA);
|
|
|
|
#include <tonemapping_fragment>
|
|
#include <colorspace_fragment>
|
|
}
|
|
`;
|
|
|
|
const easeInOutCubic = (e: number) =>
|
|
e < 0.5 ? 4 * e * e * e : 1 - Math.pow(-2 * e + 2, 3) / 2;
|
|
|
|
type Rect = {
|
|
left: number;
|
|
top: number;
|
|
width: number;
|
|
height: number;
|
|
right: number;
|
|
bottom: number;
|
|
};
|
|
|
|
type ImageLayerDef = {
|
|
key: string;
|
|
imageUrl: string;
|
|
hoverImageUrl?: string;
|
|
};
|
|
|
|
/** Original cB: texture prep (color space, wrap, mip/filter, anisotropy). */
|
|
function prepareTexture(
|
|
texture: THREE.Texture,
|
|
gl: THREE.WebGLRenderer,
|
|
isMobile: boolean,
|
|
hasHover: boolean
|
|
) {
|
|
texture.colorSpace = THREE.SRGBColorSpace;
|
|
texture.needsUpdate = true;
|
|
texture.wrapS = THREE.ClampToEdgeWrapping;
|
|
texture.wrapT = THREE.ClampToEdgeWrapping;
|
|
if (hasHover) {
|
|
texture.generateMipmaps = false;
|
|
texture.minFilter = THREE.LinearFilter;
|
|
} else {
|
|
texture.minFilter = THREE.LinearMipmapLinearFilter;
|
|
}
|
|
texture.magFilter = THREE.LinearFilter;
|
|
const maxAnisotropy = gl.capabilities.getMaxAnisotropy?.() ?? 1;
|
|
texture.anisotropy = isMobile ? Math.min(4, maxAnisotropy) : Math.min(8, maxAnisotropy);
|
|
}
|
|
|
|
function usePrefersReducedMotion(): boolean {
|
|
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;
|
|
}
|
|
|
|
/** Scroll-velocity curl strength (original inline hook in cE). */
|
|
function useCurlStrength() {
|
|
const lastScrollRef = useRef<number | null>(null);
|
|
const strengthRef = useRef(0);
|
|
return useCallback((rawDelta: number) => {
|
|
const step = Math.max(1 / 240, Math.min(rawDelta, 0.1));
|
|
const scrollTop = scrollBus.current;
|
|
const prev = lastScrollRef.current;
|
|
lastScrollRef.current = scrollTop;
|
|
const speed = prev == null ? 0 : Math.abs(scrollTop - prev) / step;
|
|
const target = THREE.MathUtils.clamp(speed / 800, 0, 1);
|
|
const current = strengthRef.current;
|
|
const tau = target > current ? 0.025 : 0.175;
|
|
const next = current + (target - current) * (1 - Math.exp(-step / Math.max(tau, 1e-4)));
|
|
strengthRef.current = next;
|
|
return 0.06 * next;
|
|
}, []);
|
|
}
|
|
|
|
function ImageLayer({ def }: { def: ImageLayerDef }) {
|
|
const target = imageLayerTargets[def.key];
|
|
const meshRef = useRef<THREE.Mesh>(null!);
|
|
const material = useMemo(
|
|
() =>
|
|
new THREE.ShaderMaterial({
|
|
uniforms: {
|
|
map: { value: null },
|
|
mapHover: { value: null },
|
|
uRect: { value: new THREE.Vector4(0, 0, 1, 1) },
|
|
uCurlStrength: { value: 0 },
|
|
uPolarityPositive: { value: 0 },
|
|
uLayerOpacity: { value: 1 },
|
|
uRevealProgress: { value: 1 },
|
|
uRevealSoftness: { value: 0 },
|
|
uRevealDirection: { value: 1 },
|
|
uHoverRevealProgress: { value: 0 },
|
|
uDotPixelSize: { value: 18 },
|
|
uViewportPx: { value: new THREE.Vector2(1, 1) },
|
|
},
|
|
vertexShader: VERT,
|
|
fragmentShader: FRAG,
|
|
transparent: true,
|
|
toneMapped: false,
|
|
depthTest: false,
|
|
depthWrite: false,
|
|
polygonOffset: true,
|
|
polygonOffsetFactor: -1,
|
|
polygonOffsetUnits: -1,
|
|
alphaToCoverage: false,
|
|
}),
|
|
[]
|
|
);
|
|
useEffect(() => () => material.dispose(), [material]);
|
|
|
|
const hasHover = !!def.hoverImageUrl;
|
|
const size = useThree((s) => s.size);
|
|
const gl = useThree((s) => s.gl);
|
|
const isMobile = useIsMobile();
|
|
const reducedMotion = usePrefersReducedMotion();
|
|
|
|
// [base, hover ?? base] — same shape as the original useLoader call
|
|
const [base, hover] = useLoader(THREE.TextureLoader, [
|
|
def.imageUrl,
|
|
def.hoverImageUrl ?? def.imageUrl,
|
|
]);
|
|
|
|
// Hover pointer tracking on the target's parent (original effect).
|
|
const hoverActiveRef = useRef(false);
|
|
const hoverEnabledRef = useRef(false);
|
|
useEffect(() => {
|
|
if (!hasHover) {
|
|
hoverActiveRef.current = false;
|
|
return;
|
|
}
|
|
let cancelled = false;
|
|
let timeout = 0;
|
|
let detach: (() => void) | null = null;
|
|
let attached: HTMLElement | null = null;
|
|
const attach = (attempt: number) => {
|
|
if (cancelled) return;
|
|
const parent = target?.current?.parentElement;
|
|
if (!parent) {
|
|
if (attempt < 120) timeout = window.setTimeout(() => attach(attempt + 1), 50);
|
|
return;
|
|
}
|
|
if (attached === parent && detach) return;
|
|
detach?.();
|
|
attached = parent;
|
|
let removeMove: (() => void) | null = null;
|
|
const cleanup = () => {
|
|
removeMove?.();
|
|
removeMove = null;
|
|
};
|
|
const setHover = () => {
|
|
hoverActiveRef.current = true;
|
|
cleanup();
|
|
};
|
|
const onLeave = (e: PointerEvent) => {
|
|
if (e.pointerType !== "mouse") {
|
|
hoverActiveRef.current = false;
|
|
cleanup();
|
|
return;
|
|
}
|
|
const rect = parent.getBoundingClientRect();
|
|
if (e.clientX >= rect.left && e.clientX <= rect.right && e.clientY >= rect.top && e.clientY <= rect.bottom) {
|
|
if (removeMove) return;
|
|
const onMove = (ev: PointerEvent) => {
|
|
if (ev.pointerType !== "mouse") return;
|
|
const r = parent.getBoundingClientRect();
|
|
if (ev.clientX < r.left || ev.clientX > r.right || ev.clientY < r.top || ev.clientY > r.bottom) {
|
|
hoverActiveRef.current = false;
|
|
cleanup();
|
|
}
|
|
};
|
|
window.addEventListener("pointermove", onMove, { passive: true });
|
|
removeMove = () => window.removeEventListener("pointermove", onMove);
|
|
return;
|
|
}
|
|
hoverActiveRef.current = false;
|
|
};
|
|
const onFocus = () => {
|
|
hoverActiveRef.current = true;
|
|
};
|
|
const onFocusOut = (e: FocusEvent) => {
|
|
if (!parent.contains(e.relatedTarget as Node)) hoverActiveRef.current = false;
|
|
};
|
|
parent.addEventListener("pointerenter", setHover);
|
|
parent.addEventListener("pointerleave", onLeave);
|
|
parent.addEventListener("focusin", onFocus);
|
|
parent.addEventListener("focusout", onFocusOut);
|
|
detach = () => {
|
|
parent.removeEventListener("pointerenter", setHover);
|
|
parent.removeEventListener("pointerleave", onLeave);
|
|
parent.removeEventListener("focusin", onFocus);
|
|
parent.removeEventListener("focusout", onFocusOut);
|
|
cleanup();
|
|
detach = null;
|
|
attached = null;
|
|
};
|
|
};
|
|
const observer =
|
|
typeof MutationObserver !== "undefined"
|
|
? new MutationObserver(() => attach(0))
|
|
: null;
|
|
observer?.observe(document.body, { childList: true, subtree: true });
|
|
attach(0);
|
|
return () => {
|
|
cancelled = true;
|
|
window.clearTimeout(timeout);
|
|
observer?.disconnect();
|
|
detach?.();
|
|
hoverActiveRef.current = false;
|
|
};
|
|
}, [hasHover, target]);
|
|
|
|
// Textures: prep + uniform wiring (original effect after useLoader).
|
|
useEffect(() => {
|
|
if (!base) return;
|
|
prepareTexture(base, gl, isMobile, hasHover);
|
|
if (hover) prepareTexture(hover, gl, isMobile, hasHover);
|
|
material.uniforms.map.value = base;
|
|
material.uniforms.mapHover.value = hover ?? base;
|
|
material.uniforms.uLayerOpacity.value = 1;
|
|
material.uniforms.uRevealProgress.value = 1;
|
|
material.uniforms.uRevealSoftness.value = 0;
|
|
material.uniforms.uRevealDirection.value = 1;
|
|
material.uniforms.uHoverRevealProgress.value = 0;
|
|
hoverProgressRef.current = 0;
|
|
hoverEnabledRef.current = !!hover || !hasHover;
|
|
loadingBus.mark(`img:${def.key}`);
|
|
}, [base, hover, gl, isMobile, hasHover, material, def.key]);
|
|
|
|
const geometry = useMemo(() => new THREE.PlaneGeometry(2, 2), []);
|
|
useEffect(() => () => geometry.dispose(), [geometry]);
|
|
|
|
const hoverProgressRef = useRef(0);
|
|
const polarityRef = useRef(0);
|
|
const rectRef = useRef<Rect | null>(null);
|
|
const scrollRef = useRef({ top: 0, left: 0 });
|
|
const frameRef = useRef(0);
|
|
const getCurl = useCurlStrength();
|
|
|
|
useFrame((_state, rawDelta) => {
|
|
const frame = frameRef.current++;
|
|
|
|
// --- Original cz: cached rect shifted by scroll delta, re-measured on
|
|
// the layer's measure frame (frame % 12) or when potentially visible
|
|
// without a cache. ---
|
|
const scrollTop = scrollBus.current;
|
|
const dTop = scrollTop - scrollRef.current.top;
|
|
scrollRef.current.top = scrollTop;
|
|
const rect = rectRef.current;
|
|
if (dTop !== 0 && rect) {
|
|
rect.top -= dTop;
|
|
rect.bottom -= dTop;
|
|
}
|
|
const vh = Math.max(1, size.height);
|
|
const element = target?.current;
|
|
if (element) {
|
|
const measureWindow = Math.max(1.5 * vh, 600);
|
|
const potentiallyVisible =
|
|
!rect || (rect.bottom > -measureWindow && rect.top < vh + measureWindow);
|
|
const isMeasureFrame = frame % 12 === 0;
|
|
if (potentiallyVisible || isMeasureFrame) {
|
|
const box = element.getBoundingClientRect();
|
|
if (rect) {
|
|
rect.left = box.left;
|
|
rect.top = box.top;
|
|
rect.width = box.width;
|
|
rect.height = box.height;
|
|
rect.right = box.right;
|
|
rect.bottom = box.bottom;
|
|
} else {
|
|
rectRef.current = { ...box };
|
|
}
|
|
}
|
|
}
|
|
|
|
// --- Original cE useFrame body ---
|
|
const mesh = meshRef.current;
|
|
const hide = () => {
|
|
if (mesh) mesh.visible = false;
|
|
polarityRef.current = 0;
|
|
material.uniforms.uPolarityPositive.value = 0;
|
|
};
|
|
const cached = rectRef.current;
|
|
const hasMap = !!material.uniforms.map.value;
|
|
const W = Math.max(1, size.width);
|
|
const H = Math.max(1, size.height);
|
|
if (!cached || !hasMap) return hide();
|
|
const w = cached.width;
|
|
const h = cached.height;
|
|
if (w <= 0 || h <= 0) return hide();
|
|
const margin = 0.25 * H;
|
|
const near = cached.bottom > -margin && cached.top < H + margin;
|
|
const far = cached.bottom < -(2 * H) || cached.top > 3 * H;
|
|
if (!near || far) return hide();
|
|
|
|
const hoverStep = Math.min(rawDelta, 0.1) / 0.42;
|
|
const hoverTarget =
|
|
hasHover && hoverActiveRef.current && hoverEnabledRef.current ? 1 : 0;
|
|
let progress = hoverProgressRef.current;
|
|
if (hoverTarget) {
|
|
progress = Math.min(1, progress + hoverStep);
|
|
if (progress > 0.999) progress = 1;
|
|
} else {
|
|
progress = Math.max(0, progress - hoverStep);
|
|
if (progress < 0.001) progress = 0;
|
|
}
|
|
hoverProgressRef.current = progress;
|
|
if (mesh) mesh.visible = true;
|
|
|
|
const curl = getCurl(rawDelta);
|
|
material.uniforms.uLayerOpacity.value = 1;
|
|
material.uniforms.uHoverRevealProgress.value = hasHover
|
|
? 0.5 - 0.5 * Math.cos(Math.PI * THREE.MathUtils.clamp(progress, 0, 1))
|
|
: 0;
|
|
material.uniforms.uViewportPx.value.set(W, H);
|
|
|
|
let polarity: number;
|
|
if (cached.right <= 0 || cached.left >= W || cached.bottom <= 0 || cached.top >= H) {
|
|
polarityRef.current = 0;
|
|
polarity = 0;
|
|
} else if (reducedMotion) {
|
|
polarityRef.current = 1;
|
|
polarity = 1;
|
|
} else {
|
|
polarityRef.current = Math.min(1, polarityRef.current + rawDelta / 0.8);
|
|
polarity = easeInOutCubic(polarityRef.current);
|
|
}
|
|
material.uniforms.uRect.value.set(
|
|
cached.left / W,
|
|
1 - (cached.top + h) / H,
|
|
w / W,
|
|
h / H
|
|
);
|
|
material.uniforms.uCurlStrength.value = curl;
|
|
material.uniforms.uPolarityPositive.value = polarity;
|
|
});
|
|
|
|
return (
|
|
<mesh ref={meshRef} geometry={geometry} renderOrder={20} frustumCulled={false} material={material} />
|
|
);
|
|
}
|
|
|
|
/** Canvas-side entry: renders every registered image layer. */
|
|
export function ImageLayers() {
|
|
const defs = useMemo<ImageLayerDef[]>(() => {
|
|
const layers: ImageLayerDef[] = [];
|
|
if (imageLayerTargets.about) layers.push({ key: "about", imageUrl: "/img/m3.png" });
|
|
return layers;
|
|
}, []);
|
|
|
|
return (
|
|
<Suspense fallback={null}>
|
|
{defs.map((def) => (
|
|
<ImageLayer key={def.key} def={def} />
|
|
))}
|
|
</Suspense>
|
|
);
|
|
}
|
|
|
|
export default ImageLayers;
|