agentcut 0.2.26 → 0.2.28

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 (97) hide show
  1. package/README.md +177 -177
  2. package/dist/studio/agents/claude.js +3 -2
  3. package/dist/studio/agents/claude.js.map +1 -1
  4. package/dist/studio/chat.js +1 -1
  5. package/dist/studio/chat.js.map +1 -1
  6. package/dist/studio/cli.js +24 -24
  7. package/dist/studio/download.d.ts +24 -0
  8. package/dist/studio/download.js +234 -0
  9. package/dist/studio/download.js.map +1 -0
  10. package/dist/studio/host.js +8 -0
  11. package/dist/studio/host.js.map +1 -1
  12. package/dist/studio/housekeeping.d.ts +2 -2
  13. package/dist/studio/housekeeping.js +18 -17
  14. package/dist/studio/housekeeping.js.map +1 -1
  15. package/dist/studio/release-cursor.js +21 -21
  16. package/dist/studio/release-video.js +10 -10
  17. package/dist/studio/schema.d.ts +102 -0
  18. package/dist/studio/schema.js +5 -0
  19. package/dist/studio/schema.js.map +1 -1
  20. package/dist/studio/tools.js +7 -0
  21. package/dist/studio/tools.js.map +1 -1
  22. package/package.json +56 -56
  23. package/template/AGENTS.md +14 -14
  24. package/template/components/README.md +70 -70
  25. package/template/components/before-after/BeforeAfter.tsx +89 -89
  26. package/template/components/catalog.ts +107 -107
  27. package/template/components/cinematic/Device3D.tsx +324 -324
  28. package/template/components/cinematic/devices3d/display-model.ts +184 -184
  29. package/template/components/cinematic/devices3d/iphone-model.ts +643 -643
  30. package/template/components/cinematic/devices3d/macbook-model.ts +608 -608
  31. package/template/components/cinematic/devices3d/models.ts +657 -657
  32. package/template/components/cinematic/devices3d/runtime.ts +267 -267
  33. package/template/components/cinematic/math.ts +100 -100
  34. package/template/components/color/grade-effect.ts +650 -650
  35. package/template/components/color/grade.ts +512 -512
  36. package/template/components/color/looks.ts +154 -154
  37. package/template/components/countdown/Countdown.tsx +166 -166
  38. package/template/components/dot-trace-title/DotTraceTitle.tsx +181 -181
  39. package/template/components/dot-trace-title/meta.json +51 -51
  40. package/template/components/generate/GenerateReveal.tsx +381 -381
  41. package/template/components/hook-card/HookCard.tsx +554 -554
  42. package/template/components/index.ts +199 -199
  43. package/template/components/lib.ts +191 -191
  44. package/template/components/line-diagram/LineDiagram.tsx +205 -205
  45. package/template/components/line-diagram/meta.json +215 -215
  46. package/template/components/list-card/ListCard.tsx +104 -104
  47. package/template/components/live-clip.ts +75 -75
  48. package/template/components/looks.tsx +1447 -1447
  49. package/template/components/loops/LoopCanvas.tsx +132 -132
  50. package/template/components/loops/engine.ts +184 -184
  51. package/template/components/loops/library.ts +667 -667
  52. package/template/components/loops/library2.ts +777 -777
  53. package/template/components/motion-loop/MotionLoop.tsx +37 -37
  54. package/template/components/motion-loop/meta.json +311 -311
  55. package/template/components/props.ts +73 -73
  56. package/template/components/quote-card/QuoteCard.tsx +71 -71
  57. package/template/components/style-layer.tsx +233 -233
  58. package/template/components/styles.ts +113 -113
  59. package/template/components/types.ts +66 -66
  60. package/template/components/word-pop/WordPop.tsx +59 -59
  61. package/template/components/word-stack/WordStack.tsx +122 -122
  62. package/template/presets/charcoal-sketch/PROMPT.md +48 -48
  63. package/template/presets/charcoal-sketch/scripts/duck_music.py +34 -34
  64. package/template/presets/punk-cut-paper/scenes/Motif.tsx +376 -376
  65. package/template/presets/punk-cut-paper/scenes/Photo.tsx +73 -73
  66. package/template/presets/punk-cut-paper/scenes/Ransom.tsx +119 -119
  67. package/template/runtime/composition.tsx +1207 -1189
  68. package/template/runtime/helpers.tsx +230 -230
  69. package/template/runtime/index.tsx +26 -22
  70. package/template/runtime/types.ts +324 -315
  71. package/template/skill/SKILL.md +188 -187
  72. package/template/skill/references/broll.md +44 -44
  73. package/template/skill/references/browser-export.md +61 -61
  74. package/template/skill/references/cinematic.md +206 -206
  75. package/template/skill/references/color.md +79 -79
  76. package/template/skill/references/craft.md +97 -97
  77. package/template/skill/references/editing-styles.md +57 -57
  78. package/template/skill/references/feature-launch.md +34 -34
  79. package/template/skill/references/footage.md +72 -72
  80. package/template/skill/references/format.md +145 -142
  81. package/template/skill/references/how-to.md +146 -146
  82. package/template/skill/references/launch-direction.md +89 -89
  83. package/template/skill/references/loops.md +151 -151
  84. package/template/skill/references/mcp-external.md +19 -19
  85. package/template/skill/references/performance.md +93 -93
  86. package/template/skill/references/person-cutout.md +19 -19
  87. package/template/skill/references/presets.md +60 -60
  88. package/template/skill/references/product-launch.md +134 -134
  89. package/template/skill/references/release-capture.md +53 -53
  90. package/template/skill/references/sound.md +167 -166
  91. package/template/skill/references/style-spy.md +160 -160
  92. package/template/skill/references/website-howto.md +45 -45
  93. package/template/starter/.studio/chat.json +1 -1
  94. package/template/starter/layers.json +46 -46
  95. package/template/starter/notes.md +4 -4
  96. package/template/starter/project.json +13 -13
  97. package/template/starter/scenes/Title.tsx +38 -38
