π₯ Make videos programmatically with React
Docs: Using video as a texture in Three.js
Jonny Burgeraa1a88f3b97e
diff --git a/packages/docs/docs/videos/as-threejs-texture.mdx b/packages/docs/docs/videos/as-threejs-texture.mdxnew file mode 100644index 0000000000..7057f1be6c--- /dev/null+++ b/packages/docs/docs/videos/as-threejs-texture.mdx@@ -0,0 +1,113 @@+---+image: /generated/articles-docs-videos-as-threejs-texture.png+title: Using a video as a texture in Three.js+crumb: 'Videos'+sidebar_label: As Three.js texture+---++If you want to embed a video as a texture in Three.js, you have multiple options.++## Using `@remotion/media`<AvailableFrom v="4.0.387" />++We recommend that you mount a [`<Video>`](/docs/media/video) tag in [`headless`](/docs/media/video#headless) mode and use the [`onVideoFrame`](/docs/media/video#onvideoframe) prop to update the Three.js texture whenever a new frame is being drawn.++```tsx twoslash title="VideoTexture.tsx"+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>+ );+};+```++### Notes++- By using the [`headless`](/docs/media/video#headless) prop, nothing will be returned by the `<Video>` tag, so it can be mounted within a [`<ThreeCanvas>`](/docs/three-canvas) without affecting the rendering.+- For preview and rendering, separate approaches are needed to invalida the texture when it is being updated. See++## Examples++- Look at the source code of the [React Three Fiber template](/templates/three) for a practical example.+- The above example can be found in the [Remotion testbed](https://github.com/remotion-dev/remotion/tree/main/packages/example/src/VideoTexture.tsx).++## Using `<OffthreadVideo>`++_deprecated in favor of using `@remotion/media`_++You can use the [`useOffthreadVideoTexture()`](/docs/use-offthread-video-texture) hook from [`@remotion/three`](/docs/three-canvas) to get a texture from a video.++**Drawbacks:**++- It requires the whole video to be downloaded to disk first before frames can be extracted+- It does not work in [client-side rendering](/docs/client-side-rendering)+- It creates a new texture for each frame, which is less efficient than using `@remotion/media`++This API is therefore **deprecated** in favor of using the recommended approach mentioned above.++## Using `<Html5Video>`++_deprecated in favor of using `@remotion/media`_++You can use the [`useVideoTexture()`](/docs/use-video-texture) hook from [`@remotion/three`](/docs/three-canvas) to get a texture from a video.++It has all the drawbacks of the [`<Html5Video>`](/docs/video-tags) tag and is therefore **deprecated** in favor of using the recommended approach mentioned above.++## See also++- [`@remotion/three`](/docs/three)+- [React Three Fiber template](/templates/three)+- [Comparison of video tags](/docs/video-tags)+- [`<Video>`β `headless` prop](/docs/media/video)diff --git a/packages/docs/sidebars.ts b/packages/docs/sidebars.tsindex afaec7348d..995a8e4e9e 100644--- a/packages/docs/sidebars.ts+++ b/packages/docs/sidebars.ts@@ -842,6 +842,7 @@ const sidebars: SidebarsConfig = {'miscellaneous/snippets/freeze-portions','video-tags','miscellaneous/snippets/hls',+ 'videos/as-threejs-texture',],},{diff --git a/packages/docs/src/data/articles.ts b/packages/docs/src/data/articles.tsindex 4a245a27f3..e873dcc8ad 100644--- a/packages/docs/src/data/articles.ts+++ b/packages/docs/src/data/articles.ts@@ -6376,6 +6376,15 @@ export const articles = [noAi: false,slug: 'video-uploads',},+ {+ id: 'videos/as-threejs-texture',+ title: 'Using a video as a texture in Three.js',+ relativePath: 'docs/videos/as-threejs-texture.mdx',+ compId: 'articles-docs-videos-as-threejs-texture',+ crumb: 'Videos',+ noAi: false,+ slug: 'videos/as-threejs-texture',+ },{id: 'videos/index',title: 'Embedding videos into Remotion',diff --git a/packages/docs/static/generated/articles-docs-videos-as-threejs-texture.png b/packages/docs/static/generated/articles-docs-videos-as-threejs-texture.pngnew file mode 100644index 0000000000..1afd99312aBinary files /dev/null and b/packages/docs/static/generated/articles-docs-videos-as-threejs-texture.png differ