@biolibs/bionet-scene 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +54 -2
- package/dist/explorer.d.ts +57 -0
- package/dist/explorer.d.ts.map +1 -0
- package/dist/explorer.js +77 -0
- package/dist/explorer.js.map +1 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +18 -0
- package/dist/index.js.map +1 -0
- package/dist/scene.d.ts +49 -0
- package/dist/scene.d.ts.map +1 -0
- package/dist/scene.js +239 -0
- package/dist/scene.js.map +1 -0
- package/dist/view.d.ts +132 -0
- package/dist/view.d.ts.map +1 -0
- package/dist/view.js +161 -0
- package/dist/view.js.map +1 -0
- package/package.json +57 -4
- package/scene.css +43 -0
- package/src/explorer.tsx +112 -0
- package/src/index.ts +17 -0
- package/src/scene.tsx +337 -0
- package/src/view.ts +251 -0
package/src/scene.tsx
ADDED
|
@@ -0,0 +1,337 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The scene: a list of things to draw, drawn.
|
|
5
|
+
*
|
|
6
|
+
* Everything worth deciding was decided in `view.ts`. This turns each place into the edges of a
|
|
7
|
+
* box, each tube into a cylinder and each empty position into a ring, in the colours the theme
|
|
8
|
+
* gives for what they mean, and moves the camera to where the view says to stand.
|
|
9
|
+
*
|
|
10
|
+
* In the dark theme the edges are drawn brighter than white and a bloom pass makes them glow. In
|
|
11
|
+
* the light theme the same edges are drawn plainly on a pale ground: a glow needs something dark to
|
|
12
|
+
* glow against.
|
|
13
|
+
*/
|
|
14
|
+
import { Line, OrbitControls } from "@react-three/drei";
|
|
15
|
+
import { Canvas, useFrame, useThree, type ThreeEvent } from "@react-three/fiber";
|
|
16
|
+
import { Bloom, EffectComposer } from "@react-three/postprocessing";
|
|
17
|
+
import { useEffect, useLayoutEffect, useMemo, useRef, useState, type ReactNode, type RefObject } from "react";
|
|
18
|
+
import { BoxGeometry, Color, CylinderGeometry, EdgesGeometry, Object3D, QuadraticBezierCurve3, RingGeometry, Vector3,
|
|
19
|
+
type InstancedMesh, type Mesh } from "three";
|
|
20
|
+
import { colour, type Meaning, type Theme } from "@biolibs/bionet-theme";
|
|
21
|
+
import type { DrawnPlace, DrawnSlot, DrawnTube, Focus, SceneView } from "./view.js";
|
|
22
|
+
|
|
23
|
+
/** Another lab, drawn beside this one. */
|
|
24
|
+
export interface Neighbour {
|
|
25
|
+
/** Something to tell it apart by, such as its address. */
|
|
26
|
+
id: string;
|
|
27
|
+
/** What it is called. */
|
|
28
|
+
name: string;
|
|
29
|
+
/** A line about it, such as how many parts it offers. */
|
|
30
|
+
note?: string;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const BOX = new BoxGeometry(1, 1, 1);
|
|
34
|
+
const EDGES = new EdgesGeometry(BOX);
|
|
35
|
+
const TUBE = new CylinderGeometry(1, 1, 1, 24);
|
|
36
|
+
const RING = new RingGeometry(0.62, 1, 28).rotateX(-Math.PI / 2);
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* The colour and opacity to draw something in.
|
|
40
|
+
*
|
|
41
|
+
* @param tone - What its colour means.
|
|
42
|
+
* @param theme - Which theme is showing.
|
|
43
|
+
* @param strength - How strongly to draw it, from 0 to 1.
|
|
44
|
+
* @returns In the dark theme a colour that may be brighter than white, which is what the bloom
|
|
45
|
+
* picks up; in the light theme the plain colour, made fainter by opacity.
|
|
46
|
+
*/
|
|
47
|
+
export function paint(tone: Meaning, theme: Theme, strength: number): { color: Color; opacity: number } {
|
|
48
|
+
const color = new Color(colour(tone, theme));
|
|
49
|
+
return theme === "dark" ? { color: color.multiplyScalar(0.3 + strength * 2.3), opacity: 1 }
|
|
50
|
+
: { color, opacity: 0.22 + 0.78 * strength };
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Which theme the system is asking for.
|
|
55
|
+
*
|
|
56
|
+
* @returns `dark` or `light`, and again whenever the system changes its mind.
|
|
57
|
+
*/
|
|
58
|
+
export function useSystemTheme(): Theme {
|
|
59
|
+
const [theme, setTheme] = useState<Theme>("dark");
|
|
60
|
+
useEffect(() => {
|
|
61
|
+
const query = window.matchMedia("(prefers-color-scheme: dark)");
|
|
62
|
+
const read = (): void => setTheme(query.matches ? "dark" : "light");
|
|
63
|
+
read();
|
|
64
|
+
query.addEventListener("change", read);
|
|
65
|
+
return () => query.removeEventListener("change", read);
|
|
66
|
+
}, []);
|
|
67
|
+
return theme;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** How big another lab is drawn, in metres. */
|
|
71
|
+
const NEIGHBOUR_SIZE: [number, number, number] = [1.8, 1.5, 1.8];
|
|
72
|
+
|
|
73
|
+
/** Something written over the scene, at a point in it. */
|
|
74
|
+
interface Label {
|
|
75
|
+
/** What tells it apart from the others. */
|
|
76
|
+
key: string;
|
|
77
|
+
/** The point it is written at. */
|
|
78
|
+
at: [number, number, number];
|
|
79
|
+
/** What is written. */
|
|
80
|
+
content: ReactNode;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Keeps each label over its point as the camera moves.
|
|
85
|
+
*
|
|
86
|
+
* The labels are ordinary elements beside the canvas, drawn by the same React as everything else.
|
|
87
|
+
* This only moves them: once a frame it works out where each point falls on screen.
|
|
88
|
+
*/
|
|
89
|
+
function Follow({ labels, layer }: { labels: readonly Label[]; layer: RefObject<HTMLDivElement | null> }) {
|
|
90
|
+
const point = useMemo(() => new Vector3(), []);
|
|
91
|
+
useFrame(({ camera, size }) => {
|
|
92
|
+
const elements = layer.current?.children;
|
|
93
|
+
if (elements === undefined) return;
|
|
94
|
+
labels.forEach((label, index) => {
|
|
95
|
+
const element = elements[index] as HTMLElement | undefined;
|
|
96
|
+
if (element === undefined) return;
|
|
97
|
+
point.set(...label.at).project(camera);
|
|
98
|
+
// Past the far side of the camera a point still projects onto the screen, mirrored.
|
|
99
|
+
const behind = point.z > 1 || point.z < -1;
|
|
100
|
+
element.style.visibility = behind ? "hidden" : "visible";
|
|
101
|
+
element.style.transform = `translate(${((point.x + 1) / 2) * size.width}px, ${((1 - point.y) / 2) * size.height}px) translate(-50%, -50%)`;
|
|
102
|
+
});
|
|
103
|
+
});
|
|
104
|
+
return null;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** A click that was really the end of a drag is not a click. */
|
|
108
|
+
const dragged = (event: ThreeEvent<MouseEvent>): boolean => event.delta > 4;
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Whether a press on this was meant for it.
|
|
112
|
+
*
|
|
113
|
+
* A freezer stands inside a room, so a press on the freezer passes through the room's wall first.
|
|
114
|
+
* The press belongs to the smallest thing under the pointer, not the nearest.
|
|
115
|
+
*/
|
|
116
|
+
const meantFor = (event: ThreeEvent<MouseEvent>, volume: number): boolean =>
|
|
117
|
+
!dragged(event) && event.intersections.every((hit) => {
|
|
118
|
+
const other = (hit.object.userData as { volume?: number }).volume;
|
|
119
|
+
return other === undefined || other >= volume;
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
function PlaceBox({ place, theme, onPick }: { place: DrawnPlace; theme: Theme; onPick: (id: string) => void }) {
|
|
123
|
+
const edge = paint(place.tone, theme, place.strength);
|
|
124
|
+
const face = colour(place.tone, theme);
|
|
125
|
+
const volume = place.size[0] * place.size[1] * place.size[2];
|
|
126
|
+
return (
|
|
127
|
+
<group position={place.center}>
|
|
128
|
+
<lineSegments scale={place.size} geometry={EDGES}>
|
|
129
|
+
<lineBasicMaterial color={edge.color} transparent opacity={edge.opacity} toneMapped={false} />
|
|
130
|
+
</lineSegments>
|
|
131
|
+
{(place.focusable || place.fill > 0) && (
|
|
132
|
+
<mesh scale={place.size} geometry={BOX} userData={place.focusable ? { volume } : {}}
|
|
133
|
+
onClick={place.focusable ? (event) => { if (meantFor(event, volume)) onPick(place.id); } : undefined}
|
|
134
|
+
onPointerOver={place.focusable ? (event) => { event.stopPropagation(); document.body.style.cursor = "pointer"; } : undefined}
|
|
135
|
+
onPointerOut={place.focusable ? () => { document.body.style.cursor = ""; } : undefined}>
|
|
136
|
+
<meshBasicMaterial color={face} transparent opacity={theme === "dark" ? place.fill : place.fill * 1.6} depthWrite={false} toneMapped={false} />
|
|
137
|
+
</mesh>
|
|
138
|
+
)}
|
|
139
|
+
</group>
|
|
140
|
+
);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* The tubes of a box: a quiet body with a bright rim.
|
|
145
|
+
*
|
|
146
|
+
* A box of solid glowing cylinders reads as one bright slab. Only the rims glow, so each tube stays
|
|
147
|
+
* its own.
|
|
148
|
+
*/
|
|
149
|
+
function Tubes({ tubes, theme, onPick }: { tubes: DrawnTube[]; theme: Theme; onPick: (tube: DrawnTube) => void }) {
|
|
150
|
+
const bodies = useRef<InstancedMesh>(null);
|
|
151
|
+
const rims = useRef<InstancedMesh>(null);
|
|
152
|
+
useLayoutEffect(() => {
|
|
153
|
+
const body = bodies.current;
|
|
154
|
+
const rim = rims.current;
|
|
155
|
+
if (body === null || rim === null) return;
|
|
156
|
+
const at = new Object3D();
|
|
157
|
+
tubes.forEach((tube, index) => {
|
|
158
|
+
const tone = tube.selected ? "ink" : tube.tone;
|
|
159
|
+
at.position.set(...tube.center);
|
|
160
|
+
at.scale.set(tube.radius, tube.height, tube.radius);
|
|
161
|
+
at.updateMatrix();
|
|
162
|
+
body.setMatrixAt(index, at.matrix);
|
|
163
|
+
body.setColorAt(index, theme === "dark" ? new Color(colour(tone, theme)).multiplyScalar(tube.selected ? 0.8 : 0.34) : new Color(colour(tone, theme)));
|
|
164
|
+
at.position.set(tube.center[0], tube.center[1] + tube.height / 2 + 0.0004, tube.center[2]);
|
|
165
|
+
at.scale.set(tube.radius, 1, tube.radius);
|
|
166
|
+
at.updateMatrix();
|
|
167
|
+
rim.setMatrixAt(index, at.matrix);
|
|
168
|
+
rim.setColorAt(index, theme === "dark" ? paint(tone, theme, tube.selected ? 1 : 0.6).color : new Color(colour("surface", theme)));
|
|
169
|
+
});
|
|
170
|
+
for (const mesh of [body, rim]) {
|
|
171
|
+
mesh.instanceMatrix.needsUpdate = true;
|
|
172
|
+
if (mesh.instanceColor !== null) mesh.instanceColor.needsUpdate = true;
|
|
173
|
+
mesh.computeBoundingSphere();
|
|
174
|
+
}
|
|
175
|
+
}, [tubes, theme]);
|
|
176
|
+
if (tubes.length === 0) return null;
|
|
177
|
+
return (
|
|
178
|
+
<>
|
|
179
|
+
<instancedMesh ref={bodies} key={`body-${tubes.length}`} args={[TUBE, undefined, tubes.length]} userData={{ volume: 0 }}
|
|
180
|
+
onClick={(event) => { if (dragged(event) || event.instanceId === undefined) return; event.stopPropagation(); onPick(tubes[event.instanceId]!); }}
|
|
181
|
+
onPointerOver={() => { document.body.style.cursor = "pointer"; }} onPointerOut={() => { document.body.style.cursor = ""; }}>
|
|
182
|
+
<meshBasicMaterial toneMapped={false} />
|
|
183
|
+
</instancedMesh>
|
|
184
|
+
<instancedMesh ref={rims} key={`rim-${tubes.length}`} args={[RING, undefined, tubes.length]}>
|
|
185
|
+
<meshBasicMaterial toneMapped={false} />
|
|
186
|
+
</instancedMesh>
|
|
187
|
+
</>
|
|
188
|
+
);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
function EmptySlots({ slots, theme }: { slots: DrawnSlot[]; theme: Theme }) {
|
|
192
|
+
const mesh = useRef<InstancedMesh>(null);
|
|
193
|
+
useLayoutEffect(() => {
|
|
194
|
+
const instanced = mesh.current;
|
|
195
|
+
if (instanced === null) return;
|
|
196
|
+
const at = new Object3D();
|
|
197
|
+
slots.forEach((slot, index) => {
|
|
198
|
+
at.position.set(slot.center[0], slot.center[1] + 0.0005, slot.center[2]);
|
|
199
|
+
at.scale.set(slot.radius, 1, slot.radius);
|
|
200
|
+
at.updateMatrix();
|
|
201
|
+
instanced.setMatrixAt(index, at.matrix);
|
|
202
|
+
});
|
|
203
|
+
instanced.instanceMatrix.needsUpdate = true;
|
|
204
|
+
instanced.computeBoundingSphere();
|
|
205
|
+
}, [slots]);
|
|
206
|
+
if (slots.length === 0) return null;
|
|
207
|
+
const ring = paint("dim", theme, 0.35);
|
|
208
|
+
return (
|
|
209
|
+
<instancedMesh ref={mesh} key={slots.length} args={[RING, undefined, slots.length]}>
|
|
210
|
+
<meshBasicMaterial color={ring.color} transparent opacity={theme === "dark" ? 0.7 : 0.5} toneMapped={false} />
|
|
211
|
+
</instancedMesh>
|
|
212
|
+
);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/** Another lab: a quiet outline beside this one, and a line to it that something travels along. */
|
|
216
|
+
function NeighbourLab({ neighbour, from, to, theme, onPick }: {
|
|
217
|
+
neighbour: Neighbour; from: [number, number, number]; to: [number, number, number]; theme: Theme; onPick?: (id: string) => void;
|
|
218
|
+
}) {
|
|
219
|
+
const pulse = useRef<Mesh>(null);
|
|
220
|
+
const curve = useMemo(() => new QuadraticBezierCurve3(new Vector3(...from), new Vector3((from[0] + to[0]) / 2, from[1] + 1.6, (from[2] + to[2]) / 2), new Vector3(...to)), [from, to]);
|
|
221
|
+
const points = useMemo(() => curve.getPoints(40), [curve]);
|
|
222
|
+
useFrame(({ clock }) => { pulse.current?.position.copy(curve.getPoint((clock.elapsedTime * 0.22) % 1)); });
|
|
223
|
+
const edge = paint("theirs", theme, 0.7);
|
|
224
|
+
const size = NEIGHBOUR_SIZE;
|
|
225
|
+
return (
|
|
226
|
+
<>
|
|
227
|
+
<group position={[to[0] + size[0] / 2, size[1] / 2, to[2]]}>
|
|
228
|
+
<lineSegments scale={size} geometry={EDGES}>
|
|
229
|
+
<lineBasicMaterial color={edge.color} transparent opacity={edge.opacity} toneMapped={false} />
|
|
230
|
+
</lineSegments>
|
|
231
|
+
<mesh scale={size} geometry={BOX} onClick={onPick === undefined ? undefined : (event) => { if (dragged(event)) return; event.stopPropagation(); onPick(neighbour.id); }}>
|
|
232
|
+
<meshBasicMaterial color={colour("theirs", theme)} transparent opacity={0.03} depthWrite={false} toneMapped={false} />
|
|
233
|
+
</mesh>
|
|
234
|
+
</group>
|
|
235
|
+
<Line points={points} color={paint("theirs", theme, 0.5).color} lineWidth={1.2} transparent opacity={theme === "dark" ? 0.9 : 0.6} toneMapped={false} />
|
|
236
|
+
<mesh ref={pulse}>
|
|
237
|
+
<sphereGeometry args={[0.07, 16, 16]} />
|
|
238
|
+
<meshBasicMaterial color={paint("yours", theme, 1).color} toneMapped={false} />
|
|
239
|
+
</mesh>
|
|
240
|
+
</>
|
|
241
|
+
);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/** Moves the camera to where the view says to stand, then leaves it to the person. */
|
|
245
|
+
function Rig({ view }: { view: SceneView }) {
|
|
246
|
+
const camera = useThree((state) => state.camera);
|
|
247
|
+
const controls = useThree((state) => state.controls) as unknown as { target: Vector3; update: () => void } | null;
|
|
248
|
+
const left = useRef(0);
|
|
249
|
+
const first = useRef(true);
|
|
250
|
+
const position = useMemo(() => new Vector3(...view.pose.position), [view.pose]);
|
|
251
|
+
const target = useMemo(() => new Vector3(...view.pose.target), [view.pose]);
|
|
252
|
+
useEffect(() => { left.current = 1.6; }, [position, target]);
|
|
253
|
+
useFrame((_, delta) => {
|
|
254
|
+
if (controls === null || left.current <= 0) return;
|
|
255
|
+
if (first.current) {
|
|
256
|
+
// The first look is not a journey: start where the view says to stand.
|
|
257
|
+
first.current = false;
|
|
258
|
+
camera.position.copy(position);
|
|
259
|
+
controls.target.copy(target);
|
|
260
|
+
left.current = 0;
|
|
261
|
+
} else {
|
|
262
|
+
left.current -= delta;
|
|
263
|
+
const step = 1 - Math.exp(-delta * 5.5);
|
|
264
|
+
camera.position.lerp(position, step);
|
|
265
|
+
controls.target.lerp(target, step);
|
|
266
|
+
}
|
|
267
|
+
controls.update();
|
|
268
|
+
});
|
|
269
|
+
return null;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/**
|
|
273
|
+
* A lab, drawn.
|
|
274
|
+
*
|
|
275
|
+
* It holds no state about the lab and decides nothing about it: it is given a view from
|
|
276
|
+
* `sceneView` and reports what was pressed.
|
|
277
|
+
*
|
|
278
|
+
* @param props - `view` is what to draw; `theme` picks the colours; `onFocus` is told which place or
|
|
279
|
+
* tube was pressed; `neighbours` are other labs to draw beside this one, and `onNeighbour` is told
|
|
280
|
+
* when one is pressed.
|
|
281
|
+
*/
|
|
282
|
+
export function LabScene({ view, theme, onFocus, neighbours = [], onNeighbour }: {
|
|
283
|
+
view: SceneView; theme: Theme; onFocus: (focus: Focus) => void; neighbours?: readonly Neighbour[]; onNeighbour?: (id: string) => void;
|
|
284
|
+
}) {
|
|
285
|
+
const rooms = view.places.filter((place) => place.kind === "room");
|
|
286
|
+
const right = Math.max(0, ...rooms.map((room) => room.center[0] + room.size[0] / 2));
|
|
287
|
+
const middle = rooms.length === 0 ? 0 : rooms.reduce((sum, room) => sum + room.center[2], 0) / rooms.length;
|
|
288
|
+
const rule = paint("rule", theme, 0.5);
|
|
289
|
+
const beside = neighbours.map((neighbour, index) => ({
|
|
290
|
+
neighbour,
|
|
291
|
+
from: [right, 1.2, middle + index * 0.4] as [number, number, number],
|
|
292
|
+
to: [right + 1.2, 0.8, middle + (index - (neighbours.length - 1) / 2) * 2.6] as [number, number, number],
|
|
293
|
+
}));
|
|
294
|
+
const picked = view.tubes.find((tube) => tube.selected);
|
|
295
|
+
const labels: Label[] = [
|
|
296
|
+
...view.places.filter((place) => place.labelled).map((place): Label => ({
|
|
297
|
+
key: place.id, at: [place.center[0], place.center[1] + place.size[1] / 2, place.center[2]],
|
|
298
|
+
content: <span className={`tag tone-${place.tone}`}>{place.name}</span>,
|
|
299
|
+
})),
|
|
300
|
+
...beside.map(({ neighbour, to }): Label => ({
|
|
301
|
+
key: `lab ${neighbour.id}`, at: [to[0] + NEIGHBOUR_SIZE[0] / 2, NEIGHBOUR_SIZE[1], to[2]],
|
|
302
|
+
content: <span className="tag tone-theirs">{neighbour.name}{neighbour.note !== undefined && <small>{neighbour.note}</small>}</span>,
|
|
303
|
+
})),
|
|
304
|
+
...(picked === undefined ? [] : [{
|
|
305
|
+
key: `tube ${picked.item}`, at: [picked.center[0], picked.center[1] + picked.height, picked.center[2]] as [number, number, number],
|
|
306
|
+
content: <span className="tag tone-ink raised">{picked.name} · {picked.position}</span>,
|
|
307
|
+
}]),
|
|
308
|
+
];
|
|
309
|
+
const layer = useRef<HTMLDivElement>(null);
|
|
310
|
+
return (
|
|
311
|
+
<div className="stage">
|
|
312
|
+
<Canvas camera={{ fov: 45, near: 0.01, far: 400, position: view.pose.position }} dpr={[1, 2]} gl={{ antialias: true }}>
|
|
313
|
+
<color attach="background" args={[colour("ground", theme)]} />
|
|
314
|
+
<gridHelper args={[60, 60, rule.color, rule.color]} position={[0, -0.002, 0]}>
|
|
315
|
+
<lineBasicMaterial attach="material" color={rule.color} transparent opacity={theme === "dark" ? 0.5 : 0.7} toneMapped={false} />
|
|
316
|
+
</gridHelper>
|
|
317
|
+
{view.places.map((place) => <PlaceBox key={place.id} place={place} theme={theme} onPick={(id) => onFocus({ place: id })} />)}
|
|
318
|
+
<EmptySlots slots={view.empty} theme={theme} />
|
|
319
|
+
<Tubes tubes={view.tubes} theme={theme} onPick={(tube) => onFocus({ place: tube.place, item: tube.item })} />
|
|
320
|
+
{beside.map(({ neighbour, from, to }) => (
|
|
321
|
+
<NeighbourLab key={neighbour.id} neighbour={neighbour} theme={theme} onPick={onNeighbour} from={from} to={to} />
|
|
322
|
+
))}
|
|
323
|
+
<Follow labels={labels} layer={layer} />
|
|
324
|
+
<OrbitControls makeDefault enableDamping dampingFactor={0.12} maxPolarAngle={Math.PI / 2 - 0.03} minDistance={0.08} maxDistance={60} />
|
|
325
|
+
<Rig view={view} />
|
|
326
|
+
{theme === "dark" && (
|
|
327
|
+
<EffectComposer>
|
|
328
|
+
<Bloom mipmapBlur intensity={0.85} luminanceThreshold={0.55} luminanceSmoothing={0.3} />
|
|
329
|
+
</EffectComposer>
|
|
330
|
+
)}
|
|
331
|
+
</Canvas>
|
|
332
|
+
<div className="pins" ref={layer}>
|
|
333
|
+
{labels.map((label) => <div key={label.key} className="pin">{label.content}</div>)}
|
|
334
|
+
</div>
|
|
335
|
+
</div>
|
|
336
|
+
);
|
|
337
|
+
}
|
package/src/view.ts
ADDED
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What to draw, worked out as plain data.
|
|
3
|
+
*
|
|
4
|
+
* A scene of a lab is the same lab seen from different distances: the whole room, one freezer, one
|
|
5
|
+
* box, one tube. What changes with distance is what is worth drawing and how strongly. That is
|
|
6
|
+
* decided here, by a function from the lab and what is being looked at to a list of things to draw,
|
|
7
|
+
* and nothing in this file knows how to draw anything.
|
|
8
|
+
*
|
|
9
|
+
* Keeping it apart is what makes the scene checkable. Whether a search match lights up its box, or
|
|
10
|
+
* a tube needing a count is flagged, is a question about data and is tested as one. The part that
|
|
11
|
+
* turns the list into lines and cylinders is left with nothing to decide.
|
|
12
|
+
*
|
|
13
|
+
* Colours are given as meanings from `@biolibs/bionet-theme`, never as colours: `yours` for the
|
|
14
|
+
* lab's own structure, `held` for a box or tube with something in it, `found` for a search match,
|
|
15
|
+
* `attention` for something that needs a person. The scene looks the colour up for whichever theme
|
|
16
|
+
* is showing.
|
|
17
|
+
*/
|
|
18
|
+
import { arrange, firstFreeSlot, levelOf, poseFor, slotCenter, slotName, slotSize, type Arranged, type Layout, type Level,
|
|
19
|
+
type Pose, type Slot, type Stored } from "@biolibs/bionet-space";
|
|
20
|
+
import type { Meaning } from "@biolibs/bionet-theme";
|
|
21
|
+
|
|
22
|
+
/** What is being looked at: a place, or the whole lab, and perhaps one item in it. */
|
|
23
|
+
export interface Focus {
|
|
24
|
+
/** The place, or `null` for the whole lab. */
|
|
25
|
+
place: string | null;
|
|
26
|
+
/** One item singled out, if any. */
|
|
27
|
+
item?: string | null;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** One place to draw, as a box. */
|
|
31
|
+
export interface DrawnPlace {
|
|
32
|
+
id: string;
|
|
33
|
+
kind: string;
|
|
34
|
+
name: string;
|
|
35
|
+
/** The centre of the box, in metres. */
|
|
36
|
+
center: [number, number, number];
|
|
37
|
+
/** How wide, tall and deep it is. */
|
|
38
|
+
size: [number, number, number];
|
|
39
|
+
/** What its colour means. */
|
|
40
|
+
tone: Meaning;
|
|
41
|
+
/** How strongly to draw its edges, from 0 to 1. */
|
|
42
|
+
strength: number;
|
|
43
|
+
/** How solid its faces are, from 0 (edges only) to 1. */
|
|
44
|
+
fill: number;
|
|
45
|
+
/** Whether pressing it should look at it. */
|
|
46
|
+
focusable: boolean;
|
|
47
|
+
/** Whether to write its name beside it. */
|
|
48
|
+
labelled: boolean;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** One tube to draw, standing in its position. */
|
|
52
|
+
export interface DrawnTube {
|
|
53
|
+
item: string;
|
|
54
|
+
/** The box it is in. */
|
|
55
|
+
place: string;
|
|
56
|
+
/** The centre of the tube. */
|
|
57
|
+
center: [number, number, number];
|
|
58
|
+
radius: number;
|
|
59
|
+
height: number;
|
|
60
|
+
tone: Meaning;
|
|
61
|
+
/** Whether it is the one singled out. */
|
|
62
|
+
selected: boolean;
|
|
63
|
+
/** What the part is called. */
|
|
64
|
+
name: string;
|
|
65
|
+
/** How much there is, with its unit. */
|
|
66
|
+
amount: string;
|
|
67
|
+
/** Its position, as a lid names it, like `C4`. */
|
|
68
|
+
position: string;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** One empty position to draw. */
|
|
72
|
+
export interface DrawnSlot {
|
|
73
|
+
center: [number, number, number];
|
|
74
|
+
radius: number;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** One step of the way from the whole lab to what is being looked at. */
|
|
78
|
+
export interface Crumb {
|
|
79
|
+
/** The place, or `null` for the whole lab. */
|
|
80
|
+
id: string | null;
|
|
81
|
+
name: string;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** Everything to draw for one look at a lab. */
|
|
85
|
+
export interface SceneView {
|
|
86
|
+
/** Every place worth drawing. */
|
|
87
|
+
places: DrawnPlace[];
|
|
88
|
+
/** The tubes of the box being looked at. */
|
|
89
|
+
tubes: DrawnTube[];
|
|
90
|
+
/** The empty positions of that box. */
|
|
91
|
+
empty: DrawnSlot[];
|
|
92
|
+
/** Where the camera should stand. */
|
|
93
|
+
pose: Pose;
|
|
94
|
+
/** The way here, from the whole lab. */
|
|
95
|
+
crumbs: Crumb[];
|
|
96
|
+
/** How close this look is. */
|
|
97
|
+
level: Level;
|
|
98
|
+
/** How many items are in what is being looked at, however deep. */
|
|
99
|
+
holding: number;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** What `sceneView` can be told beyond the lab and the focus. */
|
|
103
|
+
export interface ViewOptions {
|
|
104
|
+
/** Items a search has matched. Their boxes and tubes are drawn as found. */
|
|
105
|
+
matches?: ReadonlySet<string>;
|
|
106
|
+
/** What to call the whole lab in the crumbs. */
|
|
107
|
+
labName?: string;
|
|
108
|
+
/** The camera's vertical field of view, in degrees. */
|
|
109
|
+
fov?: number;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** A tube's height, in metres. */
|
|
113
|
+
const TUBE_HEIGHT = 0.04;
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Work out what to draw.
|
|
117
|
+
*
|
|
118
|
+
* @param layout - The lab: its places and what is stored in them.
|
|
119
|
+
* @param focus - What is being looked at.
|
|
120
|
+
* @param options - Search matches, the lab's name, the camera's field of view.
|
|
121
|
+
* @returns Everything to draw, and where to draw it from.
|
|
122
|
+
*
|
|
123
|
+
* @example
|
|
124
|
+
* ```ts
|
|
125
|
+
* import { exampleLab } from "@biolibs/bionet-space";
|
|
126
|
+
* import { sceneView } from "@biolibs/bionet-scene";
|
|
127
|
+
*
|
|
128
|
+
* const lab = exampleLab();
|
|
129
|
+
* sceneView(lab, { place: null }).tubes.length; // 0: from across the room, no tube is worth drawing
|
|
130
|
+
* sceneView(lab, { place: "fridge-s1-1" }).level; // "box"
|
|
131
|
+
* sceneView(lab, { place: "fridge-s1-1" }).crumbs.map((c) => c.name);
|
|
132
|
+
* // [ "Lab", "Main lab", "4 °C fridge", "Shelf 1", "Box F1-1" ]
|
|
133
|
+
* ```
|
|
134
|
+
*/
|
|
135
|
+
export function sceneView(layout: Layout, focus: Focus, options: ViewOptions = {}): SceneView {
|
|
136
|
+
const arranged = arrange(layout);
|
|
137
|
+
const by = new Map(arranged.map((one) => [one.place.id, one]));
|
|
138
|
+
const focused = focus.place === null ? null : by.get(focus.place) ?? null;
|
|
139
|
+
const level = levelOf(focused);
|
|
140
|
+
const matches = options.matches ?? new Set<string>();
|
|
141
|
+
|
|
142
|
+
// What each place holds, itself and everything inside it.
|
|
143
|
+
const direct = new Map<string, Stored[]>();
|
|
144
|
+
for (const one of layout.stored) direct.set(one.place, [...(direct.get(one.place) ?? []), one]);
|
|
145
|
+
const within = new Map<string, Stored[]>();
|
|
146
|
+
for (const one of arranged) {
|
|
147
|
+
for (const item of direct.get(one.place.id) ?? []) {
|
|
148
|
+
for (const id of one.path) within.set(id, [...(within.get(id) ?? []), item]);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** Whether a place is what is looked at, something it is inside, or something inside it. */
|
|
153
|
+
const near = (one: Arranged): boolean =>
|
|
154
|
+
focused === null || focused.path.includes(one.place.id) || one.path.includes(focused.place.id);
|
|
155
|
+
/** The thing standing in a room that a place belongs to: its freezer, its bench. */
|
|
156
|
+
const unitOf = (one: Arranged): string | undefined => one.path[1];
|
|
157
|
+
const focusedUnit = focused === null ? undefined : unitOf(focused);
|
|
158
|
+
|
|
159
|
+
const places: DrawnPlace[] = [];
|
|
160
|
+
for (const one of arranged) {
|
|
161
|
+
const { place } = one;
|
|
162
|
+
const held = within.get(place.id) ?? [];
|
|
163
|
+
const base = { id: place.id, kind: place.kind, name: place.name, center: one.center, size: one.size };
|
|
164
|
+
const depth = one.path.length;
|
|
165
|
+
const isFocus = focused !== null && place.id === focused.place.id;
|
|
166
|
+
const childOfFocus = focused !== null && place.parent === focused.place.id;
|
|
167
|
+
|
|
168
|
+
if (focused !== null && focused.place.grid !== undefined && !isFocus) {
|
|
169
|
+
// A box is looked into as if it had been taken out: what it was standing in is left as a faint
|
|
170
|
+
// outline to say where it came from, and nothing else is drawn, since all of it would be in the way.
|
|
171
|
+
if (focused.path.includes(place.id)) places.push({ ...base, tone: depth === 1 ? "dim" : "yours", strength: 0.1, fill: 0, focusable: false, labelled: false });
|
|
172
|
+
} else if (depth === 1) {
|
|
173
|
+
// A room is the frame everything else stands in, and is drawn quietly.
|
|
174
|
+
places.push({ ...base, tone: "dim", strength: level === "lab" || level === "room" ? 0.55 : 0.28, fill: 0,
|
|
175
|
+
focusable: level === "lab", labelled: level === "lab" });
|
|
176
|
+
} else if (depth === 2 || place.kind === "instrument") {
|
|
177
|
+
// What stands in a room, and what stands on a bench.
|
|
178
|
+
const close = near(one) || (place.kind === "instrument" && focusedUnit === unitOf(one));
|
|
179
|
+
places.push({ ...base, tone: "yours", strength: close ? (place.kind === "bench" ? 0.6 : 1) : 0.25, fill: close ? 0.035 : 0,
|
|
180
|
+
focusable: true, labelled: isFocus || childOfFocus || level === "lab" || level === "room" });
|
|
181
|
+
} else if (place.grid !== undefined) {
|
|
182
|
+
// A box. From across the room it is a small slab that says whether anything is in it.
|
|
183
|
+
const inUnit = focusedUnit !== undefined && unitOf(one) === focusedUnit;
|
|
184
|
+
const found = held.some((item) => matches.has(item.item));
|
|
185
|
+
const tone: Meaning = found ? "found" : held.some((item) => item.needsRecount === true) ? "attention" : held.length > 0 ? "held" : "dim";
|
|
186
|
+
places.push({ ...base, tone, strength: found || isFocus ? 1 : inUnit ? (near(one) ? 0.85 : 0.5) : 0.3,
|
|
187
|
+
fill: held.length === 0 ? 0 : isFocus ? 0.05 : 0.14, focusable: inUnit, labelled: isFocus || childOfFocus });
|
|
188
|
+
} else if (focusedUnit !== undefined && unitOf(one) === focusedUnit) {
|
|
189
|
+
// A shelf or a rack, drawn only once its freezer is being looked into.
|
|
190
|
+
places.push({ ...base, tone: "yours", strength: near(one) ? 0.55 : 0.25, fill: 0, focusable: true, labelled: isFocus || childOfFocus });
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
// The tubes and empty positions of the box being looked at.
|
|
195
|
+
const tubes: DrawnTube[] = [];
|
|
196
|
+
const empty: DrawnSlot[] = [];
|
|
197
|
+
if (focused !== null && focused.place.grid !== undefined) {
|
|
198
|
+
const side = slotSize(focused);
|
|
199
|
+
const height = Math.min(TUBE_HEIGHT, focused.size[1] * 0.8);
|
|
200
|
+
const taken: Slot[] = [];
|
|
201
|
+
const items = direct.get(focused.place.id) ?? [];
|
|
202
|
+
// An item with no position of its own is stood in the first free one, so it can still be seen.
|
|
203
|
+
const placed = items.filter((item) => item.slot !== undefined && item.slot !== null && slotCenter(focused, item.slot) !== null);
|
|
204
|
+
for (const item of placed) taken.push(item.slot!);
|
|
205
|
+
const stand = (item: Stored, slot: Slot): void => {
|
|
206
|
+
const at = slotCenter(focused, slot)!;
|
|
207
|
+
tubes.push({
|
|
208
|
+
item: item.item, place: focused.place.id, center: [at[0], at[1] + height / 2, at[2]], radius: side * 0.36, height,
|
|
209
|
+
tone: matches.has(item.item) ? "found" : item.needsRecount === true ? "attention" : "held",
|
|
210
|
+
selected: focus.item === item.item, name: item.name,
|
|
211
|
+
amount: `${item.amount}${item.unit === null ? "" : ` ${item.unit}`}`, position: slotName(slot),
|
|
212
|
+
});
|
|
213
|
+
};
|
|
214
|
+
for (const item of placed) stand(item, item.slot!);
|
|
215
|
+
for (const item of items.filter((one) => !placed.includes(one))) {
|
|
216
|
+
const free = firstFreeSlot(focused.place, taken);
|
|
217
|
+
if (free === null) break;
|
|
218
|
+
taken.push(free);
|
|
219
|
+
stand(item, free);
|
|
220
|
+
}
|
|
221
|
+
const used = new Set(taken.map((slot) => `${slot.row},${slot.column}`));
|
|
222
|
+
for (let row = 0; row < focused.place.grid.rows; row++) {
|
|
223
|
+
for (let column = 0; column < focused.place.grid.columns; column++) {
|
|
224
|
+
if (used.has(`${row},${column}`)) continue;
|
|
225
|
+
empty.push({ center: slotCenter(focused, { row, column })!, radius: side * 0.3 });
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
return {
|
|
231
|
+
places,
|
|
232
|
+
tubes,
|
|
233
|
+
empty,
|
|
234
|
+
pose: poseFor(arranged, focused === null ? null : focused.place.id, options.fov ?? 45),
|
|
235
|
+
crumbs: [{ id: null, name: options.labName ?? "Lab" }, ...(focused?.path ?? []).map((id) => ({ id, name: by.get(id)!.place.name }))],
|
|
236
|
+
level,
|
|
237
|
+
holding: focused === null ? layout.stored.length : (within.get(focused.place.id) ?? []).length,
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* Where to look to see one item: its box, with the item singled out.
|
|
243
|
+
*
|
|
244
|
+
* @param layout - The lab.
|
|
245
|
+
* @param item - The item's ID.
|
|
246
|
+
* @returns The focus that shows it, or `null` if there is no such item.
|
|
247
|
+
*/
|
|
248
|
+
export function focusOn(layout: Pick<Layout, "stored">, item: string): Focus | null {
|
|
249
|
+
const found = layout.stored.find((one) => one.item === item);
|
|
250
|
+
return found === undefined ? null : { place: found.place, item };
|
|
251
|
+
}
|