@@ -1,608 +1,608 @@
1
- import * as THREE from "three";
2
- import { RoundedBoxGeometry } from "three/examples/jsm/geometries/RoundedBoxGeometry.js";
3
- import { SVGLoader } from "three/examples/jsm/loaders/SVGLoader.js";
4
- import { mergeGeometries } from "three/examples/jsm/utils/BufferGeometryUtils.js";
5
- import {
6
- APPLE_MARK_SVG,
7
- createRoundedExtrudedGeometry,
8
- createRoundedPlaneGeometry,
9
- } from "./iphone-model";
10
- const DEFAULT_LAPTOP_LID_ANGLE = 110;
11
-
12
- /**
13
- * Procedural 13-inch laptop in the spirit of a MacBook Air. Built from real
14
- * millimetre proportions, then scaled so the deck is 2 scene units wide.
15
- *
16
- * Axes: +x right, +y up, +z toward the viewer. The deck's top surface sits at
17
- * y = 0 and the lid hinges along its back edge.
18
- */
19
-
20
- const MM = 2 / 312.6;
21
- const mm = (value: number) => value * MM;
22
-
23
- const BASE = { w: 312.6, d: 221.2, t: 10.4, corner: 10.5 };
24
- const LID = {
25
- h: 221.2,
26
- t: 4.8,
27
- corner: 10.5,
28
- glassInset: 1.1,
29
- bezelSide: 5.6,
30
- bezelTop: 5.6,
31
- bezelBottom: 7.6,
32
- notch: { w: 31, h: 6.4 },
33
- };
34
- const KEYBOARD = { pitch: 18.55, gap: 2.35, back: 13, corner: 1.6 };
35
- const TRACKPAD = { w: 140, h: 82, gap: 10, corner: 7.5 };
36
-
37
- export const MACBOOK_SCREEN_ASPECT =
38
- (BASE.w - LID.bezelSide * 2) / (LID.h - LID.bezelTop - LID.bezelBottom);
39
-
40
-
41
- type KeySpec = [label: string, width: number, kind?: "mod" | "half"];
42
-
43
- const U = 1;
44
- const KEY_ROWS: KeySpec[][] = [
45
- [
46
- ["esc", 1.5, "mod"],
47
- ...Array.from(
48
- { length: 12 },
49
- (_, index): KeySpec => [`F${index + 1}`, U, "mod"],
50
- ),
51
- ["", U],
52
- ],
53
- [
54
- ...["`", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "-", "="].map(
55
- (label): KeySpec => [label, U],
56
- ),
57
- ["delete", 1.5, "mod"],
58
- ],
59
- [
60
- ["tab", 1.5, "mod"],
61
- ...[..."QWERTYUIOP", "[", "]", "\\"].map((label): KeySpec => [label, U]),
62
- ],
63
- [
64
- ["caps lock", 1.8, "mod"],
65
- ...[..."ASDFGHJKL", ";", "'"].map((label): KeySpec => [label, U]),
66
- ["return", 1.7, "mod"],
67
- ],
68
- [
69
- ["shift", 2.35, "mod"],
70
- ...[..."ZXCVBNM", ",", ".", "/"].map((label): KeySpec => [label, U]),
71
- ["shift", 2.15, "mod"],
72
- ],
73
- [
74
- ["fn", U, "mod"],
75
- ["control", U, "mod"],
76
- ["option", U, "mod"],
77
- ["command", 1.25, "mod"],
78
- ["", 5],
79
- ["command", 1.25, "mod"],
80
- ["option", U, "mod"],
81
- ["◀", U, "half"],
82
- ["▲▼", U, "half"],
83
- ["▶", U, "half"],
84
- ],
85
- ];
86
-
87
- interface KeyRect {
88
- label: string;
89
- kind?: "mod" | "half";
90
- x: number;
91
- z: number;
92
- w: number;
93
- d: number;
94
- }
95
-
96
- function layoutKeys() {
97
- const { pitch, gap, back } = KEYBOARD;
98
- const rowWidth = 14.5 * pitch - gap;
99
- const keys: KeyRect[] = [];
100
- let z = -BASE.d / 2 + back;
101
- KEY_ROWS.forEach((row, rowIndex) => {
102
- const rowDepth = rowIndex === 0 ? pitch * 0.55 : pitch;
103
- let x = -rowWidth / 2;
104
- for (const [label, width, kind] of row) {
105
- const keyWidth = width * pitch - gap;
106
- const keyDepth = rowDepth - gap;
107
- if (kind === "half") {
108
- if (label === "▲▼") {
109
- const half = (keyDepth - gap * 0.5) / 2;
110
- keys.push({ label: "▲", kind, x, z, w: keyWidth, d: half });
111
- keys.push({
112
- label: "▼",
113
- kind,
114
- x,
115
- z: z + half + gap * 0.5,
116
- w: keyWidth,
117
- d: half,
118
- });
119
- } else {
120
- const half = (keyDepth - gap * 0.5) / 2;
121
- keys.push({
122
- label,
123
- kind,
124
- x,
125
- z: z + half + gap * 0.5,
126
- w: keyWidth,
127
- d: half,
128
- });
129
- }
130
- } else {
131
- keys.push({ label, kind, x, z, w: keyWidth, d: keyDepth });
132
- }
133
- x += width * pitch;
134
- }
135
- z += rowDepth;
136
- });
137
- return { keys, rowWidth, depth: z - (-BASE.d / 2 + back) };
138
- }
139
-
140
- function createCanvas(width: number, height: number) {
141
- if (typeof OffscreenCanvas !== "undefined") {
142
- return new OffscreenCanvas(width, height);
143
- }
144
- if (typeof document !== "undefined") {
145
- const canvas = document.createElement("canvas");
146
- canvas.width = width;
147
- canvas.height = height;
148
- return canvas;
149
- }
150
- return null;
151
- }
152
-
153
- /** Key legends painted once into a texture laid over the keycaps. */
154
- function createLegendTexture(
155
- keys: KeyRect[],
156
- bounds: { x: number; z: number; w: number; d: number },
157
- ) {
158
- // Dense enough that the legends stay crisp in close-ups.
159
- const pxPerMm = 14;
160
- const width = Math.ceil(bounds.w * pxPerMm);
161
- const height = Math.ceil(bounds.d * pxPerMm);
162
- const canvas = createCanvas(width, height);
163
- const context = canvas?.getContext("2d") as
164
- | CanvasRenderingContext2D
165
- | OffscreenCanvasRenderingContext2D
166
- | null
167
- | undefined;
168
- if (!canvas || !context) return null;
169
- context.clearRect(0, 0, width, height);
170
- // Bright, slightly bold legends: backlit-key white that reads against the black caps.
171
- context.fillStyle = "#ffffff";
172
- context.textBaseline = "middle";
173
- for (const key of keys) {
174
- if (!key.label) continue;
175
- const left = (key.x - bounds.x) * pxPerMm;
176
- const top = (key.z - bounds.z) * pxPerMm;
177
- const keyWidth = key.w * pxPerMm;
178
- const keyDepth = key.d * pxPerMm;
179
- if (key.kind === "mod") {
180
- context.font = `600 ${Math.round(2.5 * pxPerMm)}px -apple-system, "SF Pro Text", "Helvetica Neue", Arial, sans-serif`;
181
- context.textAlign = "left";
182
- context.fillText(
183
- key.label,
184
- left + 1.6 * pxPerMm,
185
- top + keyDepth - 2.4 * pxPerMm,
186
- keyWidth - 3 * pxPerMm,
187
- );
188
- } else {
189
- const fontSize = key.kind === "half" ? 2.6 : 3.9;
190
- context.font = `600 ${Math.round(fontSize * pxPerMm)}px -apple-system, "SF Pro Text", "Helvetica Neue", Arial, sans-serif`;
191
- context.textAlign = "center";
192
- context.fillText(
193
- key.label,
194
- left + keyWidth / 2,
195
- top + keyDepth / 2,
196
- keyWidth - 2 * pxPerMm,
197
- );
198
- }
199
- }
200
- const texture = new THREE.CanvasTexture(canvas as HTMLCanvasElement);
201
- texture.colorSpace = THREE.SRGBColorSpace;
202
- texture.anisotropy = 8;
203
- return texture;
204
- }
205
-
206
- /** Speaker grille: a fine field of dark perforations on a transparent texture. */
207
- function createGrilleTexture(widthMm: number, depthMm: number) {
208
- const pxPerMm = 12;
209
- const width = Math.ceil(widthMm * pxPerMm);
210
- const height = Math.ceil(depthMm * pxPerMm);
211
- const canvas = createCanvas(width, height);
212
- const context = canvas?.getContext("2d") as CanvasRenderingContext2D | OffscreenCanvasRenderingContext2D | null | undefined;
213
- if (!canvas || !context) return null;
214
- context.clearRect(0, 0, width, height);
215
- context.fillStyle = "rgba(6, 6, 8, 0.85)";
216
- const pitch = 1.15 * pxPerMm;
217
- for (let y = pitch / 2, row = 0; y < height; y += pitch * 0.87, row++) {
218
- for (let x = pitch / 2 + (row % 2) * (pitch / 2); x < width; x += pitch) {
219
- context.beginPath();
220
- context.arc(x, y, 0.32 * pxPerMm, 0, Math.PI * 2);
221
- context.fill();
222
- }
223
- }
224
- const texture = new THREE.CanvasTexture(canvas as HTMLCanvasElement);
225
- texture.colorSpace = THREE.SRGBColorSpace;
226
- texture.anisotropy = 8;
227
- return texture;
228
- }
229
-
230
- function createFlatRoundedRect(width: number, depth: number, radius: number) {
231
- const geometry = createRoundedPlaneGeometry(width, depth, radius);
232
- geometry.rotateX(-Math.PI / 2);
233
- return geometry;
234
- }
235
-
236
- function createLogoGeometry(width: number) {
237
- const svg = new SVGLoader().parse(APPLE_MARK_SVG);
238
- const shapes = svg.paths.flatMap((path) => SVGLoader.createShapes(path));
239
- const geometry = new THREE.ShapeGeometry(shapes, 32);
240
- geometry.center();
241
- const scale = width / 814;
242
- geometry.scale(scale, -scale, 1);
243
- return geometry;
244
- }
245
-
246
- function createMaterials(finishColor: THREE.ColorRepresentation) {
247
- const finish = new THREE.Color(finishColor);
248
- const { l: lightness } = finish.getHSL({ h: 0, s: 0, l: 0 });
249
- // Space black and darker finishes count as dark.
250
- const isDark = lightness < 0.4;
251
- return {
252
- // Bead-blasted anodised aluminium: soft, wide highlights, the finish's own colour in the shadows.
253
- // Dark finishes (space black) reflect much less of the studio, like real dark anodising: otherwise a
254
- // low camera facing the deck sees the bright environment and the black washes out to light grey.
255
- aluminum: new THREE.MeshPhysicalMaterial({
256
- color: finish,
257
- metalness: isDark ? 0.55 : 0.85,
258
- roughness: isDark ? 0.48 : 0.34,
259
- clearcoat: isDark ? 0.04 : 0.18,
260
- clearcoatRoughness: 0.4,
261
- envMapIntensity: isDark ? 0.3 : 0.75,
262
- }),
263
- edge: new THREE.MeshPhysicalMaterial({
264
- color: finish.clone().lerp(new THREE.Color(0xffffff), isDark ? 0.08 : 0.2),
265
- metalness: isDark ? 0.85 : 1,
266
- roughness: isDark ? 0.3 : 0.22,
267
- envMapIntensity: isDark ? 0.55 : 1,
268
- }),
269
- trackpad: new THREE.MeshPhysicalMaterial({
270
- color: finish.clone().lerp(new THREE.Color(0x000000), isDark ? 0.12 : 0.06),
271
- metalness: isDark ? 0.4 : 0.5,
272
- roughness: isDark ? 0.36 : 0.28,
273
- clearcoat: isDark ? 0.25 : 0.6,
274
- clearcoatRoughness: 0.18,
275
- envMapIntensity: isDark ? 0.28 : 0.6,
276
- }),
277
- well: new THREE.MeshStandardMaterial({
278
- color: 0x050506,
279
- roughness: 0.85,
280
- metalness: 0,
281
- envMapIntensity: 0.06,
282
- }),
283
- key: new THREE.MeshPhysicalMaterial({
284
- color: 0x0a0a0c,
285
- roughness: 0.7,
286
- metalness: 0.05,
287
- clearcoat: 0.05,
288
- envMapIntensity: 0.16,
289
- }),
290
- grille: new THREE.MeshBasicMaterial({ transparent: true, depthWrite: false }),
291
- legends: new THREE.MeshBasicMaterial({
292
- transparent: true,
293
- depthWrite: false,
294
- toneMapped: false,
295
- }),
296
- glass: new THREE.MeshBasicMaterial({ color: 0x030405 }),
297
- notch: new THREE.MeshBasicMaterial({ color: 0x030304 }),
298
- lens: new THREE.MeshPhysicalMaterial({
299
- color: 0x10162a,
300
- roughness: 0.05,
301
- metalness: 0.3,
302
- clearcoat: 1,
303
- }),
304
- hinge: new THREE.MeshPhysicalMaterial({
305
- color: finish.clone().lerp(new THREE.Color(0x000000), 0.45),
306
- metalness: 0.8,
307
- roughness: 0.35,
308
- }),
309
- rubber: new THREE.MeshStandardMaterial({ color: 0x18181a, roughness: 0.9, envMapIntensity: 0.2 }),
310
- logo: new THREE.MeshPhysicalMaterial({
311
- color: finish
312
- .clone()
313
- .lerp(new THREE.Color(isDark ? 0xffffff : 0x000000), isDark ? 0.2 : 0.45),
314
- metalness: 1,
315
- roughness: 0.05,
316
- side: THREE.DoubleSide,
317
- }),
318
- screenOff: new THREE.MeshBasicMaterial({ color: 0x050608 }),
319
- };
320
- }
321
-
322
- export interface MacbookModelOptions {
323
- screenMaterial?: THREE.Material;
324
- finishColor?: THREE.ColorRepresentation;
325
- lidAngle?: number;
326
- }
327
-
328
- export function createMacbookModel(options: MacbookModelOptions = {}) {
329
- const root = new THREE.Group();
330
- root.name = "procedural-macbook";
331
- const materials = createMaterials(options.finishColor ?? 0xcfd1d3);
332
- const add = (
333
- name: string,
334
- mesh: THREE.Mesh,
335
- parent: THREE.Object3D = root,
336
- ) => {
337
- mesh.name = name;
338
- mesh.castShadow = true;
339
- mesh.receiveShadow = true;
340
- parent.add(mesh);
341
- return mesh;
342
- };
343
-
344
- // ── Deck ─────────────────────────────────────────────────────────────
345
- const baseGeometry = createRoundedExtrudedGeometry(
346
- mm(BASE.w),
347
- mm(BASE.d),
348
- mm(BASE.corner),
349
- mm(BASE.t - 1.2),
350
- mm(0.6),
351
- mm(0.6),
352
- );
353
- baseGeometry.rotateX(-Math.PI / 2);
354
- const base = add("laptop-base", new THREE.Mesh(baseGeometry, materials.aluminum));
355
- base.position.y = -mm(BASE.t) / 2;
356
-
357
- // Chamfered rim catching a bright highlight around the deck.
358
- const rim = add(
359
- "laptop-deck-rim",
360
- new THREE.Mesh(
361
- createFlatRoundedRect(mm(BASE.w - 0.8), mm(BASE.d - 0.8), mm(BASE.corner)),
362
- materials.edge,
363
- ),
364
- );
365
- rim.position.y = mm(0.12);
366
- const deckTop = add(
367
- "laptop-deck-top",
368
- new THREE.Mesh(
369
- createFlatRoundedRect(mm(BASE.w - 2.2), mm(BASE.d - 2.2), mm(BASE.corner - 1)),
370
- materials.aluminum,
371
- ),
372
- );
373
- deckTop.position.y = mm(0.24);
374
-
375
- // ── Keyboard ─────────────────────────────────────────────────────────
376
- const { keys, rowWidth, depth } = layoutKeys();
377
- const keyboardBounds = {
378
- x: -rowWidth / 2 - 1.2,
379
- z: -BASE.d / 2 + KEYBOARD.back - 1.2,
380
- w: rowWidth + 2.4,
381
- d: depth - KEYBOARD.gap + 2.4,
382
- };
383
- const well = add(
384
- "laptop-keyboard-well",
385
- new THREE.Mesh(
386
- createFlatRoundedRect(
387
- mm(keyboardBounds.w),
388
- mm(keyboardBounds.d),
389
- mm(2.4),
390
- ),
391
- materials.well,
392
- ),
393
- );
394
- well.position.set(
395
- mm(keyboardBounds.x + keyboardBounds.w / 2),
396
- mm(0.4),
397
- mm(keyboardBounds.z + keyboardBounds.d / 2),
398
- );
399
-
400
- const keyHeight = 0.9;
401
- const keyGeometries = keys.map((key) => {
402
- const geometry = new RoundedBoxGeometry(
403
- mm(key.w),
404
- mm(keyHeight),
405
- mm(key.d),
406
- 2,
407
- mm(Math.min(KEYBOARD.corner, key.d / 2.2, key.w / 2.2)),
408
- );
409
- geometry.translate(
410
- mm(key.x + key.w / 2),
411
- mm(keyHeight / 2 + 0.45),
412
- mm(key.z + key.d / 2),
413
- );
414
- return geometry;
415
- });
416
- const keysGeometry = mergeGeometries(keyGeometries, false);
417
- keyGeometries.forEach((geometry) => geometry.dispose());
418
- if (keysGeometry) add("laptop-keys", new THREE.Mesh(keysGeometry, materials.key));
419
-
420
- const legendTexture = createLegendTexture(keys, keyboardBounds);
421
- if (legendTexture) {
422
- materials.legends.map = legendTexture;
423
- const legends = add(
424
- "laptop-key-legends",
425
- new THREE.Mesh(
426
- new THREE.PlaneGeometry(mm(keyboardBounds.w), mm(keyboardBounds.d)).rotateX(
427
- -Math.PI / 2,
428
- ),
429
- materials.legends,
430
- ),
431
- );
432
- legends.castShadow = false;
433
- legends.position.set(
434
- mm(keyboardBounds.x + keyboardBounds.w / 2),
435
- mm(keyHeight + 0.6),
436
- mm(keyboardBounds.z + keyboardBounds.d / 2),
437
- );
438
- legends.renderOrder = 2;
439
- }
440
-
441
- // ── Speaker grilles either side of the keyboard ─────────────────────
442
- const sideRoom = (BASE.w - keyboardBounds.w) / 2;
443
- const grilleW = Math.min(12, sideRoom - 6);
444
- const grilleTexture = grilleW > 3 ? createGrilleTexture(grilleW, keyboardBounds.d) : null;
445
- if (grilleTexture) {
446
- materials.grille.map = grilleTexture;
447
- for (const side of [-1, 1]) {
448
- const grille = add(
449
- "laptop-speaker-grille",
450
- new THREE.Mesh(new THREE.PlaneGeometry(mm(grilleW), mm(keyboardBounds.d)).rotateX(-Math.PI / 2), materials.grille),
451
- );
452
- grille.castShadow = false;
453
- grille.position.set(
454
- side * mm(keyboardBounds.w / 2 + sideRoom / 2),
455
- mm(0.3),
456
- mm(keyboardBounds.z + keyboardBounds.d / 2),
457
- );
458
- grille.renderOrder = 2;
459
- }
460
- }
461
-
462
- // ── Trackpad and front lip ──────────────────────────────────────────
463
- const trackpadCenterZ =
464
- keyboardBounds.z + keyboardBounds.d + TRACKPAD.gap + TRACKPAD.h / 2;
465
- const trackpad = add(
466
- "laptop-trackpad",
467
- new THREE.Mesh(
468
- createFlatRoundedRect(mm(TRACKPAD.w), mm(TRACKPAD.h), mm(TRACKPAD.corner)),
469
- materials.trackpad,
470
- ),
471
- );
472
- trackpad.position.set(0, mm(0.4), mm(trackpadCenterZ));
473
-
474
- const notchGeometry = new THREE.CylinderGeometry(
475
- mm(14),
476
- mm(14),
477
- mm(1.4),
478
- 48,
479
- 1,
480
- false,
481
- Math.PI / 2,
482
- Math.PI,
483
- );
484
- notchGeometry.scale(1, 1, 0.22);
485
- const thumbNotch = add(
486
- "laptop-thumb-notch",
487
- new THREE.Mesh(notchGeometry, materials.hinge),
488
- );
489
- thumbNotch.position.set(0, -mm(0.6), mm(BASE.d / 2 + 0.25));
490
-
491
- // ── Feet ─────────────────────────────────────────────────────────────
492
- const footGeometry = new THREE.CylinderGeometry(mm(6), mm(6.5), mm(1.3), 32);
493
- for (const x of [-1, 1]) {
494
- for (const z of [-1, 1]) {
495
- const foot = add("laptop-foot", new THREE.Mesh(footGeometry, materials.rubber));
496
- foot.position.set(
497
- x * mm(BASE.w / 2 - 24),
498
- -mm(BASE.t) - mm(0.5),
499
- z * mm(BASE.d / 2 - 24),
500
- );
501
- }
502
- }
503
-
504
- // ── Hinge and lid ───────────────────────────────────────────────────
505
- const hingeZ = -mm(BASE.d / 2) + mm(3.2);
506
- const hingeY = mm(1.2);
507
- const hinge = add(
508
- "laptop-hinge",
509
- new THREE.Mesh(
510
- new THREE.CylinderGeometry(mm(3.4), mm(3.4), mm(BASE.w - 72), 32).rotateZ(
511
- Math.PI / 2,
512
- ),
513
- materials.hinge,
514
- ),
515
- );
516
- hinge.position.set(0, -mm(1.8), hingeZ - mm(1.4));
517
-
518
- const lid = new THREE.Group();
519
- lid.name = "laptop-lid-pivot";
520
- lid.position.set(0, hingeY, hingeZ);
521
- root.add(lid);
522
-
523
- const lidBevel = mm(0.5);
524
- const lidGeometry = createRoundedExtrudedGeometry(
525
- mm(BASE.w),
526
- mm(LID.h),
527
- mm(LID.corner),
528
- mm(LID.t) - lidBevel * 2,
529
- lidBevel,
530
- lidBevel,
531
- );
532
- const lidShell = add(
533
- "laptop-lid",
534
- new THREE.Mesh(lidGeometry, materials.aluminum),
535
- lid,
536
- );
537
- lidShell.position.set(0, mm(LID.h / 2), -mm(LID.t / 2));
538
-
539
- const glass = add(
540
- "laptop-display-glass",
541
- new THREE.Mesh(
542
- createRoundedPlaneGeometry(
543
- mm(BASE.w - LID.glassInset * 2),
544
- mm(LID.h - LID.glassInset * 2),
545
- mm(LID.corner - LID.glassInset),
546
- ),
547
- materials.glass,
548
- ),
549
- lid,
550
- );
551
- glass.position.set(0, mm(LID.h / 2), mm(0.25));
552
-
553
- const screenWidth = BASE.w - LID.bezelSide * 2;
554
- const screenHeight = LID.h - LID.bezelTop - LID.bezelBottom;
555
- const screen = add(
556
- "display-surface",
557
- new THREE.Mesh(
558
- createRoundedPlaneGeometry(mm(screenWidth), mm(screenHeight), mm(4.2)),
559
- options.screenMaterial ?? materials.screenOff,
560
- ),
561
- lid,
562
- );
563
- screen.position.set(0, mm(LID.bezelBottom + screenHeight / 2), mm(0.5));
564
- screen.castShadow = false;
565
-
566
- const notch = add(
567
- "laptop-camera-notch",
568
- new THREE.Mesh(
569
- createRoundedPlaneGeometry(mm(LID.notch.w), mm(LID.notch.h + 4), mm(2.6)),
570
- materials.notch,
571
- ),
572
- lid,
573
- );
574
- notch.position.set(
575
- 0,
576
- mm(LID.bezelBottom + screenHeight - LID.notch.h / 2 + 2),
577
- mm(0.75),
578
- );
579
- const lens = add(
580
- "laptop-camera-lens",
581
- new THREE.Mesh(new THREE.CircleGeometry(mm(1.1), 24), materials.lens),
582
- lid,
583
- );
584
- lens.position.set(0, mm(LID.bezelBottom + screenHeight + 2), mm(0.85));
585
-
586
- const logo = add(
587
- "laptop-lid-logo",
588
- new THREE.Mesh(createLogoGeometry(mm(26)), materials.logo),
589
- lid,
590
- );
591
- logo.rotation.y = Math.PI;
592
- logo.position.set(0, mm(LID.h * 0.53), -mm(LID.t) - mm(0.3));
593
-
594
- setMacbookLidAngle(root, options.lidAngle ?? DEFAULT_LAPTOP_LID_ANGLE);
595
- return root;
596
- }
597
-
598
- /** Works on clones too, since it looks the hinge up by name. */
599
- export function setMacbookLidAngle(root: THREE.Object3D, degrees: number) {
600
- const lid = root.getObjectByName("laptop-lid-pivot");
601
- if (!lid) return;
602
- const clamped = THREE.MathUtils.clamp(degrees, 0, 140);
603
- lid.rotation.x = THREE.MathUtils.degToRad(90 - clamped);
604
- }
605
-
606
- export function isMacbookModel(root: THREE.Object3D) {
607
- return Boolean(root.getObjectByName("laptop-lid-pivot"));
608
- }
1
+ import * as THREE from "three";
2
+ import { RoundedBoxGeometry } from "three/examples/jsm/geometries/RoundedBoxGeometry.js";
3
+ import { SVGLoader } from "three/examples/jsm/loaders/SVGLoader.js";
4
+ import { mergeGeometries } from "three/examples/jsm/utils/BufferGeometryUtils.js";
5
+ import {
6
+ APPLE_MARK_SVG,
7
+ createRoundedExtrudedGeometry,
8
+ createRoundedPlaneGeometry,
9
+ } from "./iphone-model";
10
+ const DEFAULT_LAPTOP_LID_ANGLE = 110;
11
+
12
+ /**
13
+ * Procedural 13-inch laptop in the spirit of a MacBook Air. Built from real
14
+ * millimetre proportions, then scaled so the deck is 2 scene units wide.
15
+ *
16
+ * Axes: +x right, +y up, +z toward the viewer. The deck's top surface sits at
17
+ * y = 0 and the lid hinges along its back edge.
18
+ */
19
+
20
+ const MM = 2 / 312.6;
21
+ const mm = (value: number) => value * MM;
22
+
23
+ const BASE = { w: 312.6, d: 221.2, t: 10.4, corner: 10.5 };
24
+ const LID = {
25
+ h: 221.2,
26
+ t: 4.8,
27
+ corner: 10.5,
28
+ glassInset: 1.1,
29
+ bezelSide: 5.6,
30
+ bezelTop: 5.6,
31
+ bezelBottom: 7.6,
32
+ notch: { w: 31, h: 6.4 },
33
+ };
34
+ const KEYBOARD = { pitch: 18.55, gap: 2.35, back: 13, corner: 1.6 };
35
+ const TRACKPAD = { w: 140, h: 82, gap: 10, corner: 7.5 };
36
+
37
+ export const MACBOOK_SCREEN_ASPECT =
38
+ (BASE.w - LID.bezelSide * 2) / (LID.h - LID.bezelTop - LID.bezelBottom);
39
+
40
+
41
+ type KeySpec = [label: string, width: number, kind?: "mod" | "half"];
42
+
43
+ const U = 1;
44
+ const KEY_ROWS: KeySpec[][] = [
45
+ [
46
+ ["esc", 1.5, "mod"],
47
+ ...Array.from(
48
+ { length: 12 },
49
+ (_, index): KeySpec => [`F${index + 1}`, U, "mod"],
50
+ ),
51
+ ["", U],
52
+ ],
53
+ [
54
+ ...["`", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "-", "="].map(
55
+ (label): KeySpec => [label, U],
56
+ ),
57
+ ["delete", 1.5, "mod"],
58
+ ],
59
+ [
60
+ ["tab", 1.5, "mod"],
61
+ ...[..."QWERTYUIOP", "[", "]", "\\"].map((label): KeySpec => [label, U]),
62
+ ],
63
+ [
64
+ ["caps lock", 1.8, "mod"],
65
+ ...[..."ASDFGHJKL", ";", "'"].map((label): KeySpec => [label, U]),
66
+ ["return", 1.7, "mod"],
67
+ ],
68
+ [
69
+ ["shift", 2.35, "mod"],
70
+ ...[..."ZXCVBNM", ",", ".", "/"].map((label): KeySpec => [label, U]),
71
+ ["shift", 2.15, "mod"],
72
+ ],
73
+ [
74
+ ["fn", U, "mod"],
75
+ ["control", U, "mod"],
76
+ ["option", U, "mod"],
77
+ ["command", 1.25, "mod"],
78
+ ["", 5],
79
+ ["command", 1.25, "mod"],
80
+ ["option", U, "mod"],
81
+ ["◀", U, "half"],
82
+ ["▲▼", U, "half"],
83
+ ["▶", U, "half"],
84
+ ],
85
+ ];
86
+
87
+ interface KeyRect {
88
+ label: string;
89
+ kind?: "mod" | "half";
90
+ x: number;
91
+ z: number;
92
+ w: number;
93
+ d: number;
94
+ }
95
+
96
+ function layoutKeys() {
97
+ const { pitch, gap, back } = KEYBOARD;
98
+ const rowWidth = 14.5 * pitch - gap;
99
+ const keys: KeyRect[] = [];
100
+ let z = -BASE.d / 2 + back;
101
+ KEY_ROWS.forEach((row, rowIndex) => {
102
+ const rowDepth = rowIndex === 0 ? pitch * 0.55 : pitch;
103
+ let x = -rowWidth / 2;
104
+ for (const [label, width, kind] of row) {
105
+ const keyWidth = width * pitch - gap;
106
+ const keyDepth = rowDepth - gap;
107
+ if (kind === "half") {
108
+ if (label === "▲▼") {
109
+ const half = (keyDepth - gap * 0.5) / 2;
110
+ keys.push({ label: "▲", kind, x, z, w: keyWidth, d: half });
111
+ keys.push({
112
+ label: "▼",
113
+ kind,
114
+ x,
115
+ z: z + half + gap * 0.5,
116
+ w: keyWidth,
117
+ d: half,
118
+ });
119
+ } else {
120
+ const half = (keyDepth - gap * 0.5) / 2;
121
+ keys.push({
122
+ label,
123
+ kind,
124
+ x,
125
+ z: z + half + gap * 0.5,
126
+ w: keyWidth,
127
+ d: half,
128
+ });
129
+ }
130
+ } else {
131
+ keys.push({ label, kind, x, z, w: keyWidth, d: keyDepth });
132
+ }
133
+ x += width * pitch;
134
+ }
135
+ z += rowDepth;
136
+ });
137
+ return { keys, rowWidth, depth: z - (-BASE.d / 2 + back) };
138
+ }
139
+
140
+ function createCanvas(width: number, height: number) {
141
+ if (typeof OffscreenCanvas !== "undefined") {
142
+ return new OffscreenCanvas(width, height);
143
+ }
144
+ if (typeof document !== "undefined") {
145
+ const canvas = document.createElement("canvas");
146
+ canvas.width = width;
147
+ canvas.height = height;
148
+ return canvas;
149
+ }
150
+ return null;
151
+ }
152
+
153
+ /** Key legends painted once into a texture laid over the keycaps. */
154
+ function createLegendTexture(
155
+ keys: KeyRect[],
156
+ bounds: { x: number; z: number; w: number; d: number },
157
+ ) {
158
+ // Dense enough that the legends stay crisp in close-ups.
159
+ const pxPerMm = 14;
160
+ const width = Math.ceil(bounds.w * pxPerMm);
161
+ const height = Math.ceil(bounds.d * pxPerMm);
162
+ const canvas = createCanvas(width, height);
163
+ const context = canvas?.getContext("2d") as
164
+ | CanvasRenderingContext2D
165
+ | OffscreenCanvasRenderingContext2D
166
+ | null
167
+ | undefined;
168
+ if (!canvas || !context) return null;
169
+ context.clearRect(0, 0, width, height);
170
+ // Bright, slightly bold legends: backlit-key white that reads against the black caps.
171
+ context.fillStyle = "#ffffff";
172
+ context.textBaseline = "middle";
173
+ for (const key of keys) {
174
+ if (!key.label) continue;
175
+ const left = (key.x - bounds.x) * pxPerMm;
176
+ const top = (key.z - bounds.z) * pxPerMm;
177
+ const keyWidth = key.w * pxPerMm;
178
+ const keyDepth = key.d * pxPerMm;
179
+ if (key.kind === "mod") {
180
+ context.font = `600 ${Math.round(2.5 * pxPerMm)}px -apple-system, "SF Pro Text", "Helvetica Neue", Arial, sans-serif`;
181
+ context.textAlign = "left";
182
+ context.fillText(
183
+ key.label,
184
+ left + 1.6 * pxPerMm,
185
+ top + keyDepth - 2.4 * pxPerMm,
186
+ keyWidth - 3 * pxPerMm,
187
+ );
188
+ } else {
189
+ const fontSize = key.kind === "half" ? 2.6 : 3.9;
190
+ context.font = `600 ${Math.round(fontSize * pxPerMm)}px -apple-system, "SF Pro Text", "Helvetica Neue", Arial, sans-serif`;
191
+ context.textAlign = "center";
192
+ context.fillText(
193
+ key.label,
194
+ left + keyWidth / 2,
195
+ top + keyDepth / 2,
196
+ keyWidth - 2 * pxPerMm,
197
+ );
198
+ }
199
+ }
200
+ const texture = new THREE.CanvasTexture(canvas as HTMLCanvasElement);
201
+ texture.colorSpace = THREE.SRGBColorSpace;
202
+ texture.anisotropy = 8;
203
+ return texture;
204
+ }
205
+
206
+ /** Speaker grille: a fine field of dark perforations on a transparent texture. */
207
+ function createGrilleTexture(widthMm: number, depthMm: number) {
208
+ const pxPerMm = 12;
209
+ const width = Math.ceil(widthMm * pxPerMm);
210
+ const height = Math.ceil(depthMm * pxPerMm);
211
+ const canvas = createCanvas(width, height);
212
+ const context = canvas?.getContext("2d") as CanvasRenderingContext2D | OffscreenCanvasRenderingContext2D | null | undefined;
213
+ if (!canvas || !context) return null;
214
+ context.clearRect(0, 0, width, height);
215
+ context.fillStyle = "rgba(6, 6, 8, 0.85)";
216
+ const pitch = 1.15 * pxPerMm;
217
+ for (let y = pitch / 2, row = 0; y < height; y += pitch * 0.87, row++) {
218
+ for (let x = pitch / 2 + (row % 2) * (pitch / 2); x < width; x += pitch) {
219
+ context.beginPath();
220
+ context.arc(x, y, 0.32 * pxPerMm, 0, Math.PI * 2);
221
+ context.fill();
222
+ }
223
+ }
224
+ const texture = new THREE.CanvasTexture(canvas as HTMLCanvasElement);
225
+ texture.colorSpace = THREE.SRGBColorSpace;
226
+ texture.anisotropy = 8;
227
+ return texture;
228
+ }
229
+
230
+ function createFlatRoundedRect(width: number, depth: number, radius: number) {
231
+ const geometry = createRoundedPlaneGeometry(width, depth, radius);
232
+ geometry.rotateX(-Math.PI / 2);
233
+ return geometry;
234
+ }
235
+
236
+ function createLogoGeometry(width: number) {
237
+ const svg = new SVGLoader().parse(APPLE_MARK_SVG);
238
+ const shapes = svg.paths.flatMap((path) => SVGLoader.createShapes(path));
239
+ const geometry = new THREE.ShapeGeometry(shapes, 32);
240
+ geometry.center();
241
+ const scale = width / 814;
242
+ geometry.scale(scale, -scale, 1);
243
+ return geometry;
244
+ }
245
+
246
+ function createMaterials(finishColor: THREE.ColorRepresentation) {
247
+ const finish = new THREE.Color(finishColor);
248
+ const { l: lightness } = finish.getHSL({ h: 0, s: 0, l: 0 });
249
+ // Space black and darker finishes count as dark.
250
+ const isDark = lightness < 0.4;
251
+ return {
252
+ // Bead-blasted anodised aluminium: soft, wide highlights, the finish's own colour in the shadows.
253
+ // Dark finishes (space black) reflect much less of the studio, like real dark anodising: otherwise a
254
+ // low camera facing the deck sees the bright environment and the black washes out to light grey.
255
+ aluminum: new THREE.MeshPhysicalMaterial({
256
+ color: finish,
257
+ metalness: isDark ? 0.55 : 0.85,
258
+ roughness: isDark ? 0.48 : 0.34,
259
+ clearcoat: isDark ? 0.04 : 0.18,
260
+ clearcoatRoughness: 0.4,
261
+ envMapIntensity: isDark ? 0.3 : 0.75,
262
+ }),
263
+ edge: new THREE.MeshPhysicalMaterial({
264
+ color: finish.clone().lerp(new THREE.Color(0xffffff), isDark ? 0.08 : 0.2),
265
+ metalness: isDark ? 0.85 : 1,
266
+ roughness: isDark ? 0.3 : 0.22,
267
+ envMapIntensity: isDark ? 0.55 : 1,
268
+ }),
269
+ trackpad: new THREE.MeshPhysicalMaterial({
270
+ color: finish.clone().lerp(new THREE.Color(0x000000), isDark ? 0.12 : 0.06),
271
+ metalness: isDark ? 0.4 : 0.5,
272
+ roughness: isDark ? 0.36 : 0.28,
273
+ clearcoat: isDark ? 0.25 : 0.6,
274
+ clearcoatRoughness: 0.18,
275
+ envMapIntensity: isDark ? 0.28 : 0.6,
276
+ }),
277
+ well: new THREE.MeshStandardMaterial({
278
+ color: 0x050506,
279
+ roughness: 0.85,
280
+ metalness: 0,
281
+ envMapIntensity: 0.06,
282
+ }),
283
+ key: new THREE.MeshPhysicalMaterial({
284
+ color: 0x0a0a0c,
285
+ roughness: 0.7,
286
+ metalness: 0.05,
287
+ clearcoat: 0.05,
288
+ envMapIntensity: 0.16,
289
+ }),
290
+ grille: new THREE.MeshBasicMaterial({ transparent: true, depthWrite: false }),
291
+ legends: new THREE.MeshBasicMaterial({
292
+ transparent: true,
293
+ depthWrite: false,
294
+ toneMapped: false,
295
+ }),
296
+ glass: new THREE.MeshBasicMaterial({ color: 0x030405 }),
297
+ notch: new THREE.MeshBasicMaterial({ color: 0x030304 }),
298
+ lens: new THREE.MeshPhysicalMaterial({
299
+ color: 0x10162a,
300
+ roughness: 0.05,
301
+ metalness: 0.3,
302
+ clearcoat: 1,
303
+ }),
304
+ hinge: new THREE.MeshPhysicalMaterial({
305
+ color: finish.clone().lerp(new THREE.Color(0x000000), 0.45),
306
+ metalness: 0.8,
307
+ roughness: 0.35,
308
+ }),
309
+ rubber: new THREE.MeshStandardMaterial({ color: 0x18181a, roughness: 0.9, envMapIntensity: 0.2 }),
310
+ logo: new THREE.MeshPhysicalMaterial({
311
+ color: finish
312
+ .clone()
313
+ .lerp(new THREE.Color(isDark ? 0xffffff : 0x000000), isDark ? 0.2 : 0.45),
314
+ metalness: 1,
315
+ roughness: 0.05,
316
+ side: THREE.DoubleSide,
317
+ }),
318
+ screenOff: new THREE.MeshBasicMaterial({ color: 0x050608 }),
319
+ };
320
+ }
321
+
322
+ export interface MacbookModelOptions {
323
+ screenMaterial?: THREE.Material;
324
+ finishColor?: THREE.ColorRepresentation;
325
+ lidAngle?: number;
326
+ }
327
+
328
+ export function createMacbookModel(options: MacbookModelOptions = {}) {
329
+ const root = new THREE.Group();
330
+ root.name = "procedural-macbook";
331
+ const materials = createMaterials(options.finishColor ?? 0xcfd1d3);
332
+ const add = (
333
+ name: string,
334
+ mesh: THREE.Mesh,
335
+ parent: THREE.Object3D = root,
336
+ ) => {
337
+ mesh.name = name;
338
+ mesh.castShadow = true;
339
+ mesh.receiveShadow = true;
340
+ parent.add(mesh);
341
+ return mesh;
342
+ };
343
+
344
+ // ── Deck ─────────────────────────────────────────────────────────────
345
+ const baseGeometry = createRoundedExtrudedGeometry(
346
+ mm(BASE.w),
347
+ mm(BASE.d),
348
+ mm(BASE.corner),
349
+ mm(BASE.t - 1.2),
350
+ mm(0.6),
351
+ mm(0.6),
352
+ );
353
+ baseGeometry.rotateX(-Math.PI / 2);
354
+ const base = add("laptop-base", new THREE.Mesh(baseGeometry, materials.aluminum));
355
+ base.position.y = -mm(BASE.t) / 2;
356
+
357
+ // Chamfered rim catching a bright highlight around the deck.
358
+ const rim = add(
359
+ "laptop-deck-rim",
360
+ new THREE.Mesh(
361
+ createFlatRoundedRect(mm(BASE.w - 0.8), mm(BASE.d - 0.8), mm(BASE.corner)),
362
+ materials.edge,
363
+ ),
364
+ );
365
+ rim.position.y = mm(0.12);
366
+ const deckTop = add(
367
+ "laptop-deck-top",
368
+ new THREE.Mesh(
369
+ createFlatRoundedRect(mm(BASE.w - 2.2), mm(BASE.d - 2.2), mm(BASE.corner - 1)),
370
+ materials.aluminum,
371
+ ),
372
+ );
373
+ deckTop.position.y = mm(0.24);
374
+
375
+ // ── Keyboard ─────────────────────────────────────────────────────────
376
+ const { keys, rowWidth, depth } = layoutKeys();
377
+ const keyboardBounds = {
378
+ x: -rowWidth / 2 - 1.2,
379
+ z: -BASE.d / 2 + KEYBOARD.back - 1.2,
380
+ w: rowWidth + 2.4,
381
+ d: depth - KEYBOARD.gap + 2.4,
382
+ };
383
+ const well = add(
384
+ "laptop-keyboard-well",
385
+ new THREE.Mesh(
386
+ createFlatRoundedRect(
387
+ mm(keyboardBounds.w),
388
+ mm(keyboardBounds.d),
389
+ mm(2.4),
390
+ ),
391
+ materials.well,
392
+ ),
393
+ );
394
+ well.position.set(
395
+ mm(keyboardBounds.x + keyboardBounds.w / 2),
396
+ mm(0.4),
397
+ mm(keyboardBounds.z + keyboardBounds.d / 2),
398
+ );
399
+
400
+ const keyHeight = 0.9;
401
+ const keyGeometries = keys.map((key) => {
402
+ const geometry = new RoundedBoxGeometry(
403
+ mm(key.w),
404
+ mm(keyHeight),
405
+ mm(key.d),
406
+ 2,
407
+ mm(Math.min(KEYBOARD.corner, key.d / 2.2, key.w / 2.2)),
408
+ );
409
+ geometry.translate(
410
+ mm(key.x + key.w / 2),
411
+ mm(keyHeight / 2 + 0.45),
412
+ mm(key.z + key.d / 2),
413
+ );
414
+ return geometry;
415
+ });
416
+ const keysGeometry = mergeGeometries(keyGeometries, false);
417
+ keyGeometries.forEach((geometry) => geometry.dispose());
418
+ if (keysGeometry) add("laptop-keys", new THREE.Mesh(keysGeometry, materials.key));
419
+
420
+ const legendTexture = createLegendTexture(keys, keyboardBounds);
421
+ if (legendTexture) {
422
+ materials.legends.map = legendTexture;
423
+ const legends = add(
424
+ "laptop-key-legends",
425
+ new THREE.Mesh(
426
+ new THREE.PlaneGeometry(mm(keyboardBounds.w), mm(keyboardBounds.d)).rotateX(
427
+ -Math.PI / 2,
428
+ ),
429
+ materials.legends,
430
+ ),
431
+ );
432
+ legends.castShadow = false;
433
+ legends.position.set(
434
+ mm(keyboardBounds.x + keyboardBounds.w / 2),
435
+ mm(keyHeight + 0.6),
436
+ mm(keyboardBounds.z + keyboardBounds.d / 2),
437
+ );
438
+ legends.renderOrder = 2;
439
+ }
440
+
441
+ // ── Speaker grilles either side of the keyboard ─────────────────────
442
+ const sideRoom = (BASE.w - keyboardBounds.w) / 2;
443
+ const grilleW = Math.min(12, sideRoom - 6);
444
+ const grilleTexture = grilleW > 3 ? createGrilleTexture(grilleW, keyboardBounds.d) : null;
445
+ if (grilleTexture) {
446
+ materials.grille.map = grilleTexture;
447
+ for (const side of [-1, 1]) {
448
+ const grille = add(
449
+ "laptop-speaker-grille",
450
+ new THREE.Mesh(new THREE.PlaneGeometry(mm(grilleW), mm(keyboardBounds.d)).rotateX(-Math.PI / 2), materials.grille),
451
+ );
452
+ grille.castShadow = false;
453
+ grille.position.set(
454
+ side * mm(keyboardBounds.w / 2 + sideRoom / 2),
455
+ mm(0.3),
456
+ mm(keyboardBounds.z + keyboardBounds.d / 2),
457
+ );
458
+ grille.renderOrder = 2;
459
+ }
460
+ }
461
+
462
+ // ── Trackpad and front lip ──────────────────────────────────────────
463
+ const trackpadCenterZ =
464
+ keyboardBounds.z + keyboardBounds.d + TRACKPAD.gap + TRACKPAD.h / 2;
465
+ const trackpad = add(
466
+ "laptop-trackpad",
467
+ new THREE.Mesh(
468
+ createFlatRoundedRect(mm(TRACKPAD.w), mm(TRACKPAD.h), mm(TRACKPAD.corner)),
469
+ materials.trackpad,
470
+ ),
471
+ );
472
+ trackpad.position.set(0, mm(0.4), mm(trackpadCenterZ));
473
+
474
+ const notchGeometry = new THREE.CylinderGeometry(
475
+ mm(14),
476
+ mm(14),
477
+ mm(1.4),
478
+ 48,
479
+ 1,
480
+ false,
481
+ Math.PI / 2,
482
+ Math.PI,
483
+ );
484
+ notchGeometry.scale(1, 1, 0.22);
485
+ const thumbNotch = add(
486
+ "laptop-thumb-notch",
487
+ new THREE.Mesh(notchGeometry, materials.hinge),
488
+ );
489
+ thumbNotch.position.set(0, -mm(0.6), mm(BASE.d / 2 + 0.25));
490
+
491
+ // ── Feet ─────────────────────────────────────────────────────────────
492
+ const footGeometry = new THREE.CylinderGeometry(mm(6), mm(6.5), mm(1.3), 32);
493
+ for (const x of [-1, 1]) {
494
+ for (const z of [-1, 1]) {
495
+ const foot = add("laptop-foot", new THREE.Mesh(footGeometry, materials.rubber));
496
+ foot.position.set(
497
+ x * mm(BASE.w / 2 - 24),
498
+ -mm(BASE.t) - mm(0.5),
499
+ z * mm(BASE.d / 2 - 24),
500
+ );
501
+ }
502
+ }
503
+
504
+ // ── Hinge and lid ───────────────────────────────────────────────────
505
+ const hingeZ = -mm(BASE.d / 2) + mm(3.2);
506
+ const hingeY = mm(1.2);
507
+ const hinge = add(
508
+ "laptop-hinge",
509
+ new THREE.Mesh(
510
+ new THREE.CylinderGeometry(mm(3.4), mm(3.4), mm(BASE.w - 72), 32).rotateZ(
511
+ Math.PI / 2,
512
+ ),
513
+ materials.hinge,
514
+ ),
515
+ );
516
+ hinge.position.set(0, -mm(1.8), hingeZ - mm(1.4));
517
+
518
+ const lid = new THREE.Group();
519
+ lid.name = "laptop-lid-pivot";
520
+ lid.position.set(0, hingeY, hingeZ);
521
+ root.add(lid);
522
+
523
+ const lidBevel = mm(0.5);
524
+ const lidGeometry = createRoundedExtrudedGeometry(
525
+ mm(BASE.w),
526
+ mm(LID.h),
527
+ mm(LID.corner),
528
+ mm(LID.t) - lidBevel * 2,
529
+ lidBevel,
530
+ lidBevel,
531
+ );
532
+ const lidShell = add(
533
+ "laptop-lid",
534
+ new THREE.Mesh(lidGeometry, materials.aluminum),
535
+ lid,
536
+ );
537
+ lidShell.position.set(0, mm(LID.h / 2), -mm(LID.t / 2));
538
+
539
+ const glass = add(
540
+ "laptop-display-glass",
541
+ new THREE.Mesh(
542
+ createRoundedPlaneGeometry(
543
+ mm(BASE.w - LID.glassInset * 2),
544
+ mm(LID.h - LID.glassInset * 2),
545
+ mm(LID.corner - LID.glassInset),
546
+ ),
547
+ materials.glass,
548
+ ),
549
+ lid,
550
+ );
551
+ glass.position.set(0, mm(LID.h / 2), mm(0.25));
552
+
553
+ const screenWidth = BASE.w - LID.bezelSide * 2;
554
+ const screenHeight = LID.h - LID.bezelTop - LID.bezelBottom;
555
+ const screen = add(
556
+ "display-surface",
557
+ new THREE.Mesh(
558
+ createRoundedPlaneGeometry(mm(screenWidth), mm(screenHeight), mm(4.2)),
559
+ options.screenMaterial ?? materials.screenOff,
560
+ ),
561
+ lid,
562
+ );
563
+ screen.position.set(0, mm(LID.bezelBottom + screenHeight / 2), mm(0.5));
564
+ screen.castShadow = false;
565
+
566
+ const notch = add(
567
+ "laptop-camera-notch",
568
+ new THREE.Mesh(
569
+ createRoundedPlaneGeometry(mm(LID.notch.w), mm(LID.notch.h + 4), mm(2.6)),
570
+ materials.notch,
571
+ ),
572
+ lid,
573
+ );
574
+ notch.position.set(
575
+ 0,
576
+ mm(LID.bezelBottom + screenHeight - LID.notch.h / 2 + 2),
577
+ mm(0.75),
578
+ );
579
+ const lens = add(
580
+ "laptop-camera-lens",
581
+ new THREE.Mesh(new THREE.CircleGeometry(mm(1.1), 24), materials.lens),
582
+ lid,
583
+ );
584
+ lens.position.set(0, mm(LID.bezelBottom + screenHeight + 2), mm(0.85));
585
+
586
+ const logo = add(
587
+ "laptop-lid-logo",
588
+ new THREE.Mesh(createLogoGeometry(mm(26)), materials.logo),
589
+ lid,
590
+ );
591
+ logo.rotation.y = Math.PI;
592
+ logo.position.set(0, mm(LID.h * 0.53), -mm(LID.t) - mm(0.3));
593
+
594
+ setMacbookLidAngle(root, options.lidAngle ?? DEFAULT_LAPTOP_LID_ANGLE);
595
+ return root;
596
+ }
597
+
598
+ /** Works on clones too, since it looks the hinge up by name. */
599
+ export function setMacbookLidAngle(root: THREE.Object3D, degrees: number) {
600
+ const lid = root.getObjectByName("laptop-lid-pivot");
601
+ if (!lid) return;
602
+ const clamped = THREE.MathUtils.clamp(degrees, 0, 140);
603
+ lid.rotation.x = THREE.MathUtils.degToRad(90 - clamped);
604
+ }
605
+
606
+ export function isMacbookModel(root: THREE.Object3D) {
607
+ return Boolean(root.getObjectByName("laptop-lid-pivot"));
608
+ }