feat: 更新玻璃模型和英雄场景,优化贴纸层的渲染逻辑
This commit is contained in:
@@ -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,22 +546,27 @@ 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}>
|
||||
<mesh
|
||||
ref={meshLayerRef}
|
||||
geometry={mergedGeometry}
|
||||
scale={[scale, scale, scale]}
|
||||
>
|
||||
<shaderMaterial
|
||||
ref={materialRef}
|
||||
vertexShader={VERT}
|
||||
fragmentShader={FRAG}
|
||||
uniforms={uniforms}
|
||||
toneMapped={false}
|
||||
transparent
|
||||
/>
|
||||
</mesh>
|
||||
<group rotation={negatedTilt}>
|
||||
<mesh
|
||||
ref={meshLayerRef}
|
||||
geometry={mergedGeometry}
|
||||
scale={[scale, scale, scale]}
|
||||
>
|
||||
<shaderMaterial
|
||||
ref={materialRef}
|
||||
vertexShader={VERT}
|
||||
fragmentShader={FRAG}
|
||||
uniforms={uniforms}
|
||||
toneMapped={false}
|
||||
transparent
|
||||
/>
|
||||
</mesh>
|
||||
</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}
|
||||
/>
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user