agentcut 0.2.5 → 0.2.7
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/package.json +1 -1
- package/template/components/cinematic/Cinematic.tsx +1177 -1139
- package/template/components/cinematic/Device3D.tsx +35 -10
- package/template/components/cinematic/devices3d/macbook-model.ts +78 -23
- package/template/components/cinematic/devices3d/models.ts +75 -30
- package/template/components/cinematic/devices3d/runtime.ts +18 -9
- package/template/skill/references/cinematic.md +40 -5
|
@@ -1,1139 +1,1177 @@
|
|
|
1
|
-
// Cinematic toolkit: a real 3D camera for product shots (screens, devices and UI pieces in depth,
|
|
2
|
-
// with lens blur, glare, shadows and reflections). Scenes import it from "agentcut/components".
|
|
3
|
-
//
|
|
4
|
-
// Every plane is one element with a full matrix3d(): the browser preview draws it natively and the
|
|
5
|
-
// browser export draws it through WebGL, so the two match. Never put CSS `perspective` or
|
|
6
|
-
// `preserve-3d` around it: the stage already projects every plane itself.
|
|
7
|
-
import { Video } from "@remotion/media";
|
|
8
|
-
import { createContext, useContext, useId, type ComponentProps, type CSSProperties, type ReactNode } from "react";
|
|
9
|
-
import { AbsoluteFill, Img, interpolate, useCurrentFrame, useRemotionEnvironment, useVideoConfig } from "remotion";
|
|
10
|
-
import { alpha, glowStyle } from "../lib";
|
|
11
|
-
import {
|
|
12
|
-
apply,
|
|
13
|
-
EASES,
|
|
14
|
-
identity,
|
|
15
|
-
mul,
|
|
16
|
-
perspective,
|
|
17
|
-
placementMatrix,
|
|
18
|
-
rotateX,
|
|
19
|
-
rotateY,
|
|
20
|
-
rotateZ,
|
|
21
|
-
scale,
|
|
22
|
-
toCss,
|
|
23
|
-
translate,
|
|
24
|
-
type EaseName,
|
|
25
|
-
type M4,
|
|
26
|
-
type Placement,
|
|
27
|
-
} from "./math";
|
|
28
|
-
|
|
29
|
-
export type { EaseName, M4, Placement };
|
|
30
|
-
|
|
31
|
-
// ─── Camera ──────────────────────────────────────────────────────────────────────────────────────
|
|
32
|
-
|
|
33
|
-
export interface CameraPose {
|
|
34
|
-
/** The point the camera looks at, in world px (0,0,0 = frame center; y down, z toward the viewer). */
|
|
35
|
-
x?: number;
|
|
36
|
-
y?: number;
|
|
37
|
-
z?: number;
|
|
38
|
-
/** 1: things at z = 0 show at their size. 2: twice as close. 0.6: pulled back. */
|
|
39
|
-
zoom?: number;
|
|
40
|
-
/** Degrees. tilt > 0: the camera rises and looks down (floors and laptop decks show their top).
|
|
41
|
-
* turn > 0: the camera orbits to the left, so the right side recedes. roll > 0: the frame rotates
|
|
42
|
-
* clockwise. To lay a screen back like a laptop lid, tilt the Screen/Group itself (tilt 25–55). */
|
|
43
|
-
tilt?: number;
|
|
44
|
-
turn?: number;
|
|
45
|
-
roll?: number;
|
|
46
|
-
/** Vertical field of view in degrees: 15–25 flat telephoto (product), 35 natural, 50–70 dramatic. */
|
|
47
|
-
fov?: number;
|
|
48
|
-
/** What's sharp: a world point [x, y, z], or a distance in px toward the camera from the target (0 = target). */
|
|
49
|
-
focus?: [number, number, number] | number;
|
|
50
|
-
/** Lens blur away from the focus: 0 none, 0.4 subtle, 1 strong, 2 macro. */
|
|
51
|
-
aperture?: number;
|
|
52
|
-
/** Moves the target off-center on screen, in fractions of the frame (-0.5…0.5): room for a title. */
|
|
53
|
-
shiftX?: number;
|
|
54
|
-
shiftY?: number;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
export interface CameraKey extends CameraPose {
|
|
58
|
-
/** Frame (local to the clip) where the camera reaches this pose. */
|
|
59
|
-
at: number;
|
|
60
|
-
/** Easing of the move INTO this key. */
|
|
61
|
-
ease?: EaseName;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
const POSE_DEFAULTS: Required<Omit<CameraPose, "focus">> & { focus: CameraPose["focus"] } = {
|
|
65
|
-
x: 0,
|
|
66
|
-
y: 0,
|
|
67
|
-
z: 0,
|
|
68
|
-
zoom: 1,
|
|
69
|
-
tilt: 0,
|
|
70
|
-
turn: 0,
|
|
71
|
-
roll: 0,
|
|
72
|
-
fov: 24,
|
|
73
|
-
focus: 0,
|
|
74
|
-
aperture: 0,
|
|
75
|
-
shiftX: 0,
|
|
76
|
-
shiftY: 0,
|
|
77
|
-
};
|
|
78
|
-
|
|
79
|
-
type FullPose = typeof POSE_DEFAULTS;
|
|
80
|
-
const NUMERIC = ["x", "y", "z", "zoom", "tilt", "turn", "roll", "fov", "aperture", "shiftX", "shiftY"] as const;
|
|
81
|
-
|
|
82
|
-
/** The camera pose at `frame`: keys carry their values forward, each move eases into its key. */
|
|
83
|
-
export function cameraAt(frame: number, camera: CameraPose | CameraKey[]): FullPose {
|
|
84
|
-
if (!Array.isArray(camera)) return { ...POSE_DEFAULTS, ...stripUndefined(camera) };
|
|
85
|
-
const keys = [...camera].sort((a, b) => a.at - b.at);
|
|
86
|
-
if (keys.length === 0) return { ...POSE_DEFAULTS };
|
|
87
|
-
// Fill every key with the values before it.
|
|
88
|
-
const full: (FullPose & { at: number; ease?: EaseName })[] = [];
|
|
89
|
-
let prev: FullPose = { ...POSE_DEFAULTS };
|
|
90
|
-
for (const k of keys) {
|
|
91
|
-
const next = { ...prev, ...stripUndefined(k), at: k.at, ease: k.ease } as FullPose & { at: number; ease?: EaseName };
|
|
92
|
-
full.push(next);
|
|
93
|
-
prev = next;
|
|
94
|
-
}
|
|
95
|
-
if (frame <= full[0].at) return full[0];
|
|
96
|
-
const last = full[full.length - 1];
|
|
97
|
-
if (frame >= last.at) return last;
|
|
98
|
-
let i = 0;
|
|
99
|
-
while (full[i + 1].at < frame) i++;
|
|
100
|
-
const a = full[i];
|
|
101
|
-
const b = full[i + 1];
|
|
102
|
-
const t = EASES[b.ease ?? "inOut"]((frame - a.at) / Math.max(1e-6, b.at - a.at));
|
|
103
|
-
const out = { ...b } as FullPose;
|
|
104
|
-
for (const key of NUMERIC) out[key] = a[key] + (b[key] - a[key]) * t;
|
|
105
|
-
out.focus = mixFocus(a.focus, b.focus, t);
|
|
106
|
-
return out;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
function stripUndefined<T extends object>(o: T): Partial<T> {
|
|
110
|
-
return Object.fromEntries(Object.entries(o).filter(([, v]) => v !== undefined)) as Partial<T>;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
function mixFocus(a: CameraPose["focus"], b: CameraPose["focus"], t: number): CameraPose["focus"] {
|
|
114
|
-
if (Array.isArray(a) && Array.isArray(b)) return [0, 1, 2].map((i) => a[i] + (b[i] - a[i]) * t) as [number, number, number];
|
|
115
|
-
if (typeof a === "number" && typeof b === "number") return a + (b - a) * t;
|
|
116
|
-
return t < 0.5 ? a : b;
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
/** Slow, smooth drift that makes a locked-off shot feel filmed (deterministic). */
|
|
120
|
-
function floatOffset(frame: number, fps: number, amount: number, seed: number) {
|
|
121
|
-
if (!amount) return { x: 0, y: 0, tilt: 0, turn: 0, roll: 0 };
|
|
122
|
-
const t = frame / fps + seed * 7.31;
|
|
123
|
-
const w = (a: number, b: number) => Math.sin(t * a + seed * b) * 0.6 + Math.sin(t * a * 2.3 + seed * b * 1.7) * 0.4;
|
|
124
|
-
return {
|
|
125
|
-
x: w(0.31, 1.1) * 10 * amount,
|
|
126
|
-
y: w(0.23, 2.3) * 7 * amount,
|
|
127
|
-
tilt: w(0.19, 3.7) * 0.5 * amount,
|
|
128
|
-
turn: w(0.27, 4.1) * 0.6 * amount,
|
|
129
|
-
roll: w(0.17, 5.3) * 0.25 * amount,
|
|
130
|
-
};
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
// ─── Stage context ───────────────────────────────────────────────────────────────────────────────
|
|
134
|
-
|
|
135
|
-
interface StageContext {
|
|
136
|
-
width: number;
|
|
137
|
-
height: number;
|
|
138
|
-
/** World px → screen px at zoom 1 (1 at 1080 px on the short side). */
|
|
139
|
-
u: number;
|
|
140
|
-
/** Viewer distance of the projection (px). */
|
|
141
|
-
D: number;
|
|
142
|
-
/** World → camera space. */
|
|
143
|
-
view: M4;
|
|
144
|
-
/** Camera space → screen (perspective + centering). */
|
|
145
|
-
project: M4;
|
|
146
|
-
focusDist: number;
|
|
147
|
-
aperture: number;
|
|
148
|
-
/** Lens and target shift, for renderers that draw through the same camera (Device3D). */
|
|
149
|
-
fov: number;
|
|
150
|
-
shiftX: number;
|
|
151
|
-
shiftY: number;
|
|
152
|
-
/** Parent placement (groups). */
|
|
153
|
-
model: M4;
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
const StageCtx = createContext<StageContext | null>(null);
|
|
157
|
-
|
|
158
|
-
export function useStageContext(component: string) {
|
|
159
|
-
const ctx = useContext(StageCtx);
|
|
160
|
-
if (!ctx) throw new Error(`<${component}> must be inside a <Stage>.`);
|
|
161
|
-
return ctx;
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
/** Frame size, world unit and orientation, for sizing things in both variants. */
|
|
165
|
-
export function useStage() {
|
|
166
|
-
const { width, height } = useVideoConfig();
|
|
167
|
-
const u = Math.min(width, height) / 1080;
|
|
168
|
-
return { width, height, u, vertical: height > width, worldWidth: width / u, worldHeight: height / u };
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
export interface StageProps {
|
|
172
|
-
/** One pose, or keyframes the camera moves through. */
|
|
173
|
-
camera: CameraPose | CameraKey[];
|
|
174
|
-
/** Handheld drift, 0 locked off, 0.5 gentle, 1 floating. */
|
|
175
|
-
float?: number;
|
|
176
|
-
/** Blur while the camera moves fast (whips, snaps): 0 off, 1 natural. */
|
|
177
|
-
motionBlur?: number;
|
|
178
|
-
seed?: number;
|
|
179
|
-
/** Background behind the planes (a color, a linear-gradient, or a <Backdrop> as the first child). */
|
|
180
|
-
background?: string;
|
|
181
|
-
children?: ReactNode;
|
|
182
|
-
style?: CSSProperties;
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
function buildCamera(pose: FullPose, width: number, height: number) {
|
|
186
|
-
const u = Math.min(width, height) / 1080;
|
|
187
|
-
const fov = Math.min(110, Math.max(4, pose.fov));
|
|
188
|
-
const D = height / 2 / Math.tan((fov * Math.PI) / 360);
|
|
189
|
-
const d = D / Math.max(0.05, pose.zoom);
|
|
190
|
-
const view = mul(
|
|
191
|
-
translate(0, 0, D - d),
|
|
192
|
-
rotateZ(pose.roll),
|
|
193
|
-
// Camera above looking down (tilt > 0) = the world turns its top toward the lens.
|
|
194
|
-
rotateX(-pose.tilt),
|
|
195
|
-
rotateY(pose.turn),
|
|
196
|
-
scale(u),
|
|
197
|
-
translate(-pose.x, -pose.y, -pose.z),
|
|
198
|
-
);
|
|
199
|
-
const project = mul(translate(width / 2 + pose.shiftX * width, height / 2 + pose.shiftY * height, 0), perspective(D));
|
|
200
|
-
let focusDist = d;
|
|
201
|
-
if (Array.isArray(pose.focus)) focusDist = D - apply(view, ...pose.focus)[2];
|
|
202
|
-
else if (typeof pose.focus === "number") focusDist = d - pose.focus * u;
|
|
203
|
-
return { u, D, d, view, project, focusDist };
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
/** A 3D stage: everything inside (Plane, Screen, Lift, Group) is seen through one moving camera. */
|
|
207
|
-
export function Stage({ camera, float = 0, motionBlur = 0, seed = 1, background, children, style }: StageProps) {
|
|
208
|
-
const frame = useCurrentFrame();
|
|
209
|
-
const { width, height, fps } = useVideoConfig();
|
|
210
|
-
const poseAt = (f: number) => {
|
|
211
|
-
const p = cameraAt(f, camera);
|
|
212
|
-
const o = floatOffset(f, fps, float, seed);
|
|
213
|
-
return { ...p, x: p.x + o.x, y: p.y + o.y, tilt: p.tilt + o.tilt, turn: p.turn + o.turn, roll: p.roll + o.roll };
|
|
214
|
-
};
|
|
215
|
-
const pose = poseAt(frame);
|
|
216
|
-
const cam = buildCamera(pose, width, height);
|
|
217
|
-
const ctx: StageContext = {
|
|
218
|
-
width,
|
|
219
|
-
height,
|
|
220
|
-
u: cam.u,
|
|
221
|
-
D: cam.D,
|
|
222
|
-
view: cam.view,
|
|
223
|
-
project: cam.project,
|
|
224
|
-
focusDist: cam.focusDist,
|
|
225
|
-
aperture: Math.max(0, pose.aperture),
|
|
226
|
-
fov: Math.min(110, Math.max(4, pose.fov)),
|
|
227
|
-
shiftX: pose.shiftX * width,
|
|
228
|
-
shiftY: pose.shiftY * height,
|
|
229
|
-
model: identity(),
|
|
230
|
-
};
|
|
231
|
-
|
|
232
|
-
// Motion blur from how far the frame center travels between neighbouring frames.
|
|
233
|
-
let blur = 0;
|
|
234
|
-
if (motionBlur > 0) {
|
|
235
|
-
const travel = (f: number) => {
|
|
236
|
-
const c = buildCamera(poseAt(f), width, height);
|
|
237
|
-
const m = mul(c.project, c.view);
|
|
238
|
-
const p = apply(m, pose.x, pose.y, pose.z);
|
|
239
|
-
const s = apply(m, pose.x + 100, pose.y, pose.z);
|
|
240
|
-
return { x: p[0] / p[3], y: p[1] / p[3], s: Math.hypot(s[0] / s[3] - p[0] / p[3], s[1] / s[3] - p[1] / p[3]) };
|
|
241
|
-
};
|
|
242
|
-
const a = travel(frame - 1);
|
|
243
|
-
const b = travel(frame + 1);
|
|
244
|
-
const speed = Math.hypot(b.x - a.x, b.y - a.y) / 2 + (Math.abs(b.s - a.s) / 100) * Math.min(width, height) * 0.25;
|
|
245
|
-
// Screen px, so the thresholds follow the frame size (u = 1 at 1080).
|
|
246
|
-
blur = Math.min(28 * cam.u, Math.max(0, speed - 6 * cam.u) * 0.22 * motionBlur);
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
return (
|
|
250
|
-
<AbsoluteFill style={{ overflow: "hidden", background, ...style }}>
|
|
251
|
-
<StageCtx.Provider value={ctx}>
|
|
252
|
-
<AbsoluteFill style={blur > 0.4 * cam.u ? { filter: `blur(${blur.toFixed(2)}px)` } : undefined}>{children}</AbsoluteFill>
|
|
253
|
-
</StageCtx.Provider>
|
|
254
|
-
</AbsoluteFill>
|
|
255
|
-
);
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
// ─── Groups and planes ───────────────────────────────────────────────────────────────────────────
|
|
259
|
-
|
|
260
|
-
/** Moves, turns and scales everything inside it together (a device and the UI lifted off it). */
|
|
261
|
-
export function Group({ children, ...placement }: Placement & { children?: ReactNode }) {
|
|
262
|
-
const ctx = useStageContext("Group");
|
|
263
|
-
return <StageCtx.Provider value={{ ...ctx, model: mul(ctx.model, placementMatrix(placement)) }}>{children}</StageCtx.Provider>;
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
export interface PlaneProps extends Placement {
|
|
267
|
-
/** Size in world px (1080 = the short side of the frame). */
|
|
268
|
-
width: number;
|
|
269
|
-
height: number;
|
|
270
|
-
radius?: number;
|
|
271
|
-
background?: string;
|
|
272
|
-
opacity?: number;
|
|
273
|
-
/** Soft shadow cast behind the plane: 0 none, 1 strong. */
|
|
274
|
-
shadow?: number;
|
|
275
|
-
/** How far behind the plane the shadow falls (world px). Lifted cards: 40–120. */
|
|
276
|
-
shadowDepth?: number;
|
|
277
|
-
/** Mirror below the plane on a glossy floor: 0 none, 0.25 subtle, 0.5 strong. */
|
|
278
|
-
reflection?: number;
|
|
279
|
-
/** Lens blur from the camera's focus and aperture (true), or always sharp (false). */
|
|
280
|
-
dof?: boolean;
|
|
281
|
-
/** A thin light edge, like glass catching light: 0 none, 1 bright. */
|
|
282
|
-
rim?: number;
|
|
283
|
-
/** Extra softness in world px on top of the lens blur (soft shadows, glows). */
|
|
284
|
-
blur?: number;
|
|
285
|
-
/** Exact placement matrix (world, row-major) instead of x/y/z/tilt/turn/roll: for planes laid onto
|
|
286
|
-
* other surfaces (lifts off a 3D device's screen). */
|
|
287
|
-
basis?: M4;
|
|
288
|
-
children?: ReactNode;
|
|
289
|
-
style?: CSSProperties;
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
/** Lens blur cap in screen px at 1080 (scaled with the frame). */
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
const
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
*
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
const
|
|
325
|
-
const
|
|
326
|
-
const
|
|
327
|
-
const
|
|
328
|
-
const
|
|
329
|
-
|
|
330
|
-
const
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
const
|
|
340
|
-
const
|
|
341
|
-
const
|
|
342
|
-
const
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
}
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
.
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
*
|
|
376
|
-
*/
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
}
|
|
384
|
-
return
|
|
385
|
-
}
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
*
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
}
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
<
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
const
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
}
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
}
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
const
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
export
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
{
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
}}
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
}
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
}
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
const
|
|
995
|
-
const
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
}
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
)
|
|
1061
|
-
}
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1
|
+
// Cinematic toolkit: a real 3D camera for product shots (screens, devices and UI pieces in depth,
|
|
2
|
+
// with lens blur, glare, shadows and reflections). Scenes import it from "agentcut/components".
|
|
3
|
+
//
|
|
4
|
+
// Every plane is one element with a full matrix3d(): the browser preview draws it natively and the
|
|
5
|
+
// browser export draws it through WebGL, so the two match. Never put CSS `perspective` or
|
|
6
|
+
// `preserve-3d` around it: the stage already projects every plane itself.
|
|
7
|
+
import { Video } from "@remotion/media";
|
|
8
|
+
import { createContext, useContext, useId, type ComponentProps, type CSSProperties, type ReactNode } from "react";
|
|
9
|
+
import { AbsoluteFill, Img, interpolate, useCurrentFrame, useRemotionEnvironment, useVideoConfig } from "remotion";
|
|
10
|
+
import { alpha, glowStyle } from "../lib";
|
|
11
|
+
import {
|
|
12
|
+
apply,
|
|
13
|
+
EASES,
|
|
14
|
+
identity,
|
|
15
|
+
mul,
|
|
16
|
+
perspective,
|
|
17
|
+
placementMatrix,
|
|
18
|
+
rotateX,
|
|
19
|
+
rotateY,
|
|
20
|
+
rotateZ,
|
|
21
|
+
scale,
|
|
22
|
+
toCss,
|
|
23
|
+
translate,
|
|
24
|
+
type EaseName,
|
|
25
|
+
type M4,
|
|
26
|
+
type Placement,
|
|
27
|
+
} from "./math";
|
|
28
|
+
|
|
29
|
+
export type { EaseName, M4, Placement };
|
|
30
|
+
|
|
31
|
+
// ─── Camera ──────────────────────────────────────────────────────────────────────────────────────
|
|
32
|
+
|
|
33
|
+
export interface CameraPose {
|
|
34
|
+
/** The point the camera looks at, in world px (0,0,0 = frame center; y down, z toward the viewer). */
|
|
35
|
+
x?: number;
|
|
36
|
+
y?: number;
|
|
37
|
+
z?: number;
|
|
38
|
+
/** 1: things at z = 0 show at their size. 2: twice as close. 0.6: pulled back. */
|
|
39
|
+
zoom?: number;
|
|
40
|
+
/** Degrees. tilt > 0: the camera rises and looks down (floors and laptop decks show their top).
|
|
41
|
+
* turn > 0: the camera orbits to the left, so the right side recedes. roll > 0: the frame rotates
|
|
42
|
+
* clockwise. To lay a screen back like a laptop lid, tilt the Screen/Group itself (tilt 25–55). */
|
|
43
|
+
tilt?: number;
|
|
44
|
+
turn?: number;
|
|
45
|
+
roll?: number;
|
|
46
|
+
/** Vertical field of view in degrees: 15–25 flat telephoto (product), 35 natural, 50–70 dramatic. */
|
|
47
|
+
fov?: number;
|
|
48
|
+
/** What's sharp: a world point [x, y, z], or a distance in px toward the camera from the target (0 = target). */
|
|
49
|
+
focus?: [number, number, number] | number;
|
|
50
|
+
/** Lens blur away from the focus: 0 none, 0.4 subtle, 1 strong, 2 macro. */
|
|
51
|
+
aperture?: number;
|
|
52
|
+
/** Moves the target off-center on screen, in fractions of the frame (-0.5…0.5): room for a title. */
|
|
53
|
+
shiftX?: number;
|
|
54
|
+
shiftY?: number;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export interface CameraKey extends CameraPose {
|
|
58
|
+
/** Frame (local to the clip) where the camera reaches this pose. */
|
|
59
|
+
at: number;
|
|
60
|
+
/** Easing of the move INTO this key. */
|
|
61
|
+
ease?: EaseName;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const POSE_DEFAULTS: Required<Omit<CameraPose, "focus">> & { focus: CameraPose["focus"] } = {
|
|
65
|
+
x: 0,
|
|
66
|
+
y: 0,
|
|
67
|
+
z: 0,
|
|
68
|
+
zoom: 1,
|
|
69
|
+
tilt: 0,
|
|
70
|
+
turn: 0,
|
|
71
|
+
roll: 0,
|
|
72
|
+
fov: 24,
|
|
73
|
+
focus: 0,
|
|
74
|
+
aperture: 0,
|
|
75
|
+
shiftX: 0,
|
|
76
|
+
shiftY: 0,
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
type FullPose = typeof POSE_DEFAULTS;
|
|
80
|
+
const NUMERIC = ["x", "y", "z", "zoom", "tilt", "turn", "roll", "fov", "aperture", "shiftX", "shiftY"] as const;
|
|
81
|
+
|
|
82
|
+
/** The camera pose at `frame`: keys carry their values forward, each move eases into its key. */
|
|
83
|
+
export function cameraAt(frame: number, camera: CameraPose | CameraKey[]): FullPose {
|
|
84
|
+
if (!Array.isArray(camera)) return { ...POSE_DEFAULTS, ...stripUndefined(camera) };
|
|
85
|
+
const keys = [...camera].sort((a, b) => a.at - b.at);
|
|
86
|
+
if (keys.length === 0) return { ...POSE_DEFAULTS };
|
|
87
|
+
// Fill every key with the values before it.
|
|
88
|
+
const full: (FullPose & { at: number; ease?: EaseName })[] = [];
|
|
89
|
+
let prev: FullPose = { ...POSE_DEFAULTS };
|
|
90
|
+
for (const k of keys) {
|
|
91
|
+
const next = { ...prev, ...stripUndefined(k), at: k.at, ease: k.ease } as FullPose & { at: number; ease?: EaseName };
|
|
92
|
+
full.push(next);
|
|
93
|
+
prev = next;
|
|
94
|
+
}
|
|
95
|
+
if (frame <= full[0].at) return full[0];
|
|
96
|
+
const last = full[full.length - 1];
|
|
97
|
+
if (frame >= last.at) return last;
|
|
98
|
+
let i = 0;
|
|
99
|
+
while (full[i + 1].at < frame) i++;
|
|
100
|
+
const a = full[i];
|
|
101
|
+
const b = full[i + 1];
|
|
102
|
+
const t = EASES[b.ease ?? "inOut"]((frame - a.at) / Math.max(1e-6, b.at - a.at));
|
|
103
|
+
const out = { ...b } as FullPose;
|
|
104
|
+
for (const key of NUMERIC) out[key] = a[key] + (b[key] - a[key]) * t;
|
|
105
|
+
out.focus = mixFocus(a.focus, b.focus, t);
|
|
106
|
+
return out;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function stripUndefined<T extends object>(o: T): Partial<T> {
|
|
110
|
+
return Object.fromEntries(Object.entries(o).filter(([, v]) => v !== undefined)) as Partial<T>;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function mixFocus(a: CameraPose["focus"], b: CameraPose["focus"], t: number): CameraPose["focus"] {
|
|
114
|
+
if (Array.isArray(a) && Array.isArray(b)) return [0, 1, 2].map((i) => a[i] + (b[i] - a[i]) * t) as [number, number, number];
|
|
115
|
+
if (typeof a === "number" && typeof b === "number") return a + (b - a) * t;
|
|
116
|
+
return t < 0.5 ? a : b;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** Slow, smooth drift that makes a locked-off shot feel filmed (deterministic). */
|
|
120
|
+
function floatOffset(frame: number, fps: number, amount: number, seed: number) {
|
|
121
|
+
if (!amount) return { x: 0, y: 0, tilt: 0, turn: 0, roll: 0 };
|
|
122
|
+
const t = frame / fps + seed * 7.31;
|
|
123
|
+
const w = (a: number, b: number) => Math.sin(t * a + seed * b) * 0.6 + Math.sin(t * a * 2.3 + seed * b * 1.7) * 0.4;
|
|
124
|
+
return {
|
|
125
|
+
x: w(0.31, 1.1) * 10 * amount,
|
|
126
|
+
y: w(0.23, 2.3) * 7 * amount,
|
|
127
|
+
tilt: w(0.19, 3.7) * 0.5 * amount,
|
|
128
|
+
turn: w(0.27, 4.1) * 0.6 * amount,
|
|
129
|
+
roll: w(0.17, 5.3) * 0.25 * amount,
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// ─── Stage context ───────────────────────────────────────────────────────────────────────────────
|
|
134
|
+
|
|
135
|
+
interface StageContext {
|
|
136
|
+
width: number;
|
|
137
|
+
height: number;
|
|
138
|
+
/** World px → screen px at zoom 1 (1 at 1080 px on the short side). */
|
|
139
|
+
u: number;
|
|
140
|
+
/** Viewer distance of the projection (px). */
|
|
141
|
+
D: number;
|
|
142
|
+
/** World → camera space. */
|
|
143
|
+
view: M4;
|
|
144
|
+
/** Camera space → screen (perspective + centering). */
|
|
145
|
+
project: M4;
|
|
146
|
+
focusDist: number;
|
|
147
|
+
aperture: number;
|
|
148
|
+
/** Lens and target shift, for renderers that draw through the same camera (Device3D). */
|
|
149
|
+
fov: number;
|
|
150
|
+
shiftX: number;
|
|
151
|
+
shiftY: number;
|
|
152
|
+
/** Parent placement (groups). */
|
|
153
|
+
model: M4;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
const StageCtx = createContext<StageContext | null>(null);
|
|
157
|
+
|
|
158
|
+
export function useStageContext(component: string) {
|
|
159
|
+
const ctx = useContext(StageCtx);
|
|
160
|
+
if (!ctx) throw new Error(`<${component}> must be inside a <Stage>.`);
|
|
161
|
+
return ctx;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/** Frame size, world unit and orientation, for sizing things in both variants. */
|
|
165
|
+
export function useStage() {
|
|
166
|
+
const { width, height } = useVideoConfig();
|
|
167
|
+
const u = Math.min(width, height) / 1080;
|
|
168
|
+
return { width, height, u, vertical: height > width, worldWidth: width / u, worldHeight: height / u };
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
export interface StageProps {
|
|
172
|
+
/** One pose, or keyframes the camera moves through. */
|
|
173
|
+
camera: CameraPose | CameraKey[];
|
|
174
|
+
/** Handheld drift, 0 locked off, 0.5 gentle, 1 floating. */
|
|
175
|
+
float?: number;
|
|
176
|
+
/** Blur while the camera moves fast (whips, snaps): 0 off, 1 natural. */
|
|
177
|
+
motionBlur?: number;
|
|
178
|
+
seed?: number;
|
|
179
|
+
/** Background behind the planes (a color, a linear-gradient, or a <Backdrop> as the first child). */
|
|
180
|
+
background?: string;
|
|
181
|
+
children?: ReactNode;
|
|
182
|
+
style?: CSSProperties;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function buildCamera(pose: FullPose, width: number, height: number) {
|
|
186
|
+
const u = Math.min(width, height) / 1080;
|
|
187
|
+
const fov = Math.min(110, Math.max(4, pose.fov));
|
|
188
|
+
const D = height / 2 / Math.tan((fov * Math.PI) / 360);
|
|
189
|
+
const d = D / Math.max(0.05, pose.zoom);
|
|
190
|
+
const view = mul(
|
|
191
|
+
translate(0, 0, D - d),
|
|
192
|
+
rotateZ(pose.roll),
|
|
193
|
+
// Camera above looking down (tilt > 0) = the world turns its top toward the lens.
|
|
194
|
+
rotateX(-pose.tilt),
|
|
195
|
+
rotateY(pose.turn),
|
|
196
|
+
scale(u),
|
|
197
|
+
translate(-pose.x, -pose.y, -pose.z),
|
|
198
|
+
);
|
|
199
|
+
const project = mul(translate(width / 2 + pose.shiftX * width, height / 2 + pose.shiftY * height, 0), perspective(D));
|
|
200
|
+
let focusDist = d;
|
|
201
|
+
if (Array.isArray(pose.focus)) focusDist = D - apply(view, ...pose.focus)[2];
|
|
202
|
+
else if (typeof pose.focus === "number") focusDist = d - pose.focus * u;
|
|
203
|
+
return { u, D, d, view, project, focusDist };
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/** A 3D stage: everything inside (Plane, Screen, Lift, Group) is seen through one moving camera. */
|
|
207
|
+
export function Stage({ camera, float = 0, motionBlur = 0, seed = 1, background, children, style }: StageProps) {
|
|
208
|
+
const frame = useCurrentFrame();
|
|
209
|
+
const { width, height, fps } = useVideoConfig();
|
|
210
|
+
const poseAt = (f: number) => {
|
|
211
|
+
const p = cameraAt(f, camera);
|
|
212
|
+
const o = floatOffset(f, fps, float, seed);
|
|
213
|
+
return { ...p, x: p.x + o.x, y: p.y + o.y, tilt: p.tilt + o.tilt, turn: p.turn + o.turn, roll: p.roll + o.roll };
|
|
214
|
+
};
|
|
215
|
+
const pose = poseAt(frame);
|
|
216
|
+
const cam = buildCamera(pose, width, height);
|
|
217
|
+
const ctx: StageContext = {
|
|
218
|
+
width,
|
|
219
|
+
height,
|
|
220
|
+
u: cam.u,
|
|
221
|
+
D: cam.D,
|
|
222
|
+
view: cam.view,
|
|
223
|
+
project: cam.project,
|
|
224
|
+
focusDist: cam.focusDist,
|
|
225
|
+
aperture: Math.max(0, pose.aperture),
|
|
226
|
+
fov: Math.min(110, Math.max(4, pose.fov)),
|
|
227
|
+
shiftX: pose.shiftX * width,
|
|
228
|
+
shiftY: pose.shiftY * height,
|
|
229
|
+
model: identity(),
|
|
230
|
+
};
|
|
231
|
+
|
|
232
|
+
// Motion blur from how far the frame center travels between neighbouring frames.
|
|
233
|
+
let blur = 0;
|
|
234
|
+
if (motionBlur > 0) {
|
|
235
|
+
const travel = (f: number) => {
|
|
236
|
+
const c = buildCamera(poseAt(f), width, height);
|
|
237
|
+
const m = mul(c.project, c.view);
|
|
238
|
+
const p = apply(m, pose.x, pose.y, pose.z);
|
|
239
|
+
const s = apply(m, pose.x + 100, pose.y, pose.z);
|
|
240
|
+
return { x: p[0] / p[3], y: p[1] / p[3], s: Math.hypot(s[0] / s[3] - p[0] / p[3], s[1] / s[3] - p[1] / p[3]) };
|
|
241
|
+
};
|
|
242
|
+
const a = travel(frame - 1);
|
|
243
|
+
const b = travel(frame + 1);
|
|
244
|
+
const speed = Math.hypot(b.x - a.x, b.y - a.y) / 2 + (Math.abs(b.s - a.s) / 100) * Math.min(width, height) * 0.25;
|
|
245
|
+
// Screen px, so the thresholds follow the frame size (u = 1 at 1080).
|
|
246
|
+
blur = Math.min(28 * cam.u, Math.max(0, speed - 6 * cam.u) * 0.22 * motionBlur);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
return (
|
|
250
|
+
<AbsoluteFill style={{ overflow: "hidden", background, ...style }}>
|
|
251
|
+
<StageCtx.Provider value={ctx}>
|
|
252
|
+
<AbsoluteFill style={blur > 0.4 * cam.u ? { filter: `blur(${blur.toFixed(2)}px)` } : undefined}>{children}</AbsoluteFill>
|
|
253
|
+
</StageCtx.Provider>
|
|
254
|
+
</AbsoluteFill>
|
|
255
|
+
);
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
// ─── Groups and planes ───────────────────────────────────────────────────────────────────────────
|
|
259
|
+
|
|
260
|
+
/** Moves, turns and scales everything inside it together (a device and the UI lifted off it). */
|
|
261
|
+
export function Group({ children, ...placement }: Placement & { children?: ReactNode }) {
|
|
262
|
+
const ctx = useStageContext("Group");
|
|
263
|
+
return <StageCtx.Provider value={{ ...ctx, model: mul(ctx.model, placementMatrix(placement)) }}>{children}</StageCtx.Provider>;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
export interface PlaneProps extends Placement {
|
|
267
|
+
/** Size in world px (1080 = the short side of the frame). */
|
|
268
|
+
width: number;
|
|
269
|
+
height: number;
|
|
270
|
+
radius?: number;
|
|
271
|
+
background?: string;
|
|
272
|
+
opacity?: number;
|
|
273
|
+
/** Soft shadow cast behind the plane: 0 none, 1 strong. */
|
|
274
|
+
shadow?: number;
|
|
275
|
+
/** How far behind the plane the shadow falls (world px). Lifted cards: 40–120. */
|
|
276
|
+
shadowDepth?: number;
|
|
277
|
+
/** Mirror below the plane on a glossy floor: 0 none, 0.25 subtle, 0.5 strong. */
|
|
278
|
+
reflection?: number;
|
|
279
|
+
/** Lens blur from the camera's focus and aperture (true), or always sharp (false). */
|
|
280
|
+
dof?: boolean;
|
|
281
|
+
/** A thin light edge, like glass catching light: 0 none, 1 bright. */
|
|
282
|
+
rim?: number;
|
|
283
|
+
/** Extra softness in world px on top of the lens blur (soft shadows, glows). */
|
|
284
|
+
blur?: number;
|
|
285
|
+
/** Exact placement matrix (world, row-major) instead of x/y/z/tilt/turn/roll: for planes laid onto
|
|
286
|
+
* other surfaces (lifts off a 3D device's screen). */
|
|
287
|
+
basis?: M4;
|
|
288
|
+
children?: ReactNode;
|
|
289
|
+
style?: CSSProperties;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
/** Lens blur cap in screen px at 1080 (scaled with the frame). */
|
|
293
|
+
// Room for macro close-ups (aperture 2–4): a razor-thin focus band with everything else melted.
|
|
294
|
+
const MAX_BLUR = 80;
|
|
295
|
+
|
|
296
|
+
/** Camera distance of a world-space point on this plane (local px). */
|
|
297
|
+
function distAt(m: M4, D: number, x: number, y: number) {
|
|
298
|
+
return D - apply(m, x, y, 0)[2];
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/** Lens blur in screen px for a point `dist` from the viewer. */
|
|
302
|
+
export function blurFor(ctx: StageContext, dist: number) {
|
|
303
|
+
if (ctx.aperture <= 0 || dist <= 1) return 0;
|
|
304
|
+
return Math.min(MAX_BLUR * ctx.u, (ctx.aperture * 0.09 * ctx.height * Math.abs(dist - ctx.focusDist)) / dist);
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
/** Camera-space px per local px of a plane (its placement scale times u, before perspective). */
|
|
308
|
+
export function localScale(camModel: M4) {
|
|
309
|
+
const kx = Math.hypot(camModel[0], camModel[4], camModel[8]);
|
|
310
|
+
const ky = Math.hypot(camModel[1], camModel[5], camModel[9]);
|
|
311
|
+
return Math.max(1e-6, Math.sqrt(kx * ky));
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
// Fine enough that a macro shot's thin focus band stays smooth.
|
|
315
|
+
const STOPS = 49;
|
|
316
|
+
|
|
317
|
+
/**
|
|
318
|
+
* Lens blur across a plane: none, one blur, or a gradient of blur along the plane's depth. Blurs come
|
|
319
|
+
* back in the plane's local px: CSS (and the browser export) blur an element before its matrix3d()
|
|
320
|
+
* projects it, so a screen-px blur is divided by how much the plane is magnified there.
|
|
321
|
+
*/
|
|
322
|
+
function planeFocus(ctx: StageContext, camModel: M4, w: number, h: number) {
|
|
323
|
+
if (ctx.aperture <= 0) return { kind: "sharp" as const };
|
|
324
|
+
const d00 = distAt(camModel, ctx.D, 0, 0);
|
|
325
|
+
const d10 = distAt(camModel, ctx.D, w, 0);
|
|
326
|
+
const d01 = distAt(camModel, ctx.D, 0, h);
|
|
327
|
+
const gx = (d10 - d00) / Math.max(1, w);
|
|
328
|
+
const gy = (d01 - d00) / Math.max(1, h);
|
|
329
|
+
const k = localScale(camModel);
|
|
330
|
+
const distAtLocal = (x: number, y: number) => d00 + gx * x + gy * y;
|
|
331
|
+
// One local px covers k·D/dist screen px.
|
|
332
|
+
const localBlur = (dist: number) => (dist <= 1 ? 0 : (blurFor(ctx, dist) * dist) / (ctx.D * k));
|
|
333
|
+
const len = Math.hypot(gx, gy);
|
|
334
|
+
if (len * Math.hypot(w, h) < 2) {
|
|
335
|
+
const center = distAtLocal(w / 2, h / 2);
|
|
336
|
+
return blurFor(ctx, center) < 0.5 ? { kind: "sharp" as const } : { kind: "uniform" as const, blur: localBlur(center) };
|
|
337
|
+
}
|
|
338
|
+
// CSS gradient angle: 0deg points up, 90deg right; direction of increasing distance.
|
|
339
|
+
const dx = gx / len;
|
|
340
|
+
const dy = gy / len;
|
|
341
|
+
const angle = (Math.atan2(dx, -dy) * 180) / Math.PI;
|
|
342
|
+
const L = Math.abs(w * dx) + Math.abs(h * dy);
|
|
343
|
+
const screen: number[] = [];
|
|
344
|
+
const local: number[] = [];
|
|
345
|
+
const onScreen: boolean[] = [];
|
|
346
|
+
const toScreen = mul(ctx.project, camModel);
|
|
347
|
+
for (let i = 0; i < STOPS; i++) {
|
|
348
|
+
const t = i / (STOPS - 1) - 0.5;
|
|
349
|
+
const px = w / 2 + dx * t * L;
|
|
350
|
+
const py = h / 2 + dy * t * L;
|
|
351
|
+
const dist = distAtLocal(px, py);
|
|
352
|
+
screen.push(blurFor(ctx, dist));
|
|
353
|
+
local.push(localBlur(dist));
|
|
354
|
+
const [sx, sy, , sw] = apply(toScreen, px, py, 0);
|
|
355
|
+
const margin = 0.25;
|
|
356
|
+
onScreen.push(
|
|
357
|
+
sw > 0 &&
|
|
358
|
+
sx / sw > -margin * ctx.width &&
|
|
359
|
+
sx / sw < (1 + margin) * ctx.width &&
|
|
360
|
+
sy / sw > -margin * ctx.height &&
|
|
361
|
+
sy / sw < (1 + margin) * ctx.height,
|
|
362
|
+
);
|
|
363
|
+
}
|
|
364
|
+
// Scale the blur copies to what's on screen. In a close-up most of the plane is out of frame, and its
|
|
365
|
+
// far end (tiny on screen, so a huge blur in local px) would otherwise set the scale and leave the
|
|
366
|
+
// visible part a faint mix of sharp and soft whatever the aperture.
|
|
367
|
+
const visible = (values: number[]) => {
|
|
368
|
+
const kept = values.filter((_, i) => onScreen[i]);
|
|
369
|
+
return kept.length ? kept : values;
|
|
370
|
+
};
|
|
371
|
+
const maxScreen = Math.max(...visible(screen));
|
|
372
|
+
const minScreen = Math.min(...visible(screen));
|
|
373
|
+
const max = Math.max(...visible(local));
|
|
374
|
+
if (maxScreen < 0.6) return { kind: "sharp" as const };
|
|
375
|
+
if (maxScreen - minScreen < Math.max(1, maxScreen * 0.2)) return { kind: "uniform" as const, blur: (max + Math.min(...local)) / 2 };
|
|
376
|
+
/** Local blur along the gradient line of the box padded by `pad` on every side (the copies' box). */
|
|
377
|
+
const blursAcross = (pad: number) => {
|
|
378
|
+
const Lp = Math.abs((w + 2 * pad) * dx) + Math.abs((h + 2 * pad) * dy);
|
|
379
|
+
return Array.from({ length: STOPS }, (_, i) => {
|
|
380
|
+
const t = i / (STOPS - 1) - 0.5;
|
|
381
|
+
return Math.min(max, localBlur(distAtLocal(w / 2 + dx * t * Lp, h / 2 + dy * t * Lp)));
|
|
382
|
+
});
|
|
383
|
+
};
|
|
384
|
+
return { kind: "gradient" as const, angle, max, blursAcross };
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
function maskFrom(angle: number, alphas: number[]): CSSProperties {
|
|
388
|
+
const stops = alphas
|
|
389
|
+
.map((a, i) => `rgba(0,0,0,${Math.min(1, Math.max(0, a)).toFixed(3)}) ${((i / (alphas.length - 1)) * 100).toFixed(1)}%`)
|
|
390
|
+
.join(", ");
|
|
391
|
+
const mask = `linear-gradient(${angle.toFixed(2)}deg, ${stops})`;
|
|
392
|
+
return { maskImage: mask, WebkitMaskImage: mask };
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
/**
|
|
396
|
+
* Alphas for copies stacked bottom → top so copy i shows weights[i] of the result at `opacity`. A fading
|
|
397
|
+
* plane keeps its opacity instead of compounding it once per copy.
|
|
398
|
+
*/
|
|
399
|
+
function stackAlphas(weights: number[], opacity: number) {
|
|
400
|
+
const alphas = new Array<number>(weights.length).fill(0);
|
|
401
|
+
let below = 1;
|
|
402
|
+
for (let i = weights.length - 1; i >= 0; i--) {
|
|
403
|
+
alphas[i] = below > 1e-4 ? Math.min(1, Math.max(0, (opacity * weights[i]) / below)) : 0;
|
|
404
|
+
below *= 1 - alphas[i];
|
|
405
|
+
}
|
|
406
|
+
return alphas;
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
// Blur plus a no-op shadow: the browser export then blurs the copy as one picture, so its soft edge
|
|
410
|
+
// spills past the rounded box as in the preview instead of being cut at it.
|
|
411
|
+
const groupBlur = (px: number) => (px > 0.05 ? `blur(${px.toFixed(2)}px) drop-shadow(0 0 0 rgba(0,0,0,0))` : undefined);
|
|
412
|
+
const padFor = (px: number) => (px > 0.05 ? Math.ceil(px * 3) + 1 : 0);
|
|
413
|
+
|
|
414
|
+
const PlaneCopyCtx = createContext(false);
|
|
415
|
+
|
|
416
|
+
/**
|
|
417
|
+
* `copy` is true inside the extra copies a Plane draws for lens blur and reflections. Content with sound
|
|
418
|
+
* or heavy work checks it: a copy is only ever seen blurred or mirrored.
|
|
419
|
+
*/
|
|
420
|
+
export function usePlaneLayer() {
|
|
421
|
+
return { copy: useContext(PlaneCopyCtx) };
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
/** A <Video> (@remotion/media) for plane and screen content: only the main copy plays its sound. */
|
|
425
|
+
export function PlaneVideo(props: ComponentProps<typeof Video>) {
|
|
426
|
+
const { copy } = usePlaneLayer();
|
|
427
|
+
return <Video {...props} muted={copy || props.muted} />;
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
export type { StageContext };
|
|
431
|
+
|
|
432
|
+
/** A flat surface in 3D: put any content inside (an <Img>, a <PlaneVideo>, a <Device>, text, a card). */
|
|
433
|
+
export function Plane({
|
|
434
|
+
width,
|
|
435
|
+
height,
|
|
436
|
+
radius = 0,
|
|
437
|
+
background,
|
|
438
|
+
opacity = 1,
|
|
439
|
+
shadow = 0,
|
|
440
|
+
shadowDepth = 60,
|
|
441
|
+
reflection = 0,
|
|
442
|
+
dof = true,
|
|
443
|
+
rim = 0,
|
|
444
|
+
blur = 0,
|
|
445
|
+
basis,
|
|
446
|
+
children,
|
|
447
|
+
style,
|
|
448
|
+
...placement
|
|
449
|
+
}: PlaneProps) {
|
|
450
|
+
const ctx = useStageContext("Plane");
|
|
451
|
+
const env = useRemotionEnvironment();
|
|
452
|
+
// The live preview draws two copies for a depth blur instead of three (captures and exports get three).
|
|
453
|
+
const lightPreview = env.isPlayer && !env.isRendering;
|
|
454
|
+
const model = mul(ctx.model, basis ?? placementMatrix(placement));
|
|
455
|
+
const camModel = mul(ctx.view, model, translate(-width / 2, -height / 2, 0));
|
|
456
|
+
// A plane reaching behind the camera can't be projected as a whole: below, only its part in front of
|
|
457
|
+
// the lens is drawn. Big surfaces (Floor) are also tiled.
|
|
458
|
+
const nearest = Math.min(
|
|
459
|
+
distAt(camModel, ctx.D, 0, 0),
|
|
460
|
+
distAt(camModel, ctx.D, width, 0),
|
|
461
|
+
distAt(camModel, ctx.D, 0, height),
|
|
462
|
+
distAt(camModel, ctx.D, width, height),
|
|
463
|
+
);
|
|
464
|
+
if (opacity <= 0) return null;
|
|
465
|
+
const near = ctx.D * 0.04;
|
|
466
|
+
if (nearest < near) {
|
|
467
|
+
// The plane passes the lens (macro close-ups, low fly-overs). Keep the part in front of the camera:
|
|
468
|
+
// the largest edge-aligned rectangle whose corners are all safely in front, redrawn as a smaller
|
|
469
|
+
// plane with the content offset so nothing moves.
|
|
470
|
+
const d00 = distAt(camModel, ctx.D, 0, 0);
|
|
471
|
+
const gx = (distAt(camModel, ctx.D, width, 0) - d00) / Math.max(1, width);
|
|
472
|
+
const gy = (distAt(camModel, ctx.D, 0, height) - d00) / Math.max(1, height);
|
|
473
|
+
const dist = (x: number, y: number) => d00 + gx * x + gy * y;
|
|
474
|
+
const safe = near * 1.5;
|
|
475
|
+
let [x0, y0, x1, y1] = [0, 0, width, height];
|
|
476
|
+
for (let i = 0; i < 40; i++) {
|
|
477
|
+
const corners: [number, number][] = [[x0, y0], [x1, y0], [x0, y1], [x1, y1]];
|
|
478
|
+
const worst = corners.reduce((a, c) => (dist(c[0], c[1]) < dist(a[0], a[1]) ? c : a));
|
|
479
|
+
if (dist(worst[0], worst[1]) >= safe) break;
|
|
480
|
+
// Pull in the two edges that meet at the closest corner, in proportion to how fast depth changes.
|
|
481
|
+
const step = 0.05;
|
|
482
|
+
if (Math.abs(gx) * width > 1e-6) {
|
|
483
|
+
if (worst[0] === x0 && gx > 0) x0 += (x1 - x0) * step;
|
|
484
|
+
else if (worst[0] === x1 && gx < 0) x1 -= (x1 - x0) * step;
|
|
485
|
+
}
|
|
486
|
+
if (Math.abs(gy) * height > 1e-6) {
|
|
487
|
+
if (worst[1] === y0 && gy > 0) y0 += (y1 - y0) * step;
|
|
488
|
+
else if (worst[1] === y1 && gy < 0) y1 -= (y1 - y0) * step;
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
const sw = x1 - x0;
|
|
492
|
+
const sh = y1 - y0;
|
|
493
|
+
const ok = [[x0, y0], [x1, y0], [x0, y1], [x1, y1]].every(([x, y]) => dist(x, y) >= near);
|
|
494
|
+
if (!ok || sw < 2 || sh < 2) return null;
|
|
495
|
+
const subBasis = mul(basis ?? placementMatrix(placement), translate(x0 + sw / 2 - width / 2, y0 + sh / 2 - height / 2, 0));
|
|
496
|
+
return (
|
|
497
|
+
<Plane
|
|
498
|
+
basis={subBasis}
|
|
499
|
+
width={sw}
|
|
500
|
+
height={sh}
|
|
501
|
+
radius={radius}
|
|
502
|
+
background={background}
|
|
503
|
+
opacity={opacity}
|
|
504
|
+
dof={dof}
|
|
505
|
+
rim={rim}
|
|
506
|
+
blur={blur}
|
|
507
|
+
style={style}
|
|
508
|
+
>
|
|
509
|
+
<div style={{ position: "absolute", left: -x0, top: -y0, width, height }}>{children}</div>
|
|
510
|
+
</Plane>
|
|
511
|
+
);
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
// Extra softness asked for in world px, in this plane's local px.
|
|
515
|
+
const extra = (blur * ctx.u) / localScale(camModel);
|
|
516
|
+
const withExtra = (b: number) => Math.hypot(b, extra);
|
|
517
|
+
const matrix = (m: M4) => toCss(mul(ctx.project, m));
|
|
518
|
+
const focus = dof ? planeFocus(ctx, camModel, width, height) : ({ kind: "sharp" } as const);
|
|
519
|
+
const surface: CSSProperties = {
|
|
520
|
+
position: "absolute",
|
|
521
|
+
left: 0,
|
|
522
|
+
top: 0,
|
|
523
|
+
width,
|
|
524
|
+
height,
|
|
525
|
+
borderRadius: radius,
|
|
526
|
+
overflow: "hidden",
|
|
527
|
+
background,
|
|
528
|
+
...style,
|
|
529
|
+
};
|
|
530
|
+
const content = (
|
|
531
|
+
<>
|
|
532
|
+
{children}
|
|
533
|
+
{rim > 0 ? (
|
|
534
|
+
<div
|
|
535
|
+
style={{
|
|
536
|
+
position: "absolute",
|
|
537
|
+
inset: 0,
|
|
538
|
+
borderRadius: radius,
|
|
539
|
+
border: `${Math.max(1, width * 0.0015)}px solid ${alpha("#ffffff", 0.35 * rim)}`,
|
|
540
|
+
background: `linear-gradient(160deg, ${alpha("#ffffff", 0.16 * rim)} 0%, ${alpha("#ffffff", 0)} 38%)`,
|
|
541
|
+
}}
|
|
542
|
+
/>
|
|
543
|
+
) : null}
|
|
544
|
+
</>
|
|
545
|
+
);
|
|
546
|
+
|
|
547
|
+
// One copy: a 3D box padded for the blur's spill (it carries the projection and the depth mask), the
|
|
548
|
+
// blur inside it, then the clipped surface with the content.
|
|
549
|
+
const copyOf = (
|
|
550
|
+
key: string,
|
|
551
|
+
m: M4,
|
|
552
|
+
o: { blur: number; pad: number; mask?: CSSProperties; opacity: number; copy: boolean; body?: ReactNode },
|
|
553
|
+
) => (
|
|
554
|
+
<div
|
|
555
|
+
key={key}
|
|
556
|
+
style={{
|
|
557
|
+
position: "absolute",
|
|
558
|
+
left: 0,
|
|
559
|
+
top: 0,
|
|
560
|
+
width: width + o.pad * 2,
|
|
561
|
+
height: height + o.pad * 2,
|
|
562
|
+
transformOrigin: "0 0",
|
|
563
|
+
transform: matrix(mul(m, translate(-o.pad, -o.pad, 0))),
|
|
564
|
+
...o.mask,
|
|
565
|
+
}}
|
|
566
|
+
>
|
|
567
|
+
<div style={{ position: "absolute", left: o.pad, top: o.pad, width, height, filter: groupBlur(o.blur) }}>
|
|
568
|
+
<div style={{ ...surface, opacity: o.opacity }}>
|
|
569
|
+
<PlaneCopyCtx.Provider value={o.copy}>{o.body ?? content}</PlaneCopyCtx.Provider>
|
|
570
|
+
</div>
|
|
571
|
+
</div>
|
|
572
|
+
</div>
|
|
573
|
+
);
|
|
574
|
+
|
|
575
|
+
const layers: ReactNode[] = [];
|
|
576
|
+
|
|
577
|
+
if (shadow > 0) {
|
|
578
|
+
const sm = mul(ctx.view, model, translate(0, height * 0.06, -shadowDepth), scale(0.94), translate(-width / 2, -height / 2, 0));
|
|
579
|
+
// World px of softness, in the shadow's local px.
|
|
580
|
+
const sb = (24 + shadowDepth * 0.35) * (ctx.u / localScale(sm));
|
|
581
|
+
const pad = padFor(sb);
|
|
582
|
+
layers.push(
|
|
583
|
+
<div
|
|
584
|
+
key="shadow"
|
|
585
|
+
style={{
|
|
586
|
+
position: "absolute",
|
|
587
|
+
left: 0,
|
|
588
|
+
top: 0,
|
|
589
|
+
width: width + pad * 2,
|
|
590
|
+
height: height + pad * 2,
|
|
591
|
+
transformOrigin: "0 0",
|
|
592
|
+
transform: matrix(mul(sm, translate(-pad, -pad, 0))),
|
|
593
|
+
}}
|
|
594
|
+
>
|
|
595
|
+
<div
|
|
596
|
+
style={{
|
|
597
|
+
position: "absolute",
|
|
598
|
+
left: pad,
|
|
599
|
+
top: pad,
|
|
600
|
+
width,
|
|
601
|
+
height,
|
|
602
|
+
borderRadius: radius,
|
|
603
|
+
background: `rgba(0,0,0,${Math.min(0.85, 0.5 * shadow) * opacity})`,
|
|
604
|
+
filter: groupBlur(sb),
|
|
605
|
+
}}
|
|
606
|
+
/>
|
|
607
|
+
</div>,
|
|
608
|
+
);
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
if (reflection > 0) {
|
|
612
|
+
const rm = mul(ctx.view, model, translate(0, height + 6, 0), scale(1, -1, 1), translate(-width / 2, -height / 2, 0));
|
|
613
|
+
const rb = withExtra(1.5);
|
|
614
|
+
const mask = `linear-gradient(0deg, rgba(0,0,0,${(0.9 * reflection).toFixed(3)}) 0%, rgba(0,0,0,0) 45%)`;
|
|
615
|
+
layers.push(
|
|
616
|
+
copyOf("reflection", rm, {
|
|
617
|
+
blur: rb,
|
|
618
|
+
pad: padFor(rb),
|
|
619
|
+
mask: { maskImage: mask, WebkitMaskImage: mask },
|
|
620
|
+
opacity: opacity * 0.9,
|
|
621
|
+
copy: true,
|
|
622
|
+
body: children,
|
|
623
|
+
}),
|
|
624
|
+
);
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
// The main copy is always last and keyed "main", so its content never remounts when the focus changes
|
|
628
|
+
// (videos keep playing, loaded images stay); the blurred copies come and go under it.
|
|
629
|
+
if (focus.kind === "sharp") {
|
|
630
|
+
layers.push(copyOf("main", camModel, { blur: extra, pad: padFor(extra), opacity, copy: false }));
|
|
631
|
+
} else if (focus.kind === "uniform") {
|
|
632
|
+
const b = withExtra(focus.blur);
|
|
633
|
+
layers.push(copyOf("main", camModel, { blur: b, pad: padFor(b), opacity, copy: false }));
|
|
634
|
+
} else {
|
|
635
|
+
// Copies at geometric blur levels (each about twice the last), cross-faded along the plane's depth:
|
|
636
|
+
// each point mixes the two levels around its own blur. A screen-space blur that's even across a
|
|
637
|
+
// tilted plane is a 10× range in the plane's own px (near parts are magnified), which one blurred
|
|
638
|
+
// copy can't follow; macro close-ups need the full ladder. The preview climbs fewer rungs.
|
|
639
|
+
const { max, angle } = focus;
|
|
640
|
+
// Enough rungs to reach ~1 px, so the in-focus band resolves cleanly (capped for cost).
|
|
641
|
+
const rungs = Math.max(1, Math.min(lightPreview ? 2 : 6, Math.ceil(Math.log(Math.max(1.01, max / 0.9)) / Math.log(2.2))));
|
|
642
|
+
const levels = [0];
|
|
643
|
+
for (let i = rungs - 1; i >= 0; i--) levels.push(max / Math.pow(2.2, i));
|
|
644
|
+
const pad = padFor(withExtra(max));
|
|
645
|
+
const weights = focus.blursAcross(pad).map((b) => {
|
|
646
|
+
const w = new Array<number>(levels.length).fill(0);
|
|
647
|
+
if (b <= 0) {
|
|
648
|
+
w[0] = 1;
|
|
649
|
+
return w;
|
|
650
|
+
}
|
|
651
|
+
let i = 0;
|
|
652
|
+
while (i < levels.length - 2 && b > levels[i + 1]) i++;
|
|
653
|
+
const t = Math.min(1, Math.max(0, (b - levels[i]) / Math.max(1e-6, levels[i + 1] - levels[i])));
|
|
654
|
+
w[i] = 1 - t;
|
|
655
|
+
w[i + 1] = t;
|
|
656
|
+
return w;
|
|
657
|
+
});
|
|
658
|
+
// Stack from the blurriest copy (bottom) up to the sharp main copy (top).
|
|
659
|
+
const order = levels.map((_, i) => levels.length - 1 - i);
|
|
660
|
+
const alphas = weights.map((w) => stackAlphas(order.map((i) => w[i]), opacity));
|
|
661
|
+
order.forEach((level, k) => {
|
|
662
|
+
const isMain = level === 0;
|
|
663
|
+
layers.push(
|
|
664
|
+
copyOf(isMain ? "main" : `blur${level}`, camModel, {
|
|
665
|
+
blur: isMain ? extra : withExtra(levels[level]),
|
|
666
|
+
pad,
|
|
667
|
+
mask: maskFrom(angle, alphas.map((a) => a[k])),
|
|
668
|
+
opacity: 1,
|
|
669
|
+
copy: !isMain,
|
|
670
|
+
}),
|
|
671
|
+
);
|
|
672
|
+
});
|
|
673
|
+
}
|
|
674
|
+
return <>{layers}</>;
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
// ─── Devices and screens ─────────────────────────────────────────────────────────────────────────
|
|
678
|
+
|
|
679
|
+
// No flat MacBook: laptops are 3D (Device3D "macbook" / "macbook-pro"). A scene that still passes
|
|
680
|
+
// kind="macbook" gets a frameless screen.
|
|
681
|
+
export type DeviceKind = "none" | "browser" | "iphone" | "ipad" | "monitor";
|
|
682
|
+
|
|
683
|
+
/** Screen aspect (width / height) each device shows by default. */
|
|
684
|
+
export const DEVICE_ASPECT: Record<DeviceKind, number> = {
|
|
685
|
+
none: 16 / 10,
|
|
686
|
+
browser: 16 / 10,
|
|
687
|
+
iphone: 9 / 19.5,
|
|
688
|
+
ipad: 3 / 4,
|
|
689
|
+
monitor: 16 / 9,
|
|
690
|
+
};
|
|
691
|
+
|
|
692
|
+
interface DeviceGeometry {
|
|
693
|
+
/** Outer size for a screen of `width` world px. */
|
|
694
|
+
outer: { width: number; height: number };
|
|
695
|
+
screen: { left: number; top: number; width: number; height: number; radius: number };
|
|
696
|
+
radius: number;
|
|
697
|
+
}
|
|
698
|
+
|
|
699
|
+
/** Outer size and screen rectangle of a device whose SCREEN is `screenWidth` wide. */
|
|
700
|
+
export function deviceGeometry(kind: DeviceKind, screenWidth: number, aspect = DEVICE_ASPECT[kind]): DeviceGeometry {
|
|
701
|
+
const w = screenWidth;
|
|
702
|
+
const h = w / aspect;
|
|
703
|
+
switch (kind) {
|
|
704
|
+
case "iphone": {
|
|
705
|
+
const b = w * 0.045;
|
|
706
|
+
return {
|
|
707
|
+
outer: { width: w + b * 2, height: h + b * 2 },
|
|
708
|
+
screen: { left: b, top: b, width: w, height: h, radius: w * 0.13 },
|
|
709
|
+
radius: w * 0.16,
|
|
710
|
+
};
|
|
711
|
+
}
|
|
712
|
+
case "ipad": {
|
|
713
|
+
const b = w * 0.045;
|
|
714
|
+
return {
|
|
715
|
+
outer: { width: w + b * 2, height: h + b * 2 },
|
|
716
|
+
screen: { left: b, top: b, width: w, height: h, radius: w * 0.025 },
|
|
717
|
+
radius: w * 0.06,
|
|
718
|
+
};
|
|
719
|
+
}
|
|
720
|
+
case "monitor": {
|
|
721
|
+
const b = w * 0.012;
|
|
722
|
+
return {
|
|
723
|
+
outer: { width: w + b * 2, height: h + b * 2 },
|
|
724
|
+
screen: { left: b, top: b, width: w, height: h, radius: w * 0.002 },
|
|
725
|
+
radius: w * 0.01,
|
|
726
|
+
};
|
|
727
|
+
}
|
|
728
|
+
case "browser": {
|
|
729
|
+
const bar = w * 0.042;
|
|
730
|
+
return {
|
|
731
|
+
outer: { width: w, height: h + bar },
|
|
732
|
+
screen: { left: 0, top: bar, width: w, height: h, radius: 0 },
|
|
733
|
+
radius: w * 0.012,
|
|
734
|
+
};
|
|
735
|
+
}
|
|
736
|
+
default:
|
|
737
|
+
return { outer: { width: w, height: h }, screen: { left: 0, top: 0, width: w, height: h, radius: w * 0.012 }, radius: w * 0.012 };
|
|
738
|
+
}
|
|
739
|
+
}
|
|
740
|
+
|
|
741
|
+
export interface DeviceProps {
|
|
742
|
+
kind: DeviceKind;
|
|
743
|
+
/** Width of the SCREEN in px (the device is a little larger: see deviceGeometry). */
|
|
744
|
+
screenWidth: number;
|
|
745
|
+
/** Screen width / height (defaults per device). Match the screenshot's aspect to avoid empty space. */
|
|
746
|
+
aspect?: number;
|
|
747
|
+
/** "black" or "silver" hardware; "light" or "dark" browser chrome. */
|
|
748
|
+
finish?: "black" | "silver" | "light" | "dark";
|
|
749
|
+
/** Address shown in the browser bar. */
|
|
750
|
+
url?: string;
|
|
751
|
+
/** Screen content. Images and video should fill the width: <Img style={{ width: "100%", display: "block" }} />. */
|
|
752
|
+
children?: ReactNode;
|
|
753
|
+
screenBackground?: string;
|
|
754
|
+
}
|
|
755
|
+
|
|
756
|
+
/** A flat device frame (iPhone, iPad, monitor, browser window) around its screen content. */
|
|
757
|
+
export function Device({ kind, screenWidth, aspect, finish, url, children, screenBackground = "#ffffff" }: DeviceProps) {
|
|
758
|
+
const g = deviceGeometry(kind, screenWidth, aspect);
|
|
759
|
+
const w = screenWidth;
|
|
760
|
+
const dark = finish === "dark" || finish === "black" || (finish === undefined && kind === "iphone");
|
|
761
|
+
const metal = finish === "silver" ? "#d9dce1" : "#1d1e22";
|
|
762
|
+
const screen = (
|
|
763
|
+
<div
|
|
764
|
+
style={{
|
|
765
|
+
position: "absolute",
|
|
766
|
+
left: g.screen.left,
|
|
767
|
+
top: g.screen.top,
|
|
768
|
+
width: g.screen.width,
|
|
769
|
+
height: g.screen.height,
|
|
770
|
+
borderRadius: g.screen.radius,
|
|
771
|
+
overflow: "hidden",
|
|
772
|
+
background: screenBackground,
|
|
773
|
+
}}
|
|
774
|
+
>
|
|
775
|
+
{children}
|
|
776
|
+
</div>
|
|
777
|
+
);
|
|
778
|
+
if (kind === "browser") {
|
|
779
|
+
const bar = g.screen.top;
|
|
780
|
+
const chrome = dark ? "#202124" : "#eef0f3";
|
|
781
|
+
const pill = dark ? "#303134" : "#ffffff";
|
|
782
|
+
const ink = dark ? "#9aa0a6" : "#5f6368";
|
|
783
|
+
return (
|
|
784
|
+
<div style={{ position: "relative", width: g.outer.width, height: g.outer.height, borderRadius: g.radius, overflow: "hidden", background: chrome }}>
|
|
785
|
+
<div style={{ position: "absolute", left: 0, top: 0, right: 0, height: bar, display: "flex", alignItems: "center", gap: bar * 0.18, paddingLeft: bar * 0.45 }}>
|
|
786
|
+
{["#ff5f57", "#febc2e", "#28c840"].map((c) => (
|
|
787
|
+
<div key={c} style={{ width: bar * 0.26, height: bar * 0.26, borderRadius: "50%", background: c }} />
|
|
788
|
+
))}
|
|
789
|
+
<div
|
|
790
|
+
style={{
|
|
791
|
+
marginLeft: bar * 0.6,
|
|
792
|
+
width: w * 0.42,
|
|
793
|
+
height: bar * 0.56,
|
|
794
|
+
borderRadius: bar * 0.28,
|
|
795
|
+
background: pill,
|
|
796
|
+
display: "flex",
|
|
797
|
+
alignItems: "center",
|
|
798
|
+
paddingLeft: bar * 0.35,
|
|
799
|
+
color: ink,
|
|
800
|
+
font: `500 ${bar * 0.3}px Inter, system-ui, sans-serif`,
|
|
801
|
+
whiteSpace: "nowrap",
|
|
802
|
+
overflow: "hidden",
|
|
803
|
+
}}
|
|
804
|
+
>
|
|
805
|
+
{url ?? ""}
|
|
806
|
+
</div>
|
|
807
|
+
</div>
|
|
808
|
+
{screen}
|
|
809
|
+
</div>
|
|
810
|
+
);
|
|
811
|
+
}
|
|
812
|
+
const frameColor = kind === "iphone" || kind === "ipad" ? metal : dark ? "#111214" : "#e4e6ea";
|
|
813
|
+
return (
|
|
814
|
+
<div
|
|
815
|
+
style={{
|
|
816
|
+
position: "relative",
|
|
817
|
+
width: g.outer.width,
|
|
818
|
+
height: g.outer.height,
|
|
819
|
+
borderRadius: g.radius,
|
|
820
|
+
background: frameColor,
|
|
821
|
+
border: kind === "iphone" || kind === "ipad" ? `${w * 0.006}px solid ${finish === "silver" ? "#b7bbc1" : "#3b3c41"}` : undefined,
|
|
822
|
+
boxSizing: "border-box",
|
|
823
|
+
}}
|
|
824
|
+
>
|
|
825
|
+
{screen}
|
|
826
|
+
{kind === "iphone" ? (
|
|
827
|
+
<div
|
|
828
|
+
style={{
|
|
829
|
+
position: "absolute",
|
|
830
|
+
left: "50%",
|
|
831
|
+
top: g.screen.top + w * 0.03,
|
|
832
|
+
width: w * 0.3,
|
|
833
|
+
height: w * 0.085,
|
|
834
|
+
marginLeft: -w * 0.15,
|
|
835
|
+
borderRadius: w * 0.05,
|
|
836
|
+
background: "#000000",
|
|
837
|
+
}}
|
|
838
|
+
/>
|
|
839
|
+
) : null}
|
|
840
|
+
</div>
|
|
841
|
+
);
|
|
842
|
+
}
|
|
843
|
+
|
|
844
|
+
export interface ScreenProps extends Placement, Omit<DeviceProps, "children" | "screenWidth"> {
|
|
845
|
+
/** Width of the screen area in world px. */
|
|
846
|
+
width: number;
|
|
847
|
+
/** An image to show (asset("assets/…")). */
|
|
848
|
+
src?: string;
|
|
849
|
+
/** A screen recording to play instead (asset("assets/…")); only the sharp copy plays its sound. */
|
|
850
|
+
video?: string;
|
|
851
|
+
/** Extra <Video> props for `video` (volume, muted, loop, trimBefore, playbackRate, …). */
|
|
852
|
+
videoProps?: Omit<ComponentProps<typeof Video>, "src">;
|
|
853
|
+
/** Scrolls a tall screenshot: 0 top … 1 bottom (needs `imageAspect`). */
|
|
854
|
+
scroll?: number;
|
|
855
|
+
/** Width / height of the image, to scroll a tall page. */
|
|
856
|
+
imageAspect?: number;
|
|
857
|
+
shadow?: number;
|
|
858
|
+
shadowDepth?: number;
|
|
859
|
+
reflection?: number;
|
|
860
|
+
rim?: number;
|
|
861
|
+
dof?: boolean;
|
|
862
|
+
opacity?: number;
|
|
863
|
+
/** A light band sweeping across the glass: 0…1 is its position (leave out for none). */
|
|
864
|
+
glare?: number;
|
|
865
|
+
children?: ReactNode;
|
|
866
|
+
}
|
|
867
|
+
|
|
868
|
+
/** A screenshot or video in a device, placed in 3D: the workhorse of a product shot. */
|
|
869
|
+
export function Screen({
|
|
870
|
+
kind,
|
|
871
|
+
width,
|
|
872
|
+
aspect,
|
|
873
|
+
finish,
|
|
874
|
+
url,
|
|
875
|
+
src,
|
|
876
|
+
video,
|
|
877
|
+
videoProps,
|
|
878
|
+
scroll = 0,
|
|
879
|
+
imageAspect,
|
|
880
|
+
screenBackground,
|
|
881
|
+
shadow = 0.7,
|
|
882
|
+
shadowDepth = 70,
|
|
883
|
+
reflection = 0,
|
|
884
|
+
rim = 0,
|
|
885
|
+
dof,
|
|
886
|
+
opacity,
|
|
887
|
+
glare,
|
|
888
|
+
children,
|
|
889
|
+
...placement
|
|
890
|
+
}: ScreenProps) {
|
|
891
|
+
const g = deviceGeometry(kind, width, aspect);
|
|
892
|
+
const screenH = g.screen.height;
|
|
893
|
+
const imageH = imageAspect ? width / imageAspect : screenH;
|
|
894
|
+
const offset = Math.max(0, imageH - screenH) * Math.min(1, Math.max(0, scroll));
|
|
895
|
+
return (
|
|
896
|
+
<Plane
|
|
897
|
+
{...placement}
|
|
898
|
+
width={g.outer.width}
|
|
899
|
+
height={g.outer.height}
|
|
900
|
+
radius={g.radius}
|
|
901
|
+
shadow={shadow}
|
|
902
|
+
shadowDepth={shadowDepth}
|
|
903
|
+
reflection={reflection}
|
|
904
|
+
rim={rim}
|
|
905
|
+
dof={dof}
|
|
906
|
+
opacity={opacity}
|
|
907
|
+
>
|
|
908
|
+
<Device kind={kind} screenWidth={width} aspect={aspect} finish={finish} url={url} screenBackground={screenBackground}>
|
|
909
|
+
{src ? (
|
|
910
|
+
<Img src={src} style={{ position: "absolute", left: 0, top: -offset, width: "100%", display: "block" }} />
|
|
911
|
+
) : video ? (
|
|
912
|
+
<PlaneVideo
|
|
913
|
+
{...videoProps}
|
|
914
|
+
src={video}
|
|
915
|
+
style={{ position: "absolute", left: 0, top: -offset, width: "100%", display: "block", ...videoProps?.style }}
|
|
916
|
+
/>
|
|
917
|
+
) : (
|
|
918
|
+
children
|
|
919
|
+
)}
|
|
920
|
+
</Device>
|
|
921
|
+
{glare !== undefined ? <Glare progress={glare} /> : null}
|
|
922
|
+
</Plane>
|
|
923
|
+
);
|
|
924
|
+
}
|
|
925
|
+
|
|
926
|
+
/** Where a Screen's picture sits inside its device, relative to the device center (for Lift). */
|
|
927
|
+
export function screenRect(kind: DeviceKind, width: number, aspect?: number) {
|
|
928
|
+
const g = deviceGeometry(kind, width, aspect);
|
|
929
|
+
return {
|
|
930
|
+
x: g.screen.left + g.screen.width / 2 - g.outer.width / 2,
|
|
931
|
+
y: g.screen.top + g.screen.height / 2 - g.outer.height / 2,
|
|
932
|
+
width: g.screen.width,
|
|
933
|
+
height: g.screen.height,
|
|
934
|
+
};
|
|
935
|
+
}
|
|
936
|
+
|
|
937
|
+
export interface LiftProps {
|
|
938
|
+
/** The same image the screen shows. */
|
|
939
|
+
src: string;
|
|
940
|
+
/** The region to lift, in fractions of the screen picture: [x, y, width, height] (0…1). */
|
|
941
|
+
box: [number, number, number, number];
|
|
942
|
+
/** The screen it comes from: the Screen's kind, width and aspect, so the lift starts exactly in place. */
|
|
943
|
+
kind?: DeviceKind;
|
|
944
|
+
width: number;
|
|
945
|
+
aspect?: number;
|
|
946
|
+
/** Height of the picture when the screenshot is taller than the screen (imageAspect), and the scroll. */
|
|
947
|
+
imageAspect?: number;
|
|
948
|
+
scroll?: number;
|
|
949
|
+
/** How far it rises toward the camera (world px). 0 = flush with the screen. */
|
|
950
|
+
z?: number;
|
|
951
|
+
/** Extra scale while lifted (1.0–1.15 reads well). */
|
|
952
|
+
scale?: number;
|
|
953
|
+
/** Small extra offset or angle while lifted. */
|
|
954
|
+
x?: number;
|
|
955
|
+
y?: number;
|
|
956
|
+
tilt?: number;
|
|
957
|
+
turn?: number;
|
|
958
|
+
roll?: number;
|
|
959
|
+
radius?: number;
|
|
960
|
+
shadow?: number;
|
|
961
|
+
/** 0…1: fades the copy in (use with z for a pop-out). */
|
|
962
|
+
opacity?: number;
|
|
963
|
+
rim?: number;
|
|
964
|
+
/** Fill behind the cut-out, in case the image has transparency. */
|
|
965
|
+
background?: string;
|
|
966
|
+
}
|
|
967
|
+
|
|
968
|
+
/**
|
|
969
|
+
* A piece of the screenshot that rises out of the screen toward the camera (a card, a button, a chart):
|
|
970
|
+
* put it in the same <Group> as its <Screen>, so it starts exactly where it is on screen.
|
|
971
|
+
*/
|
|
972
|
+
export function Lift({
|
|
973
|
+
src,
|
|
974
|
+
box,
|
|
975
|
+
kind = "none",
|
|
976
|
+
width,
|
|
977
|
+
aspect,
|
|
978
|
+
imageAspect,
|
|
979
|
+
scroll = 0,
|
|
980
|
+
z = 0,
|
|
981
|
+
scale: s = 1,
|
|
982
|
+
x = 0,
|
|
983
|
+
y = 0,
|
|
984
|
+
tilt = 0,
|
|
985
|
+
turn = 0,
|
|
986
|
+
roll = 0,
|
|
987
|
+
radius,
|
|
988
|
+
shadow = 0.8,
|
|
989
|
+
opacity = 1,
|
|
990
|
+
rim = 0.4,
|
|
991
|
+
background,
|
|
992
|
+
}: LiftProps) {
|
|
993
|
+
const r = screenRect(kind, width, aspect);
|
|
994
|
+
const imageH = imageAspect ? width / imageAspect : r.height;
|
|
995
|
+
const offset = Math.max(0, imageH - r.height) * Math.min(1, Math.max(0, scroll));
|
|
996
|
+
const [bx, by, bw, bh] = box;
|
|
997
|
+
const w = bw * r.width;
|
|
998
|
+
const h = bh * r.height;
|
|
999
|
+
const cx = r.x - r.width / 2 + (bx + bw / 2) * r.width;
|
|
1000
|
+
const cy = r.y - r.height / 2 + (by + bh / 2) * r.height;
|
|
1001
|
+
return (
|
|
1002
|
+
<Plane
|
|
1003
|
+
x={cx + x}
|
|
1004
|
+
y={cy + y}
|
|
1005
|
+
z={z + 0.5}
|
|
1006
|
+
tilt={tilt}
|
|
1007
|
+
turn={turn}
|
|
1008
|
+
roll={roll}
|
|
1009
|
+
scale={s}
|
|
1010
|
+
width={w}
|
|
1011
|
+
height={h}
|
|
1012
|
+
radius={radius ?? Math.min(w, h) * 0.08}
|
|
1013
|
+
shadow={shadow * Math.min(1, z / 40)}
|
|
1014
|
+
shadowDepth={Math.max(10, z)}
|
|
1015
|
+
rim={rim}
|
|
1016
|
+
opacity={opacity}
|
|
1017
|
+
background={background}
|
|
1018
|
+
>
|
|
1019
|
+
<Img
|
|
1020
|
+
src={src}
|
|
1021
|
+
// Same sizing as the Screen's picture: full width, natural height unless imageAspect says otherwise.
|
|
1022
|
+
style={{ position: "absolute", left: -bx * r.width, top: -by * r.height - offset, width: r.width, height: imageAspect ? imageH : "auto", maxWidth: "none" }}
|
|
1023
|
+
/>
|
|
1024
|
+
</Plane>
|
|
1025
|
+
);
|
|
1026
|
+
}
|
|
1027
|
+
|
|
1028
|
+
// ─── Light and backgrounds ───────────────────────────────────────────────────────────────────────
|
|
1029
|
+
|
|
1030
|
+
/** A band of light sweeping across a plane (put it last inside a Plane or Device screen). */
|
|
1031
|
+
export function Glare({ progress, angle = 115, width = 0.35, strength = 0.35, color = "#ffffff" }: { progress: number; angle?: number; width?: number; strength?: number; color?: string }) {
|
|
1032
|
+
const p = -0.6 + progress * 2.2;
|
|
1033
|
+
const g = `linear-gradient(${angle}deg, ${alpha(color, 0)} ${((p - width / 2) * 100).toFixed(1)}%, ${alpha(color, strength)} ${(p * 100).toFixed(1)}%, ${alpha(color, 0)} ${((p + width / 2) * 100).toFixed(1)}%)`;
|
|
1034
|
+
return <div style={{ position: "absolute", inset: 0, background: g, pointerEvents: "none" }} />;
|
|
1035
|
+
}
|
|
1036
|
+
|
|
1037
|
+
export type BackdropLook = "studio" | "light" | "aurora" | "spotlight" | "mesh" | "solid";
|
|
1038
|
+
|
|
1039
|
+
/** A full-frame background for product shots. Put it first inside the Stage (it doesn't move with the camera). */
|
|
1040
|
+
export function Backdrop({
|
|
1041
|
+
look = "studio",
|
|
1042
|
+
color = "#0b0b10",
|
|
1043
|
+
accent = "#7c5cff",
|
|
1044
|
+
accent2 = "#ff5ca8",
|
|
1045
|
+
glowX = 0.5,
|
|
1046
|
+
glowY = 0.42,
|
|
1047
|
+
intensity = 1,
|
|
1048
|
+
}: {
|
|
1049
|
+
look?: BackdropLook;
|
|
1050
|
+
color?: string;
|
|
1051
|
+
accent?: string;
|
|
1052
|
+
accent2?: string;
|
|
1053
|
+
/** Where the main glow sits (fractions of the frame). Move it slowly for life. */
|
|
1054
|
+
glowX?: number;
|
|
1055
|
+
glowY?: number;
|
|
1056
|
+
intensity?: number;
|
|
1057
|
+
}) {
|
|
1058
|
+
const { width, height } = useVideoConfig();
|
|
1059
|
+
const m = Math.max(width, height);
|
|
1060
|
+
const glow = (x: number, y: number, size: number, c: string, o: number) => (
|
|
1061
|
+
<div style={glowStyle(x * width, y * height, size * m, size * m, c, o * intensity)} />
|
|
1062
|
+
);
|
|
1063
|
+
if (look === "solid") return <AbsoluteFill style={{ background: color }} />;
|
|
1064
|
+
if (look === "light")
|
|
1065
|
+
return (
|
|
1066
|
+
<AbsoluteFill style={{ background: "linear-gradient(180deg, #fbfbfd 0%, #eef0f4 60%, #e2e5eb 100%)" }}>
|
|
1067
|
+
{glow(glowX, glowY, 1.1, "#ffffff", 0.9)}
|
|
1068
|
+
{glow(glowX + 0.2, glowY + 0.3, 0.8, accent, 0.12)}
|
|
1069
|
+
</AbsoluteFill>
|
|
1070
|
+
);
|
|
1071
|
+
if (look === "aurora")
|
|
1072
|
+
return (
|
|
1073
|
+
<AbsoluteFill style={{ background: color }}>
|
|
1074
|
+
{glow(glowX - 0.25, glowY - 0.1, 1.0, accent, 0.55)}
|
|
1075
|
+
{glow(glowX + 0.3, glowY + 0.2, 1.0, accent2, 0.45)}
|
|
1076
|
+
{glow(glowX, glowY + 0.55, 0.9, accent, 0.25)}
|
|
1077
|
+
</AbsoluteFill>
|
|
1078
|
+
);
|
|
1079
|
+
if (look === "spotlight")
|
|
1080
|
+
return (
|
|
1081
|
+
<AbsoluteFill style={{ background: color }}>
|
|
1082
|
+
{glow(glowX, glowY, 0.9, "#ffffff", 0.16)}
|
|
1083
|
+
{glow(glowX, glowY, 0.45, accent, 0.22)}
|
|
1084
|
+
</AbsoluteFill>
|
|
1085
|
+
);
|
|
1086
|
+
if (look === "mesh")
|
|
1087
|
+
return (
|
|
1088
|
+
<AbsoluteFill style={{ background: `linear-gradient(135deg, ${accent} 0%, ${accent2} 100%)` }}>
|
|
1089
|
+
{glow(glowX - 0.2, glowY - 0.2, 0.9, "#ffffff", 0.35)}
|
|
1090
|
+
{glow(glowX + 0.3, glowY + 0.4, 0.9, color, 0.35)}
|
|
1091
|
+
</AbsoluteFill>
|
|
1092
|
+
);
|
|
1093
|
+
return (
|
|
1094
|
+
<AbsoluteFill style={{ background: `linear-gradient(180deg, ${color} 0%, #050507 100%)` }}>
|
|
1095
|
+
{glow(glowX, glowY, 1.1, accent, 0.28)}
|
|
1096
|
+
{glow(glowX, glowY + 0.1, 0.5, "#ffffff", 0.06)}
|
|
1097
|
+
</AbsoluteFill>
|
|
1098
|
+
);
|
|
1099
|
+
}
|
|
1100
|
+
|
|
1101
|
+
/** A floor grid that recedes with the camera (put it inside the Stage, behind the screens). */
|
|
1102
|
+
export function Floor({
|
|
1103
|
+
y = 520,
|
|
1104
|
+
size = 5000,
|
|
1105
|
+
near = 600,
|
|
1106
|
+
spacing = 120,
|
|
1107
|
+
color = "#ffffff",
|
|
1108
|
+
opacity = 0.12,
|
|
1109
|
+
glow = "#7c5cff",
|
|
1110
|
+
}: {
|
|
1111
|
+
y?: number;
|
|
1112
|
+
size?: number;
|
|
1113
|
+
/** How far the floor reaches toward the camera (world px from z = 0); it must stay in front of the lens. */
|
|
1114
|
+
near?: number;
|
|
1115
|
+
spacing?: number;
|
|
1116
|
+
color?: string;
|
|
1117
|
+
opacity?: number;
|
|
1118
|
+
glow?: string;
|
|
1119
|
+
}) {
|
|
1120
|
+
const id = `floor${useId().replace(/[^a-zA-Z0-9]/g, "")}`;
|
|
1121
|
+
// Tiles of at most ~1250 px: each one blurs with its own depth, the part at the lens drops out alone,
|
|
1122
|
+
// and no layer gets bigger than a GPU texture.
|
|
1123
|
+
const n = Math.max(1, Math.ceil(size / 1250));
|
|
1124
|
+
const tile = size / n;
|
|
1125
|
+
const z0 = near - size / 2;
|
|
1126
|
+
const count = Math.floor(size / spacing);
|
|
1127
|
+
const tiles: ReactNode[] = [];
|
|
1128
|
+
for (let j = 0; j < n; j++) {
|
|
1129
|
+
for (let i = 0; i < n; i++) {
|
|
1130
|
+
const x0 = i * tile;
|
|
1131
|
+
const y0 = j * tile;
|
|
1132
|
+
const lines: ReactNode[] = [];
|
|
1133
|
+
for (let l = 0; l <= count; l++) {
|
|
1134
|
+
const p = l * spacing;
|
|
1135
|
+
if (p >= x0 - 2 && p <= x0 + tile + 2) lines.push(<line key={`v${l}`} x1={p} y1={y0} x2={p} y2={y0 + tile} />);
|
|
1136
|
+
if (p >= y0 - 2 && p <= y0 + tile + 2) lines.push(<line key={`h${l}`} x1={x0} y1={p} x2={x0 + tile} y2={p} />);
|
|
1137
|
+
}
|
|
1138
|
+
const tid = `${id}-${i}-${j}`;
|
|
1139
|
+
tiles.push(
|
|
1140
|
+
<Plane
|
|
1141
|
+
key={tid}
|
|
1142
|
+
x={x0 + tile / 2 - size / 2}
|
|
1143
|
+
y={y}
|
|
1144
|
+
z={z0 + y0 + tile / 2 - size / 2}
|
|
1145
|
+
tilt={90}
|
|
1146
|
+
width={tile}
|
|
1147
|
+
height={tile}
|
|
1148
|
+
dof
|
|
1149
|
+
>
|
|
1150
|
+
<svg width={tile} height={tile} viewBox={`${x0} ${y0} ${tile} ${tile}`} style={{ position: "absolute", inset: 0 }}>
|
|
1151
|
+
<defs>
|
|
1152
|
+
<radialGradient id={`${tid}-fade`} gradientUnits="userSpaceOnUse" cx={size / 2} cy={size / 2} r={size / 2}>
|
|
1153
|
+
<stop offset="0%" stopColor="#ffffff" stopOpacity={1} />
|
|
1154
|
+
<stop offset="70%" stopColor="#ffffff" stopOpacity={0} />
|
|
1155
|
+
</radialGradient>
|
|
1156
|
+
<mask id={`${tid}-mask`} maskUnits="userSpaceOnUse" x={x0} y={y0} width={tile} height={tile}>
|
|
1157
|
+
<rect x={x0} y={y0} width={tile} height={tile} fill={`url(#${tid}-fade)`} />
|
|
1158
|
+
</mask>
|
|
1159
|
+
</defs>
|
|
1160
|
+
<circle cx={size / 2} cy={size / 2} r={size * 0.3} fill={glow} opacity={0.18} mask={`url(#${tid}-mask)`} />
|
|
1161
|
+
<g stroke={color} strokeOpacity={opacity} strokeWidth={2} mask={`url(#${tid}-mask)`}>
|
|
1162
|
+
{lines}
|
|
1163
|
+
</g>
|
|
1164
|
+
</svg>
|
|
1165
|
+
</Plane>,
|
|
1166
|
+
);
|
|
1167
|
+
}
|
|
1168
|
+
}
|
|
1169
|
+
return <>{tiles}</>;
|
|
1170
|
+
}
|
|
1171
|
+
|
|
1172
|
+
// ─── Small helpers for directing ────────────────────────────────────────────────────────────────
|
|
1173
|
+
|
|
1174
|
+
/** 0→1 between two frames with a named ease (for lifts, glare sweeps, text). */
|
|
1175
|
+
export function progress(frame: number, from: number, to: number, ease: EaseName = "inOut") {
|
|
1176
|
+
return EASES[ease](interpolate(frame, [from, to], [0, 1], { extrapolateLeft: "clamp", extrapolateRight: "clamp" }));
|
|
1177
|
+
}
|