agentcut 0.2.26 → 0.2.27
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +177 -177
- package/dist/studio/agents/claude.js +3 -2
- package/dist/studio/agents/claude.js.map +1 -1
- package/dist/studio/chat.js +1 -1
- package/dist/studio/chat.js.map +1 -1
- package/dist/studio/cli.js +24 -24
- package/dist/studio/download.d.ts +24 -0
- package/dist/studio/download.js +234 -0
- package/dist/studio/download.js.map +1 -0
- package/dist/studio/host.js +8 -0
- package/dist/studio/host.js.map +1 -1
- package/dist/studio/housekeeping.d.ts +2 -2
- package/dist/studio/housekeeping.js +18 -17
- package/dist/studio/housekeeping.js.map +1 -1
- package/dist/studio/release-cursor.js +21 -21
- package/dist/studio/release-video.js +10 -10
- package/dist/studio/tools.js +7 -0
- package/dist/studio/tools.js.map +1 -1
- package/package.json +56 -56
- package/template/AGENTS.md +14 -14
- package/template/components/README.md +70 -70
- package/template/components/before-after/BeforeAfter.tsx +89 -89
- package/template/components/catalog.ts +107 -107
- package/template/components/cinematic/Device3D.tsx +324 -324
- package/template/components/cinematic/devices3d/display-model.ts +184 -184
- package/template/components/cinematic/devices3d/iphone-model.ts +643 -643
- package/template/components/cinematic/devices3d/macbook-model.ts +608 -608
- package/template/components/cinematic/devices3d/models.ts +657 -657
- package/template/components/cinematic/devices3d/runtime.ts +267 -267
- package/template/components/cinematic/math.ts +100 -100
- package/template/components/color/grade-effect.ts +650 -650
- package/template/components/color/grade.ts +512 -512
- package/template/components/color/looks.ts +154 -154
- package/template/components/countdown/Countdown.tsx +166 -166
- package/template/components/dot-trace-title/DotTraceTitle.tsx +181 -181
- package/template/components/dot-trace-title/meta.json +51 -51
- package/template/components/generate/GenerateReveal.tsx +381 -381
- package/template/components/hook-card/HookCard.tsx +554 -554
- package/template/components/index.ts +199 -199
- package/template/components/lib.ts +191 -191
- package/template/components/line-diagram/LineDiagram.tsx +205 -205
- package/template/components/line-diagram/meta.json +215 -215
- package/template/components/list-card/ListCard.tsx +104 -104
- package/template/components/live-clip.ts +75 -75
- package/template/components/looks.tsx +1447 -1447
- package/template/components/loops/LoopCanvas.tsx +132 -132
- package/template/components/loops/engine.ts +184 -184
- package/template/components/loops/library.ts +667 -667
- package/template/components/loops/library2.ts +777 -777
- package/template/components/motion-loop/MotionLoop.tsx +37 -37
- package/template/components/motion-loop/meta.json +311 -311
- package/template/components/props.ts +73 -73
- package/template/components/quote-card/QuoteCard.tsx +71 -71
- package/template/components/style-layer.tsx +233 -233
- package/template/components/styles.ts +113 -113
- package/template/components/types.ts +66 -66
- package/template/components/word-pop/WordPop.tsx +59 -59
- package/template/components/word-stack/WordStack.tsx +122 -122
- package/template/presets/charcoal-sketch/PROMPT.md +48 -48
- package/template/presets/charcoal-sketch/scripts/duck_music.py +34 -34
- package/template/presets/punk-cut-paper/scenes/Motif.tsx +376 -376
- package/template/presets/punk-cut-paper/scenes/Photo.tsx +73 -73
- package/template/presets/punk-cut-paper/scenes/Ransom.tsx +119 -119
- package/template/runtime/composition.tsx +1189 -1189
- package/template/runtime/helpers.tsx +230 -230
- package/template/runtime/index.tsx +22 -22
- package/template/runtime/types.ts +315 -315
- package/template/skill/SKILL.md +186 -187
- package/template/skill/references/broll.md +44 -44
- package/template/skill/references/browser-export.md +61 -61
- package/template/skill/references/cinematic.md +206 -206
- package/template/skill/references/color.md +79 -79
- package/template/skill/references/craft.md +97 -97
- package/template/skill/references/editing-styles.md +57 -57
- package/template/skill/references/feature-launch.md +34 -34
- package/template/skill/references/footage.md +72 -72
- package/template/skill/references/format.md +142 -142
- package/template/skill/references/how-to.md +146 -146
- package/template/skill/references/launch-direction.md +89 -89
- package/template/skill/references/loops.md +151 -151
- package/template/skill/references/mcp-external.md +19 -19
- package/template/skill/references/performance.md +93 -93
- package/template/skill/references/person-cutout.md +19 -19
- package/template/skill/references/presets.md +60 -60
- package/template/skill/references/product-launch.md +134 -134
- package/template/skill/references/release-capture.md +53 -53
- package/template/skill/references/sound.md +167 -166
- package/template/skill/references/style-spy.md +160 -160
- package/template/skill/references/website-howto.md +45 -45
- package/template/starter/.studio/chat.json +1 -1
- package/template/starter/layers.json +46 -46
- package/template/starter/notes.md +4 -4
- package/template/starter/project.json +13 -13
- package/template/starter/scenes/Title.tsx +38 -38
|
@@ -1,267 +1,267 @@
|
|
|
1
|
-
// three.js side of <Device3D>: one renderer per device, lit like ScreenSlick's device stage, drawn
|
|
2
|
-
// through the cinematic Stage's camera. Loaded on demand so pages without 3D devices don't pay for three.
|
|
3
|
-
import * as THREE from "three";
|
|
4
|
-
import { RoomEnvironment } from "three/examples/jsm/environments/RoomEnvironment.js";
|
|
5
|
-
import {
|
|
6
|
-
disposeVideoDeviceModel,
|
|
7
|
-
loadVideoDeviceModel,
|
|
8
|
-
type LoadedVideoDeviceModel,
|
|
9
|
-
type VideoDeviceModelId,
|
|
10
|
-
} from "./models";
|
|
11
|
-
import { setMacbookLidAngle } from "./macbook-model";
|
|
12
|
-
|
|
13
|
-
const SCREEN_VERTEX = /* glsl */ `
|
|
14
|
-
varying vec2 vUv;
|
|
15
|
-
varying vec3 vViewNormal;
|
|
16
|
-
varying vec3 vViewDir;
|
|
17
|
-
void main() {
|
|
18
|
-
vUv = uv;
|
|
19
|
-
vec4 mvPosition = modelViewMatrix * vec4(position, 1.0);
|
|
20
|
-
vViewNormal = normalize(normalMatrix * normal);
|
|
21
|
-
vViewDir = -mvPosition.xyz;
|
|
22
|
-
gl_Position = projectionMatrix * mvPosition;
|
|
23
|
-
}
|
|
24
|
-
`;
|
|
25
|
-
|
|
26
|
-
const SCREEN_FRAGMENT = /* glsl */ `
|
|
27
|
-
uniform sampler2D map;
|
|
28
|
-
uniform mat3 uvTransform;
|
|
29
|
-
uniform float brightness;
|
|
30
|
-
uniform float hasMap;
|
|
31
|
-
uniform vec3 offColor;
|
|
32
|
-
varying vec2 vUv;
|
|
33
|
-
varying vec3 vViewNormal;
|
|
34
|
-
varying vec3 vViewDir;
|
|
35
|
-
void main() {
|
|
36
|
-
vec2 uv = (uvTransform * vec3(vUv, 1.0)).xy;
|
|
37
|
-
float inside = step(0.0, uv.x) * step(uv.x, 1.0) * step(0.0, uv.y) * step(uv.y, 1.0);
|
|
38
|
-
vec3 color = offColor;
|
|
39
|
-
if (hasMap > 0.5) color = texture2D(map, clamp(uv, 0.0, 1.0)).rgb * inside;
|
|
40
|
-
// Real displays go dark edge-on.
|
|
41
|
-
float facing = abs(dot(normalize(vViewNormal), normalize(vViewDir)));
|
|
42
|
-
float grazing = smoothstep(0.06, 0.32, facing);
|
|
43
|
-
gl_FragColor = vec4(mix(offColor, color, brightness * grazing), 1.0);
|
|
44
|
-
#include <colorspace_fragment>
|
|
45
|
-
}
|
|
46
|
-
`;
|
|
47
|
-
|
|
48
|
-
export interface DeviceFrameParams {
|
|
49
|
-
width: number;
|
|
50
|
-
height: number;
|
|
51
|
-
/** Viewer distance of the Stage projection (px). */
|
|
52
|
-
D: number;
|
|
53
|
-
fov: number;
|
|
54
|
-
/** Screen-space shift of the Stage's target (px). */
|
|
55
|
-
shiftX: number;
|
|
56
|
-
shiftY: number;
|
|
57
|
-
/** Stage camera-space placement (row-major 4×4, CSS axes, px): view × model. */
|
|
58
|
-
matrix: number[];
|
|
59
|
-
/** Largest dimension of the device in camera-space px at scale 1. */
|
|
60
|
-
size: number;
|
|
61
|
-
exposure: number;
|
|
62
|
-
lidAngle: number;
|
|
63
|
-
/** Where the screenshot sits when it's taller than the screen: 0 top … 1 bottom. */
|
|
64
|
-
scroll: number;
|
|
65
|
-
pixelRatio: number;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
export interface DeviceRenderer {
|
|
69
|
-
readonly screenAspect: number;
|
|
70
|
-
readonly targetSize: number;
|
|
71
|
-
/**
|
|
72
|
-
* Where a point of the screenshot (fractions, y down) sits on the device, in model units (y up),
|
|
73
|
-
* relative to the device center, for the given scroll and lid angle. Lifts start from these points.
|
|
74
|
-
*/
|
|
75
|
-
imagePoint(x: number, y: number, scroll: number, lidAngle: number): [number, number, number];
|
|
76
|
-
setImage(image: HTMLImageElement | null): void;
|
|
77
|
-
render(params: DeviceFrameParams): void;
|
|
78
|
-
dispose(): void;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
export async function createDeviceRenderer(
|
|
82
|
-
canvas: HTMLCanvasElement,
|
|
83
|
-
id: VideoDeviceModelId,
|
|
84
|
-
finishColor?: string,
|
|
85
|
-
/** A GLB the user brings (in place of the bundled model, or required for external models). */
|
|
86
|
-
modelSrc?: string,
|
|
87
|
-
): Promise<DeviceRenderer> {
|
|
88
|
-
const renderer = new THREE.WebGLRenderer({ canvas, alpha: true, antialias: true, preserveDrawingBuffer: true });
|
|
89
|
-
renderer.setPixelRatio(1);
|
|
90
|
-
renderer.outputColorSpace = THREE.SRGBColorSpace;
|
|
91
|
-
renderer.toneMapping = THREE.ACESFilmicToneMapping;
|
|
92
|
-
renderer.toneMappingExposure = 1.12;
|
|
93
|
-
renderer.setClearColor(0x000000, 0);
|
|
94
|
-
|
|
95
|
-
const scene = new THREE.Scene();
|
|
96
|
-
const pmrem = new THREE.PMREMGenerator(renderer);
|
|
97
|
-
const room = new RoomEnvironment();
|
|
98
|
-
const environment = pmrem.fromScene(room, 0.04).texture;
|
|
99
|
-
room.traverse((object) => {
|
|
100
|
-
if (object instanceof THREE.Mesh) {
|
|
101
|
-
object.geometry.dispose();
|
|
102
|
-
(object.material as THREE.Material).dispose();
|
|
103
|
-
}
|
|
104
|
-
});
|
|
105
|
-
pmrem.dispose();
|
|
106
|
-
scene.environment = environment;
|
|
107
|
-
// Laptops and displays are big flat aluminium and black keys: a softer, darker studio keeps the
|
|
108
|
-
// anodised finish and the keyboard from washing out. Phones keep the brighter rig.
|
|
109
|
-
const studio = id === "procedural-macbook" || id === "macbook-pro-14" || id === "procedural-display";
|
|
110
|
-
scene.environmentIntensity = studio ? 0.6 : 1;
|
|
111
|
-
const baseExposure = studio ? 0.98 : 1.12;
|
|
112
|
-
|
|
113
|
-
const camera = new THREE.PerspectiveCamera(24, 16 / 9, 1, 100000);
|
|
114
|
-
scene.add(camera);
|
|
115
|
-
scene.add(new THREE.HemisphereLight(0xf1f5ff, 0x14161b, studio ? 0.35 : 1.4));
|
|
116
|
-
// Lights ride with the camera so every angle stays product-lit (ScreenSlick's rig).
|
|
117
|
-
for (const [color, intensity, position] of [
|
|
118
|
-
[0xfff4e7, studio ? 3.4 : 5.2, [-2.8, 3.4, 4.4]],
|
|
119
|
-
[0x86aaff, studio ? 2.4 : 3.1, [3.5, 1.5, -3.2]],
|
|
120
|
-
[0xffffff, studio ? 0.6 : 1.4, [1.8, -1.4, 3.2]],
|
|
121
|
-
] as const) {
|
|
122
|
-
const light = new THREE.DirectionalLight(color, intensity);
|
|
123
|
-
light.position.set(position[0], position[1], position[2]);
|
|
124
|
-
camera.add(light);
|
|
125
|
-
camera.add(light.target);
|
|
126
|
-
light.target.position.set(0, 0, -3);
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
const screenMaterial = new THREE.ShaderMaterial({
|
|
130
|
-
uniforms: {
|
|
131
|
-
map: { value: null },
|
|
132
|
-
uvTransform: { value: new THREE.Matrix3() },
|
|
133
|
-
brightness: { value: 1 },
|
|
134
|
-
hasMap: { value: 0 },
|
|
135
|
-
offColor: { value: new THREE.Color(0x07080b) },
|
|
136
|
-
},
|
|
137
|
-
vertexShader: SCREEN_VERTEX,
|
|
138
|
-
fragmentShader: SCREEN_FRAGMENT,
|
|
139
|
-
side: THREE.DoubleSide,
|
|
140
|
-
toneMapped: false,
|
|
141
|
-
});
|
|
142
|
-
|
|
143
|
-
let model: LoadedVideoDeviceModel | null = null;
|
|
144
|
-
try {
|
|
145
|
-
model = await loadVideoDeviceModel(id, screenMaterial, finishColor, modelSrc);
|
|
146
|
-
if (!model) throw new Error(`Couldn't load the ${id} device model.`);
|
|
147
|
-
} catch (error) {
|
|
148
|
-
// Free the GPU side before giving up: a failed load must not keep a WebGL context alive.
|
|
149
|
-
screenMaterial.dispose();
|
|
150
|
-
environment.dispose();
|
|
151
|
-
renderer.dispose();
|
|
152
|
-
renderer.forceContextLoss();
|
|
153
|
-
throw error;
|
|
154
|
-
}
|
|
155
|
-
const holder = new THREE.Group();
|
|
156
|
-
holder.matrixAutoUpdate = false;
|
|
157
|
-
holder.add(model.root);
|
|
158
|
-
scene.add(holder);
|
|
159
|
-
|
|
160
|
-
let texture: THREE.Texture | null = null;
|
|
161
|
-
let imageAspect = model.config.screenAspect;
|
|
162
|
-
let lastScroll = -1;
|
|
163
|
-
|
|
164
|
-
// Cover the screen; a taller screenshot shows its top (or scrolls), a wider one is centered.
|
|
165
|
-
// Screen uv → image uv: (tx + sx·u, ty + sy·v).
|
|
166
|
-
const uvFit = (scroll: number) => {
|
|
167
|
-
const screenAspect = model.config.screenAspect;
|
|
168
|
-
let repeatX = 1;
|
|
169
|
-
let repeatY = 1;
|
|
170
|
-
if (imageAspect > screenAspect) repeatX = screenAspect / imageAspect;
|
|
171
|
-
else repeatY = imageAspect / screenAspect;
|
|
172
|
-
const offsetX = (1 - repeatX) / 2;
|
|
173
|
-
const s = Math.min(1, Math.max(0, scroll));
|
|
174
|
-
// flipY puts the image's top at v = 1; without it (glTF UVs) the top is at v = 0.
|
|
175
|
-
const offsetY = (1 - repeatY) * (model.config.textureFlipY ? 1 - s : s);
|
|
176
|
-
const mirror = model.config.textureMirrorX;
|
|
177
|
-
return { tx: mirror ? 1 - offsetX : offsetX, ty: offsetY, sx: mirror ? -repeatX : repeatX, sy: repeatY };
|
|
178
|
-
};
|
|
179
|
-
|
|
180
|
-
const fitTexture = (scroll: number) => {
|
|
181
|
-
const f = uvFit(scroll);
|
|
182
|
-
(screenMaterial.uniforms.uvTransform.value as THREE.Matrix3).setUvTransform(f.tx, f.ty, f.sx, f.sy, 0, 0, 0);
|
|
183
|
-
lastScroll = scroll;
|
|
184
|
-
};
|
|
185
|
-
|
|
186
|
-
const relative = new THREE.Matrix4();
|
|
187
|
-
const point = new THREE.Vector3();
|
|
188
|
-
|
|
189
|
-
// CSS camera space (y down) ↔ three (y up): conjugate by a y flip.
|
|
190
|
-
const flip = new THREE.Matrix4().makeScale(1, -1, 1);
|
|
191
|
-
const placement = new THREE.Matrix4();
|
|
192
|
-
const scale = new THREE.Matrix4();
|
|
193
|
-
|
|
194
|
-
return {
|
|
195
|
-
screenAspect: model.config.screenAspect,
|
|
196
|
-
targetSize: model.config.targetSize,
|
|
197
|
-
imagePoint(x, y, scroll, lidAngle) {
|
|
198
|
-
if (id === "procedural-macbook") setMacbookLidAngle(model.root, lidAngle);
|
|
199
|
-
model.root.updateMatrixWorld(true);
|
|
200
|
-
holder.updateMatrixWorld(true);
|
|
201
|
-
relative.copy(holder.matrixWorld).invert().multiply(model.screen.matrixWorld);
|
|
202
|
-
const f = uvFit(scroll);
|
|
203
|
-
// With flipY image v runs up (the top of the screenshot is v = 1); without it, v runs down.
|
|
204
|
-
const u = (x - f.tx) / f.sx;
|
|
205
|
-
const v = ((model.config.textureFlipY ? 1 - y : y) - f.ty) / f.sy;
|
|
206
|
-
const frame = model.screenFrame;
|
|
207
|
-
point.copy(frame.origin).addScaledVector(frame.du, u).addScaledVector(frame.dv, v).applyMatrix4(relative);
|
|
208
|
-
return [point.x, point.y, point.z];
|
|
209
|
-
},
|
|
210
|
-
setImage(image) {
|
|
211
|
-
texture?.dispose();
|
|
212
|
-
texture = null;
|
|
213
|
-
if (!image) {
|
|
214
|
-
screenMaterial.uniforms.map.value = null;
|
|
215
|
-
screenMaterial.uniforms.hasMap.value = 0;
|
|
216
|
-
return;
|
|
217
|
-
}
|
|
218
|
-
texture = new THREE.Texture(image);
|
|
219
|
-
texture.colorSpace = THREE.SRGBColorSpace;
|
|
220
|
-
texture.flipY = model.config.textureFlipY;
|
|
221
|
-
texture.anisotropy = renderer.capabilities.getMaxAnisotropy();
|
|
222
|
-
texture.needsUpdate = true;
|
|
223
|
-
imageAspect = image.naturalWidth / Math.max(1, image.naturalHeight);
|
|
224
|
-
screenMaterial.uniforms.map.value = texture;
|
|
225
|
-
screenMaterial.uniforms.hasMap.value = 1;
|
|
226
|
-
lastScroll = -1;
|
|
227
|
-
},
|
|
228
|
-
render(p) {
|
|
229
|
-
if (canvas.width !== Math.round(p.width * p.pixelRatio) || canvas.height !== Math.round(p.height * p.pixelRatio)) {
|
|
230
|
-
renderer.setPixelRatio(p.pixelRatio);
|
|
231
|
-
renderer.setSize(p.width, p.height, false);
|
|
232
|
-
}
|
|
233
|
-
renderer.toneMappingExposure = baseExposure * p.exposure;
|
|
234
|
-
if (p.scroll !== lastScroll) fitTexture(p.scroll);
|
|
235
|
-
if (id === "procedural-macbook") setMacbookLidAngle(model.root, p.lidAngle);
|
|
236
|
-
|
|
237
|
-
camera.fov = p.fov;
|
|
238
|
-
camera.aspect = p.width / p.height;
|
|
239
|
-
camera.near = Math.max(1, p.D * 0.02);
|
|
240
|
-
camera.far = p.D * 40;
|
|
241
|
-
camera.position.set(0, 0, p.D);
|
|
242
|
-
camera.lookAt(0, 0, 0);
|
|
243
|
-
camera.setViewOffset(p.width, p.height, -p.shiftX, -p.shiftY, p.width, p.height);
|
|
244
|
-
camera.updateProjectionMatrix();
|
|
245
|
-
|
|
246
|
-
// Row-major (CSS) → three's column-major set().
|
|
247
|
-
const m = p.matrix;
|
|
248
|
-
placement.set(m[0], m[1], m[2], m[3], m[4], m[5], m[6], m[7], m[8], m[9], m[10], m[11], m[12], m[13], m[14], m[15]);
|
|
249
|
-
const k = p.size / model.config.targetSize;
|
|
250
|
-
scale.makeScale(k, k, k);
|
|
251
|
-
holder.matrix.copy(flip).multiply(placement).multiply(flip).multiply(scale);
|
|
252
|
-
holder.matrixWorldNeedsUpdate = true;
|
|
253
|
-
renderer.render(scene, camera);
|
|
254
|
-
},
|
|
255
|
-
dispose() {
|
|
256
|
-
texture?.dispose();
|
|
257
|
-
disposeVideoDeviceModel(model);
|
|
258
|
-
screenMaterial.dispose();
|
|
259
|
-
environment.dispose();
|
|
260
|
-
renderer.dispose();
|
|
261
|
-
// Browsers keep only ~16 WebGL contexts; release this one now instead of at garbage collection.
|
|
262
|
-
renderer.forceContextLoss();
|
|
263
|
-
},
|
|
264
|
-
};
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
export type { VideoDeviceModelId };
|
|
1
|
+
// three.js side of <Device3D>: one renderer per device, lit like ScreenSlick's device stage, drawn
|
|
2
|
+
// through the cinematic Stage's camera. Loaded on demand so pages without 3D devices don't pay for three.
|
|
3
|
+
import * as THREE from "three";
|
|
4
|
+
import { RoomEnvironment } from "three/examples/jsm/environments/RoomEnvironment.js";
|
|
5
|
+
import {
|
|
6
|
+
disposeVideoDeviceModel,
|
|
7
|
+
loadVideoDeviceModel,
|
|
8
|
+
type LoadedVideoDeviceModel,
|
|
9
|
+
type VideoDeviceModelId,
|
|
10
|
+
} from "./models";
|
|
11
|
+
import { setMacbookLidAngle } from "./macbook-model";
|
|
12
|
+
|
|
13
|
+
const SCREEN_VERTEX = /* glsl */ `
|
|
14
|
+
varying vec2 vUv;
|
|
15
|
+
varying vec3 vViewNormal;
|
|
16
|
+
varying vec3 vViewDir;
|
|
17
|
+
void main() {
|
|
18
|
+
vUv = uv;
|
|
19
|
+
vec4 mvPosition = modelViewMatrix * vec4(position, 1.0);
|
|
20
|
+
vViewNormal = normalize(normalMatrix * normal);
|
|
21
|
+
vViewDir = -mvPosition.xyz;
|
|
22
|
+
gl_Position = projectionMatrix * mvPosition;
|
|
23
|
+
}
|
|
24
|
+
`;
|
|
25
|
+
|
|
26
|
+
const SCREEN_FRAGMENT = /* glsl */ `
|
|
27
|
+
uniform sampler2D map;
|
|
28
|
+
uniform mat3 uvTransform;
|
|
29
|
+
uniform float brightness;
|
|
30
|
+
uniform float hasMap;
|
|
31
|
+
uniform vec3 offColor;
|
|
32
|
+
varying vec2 vUv;
|
|
33
|
+
varying vec3 vViewNormal;
|
|
34
|
+
varying vec3 vViewDir;
|
|
35
|
+
void main() {
|
|
36
|
+
vec2 uv = (uvTransform * vec3(vUv, 1.0)).xy;
|
|
37
|
+
float inside = step(0.0, uv.x) * step(uv.x, 1.0) * step(0.0, uv.y) * step(uv.y, 1.0);
|
|
38
|
+
vec3 color = offColor;
|
|
39
|
+
if (hasMap > 0.5) color = texture2D(map, clamp(uv, 0.0, 1.0)).rgb * inside;
|
|
40
|
+
// Real displays go dark edge-on.
|
|
41
|
+
float facing = abs(dot(normalize(vViewNormal), normalize(vViewDir)));
|
|
42
|
+
float grazing = smoothstep(0.06, 0.32, facing);
|
|
43
|
+
gl_FragColor = vec4(mix(offColor, color, brightness * grazing), 1.0);
|
|
44
|
+
#include <colorspace_fragment>
|
|
45
|
+
}
|
|
46
|
+
`;
|
|
47
|
+
|
|
48
|
+
export interface DeviceFrameParams {
|
|
49
|
+
width: number;
|
|
50
|
+
height: number;
|
|
51
|
+
/** Viewer distance of the Stage projection (px). */
|
|
52
|
+
D: number;
|
|
53
|
+
fov: number;
|
|
54
|
+
/** Screen-space shift of the Stage's target (px). */
|
|
55
|
+
shiftX: number;
|
|
56
|
+
shiftY: number;
|
|
57
|
+
/** Stage camera-space placement (row-major 4×4, CSS axes, px): view × model. */
|
|
58
|
+
matrix: number[];
|
|
59
|
+
/** Largest dimension of the device in camera-space px at scale 1. */
|
|
60
|
+
size: number;
|
|
61
|
+
exposure: number;
|
|
62
|
+
lidAngle: number;
|
|
63
|
+
/** Where the screenshot sits when it's taller than the screen: 0 top … 1 bottom. */
|
|
64
|
+
scroll: number;
|
|
65
|
+
pixelRatio: number;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export interface DeviceRenderer {
|
|
69
|
+
readonly screenAspect: number;
|
|
70
|
+
readonly targetSize: number;
|
|
71
|
+
/**
|
|
72
|
+
* Where a point of the screenshot (fractions, y down) sits on the device, in model units (y up),
|
|
73
|
+
* relative to the device center, for the given scroll and lid angle. Lifts start from these points.
|
|
74
|
+
*/
|
|
75
|
+
imagePoint(x: number, y: number, scroll: number, lidAngle: number): [number, number, number];
|
|
76
|
+
setImage(image: HTMLImageElement | null): void;
|
|
77
|
+
render(params: DeviceFrameParams): void;
|
|
78
|
+
dispose(): void;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export async function createDeviceRenderer(
|
|
82
|
+
canvas: HTMLCanvasElement,
|
|
83
|
+
id: VideoDeviceModelId,
|
|
84
|
+
finishColor?: string,
|
|
85
|
+
/** A GLB the user brings (in place of the bundled model, or required for external models). */
|
|
86
|
+
modelSrc?: string,
|
|
87
|
+
): Promise<DeviceRenderer> {
|
|
88
|
+
const renderer = new THREE.WebGLRenderer({ canvas, alpha: true, antialias: true, preserveDrawingBuffer: true });
|
|
89
|
+
renderer.setPixelRatio(1);
|
|
90
|
+
renderer.outputColorSpace = THREE.SRGBColorSpace;
|
|
91
|
+
renderer.toneMapping = THREE.ACESFilmicToneMapping;
|
|
92
|
+
renderer.toneMappingExposure = 1.12;
|
|
93
|
+
renderer.setClearColor(0x000000, 0);
|
|
94
|
+
|
|
95
|
+
const scene = new THREE.Scene();
|
|
96
|
+
const pmrem = new THREE.PMREMGenerator(renderer);
|
|
97
|
+
const room = new RoomEnvironment();
|
|
98
|
+
const environment = pmrem.fromScene(room, 0.04).texture;
|
|
99
|
+
room.traverse((object) => {
|
|
100
|
+
if (object instanceof THREE.Mesh) {
|
|
101
|
+
object.geometry.dispose();
|
|
102
|
+
(object.material as THREE.Material).dispose();
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
pmrem.dispose();
|
|
106
|
+
scene.environment = environment;
|
|
107
|
+
// Laptops and displays are big flat aluminium and black keys: a softer, darker studio keeps the
|
|
108
|
+
// anodised finish and the keyboard from washing out. Phones keep the brighter rig.
|
|
109
|
+
const studio = id === "procedural-macbook" || id === "macbook-pro-14" || id === "procedural-display";
|
|
110
|
+
scene.environmentIntensity = studio ? 0.6 : 1;
|
|
111
|
+
const baseExposure = studio ? 0.98 : 1.12;
|
|
112
|
+
|
|
113
|
+
const camera = new THREE.PerspectiveCamera(24, 16 / 9, 1, 100000);
|
|
114
|
+
scene.add(camera);
|
|
115
|
+
scene.add(new THREE.HemisphereLight(0xf1f5ff, 0x14161b, studio ? 0.35 : 1.4));
|
|
116
|
+
// Lights ride with the camera so every angle stays product-lit (ScreenSlick's rig).
|
|
117
|
+
for (const [color, intensity, position] of [
|
|
118
|
+
[0xfff4e7, studio ? 3.4 : 5.2, [-2.8, 3.4, 4.4]],
|
|
119
|
+
[0x86aaff, studio ? 2.4 : 3.1, [3.5, 1.5, -3.2]],
|
|
120
|
+
[0xffffff, studio ? 0.6 : 1.4, [1.8, -1.4, 3.2]],
|
|
121
|
+
] as const) {
|
|
122
|
+
const light = new THREE.DirectionalLight(color, intensity);
|
|
123
|
+
light.position.set(position[0], position[1], position[2]);
|
|
124
|
+
camera.add(light);
|
|
125
|
+
camera.add(light.target);
|
|
126
|
+
light.target.position.set(0, 0, -3);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const screenMaterial = new THREE.ShaderMaterial({
|
|
130
|
+
uniforms: {
|
|
131
|
+
map: { value: null },
|
|
132
|
+
uvTransform: { value: new THREE.Matrix3() },
|
|
133
|
+
brightness: { value: 1 },
|
|
134
|
+
hasMap: { value: 0 },
|
|
135
|
+
offColor: { value: new THREE.Color(0x07080b) },
|
|
136
|
+
},
|
|
137
|
+
vertexShader: SCREEN_VERTEX,
|
|
138
|
+
fragmentShader: SCREEN_FRAGMENT,
|
|
139
|
+
side: THREE.DoubleSide,
|
|
140
|
+
toneMapped: false,
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
let model: LoadedVideoDeviceModel | null = null;
|
|
144
|
+
try {
|
|
145
|
+
model = await loadVideoDeviceModel(id, screenMaterial, finishColor, modelSrc);
|
|
146
|
+
if (!model) throw new Error(`Couldn't load the ${id} device model.`);
|
|
147
|
+
} catch (error) {
|
|
148
|
+
// Free the GPU side before giving up: a failed load must not keep a WebGL context alive.
|
|
149
|
+
screenMaterial.dispose();
|
|
150
|
+
environment.dispose();
|
|
151
|
+
renderer.dispose();
|
|
152
|
+
renderer.forceContextLoss();
|
|
153
|
+
throw error;
|
|
154
|
+
}
|
|
155
|
+
const holder = new THREE.Group();
|
|
156
|
+
holder.matrixAutoUpdate = false;
|
|
157
|
+
holder.add(model.root);
|
|
158
|
+
scene.add(holder);
|
|
159
|
+
|
|
160
|
+
let texture: THREE.Texture | null = null;
|
|
161
|
+
let imageAspect = model.config.screenAspect;
|
|
162
|
+
let lastScroll = -1;
|
|
163
|
+
|
|
164
|
+
// Cover the screen; a taller screenshot shows its top (or scrolls), a wider one is centered.
|
|
165
|
+
// Screen uv → image uv: (tx + sx·u, ty + sy·v).
|
|
166
|
+
const uvFit = (scroll: number) => {
|
|
167
|
+
const screenAspect = model.config.screenAspect;
|
|
168
|
+
let repeatX = 1;
|
|
169
|
+
let repeatY = 1;
|
|
170
|
+
if (imageAspect > screenAspect) repeatX = screenAspect / imageAspect;
|
|
171
|
+
else repeatY = imageAspect / screenAspect;
|
|
172
|
+
const offsetX = (1 - repeatX) / 2;
|
|
173
|
+
const s = Math.min(1, Math.max(0, scroll));
|
|
174
|
+
// flipY puts the image's top at v = 1; without it (glTF UVs) the top is at v = 0.
|
|
175
|
+
const offsetY = (1 - repeatY) * (model.config.textureFlipY ? 1 - s : s);
|
|
176
|
+
const mirror = model.config.textureMirrorX;
|
|
177
|
+
return { tx: mirror ? 1 - offsetX : offsetX, ty: offsetY, sx: mirror ? -repeatX : repeatX, sy: repeatY };
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
const fitTexture = (scroll: number) => {
|
|
181
|
+
const f = uvFit(scroll);
|
|
182
|
+
(screenMaterial.uniforms.uvTransform.value as THREE.Matrix3).setUvTransform(f.tx, f.ty, f.sx, f.sy, 0, 0, 0);
|
|
183
|
+
lastScroll = scroll;
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
const relative = new THREE.Matrix4();
|
|
187
|
+
const point = new THREE.Vector3();
|
|
188
|
+
|
|
189
|
+
// CSS camera space (y down) ↔ three (y up): conjugate by a y flip.
|
|
190
|
+
const flip = new THREE.Matrix4().makeScale(1, -1, 1);
|
|
191
|
+
const placement = new THREE.Matrix4();
|
|
192
|
+
const scale = new THREE.Matrix4();
|
|
193
|
+
|
|
194
|
+
return {
|
|
195
|
+
screenAspect: model.config.screenAspect,
|
|
196
|
+
targetSize: model.config.targetSize,
|
|
197
|
+
imagePoint(x, y, scroll, lidAngle) {
|
|
198
|
+
if (id === "procedural-macbook") setMacbookLidAngle(model.root, lidAngle);
|
|
199
|
+
model.root.updateMatrixWorld(true);
|
|
200
|
+
holder.updateMatrixWorld(true);
|
|
201
|
+
relative.copy(holder.matrixWorld).invert().multiply(model.screen.matrixWorld);
|
|
202
|
+
const f = uvFit(scroll);
|
|
203
|
+
// With flipY image v runs up (the top of the screenshot is v = 1); without it, v runs down.
|
|
204
|
+
const u = (x - f.tx) / f.sx;
|
|
205
|
+
const v = ((model.config.textureFlipY ? 1 - y : y) - f.ty) / f.sy;
|
|
206
|
+
const frame = model.screenFrame;
|
|
207
|
+
point.copy(frame.origin).addScaledVector(frame.du, u).addScaledVector(frame.dv, v).applyMatrix4(relative);
|
|
208
|
+
return [point.x, point.y, point.z];
|
|
209
|
+
},
|
|
210
|
+
setImage(image) {
|
|
211
|
+
texture?.dispose();
|
|
212
|
+
texture = null;
|
|
213
|
+
if (!image) {
|
|
214
|
+
screenMaterial.uniforms.map.value = null;
|
|
215
|
+
screenMaterial.uniforms.hasMap.value = 0;
|
|
216
|
+
return;
|
|
217
|
+
}
|
|
218
|
+
texture = new THREE.Texture(image);
|
|
219
|
+
texture.colorSpace = THREE.SRGBColorSpace;
|
|
220
|
+
texture.flipY = model.config.textureFlipY;
|
|
221
|
+
texture.anisotropy = renderer.capabilities.getMaxAnisotropy();
|
|
222
|
+
texture.needsUpdate = true;
|
|
223
|
+
imageAspect = image.naturalWidth / Math.max(1, image.naturalHeight);
|
|
224
|
+
screenMaterial.uniforms.map.value = texture;
|
|
225
|
+
screenMaterial.uniforms.hasMap.value = 1;
|
|
226
|
+
lastScroll = -1;
|
|
227
|
+
},
|
|
228
|
+
render(p) {
|
|
229
|
+
if (canvas.width !== Math.round(p.width * p.pixelRatio) || canvas.height !== Math.round(p.height * p.pixelRatio)) {
|
|
230
|
+
renderer.setPixelRatio(p.pixelRatio);
|
|
231
|
+
renderer.setSize(p.width, p.height, false);
|
|
232
|
+
}
|
|
233
|
+
renderer.toneMappingExposure = baseExposure * p.exposure;
|
|
234
|
+
if (p.scroll !== lastScroll) fitTexture(p.scroll);
|
|
235
|
+
if (id === "procedural-macbook") setMacbookLidAngle(model.root, p.lidAngle);
|
|
236
|
+
|
|
237
|
+
camera.fov = p.fov;
|
|
238
|
+
camera.aspect = p.width / p.height;
|
|
239
|
+
camera.near = Math.max(1, p.D * 0.02);
|
|
240
|
+
camera.far = p.D * 40;
|
|
241
|
+
camera.position.set(0, 0, p.D);
|
|
242
|
+
camera.lookAt(0, 0, 0);
|
|
243
|
+
camera.setViewOffset(p.width, p.height, -p.shiftX, -p.shiftY, p.width, p.height);
|
|
244
|
+
camera.updateProjectionMatrix();
|
|
245
|
+
|
|
246
|
+
// Row-major (CSS) → three's column-major set().
|
|
247
|
+
const m = p.matrix;
|
|
248
|
+
placement.set(m[0], m[1], m[2], m[3], m[4], m[5], m[6], m[7], m[8], m[9], m[10], m[11], m[12], m[13], m[14], m[15]);
|
|
249
|
+
const k = p.size / model.config.targetSize;
|
|
250
|
+
scale.makeScale(k, k, k);
|
|
251
|
+
holder.matrix.copy(flip).multiply(placement).multiply(flip).multiply(scale);
|
|
252
|
+
holder.matrixWorldNeedsUpdate = true;
|
|
253
|
+
renderer.render(scene, camera);
|
|
254
|
+
},
|
|
255
|
+
dispose() {
|
|
256
|
+
texture?.dispose();
|
|
257
|
+
disposeVideoDeviceModel(model);
|
|
258
|
+
screenMaterial.dispose();
|
|
259
|
+
environment.dispose();
|
|
260
|
+
renderer.dispose();
|
|
261
|
+
// Browsers keep only ~16 WebGL contexts; release this one now instead of at garbage collection.
|
|
262
|
+
renderer.forceContextLoss();
|
|
263
|
+
},
|
|
264
|
+
};
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
export type { VideoDeviceModelId };
|