🎥 Make videos programmatically with React
Add video texture example to testbed
Jonny Burger3ac887bff560
diff --git a/packages/example/src/Root.tsx b/packages/example/src/Root.tsxindex bc16df0387..e317454f96 100644--- a/packages/example/src/Root.tsx+++ b/packages/example/src/Root.tsx@@ -149,6 +149,7 @@ import {TransitionRounding} from './TransitionRounding';import {TriangleComp} from './Triangle';import {VideoTestingPlayback} from './VideoTesting/playback';import {VideoTestingTrim} from './VideoTesting/trim';+import {RemotionMediaVideoTexture} from './VideoTexture';import {VisualControls} from './VisualControls';import {VoiceVisualization} from './voice-visualization';import {WhisperWeb} from './WhisperWeb';@@ -1791,6 +1792,14 @@ export const Index: React.FC = () => {fps={30}durationInFrames={100}/>+ <Composition+ id="media-video-texture"+ component={RemotionMediaVideoTexture}+ width={1280}+ height={720}+ fps={30}+ durationInFrames={600}+ /><ChangingTrimBeforeValue /></>);diff --git a/packages/example/src/VideoTexture.tsx b/packages/example/src/VideoTexture.tsxnew file mode 100644index 0000000000..33cd0dac10--- /dev/null+++ b/packages/example/src/VideoTexture.tsx@@ -0,0 +1,68 @@+import {useThree} from '@react-three/fiber';+import {Video} from '@remotion/media';+import {ThreeCanvas} from '@remotion/three';+import React, {useCallback, useState} from 'react';+import {useVideoConfig} from 'remotion';+import {CanvasTexture} from 'three';++const videoSrc = 'https://remotion.media/video.mp4';++const videoWidth = 1920;+const videoHeight = 1080;+const aspectRatio = videoWidth / videoHeight;+const scale = 3;+const planeHeight = scale;+const planeWidth = aspectRatio * scale;++const Inner: React.FC = () => {+ const [canvasStuff] = useState(() => {+ const canvas = new OffscreenCanvas(videoWidth, videoHeight);+ const context = canvas.getContext('2d')!;+ const texture = new CanvasTexture(canvas);+ return {canvas, context, texture};+ });++ const {invalidate} = useThree();++ const onVideoFrame = useCallback(+ (frame: CanvasImageSource) => {+ canvasStuff.context.drawImage(frame, 0, 0, videoWidth, videoHeight);+ // This is needed during preview - the frame loop synchronizes with the monitor refresh rate+ // By setting this, we signal that the texture has been updated+ canvasStuff.texture.needsUpdate = true;+ // This is needed during rendering - the frame loop is only triggered on demand.+ // We have to call "invalidate()" to trigger a new frame loop.+ invalidate();+ },+ [canvasStuff.context, canvasStuff.texture, invalidate],+ );++ return (+ <>+ <Video src={videoSrc} onVideoFrame={onVideoFrame} muted headless />+ <mesh>+ <planeGeometry args={[planeWidth, planeHeight]} />+ <meshBasicMaterial+ color={0xffffff}+ toneMapped={false}+ map={canvasStuff.texture}+ />+ </mesh>+ </>+ );+};++export const RemotionMediaVideoTexture: React.FC = () => {+ const {width, height} = useVideoConfig();++ return (+ <ThreeCanvas+ style={{backgroundColor: 'white'}}+ linear+ width={width}+ height={height}+ >+ <Inner />+ </ThreeCanvas>+ );+};diff --git a/packages/template-three/src/Phone.tsx b/packages/template-three/src/Phone.tsxindex 17f3c34a65..a9499b0562 100644--- a/packages/template-three/src/Phone.tsx+++ b/packages/template-three/src/Phone.tsx@@ -14,7 +14,7 @@ import { RoundedBox } from "./RoundedBox";export const Phone: React.FC<{readonly videoTexture: Texture;readonly phoneColor: string;- phoneLayout: PhoneLayout;+ readonly phoneLayout: PhoneLayout;}> = ({ videoTexture, phoneColor, phoneLayout }) => {const frame = useCurrentFrame();const { fps, durationInFrames } = useVideoConfig();