agentcut 0.2.3 → 0.2.5

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.
Files changed (35) hide show
  1. package/dist/studio/tools.js +17 -2
  2. package/dist/studio/tools.js.map +1 -1
  3. package/dist/studio/workspace.js +4 -0
  4. package/dist/studio/workspace.js.map +1 -1
  5. package/package.json +1 -1
  6. package/template/components/CATALOG.md +52 -0
  7. package/template/components/README.md +70 -61
  8. package/template/components/STYLES.md +2 -0
  9. package/template/components/catalog.json +477 -0
  10. package/template/components/catalog.ts +4 -0
  11. package/template/components/cinematic/Cinematic.tsx +1139 -0
  12. package/template/components/cinematic/Device3D.tsx +299 -0
  13. package/template/components/cinematic/devices3d/display-model.ts +184 -0
  14. package/template/components/cinematic/devices3d/iphone-model.ts +643 -0
  15. package/template/components/cinematic/devices3d/macbook-model.ts +553 -0
  16. package/template/components/cinematic/devices3d/models.ts +612 -0
  17. package/template/components/cinematic/devices3d/runtime.ts +258 -0
  18. package/template/components/cinematic/math.ts +100 -0
  19. package/template/components/dot-trace-title/DotTraceTitle.tsx +181 -0
  20. package/template/components/dot-trace-title/meta.json +51 -0
  21. package/template/components/generate/GenerateReveal.tsx +381 -0
  22. package/template/components/index.ts +36 -0
  23. package/template/components/lib.ts +18 -6
  24. package/template/components/loops/LoopCanvas.tsx +132 -0
  25. package/template/components/loops/engine.ts +184 -0
  26. package/template/components/loops/library.ts +667 -0
  27. package/template/components/loops/library2.ts +777 -0
  28. package/template/components/motion-loop/MotionLoop.tsx +37 -0
  29. package/template/components/motion-loop/meta.json +312 -0
  30. package/template/runtime/composition.tsx +927 -920
  31. package/template/skill/SKILL.md +20 -3
  32. package/template/skill/references/browser-export.md +61 -61
  33. package/template/skill/references/cinematic.md +171 -0
  34. package/template/skill/references/loops.md +151 -0
  35. package/template/skill/references/performance.md +81 -0
