Skip to content

🎥 Make videos programmatically with React

Update Three.js template to use new best practices

Jonny Burger60ce2f5b5953
diff --git a/packages/template-three/src/Phone.tsx b/packages/template-three/src/Phone.tsx
index a9499b0562..7a5d620404 100644
--- a/packages/template-three/src/Phone.tsx
+++ b/packages/template-three/src/Phone.tsx
@@ -1,7 +1,8 @@
import { useThree } from "@react-three/fiber";
-import React, { useEffect, useMemo } from "react";
+import React, { useCallback, useEffect, useMemo, useState } from "react";
import { interpolate, spring, useCurrentFrame, useVideoConfig } from "remotion";
-import { Texture } from "three";
+import { Video } from "@remotion/media";
+import { CanvasTexture, Texture } from "three";
import {
CAMERA_DISTANCE,
PHONE_CURVE_SEGMENTS,
@@ -10,12 +11,14 @@ import {
} from "./helpers/layout";
import { roundedRect } from "./helpers/rounded-rectangle";
import { RoundedBox } from "./RoundedBox";
+import { MediabunnyMetadata } from "./helpers/get-media-metadata";
export const Phone: React.FC<{
- readonly videoTexture: Texture;
readonly phoneColor: string;
readonly phoneLayout: PhoneLayout;
-}> = ({ videoTexture, phoneColor, phoneLayout }) => {
+ readonly mediaMetadata: MediabunnyMetadata;
+ readonly videoSrc: string;
+}> = ({ phoneColor, phoneLayout, mediaMetadata, videoSrc }) => {
const frame = useCurrentFrame();
const { fps, durationInFrames } = useVideoConfig();
@@ -76,12 +79,47 @@ export const Phone: React.FC<{
phoneLayout.screen.width,
]);
+ const [canvasTexture] = useState(() => {
+ return new OffscreenCanvas(
+ mediaMetadata.dimensions.width,
+ mediaMetadata.dimensions.height,
+ );
+ });
+
+ const [context] = useState(() => {
+ const context = canvasTexture.getContext("2d");
+ if (!context) {
+ throw new Error("Failed to get context");
+ }
+ return context;
+ });
+
+ const [texture] = useState<Texture>(() => {
+ const tex = new CanvasTexture(canvasTexture);
+ tex.repeat.y = 1 / phoneLayout.screen.height;
+ tex.repeat.x = 1 / phoneLayout.screen.width;
+ return tex;
+ });
+
+ const { invalidate } = useThree();
+
+ const onVideoFrame = useCallback(
+ (frame: CanvasImageSource) => {
+ context.drawImage(frame, 0, 0);
+ texture.needsUpdate = true;
+ invalidate();
+ },
+ [context, texture, invalidate],
+ );
+
return (
<group
scale={entranceAnimation}
rotation={[0, rotateY, 0]}
position={[0, translateY, 0]}
>
+ <Video src={videoSrc} onVideoFrame={onVideoFrame} headless muted />
+
<RoundedBox
radius={phoneLayout.phone.radius}
depth={phoneLayout.phone.thickness}
@@ -94,11 +132,7 @@ export const Phone: React.FC<{
</RoundedBox>
<mesh position={phoneLayout.screen.position}>
<shapeGeometry args={[screenGeometry]} />
- <meshBasicMaterial
- color={0xffffff}
- toneMapped={false}
- map={videoTexture}
- />
+ <meshBasicMaterial color={0xffffff} toneMapped={false} map={texture} />
</mesh>
</group>
);
diff --git a/packages/template-three/src/Scene.tsx b/packages/template-three/src/Scene.tsx
index 700c97552e..eef87004f8 100644
--- a/packages/template-three/src/Scene.tsx
+++ b/packages/template-three/src/Scene.tsx
@@ -1,23 +1,16 @@
-import { ThreeCanvas, ThreeCanvasRef } from "@remotion/three";
-import React, { useCallback, useMemo, useRef, useState } from "react";
+import { ThreeCanvas } from "@remotion/three";
+import React, { useMemo } from "react";
import { AbsoluteFill, useVideoConfig } from "remotion";
import { Phone } from "./Phone";
import { z } from "zod";
import { zColor } from "@remotion/zod-types";
import { MediabunnyMetadata } from "./helpers/get-media-metadata";
-import { Video } from "@remotion/media";
-import { CanvasTexture, Texture } from "three";
import { getPhoneLayout } from "./helpers/layout";
const container: React.CSSProperties = {
backgroundColor: "white",
};
-const videoStyle: React.CSSProperties = {
- position: "absolute",
- opacity: 0,
-};
-
export const myCompSchema = z.object({
phoneColor: zColor(),
deviceType: z.enum(["phone", "tablet"]),
@@ -35,29 +28,12 @@ export const Scene: React.FC<
const { width, height } = useVideoConfig();
if (!mediaMetadata) {
- throw new Error("Media metadata is not supported yet");
+ throw new Error("Media metadata is not available");
}
if (!videoSrc) {
- throw new Error("Video source is not supported yet");
+ throw new Error("Video source is not available");
}
- const [canvasTexture] = useState(() => {
- return new OffscreenCanvas(
- mediaMetadata.dimensions.width,
- mediaMetadata.dimensions.height,
- );
- });
-
- const [context] = useState(() => {
- const context = canvasTexture.getContext("2d");
- if (!context) {
- throw new Error("Failed to get context");
- }
- return context;
- });
-
- const threeCanvasRef = useRef<ThreeCanvasRef>(null);
-
const aspectRatio = useMemo(
() => mediaMetadata.dimensions.width / mediaMetadata.dimensions.height,
[mediaMetadata.dimensions.width, mediaMetadata.dimensions.height],
@@ -68,32 +44,16 @@ export const Scene: React.FC<
[aspectRatio, baseScale],
);
- const [texture] = useState<Texture>(() => {
- const tex = new CanvasTexture(canvasTexture);
- tex.repeat.y = 1 / layout.screen.height;
- tex.repeat.x = 1 / layout.screen.width;
- return tex;
- });
-
- const onVideoFrame = useCallback(
- (frame: CanvasImageSource) => {
- context.drawImage(frame, 0, 0);
- texture.needsUpdate = true;
- threeCanvasRef.current?.invalidate();
- },
- [context, texture],
- );
-
return (
<AbsoluteFill style={container}>
- <Video src={videoSrc} style={videoStyle} onVideoFrame={onVideoFrame} />
- <ThreeCanvas ref={threeCanvasRef} linear width={width} height={height}>
+ <ThreeCanvas linear width={width} height={height}>
<ambientLight intensity={1.5} color={0xffffff} />
<pointLight position={[10, 10, 0]} />
<Phone
phoneColor={phoneColor}
phoneLayout={layout}
- videoTexture={texture}
+ mediaMetadata={mediaMetadata}
+ videoSrc={videoSrc}
/>
</ThreeCanvas>
</AbsoluteFill>