122 lines
4.6 KiB
TypeScript
122 lines
4.6 KiB
TypeScript
"use client";
|
|
|
|
import { Canvas, useFrame } from "@react-three/fiber";
|
|
import { useGLTF, useAnimations, Center, OrbitControls } from "@react-three/drei";
|
|
import { useEffect, useRef, useMemo } from "react";
|
|
import * as THREE from "three";
|
|
|
|
function FoxCampfireModel() {
|
|
const group = useRef<THREE.Group>(null);
|
|
const { scene, animations } = useGLTF("/campfire.gltf");
|
|
const { actions, mixer } = useAnimations(animations, group);
|
|
|
|
const cloned = useMemo(() => scene.clone(true), [scene]);
|
|
|
|
// Fix texture filtering - keep crisp pixel art, don't scroll it
|
|
useMemo(() => {
|
|
cloned.traverse((o) => {
|
|
if ((o as THREE.Mesh).isMesh) {
|
|
const mesh = o as THREE.Mesh;
|
|
mesh.frustumCulled = false;
|
|
const mat = mesh.material as THREE.MeshStandardMaterial;
|
|
if (mat?.map) {
|
|
mat.map.magFilter = THREE.NearestFilter;
|
|
mat.map.minFilter = THREE.NearestFilter;
|
|
mat.map.colorSpace = THREE.SRGBColorSpace;
|
|
}
|
|
}
|
|
});
|
|
}, [cloned]);
|
|
|
|
// Use vanilla Bedrock fox.sleep values (ZtechNetwork/MCBVanillaResourcePack)
|
|
// animation.fox.sleep: body pos [0,-4.8,0] rot [0,0,-90], head pos [1.8,-0.4,-2] rot [0,-115, cos(t*slow)+90], tail pos [0,0,1.5] rot [-125,0,0]
|
|
// We apply statically and animate head Z breathing slowly (vanilla uses 160* which is too fast for web)
|
|
useEffect(() => {
|
|
Object.values(actions).forEach((a) => a?.stop());
|
|
if (mixer) mixer.stopAllAction();
|
|
// Hide awake head meshes, keep head_sleeping (closed eyes)
|
|
cloned.traverse((o) => {
|
|
if ((o as THREE.Mesh).isMesh && o.name === "head") {
|
|
let cur: THREE.Object3D | null = o;
|
|
let isSleep = false;
|
|
while (cur) { if (cur.name === "head_sleeping") isSleep = true; cur = cur.parent; }
|
|
if (!isSleep) (o as THREE.Mesh).visible = false;
|
|
}
|
|
});
|
|
if (mixer) mixer.timeScale = 0;
|
|
}, [actions, mixer, cloned]);
|
|
|
|
const lightRef = useRef<THREE.PointLight>(null);
|
|
|
|
const foxRef = useRef<THREE.Group>(null);
|
|
useFrame((state) => {
|
|
const t = state.clock.elapsedTime;
|
|
if (lightRef.current) {
|
|
lightRef.current.intensity = 3 + Math.sin(t * 3) * 0.5 + Math.sin(t * 7) * 0.25;
|
|
}
|
|
// Vanilla sleep pose - applied each frame (overrides any glTF animation)
|
|
const body = cloned.getObjectByName("body") as THREE.Group | null;
|
|
const head = cloned.getObjectByName("head") as THREE.Group | null; // pivot containing sleep head
|
|
const tail = cloned.getObjectByName("tail") as THREE.Group | null;
|
|
const toRad = (d: number) => (d * Math.PI) / 180;
|
|
if (body) {
|
|
body.rotation.set(0, 0, toRad(-90));
|
|
// keep original body position y offset from glTF, just lower slightly for center
|
|
body.position.set(0, -0.35, 0);
|
|
}
|
|
if (head) {
|
|
// Head is child of body - keep its original pos [0,-0.5,0.1875] + vanilla delta scaled
|
|
head.position.set(0.1125, -0.525, 0.0625);
|
|
const breath = Math.cos(t * 1.1) * toRad(1.5);
|
|
head.rotation.set(0, toRad(-115), toRad(90) + breath);
|
|
}
|
|
if (tail) {
|
|
tail.rotation.set(toRad(-125), 0, 0);
|
|
tail.position.set(0, 0, 0.09);
|
|
}
|
|
// Gentle overall bob
|
|
if (foxRef.current) {
|
|
foxRef.current.position.y = Math.sin(t * 0.7) * 0.02;
|
|
}
|
|
});
|
|
|
|
return (
|
|
<group ref={group}>
|
|
<group ref={foxRef}>
|
|
<Center>
|
|
<primitive object={cloned} scale={1.2} />
|
|
</Center>
|
|
</group>
|
|
<pointLight ref={lightRef} position={[0, 0.5, 0]} intensity={3} distance={5} color="#ff7a18" decay={2} />
|
|
</group>
|
|
);
|
|
}
|
|
|
|
export default function CampfireViewer() {
|
|
return (
|
|
<div className="h-full w-full min-h-[520px] flex items-center justify-center rounded-2xl overflow-hidden bg-card border">
|
|
<Canvas
|
|
camera={{ position: [-4.5, 2.36, -5.1], fov: 38 }}
|
|
dpr={[1, 2]}
|
|
gl={{ antialias: true, alpha: true }}
|
|
onCreated={({ gl }) => {
|
|
gl.toneMapping = THREE.ACESFilmicToneMapping;
|
|
gl.outputColorSpace = THREE.SRGBColorSpace;
|
|
}}
|
|
>
|
|
<ambientLight intensity={0.9} />
|
|
<directionalLight position={[3, 4, 2]} intensity={1} />
|
|
<FoxCampfireModel />
|
|
<OrbitControls
|
|
enablePan={false}
|
|
onEnd={(e) => {
|
|
const t = e?.target as unknown as { object: { position: { x: number; y: number; z: number } }; target: { x: number; y: number; z: number } };
|
|
if (t?.object) console.log(`camera position={[${t.object.position.x.toFixed(2)}, ${t.object.position.y.toFixed(2)}, ${t.object.position.z.toFixed(2)}]} target={[${t.target.x.toFixed(2)}, ${t.target.y.toFixed(2)}, ${t.target.z.toFixed(2)}]}`);
|
|
}}
|
|
/>
|
|
</Canvas>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
useGLTF.preload("/campfire.gltf");
|