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
+86 -41
View File
@@ -277,6 +277,14 @@ type GlassModelProps = {
tintEnabled?: boolean;
scrollSyncFactor?: number;
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) {
@@ -303,6 +311,9 @@ function GlassModel({
tintEnabled = false,
scrollSyncFactor = 0.72,
floating = true,
isMobile = false,
sectionTopDocY = 0,
sectionHeightPx,
}: GlassModelProps) {
const gltf = useLoader(GLTFLoader, url);
const groupRef = useRef<THREE.Group>(null!);
@@ -332,7 +343,7 @@ function GlassModel({
} catch {
merged = null;
}
if (!merged && geometries.length === 1) merged = geometries[0];
if (!merged && geometries.length >= 1) merged = geometries[0];
if (!merged) return null;
merged.computeBoundingBox();
const bb = merged.boundingBox!;
@@ -347,7 +358,7 @@ function GlassModel({
}, [gltf]);
// 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(() => {
meshLayerRef.current?.layers.enable(10);
}, [mergedGeometry]);
@@ -361,7 +372,9 @@ function GlassModel({
: [bb.min.y, bb.max.y];
}, [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 dpr = gl.getPixelRatio();
const w = Math.max(2, Math.floor((size.width * dpr) / 2));
@@ -436,25 +449,36 @@ function GlassModel({
);
}, [gl, size, uniforms]);
// Entrance animation: damp rotation from beforeRotation -> rotation -> afterRotation
const entrance = useRef({ started: false, elapsed: 0 });
// Float bob: desktop only in the original (floatingMotion && !isMobileWidth)
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) => {
const delta = Math.min(rawDelta, 0.1);
// Render the scene (layer 1 only) into the FBO for refraction.
// Background/sticker content must be on layer 1; glass models render on
// layer 0 + 10 (10 excluded from the FBO pass to avoid self-refraction).
// Render the scene (layer 0 mask) into the FBO for refraction.
// Stickers/background live on layer 0; glass models render on layer 0+10
// and are excluded by the layer-0 mask (no self-refraction). Original:
// `camera.layers.mask = 1; render; restore`.
const prevTarget = gl.getRenderTarget();
const prevAutoClear = gl.autoClear;
const cameraMask = camera.layers.mask;
const prevMask = camera.layers.mask;
gl.setRenderTarget(fbo);
gl.clear();
camera.layers.set(1);
camera.layers.set(0);
gl.render(scene, camera);
camera.layers.mask = cameraMask;
camera.layers.mask = prevMask;
gl.setRenderTarget(prevTarget);
gl.autoClear = prevAutoClear;
@@ -462,40 +486,54 @@ function GlassModel({
const spin = spinRef.current;
if (!g) return;
// Scroll sync: model y follows document anchor with factor 0.72
const viewportHeight =
state.viewport.getCurrentViewport(camera, new THREE.Vector3(0, 0, position[2]))
.height;
const docProgress = scrollBus.progress; // 0..1
// Original: worldY from section anchor docY with scrollSyncFactor; here we
// approximate: hero content center scrolls up as user scrolls.
const scrollWorldY = docProgress * viewportHeight * scrollSyncFactor;
// --- Section-anchored scroll state (banner: top 0, h-dvh) ---
const vh = Math.max(1, state.size.height);
const sectionH = Math.max(1, sectionHeightPx ?? vh);
const scrollTop = Math.max(0, scrollBus.current);
// Original X(): hide the model once its section leaves the viewport.
const deltaTop = sectionTopDocY - scrollTop;
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());
g.position.y = position[1] - scrollWorldY + float;
g.position.y = syncWorldY + position[1] + float;
g.position.x = position[0];
g.position.z = position[2];
// Entrance rotation damp
const before = beforeRotation ?? rotation;
const after = afterRotation ?? null;
const e = entrance.current;
if (!e.started) {
e.started = true;
e.elapsed = 0;
// --- Scroll-driven rotation targets (original Z()) ---
// i = (scrollTop + vh - sectionTop) / vh
// entry = clamp(i, 0, 1) after = clamp(i - 1, 0, 1)
// At page top entry=1 / after=0, so nothing animates until the user
// scrolls; `after` then ramps over the first viewport height.
let entry = 1;
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;
const mix = 1 - Math.exp(-lambda * e.elapsed);
const rad = (v: number) => THREE.MathUtils.degToRad(v);
let tx = THREE.MathUtils.lerp(rad(before[0]), rad(rotation[0]), mix);
let ty = THREE.MathUtils.lerp(rad(before[1]), rad(rotation[1]), mix);
let tz = THREE.MathUtils.lerp(rad(before[2]), rad(rotation[2]), mix);
if (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);
let tx = THREE.MathUtils.lerp(rad(before[0]), rad(rotation[0]), entry);
let ty = THREE.MathUtils.lerp(rad(before[1]), rad(rotation[1]), entry);
let tz = THREE.MathUtils.lerp(rad(before[2]), rad(rotation[2]), entry);
if (afterRotation) {
tx = THREE.MathUtils.lerp(tx, rad(afterRotation[0]), after);
ty = THREE.MathUtils.lerp(ty, rad(afterRotation[1]), after);
tz = THREE.MathUtils.lerp(tz, rad(afterRotation[2]), after);
}
if (spin) {
spin.rotation.x = THREE.MathUtils.damp(spin.rotation.x, tx, 6, delta);
@@ -508,8 +546,12 @@ function GlassModel({
return (
<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 ref={spinRef} rotation={rotation}>
<group rotation={negatedTilt}>
<mesh
ref={meshLayerRef}
geometry={mergedGeometry}
@@ -527,6 +569,7 @@ function GlassModel({
</group>
</group>
</group>
</group>
);
}
@@ -540,6 +583,7 @@ export function HelloModel({ isMobile = false }: { isMobile?: boolean }) {
rotation={[0, 4, 0]}
scale={isMobile ? 19 : 22}
tintEnabled
isMobile={isMobile}
scrollSyncFactor={0.72}
/>
);
@@ -557,6 +601,7 @@ export function CursorStar({ isMobile = false }: { isMobile?: boolean }) {
scale={0.1}
tintColors={["#009dff", "#64c3ff"]}
tintEnabled
isMobile={isMobile}
scrollSyncFactor={0.72}
/>
);
+8 -6
View File
@@ -12,11 +12,12 @@ import { useIsMobile } from "@/lib/useIsMobile";
/**
* Hero canvas: falling stickers + glass hello/cursor models.
*
* The glass shader refracts a scene texture rendered into an FBO from layer 1.
* BackgroundGrid lives in the DOM (below the canvas), so it can't be refracted;
* the FBO pass still captures the sticker particles, giving the glass its
* refraction content — the same hierarchy the original achieves with its
* "glass hosting section" FBO.
* The glass shader refracts a scene texture rendered into an FBO with a
* layer-0 camera mask (stickers/background), matching the original
* "glass hosting section" FBO. Glass meshes enable layer 10 so they are
* excluded from their own refraction pass.
* Models animate only from scroll (entry/after targets, damp 6) — no
* time-based motion besides the desktop float bob.
*/
export function HeroScene() {
@@ -36,7 +37,8 @@ export function HeroScene() {
<StickerCamera />
{/* 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 />
{/* 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.count = 0;
// Layer 1: the glass models' FBO pass captures only this layer, so the
// falling stickers appear refracted through the hello/cursor glass.
mesh.layers.set(1);
// Default layer 0 (same as the original): visible to the main camera AND
// captured by the glass FBO pass, which temporarily renders with
// camera.layers.mask = 1 (layer 0 only) so glass meshes (layer 10) are
// excluded while stickers/background are included.
this.geometry = geometry;
this.material = material;