Skip to content

🎥 Make videos programmatically with React

aha

Jonny Burger1c185afa3a31
diff --git a/packages/docs/tsconfig.json b/packages/docs/tsconfig.json
index c04253af03..d1897513d0 100644
--- a/packages/docs/tsconfig.json
+++ b/packages/docs/tsconfig.json
@@ -46,6 +46,7 @@
{"path": "../lambda"},
{"path": "../bundler"},
{"path": "../renderer"},
+ {"path": "../svg-3d-engine"},
{"path": "../transitions"},
{"path": "../cli"}
],
diff --git a/packages/example/src/3DEngine/Svg.tsx b/packages/example/src/3DEngine/Svg.tsx
new file mode 100644
index 0000000000..daa410e995
--- /dev/null
+++ b/packages/example/src/3DEngine/Svg.tsx
@@ -0,0 +1,28 @@
+import {getBoundingBox} from '@remotion/paths';
+import {MatrixTransform4D, transformPath} from '@remotion/svg-3d-engine';
+import React, {useMemo} from 'react';
+
+export const Svg: React.FC<{
+ readonly svgShape: string;
+ readonly transformations: MatrixTransform4D[];
+}> = ({svgShape, transformations}) => {
+ const boundingBox = useMemo(() => getBoundingBox(svgShape), [svgShape]);
+
+ return (
+ <svg
+ viewBox={boundingBox.viewBox}
+ style={{
+ height: boundingBox.height,
+ width: boundingBox.width,
+ overflow: 'visible',
+ }}
+ >
+ <path
+ d={transformPath({path: svgShape, transformations})}
+ fill="gray"
+ stroke="black"
+ strokeWidth="1"
+ />
+ </svg>
+ );
+};
diff --git a/packages/example/src/3DEngine/index.tsx b/packages/example/src/3DEngine/index.tsx
index 83df49c59e..c10822f522 100644
--- a/packages/example/src/3DEngine/index.tsx
+++ b/packages/example/src/3DEngine/index.tsx
@@ -1,7 +1,13 @@
-import {getBoundingBox} from '@remotion/paths';
import {makeRect} from '@remotion/shapes';
+import {
+ MatrixTransform4D,
+ rotateX,
+ rotateY,
+ scaled,
+} from '@remotion/svg-3d-engine';
import React, {useEffect, useRef, useState} from 'react';
import {AbsoluteFill, useCurrentScale} from 'remotion';
+import {Svg} from './Svg';
const measure = (
ref: React.RefObject<HTMLDivElement | null>,
@@ -17,6 +23,12 @@ const measure = (
};
};
+const transformations: MatrixTransform4D[] = [
+ scaled(2),
+ rotateY(Math.PI / 8),
+ rotateX(Math.PI / 8),
+];
+
export const ThreeDEngine = () => {
const ref = useRef<HTMLDivElement>(null);
const [svgShape, setSvgShape] = useState<string | null>(null);
@@ -41,16 +53,7 @@ export const ThreeDEngine = () => {
hi there
</div>
{svgShape ? (
- <svg
- viewBox={getBoundingBox(svgShape).viewBox}
- style={{
- height: getBoundingBox(svgShape).height,
- width: getBoundingBox(svgShape).width,
- overflow: 'visible',
- }}
- >
- <path d={svgShape} fill="gray" stroke="black" strokeWidth="1" />
- </svg>
+ <Svg transformations={transformations} svgShape={svgShape} />
) : null}
</AbsoluteFill>
);
diff --git a/packages/example/tsconfig.json b/packages/example/tsconfig.json
index 7f98659cb4..ffe58b54df 100644
--- a/packages/example/tsconfig.json
+++ b/packages/example/tsconfig.json
@@ -34,6 +34,7 @@
{"path": "../three"},
{"path": "../transitions"},
{"path": "../zod-types"},
+ {"path": "../svg-3d-engine"},
{"path": "../media-parser"},
{"path": "../webcodecs"},
{"path": "../layout-utils"}
diff --git a/packages/svg-3d-engine/src/3d-svg.ts b/packages/svg-3d-engine/src/3d-svg.ts
index 0aa9bc893e..e053e0cbc1 100644
--- a/packages/svg-3d-engine/src/3d-svg.ts
+++ b/packages/svg-3d-engine/src/3d-svg.ts
@@ -25,7 +25,7 @@ export type ThreeDReducedInstruction =
point: Vector4D;
};
-const serializeThreeDReducedInstruction = (
+export const serializeThreeDReducedInstruction = (
instruction: ThreeDReducedInstruction,
): string => {
if (instruction.type === 'M') {
diff --git a/packages/svg-3d-engine/src/index.ts b/packages/svg-3d-engine/src/index.ts
index b39ffbf925..aedcaf4c96 100644
--- a/packages/svg-3d-engine/src/index.ts
+++ b/packages/svg-3d-engine/src/index.ts
@@ -1,8 +1,11 @@
+export {MatrixTransform4D} from './matrix';
+
export type {ThreeDReducedInstruction} from './3d-svg';
export {threeDIntoSvgPath} from './3d-svg';
export {transformElement} from './elements';
export {extrudeElement} from './extrude-element';
+export {transformPath} from './map-face';
export {
rotateX,
rotateY,
diff --git a/packages/svg-3d-engine/src/map-face.ts b/packages/svg-3d-engine/src/map-face.ts
index c142e59a55..2c44fa8b3b 100644
--- a/packages/svg-3d-engine/src/map-face.ts
+++ b/packages/svg-3d-engine/src/map-face.ts
@@ -1,10 +1,14 @@
-import {parsePath} from '@remotion/paths';
-import type {ThreeDReducedInstruction} from './3d-svg';
+import {parsePath, reduceInstructions} from '@remotion/paths';
+import {threeDIntoSvgPath, type ThreeDReducedInstruction} from './3d-svg';
import type {ThreeDElement} from './elements';
import {transformElement} from './elements';
import {turnInto3D} from './fix-z';
import type {MatrixTransform4D, Vector4D} from './matrix';
-import {multiplyMatrix, multiplyMatrixAndSvgInstruction} from './matrix';
+import {
+ multiplyMatrix,
+ multiplyMatrixAndSvgInstruction,
+ reduceMatrices,
+} from './matrix';
export type FaceType = {
color: string;
@@ -103,6 +107,26 @@ export const translateSvgInstruction = (
throw new Error('Unknown instruction type: ' + JSON.stringify(instruction));
};
+export const transformPath = ({
+ path,
+ transformations,
+}: {
+ path: string;
+ transformations: MatrixTransform4D[];
+}): string => {
+ const parsed = parsePath(path);
+ const reduced = reduceInstructions(parsed);
+ const threeD = turnInto3D(reduced);
+ return threeDIntoSvgPath(
+ threeD.map((p) => {
+ return multiplyMatrixAndSvgInstruction(
+ reduceMatrices(transformations),
+ p,
+ );
+ }),
+ );
+};
+
export const transformFace = (
face: FaceType,
transformations: MatrixTransform4D[],
diff --git a/packages/svg-3d-engine/src/matrix.ts b/packages/svg-3d-engine/src/matrix.ts
index 5ac8dadbe7..d636324e9c 100644
--- a/packages/svg-3d-engine/src/matrix.ts
+++ b/packages/svg-3d-engine/src/matrix.ts
@@ -137,6 +137,34 @@ export type MatrixTransform4D = [
number,
];
+export const multiplyMatrices = (
+ matrix1: MatrixTransform4D,
+ matrix2: MatrixTransform4D,
+): MatrixTransform4D => {
+ const result: MatrixTransform4D = new Array(16).fill(0) as MatrixTransform4D;
+
+ for (let i = 0; i < 4; i++) {
+ for (let j = 0; j < 4; j++) {
+ for (let k = 0; k < 4; k++) {
+ result[i * 4 + j] += matrix1[i * 4 + k] * matrix2[k * 4 + j];
+ }
+ }
+ }
+
+ return result;
+};
+
+export const reduceMatrices = (
+ matrices: MatrixTransform4D[],
+): MatrixTransform4D => {
+ return matrices
+ .slice()
+ .reverse()
+ .reduce((acc, cur) => {
+ return multiplyMatrices(acc, cur);
+ }, identity4());
+};
+
export const scaled = function (value: number | Vector) {
const vec: Vector = typeof value === 'number' ? [value, value, value] : value;
return stride({v: vec, m: identity4(), width: 4, offset: 0, colStride: 1});