实现贴纸粒子系统,添加 HeroScene 组件,优化 Next.js 配置,更新依赖
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
"use client";
|
||||
|
||||
import { Canvas } from "@react-three/fiber";
|
||||
import { PerspectiveCamera } from "@react-three/drei";
|
||||
|
||||
import { StickerDrop, StickerCamera } from "@/components/canvas/StickerDrop";
|
||||
|
||||
export function HeroScene() {
|
||||
return (
|
||||
// z-30 keeps it above the BackgroundGrid (z-20) to prevent mix-blend-difference from making it gray
|
||||
<div className="z-30 fixed inset-0 w-full h-full pointer-events-none">
|
||||
<Canvas
|
||||
dpr={[1, 2]}
|
||||
gl={{ alpha: true, antialias: true }}
|
||||
>
|
||||
{/* Camera rig: horizontal FOV 60/38 + scroll-driven distance lerp(24, 32).
|
||||
StickerCamera drives fov/aspect/z every frame; PerspectiveCamera is the
|
||||
makeDefault camera it mutates. */}
|
||||
<PerspectiveCamera makeDefault position={[0, 0, 24]} near={0.1} far={200} />
|
||||
<StickerCamera />
|
||||
|
||||
{/* Falling stickers: 1:1 re-implementation of the original particle system */}
|
||||
<StickerDrop />
|
||||
</Canvas>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export default HeroScene;
|
||||
@@ -0,0 +1,90 @@
|
||||
import { Effect, BlendFunction } from 'postprocessing';
|
||||
import * as THREE from 'three';
|
||||
import { wrapEffect } from '@react-three/postprocessing';
|
||||
|
||||
const fragmentShader = `
|
||||
uniform float pixelSize;
|
||||
uniform float uFeather;
|
||||
uniform float uAspect;
|
||||
uniform float uHoleRadius;
|
||||
uniform float uProgress;
|
||||
uniform vec3 uOverlayColor;
|
||||
|
||||
void dotsMainImage(const in vec4 inputColor, const in vec2 uv, out vec4 outputColor) {
|
||||
vec2 normalizedPixelSize = vec2(pixelSize) / resolution;
|
||||
float a = clamp(inputColor.a, 0.0, 1.0);
|
||||
vec2 cellUV = fract(uv / normalizedPixelSize);
|
||||
float radius = 0.8 * a;
|
||||
vec2 circleCenter = vec2(0.5, 0.5);
|
||||
float distanceFromCenter = distance(cellUV, circleCenter);
|
||||
float aa = fwidth(distanceFromCenter) * 1.5;
|
||||
// Safe smoothstep: edge0 must be < edge1 in GLSL
|
||||
float circleMask = 1.0 - smoothstep(radius - aa, radius, distanceFromCenter);
|
||||
outputColor = vec4(vec3(circleMask), circleMask);
|
||||
}
|
||||
|
||||
float radialMaskAlpha(vec2 uv) {
|
||||
vec2 p = uv * 2.0 - 1.0;
|
||||
if (uAspect > 1.0) {
|
||||
p.x *= uAspect;
|
||||
} else {
|
||||
p.y /= max(uAspect, 0.0001);
|
||||
}
|
||||
float d = length(p);
|
||||
float edge = max(uFeather, uHoleRadius * 0.12);
|
||||
float alphaHole = smoothstep(uHoleRadius, uHoleRadius + edge, d);
|
||||
float fillMix = smoothstep(0.92, 1.0, uProgress);
|
||||
return mix(alphaHole, 1.0, fillMix);
|
||||
}
|
||||
|
||||
void mainImage(const in vec4 inputColor, const in vec2 uv, out vec4 outputColor) {
|
||||
vec2 normalizedPixelSize = vec2(pixelSize) / resolution;
|
||||
vec2 cellId = floor(uv / normalizedPixelSize);
|
||||
vec2 uvCellCenter = (cellId + vec2(0.5)) * normalizedPixelSize;
|
||||
float cellAlpha = clamp(radialMaskAlpha(uvCellCenter), 0.0, 1.0);
|
||||
|
||||
vec4 dotOut;
|
||||
dotsMainImage(vec4(0.0, 0.0, 0.0, cellAlpha), uv, dotOut);
|
||||
float dotMask = clamp(dotOut.a, 0.0, 1.0);
|
||||
|
||||
float finalAlpha = dotMask;
|
||||
vec3 outRgb = uOverlayColor * finalAlpha;
|
||||
|
||||
// Blend with inputColor to only apply mask where finalAlpha > 0
|
||||
// Since we use BlendFunction.NORMAL, we just return the color and alpha
|
||||
outputColor = vec4(outRgb, finalAlpha);
|
||||
}
|
||||
`;
|
||||
|
||||
export class MaskedDotsEffectImpl extends Effect {
|
||||
constructor({
|
||||
pixelSize = 32,
|
||||
feather = 0.5,
|
||||
aspect = 1,
|
||||
holeRadius = 2,
|
||||
progress = 0,
|
||||
overlayColor = new THREE.Color('#fbfaf4')
|
||||
} = {}) {
|
||||
super('MaskedDotsEffect', fragmentShader, {
|
||||
blendFunction: BlendFunction.NORMAL,
|
||||
uniforms: new Map<string, THREE.Uniform<number> | THREE.Uniform<THREE.Color>>([
|
||||
['pixelSize', new THREE.Uniform(pixelSize)],
|
||||
['uFeather', new THREE.Uniform(feather)],
|
||||
['uAspect', new THREE.Uniform(aspect)],
|
||||
['uHoleRadius', new THREE.Uniform(holeRadius)],
|
||||
['uProgress', new THREE.Uniform(progress)],
|
||||
['uOverlayColor', new THREE.Uniform(overlayColor)]
|
||||
])
|
||||
});
|
||||
}
|
||||
|
||||
update(
|
||||
_renderer: THREE.WebGLRenderer,
|
||||
_inputBuffer: THREE.WebGLRenderTarget,
|
||||
_deltaTime?: number
|
||||
) {
|
||||
// Uniforms are driven externally via ref.current.uniforms.get(...)
|
||||
}
|
||||
}
|
||||
|
||||
export const MaskedDots = wrapEffect(MaskedDotsEffectImpl);
|
||||
@@ -0,0 +1,144 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect, useRef } from "react";
|
||||
import { useFrame, useThree } from "@react-three/fiber";
|
||||
import * as THREE from "three";
|
||||
import { StickerLayer } from "@/components/webgl/StickerLayer";
|
||||
import { scrollBus } from "@/lib/scrollBus";
|
||||
|
||||
const STICKER_IMAGES = Array.from(
|
||||
{ length: 12 },
|
||||
(_, i) => `/sticker_img/s_${String(i + 1).padStart(2, "0")}.png`
|
||||
);
|
||||
|
||||
/**
|
||||
* Falling-sticker particle layer.
|
||||
*
|
||||
* Simulation constants live in StickerLayer (reverse-engineered 1:1 from the
|
||||
* production bundle): 12 persistent particles, 48-unit fall distance, wind
|
||||
* sway, respawn with texture de-duplication, 12-particle click bursts.
|
||||
*
|
||||
* This component only wires the layer into the R3F scene graph and runs the
|
||||
* pointer→raycast→burst interaction (drag threshold 4px/10px, 600ms window,
|
||||
* text-selection guard — same as the original).
|
||||
*/
|
||||
export function StickerDrop() {
|
||||
const scene = useThree((s) => s.scene);
|
||||
const camera = useThree((s) => s.camera);
|
||||
const gl = useThree((s) => s.gl);
|
||||
const layerRef = useRef<StickerLayer | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
const layer = new StickerLayer(scene);
|
||||
layerRef.current = layer;
|
||||
layer.load(STICKER_IMAGES).catch(() => {});
|
||||
return () => {
|
||||
layer.dispose();
|
||||
layerRef.current = null;
|
||||
};
|
||||
}, [scene]);
|
||||
|
||||
useEffect(() => {
|
||||
const raycaster = new THREE.Raycaster();
|
||||
const ndc = new THREE.Vector2();
|
||||
const plane = new THREE.Plane(new THREE.Vector3(0, 0, 1), 0);
|
||||
const hit = new THREE.Vector3();
|
||||
|
||||
type Pending = {
|
||||
id: number;
|
||||
startX: number;
|
||||
startY: number;
|
||||
startAt: number;
|
||||
cancelled: boolean;
|
||||
};
|
||||
let pending: Pending | null = null;
|
||||
|
||||
const onPointerDown = (e: PointerEvent) => {
|
||||
if (
|
||||
e.isPrimary &&
|
||||
((e.pointerType !== "mouse" && e.pointerType !== "pen") || e.button === 0)
|
||||
) {
|
||||
pending = {
|
||||
id: e.pointerId,
|
||||
startX: e.clientX,
|
||||
startY: e.clientY,
|
||||
startAt: e.timeStamp || performance.now(),
|
||||
cancelled: false
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
const onPointerMove = (e: PointerEvent) => {
|
||||
if (!pending || e.pointerId !== pending.id || pending.cancelled) return;
|
||||
const dx = e.clientX - pending.startX;
|
||||
const dy = e.clientY - pending.startY;
|
||||
const threshold = e.pointerType === "touch" ? 10 : 4;
|
||||
if (dx * dx + dy * dy > threshold * threshold) pending.cancelled = true;
|
||||
};
|
||||
|
||||
const onPointerUp = (e: PointerEvent) => {
|
||||
const p = pending;
|
||||
if (!p || e.pointerId !== p.id) return;
|
||||
pending = null;
|
||||
if (p.cancelled || (e.timeStamp || performance.now()) - p.startAt > 600) return;
|
||||
|
||||
const selection = window.getSelection();
|
||||
if (selection && !selection.isCollapsed && selection.toString().trim().length > 0) return;
|
||||
|
||||
const rect = gl.domElement.getBoundingClientRect();
|
||||
ndc.set(
|
||||
((e.clientX - rect.left) / rect.width) * 2 - 1,
|
||||
-(((e.clientY - rect.top) / rect.height) * 2 - 1)
|
||||
);
|
||||
raycaster.setFromCamera(ndc, camera);
|
||||
if (!raycaster.ray.intersectPlane(plane, hit)) return;
|
||||
layerRef.current?.burst(hit);
|
||||
};
|
||||
|
||||
const onPointerCancel = (e: PointerEvent) => {
|
||||
if (pending && e.pointerId === pending.id) pending = null;
|
||||
};
|
||||
|
||||
const options = { capture: true, passive: true } as const;
|
||||
window.addEventListener("pointerdown", onPointerDown, options);
|
||||
window.addEventListener("pointermove", onPointerMove, options);
|
||||
window.addEventListener("pointerup", onPointerUp, options);
|
||||
window.addEventListener("pointercancel", onPointerCancel, options);
|
||||
return () => {
|
||||
window.removeEventListener("pointerdown", onPointerDown, true);
|
||||
window.removeEventListener("pointermove", onPointerMove, true);
|
||||
window.removeEventListener("pointerup", onPointerUp, true);
|
||||
window.removeEventListener("pointercancel", onPointerCancel, true);
|
||||
};
|
||||
}, [camera, gl]);
|
||||
|
||||
useFrame((_, delta) => {
|
||||
layerRef.current?.update(delta);
|
||||
});
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Camera rig matching the original: horizontal FOV 60° (desktop) / 38°
|
||||
* (mobile) converted to vertical FOV per aspect, and camera distance
|
||||
* lerp(24, 32) driven by page scroll progress.
|
||||
*/
|
||||
export function StickerCamera() {
|
||||
const camera = useThree((s) => s.camera) as THREE.PerspectiveCamera;
|
||||
const size = useThree((s) => s.size);
|
||||
|
||||
useFrame(() => {
|
||||
const aspect = size.width / Math.max(1, size.height);
|
||||
const horizontalFov = size.width >= 1024 ? 60 : 38;
|
||||
const verticalFov =
|
||||
2 * Math.atan(Math.tan(THREE.MathUtils.degToRad(horizontalFov) / 2) / aspect);
|
||||
camera.fov = THREE.MathUtils.radToDeg(verticalFov);
|
||||
camera.aspect = aspect;
|
||||
const progress = THREE.MathUtils.clamp(scrollBus.progress, 0, 1);
|
||||
camera.position.z = THREE.MathUtils.lerp(24, 32, progress);
|
||||
camera.updateProjectionMatrix();
|
||||
});
|
||||
|
||||
return null;
|
||||
}
|
||||
Reference in New Issue
Block a user