File size: 3,611 Bytes
5a3b695
d9bb6b1
 
 
 
 
 
 
5a3b695
 
d9bb6b1
 
 
 
 
 
 
 
 
 
 
 
5a3b695
 
d9bb6b1
5a3b695
 
 
 
 
 
d9bb6b1
5a3b695
 
 
 
 
d9bb6b1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5a3b695
d9bb6b1
 
 
 
 
 
 
 
5a3b695
d9bb6b1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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");