feat: 更新玻璃模型和英雄场景,优化贴纸层的渲染逻辑

This commit is contained in:
2026-09-01 00:16:54 +08:00
parent 744ec0becc
commit e4a6ea937a
3 changed files with 112 additions and 64 deletions
+100 -55
View File
@@ -277,6 +277,14 @@ type GlassModelProps = {
tintEnabled?: boolean; tintEnabled?: boolean;
scrollSyncFactor?: number; scrollSyncFactor?: number;
floating?: boolean; floating?: boolean;
isMobile?: boolean;
/**
* Document-space geometry of the section the model belongs to.
* The banner sits at the top of the page and is h-dvh tall, so the defaults
* (top 0, height = viewport height) match the original "banner" section.
*/
sectionTopDocY?: number;
sectionHeightPx?: number;
}; };
function useFloatOffset(enabled: boolean) { function useFloatOffset(enabled: boolean) {
@@ -303,6 +311,9 @@ function GlassModel({
tintEnabled = false, tintEnabled = false,
scrollSyncFactor = 0.72, scrollSyncFactor = 0.72,
floating = true, floating = true,
isMobile = false,
sectionTopDocY = 0,
sectionHeightPx,
}: GlassModelProps) { }: GlassModelProps) {
const gltf = useLoader(GLTFLoader, url); const gltf = useLoader(GLTFLoader, url);
const groupRef = useRef<THREE.Group>(null!); const groupRef = useRef<THREE.Group>(null!);
@@ -332,7 +343,7 @@ function GlassModel({
} catch { } catch {
merged = null; merged = null;
} }
if (!merged && geometries.length === 1) merged = geometries[0]; if (!merged && geometries.length >= 1) merged = geometries[0];
if (!merged) return null; if (!merged) return null;
merged.computeBoundingBox(); merged.computeBoundingBox();
const bb = merged.boundingBox!; const bb = merged.boundingBox!;
@@ -347,7 +358,7 @@ function GlassModel({
}, [gltf]); }, [gltf]);
// Glass renders on layers 0+10: visible in the main pass, excluded from the // Glass renders on layers 0+10: visible in the main pass, excluded from the
// layer-1 FBO pass that feeds the refraction texture (no self-refraction). // layer-0-mask FBO pass that feeds the refraction texture (no self-refraction).
useEffect(() => { useEffect(() => {
meshLayerRef.current?.layers.enable(10); meshLayerRef.current?.layers.enable(10);
}, [mergedGeometry]); }, [mergedGeometry]);
@@ -361,7 +372,9 @@ function GlassModel({
: [bb.min.y, bb.max.y]; : [bb.min.y, bb.max.y];
}, [mergedGeometry]); }, [mergedGeometry]);
// Scene-texture FBO: render layer 1 (background + stickers) at half res // Scene-texture FBO: render layer 0 (background + stickers) at half res.
// Same as the original GlassSystem: one shared pass, camera mask forced to
// layer 0 so glass meshes (layer 10) are excluded from their own refraction.
const fbo = useMemo(() => { const fbo = useMemo(() => {
const dpr = gl.getPixelRatio(); const dpr = gl.getPixelRatio();
const w = Math.max(2, Math.floor((size.width * dpr) / 2)); const w = Math.max(2, Math.floor((size.width * dpr) / 2));
@@ -436,25 +449,36 @@ function GlassModel({
); );
}, [gl, size, uniforms]); }, [gl, size, uniforms]);
// Entrance animation: damp rotation from beforeRotation -> rotation -> afterRotation // Float bob: desktop only in the original (floatingMotion && !isMobileWidth)
const entrance = useRef({ started: false, elapsed: 0 }); const floatEnabled = floating && !isMobile;
const getFloat = useFloatOffset(floatEnabled);
const getFloat = useFloatOffset(floating); // Counter tilt: cancels rotationAxisTilt so the model's static orientation
// is upright; the tilt only skews the spin axis (original nests -tilt
// INSIDE the spin group).
const negatedTilt = useMemo<[number, number, number]>(
() => [-rotationAxisTilt[0], -rotationAxisTilt[1], -rotationAxisTilt[2]],
[rotationAxisTilt]
);
const rad = (v: number) => THREE.MathUtils.degToRad(v);
const before = beforeRotation ?? rotation;
useFrame((state, rawDelta) => { useFrame((state, rawDelta) => {
const delta = Math.min(rawDelta, 0.1); const delta = Math.min(rawDelta, 0.1);
// Render the scene (layer 1 only) into the FBO for refraction. // Render the scene (layer 0 mask) into the FBO for refraction.
// Background/sticker content must be on layer 1; glass models render on // Stickers/background live on layer 0; glass models render on layer 0+10
// layer 0 + 10 (10 excluded from the FBO pass to avoid self-refraction). // and are excluded by the layer-0 mask (no self-refraction). Original:
// `camera.layers.mask = 1; render; restore`.
const prevTarget = gl.getRenderTarget(); const prevTarget = gl.getRenderTarget();
const prevAutoClear = gl.autoClear; const prevAutoClear = gl.autoClear;
const cameraMask = camera.layers.mask; const prevMask = camera.layers.mask;
gl.setRenderTarget(fbo); gl.setRenderTarget(fbo);
gl.clear(); gl.clear();
camera.layers.set(1); camera.layers.set(0);
gl.render(scene, camera); gl.render(scene, camera);
camera.layers.mask = cameraMask; camera.layers.mask = prevMask;
gl.setRenderTarget(prevTarget); gl.setRenderTarget(prevTarget);
gl.autoClear = prevAutoClear; gl.autoClear = prevAutoClear;
@@ -462,40 +486,54 @@ function GlassModel({
const spin = spinRef.current; const spin = spinRef.current;
if (!g) return; if (!g) return;
// Scroll sync: model y follows document anchor with factor 0.72 // --- Section-anchored scroll state (banner: top 0, h-dvh) ---
const viewportHeight = const vh = Math.max(1, state.size.height);
state.viewport.getCurrentViewport(camera, new THREE.Vector3(0, 0, position[2])) const sectionH = Math.max(1, sectionHeightPx ?? vh);
.height; const scrollTop = Math.max(0, scrollBus.current);
const docProgress = scrollBus.progress; // 0..1
// Original: worldY from section anchor docY with scrollSyncFactor; here we // Original X(): hide the model once its section leaves the viewport.
// approximate: hero content center scrolls up as user scrolls. const deltaTop = sectionTopDocY - scrollTop;
const scrollWorldY = docProgress * viewportHeight * scrollSyncFactor; g.visible = deltaTop + sectionH > 0 && deltaTop < vh;
// Original position sync:
// worldY = (0.5 - anchorDocY/vh) * viewportWorldH
// + (scrollTop/vh) * viewportWorldH * scrollSyncFactor
// where anchorDocY = section top + half height. For the banner at page
// top this reduces to (scrollTop/vh) * worldH * factor — i.e. the model
// tracks the first viewport of scrolling, NOT whole-page progress.
const viewportWorldHeight = state.viewport.getCurrentViewport(
camera,
new THREE.Vector3(0, 0, position[2])
).height;
const anchorDocY = sectionTopDocY + sectionH / 2;
const syncWorldY =
(0.5 - anchorDocY / vh) * viewportWorldHeight +
(scrollTop / vh) * viewportWorldHeight * scrollSyncFactor;
const float = getFloat(state.clock.getElapsedTime()); const float = getFloat(state.clock.getElapsedTime());
g.position.y = position[1] - scrollWorldY + float; g.position.y = syncWorldY + position[1] + float;
g.position.x = position[0]; g.position.x = position[0];
g.position.z = position[2]; g.position.z = position[2];
// Entrance rotation damp // --- Scroll-driven rotation targets (original Z()) ---
const before = beforeRotation ?? rotation; // i = (scrollTop + vh - sectionTop) / vh
const after = afterRotation ?? null; // entry = clamp(i, 0, 1) after = clamp(i - 1, 0, 1)
const e = entrance.current; // At page top entry=1 / after=0, so nothing animates until the user
if (!e.started) { // scrolls; `after` then ramps over the first viewport height.
e.started = true; let entry = 1;
e.elapsed = 0; let after = afterRotation ? 1 : 0;
if (vh > 0) {
const i = (scrollTop + vh - sectionTopDocY) / vh;
entry = THREE.MathUtils.clamp(i, 0, 1);
after = THREE.MathUtils.clamp(i - 1, 0, 1);
} }
e.elapsed += delta;
const lambda = 6; let tx = THREE.MathUtils.lerp(rad(before[0]), rad(rotation[0]), entry);
const mix = 1 - Math.exp(-lambda * e.elapsed); let ty = THREE.MathUtils.lerp(rad(before[1]), rad(rotation[1]), entry);
const rad = (v: number) => THREE.MathUtils.degToRad(v); let tz = THREE.MathUtils.lerp(rad(before[2]), rad(rotation[2]), entry);
let tx = THREE.MathUtils.lerp(rad(before[0]), rad(rotation[0]), mix); if (afterRotation) {
let ty = THREE.MathUtils.lerp(rad(before[1]), rad(rotation[1]), mix); tx = THREE.MathUtils.lerp(tx, rad(afterRotation[0]), after);
let tz = THREE.MathUtils.lerp(rad(before[2]), rad(rotation[2]), mix); ty = THREE.MathUtils.lerp(ty, rad(afterRotation[1]), after);
if (after) { tz = THREE.MathUtils.lerp(tz, rad(afterRotation[2]), after);
// after phase ramps in after entry (~2s), slowly continuing to afterRotation
const afterMix = 1 - Math.exp(-lambda * Math.max(0, e.elapsed - 2));
tx = THREE.MathUtils.lerp(tx, rad(after[0]), afterMix);
ty = THREE.MathUtils.lerp(ty, rad(after[1]), afterMix);
tz = THREE.MathUtils.lerp(tz, rad(after[2]), afterMix);
} }
if (spin) { if (spin) {
spin.rotation.x = THREE.MathUtils.damp(spin.rotation.x, tx, 6, delta); spin.rotation.x = THREE.MathUtils.damp(spin.rotation.x, tx, 6, delta);
@@ -508,22 +546,27 @@ function GlassModel({
return ( return (
<group ref={groupRef} position={position}> <group ref={groupRef} position={position}>
{/* rotationAxisTilt group ... then counter-tilt INSIDE the spin group:
tilt * spin * (-tilt) keeps the model upright at rest while the spin
happens around the tilted axis — exactly the original hierarchy. */}
<group rotation={rotationAxisTilt}> <group rotation={rotationAxisTilt}>
<group ref={spinRef} rotation={rotation}> <group ref={spinRef} rotation={rotation}>
<mesh <group rotation={negatedTilt}>
ref={meshLayerRef} <mesh
geometry={mergedGeometry} ref={meshLayerRef}
scale={[scale, scale, scale]} geometry={mergedGeometry}
> scale={[scale, scale, scale]}
<shaderMaterial >
ref={materialRef} <shaderMaterial
vertexShader={VERT} ref={materialRef}
fragmentShader={FRAG} vertexShader={VERT}
uniforms={uniforms} fragmentShader={FRAG}
toneMapped={false} uniforms={uniforms}
transparent toneMapped={false}
/> transparent
</mesh> />
</mesh>
</group>
</group> </group>
</group> </group>
</group> </group>
@@ -540,6 +583,7 @@ export function HelloModel({ isMobile = false }: { isMobile?: boolean }) {
rotation={[0, 4, 0]} rotation={[0, 4, 0]}
scale={isMobile ? 19 : 22} scale={isMobile ? 19 : 22}
tintEnabled tintEnabled
isMobile={isMobile}
scrollSyncFactor={0.72} scrollSyncFactor={0.72}
/> />
); );
@@ -557,6 +601,7 @@ export function CursorStar({ isMobile = false }: { isMobile?: boolean }) {
scale={0.1} scale={0.1}
tintColors={["#009dff", "#64c3ff"]} tintColors={["#009dff", "#64c3ff"]}
tintEnabled tintEnabled
isMobile={isMobile}
scrollSyncFactor={0.72} scrollSyncFactor={0.72}
/> />
); );
+8 -6
View File
@@ -12,11 +12,12 @@ import { useIsMobile } from "@/lib/useIsMobile";
/** /**
* Hero canvas: falling stickers + glass hello/cursor models. * Hero canvas: falling stickers + glass hello/cursor models.
* *
* The glass shader refracts a scene texture rendered into an FBO from layer 1. * The glass shader refracts a scene texture rendered into an FBO with a
* BackgroundGrid lives in the DOM (below the canvas), so it can't be refracted; * layer-0 camera mask (stickers/background), matching the original
* the FBO pass still captures the sticker particles, giving the glass its * "glass hosting section" FBO. Glass meshes enable layer 10 so they are
* refraction content — the same hierarchy the original achieves with its * excluded from their own refraction pass.
* "glass hosting section" FBO. * Models animate only from scroll (entry/after targets, damp 6) — no
* time-based motion besides the desktop float bob.
*/ */
export function HeroScene() { export function HeroScene() {
@@ -36,7 +37,8 @@ export function HeroScene() {
<StickerCamera /> <StickerCamera />
{/* Falling stickers: 1:1 re-implementation of the original particle system. {/* Falling stickers: 1:1 re-implementation of the original particle system.
Rendered on layer 1 so the glass FBO pass can capture them. */} Live on layer 0 — visible to the main camera and captured by the
glass FBO pass (which renders with a layer-0 camera mask). */}
<StickerDrop /> <StickerDrop />
{/* Glass hero models (hello + blue cursor star), refractive tint shader */} {/* Glass hero models (hello + blue cursor star), refractive tint shader */}
+4 -3
View File
@@ -323,9 +323,10 @@ export class StickerLayer {
); );
mesh.frustumCulled = false; mesh.frustumCulled = false;
mesh.count = 0; mesh.count = 0;
// Layer 1: the glass models' FBO pass captures only this layer, so the // Default layer 0 (same as the original): visible to the main camera AND
// falling stickers appear refracted through the hello/cursor glass. // captured by the glass FBO pass, which temporarily renders with
mesh.layers.set(1); // camera.layers.mask = 1 (layer 0 only) so glass meshes (layer 10) are
// excluded while stickers/background are included.
this.geometry = geometry; this.geometry = geometry;
this.material = material; this.material = material;