@@ -0,0 +1,612 @@
1
+ import * as THREE from "three";
2
+ import { GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader.js";
3
+ import { createIphoneVideoMockupModel } from "./iphone-model";
4
+ import {
5
+ createMacbookModel,
6
+ MACBOOK_SCREEN_ASPECT,
7
+ } from "./macbook-model";
8
+ import {
9
+ createDisplayModel,
10
+ DISPLAY_SCREEN_ASPECT,
11
+ } from "./display-model";
12
+ export type DeviceCategory = "phone" | "laptop" | "desktop";
13
+
14
+ export type VideoDeviceModelId =
15
+ | "procedural-iphone"
16
+ | "iphone-16-pro-max"
17
+ | "procedural-macbook"
18
+ | "procedural-display";
19
+
20
+ export interface VideoDeviceModelConfig {
21
+ id: VideoDeviceModelId;
22
+ name: string;
23
+ category: DeviceCategory;
24
+ source?: string;
25
+ screenAspect: number;
26
+ screenFit: "contain" | "cover";
27
+ screenMeshName?: string;
28
+ screenMaterialName?: string;
29
+ screenUvMirrorX: boolean;
30
+ shadowMode: "silhouette" | "base";
31
+ shadowSilhouetteMeshNames?: string[];
32
+ mappingVersion: number;
33
+ textureFlipY: boolean;
34
+ textureMirrorX: boolean;
35
+ targetSize: number;
36
+ rotation: [number, number, number];
37
+ cameraZ: number;
38
+ cameraY: number;
39
+ credit: string;
40
+ }
41
+
42
+ /**
43
+ * Affine map from the screen mesh's UVs to its local positions:
44
+ * position = origin + du * uv.x + dv * uv.y.
45
+ */
46
+ export interface VideoDeviceScreenFrame {
47
+ origin: THREE.Vector3;
48
+ du: THREE.Vector3;
49
+ dv: THREE.Vector3;
50
+ }
51
+
52
+ export interface LoadedVideoDeviceModel {
53
+ root: THREE.Group;
54
+ screen: THREE.Mesh;
55
+ screenFrame: VideoDeviceScreenFrame;
56
+ config: VideoDeviceModelConfig;
57
+ ownsGeometries: boolean;
58
+ }
59
+
60
+ /** Least-squares fit of the (planar) screen's UV → position mapping. */
61
+ export function computeScreenFrame(screen: THREE.Mesh): VideoDeviceScreenFrame {
62
+ const position = screen.geometry.getAttribute("position");
63
+ const uv = screen.geometry.getAttribute("uv");
64
+ const fallback = () => {
65
+ screen.geometry.computeBoundingBox();
66
+ const box = screen.geometry.boundingBox ?? new THREE.Box3();
67
+ return {
68
+ origin: new THREE.Vector3(box.min.x, box.min.y, box.max.z),
69
+ du: new THREE.Vector3(box.max.x - box.min.x, 0, 0),
70
+ dv: new THREE.Vector3(0, box.max.y - box.min.y, 0),
71
+ };
72
+ };
73
+ if (!position || !uv || uv.count < 3) return fallback();
74
+ // Normal equations for [u v 1] · [du dv o]ᵀ = p
75
+ let suu = 0, suv = 0, su = 0, svv = 0, sv = 0, n = 0;
76
+ const pu = new THREE.Vector3();
77
+ const pv = new THREE.Vector3();
78
+ const p1 = new THREE.Vector3();
79
+ const point = new THREE.Vector3();
80
+ const step = Math.max(1, Math.floor(uv.count / 400));
81
+ for (let index = 0; index < uv.count; index += step) {
82
+ const u = uv.getX(index);
83
+ const v = uv.getY(index);
84
+ point.fromBufferAttribute(position as THREE.BufferAttribute, index);
85
+ suu += u * u;
86
+ suv += u * v;
87
+ su += u;
88
+ svv += v * v;
89
+ sv += v;
90
+ n += 1;
91
+ pu.addScaledVector(point, u);
92
+ pv.addScaledVector(point, v);
93
+ p1.add(point);
94
+ }
95
+ const matrix = new THREE.Matrix3().set(suu, suv, su, suv, svv, sv, su, sv, n);
96
+ if (Math.abs(matrix.determinant()) < 1e-12) return fallback();
97
+ const inverse = matrix.clone().invert();
98
+ const e = inverse.elements; // column-major
99
+ const solve = (row: number) =>
100
+ new THREE.Vector3()
101
+ .addScaledVector(pu, e[row])
102
+ .addScaledVector(pv, e[row + 3])
103
+ .addScaledVector(p1, e[row + 6]);
104
+ return { du: solve(0), dv: solve(1), origin: solve(2) };
105
+ }
106
+
107
+ export interface DeviceSilhouettePoint {
108
+ x: number;
109
+ y: number;
110
+ }
111
+
112
+ function collectVideoDeviceMeshPoints(
113
+ model: LoadedVideoDeviceModel,
114
+ meshNames?: ReadonlySet<string>,
115
+ ) {
116
+ model.root.updateMatrixWorld(true);
117
+ const inverseRoot = model.root.matrixWorld.clone().invert();
118
+ const points: THREE.Vector3[] = [];
119
+ model.root.traverse((object) => {
120
+ if (!(object instanceof THREE.Mesh)) return;
121
+ if (meshNames && !meshNames.has(object.name)) return;
122
+ const position = object.geometry.getAttribute("position");
123
+ if (!(position instanceof THREE.BufferAttribute)) return;
124
+ object.updateWorldMatrix(true, false);
125
+ const step = Math.max(1, Math.ceil(position.count / 96));
126
+ for (let index = 0; index < position.count; index += step) {
127
+ points.push(
128
+ new THREE.Vector3()
129
+ .fromBufferAttribute(position, index)
130
+ .applyMatrix4(object.matrixWorld)
131
+ .applyMatrix4(inverseRoot),
132
+ );
133
+ }
134
+ });
135
+ return points;
136
+ }
137
+
138
+ export function collectVideoDeviceSilhouettePoints(
139
+ model: LoadedVideoDeviceModel,
140
+ ) {
141
+ return collectVideoDeviceMeshPoints(model);
142
+ }
143
+
144
+ export function collectVideoDeviceShadowSilhouettePoints(
145
+ model: LoadedVideoDeviceModel,
146
+ ) {
147
+ const meshNames = model.config.shadowSilhouetteMeshNames;
148
+ if (!meshNames?.length) return collectVideoDeviceMeshPoints(model);
149
+
150
+ const points = collectVideoDeviceMeshPoints(model, new Set(meshNames));
151
+ return points.length >= 3 ? points : collectVideoDeviceMeshPoints(model);
152
+ }
153
+
154
+ function cross(
155
+ origin: DeviceSilhouettePoint,
156
+ a: DeviceSilhouettePoint,
157
+ b: DeviceSilhouettePoint,
158
+ ) {
159
+ return (
160
+ (a.x - origin.x) * (b.y - origin.y) - (a.y - origin.y) * (b.x - origin.x)
161
+ );
162
+ }
163
+
164
+ function getConvexHull(points: DeviceSilhouettePoint[]) {
165
+ if (points.length <= 3) return points;
166
+ const sorted = [...points].sort((a, b) => a.x - b.x || a.y - b.y);
167
+ const lower: DeviceSilhouettePoint[] = [];
168
+ for (const point of sorted) {
169
+ while (
170
+ lower.length >= 2 &&
171
+ cross(lower[lower.length - 2], lower[lower.length - 1], point) <= 0
172
+ ) {
173
+ lower.pop();
174
+ }
175
+ lower.push(point);
176
+ }
177
+ const upper: DeviceSilhouettePoint[] = [];
178
+ for (let index = sorted.length - 1; index >= 0; index -= 1) {
179
+ const point = sorted[index];
180
+ while (
181
+ upper.length >= 2 &&
182
+ cross(upper[upper.length - 2], upper[upper.length - 1], point) <= 0
183
+ ) {
184
+ upper.pop();
185
+ }
186
+ upper.push(point);
187
+ }
188
+ lower.pop();
189
+ upper.pop();
190
+ return lower.concat(upper);
191
+ }
192
+
193
+ export function projectVideoDeviceSilhouette(
194
+ localPoints: THREE.Vector3[],
195
+ root: THREE.Object3D,
196
+ camera: THREE.Camera,
197
+ width: number,
198
+ height: number,
199
+ shadowMode: "silhouette" | "base" = "silhouette",
200
+ ) {
201
+ root.updateMatrixWorld(true);
202
+ camera.updateMatrixWorld(true);
203
+ const projected = localPoints
204
+ .map((localPoint) => {
205
+ const point = localPoint
206
+ .clone()
207
+ .applyMatrix4(root.matrixWorld)
208
+ .project(camera);
209
+ return {
210
+ x: (point.x * 0.5 + 0.5) * width,
211
+ y: (-point.y * 0.5 + 0.5) * height,
212
+ z: point.z,
213
+ };
214
+ })
215
+ .filter(
216
+ (point) =>
217
+ Number.isFinite(point.x) &&
218
+ Number.isFinite(point.y) &&
219
+ point.z >= -1 &&
220
+ point.z <= 1,
221
+ )
222
+ .map(({ x, y }) => ({ x, y }));
223
+ const hull = getConvexHull(projected);
224
+ if (shadowMode !== "base" || hull.length < 3) return hull;
225
+ const minX = Math.min(...hull.map((point) => point.x));
226
+ const maxX = Math.max(...hull.map((point) => point.x));
227
+ const minY = Math.min(...hull.map((point) => point.y));
228
+ const maxY = Math.max(...hull.map((point) => point.y));
229
+ const spanWidth = maxX - minX;
230
+ const contactHeight = Math.max(4, (maxY - minY) * 0.035);
231
+ return [
232
+ { x: minX + spanWidth * 0.08, y: maxY - contactHeight },
233
+ { x: maxX - spanWidth * 0.08, y: maxY - contactHeight },
234
+ { x: maxX - spanWidth * 0.02, y: maxY },
235
+ { x: minX + spanWidth * 0.02, y: maxY },
236
+ ];
237
+ }
238
+
239
+ /**
240
+ * Prevent a requested camera distance from pushing any part of a rotated
241
+ * device outside the render viewport. The returned distance preserves the
242
+ * user's zoom whenever it fits and only backs the camera off enough to keep a
243
+ * small safety margin around the hardware.
244
+ */
245
+ export function getVideoDeviceCameraZToFit(
246
+ localPoints: THREE.Vector3[],
247
+ root: THREE.Object3D,
248
+ camera: THREE.PerspectiveCamera,
249
+ requestedCameraZ: number,
250
+ visibleFraction = 0.94,
251
+ ) {
252
+ if (localPoints.length === 0 || camera.aspect <= 0) return requestedCameraZ;
253
+
254
+ root.updateMatrixWorld(true);
255
+ const verticalTangent = Math.tan(THREE.MathUtils.degToRad(camera.fov / 2));
256
+ const horizontalTangent = verticalTangent * camera.aspect;
257
+ const safeVerticalTangent = verticalTangent * visibleFraction;
258
+ const safeHorizontalTangent = horizontalTangent * visibleFraction;
259
+ let minimumCameraZ = requestedCameraZ;
260
+
261
+ for (const localPoint of localPoints) {
262
+ const point = localPoint.clone().applyMatrix4(root.matrixWorld);
263
+ minimumCameraZ = Math.max(
264
+ minimumCameraZ,
265
+ point.z + Math.abs(point.x - camera.position.x) / safeHorizontalTangent,
266
+ point.z + Math.abs(point.y - camera.position.y) / safeVerticalTangent,
267
+ point.z + camera.near * 2,
268
+ );
269
+ }
270
+
271
+ return minimumCameraZ;
272
+ }
273
+
274
+ export const VIDEO_DEVICE_MODEL_CONFIGS: Record<
275
+ VideoDeviceModelId,
276
+ VideoDeviceModelConfig
277
+ > = {
278
+ "procedural-iphone": {
279
+ id: "procedural-iphone",
280
+ name: "iPhone",
281
+ category: "phone",
282
+ screenAspect: 0.655 / 1.395,
283
+ screenFit: "cover",
284
+ screenUvMirrorX: false,
285
+ shadowMode: "silhouette",
286
+ shadowSilhouetteMeshNames: ["phone-body"],
287
+ mappingVersion: 12,
288
+ textureFlipY: true,
289
+ textureMirrorX: false,
290
+ targetSize: 1.47,
291
+ rotation: [0, 0, 0],
292
+ cameraZ: 3.05,
293
+ cameraY: 0.02,
294
+ credit: "Original ScreenSlick procedural model",
295
+ },
296
+ "iphone-16-pro-max": {
297
+ id: "iphone-16-pro-max",
298
+ name: "iPhone 16 Pro Max",
299
+ category: "phone",
300
+ source: "/models/iphone-16-pro-max.glb",
301
+ screenAspect: 1320 / 2868,
302
+ screenFit: "cover",
303
+ screenMeshName: "Cube.014_screen.001_0",
304
+ screenMaterialName: "screen.001",
305
+ screenUvMirrorX: true,
306
+ shadowMode: "silhouette",
307
+ mappingVersion: 3,
308
+ textureFlipY: true,
309
+ textureMirrorX: false,
310
+ targetSize: 1.52,
311
+ rotation: [0, Math.PI / 2, 0],
312
+ cameraZ: 3.15,
313
+ cameraY: 0.02,
314
+ credit: "MajdyModels · CC BY 4.0",
315
+ },
316
+ "procedural-macbook": {
317
+ id: "procedural-macbook",
318
+ name: "MacBook",
319
+ category: "laptop",
320
+ screenAspect: MACBOOK_SCREEN_ASPECT,
321
+ screenFit: "contain",
322
+ screenUvMirrorX: false,
323
+ shadowMode: "silhouette",
324
+ shadowSilhouetteMeshNames: ["laptop-base"],
325
+ mappingVersion: 1,
326
+ textureFlipY: true,
327
+ textureMirrorX: false,
328
+ targetSize: 2,
329
+ rotation: [0, 0, 0],
330
+ cameraZ: 4.2,
331
+ cameraY: 0.05,
332
+ credit: "Original ScreenSlick procedural model",
333
+ },
334
+ "procedural-display": {
335
+ id: "procedural-display",
336
+ name: "Studio Display",
337
+ category: "desktop",
338
+ screenAspect: DISPLAY_SCREEN_ASPECT,
339
+ screenFit: "contain",
340
+ screenUvMirrorX: false,
341
+ shadowMode: "silhouette",
342
+ shadowSilhouetteMeshNames: ["display-foot"],
343
+ mappingVersion: 1,
344
+ textureFlipY: true,
345
+ textureMirrorX: false,
346
+ targetSize: 4,
347
+ rotation: [0, 0, 0],
348
+ cameraZ: 6,
349
+ cameraY: 0.05,
350
+ credit: "Original ScreenSlick procedural model",
351
+ },
352
+ };
353
+
354
+ const MODEL_FALLBACK_ORIGIN = "https://agentcut.synteq.app";
355
+
356
+ const templatePromises = new Map<VideoDeviceModelId, Promise<THREE.Group>>();
357
+ const templates = new Map<VideoDeviceModelId, THREE.Group>();
358
+
359
+ function cloneModelTemplate(template: THREE.Group) {
360
+ const clone = template.clone(true);
361
+ clone.traverse((object) => {
362
+ if (!(object instanceof THREE.Mesh)) return;
363
+ object.castShadow = true;
364
+ object.receiveShadow = true;
365
+ object.material = Array.isArray(object.material)
366
+ ? object.material.map((material) => material.clone())
367
+ : object.material.clone();
368
+ });
369
+ return clone;
370
+ }
371
+
372
+ function normalizeModel(root: THREE.Group, config: VideoDeviceModelConfig) {
373
+ root.rotation.set(...config.rotation);
374
+ root.updateMatrixWorld(true);
375
+ const bounds = new THREE.Box3().setFromObject(root);
376
+ const size = bounds.getSize(new THREE.Vector3());
377
+ const maxDimension = Math.max(size.x, size.y, size.z, 0.0001);
378
+ root.scale.multiplyScalar(config.targetSize / maxDimension);
379
+ root.updateMatrixWorld(true);
380
+ const normalizedBounds = new THREE.Box3().setFromObject(root);
381
+ const center = normalizedBounds.getCenter(new THREE.Vector3());
382
+ root.position.sub(center);
383
+ }
384
+
385
+ function findScreenMesh(root: THREE.Group, config: VideoDeviceModelConfig) {
386
+ const named = config.screenMeshName
387
+ ? root.getObjectByName(config.screenMeshName)
388
+ : null;
389
+ if (named instanceof THREE.Mesh) return named;
390
+
391
+ const matches: THREE.Mesh[] = [];
392
+ root.traverse((object) => {
393
+ if (matches.length > 0 || !(object instanceof THREE.Mesh)) return;
394
+ const materials = Array.isArray(object.material)
395
+ ? object.material
396
+ : [object.material];
397
+ if (
398
+ materials.some((material) => material.name === config.screenMaterialName)
399
+ ) {
400
+ matches.push(object);
401
+ }
402
+ });
403
+ return matches[0] ?? null;
404
+ }
405
+
406
+ type PositionAxis = "x" | "y" | "z";
407
+
408
+ function projectScreenUvs(screen: THREE.Mesh, config: VideoDeviceModelConfig) {
409
+ const geometry = screen.geometry.clone();
410
+ geometry.computeBoundingBox();
411
+ const bounds = geometry.boundingBox;
412
+ const position = geometry.getAttribute("position");
413
+ const uv = geometry.getAttribute("uv");
414
+ if (
415
+ !bounds ||
416
+ !(position instanceof THREE.BufferAttribute) ||
417
+ !(uv instanceof THREE.BufferAttribute) ||
418
+ uv.count === 0
419
+ ) {
420
+ screen.geometry = geometry;
421
+ screen.userData.ownsVideoScreenGeometry = true;
422
+ return;
423
+ }
424
+
425
+ screen.updateWorldMatrix(true, false);
426
+ const worldBasis = new THREE.Matrix3().setFromMatrix4(screen.matrixWorld);
427
+ const axes = (["x", "y", "z"] as const)
428
+ .map((axis) => {
429
+ const localSpan = Math.max(0.0001, bounds.max[axis] - bounds.min[axis]);
430
+ const basis =
431
+ axis === "x"
432
+ ? new THREE.Vector3(1, 0, 0)
433
+ : axis === "y"
434
+ ? new THREE.Vector3(0, 1, 0)
435
+ : new THREE.Vector3(0, 0, 1);
436
+ const worldSpan = basis.applyMatrix3(worldBasis).length() * localSpan;
437
+ return { axis, localSpan, worldSpan };
438
+ })
439
+ .sort((a, b) => b.worldSpan - a.worldSpan)
440
+ .slice(0, 2);
441
+ const [longAxis, shortAxis] = axes;
442
+ const uAxis = config.screenAspect >= 1 ? longAxis : shortAxis;
443
+ const vAxis = config.screenAspect >= 1 ? shortAxis : longAxis;
444
+ const readPosition = (axis: PositionAxis, index: number) =>
445
+ axis === "x"
446
+ ? position.getX(index)
447
+ : axis === "y"
448
+ ? position.getY(index)
449
+ : position.getZ(index);
450
+
451
+ for (let index = 0; index < uv.count; index += 1) {
452
+ const projectedU =
453
+ (readPosition(uAxis.axis, index) - bounds.min[uAxis.axis]) /
454
+ uAxis.localSpan;
455
+ uv.setXY(
456
+ index,
457
+ config.screenUvMirrorX ? 1 - projectedU : projectedU,
458
+ (readPosition(vAxis.axis, index) - bounds.min[vAxis.axis]) /
459
+ vAxis.localSpan,
460
+ );
461
+ }
462
+ uv.needsUpdate = true;
463
+ screen.geometry = geometry;
464
+ screen.userData.ownsVideoScreenGeometry = true;
465
+ }
466
+
467
+ export async function preloadVideoDeviceModel(id: VideoDeviceModelId) {
468
+ if (!VIDEO_DEVICE_MODEL_CONFIGS[id]?.source || templates.has(id)) return;
469
+ const existing = templatePromises.get(id);
470
+ if (existing) {
471
+ await existing;
472
+ return;
473
+ }
474
+ const config = VIDEO_DEVICE_MODEL_CONFIGS[id];
475
+ if (!config.source) return;
476
+ const source = config.source;
477
+ // The studio serves models from its own origin; a local render (no /models there) fetches them from
478
+ // the public site.
479
+ const promise = new GLTFLoader()
480
+ .loadAsync(source)
481
+ .catch(() => new GLTFLoader().loadAsync(`${MODEL_FALLBACK_ORIGIN}${source}`))
482
+ .then(
483
+ (gltf) => {
484
+ templates.set(id, gltf.scene);
485
+ return gltf.scene;
486
+ },
487
+ (error: unknown) => {
488
+ // Forget the failure so the next device (or a retry) loads again instead of failing for good.
489
+ templatePromises.delete(id);
490
+ throw error;
491
+ },
492
+ );
493
+ templatePromises.set(id, promise);
494
+ await promise;
495
+ }
496
+
497
+ export function createPreloadedVideoDeviceModel(
498
+ id: VideoDeviceModelId,
499
+ screenMaterial: THREE.Material,
500
+ finishColor?: THREE.ColorRepresentation,
501
+ ): LoadedVideoDeviceModel | null {
502
+ const config = VIDEO_DEVICE_MODEL_CONFIGS[id];
503
+ if (!config) return null;
504
+ if (!config.source) {
505
+ const root =
506
+ id === "procedural-iphone"
507
+ ? createIphoneVideoMockupModel({ screenMaterial, finishColor })
508
+ : id === "procedural-display"
509
+ ? createDisplayModel({ screenMaterial, finishColor })
510
+ : createMacbookModel({ screenMaterial, finishColor });
511
+ const screen = root.getObjectByName("display-surface");
512
+ if (!(screen instanceof THREE.Mesh)) {
513
+ disposeVideoDeviceObjectMaterials(root);
514
+ disposeVideoDeviceObjectGeometries(root);
515
+ return null;
516
+ }
517
+ screen.renderOrder = 4;
518
+ return {
519
+ config,
520
+ root,
521
+ screen,
522
+ screenFrame: computeScreenFrame(screen),
523
+ ownsGeometries: true,
524
+ };
525
+ }
526
+
527
+ const template = templates.get(id);
528
+ if (!template) return null;
529
+ const modelRoot = cloneModelTemplate(template);
530
+ const screen = findScreenMesh(modelRoot, config);
531
+ if (!screen) {
532
+ disposeVideoDeviceObjectMaterials(modelRoot);
533
+ return null;
534
+ }
535
+ modelRoot.updateMatrixWorld(true);
536
+ projectScreenUvs(screen, config);
537
+ const previousScreenMaterials = Array.isArray(screen.material)
538
+ ? screen.material
539
+ : [screen.material];
540
+ previousScreenMaterials.forEach((material) => material.dispose());
541
+ screen.material = screenMaterial;
542
+ screen.renderOrder = 4;
543
+ normalizeModel(modelRoot, config);
544
+ const root = new THREE.Group();
545
+ root.name = `${config.name} rotation root`;
546
+ root.add(modelRoot);
547
+ return {
548
+ config,
549
+ root,
550
+ screen,
551
+ screenFrame: computeScreenFrame(screen),
552
+ ownsGeometries: false,
553
+ };
554
+ }
555
+
556
+ export async function loadVideoDeviceModel(
557
+ id: VideoDeviceModelId,
558
+ screenMaterial: THREE.Material,
559
+ finishColor?: THREE.ColorRepresentation,
560
+ ) {
561
+ await preloadVideoDeviceModel(id);
562
+ return createPreloadedVideoDeviceModel(id, screenMaterial, finishColor);
563
+ }
564
+
565
+ export function disposeVideoDeviceModel(model: LoadedVideoDeviceModel) {
566
+ const disposedGeometries = new Set<THREE.BufferGeometry>();
567
+ const disposedMaterials = new Set<THREE.Material>();
568
+ model.root.traverse((object) => {
569
+ if (!(object instanceof THREE.Mesh)) return;
570
+ if (
571
+ (model.ownsGeometries || object.userData.ownsVideoScreenGeometry) &&
572
+ !disposedGeometries.has(object.geometry)
573
+ ) {
574
+ disposedGeometries.add(object.geometry);
575
+ object.geometry.dispose();
576
+ }
577
+ const materials = Array.isArray(object.material)
578
+ ? object.material
579
+ : [object.material];
580
+ materials.forEach((material) => {
581
+ if (disposedMaterials.has(material)) return;
582
+ disposedMaterials.add(material);
583
+ material.dispose();
584
+ });
585
+ });
586
+ }
587
+
588
+ function disposeVideoDeviceObjectGeometries(root: THREE.Object3D): void {
589
+ const geometries = new Set<THREE.BufferGeometry>();
590
+ root.traverse((object) => {
591
+ if (!(object instanceof THREE.Mesh) || geometries.has(object.geometry)) {
592
+ return;
593
+ }
594
+ geometries.add(object.geometry);
595
+ object.geometry.dispose();
596
+ });
597
+ }
598
+
599
+ function disposeVideoDeviceObjectMaterials(root: THREE.Object3D): void {
600
+ const materials = new Set<THREE.Material>();
601
+ root.traverse((object) => {
602
+ if (!(object instanceof THREE.Mesh)) return;
603
+ const objectMaterials = Array.isArray(object.material)
604
+ ? object.material
605
+ : [object.material];
606
+ objectMaterials.forEach((material) => {
607
+ if (materials.has(material)) return;
608
+ materials.add(material);
609
+ material.dispose();
610
+ });
611
+ });
612
+ }