Skip to content

🎥 Make videos programmatically with React

getting there

Jonny Burgerde24485a6fc6
diff --git a/packages/cli/src/list-of-remotion-packages.ts b/packages/cli/src/list-of-remotion-packages.ts
index 2a5b08aa42..e3365c0fae 100644
--- a/packages/cli/src/list-of-remotion-packages.ts
+++ b/packages/cli/src/list-of-remotion-packages.ts
@@ -1,5 +1,6 @@
// Keep in sync with create-video
export const listOfRemotionPackages = [
+ '@remotion/svg-3d-engine',
'@remotion/layout-utils',
'@remotion/bundler',
'@remotion/cli',
diff --git a/packages/create-video/src/list-of-remotion-packages.ts b/packages/create-video/src/list-of-remotion-packages.ts
index 976095c890..fa19f8f188 100644
--- a/packages/create-video/src/list-of-remotion-packages.ts
+++ b/packages/create-video/src/list-of-remotion-packages.ts
@@ -1,5 +1,6 @@
// Keep in sync with cli
export const listOfRemotionPackages = [
+ '@remotion/svg-3d-engine',
'@remotion/layout-utils',
'@remotion/bundler',
'@remotion/cli',
diff --git a/packages/docs/package.json b/packages/docs/package.json
index 344e459317..a9796b0b1a 100644
--- a/packages/docs/package.json
+++ b/packages/docs/package.json
@@ -32,6 +32,7 @@
"@docusaurus/types": "3.7.0",
"@mdx-js/react": "3.0.1",
"@remotion-dev/cube-presentation": "https://www.remotion.pro/pkg/cube-transition/1.0.2/docs",
+ "@remotion/svg-3d-engine": "workspace:*",
"@remotion/animated-emoji": "workspace:*",
"@remotion/animation-utils": "workspace:*",
"@remotion/babel-loader": "workspace:*",
diff --git a/packages/docs/src/components/DoMoreHero/DoMoreHero.tsx b/packages/docs/src/components/DoMoreHero/DoMoreHero.tsx
index 0d0fbb8066..268a07b8aa 100644
--- a/packages/docs/src/components/DoMoreHero/DoMoreHero.tsx
+++ b/packages/docs/src/components/DoMoreHero/DoMoreHero.tsx
@@ -2,6 +2,16 @@ import {useColorMode} from '@docusaurus/theme-common';
import {getBoundingBox, parsePath, resetPath} from '@remotion/paths';
import {Player} from '@remotion/player';
import {makeTriangle} from '@remotion/shapes';
+import {
+ extrudeElement,
+ Faces,
+ rotateX,
+ rotateY,
+ rotateZ,
+ transformElement,
+ translateX,
+ translateY,
+} from '@remotion/svg-3d-engine';
import React, {useMemo} from 'react';
import {
AbsoluteFill,
@@ -11,18 +21,8 @@ import {
useVideoConfig,
} from 'remotion';
import {useMobileLayout} from '../../helpers/mobile-layout';
-import {Faces} from './Faces';
-import {transformElement} from './element';
-import {extrudeElement} from './join-inbetween-tiles';
-import {
- rotateX,
- rotateY,
- rotateZ,
- scaled,
- translateX,
- translateY,
- translateZ,
-} from './matrix';
+
+import {translateZ} from '@remotion/svg-3d-engine';
import styles from './more.module.css';
const InFrameLogo: React.FC<{
@@ -82,6 +82,7 @@ const InFrameLogo: React.FC<{
strokeWidth: 20,
description: `triangle-${i}`,
strokeColor: 'black',
+ crispEdges: false,
});
const projected = transformElement(extruded, [
translateZ(spread * i - spread),
diff --git a/packages/docs/src/components/DoMoreHero/Face.d.ts b/packages/docs/src/components/DoMoreHero/Face.d.ts
new file mode 100644
index 0000000000..90848be655
--- /dev/null
+++ b/packages/docs/src/components/DoMoreHero/Face.d.ts
@@ -0,0 +1,13 @@
+import type { ThreeDReducedInstruction } from '@remotion/svg-3d-engine';
+import React from 'react';
+export type FaceSVGProps = {
+ readonly strokeLinecap?: React.SVGAttributes<SVGPathElement>['strokeLinecap'];
+ readonly strokeMiterlimit?: React.SVGAttributes<SVGPathElement>['strokeMiterlimit'];
+};
+export declare const Face: React.FC<{
+ readonly points: ThreeDReducedInstruction[];
+ readonly color: string;
+ readonly strokeColor: string;
+ readonly strokeWidth: number;
+ readonly crispEdges: boolean;
+} & FaceSVGProps>;
diff --git a/packages/docs/src/components/DoMoreHero/Face.js b/packages/docs/src/components/DoMoreHero/Face.js
new file mode 100644
index 0000000000..d9c43490ad
--- /dev/null
+++ b/packages/docs/src/components/DoMoreHero/Face.js
@@ -0,0 +1,11 @@
+"use strict";
+Object.defineProperty(exports, "__esModule", { value: true });
+exports.Face = void 0;
+const jsx_runtime_1 = require("react/jsx-runtime");
+const react_1 = require("react");
+const Face = ({ color, points, strokeColor, strokeWidth, strokeMiterlimit, strokeLinecap, crispEdges, }) => {
+ const [id] = (0, react_1.useState)(() => Math.random().toString().replace('.', ''));
+ const d = threeDIntoSvgPath(points);
+ return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)("defs", { children: strokeWidth ? ((0, jsx_runtime_1.jsx)("mask", { id: id, children: (0, jsx_runtime_1.jsx)("path", { strokeLinecap: strokeLinecap, shapeRendering: crispEdges ? 'crispEdges' : undefined, strokeMiterlimit: strokeMiterlimit, strokeWidth: strokeWidth, d: d, fill: "white" }) })) : null }), (0, jsx_runtime_1.jsx)("path", { d: d, fill: color, mask: strokeWidth ? `url(#${id})` : undefined, stroke: strokeColor, strokeMiterlimit: strokeMiterlimit, shapeRendering: crispEdges ? 'crispEdges' : undefined, strokeLinecap: strokeLinecap, strokeWidth: strokeWidth })] }));
+};
+exports.Face = Face;
diff --git a/packages/docs/src/components/DoMoreHero/Face.tsx b/packages/docs/src/components/DoMoreHero/Face.tsx
index ae91a565e4..4624b754fe 100644
--- a/packages/docs/src/components/DoMoreHero/Face.tsx
+++ b/packages/docs/src/components/DoMoreHero/Face.tsx
@@ -1,7 +1,5 @@
+import type {ThreeDReducedInstruction} from '@remotion/svg-3d-engine';
import React, {useState} from 'react';
-import {random} from 'remotion';
-import type {ThreeDReducedInstruction} from './3d-svg';
-import {threeDIntoSvgPath} from './3d-svg';
export type FaceSVGProps = {
readonly strokeLinecap?: React.SVGAttributes<SVGPathElement>['strokeLinecap'];
@@ -25,7 +23,7 @@ export const Face: React.FC<
strokeLinecap,
crispEdges,
}) => {
- const [id] = useState(() => random(null).toString().replace('.', ''));
+ const [id] = useState(() => Math.random().toString().replace('.', ''));
const d = threeDIntoSvgPath(points);
return (
diff --git a/packages/docs/src/components/DoMoreHero/Faces.d.ts b/packages/docs/src/components/DoMoreHero/Faces.d.ts
new file mode 100644
index 0000000000..9dcf528a23
--- /dev/null
+++ b/packages/docs/src/components/DoMoreHero/Faces.d.ts
@@ -0,0 +1,7 @@
+import type { ThreeDElement } from '@remotion/svg-3d-engine/src/elements';
+import React from 'react';
+import type { FaceSVGProps } from './Face';
+export declare const Faces: React.FC<{
+ readonly elements: ThreeDElement[];
+ readonly noSort?: boolean;
+} & FaceSVGProps>;
diff --git a/packages/docs/src/components/DoMoreHero/Faces.js b/packages/docs/src/components/DoMoreHero/Faces.js
new file mode 100644
index 0000000000..73dc9b7f92
--- /dev/null
+++ b/packages/docs/src/components/DoMoreHero/Faces.js
@@ -0,0 +1,32 @@
+"use strict";
+var __importDefault = (this && this.__importDefault) || function (mod) {
+ return (mod && mod.__esModule) ? mod : { "default": mod };
+};
+Object.defineProperty(exports, "__esModule", { value: true });
+exports.Faces = void 0;
+const jsx_runtime_1 = require("react/jsx-runtime");
+const react_1 = __importDefault(require("react"));
+const Face_1 = require("./Face");
+const sortFacesZIndex = (face) => {
+ return face.slice().sort((a, b) => {
+ return b.centerPoint[2] - a.centerPoint[2];
+ });
+};
+const Faces = ({ elements, noSort, ...svgProps }) => {
+ const sortedElement = noSort
+ ? elements
+ : elements.sort((a, b) => {
+ return b.centerPoint[2] - a.centerPoint[2];
+ });
+ return ((0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: sortedElement.map((element, i) => {
+ const sortedFaces = sortFacesZIndex(element.faces);
+ return (
+ // eslint-disable-next-line react/no-array-index-key
+ (0, jsx_runtime_1.jsx)(react_1.default.Fragment, { children: sortedFaces.map(({ points, color, strokeWidth, strokeColor, crispEdges }, idx) => {
+ return ((0, jsx_runtime_1.jsx)(Face_1.Face
+ // eslint-disable-next-line react/no-array-index-key
+ , { strokeColor: strokeColor, color: color, points: points, strokeWidth: strokeWidth, crispEdges: crispEdges, ...svgProps }, JSON.stringify(points) + idx));
+ }) }, i));
+ }) }));
+};
+exports.Faces = Faces;
diff --git a/packages/docs/src/components/DoMoreHero/Faces.tsx b/packages/docs/src/components/DoMoreHero/Faces.tsx
index 2e7324085b..19a4278d4e 100644
--- a/packages/docs/src/components/DoMoreHero/Faces.tsx
+++ b/packages/docs/src/components/DoMoreHero/Faces.tsx
@@ -1,8 +1,8 @@
+import type {ThreeDElement} from '@remotion/svg-3d-engine/src/elements';
+import type {FaceType} from '@remotion/svg-3d-engine/src/map-face';
import React from 'react';
import type {FaceSVGProps} from './Face';
import {Face} from './Face';
-import type {ThreeDElement} from './element';
-import type {FaceType} from './map-face';
const sortFacesZIndex = (face: FaceType[]): FaceType[] => {
return face.slice().sort((a, b) => {
diff --git a/packages/docs/src/components/DoMoreHero/element.ts b/packages/docs/src/components/DoMoreHero/element.ts
deleted file mode 100644
index 10f93b6127..0000000000
--- a/packages/docs/src/components/DoMoreHero/element.ts
+++ /dev/null
@@ -1,42 +0,0 @@
-import {makeId} from './make-id';
-import type {FaceType} from './map-face';
-import {transformFace} from './map-face';
-import type {MatrixTransform4D, Vector4D} from './matrix';
-import {multiplyMatrix} from './matrix';
-
-export type ThreeDElement = {
- faces: FaceType[];
- id: string;
- description: string;
- centerPoint: Vector4D;
-};
-
-export const makeElement = (
- face: FaceType | FaceType[],
- centerPoint: Vector4D,
- description: string,
-): ThreeDElement => {
- return {
- faces: Array.isArray(face) ? face : [face],
- id: makeId(),
- centerPoint,
- description,
- };
-};
-
-export const transformElement = (
- element: ThreeDElement,
- transformations: MatrixTransform4D[],
-): ThreeDElement => {
- return {
- ...element,
- faces: element.faces.map((face) => {
- return transformFace(face, transformations);
- }),
- id: makeId(),
- centerPoint: transformations.reduce(
- (point, transformation) => multiplyMatrix(transformation, point),
- element.centerPoint,
- ),
- };
-};
diff --git a/packages/docs/src/components/DoMoreHero/make-id.ts b/packages/docs/src/components/DoMoreHero/make-id.ts
deleted file mode 100644
index 12175a78f5..0000000000
--- a/packages/docs/src/components/DoMoreHero/make-id.ts
+++ /dev/null
@@ -1,5 +0,0 @@
-import {random} from 'remotion';
-
-export const makeId = () => {
- return random(null).toString().replace('.', '');
-};
diff --git a/packages/promo-pages/package.json b/packages/promo-pages/package.json
index a5f2129ed2..bf8b4b545c 100644
--- a/packages/promo-pages/package.json
+++ b/packages/promo-pages/package.json
@@ -27,7 +27,6 @@
},
"devDependencies": {
"@eslint/eslintrc": "3.1.0",
- "@types/hls.js": "1.0.0",
"@types/react": "19.0.0",
"@types/react-dom": "19.0.0",
"eslint": "9.14.0",
diff --git a/packages/studio-shared/src/package-info.ts b/packages/studio-shared/src/package-info.ts
index 0eebd56fee..ee46344f44 100644
--- a/packages/studio-shared/src/package-info.ts
+++ b/packages/studio-shared/src/package-info.ts
@@ -1,4 +1,5 @@
export const packages = [
+ 'svg-3d-engine',
'ai-improvements',
'animation-utils',
'animated-emoji',
@@ -149,6 +150,7 @@ export const descriptions: {[key in Pkgs]: string | null} = {
'eslint-config-internal': "ESLint condig for Remotion's internal packages",
'example-videos': null,
'promo-pages': null,
+ 'svg-3d-engine': '3D SVG extrusion effects',
};
export const apiDocs: {[key in Pkgs]: string | null} = {
@@ -225,4 +227,5 @@ export const apiDocs: {[key in Pkgs]: string | null} = {
compositor: null,
'example-videos': null,
'promo-pages': null,
+ 'svg-3d-engine': null,
};
diff --git a/packages/svg-3d-engine/.gitignore b/packages/svg-3d-engine/.gitignore
new file mode 100644
index 0000000000..1521c8b765
--- /dev/null
+++ b/packages/svg-3d-engine/.gitignore
@@ -0,0 +1 @@
+dist
diff --git a/packages/svg-3d-engine/bundle.ts b/packages/svg-3d-engine/bundle.ts
new file mode 100644
index 0000000000..fe49d9b1fb
--- /dev/null
+++ b/packages/svg-3d-engine/bundle.ts
@@ -0,0 +1,22 @@
+import {build} from 'bun';
+
+if (process.env.NODE_ENV !== 'production') {
+ throw new Error('This script must be run using NODE_ENV=production');
+}
+
+const output = await build({
+ entrypoints: ['src/index.ts'],
+ naming: '[name].mjs',
+ external: ['remotion', 'remotion/no-react'],
+});
+
+if (!output.success) {
+ throw new Error(output.logs.join('\n'));
+}
+
+const [file] = output.outputs;
+const text = await file.text();
+
+await Bun.write('dist/esm/index.mjs', text);
+
+export {};
diff --git a/packages/svg-3d-engine/eslint.config.mjs b/packages/svg-3d-engine/eslint.config.mjs
new file mode 100644
index 0000000000..04fd4fac11
--- /dev/null
+++ b/packages/svg-3d-engine/eslint.config.mjs
@@ -0,0 +1,7 @@
+import {remotionFlatConfig} from '@remotion/eslint-config-internal';
+
+const config = remotionFlatConfig({react: false});
+
+export default {
+ ...config,
+};
diff --git a/packages/svg-3d-engine/package.json b/packages/svg-3d-engine/package.json
new file mode 100644
index 0000000000..c2dd60426e
--- /dev/null
+++ b/packages/svg-3d-engine/package.json
@@ -0,0 +1,24 @@
+{
+ "name": "@remotion/svg-3d-engine",
+ "dependencies": {
+ "@remotion/paths": "workspace:*"
+ },
+ "devDependencies": {},
+ "main": "dist/cjs/index.js",
+ "types": "dist/cjs/index.d.ts",
+ "module": "dist/esm/index.mjs",
+ "scripts": {
+ "lint": "eslint src",
+ "make": "tsc -d && bun --env-file=../.env.bundle bundle.ts"
+ },
+ "exports": {
+ "./package.json": "./package.json",
+ ".": {
+ "types": "./dist/cjs/index.d.ts",
+ "module": "./dist/esm/index.mjs",
+ "import": "./dist/esm/index.mjs",
+ "require": "./dist/cjs/index.js"
+ }
+ },
+ "version": "4.0.248"
+}
diff --git a/packages/docs/src/components/DoMoreHero/3d-svg.ts b/packages/svg-3d-engine/src/3d-svg.ts
similarity index 96%
rename from packages/docs/src/components/DoMoreHero/3d-svg.ts
rename to packages/svg-3d-engine/src/3d-svg.ts
index 0aa9bc893e..b7a538e0b3 100644
--- a/packages/docs/src/components/DoMoreHero/3d-svg.ts
+++ b/packages/svg-3d-engine/src/3d-svg.ts
@@ -1,4 +1,4 @@
-import type {Vector4D} from './matrix';
+import {Vector4D} from './matrix';
export type ThreeDReducedInstruction =
| {
@@ -31,23 +31,18 @@ const serializeThreeDReducedInstruction = (
if (instruction.type === 'M') {
return `M ${instruction.point[0]} ${instruction.point[1]}`;
}
-
if (instruction.type === 'L') {
return `L ${instruction.point[0]} ${instruction.point[1]}`;
}
-
if (instruction.type === 'C') {
return `C ${instruction.cp1[0]} ${instruction.cp1[1]} ${instruction.cp2[0]} ${instruction.cp2[1]} ${instruction.point[0]} ${instruction.point[1]}`;
}
-
if (instruction.type === 'Q') {
return `Q ${instruction.cp[0]} ${instruction.cp[1]} ${instruction.point[0]} ${instruction.point[1]}`;
}
-
if (instruction.type === 'Z') {
return 'Z';
}
-
throw new Error('Unknown instruction type');
};
diff --git a/packages/svg-3d-engine/src/elements.ts b/packages/svg-3d-engine/src/elements.ts
new file mode 100644
index 0000000000..df27167227
--- /dev/null
+++ b/packages/svg-3d-engine/src/elements.ts
@@ -0,0 +1,74 @@
+import {makeId} from './make-id';
+import {FaceType, transformFace} from './map-face';
+import {
+ MatrixTransform4D,
+ multiplyMatrix,
+ translateX,
+ translateY,
+ translateZ,
+ Vector4D,
+} from './matrix';
+
+export type ThreeDElement = {
+ faces: FaceType[];
+ id: string;
+ description: string;
+ centerPoint: Vector4D;
+};
+
+export const makeElement = (
+ face: FaceType | FaceType[],
+ centerPoint: Vector4D,
+ description: string,
+): ThreeDElement => {
+ return {
+ faces: Array.isArray(face) ? face : [face],
+ id: makeId(),
+ centerPoint,
+ description,
+ };
+};
+
+export const transformElement = (
+ element: ThreeDElement,
+ transformations: MatrixTransform4D[],
+): ThreeDElement => {
+ return {
+ ...element,
+ faces: element.faces.map((face) => {
+ return transformFace(face, transformations);
+ }),
+ id: makeId(),
+ centerPoint: transformations.reduce(
+ (point, transformation) => multiplyMatrix(transformation, point),
+ element.centerPoint,
+ ),
+ };
+};
+
+export const transformAroundItself = (
+ element: ThreeDElement,
+ transformations: MatrixTransform4D[],
+): ThreeDElement => {
+ const actualTransformations = [
+ translateX(-element.centerPoint[0]),
+ translateY(-element.centerPoint[1]),
+ translateZ(-element.centerPoint[2]),
+ ...transformations,
+ translateX(element.centerPoint[0]),
+ translateY(element.centerPoint[1]),
+ translateZ(element.centerPoint[2]),
+ ];
+
+ return {
+ ...element,
+ faces: element.faces.map((face) => {
+ return transformFace(face, actualTransformations);
+ }),
+ id: makeId(),
+ centerPoint: transformations.reduce(
+ (point, transformation) => multiplyMatrix(transformation, point),
+ element.centerPoint,
+ ),
+ };
+};
diff --git a/packages/docs/src/components/DoMoreHero/join-inbetween-tiles.ts b/packages/svg-3d-engine/src/extrude-element.ts
similarity index 86%
rename from packages/docs/src/components/DoMoreHero/join-inbetween-tiles.ts
rename to packages/svg-3d-engine/src/extrude-element.ts
index 8af533b67a..935ce20a52 100644
--- a/packages/docs/src/components/DoMoreHero/join-inbetween-tiles.ts
+++ b/packages/svg-3d-engine/src/extrude-element.ts
@@ -1,61 +1,12 @@
-import type {Instruction} from '@remotion/paths';
-import {PathInternals, reduceInstructions} from '@remotion/paths';
-import type {ThreeDReducedInstruction} from './3d-svg';
-import type {ThreeDElement} from './element';
-import {makeElement} from './element';
+import {Instruction, PathInternals, reduceInstructions} from '@remotion/paths';
+import {ThreeDReducedInstruction} from './3d-svg';
+import {makeElement, ThreeDElement} from './elements';
import {turnInto3D} from './fix-z';
-import type {FaceType} from './map-face';
-import {transformFace, translateSvgInstruction} from './map-face';
-import type {Vector4D} from './matrix';
-import {translateZ} from './matrix';
-import {subdivideInstructions} from './subdivide-instruction';
+import {FaceType, transformFace, translateSvgInstruction} from './map-face';
+import {translateZ, Vector4D} from './matrix';
+import {subdivideInstructions} from './subdivide-instructions';
import {truthy} from './truthy';
-const inverseInstruction = (
- instruction: ThreeDReducedInstruction,
- comingFrom: Vector4D,
-): ThreeDReducedInstruction => {
- if (instruction.type === 'M') {
- return {
- type: 'M',
- point: comingFrom,
- };
- }
-
- if (instruction.type === 'L') {
- return {
- type: 'L',
- point: comingFrom,
- };
- }
-
- if (instruction.type === 'C') {
- return {
- type: 'C',
- point: comingFrom,
- cp1: instruction.cp2,
- cp2: instruction.cp1,
- };
- }
-
- if (instruction.type === 'Q') {
- return {
- type: 'Q',
- point: comingFrom,
- cp: instruction.cp,
- };
- }
-
- if (instruction.type === 'Z') {
- return {
- type: 'L',
- point: comingFrom,
- };
- }
-
- throw new Error('Unknown instruction type');
-};
-
export const extrudeElement = ({
depth,
sideColor,
@@ -65,6 +16,7 @@ export const extrudeElement = ({
strokeWidth,
description,
strokeColor,
+ crispEdges,
}: {
depth: number;
sideColor: string;
@@ -74,6 +26,7 @@ export const extrudeElement = ({
strokeWidth: number;
description: string;
strokeColor: string;
+ crispEdges: boolean;
}): ThreeDElement => {
const boundingBox = PathInternals.getBoundingBoxFromInstructions(
reduceInstructions(points),
@@ -92,7 +45,7 @@ export const extrudeElement = ({
strokeColor,
color: 'black',
description,
- crispEdges: false,
+ crispEdges,
};
const unscaledBackFace = transformFace(instructions, [translateZ(depth / 2)]);
@@ -172,3 +125,43 @@ export const extrudeElement = ({
description,
);
};
+
+const inverseInstruction = (
+ instruction: ThreeDReducedInstruction,
+ comingFrom: Vector4D,
+): ThreeDReducedInstruction => {
+ if (instruction.type === 'M') {
+ return {
+ type: 'M',
+ point: comingFrom,
+ };
+ }
+ if (instruction.type === 'L') {
+ return {
+ type: 'L',
+ point: comingFrom,
+ };
+ }
+ if (instruction.type === 'C') {
+ return {
+ type: 'C',
+ point: comingFrom,
+ cp1: instruction.cp2,
+ cp2: instruction.cp1,
+ };
+ }
+ if (instruction.type === 'Q') {
+ return {
+ type: 'Q',
+ point: comingFrom,
+ cp: instruction.cp,
+ };
+ }
+ if (instruction.type === 'Z') {
+ return {
+ type: 'L',
+ point: comingFrom,
+ };
+ }
+ throw new Error('Unknown instruction type');
+};
diff --git a/packages/docs/src/components/DoMoreHero/fix-z.ts b/packages/svg-3d-engine/src/fix-z.ts
similarity index 84%
rename from packages/docs/src/components/DoMoreHero/fix-z.ts
rename to packages/svg-3d-engine/src/fix-z.ts
index 21c24a2d1e..6e5846c911 100644
--- a/packages/docs/src/components/DoMoreHero/fix-z.ts
+++ b/packages/svg-3d-engine/src/fix-z.ts
@@ -1,7 +1,6 @@
-import type {Instruction} from '@remotion/paths';
-import {reduceInstructions} from '@remotion/paths';
-import type {ThreeDReducedInstruction} from './3d-svg';
-import type {Vector4D} from './matrix';
+import {Instruction, reduceInstructions} from '@remotion/paths';
+import {ThreeDReducedInstruction} from './3d-svg';
+import {Vector4D} from './matrix';
export const turnInto3D = (
instructions: Instruction[],
diff --git a/packages/svg-3d-engine/src/index.ts b/packages/svg-3d-engine/src/index.ts
new file mode 100644
index 0000000000..ad63edea1c
--- /dev/null
+++ b/packages/svg-3d-engine/src/index.ts
@@ -0,0 +1,13 @@
+export type {ThreeDReducedInstruction} from './3d-svg';
+
+export {transformElement} from './elements';
+export {extrudeElement} from './extrude-element';
+export {
+ rotateX,
+ rotateY,
+ rotateZ,
+ scaled,
+ translateX,
+ translateY,
+ translateZ,
+} from './matrix';
diff --git a/packages/svg-3d-engine/src/make-id.ts b/packages/svg-3d-engine/src/make-id.ts
new file mode 100644
index 0000000000..aaf8ed7a3a
--- /dev/null
+++ b/packages/svg-3d-engine/src/make-id.ts
@@ -0,0 +1,3 @@
+export const makeId = () => {
+ return Math.random().toString().replace('.', '');
+};
diff --git a/packages/docs/src/components/DoMoreHero/map-face.ts b/packages/svg-3d-engine/src/map-face.ts
similarity index 91%
rename from packages/docs/src/components/DoMoreHero/map-face.ts
rename to packages/svg-3d-engine/src/map-face.ts
index db1584d3d7..9ec460cfcf 100644
--- a/packages/docs/src/components/DoMoreHero/map-face.ts
+++ b/packages/svg-3d-engine/src/map-face.ts
@@ -1,10 +1,13 @@
import {parsePath} from '@remotion/paths';
-import type {ThreeDReducedInstruction} from './3d-svg';
-import type {ThreeDElement} from './element';
-import {transformElement} from './element';
+import {ThreeDReducedInstruction} from './3d-svg';
+import {ThreeDElement, transformElement} from './elements';
import {turnInto3D} from './fix-z';
-import type {MatrixTransform4D, Vector4D} from './matrix';
-import {multiplyMatrix, multiplyMatrixAndSvgInstruction} from './matrix';
+import {
+ MatrixTransform4D,
+ multiplyMatrix,
+ multiplyMatrixAndSvgInstruction,
+ Vector4D,
+} from './matrix';
export type FaceType = {
color: string;
@@ -33,7 +36,6 @@ export const translateSvgInstruction = (
],
};
}
-
if (instruction.type === 'L') {
return {
type: 'L',
@@ -45,7 +47,6 @@ export const translateSvgInstruction = (
],
};
}
-
if (instruction.type === 'C') {
return {
type: 'C',
@@ -69,7 +70,6 @@ export const translateSvgInstruction = (
],
};
}
-
if (instruction.type === 'Q') {
return {
type: 'Q',
@@ -87,7 +87,6 @@ export const translateSvgInstruction = (
],
};
}
-
if (instruction.type === 'Z') {
return {
type: 'Z',
@@ -99,7 +98,6 @@ export const translateSvgInstruction = (
],
};
}
-
throw new Error('Unknown instruction type: ' + JSON.stringify(instruction));
};
diff --git a/packages/docs/src/components/DoMoreHero/matrix.ts b/packages/svg-3d-engine/src/matrix.ts
similarity index 98%
rename from packages/docs/src/components/DoMoreHero/matrix.ts
rename to packages/svg-3d-engine/src/matrix.ts
index 31e4537192..17e846caa0 100644
--- a/packages/docs/src/components/DoMoreHero/matrix.ts
+++ b/packages/svg-3d-engine/src/matrix.ts
@@ -1,4 +1,8 @@
-import type {ThreeDReducedInstruction} from './3d-svg';
+import {ThreeDReducedInstruction} from './3d-svg';
+
+export const translated4d = function (vec: Vector) {
+ return stride({v: vec, m: identity4(), width: 4, offset: 3, colStride: 0});
+};
export const stride = function ({
v,
@@ -19,7 +23,6 @@ export const stride = function ({
((i * colStride + offset + width) % width) // Row
] = v[i];
}
-
return m;
};
@@ -30,21 +33,39 @@ export const identity4 = function (): MatrixTransform4D {
while (size--) {
m[size] = size % (n + 1) === 0 ? 1.0 : 0.0;
}
-
return m;
};
-export const translated4d = function (vec: Vector) {
- return stride({v: vec, m: identity4(), width: 4, offset: 3, colStride: 0});
+export const m44multiply = (
+ ...matrices: MatrixTransform4D[]
+): MatrixTransform4D => {
+ return multiplyMany(4, matrices);
};
+// Accept an integer indicating the size of the matrices being multiplied (3 for 3x3), and any
+// number of matrices following it.
+function multiplyMany(
+ size: number,
+ listOfMatrices: MatrixTransform4D[],
+): MatrixTransform4D {
+ if (listOfMatrices.length < 2) {
+ throw new Error('multiplication expected two or more matrices');
+ }
+ let result = mul(listOfMatrices[0], listOfMatrices[1], size);
+ let next = 2;
+ while (next < listOfMatrices.length) {
+ result = mul(result, listOfMatrices[next], size);
+ next++;
+ }
+ return result as MatrixTransform4D;
+}
+
function mul(m1: number[], m2: number[], size: number): number[] {
if (m1.length !== m2.length) {
throw new Error(
`Undefined for matrices of different sizes. m1.length=${m1.length}, m2.length=${m2.length}`,
);
}
-
if (size * size !== m1.length) {
throw new Error(
`Undefined for non-square matrices. array size was ${size}`,
@@ -59,44 +80,34 @@ function mul(m1: number[], m2: number[], size: number): number[] {
for (let k = 0; k < size; k++) {
acc += m1[size * r + k] * m2[size * k + c];
}
-
result[r * size + c] = acc;
}
}
-
return result;
}
-// Accept an integer indicating the size of the matrices being multiplied (3 for 3x3), and any
-// number of matrices following it.
-function multiplyMany(
- size: number,
- listOfMatrices: MatrixTransform4D[],
-): MatrixTransform4D {
- if (listOfMatrices.length < 2) {
- throw new Error('multiplication expected two or more matrices');
- }
-
- let result = mul(listOfMatrices[0], listOfMatrices[1], size);
- let next = 2;
- while (next < listOfMatrices.length) {
- result = mul(result, listOfMatrices[next], size);
- next++;
- }
-
- return result as MatrixTransform4D;
-}
-
-export const m44multiply = (
- ...matrices: MatrixTransform4D[]
-): MatrixTransform4D => {
- return multiplyMany(4, matrices);
-};
-
export type Vector2D = [number, number];
export type Vector = [number, number, number];
export type Vector4D = [number, number, number, number];
+const rotated = function (axisVec: Vector, radians: number): MatrixTransform4D {
+ return rotatedUnitSinCos(
+ normalize(axisVec),
+ Math.sin(radians),
+ Math.cos(radians),
+ );
+};
+
+export const rotateX = (radians: number) => {
+ return rotated([1, 0, 0], radians);
+};
+export const rotateY = (radians: number) => {
+ return rotated([0, 1, 0], radians);
+};
+export const rotateZ = (radians: number) => {
+ return rotated([0, 0, 1], radians);
+};
+
export type MatrixTransform4D = [
number,
number,
@@ -152,13 +163,27 @@ const rotatedUnitSinCos = function (
];
};
+export const normalize = function (v: Vector): Vector {
+ return mulScalar(v, 1 / vectorLength(v));
+};
+
+export const normalize4d = function (v: Vector4D): Vector4D {
+ return mulScalar(v, 1 / vectorLength(v));
+};
+
+const vectorLength = function (v: number[]) {
+ return Math.sqrt(lengthSquared(v));
+};
+
+const lengthSquared = function (v: number[]) {
+ return dot(v, v);
+};
export const dot = function (a: number[], b: number[]) {
if (a.length !== b.length) {
throw new Error(
`Cannot perform dot product on arrays of different length (${a.length} vs ${b.length})`,
);
}
-
return a
.map((v, i) => {
return v * b[i];
@@ -190,48 +215,6 @@ export const translateZ = (z: number) => {
return translated([0, 0, z]);
};
-const lengthSquared = function (v: number[]) {
- return dot(v, v);
-};
-
-const vectorLength = function (v: number[]) {
- return Math.sqrt(lengthSquared(v));
-};
-
-export const mulScalar = function <T extends number[]>(v: T, s: number): T {
- return v.map((i) => {
- return i * s;
- }) as T;
-};
-
-export const normalize = function (v: Vector): Vector {
- return mulScalar(v, 1 / vectorLength(v));
-};
-
-export const normalize4d = function (v: Vector4D): Vector4D {
- return mulScalar(v, 1 / vectorLength(v));
-};
-
-const rotated = function (axisVec: Vector, radians: number): MatrixTransform4D {
- return rotatedUnitSinCos(
- normalize(axisVec),
- Math.sin(radians),
- Math.cos(radians),
- );
-};
-
-export const rotateX = (radians: number) => {
- return rotated([1, 0, 0], radians);
-};
-
-export const rotateY = (radians: number) => {
- return rotated([0, 1, 0], radians);
-};
-
-export const rotateZ = (radians: number) => {
- return rotated([0, 0, 1], radians);
-};
-
export type Camera = {
near: number;
far: number;
@@ -241,23 +224,10 @@ export type Camera = {
up: Vector;
};
-export const multiplyMatrix = (
- matrix: MatrixTransform4D,
- point: Vector4D,
-): Vector4D => {
- if (matrix.length !== 16 || point.length !== 4) {
- throw new Error('Invalid matrix or vector dimension');
- }
-
- const result: Vector4D = [0, 0, 0, 0];
-
- for (let i = 0; i < 4; i++) {
- for (let j = 0; j < 4; j++) {
- result[i] += matrix[i * 4 + j] * point[j];
- }
- }
-
- return result;
+export const mulScalar = function <T extends number[]>(v: T, s: number): T {
+ return v.map((i) => {
+ return i * s;
+ }) as T;
};
export function multiplyMatrixAndSvgInstruction(
@@ -272,7 +242,6 @@ export function multiplyMatrixAndSvgInstruction(
point: multiplyMatrix(matrix, point.point),
};
}
-
if (point.type === 'Q') {
return {
type: 'Q',
@@ -280,31 +249,46 @@ export function multiplyMatrixAndSvgInstruction(
point: multiplyMatrix(matrix, point.point),
};
}
-
if (point.type === 'M') {
return {
type: 'M',
point: multiplyMatrix(matrix, point.point),
};
}
-
if (point.type === 'L') {
return {
type: 'L',
point: multiplyMatrix(matrix, point.point),
};
}
-
if (point.type === 'Z') {
return {
type: 'Z',
point: multiplyMatrix(matrix, point.point),
};
}
-
throw new Error('Unknown instruction type: ' + JSON.stringify(point));
}
+export const multiplyMatrix = (
+ matrix: MatrixTransform4D,
+ point: Vector4D,
+): Vector4D => {
+ if (matrix.length !== 16 || point.length !== 4) {
+ throw new Error('Invalid matrix or vector dimension');
+ }
+
+ const result: Vector4D = [0, 0, 0, 0];
+
+ for (let i = 0; i < 4; i++) {
+ for (let j = 0; j < 4; j++) {
+ result[i] += matrix[i * 4 + j] * point[j];
+ }
+ }
+
+ return result;
+};
+
export const sub4d = function (a: Vector4D, b: Vector4D): Vector4D {
return a.map((v, i) => {
return v - b[i];
diff --git a/packages/docs/src/components/DoMoreHero/subdivide-instruction.ts b/packages/svg-3d-engine/src/subdivide-instructions.ts
similarity index 100%
rename from packages/docs/src/components/DoMoreHero/subdivide-instruction.ts
rename to packages/svg-3d-engine/src/subdivide-instructions.ts
diff --git a/packages/svg-3d-engine/src/truthy.ts b/packages/svg-3d-engine/src/truthy.ts
new file mode 100644
index 0000000000..56f9b01ea9
--- /dev/null
+++ b/packages/svg-3d-engine/src/truthy.ts
@@ -0,0 +1,4 @@
+type Truthy<T> = T extends false | '' | 0 | null | undefined ? never : T;
+export function truthy<T>(value: T): value is Truthy<T> {
+ return Boolean(value);
+}
diff --git a/packages/svg-3d-engine/tsconfig.json b/packages/svg-3d-engine/tsconfig.json
new file mode 100644
index 0000000000..60fcba5496
--- /dev/null
+++ b/packages/svg-3d-engine/tsconfig.json
@@ -0,0 +1,14 @@
+{
+ "extends": "../tsconfig.settings.json",
+ "compilerOptions": {
+ "rootDir": "./src",
+ "outDir": "./dist/cjs",
+ "skipLibCheck": true
+ },
+ "include": ["src"],
+ "references": [
+ {
+ "path": "../paths"
+ }
+ ]
+}
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 1a78e01f1c..06fe0ec426 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -677,6 +677,9 @@ importers:
'@remotion/studio':
specifier: workspace:*
version: link:../studio
+ '@remotion/svg-3d-engine':
+ specifier: workspace:*
+ version: link:../svg-3d-engine
'@remotion/tailwind':
specifier: workspace:*
version: link:../tailwind
@@ -1693,9 +1696,6 @@ importers:
'@tailwindcss/vite':
specifier: 4.0.0-beta.9
version: 4.0.0-beta.9([email protected])
- '@types/hls.js':
- specifier: 1.0.0
- version: 1.0.0
'@types/react':
specifier: 19.0.0
version: 19.0.0
@@ -2036,6 +2036,12 @@ importers:
specifier: 9.14.0
version: 9.14.0
+ packages/svg-3d-engine:
+ dependencies:
+ '@remotion/paths':
+ specifier: workspace:*
+ version: link:../paths
+
packages/tailwind:
dependencies:
autoprefixer:
@@ -14882,13 +14888,6 @@ packages:
resolution: {integrity: sha512-Po8ZPCsAcPPuf5OODPEkb6cdWJ/w4BdX1veP7IIOc2WG0x1SW4GEQ1+FHKN1AMG2AePJfNUceJbh5PKtP92yRQ==}
- resolution: {integrity: sha512-EGY2QJefX+Z9XH4PAxI7RFoNqBlQEk16UpYR3kbr82CIgMX5SlMe0PjFdFV0JytRhyVPQCiwSyONuI6S1KdSag==}
- deprecated: This is a stub types definition. hls.js provides its own type definitions, so you do not need this installed.
- dependencies:
- hls.js: 0.14.17
- dev: true
-
resolution: {integrity: sha512-iMIqiko6ooLrTh1joXodJK5X9xeEALT1kM5G3ZLhD3hszxBdIEd5C75U834D9mLcINgD4OyZf5uQXjkuYydWvA==}
dependencies:
@@ -20383,6 +20382,7 @@ packages:
resolution: {integrity: sha512-8guHBZCwKnFhYdHr2ysuRWErTwhoN2X8XELRlrRwpmfeY2jjuUN4taQMsULKUVo1K4DvZl+0pgfyoysHxvmvEw==}
+ dev: false
resolution: {integrity: sha512-GWkBvjiSZK87ELrYOSESUYeVIc9mvLLf/nXalMOS5dYrgZq9o5OVkbZAVM06CVxYsCwH9BDZFPlQTlPA1j4ahA==}
@@ -21714,6 +21714,7 @@ packages:
dependencies:
eventemitter3: 4.0.7
url-toolkit: 2.2.3
+ dev: false
resolution: {integrity: sha512-wA66nnYFvQa1o4DO/BFgLNRKnBTVXpNeldGRBJ2Y0SvFtdwvFKCbqa9zhHoZLoxHhZ+jYsj3aIBkWQQCPNOhMw==}
@@ -30244,6 +30245,7 @@ packages:
resolution: {integrity: sha512-Da75SQoxsZ+2wXS56CZBrj2nukQ4nlGUZUP/dqUBG5E1su5GKThgT94Q00x81eVII7AyS1Pn+CtTTZ4Z0pLUtQ==}
+ dev: false
resolution: {integrity: sha512-7/hqDrWa0iMnCoET9W1T07EmD4Eg/Wmoj/X8TGBc++ECRK4m5yTsjP4O6s0yagbxfqIOuUkIxe2/sA+VR2GxZA==}