"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(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(null); const foxRef = useRef(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 (
); } export default function CampfireViewer() { return (
{ gl.toneMapping = THREE.ACESFilmicToneMapping; gl.outputColorSpace = THREE.SRGBColorSpace; }} > { 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)}]}`); }} />
); } useGLTF.preload("/campfire.gltf");