Skip to content

🎥 Make videos programmatically with React

3d engine with context

Jonny Burgerab0c13f01a5e
diff --git a/packages/example/src/3DContext/DivExtrusion.tsx b/packages/example/src/3DContext/DivExtrusion.tsx
new file mode 100644
index 0000000000..71e83ffa36
--- /dev/null
+++ b/packages/example/src/3DContext/DivExtrusion.tsx
@@ -0,0 +1,39 @@
+import {PathInternals, reduceInstructions} from '@remotion/paths';
+import {makeRect} from '@remotion/shapes';
+import {MatrixTransform4D} from '@remotion/svg-3d-engine';
+import {SvgExtrusion} from './Extrusion';
+
+export const DivExtrusion: React.FC<{
+ height: number;
+ width: number;
+ cornerRadius: number;
+ depth: number;
+ transformatations: MatrixTransform4D[];
+}> = ({height, width, cornerRadius, depth, transformatations}) => {
+ const {instructions} = makeRect({height, width, cornerRadius});
+
+ const {viewBox} = PathInternals.getBoundingBoxFromInstructions(
+ reduceInstructions(instructions),
+ );
+ return (
+ <svg
+ viewBox={viewBox}
+ style={{
+ overflow: 'visible',
+ height,
+ width,
+ position: 'absolute',
+ top: 0,
+ }}
+ pointerEvents="none"
+ >
+ <SvgExtrusion
+ transformations={transformatations}
+ cornerRadius={cornerRadius}
+ depth={depth}
+ height={height}
+ width={width}
+ />
+ </svg>
+ );
+};
diff --git a/packages/example/src/3DContext/Extrusion.tsx b/packages/example/src/3DContext/Extrusion.tsx
new file mode 100644
index 0000000000..c53557208a
--- /dev/null
+++ b/packages/example/src/3DContext/Extrusion.tsx
@@ -0,0 +1,43 @@
+import {parsePath} from '@remotion/paths';
+import {makeRect} from '@remotion/shapes';
+import {
+ extrudeAndTransformElement,
+ MatrixTransform4D,
+ reduceMatrices,
+ translateX,
+ translateY,
+} from '@remotion/svg-3d-engine';
+import {Faces} from '../3DEngine/Faces';
+
+export const SvgExtrusion: React.FC<{
+ transformations: MatrixTransform4D[];
+ depth: number;
+ height: number;
+ width: number;
+ cornerRadius: number;
+}> = ({transformations, cornerRadius, depth, height, width}) => {
+ const {path} = makeRect({height, width, cornerRadius});
+ const reduced = reduceMatrices(transformations);
+
+ const centerOriented = reduceMatrices([
+ translateX(-width / 2),
+ translateY(-height / 2),
+ reduced,
+ translateX(width / 2),
+ translateY(height / 2),
+ ]);
+
+ const extruded = extrudeAndTransformElement({
+ backFaceColor: 'black',
+ sideColor: 'black',
+ crispEdges: false,
+ depth,
+ points: parsePath(path),
+ description: 'rect',
+ frontFaceColor: 'white',
+ strokeColor: 'black',
+ strokeWidth: 3,
+ transformations: centerOriented,
+ });
+ return <Faces elements={[extruded]} />;
+};
diff --git a/packages/example/src/3DContext/FrontFace.tsx b/packages/example/src/3DContext/FrontFace.tsx
new file mode 100644
index 0000000000..a185b7519a
--- /dev/null
+++ b/packages/example/src/3DContext/FrontFace.tsx
@@ -0,0 +1,31 @@
+import {
+ makeMatrix3dTransform,
+ MatrixTransform4D,
+ reduceMatrices,
+ translateZ,
+} from '@remotion/svg-3d-engine';
+
+export const FrontFace: React.FC<{
+ width: number;
+ height: number;
+ children: React.ReactNode;
+ depth: number;
+ transformations: MatrixTransform4D[];
+}> = ({width, height, children, depth, transformations: transformatations}) => {
+ const reduced = reduceMatrices(transformatations);
+
+ const frontFace = reduceMatrices([translateZ(-depth), reduced]);
+
+ return (
+ <div
+ style={{
+ transform: makeMatrix3dTransform(frontFace),
+ display: 'flex',
+ width,
+ height,
+ }}
+ >
+ {children}
+ </div>
+ );
+};
diff --git a/packages/example/src/3DContext/Outer.tsx b/packages/example/src/3DContext/Outer.tsx
new file mode 100644
index 0000000000..4a0c699347
--- /dev/null
+++ b/packages/example/src/3DContext/Outer.tsx
@@ -0,0 +1,39 @@
+import {MatrixTransform4D} from '@remotion/svg-3d-engine';
+import {useRef} from 'react';
+import {DivExtrusion} from './DivExtrusion';
+import {FrontFace} from './FrontFace';
+
+export const Div3D: React.FC<{
+ children: React.ReactNode;
+ width: number;
+ height: number;
+ depth: number;
+ transformatations: MatrixTransform4D[];
+ cornerRadius: 10;
+}> = ({children, width, height, depth, transformatations, cornerRadius}) => {
+ const ref = useRef<HTMLDivElement>(null);
+
+ return (
+ <div
+ ref={ref}
+ className="relative"
+ style={{width, height, position: 'relative'}}
+ >
+ <DivExtrusion
+ transformatations={transformatations}
+ cornerRadius={cornerRadius}
+ depth={depth}
+ height={height}
+ width={width}
+ />
+ <FrontFace
+ height={height}
+ width={width}
+ depth={depth}
+ transformations={transformatations}
+ >
+ {children}
+ </FrontFace>
+ </div>
+ );
+};
diff --git a/packages/example/src/3DContext/index.tsx b/packages/example/src/3DContext/index.tsx
new file mode 100644
index 0000000000..332701f72d
--- /dev/null
+++ b/packages/example/src/3DContext/index.tsx
@@ -0,0 +1,46 @@
+import {rotateX, rotateY, scaled} from '@remotion/svg-3d-engine';
+import {AbsoluteFill} from 'remotion';
+import {Div3D} from './Outer';
+
+const Comp = () => {
+ const width = 200;
+ const height = 70;
+
+ const cornerRadius = 10;
+
+ const angle = Math.PI / 4;
+
+ const transformations = [
+ scaled(1.1),
+ rotateX(-Math.PI / 16),
+ rotateX(Math.sin(angle) / 4),
+ // rotateY(-Math.PI / 8),
+ rotateY(-Math.cos(angle) / 4),
+ ];
+
+ return (
+ <AbsoluteFill className="flex justify-center items-center bg-[#F8FAFC]">
+ <Div3D
+ width={width}
+ height={height}
+ depth={40}
+ transformatations={transformations}
+ cornerRadius={10}
+ >
+ <div
+ style={{
+ borderRadius: cornerRadius,
+ fontFamily: 'GT Planar',
+ backgroundColor: 'white',
+ border: '2px solid black',
+ }}
+ className="text-black flex justify-center items-center font-sans text-2xl border-solid border-black font-bold cursor-pointer flex-1"
+ >
+ Get started
+ </div>
+ </Div3D>
+ </AbsoluteFill>
+ );
+};
+
+export const ThreeDContext = Comp;
diff --git a/packages/example/src/3DEngine/Outer.tsx b/packages/example/src/3DEngine/Outer.tsx
new file mode 100644
index 0000000000..8a8243903c
--- /dev/null
+++ b/packages/example/src/3DEngine/Outer.tsx
@@ -0,0 +1,109 @@
+import {parsePath, PathInternals, reduceInstructions} from '@remotion/paths';
+import {makeRect} from '@remotion/shapes';
+import {
+ extrudeAndTransformElement,
+ interpolateMatrix4d,
+ makeMatrix3dTransform,
+ MatrixTransform4D,
+ reduceMatrices,
+ rotateX,
+ rotateY,
+ scaled,
+ translateX,
+ translateY,
+ translateZ,
+} from '@remotion/svg-3d-engine';
+import {useRef} from 'react';
+import {interpolate} from 'remotion';
+import {Faces} from './Faces';
+import {
+ useClickTransforms,
+ useHoverTransforms,
+ useMousePosition,
+} from './hover-transforms';
+
+export const Outer: React.FC<{
+ children: React.ReactNode;
+ width: number;
+ height: number;
+}> = ({children, width, height}) => {
+ const ref = useRef<HTMLDivElement>(null);
+
+ const hoverTransform = useHoverTransforms(ref);
+ const clickTransform = useClickTransforms(ref);
+ const angle = useMousePosition(ref);
+ // const angle = 0
+
+ const transformationUnhovered: MatrixTransform4D = reduceMatrices([
+ rotateX(-Math.PI / 20),
+ ]);
+ const transformationHovered: MatrixTransform4D = reduceMatrices([
+ scaled(1.1),
+ rotateX(-Math.PI / 16),
+ rotateX(Math.sin(angle) / 4),
+ // rotateY(-Math.PI / 8),
+ rotateY(-Math.cos(angle) / 4),
+ ]);
+
+ const transformation = interpolateMatrix4d(
+ hoverTransform,
+ transformationUnhovered,
+ transformationHovered,
+ );
+
+ const depth =
+ interpolate(hoverTransform, [0, 1], [20, 30]) - clickTransform * 20;
+
+ const frontFace = reduceMatrices([translateZ(-depth), transformation]);
+ const centerOriented = reduceMatrices([
+ translateX(-width / 2),
+ translateY(-height / 2),
+ transformation,
+ translateX(width / 2),
+ translateY(height / 2),
+ ]);
+
+ const cornerRadius = 10;
+
+ const {path, instructions} = makeRect({height, width, cornerRadius});
+ const {viewBox} = PathInternals.getBoundingBoxFromInstructions(
+ reduceInstructions(instructions),
+ );
+ const extruded = extrudeAndTransformElement({
+ backFaceColor: 'black',
+ sideColor: 'black',
+ crispEdges: false,
+ depth,
+ points: parsePath(path),
+ description: 'rect',
+ frontFaceColor: 'white',
+ strokeColor: 'black',
+ strokeWidth: 3,
+ transformations: centerOriented,
+ });
+
+ return (
+ <div ref={ref} className="relative" style={{width, height}}>
+ <svg
+ viewBox={viewBox}
+ style={{
+ overflow: 'visible',
+ height,
+ width,
+ position: 'absolute',
+ top: 0,
+ }}
+ pointerEvents="none"
+ >
+ <Faces elements={[extruded]} />
+ </svg>
+ <div
+ style={{
+ transform: makeMatrix3dTransform(frontFace),
+ }}
+ >
+ {children}
+ </div>
+ </div>
+ );
+};
diff --git a/packages/example/src/3DEngine/index.tsx b/packages/example/src/3DEngine/index.tsx
index c0bfdad1c5..d38b192d70 100644
--- a/packages/example/src/3DEngine/index.tsx
+++ b/packages/example/src/3DEngine/index.tsx
@@ -1,105 +1,18 @@
-import {parsePath, PathInternals, reduceInstructions} from '@remotion/paths';
-import {makeRect} from '@remotion/shapes';
-import {
- extrudeAndTransformElement,
- interpolateMatrix4d,
- makeMatrix3dTransform,
- MatrixTransform4D,
- reduceMatrices,
- rotateX,
- rotateY,
- scaled,
- translateX,
- translateY,
- translateZ,
-} from '@remotion/svg-3d-engine';
-import {useRef} from 'react';
-import {AbsoluteFill, interpolate} from 'remotion';
-import {Faces} from './Faces';
-import {
- useClickTransforms,
- useHoverTransforms,
- useMousePosition,
-} from './hover-transforms';
-
-export const ThreeDEngine = () => {
- const ref = useRef<HTMLDivElement>(null);
+import {StudioInternals} from '@remotion/studio';
+import {AbsoluteFill} from 'remotion';
+import {Outer} from './Outer';
+const Comp = () => {
const width = 200;
const height = 70;
- const hoverTransform = useHoverTransforms(ref);
- const clickTransform = useClickTransforms(ref);
- const angle = useMousePosition(ref);
- // const angle = 0
-
- const transformationUnhovered: MatrixTransform4D = reduceMatrices([
- rotateX(-Math.PI / 20),
- ]);
- const transformationHovered: MatrixTransform4D = reduceMatrices([
- scaled(1.1),
- rotateX(-Math.PI / 16),
- rotateX(Math.sin(angle) / 4),
- // rotateY(-Math.PI / 8),
- rotateY(-Math.cos(angle) / 4),
- ]);
-
- const transformation = interpolateMatrix4d(
- hoverTransform,
- transformationUnhovered,
- transformationHovered,
- );
-
- const depth =
- interpolate(hoverTransform, [0, 1], [20, 30]) - clickTransform * 20;
-
- const frontFace = reduceMatrices([translateZ(-depth), transformation]);
- const centerOriented = reduceMatrices([
- translateX(-width / 2),
- translateY(-height / 2),
- transformation,
- translateX(width / 2),
- translateY(height / 2),
- ]);
-
const cornerRadius = 10;
- const {path, instructions} = makeRect({height, width, cornerRadius});
- const {viewBox} = PathInternals.getBoundingBoxFromInstructions(
- reduceInstructions(instructions),
- );
- const extruded = extrudeAndTransformElement({
- backFaceColor: 'black',
- sideColor: 'black',
- crispEdges: false,
- depth,
- points: parsePath(path),
- description: 'rect',
- frontFaceColor: 'white',
- strokeColor: 'black',
- strokeWidth: 3,
- transformations: centerOriented,
- });
-
return (
<AbsoluteFill className="flex justify-center items-center bg-[#F8FAFC]">
- <div ref={ref} className="relative" style={{width, height}}>
- <svg
- viewBox={viewBox}
- style={{
- overflow: 'visible',
- height,
- width,
- position: 'absolute',
- top: 0,
- }}
- pointerEvents="none"
- >
- <Faces elements={[extruded]} />
- </svg>
+ <Outer width={width} height={height}>
<div
style={{
- transform: makeMatrix3dTransform(frontFace),
borderRadius: cornerRadius,
width,
height,
@@ -111,7 +24,14 @@ export const ThreeDEngine = () => {
>
Get started
</div>
- </div>
+ </Outer>
</AbsoluteFill>
);
};
+
+export const ThreeDEngine = StudioInternals.createStill({
+ component: Comp,
+ id: 'ThreeDEngine',
+ height: 1080,
+ width: 1080,
+});
diff --git a/packages/example/src/Root.tsx b/packages/example/src/Root.tsx
index f775f90c8d..18bfd3da15 100644
--- a/packages/example/src/Root.tsx
+++ b/packages/example/src/Root.tsx
@@ -113,6 +113,7 @@ if (alias !== 'alias') {
const INCLUDE_COMP_BREAKING_GET_COMPOSITIONS = false;
+import {ThreeDContext} from './3DContext';
import {ThreeDEngine} from './3DEngine';
import {AnimatedImages} from './AnimatedImage/Avif';
import {SmoothTextTransition} from './SmoothTextTransition';
@@ -1485,12 +1486,15 @@ export const Index: React.FC = () => {
/>
<Still id="Emojis" component={EmojiTestbed} height={800} width={1024} />
<Still id="HugeImage" component={HugeImage} height={9000} width={9000} />
- <Still
- id="ThreeDEngine"
- component={ThreeDEngine}
- height={1000}
- width={1000}
- />
+ <Folder name="3DEngine">
+ <ThreeDEngine />
+ <Still
+ id="3DContext"
+ component={ThreeDContext}
+ width={1080}
+ height={1080}
+ />
+ </Folder>
<SmoothTextTransition />
</>
);
diff --git a/packages/studio/src/components/RenderModal/SchemaEditor/SchemaErrorMessages.tsx b/packages/studio/src/components/RenderModal/SchemaEditor/SchemaErrorMessages.tsx
index 1845f914be..b66bde2830 100644
--- a/packages/studio/src/components/RenderModal/SchemaEditor/SchemaErrorMessages.tsx
+++ b/packages/studio/src/components/RenderModal/SchemaEditor/SchemaErrorMessages.tsx
@@ -1,5 +1,5 @@
import type {z} from 'zod';
-import {BLUE, LIGHT_TEXT} from '../../../helpers/colors';
+import {BACKGROUND, BLUE, LIGHT_TEXT} from '../../../helpers/colors';
import {Button} from '../../Button';
import {inlineCodeSnippet} from '../../Menu/styles';
import {Spacing} from '../../layout';
@@ -13,6 +13,7 @@ const explainer: React.CSSProperties = {
justifyContent: 'center',
alignItems: 'center',
textAlign: 'center',
+ background: BACKGROUND,
};
const errorExplanation: React.CSSProperties = {