flyyt / src /FlyScene.tsx
gpue's picture
Deploy 771c7d3cfb0b08d632f7fe3d02e59b7bcdff47c4
5a3b695 verified
Raw History Blame Contribute Delete
3.61 kB
import { CameraControls, useGLTF } from "@react-three/drei";
import { Canvas } from "@react-three/fiber";
import { Suspense, useRef } from "react";
import CameraRig from "./CameraRig";
import { FLY_AREA_CONFIG } from "./env";
import FlyInstances from "./FlyInstances";
import FlyLabels from "./FlyLabels";
import MapView from "./MapView";
import RoomScene from "./RoomScene";
import { ROOM_CONFIG } from "./roomLayout";
import { FLY_EXTENT_MM, type FlyInstanceData } from "./useFlyLayout";
import type { LiveFlyPositions } from "./useLiveFlyState";
// Close-up "hero shot" distance/height when a specific fly is selected,
// derived from one fly's own size so every instance gets the same framing
// regardless of where in the ground area it sits. Magnitudes match the
// previous fixed diagonal offset's horizontal/vertical components, now
// applied relative to each fly's own current heading (see CameraRig).
const flyCamDist = Math.max(...FLY_EXTENT_MM) * 2.4;
const FOCUS_BACK_MM = flyCamDist * Math.hypot(0.7, 0.9);
const FOCUS_UP_MM = flyCamDist * 0.35;
// Overview camera/far-plane scale still derives from the configured ground
// area (not a single fly).
const areaSize = Math.max(FLY_AREA_CONFIG.widthMm, FLY_AREA_CONFIG.depthMm);
// Initial view: inside the room, in a free-floor corner, elevated partway
// up toward the ceiling and looking back across the room toward the
// center -- not floating outside the walls looking at the room as a
// closed box from above.
const INSIDE_WALL_MARGIN_MM = 300;
const OVERVIEW = {
position: [
-(ROOM_CONFIG.widthMm / 2 - INSIDE_WALL_MARGIN_MM),
ROOM_CONFIG.wallHeightMm * 0.65,
-(ROOM_CONFIG.depthMm / 2 - INSIDE_WALL_MARGIN_MM),
] as [number, number, number],
target: [0, 0, 0] as [number, number, number],
};
interface FlySceneProps {
flies: FlyInstanceData[];
selectedFlyId: string | null;
onSelectFly: (id: string | null) => void;
livePositionsRef: React.RefObject<LiveFlyPositions>;
joystickRef: React.RefObject<{ x: number; y: number }>;
wingSlidersRef: React.RefObject<{ left: number; right: number }>;
}
export default function FlyScene({
flies,
selectedFlyId,
onSelectFly,
livePositionsRef,
joystickRef,
wingSlidersRef,
}: FlySceneProps) {
const controlsRef = useRef<CameraControls | null>(null);
return (
<Canvas
camera={{ position: OVERVIEW.position, fov: 40, near: 1, far: areaSize * 20 }}
gl={{ preserveDrawingBuffer: true }}
>
<color attach="background" args={["#6b7280"]} />
<ambientLight intensity={0.6} />
<directionalLight position={[5, 8, 4]} intensity={1.4} />
<directionalLight position={[-6, 2, -4]} intensity={0.4} />
<Suspense fallback={null}>
<RoomScene />
<FlyInstances
flies={flies}
selectedFlyId={selectedFlyId}
onSelectFly={onSelectFly}
joystickRef={joystickRef}
wingSlidersRef={wingSlidersRef}
livePositionsRef={livePositionsRef}
/>
<FlyLabels flies={flies} livePositionsRef={livePositionsRef} />
</Suspense>
<MapView />
<CameraControls
ref={controlsRef}
makeDefault
smoothTime={0.4}
dollySpeed={2.5}
minDistance={flyCamDist * 0.3}
maxDistance={areaSize * 5}
/>
<CameraRig
controlsRef={controlsRef}
selectedFlyId={selectedFlyId}
focusBackMm={FOCUS_BACK_MM}
focusUpMm={FOCUS_UP_MM}
overview={OVERVIEW}
livePositionsRef={livePositionsRef}
/>
</Canvas>
);
}
useGLTF.preload("/models/fly.glb");