Skip to content

🎥 Make videos programmatically with React

consciously export symbols

Jonny Burgere7e09967e893
diff --git a/packages/three/src/ThreeVideo.tsx b/packages/three/src/ThreeVideo.tsx
index 27a36869f5..165efbd071 100644
--- a/packages/three/src/ThreeVideo.tsx
+++ b/packages/three/src/ThreeVideo.tsx
@@ -1,59 +1,65 @@
-import React from 'react';
import { MeshProps, useThree } from '@react-three/fiber';
-
-import useVideoTexture, { UseVideoTextureOptions } from './useVideoTexture';
+import React from 'react';
import { ShaderLib } from 'three';
-
+import useVideoTexture, { UseVideoTextureOptions } from './useVideoTexture';
import './utils/TexturedShaderMaterial';
-const vShaderFullViewport = ShaderLib.basic.vertexShader
- .replace('#include <project_vertex>',
- `
+const vShaderFullViewport = ShaderLib.basic.vertexShader.replace(
+ '#include <project_vertex>',
+ `
vec4 mvPosition = vec4( transformed, 1.0 );
#ifdef USE_INSTANCING
mvPosition = instanceMatrix * mvPosition;
#endif
mvPosition = modelViewMatrix * mvPosition;
gl_Position = vec4(position * 2.0, 1.0);
-`);
+`
+);
-export interface ThreeVideoProps extends MeshProps, Pick<UseVideoTextureOptions, 'src' | 'startFrom'> {
- fullViewport?: boolean;
- ignoreDepth?: boolean;
- videoProps?: UseVideoTextureOptions;
+export interface ThreeVideoProps
+ extends MeshProps,
+ Pick<UseVideoTextureOptions, 'src' | 'startFrom'> {
+ fullViewport?: boolean;
+ ignoreDepth?: boolean;
+ videoProps?: UseVideoTextureOptions;
}
-const ThreeVideo = React.forwardRef((props: ThreeVideoProps, ref: React.ForwardedRef<React.ComponentType<'mesh'>>) => {
- const {
- fullViewport,
- ignoreDepth,
- src,
- startFrom,
- videoProps,
- ...rest
- } = props;
- const [size] = useThree(s => [s.size] as const);
- const videoTexture = useVideoTexture({ src, startFrom, ...videoProps });
- return (
- <mesh
- ref={ref}
- scale={[size.width, size.height, 1]}
- renderOrder={ignoreDepth ? -2 : undefined}
- {...rest}
- >
- <planeGeometry args={[1, 1]} />
- {videoTexture && (
- <texturedShaderMaterial
- vertexShader={fullViewport ? vShaderFullViewport : ShaderLib.basic.vertexShader}
- fragmentShader={ShaderLib.basic.fragmentShader}
- uniforms={ShaderLib.basic.uniforms}
- map={videoTexture}
- depthTest={!ignoreDepth}
- depthWrite={!ignoreDepth}
- />
- )}
- </mesh>
- );
-});
-
-export default ThreeVideo;
+const ThreeVideo = React.forwardRef(
+ (
+ props: ThreeVideoProps,
+ ref: React.ForwardedRef<React.ComponentType<'mesh'>>
+ ) => {
+ const {
+ fullViewport,
+ ignoreDepth,
+ src,
+ startFrom,
+ videoProps,
+ ...rest
+ } = props;
+ const [size] = useThree((s) => [s.size] as const);
+ const videoTexture = useVideoTexture({ src, startFrom, ...videoProps });
+ return (
+ <mesh
+ ref={ref}
+ scale={[size.width, size.height, 1]}
+ renderOrder={ignoreDepth ? -2 : undefined}
+ {...rest}
+ >
+ <planeGeometry args={[1, 1]} />
+ {videoTexture && (
+ <texturedShaderMaterial
+ vertexShader={
+ fullViewport ? vShaderFullViewport : ShaderLib.basic.vertexShader
+ }
+ fragmentShader={ShaderLib.basic.fragmentShader}
+ uniforms={ShaderLib.basic.uniforms}
+ map={videoTexture}
+ depthTest={!ignoreDepth}
+ depthWrite={!ignoreDepth}
+ />
+ )}
+ </mesh>
+ );
+ }
+);
diff --git a/packages/three/src/index.ts b/packages/three/src/index.ts
index b3ff88f81c..861c4db628 100644
--- a/packages/three/src/index.ts
+++ b/packages/three/src/index.ts
@@ -1,9 +1,9 @@
export { SwirlEffect, SwirlEffectOptions } from './effects/SwirlEffect';
export { RemotionThreeCanvas } from './RemotionThreeCanvas';
export { useRemotionThreeDomNode } from './RemotionThreeContext';
-export * from './ThreeVideo';
-export { default as ThreeVideo } from './ThreeVideo';
-export * from './useVideoTexture';
-export { default as useVideoTexture } from './useVideoTexture';
-export * from './utils/TexturedShaderMaterial';
-export { default as TexturedShaderMaterial } from './utils/TexturedShaderMaterial';
+export { ThreeVideo, ThreeVideoProps } from './ThreeVideo';
+export { useVideoTexture, UseVideoTextureOptions } from './useVideoTexture';
+export {
+ TexturedShaderMaterial,
+ TexturedShaderMaterialParameters,
+} from './utils/TexturedShaderMaterial';
diff --git a/packages/three/src/useVideoTexture.tsx b/packages/three/src/useVideoTexture.tsx
index c36e9663d1..c94241ab1c 100644
--- a/packages/three/src/useVideoTexture.tsx
+++ b/packages/three/src/useVideoTexture.tsx
@@ -1,69 +1,74 @@
+import { useThree } from '@react-three/fiber';
import React from 'react';
import { continueRender, delayRender, useCurrentFrame, Video } from 'remotion';
-import { useThree } from '@react-three/fiber';
import { VideoTexture } from 'three';
-
import { useRemotionThreeDomNode } from './RemotionThreeContext';
declare global {
- interface HTMLVideoElement {
- requestVideoFrameCallback?: (cb: () => void) => void;
- }
+ interface HTMLVideoElement {
+ requestVideoFrameCallback?: (cb: () => void) => void;
+ }
}
export type UseVideoTextureOptions = React.ComponentProps<typeof Video>;
-export default function useVideoTexture(videoProps: UseVideoTextureOptions) {
- const render = useThree(s => s.invalidate);
+export const useVideoTexture = (videoProps: UseVideoTextureOptions) => {
+ const render = useThree((s) => s.invalidate);
- const videoRef = React.useRef<HTMLVideoElement | null>(null);
- const [videoTexture, setVideoTexture] = React.useState<VideoTexture | null>(null);
+ const videoRef = React.useRef<HTMLVideoElement | null>(null);
+ const [videoTexture, setVideoTexture] = React.useState<VideoTexture | null>(
+ null
+ );
- const videoWrapperRef = React.useCallback((wrapper: HTMLDivElement | null) => {
- if (wrapper) {
- const video = videoRef.current = wrapper.firstChild as HTMLVideoElement;
- video.addEventListener('loadeddata', () => {
- const vt = new VideoTexture(video);
- video.width = video.videoWidth;
- video.height = video.videoHeight;
- vt.needsUpdate = true;
- setVideoTexture(vt);
- });
- } else {
- videoRef.current = null;
- }
- }, []);
+ const videoWrapperRef = React.useCallback(
+ (wrapper: HTMLDivElement | null) => {
+ if (wrapper) {
+ const video = (videoRef.current = wrapper.firstChild as HTMLVideoElement);
+ video.addEventListener('loadeddata', () => {
+ const vt = new VideoTexture(video);
+ video.width = video.videoWidth;
+ video.height = video.videoHeight;
+ vt.needsUpdate = true;
+ setVideoTexture(vt);
+ });
+ } else {
+ videoRef.current = null;
+ }
+ },
+ []
+ );
- const frame = useCurrentFrame();
- React.useEffect(() => {
- const video = videoRef.current;
- if (!video || !videoTexture) {
- return;
- }
- if (!video.requestVideoFrameCallback) {
- throw new Error('HTMLVideoElement.requestVideoFrameCallback not supported');
- }
- const handle = delayRender();
- // wait for the video to render its next frame
- video.requestVideoFrameCallback(() => {
- // Now force a new render so the latest video frame shows up in the canvas
- render();
- // Allow remotion to continue
- continueRender(handle);
- });
- }, [frame]);
+ const frame = useCurrentFrame();
+ React.useEffect(() => {
+ const video = videoRef.current;
+ if (!video || !videoTexture) {
+ return;
+ }
+ if (!video.requestVideoFrameCallback) {
+ throw new Error(
+ 'HTMLVideoElement.requestVideoFrameCallback not supported'
+ );
+ }
+ const handle = delayRender();
+ // wait for the video to render its next frame
+ video.requestVideoFrameCallback(() => {
+ // Now force a new render so the latest video frame shows up in the canvas
+ render();
+ // Allow remotion to continue
+ continueRender(handle);
+ });
+ }, [frame]);
- useRemotionThreeDomNode((
- // TODO: Remove wrapper once Video supports ref
- <div ref={videoWrapperRef}>
- <Video
- crossOrigin='anonymous'
- {...videoProps}
- style={{ width: 800, height: 500 }}
- />
- </div>
- ));
-
- return videoTexture;
-}
+ useRemotionThreeDomNode(
+ // TODO: Remove wrapper once Video supports ref
+ <div ref={videoWrapperRef}>
+ <Video
+ crossOrigin="anonymous"
+ {...videoProps}
+ style={{ width: 800, height: 500 }}
+ />
+ </div>
+ );
+ return videoTexture;
+};
diff --git a/packages/three/src/utils/TexturedShaderMaterial.ts b/packages/three/src/utils/TexturedShaderMaterial.ts
index e743b09d0a..e990c03870 100644
--- a/packages/three/src/utils/TexturedShaderMaterial.ts
+++ b/packages/three/src/utils/TexturedShaderMaterial.ts
@@ -1,32 +1,34 @@
-import { ShaderMaterial, ShaderMaterialParameters, Texture } from 'three';
import { extend, MaterialNode } from '@react-three/fiber';
+import { ShaderMaterial, ShaderMaterialParameters, Texture } from 'three';
declare global {
- namespace JSX {
- interface IntrinsicElements {
- texturedShaderMaterial: MaterialNode<TexturedShaderMaterial, [TexturedShaderMaterialParameters]>;
- }
- }
+ declare namespace JSX {
+ interface IntrinsicElements {
+ texturedShaderMaterial: MaterialNode<
+ TexturedShaderMaterial,
+ [TexturedShaderMaterialParameters]
+ >;
+ }
+ }
}
-export interface TexturedShaderMaterialParameters extends ShaderMaterialParameters {
- map?: Texture;
+export interface TexturedShaderMaterialParameters
+ extends ShaderMaterialParameters {
+ map?: Texture;
}
-export default class TexturedShaderMaterial extends ShaderMaterial {
-
- constructor(params?: TexturedShaderMaterialParameters) {
- super(params);
- }
-
- public get map(): Texture | null {
- return this.uniforms.map.value;
- }
+export class TexturedShaderMaterial extends ShaderMaterial {
+ constructor(params?: TexturedShaderMaterialParameters) {
+ super(params);
+ }
- public set map(value: Texture | null) {
- this.uniforms.map.value = value;
- }
+ public get map(): Texture | null {
+ return this.uniforms.map.value;
+ }
+ public set map(value: Texture | null) {
+ this.uniforms.map.value = value;
+ }
}
extend({ TexturedShaderMaterial });