agentcut 0.2.4 → 0.2.6

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