Skip to content

🎥 Make videos programmatically with React

use offthreadvideotexture for three

Jonny Burger3bcad19ac05c
diff --git a/packages/template-three/src/Phone.tsx b/packages/template-three/src/Phone.tsx
index 75d5a85045..57c29fbd11 100644
--- a/packages/template-three/src/Phone.tsx
+++ b/packages/template-three/src/Phone.tsx
@@ -1,7 +1,7 @@
import { useThree } from "@react-three/fiber";
import React, { useEffect, useMemo } from "react";
import { interpolate, spring, useCurrentFrame, useVideoConfig } from "remotion";
-import { VideoTexture } from "three";
+import { Texture } from "three";
import {
CAMERA_DISTANCE,
getPhoneLayout,
@@ -12,7 +12,7 @@ import { roundedRect } from "./helpers/rounded-rectangle";
import { RoundedBox } from "./RoundedBox";
export const Phone: React.FC<{
- readonly videoTexture: VideoTexture | null;
+ readonly videoTexture: Texture | null;
readonly aspectRatio: number;
readonly baseScale: number;
readonly phoneColor: string;
diff --git a/packages/template-three/src/Scene.tsx b/packages/template-three/src/Scene.tsx
index b1626466c8..d504533cd2 100644
--- a/packages/template-three/src/Scene.tsx
+++ b/packages/template-three/src/Scene.tsx
@@ -1,11 +1,12 @@
import { staticFile } from "remotion";
import { getVideoMetadata, VideoMetadata } from "@remotion/media-utils";
-import { ThreeCanvas, useVideoTexture } from "@remotion/three";
+import { ThreeCanvas } from "@remotion/three";
import React, { useEffect, useRef, useState } from "react";
import { AbsoluteFill, useVideoConfig, Video } from "remotion";
import { Phone } from "./Phone";
import { z } from "zod";
import { zColor } from "@remotion/zod-types";
+import { useTexture } from "./use-texture";
const container: React.CSSProperties = {
backgroundColor: "white",
@@ -41,7 +42,8 @@ export const Scene: React.FC<
.catch((err) => console.log(err));
}, [videoSrc]);
- const texture = useVideoTexture(videoRef);
+ const texture = useTexture(videoSrc, videoRef);
+
return (
<AbsoluteFill style={container}>
<Video ref={videoRef} src={videoSrc} style={videoStyle} />
diff --git a/packages/template-three/src/use-texture.ts b/packages/template-three/src/use-texture.ts
new file mode 100644
index 0000000000..f98ec2d7ca
--- /dev/null
+++ b/packages/template-three/src/use-texture.ts
@@ -0,0 +1,16 @@
+/* eslint-disable react-hooks/rules-of-hooks */
+import { useOffthreadVideoTexture, useVideoTexture } from "@remotion/three";
+import { getRemotionEnvironment } from "remotion";
+
+export const useTexture = (
+ src: string,
+ videoRef: React.RefObject<HTMLVideoElement | null>,
+) => {
+ if (getRemotionEnvironment().isRendering) {
+ return useOffthreadVideoTexture({
+ src,
+ });
+ }
+
+ return useVideoTexture(videoRef);
+};