@agent-os-lab/agent-game-sdk 0.1.16 → 0.1.18
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 +19 -0
- package/USAGE.md +38 -4
- package/package.json +1 -1
- package/src/office/building-canvas.ts +16 -0
- package/src/office/core/types.ts +19 -0
- package/src/office/index.ts +1 -0
- package/src/office/react/OfficeBuildingCanvas.ts +87 -0
- package/src/office/react/index.ts +1 -0
- package/src/office/renderers/three/agent-animation.ts +7 -6
- package/src/office/renderers/three/agent-mesh.ts +29 -4
- package/src/office/renderers/three/building-canvas.ts +177 -0
package/README.md
CHANGED
|
@@ -82,6 +82,25 @@ export function Office() {
|
|
|
82
82
|
|
|
83
83
|
`agent-game-sdk/office` is renderer and framework neutral. `agent-game-sdk/office/react` is the React-only entrypoint.
|
|
84
84
|
|
|
85
|
+
Static building canvas:
|
|
86
|
+
|
|
87
|
+
```tsx
|
|
88
|
+
import type { AgentGameOfficeConfig } from "@agent-os-lab/agent-game-sdk/office";
|
|
89
|
+
import { OfficeBuildingCanvas } from "@agent-os-lab/agent-game-sdk/office/react";
|
|
90
|
+
|
|
91
|
+
export function BuildingPreview({ office }: { office: AgentGameOfficeConfig }) {
|
|
92
|
+
return (
|
|
93
|
+
<OfficeBuildingCanvas
|
|
94
|
+
office={office}
|
|
95
|
+
focusedFloorId="floor-2"
|
|
96
|
+
style={{ height: 360 }}
|
|
97
|
+
/>
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
Use `OfficeBuildingCanvas` or `mountOfficeBuildingCanvas` when you only need the static building model. It renders the same office scene without requiring a runtime source and without mounting agents, labels, activity effects, or movement state.
|
|
103
|
+
|
|
85
104
|
## Avatar Views
|
|
86
105
|
|
|
87
106
|
Use `mountAgentAvatar3D` when a tenant application needs the same 3D Agent shape used inside the office game view:
|
package/USAGE.md
CHANGED
|
@@ -37,8 +37,8 @@ import {
|
|
|
37
37
|
subscribeAgentPresenceList,
|
|
38
38
|
} from "@agent-os-lab/agent-game-sdk";
|
|
39
39
|
import { mountAgentAvatar3D, mountAgentAvatarCanvas } from "@agent-os-lab/agent-game-sdk/avatar";
|
|
40
|
-
import { mountAgentGameOffice } from "@agent-os-lab/agent-game-sdk/office";
|
|
41
|
-
import { AgentGameOfficeView } from "@agent-os-lab/agent-game-sdk/office/react";
|
|
40
|
+
import { mountAgentGameOffice, mountOfficeBuildingCanvas } from "@agent-os-lab/agent-game-sdk/office";
|
|
41
|
+
import { AgentGameOfficeView, OfficeBuildingCanvas } from "@agent-os-lab/agent-game-sdk/office/react";
|
|
42
42
|
import type { AgentPresence } from "@agent-os-lab/agent-game-sdk/office";
|
|
43
43
|
```
|
|
44
44
|
|
|
@@ -46,8 +46,8 @@ Available entry points:
|
|
|
46
46
|
|
|
47
47
|
- `@agent-os-lab/agent-game-sdk`: runtime clients and office exports.
|
|
48
48
|
- `@agent-os-lab/agent-game-sdk/avatar`: framework-neutral avatar view APIs.
|
|
49
|
-
- `@agent-os-lab/agent-game-sdk/office`: framework-neutral office view APIs and office types.
|
|
50
|
-
- `@agent-os-lab/agent-game-sdk/office/react`: React office view
|
|
49
|
+
- `@agent-os-lab/agent-game-sdk/office`: framework-neutral office view APIs, static building canvas APIs, and office types.
|
|
50
|
+
- `@agent-os-lab/agent-game-sdk/office/react`: React office view and static building canvas components.
|
|
51
51
|
|
|
52
52
|
Do not import files from `src/` in application code. Use the published entry points above.
|
|
53
53
|
|
|
@@ -383,6 +383,40 @@ export function Office() {
|
|
|
383
383
|
|
|
384
384
|
Keep `source` object identity stable across renders when possible. Recreating `source` every render can remount the view because the React wrapper treats it as an effect dependency. Equivalent inline `office` objects do not remount the view, but memoizing office config is still preferable when constructing it dynamically.
|
|
385
385
|
|
|
386
|
+
## Static Building Canvas
|
|
387
|
+
|
|
388
|
+
Use `OfficeBuildingCanvas` when a React surface needs the building model without agents or runtime state.
|
|
389
|
+
|
|
390
|
+
```tsx
|
|
391
|
+
import { useState } from "react";
|
|
392
|
+
import type {
|
|
393
|
+
AgentGameOfficeBuildingCanvasController,
|
|
394
|
+
AgentGameOfficeConfig,
|
|
395
|
+
} from "@agent-os-lab/agent-game-sdk/office";
|
|
396
|
+
import { OfficeBuildingCanvas } from "@agent-os-lab/agent-game-sdk/office/react";
|
|
397
|
+
|
|
398
|
+
export function BuildingPreview({ office }: { office: AgentGameOfficeConfig }) {
|
|
399
|
+
const [view, setView] = useState<AgentGameOfficeBuildingCanvasController | null>(null);
|
|
400
|
+
|
|
401
|
+
return (
|
|
402
|
+
<>
|
|
403
|
+
<button type="button" onClick={() => view?.resetCamera()}>
|
|
404
|
+
Reset building view
|
|
405
|
+
</button>
|
|
406
|
+
<OfficeBuildingCanvas
|
|
407
|
+
office={office}
|
|
408
|
+
focusedFloorId="floor-2"
|
|
409
|
+
style={{ height: 360 }}
|
|
410
|
+
onReady={setView}
|
|
411
|
+
onDestroy={() => setView(null)}
|
|
412
|
+
/>
|
|
413
|
+
</>
|
|
414
|
+
);
|
|
415
|
+
}
|
|
416
|
+
```
|
|
417
|
+
|
|
418
|
+
Framework-neutral callers can use `mountOfficeBuildingCanvas(container, { office })`. The static canvas reuses the office scene, camera, reset, and floor-focus behavior, but it does not accept a `source` and does not mount agents, labels, activity effects, movement, or runtime subscriptions.
|
|
419
|
+
|
|
386
420
|
## Snapshot Source
|
|
387
421
|
|
|
388
422
|
Use `source.type: "snapshot"` for demos, tests, static previews, or applications that already have agent presence data.
|
package/package.json
CHANGED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { resolveOfficeLayout } from "./layout";
|
|
2
|
+
import type {
|
|
3
|
+
AgentGameOfficeBuildingCanvasController,
|
|
4
|
+
AgentGameOfficeBuildingCanvasMountOptions,
|
|
5
|
+
} from "./core/types";
|
|
6
|
+
import { mountThreeOfficeBuildingCanvas } from "./renderers/three/building-canvas";
|
|
7
|
+
|
|
8
|
+
export function mountOfficeBuildingCanvas(
|
|
9
|
+
container: HTMLElement,
|
|
10
|
+
options: AgentGameOfficeBuildingCanvasMountOptions = {},
|
|
11
|
+
): AgentGameOfficeBuildingCanvasController {
|
|
12
|
+
return mountThreeOfficeBuildingCanvas(container, {
|
|
13
|
+
...options,
|
|
14
|
+
officeLayout: resolveOfficeLayout(options.office),
|
|
15
|
+
});
|
|
16
|
+
}
|
package/src/office/core/types.ts
CHANGED
|
@@ -149,3 +149,22 @@ export type AgentGameOfficeController = {
|
|
|
149
149
|
getNavigationErrors(): AgentNavigationError[];
|
|
150
150
|
destroy(): void;
|
|
151
151
|
};
|
|
152
|
+
|
|
153
|
+
export type AgentGameOfficeBuildingCanvasMountOptions = {
|
|
154
|
+
office?: AgentGameOfficeConfig;
|
|
155
|
+
focusedFloorId?: string | null;
|
|
156
|
+
className?: string;
|
|
157
|
+
onCameraChange?: (state: AgentGameOfficeCameraState) => void;
|
|
158
|
+
onFocusedFloorChange?: (floorId: string | null) => void;
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
export type AgentGameOfficeBuildingCanvasResolvedOptions = AgentGameOfficeBuildingCanvasMountOptions & {
|
|
162
|
+
officeLayout: ResolvedOfficeLayout;
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
export type AgentGameOfficeBuildingCanvasController = {
|
|
166
|
+
resetCamera(): void;
|
|
167
|
+
focusFloor(floorId: string | null): void;
|
|
168
|
+
getFocusedFloor(): string | null;
|
|
169
|
+
destroy(): void;
|
|
170
|
+
};
|
package/src/office/index.ts
CHANGED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import React, { useEffect, useRef } from "react";
|
|
2
|
+
|
|
3
|
+
import { mountOfficeBuildingCanvas } from "../building-canvas";
|
|
4
|
+
import type {
|
|
5
|
+
AgentGameOfficeBuildingCanvasController,
|
|
6
|
+
AgentGameOfficeBuildingCanvasMountOptions,
|
|
7
|
+
} from "../core/types";
|
|
8
|
+
|
|
9
|
+
export type OfficeBuildingCanvasProps = AgentGameOfficeBuildingCanvasMountOptions & {
|
|
10
|
+
className?: string;
|
|
11
|
+
style?: React.CSSProperties;
|
|
12
|
+
onReady?: (controller: AgentGameOfficeBuildingCanvasController) => void;
|
|
13
|
+
onDestroy?: () => void;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export function OfficeBuildingCanvas({
|
|
17
|
+
className,
|
|
18
|
+
onDestroy,
|
|
19
|
+
onReady,
|
|
20
|
+
style,
|
|
21
|
+
...options
|
|
22
|
+
}: OfficeBuildingCanvasProps) {
|
|
23
|
+
const containerRef = useRef<HTMLDivElement | null>(null);
|
|
24
|
+
const viewRef = useRef<AgentGameOfficeBuildingCanvasController | null>(null);
|
|
25
|
+
const onDestroyRef = useRef(onDestroy);
|
|
26
|
+
const onReadyRef = useRef(onReady);
|
|
27
|
+
const officeConfigKey = createOfficeConfigKey(options.office);
|
|
28
|
+
|
|
29
|
+
useEffect(() => {
|
|
30
|
+
onReadyRef.current = onReady;
|
|
31
|
+
onDestroyRef.current = onDestroy;
|
|
32
|
+
}, [onDestroy, onReady]);
|
|
33
|
+
|
|
34
|
+
useEffect(() => {
|
|
35
|
+
const container = containerRef.current;
|
|
36
|
+
if (!container) {
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const view = mountOfficeBuildingCanvas(container, options);
|
|
41
|
+
viewRef.current = view;
|
|
42
|
+
onReadyRef.current?.(view);
|
|
43
|
+
|
|
44
|
+
return () => {
|
|
45
|
+
viewRef.current?.destroy();
|
|
46
|
+
viewRef.current = null;
|
|
47
|
+
onDestroyRef.current?.();
|
|
48
|
+
};
|
|
49
|
+
}, [officeConfigKey]);
|
|
50
|
+
|
|
51
|
+
useEffect(() => {
|
|
52
|
+
viewRef.current?.focusFloor(options.focusedFloorId ?? null);
|
|
53
|
+
}, [options.focusedFloorId]);
|
|
54
|
+
|
|
55
|
+
return React.createElement("div", {
|
|
56
|
+
ref: containerRef,
|
|
57
|
+
className,
|
|
58
|
+
style: {
|
|
59
|
+
width: "100%",
|
|
60
|
+
height: "100%",
|
|
61
|
+
minHeight: 240,
|
|
62
|
+
...style,
|
|
63
|
+
},
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function createOfficeConfigKey(office: AgentGameOfficeBuildingCanvasMountOptions["office"]): string {
|
|
68
|
+
if (!office) {
|
|
69
|
+
return "default";
|
|
70
|
+
}
|
|
71
|
+
return JSON.stringify({
|
|
72
|
+
floors: office.building.floors.map((floor) => ({
|
|
73
|
+
id: floor.id,
|
|
74
|
+
name: floor.name,
|
|
75
|
+
rooms: floor.rooms.map((room) => ({
|
|
76
|
+
type: room.type,
|
|
77
|
+
capacity: room.capacity ?? null,
|
|
78
|
+
})),
|
|
79
|
+
})),
|
|
80
|
+
connectors: office.building.connectors.map((connector) => ({
|
|
81
|
+
id: connector.id,
|
|
82
|
+
name: connector.name,
|
|
83
|
+
type: connector.type,
|
|
84
|
+
serves: connector.serves,
|
|
85
|
+
})),
|
|
86
|
+
});
|
|
87
|
+
}
|
|
@@ -115,6 +115,7 @@ export function applyAgentPose(
|
|
|
115
115
|
function resetPose(mesh: AgentMeshParts): void {
|
|
116
116
|
mesh.visualRoot.position.y = 0;
|
|
117
117
|
mesh.body.rotation.set(0, 0, 0);
|
|
118
|
+
mesh.headRoot.rotation.set(0, 0, 0);
|
|
118
119
|
mesh.head.rotation.set(0, 0, 0);
|
|
119
120
|
mesh.leftArm.rotation.set(0, 0, 0);
|
|
120
121
|
mesh.rightArm.rotation.set(0, 0, 0);
|
|
@@ -150,7 +151,7 @@ function applyPoseMode(
|
|
|
150
151
|
applySeatedLowerBody(mesh);
|
|
151
152
|
mesh.visualRoot.position.y = CHAIR_SEATED_VISUAL_Y;
|
|
152
153
|
mesh.body.rotation.x = -0.1;
|
|
153
|
-
mesh.
|
|
154
|
+
mesh.headRoot.rotation.x = -0.06;
|
|
154
155
|
mesh.leftArm.rotation.x = 0.58 + Math.sin(phase) * 0.18;
|
|
155
156
|
mesh.rightArm.rotation.x = 0.58 - Math.sin(phase) * 0.18;
|
|
156
157
|
mesh.leftArm.rotation.z = -0.08;
|
|
@@ -158,21 +159,21 @@ function applyPoseMode(
|
|
|
158
159
|
return;
|
|
159
160
|
}
|
|
160
161
|
case "thinking":
|
|
161
|
-
mesh.
|
|
162
|
-
mesh.
|
|
162
|
+
mesh.headRoot.rotation.y = Math.sin(elapsedSeconds * 1.2) * 0.35;
|
|
163
|
+
mesh.headRoot.rotation.x = Math.sin(elapsedSeconds * 0.9) * 0.08;
|
|
163
164
|
mesh.body.rotation.z = Math.sin(elapsedSeconds * 1.6) * 0.07;
|
|
164
165
|
return;
|
|
165
166
|
case "meeting":
|
|
166
167
|
applySeatedLowerBody(mesh);
|
|
167
168
|
mesh.visualRoot.position.y = CHAIR_SEATED_VISUAL_Y;
|
|
168
|
-
mesh.
|
|
169
|
+
mesh.headRoot.rotation.x = Math.sin(elapsedSeconds * 2) * 0.08;
|
|
169
170
|
mesh.leftArm.rotation.x = 0.08;
|
|
170
171
|
mesh.rightArm.rotation.x = -0.08;
|
|
171
172
|
return;
|
|
172
173
|
case "waiting":
|
|
173
174
|
applySeatedLowerBody(mesh);
|
|
174
175
|
mesh.visualRoot.position.y = CHAIR_SEATED_VISUAL_Y;
|
|
175
|
-
mesh.
|
|
176
|
+
mesh.headRoot.rotation.y = Math.sin(elapsedSeconds * 0.9) * 0.12;
|
|
176
177
|
return;
|
|
177
178
|
case "resting": {
|
|
178
179
|
const breath = 1 + Math.sin(elapsedSeconds * 1.2) * 0.025;
|
|
@@ -180,7 +181,7 @@ function applyPoseMode(
|
|
|
180
181
|
mesh.visualRoot.position.y = SOFA_SEATED_VISUAL_Y;
|
|
181
182
|
mesh.body.scale.set(1, breath, 1);
|
|
182
183
|
mesh.body.rotation.x = 0.08;
|
|
183
|
-
mesh.
|
|
184
|
+
mesh.headRoot.rotation.z = Math.sin(elapsedSeconds * 0.8) * 0.05;
|
|
184
185
|
return;
|
|
185
186
|
}
|
|
186
187
|
case "idle": {
|
|
@@ -10,6 +10,7 @@ export type AgentMeshParts = {
|
|
|
10
10
|
group: THREE.Group;
|
|
11
11
|
visualRoot: THREE.Group;
|
|
12
12
|
body: THREE.Object3D;
|
|
13
|
+
headRoot: THREE.Object3D;
|
|
13
14
|
head: THREE.Object3D;
|
|
14
15
|
leftEye: THREE.Object3D;
|
|
15
16
|
rightEye: THREE.Object3D;
|
|
@@ -61,6 +62,15 @@ export type AgentBodyPartRenderSpec = {
|
|
|
61
62
|
z: number;
|
|
62
63
|
};
|
|
63
64
|
|
|
65
|
+
const HEAD_ROOT_PART_KEYS = new Set<AgentBodyPartKey>([
|
|
66
|
+
"head",
|
|
67
|
+
"leftEye",
|
|
68
|
+
"rightEye",
|
|
69
|
+
"mouth",
|
|
70
|
+
"hairTop",
|
|
71
|
+
"hairBack",
|
|
72
|
+
]);
|
|
73
|
+
|
|
64
74
|
export function resolveAgentBodyPartRenderSpecs(index: number): AgentBodyPartRenderSpec[] {
|
|
65
75
|
const shirt = agentColors[index % agentColors.length] ?? agentColors[0];
|
|
66
76
|
const skin = skinColors[index % skinColors.length] ?? skinColors[0];
|
|
@@ -90,11 +100,25 @@ export function createAgentMesh(agent: AgentGameOfficeAgent, index: number): Age
|
|
|
90
100
|
group.scale.setScalar(AGENT_MESH_SCALE);
|
|
91
101
|
const visualRoot = new THREE.Group();
|
|
92
102
|
group.add(visualRoot);
|
|
103
|
+
const specs = resolveAgentBodyPartRenderSpecs(index);
|
|
104
|
+
const headSpec = specs.find((part) => part.key === "head");
|
|
105
|
+
if (!headSpec) {
|
|
106
|
+
throw new Error("Agent mesh head part is required");
|
|
107
|
+
}
|
|
108
|
+
const headRoot = new THREE.Group();
|
|
109
|
+
headRoot.position.set(headSpec.x, headSpec.y, headSpec.z);
|
|
110
|
+
visualRoot.add(headRoot);
|
|
93
111
|
const parts = Object.fromEntries(
|
|
94
|
-
|
|
95
|
-
part.key
|
|
96
|
-
|
|
97
|
-
|
|
112
|
+
specs.map((part) => {
|
|
113
|
+
const parent = HEAD_ROOT_PART_KEYS.has(part.key) ? headRoot : visualRoot;
|
|
114
|
+
const x = HEAD_ROOT_PART_KEYS.has(part.key) ? part.x - headSpec.x : part.x;
|
|
115
|
+
const y = HEAD_ROOT_PART_KEYS.has(part.key) ? part.y - headSpec.y : part.y;
|
|
116
|
+
const z = HEAD_ROOT_PART_KEYS.has(part.key) ? part.z - headSpec.z : part.z;
|
|
117
|
+
return [
|
|
118
|
+
part.key,
|
|
119
|
+
addPart(parent, x, y, z),
|
|
120
|
+
];
|
|
121
|
+
}),
|
|
98
122
|
) as Record<AgentBodyPartKey, THREE.Object3D>;
|
|
99
123
|
const activityGlow = createActivityGlow();
|
|
100
124
|
const activityEffects = createAgentActivityEffects();
|
|
@@ -106,6 +130,7 @@ export function createAgentMesh(agent: AgentGameOfficeAgent, index: number): Age
|
|
|
106
130
|
group,
|
|
107
131
|
visualRoot,
|
|
108
132
|
body: parts.body,
|
|
133
|
+
headRoot,
|
|
109
134
|
head: parts.head,
|
|
110
135
|
leftEye: parts.leftEye,
|
|
111
136
|
rightEye: parts.rightEye,
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import * as THREE from "three";
|
|
2
|
+
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls.js";
|
|
3
|
+
|
|
4
|
+
import type {
|
|
5
|
+
AgentGameOfficeBuildingCanvasController,
|
|
6
|
+
AgentGameOfficeBuildingCanvasResolvedOptions,
|
|
7
|
+
} from "../../core/types";
|
|
8
|
+
import type { ResolvedOfficeLayout } from "../../layout";
|
|
9
|
+
import {
|
|
10
|
+
addLights,
|
|
11
|
+
applyOfficeFloorFocus,
|
|
12
|
+
buildOfficeScene,
|
|
13
|
+
disposeObject,
|
|
14
|
+
} from "./scene";
|
|
15
|
+
import {
|
|
16
|
+
THREE_RENDERER_MAX_CAMERA_DISTANCE,
|
|
17
|
+
THREE_RENDERER_MIN_CAMERA_DISTANCE,
|
|
18
|
+
THREE_RENDERER_PIXEL_RATIO_LIMIT,
|
|
19
|
+
THREE_RENDERER_TARGET_FPS,
|
|
20
|
+
createInitialOfficeCameraView,
|
|
21
|
+
createOfficeCameraState,
|
|
22
|
+
createOfficeMouseButtons,
|
|
23
|
+
type OfficeCameraView,
|
|
24
|
+
} from "./mount";
|
|
25
|
+
|
|
26
|
+
export function mountThreeOfficeBuildingCanvas(
|
|
27
|
+
container: HTMLElement,
|
|
28
|
+
options: AgentGameOfficeBuildingCanvasResolvedOptions,
|
|
29
|
+
): AgentGameOfficeBuildingCanvasController {
|
|
30
|
+
const width = Math.max(container.clientWidth || 960, 320);
|
|
31
|
+
const height = Math.max(container.clientHeight || 540, 240);
|
|
32
|
+
const scene = new THREE.Scene();
|
|
33
|
+
scene.background = new THREE.Color(0xf7f8fb);
|
|
34
|
+
|
|
35
|
+
const camera = new THREE.PerspectiveCamera(45, width / height, 0.1, 1000);
|
|
36
|
+
const initialCameraView = createInitialOfficeCameraView(options.officeLayout, camera.aspect, camera.fov);
|
|
37
|
+
applyOfficeCameraView(camera, initialCameraView);
|
|
38
|
+
|
|
39
|
+
const renderer = new THREE.WebGLRenderer({
|
|
40
|
+
antialias: true,
|
|
41
|
+
alpha: false,
|
|
42
|
+
powerPreference: "high-performance",
|
|
43
|
+
});
|
|
44
|
+
renderer.setSize(width, height);
|
|
45
|
+
renderer.setPixelRatio(Math.min(globalThis.devicePixelRatio || 1, THREE_RENDERER_PIXEL_RATIO_LIMIT));
|
|
46
|
+
renderer.shadowMap.enabled = false;
|
|
47
|
+
renderer.domElement.style.width = "100%";
|
|
48
|
+
renderer.domElement.style.height = "100%";
|
|
49
|
+
renderer.domElement.style.display = "block";
|
|
50
|
+
renderer.domElement.style.cursor = "grab";
|
|
51
|
+
|
|
52
|
+
function setGrabbingCursor() {
|
|
53
|
+
renderer.domElement.style.cursor = "grabbing";
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function setGrabCursor() {
|
|
57
|
+
renderer.domElement.style.cursor = "grab";
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
renderer.domElement.addEventListener("pointerdown", setGrabbingCursor);
|
|
61
|
+
renderer.domElement.addEventListener("pointerup", setGrabCursor);
|
|
62
|
+
renderer.domElement.addEventListener("pointerleave", setGrabCursor);
|
|
63
|
+
|
|
64
|
+
const controls = new OrbitControls(camera, renderer.domElement);
|
|
65
|
+
controls.target.copy(initialCameraView.target);
|
|
66
|
+
controls.enableDamping = true;
|
|
67
|
+
controls.dampingFactor = 0.08;
|
|
68
|
+
controls.enablePan = true;
|
|
69
|
+
controls.enableRotate = true;
|
|
70
|
+
controls.enableZoom = true;
|
|
71
|
+
controls.mouseButtons = createOfficeMouseButtons();
|
|
72
|
+
controls.minDistance = THREE_RENDERER_MIN_CAMERA_DISTANCE;
|
|
73
|
+
controls.maxDistance = THREE_RENDERER_MAX_CAMERA_DISTANCE;
|
|
74
|
+
controls.maxPolarAngle = Math.PI * 0.48;
|
|
75
|
+
controls.addEventListener("change", emitCameraChange);
|
|
76
|
+
controls.update();
|
|
77
|
+
|
|
78
|
+
const root = document.createElement("div");
|
|
79
|
+
root.style.position = "relative";
|
|
80
|
+
root.style.width = "100%";
|
|
81
|
+
root.style.height = "100%";
|
|
82
|
+
root.style.overflow = "hidden";
|
|
83
|
+
root.appendChild(renderer.domElement);
|
|
84
|
+
container.appendChild(root);
|
|
85
|
+
|
|
86
|
+
addLights(scene);
|
|
87
|
+
buildOfficeScene(scene, options.officeLayout);
|
|
88
|
+
|
|
89
|
+
let frameId = 0;
|
|
90
|
+
let disposed = false;
|
|
91
|
+
let previousRenderTime = performance.now();
|
|
92
|
+
let focusedFloorId = normalizeFocusedFloorId(options.officeLayout, options.focusedFloorId ?? null);
|
|
93
|
+
setFocusedFloor(focusedFloorId, false);
|
|
94
|
+
|
|
95
|
+
function animate() {
|
|
96
|
+
if (disposed) {
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
const now = performance.now();
|
|
100
|
+
const targetFrameIntervalMs = 1000 / THREE_RENDERER_TARGET_FPS;
|
|
101
|
+
if (now - previousRenderTime >= targetFrameIntervalMs) {
|
|
102
|
+
previousRenderTime = now;
|
|
103
|
+
controls.update();
|
|
104
|
+
renderer.render(scene, camera);
|
|
105
|
+
}
|
|
106
|
+
frameId = requestAnimationFrame(animate);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const resizeObserver = new ResizeObserver(() => {
|
|
110
|
+
const nextWidth = Math.max(container.clientWidth || width, 320);
|
|
111
|
+
const nextHeight = Math.max(container.clientHeight || height, 240);
|
|
112
|
+
camera.aspect = nextWidth / nextHeight;
|
|
113
|
+
camera.updateProjectionMatrix();
|
|
114
|
+
renderer.setSize(nextWidth, nextHeight);
|
|
115
|
+
emitCameraChange();
|
|
116
|
+
});
|
|
117
|
+
resizeObserver.observe(container);
|
|
118
|
+
|
|
119
|
+
emitCameraChange();
|
|
120
|
+
animate();
|
|
121
|
+
|
|
122
|
+
return {
|
|
123
|
+
resetCamera() {
|
|
124
|
+
camera.zoom = 1;
|
|
125
|
+
camera.updateProjectionMatrix();
|
|
126
|
+
const view = createInitialOfficeCameraView(options.officeLayout, camera.aspect, camera.fov);
|
|
127
|
+
applyOfficeCameraView(camera, view);
|
|
128
|
+
controls.target.copy(view.target);
|
|
129
|
+
controls.update();
|
|
130
|
+
emitCameraChange();
|
|
131
|
+
},
|
|
132
|
+
focusFloor(floorId) {
|
|
133
|
+
setFocusedFloor(floorId);
|
|
134
|
+
},
|
|
135
|
+
getFocusedFloor() {
|
|
136
|
+
return focusedFloorId;
|
|
137
|
+
},
|
|
138
|
+
destroy() {
|
|
139
|
+
disposed = true;
|
|
140
|
+
cancelAnimationFrame(frameId);
|
|
141
|
+
resizeObserver.disconnect();
|
|
142
|
+
controls.dispose();
|
|
143
|
+
renderer.domElement.removeEventListener("pointerdown", setGrabbingCursor);
|
|
144
|
+
renderer.domElement.removeEventListener("pointerup", setGrabCursor);
|
|
145
|
+
renderer.domElement.removeEventListener("pointerleave", setGrabCursor);
|
|
146
|
+
disposeObject(scene);
|
|
147
|
+
renderer.dispose();
|
|
148
|
+
root.remove();
|
|
149
|
+
},
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
function emitCameraChange() {
|
|
153
|
+
options.onCameraChange?.(createOfficeCameraState(camera, controls));
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function setFocusedFloor(floorId: string | null, emit = true) {
|
|
157
|
+
const nextFloorId = normalizeFocusedFloorId(options.officeLayout, floorId);
|
|
158
|
+
focusedFloorId = nextFloorId;
|
|
159
|
+
applyOfficeFloorFocus(scene, options.officeLayout, focusedFloorId);
|
|
160
|
+
if (emit) {
|
|
161
|
+
options.onFocusedFloorChange?.(focusedFloorId);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
function applyOfficeCameraView(camera: THREE.PerspectiveCamera, view: OfficeCameraView) {
|
|
167
|
+
camera.up.copy(view.up);
|
|
168
|
+
camera.position.copy(view.position);
|
|
169
|
+
camera.rotation.copy(view.rotation);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
function normalizeFocusedFloorId(layout: ResolvedOfficeLayout, floorId: string | null): string | null {
|
|
173
|
+
if (!floorId) {
|
|
174
|
+
return null;
|
|
175
|
+
}
|
|
176
|
+
return layout.building.floors.some((floor) => floor.id === floorId) ? floorId : null;
|
|
177
|
+
}
|