"use client"; import { useEffect, useMemo, 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"; import { loadingBus } from "@/lib/loadingBus"; import { usePointer } from "@/lib/pointer"; import { useIsMobile } from "@/lib/useIsMobile"; import { customCubic } from "@/lib/customCubic"; 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(null); const isMobile = useIsMobile(); useEffect(() => { const layer = new StickerLayer(scene); layerRef.current = layer; layer .load(STICKER_IMAGES) .then(() => loadingBus.mark("stickers")) .catch(() => {}); return () => { layer.dispose(); layerRef.current = null; }; }, [scene]); useEffect(() => { if (isMobile) return; // original: pointer bursts are desktop-only 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, isMobile]); useFrame((_, delta) => { layerRef.current?.update(delta); }); return null; } /** * Camera rig matching the original cK: * - horizontal FOV 60° (desktop) / 38° (mobile) converted to vertical FOV * - distance A = lerp(24, 32, sP); position.z = lerp(A + 8, A, * customCubic(introT)) where introT ramps 0→1 over 1.2s once `ready` * (the entrance dolly from z+8) * - mouse parallax (desktop, ready only): offset = ((.5-uv)*2) * (1.4, .84), * rotated around the base position by lookAt(gyro*-.12), lerp lag .18 * (.05 when the pointer leaves) * - sP is the original camera progress (banner/footer geometry), updated * from scrollBus.sP written by the page's scroll-geometry effect */ export function StickerCamera({ ready }: { ready: boolean }) { const camera = useThree((s) => s.camera) as THREE.PerspectiveCamera; const size = useThree((s) => s.size); const { uv, insideRef } = usePointer(); const isMobile = useIsMobile(); const baseRef = useRef(null); const posLagRef = useRef(new THREE.Vector3()); const rotLagRef = useRef(new THREE.Vector3()); const introRef = useRef(0); const distanceRef = useRef(24); const parallaxVec = useMemo(() => new THREE.Vector3(), []); useFrame((_state, rawDelta) => { const delta = Math.min(rawDelta, 0.1); 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); if ( Math.abs(camera.fov - THREE.MathUtils.radToDeg(verticalFov)) > 1e-4 || Math.abs(camera.aspect - aspect) > 1e-6 ) { camera.fov = THREE.MathUtils.radToDeg(verticalFov); camera.aspect = aspect; camera.updateProjectionMatrix(); } const progress = THREE.MathUtils.clamp(scrollBus.sP, 0, 1); const distance = THREE.MathUtils.lerp(24, 32, progress); distanceRef.current = distance; if (!baseRef.current) { baseRef.current = new THREE.Vector2(camera.position.x, camera.position.y); } introRef.current = ready ? Math.min(introRef.current + delta / 1.2, 1) : 0; camera.position.z = THREE.MathUtils.lerp( distance + 8, distance, customCubic(introRef.current) ); if (!isMobile) { const px = (0.5 - uv.x) * 2; const py = (0.5 - uv.y) * 2; const offset = posLagRef.current; const rot = rotLagRef.current; offset.lerp(parallaxVec.set(px * 1.4, py * 0.84, 0), insideRef.current ? 0.18 : 0.05); rot.lerp(parallaxVec.set(-offset.x * 0.12, -offset.y * 0.12, 0), insideRef.current ? 0.18 : 0.05); camera.position.x = baseRef.current.x + offset.x; camera.position.y = baseRef.current.y + offset.y; camera.lookAt(rot); } else { posLagRef.current.lerp(parallaxVec.set(0, 0, 0), 0.18); rotLagRef.current.lerp(parallaxVec.set(0, 0, 0), 0.18); camera.position.x = baseRef.current.x; camera.position.y = baseRef.current.y; camera.lookAt(0, 0, 0); } }); return null; }