Skip to content

πŸŽ₯ Make videos programmatically with React

Docs: Fix <ThreeCanvas> video texture example to use advance() during rendering

Igor Samokhovets31a8eaef96d5
diff --git a/packages/docs/docs/three-canvas.mdx b/packages/docs/docs/three-canvas.mdx
index dc30435474..a4c9035f02 100644
--- a/packages/docs/docs/three-canvas.mdx
+++ b/packages/docs/docs/three-canvas.mdx
@@ -48,6 +48,12 @@ const ThreeBasic: React.FC = () => {
export default ThreeBasic;
```
+## `frameloop` behavior during rendering
+
+During rendering, `<ThreeCanvas>` overrides the `frameloop` prop to `'never'` regardless of what you pass. This means the Three.js scene will not re-render on its own β€” it is only re-rendered on demand via `advance()`.
+
+If you update textures asynchronously (e.g. from a [`<Video>`](/docs/media/video) `onVideoFrame` callback), you must call `advance(performance.now())` instead of `invalidate()` to synchronously re-render the scene before the frame is captured. See [Using a video as a texture](/docs/videos/as-threejs-texture) for a full example.
+
## Note on `<Sequence>`
A [`<Sequence>`](/docs/sequence) by default will return a `<div>` component which is not allowed inside a `<ThreeCanvas>`. To avoid an error, pass `layout="none"` to `<Sequence>`.
diff --git a/packages/docs/docs/videos/as-threejs-texture.mdx b/packages/docs/docs/videos/as-threejs-texture.mdx
index 7057f1be6c..f578520470 100644
--- a/packages/docs/docs/videos/as-threejs-texture.mdx
+++ b/packages/docs/docs/videos/as-threejs-texture.mdx
@@ -16,7 +16,7 @@ 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 {useRemotionEnvironment, useVideoConfig} from 'remotion';
import {CanvasTexture} from 'three';
const videoSrc = 'https://remotion.media/video.mp4';
@@ -36,19 +36,29 @@ const Inner: React.FC = () => {
return {canvas, context, texture};
});
- const {invalidate} = useThree();
+ const {invalidate, advance} = useThree();
+ const {isRendering} = useRemotionEnvironment();
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();
+ if (isRendering) {
+ // ThreeCanvas's ManualFrameRenderer already calls advance() in a
+ // useEffect on frame change, but video frame extraction is async
+ // (BroadcastChannel round-trip) and resolves after that useEffect.
+ // So by the time onVideoFrame fires, the scene was already rendered
+ // with the stale texture. We need a second advance() here to
+ // re-render the scene now that the texture is actually updated.
+ advance(performance.now());
+ } else {
+ // During preview with the default frameloop='always', the texture
+ // is picked up automatically. This is only needed if
+ // frameloop='demand' is passed to <ThreeCanvas>.
+ invalidate();
+ }
},
- [canvasStuff.context, canvasStuff.texture, invalidate],
+ [canvasStuff.context, canvasStuff.texture, invalidate, advance, isRendering],
);
return (
@@ -76,7 +86,8 @@ export const RemotionMediaVideoTexture: React.FC = () => {
### 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
+- During rendering, [`<ThreeCanvas>`](/docs/three-canvas) sets `frameloop='never'`, which means the scene is only re-rendered on demand. Use `advance()` inside `onVideoFrame` to synchronously re-render the scene before the screenshot is taken. Using `invalidate()` would only schedule an asynchronous re-render, which can lead to stale frames β€” especially with concurrency greater than 1.
+- During preview, `invalidate()` is sufficient because the frame loop runs continuously.
## Examples
diff --git a/packages/example/src/VideoTexture.tsx b/packages/example/src/VideoTexture.tsx
index 33cd0dac10..1624f071ae 100644
--- a/packages/example/src/VideoTexture.tsx
+++ b/packages/example/src/VideoTexture.tsx
@@ -2,7 +2,7 @@ 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 {useRemotionEnvironment, useVideoConfig} from 'remotion';
import {CanvasTexture} from 'three';
const videoSrc = 'https://remotion.media/video.mp4';
@@ -22,19 +22,35 @@ const Inner: React.FC = () => {
return {canvas, context, texture};
});
- const {invalidate} = useThree();
+ const {invalidate, advance} = useThree();
+ const {isRendering} = useRemotionEnvironment();
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();
+ if (isRendering) {
+ // ThreeCanvas's ManualFrameRenderer already calls advance() in a
+ // useEffect on frame change, but video frame extraction is async
+ // (BroadcastChannel round-trip) and resolves after that useEffect.
+ // So by the time onVideoFrame fires, the scene was already rendered
+ // with the stale texture. We need a second advance() here to
+ // re-render the scene now that the texture is actually updated.
+ advance(performance.now());
+ } else {
+ // During preview with the default frameloop='always', the texture
+ // is picked up automatically. This is only needed if
+ // frameloop='demand' is passed to <ThreeCanvas>.
+ invalidate();
+ }
},
- [canvasStuff.context, canvasStuff.texture, invalidate],
+ [
+ canvasStuff.context,
+ canvasStuff.texture,
+ invalidate,
+ advance,
+ isRendering,
+ ],
);
return (