Video as Texture: Mapping Screen Recordings into a 3D Device Body
Positioning: The Screen Recording is Footage, Not Animation
This distinction must be clear (see decision in remotion-frame-driven-refactor/references/recording-vs-frames.md):
- Content inside the screen: Real screen recording, captured once, serves as footage.
- Everything outside the screen: Device motion, camera zooms, lighting, transitions, text — all frame-driven.
Example: In a vertical promo video, a laptop rises from the bottom, the camera zooms in, and finally the whole device fades out with a dot-matrix dissolve — all computed from frame; only the 3491 frames of VS Code operations inside the screen are real footage (2218×1440 / 30fps / ≈143 MB).
Architecture
<Video headless muted onVideoFrame> ← Must be placed outside <ThreeCanvas>
│ videoFrame
▼
<canvas 2d> ──drawImage──► THREE.CanvasTexture ──► screen material
▲
└── needsUpdate per frame + renderer.advance()
Three Must-Hit Pitfalls
1. The Player Must Be Placed Outside <ThreeCanvas>
If placed inside r3f's Canvas subtree, during export onVideoFrame never fires — the screen remains black; Studio preview may appear normal, leading you to believe everything is fine. Use <Sequence layout="none"> at the layout level to avoid inserting extra DOM nodes.
2. Canvas Size Must Only Be Initialized Once
if (surface.canvas.width < 16) { // Only set once
surface.canvas.width = width;
surface.canvas.height = height;
}
Assigning to canvas.width clears the contents. If drawImage fails on any frame due to timing issues (e.g., a closed VideoFrame), the canvas becomes completely black — the symptom is "the screen flickers between black and normal frames."
3. Synchronous Redraw During Export
if (isRendering) {
rendererRef.current?.advance(); // Don't wait for the next frame; the snapshot arrives immediately
} else {
rendererRef.current?.invalidate(); // Studio preview
}
Call advance() immediately after drawImage, otherwise the snapshot will capture the previous frame's content.
The argument to advance() is "clock time" – do not pass performance.now() — rendering the same frame twice will yield different values, and the scene will no longer be f(frame). Convert using the frame number: advance((frame / fps) * 1000) (already implemented in assets/useScreenVideoTexture.tsx). Likewise, if there is any useFrame((state, delta) => ...) reading delta, change it to use useCurrentFrame().
Complete Scaffold
See assets/useScreenVideoTexture.tsx (texture + per-frame drawing) and assets/useScreenDecks.tsx (two player segments: preload first frame + variable-speed playback). Mounting method:
const { texture, onVideoFrame, rendererRef } = useScreenVideoTexture();
<ThreeCanvas frameloop="never" ...>
<RendererBridge rendererRef={rendererRef} />
<mesh>
<planeGeometry args={[16, 10]} />
<meshBasicMaterial map={texture} toneMapped={false} />
</mesh>
</ThreeCanvas>
{/* Player outside the canvas */}
<ScreenDecks
src={staticFile("screen/screen.mp4")}
startFrame={430} // Which composition frame to start playing
playbackRate={1.5} // Must match the variable speed of the timeline
onVideoFrame={onVideoFrame}
/>
Detailed Checklist
| Item | Approach |
|---|---|
| Preload first frame | Before playback, freeze a segment with <Freeze frame={0}> so the screen already has an image during the rising phase |
| Variable speed | Use the same constant for playbackRate and the composition's time remapping, otherwise audio/video and subtitles will desync |
| Freeze at the end | If the video is shorter than the composition: let the player run until the composition ends; the player clamps to the last frame; do not use <Freeze> to freeze |
| Tilt blur | texture.anisotropy = gl.capabilities.getMaxAnisotropy(); use mipmap for minFilter, Linear for magFilter |
| Colors | texture.colorSpace = THREE.SRGBColorSpace, material toneMapped={false}, renderer NoToneMapping |
| Flickering | First check the three pitfalls above; if still flickering, use chunked rendering (see remotion-chunked-export) |
| Footage size | The 143 MB screen recording in the example overwhelmed concurrent decoding (40% black frames). Compress if possible, or use chunked export as a safety net (see remotion-chunked-export) |
When NOT to Use This Approach
- The screen content is actually a web page / chart / code: Render it frame-driven with the DOM — don't screen-record; it will be blurry and non-editable.
- You just want to "place a video in the frame as a picture-in-picture": Use
<Video>or<OffthreadVideo>directly; no 3D is needed. - You need interactive fake UI inside the screen: Rendering with React and then mapping it as a texture is feasible, but do not screen-record.
Accompanying Assets
| File | Purpose |
|---|---|
assets/useScreenVideoTexture.tsx | canvas → CanvasTexture, includes size initialization only once, failsafe to keep the previous frame |
assets/useScreenDecks.tsx | Preload first frame + variable-speed playback (two player segments) |