145 lines
4.8 KiB
TypeScript
145 lines
4.8 KiB
TypeScript
"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;
|
|
}
|