Skip to content

🎥 Make videos programmatically with React

3d engine simplification

Jonny Burgerdd767656679b
diff --git a/packages/example/src/3DCheck/index.tsx b/packages/example/src/3DCheck/index.tsx
new file mode 100644
index 0000000000..3281d1a0f7
--- /dev/null
+++ b/packages/example/src/3DCheck/index.tsx
@@ -0,0 +1,77 @@
+import {AbsoluteFill, spring, useCurrentFrame, useVideoConfig} from 'remotion';
+import {Div3D} from '../3DContext/Div3D';
+import {RotateY} from '../3DContext/transformation-context';
+
+const Comp = () => {
+ const width = 70;
+ const height = 70;
+
+ const cornerRadius = 70 / 2;
+ const frame = useCurrentFrame();
+ const {fps} = useVideoConfig();
+
+ const progress = spring({
+ fps,
+ frame,
+ });
+
+ return (
+ <AbsoluteFill className="flex justify-center items-center bg-[#F8FAFC]">
+ <RotateY radians={progress * Math.PI}>
+ <Div3D
+ width={width}
+ height={height}
+ depth={20}
+ cornerRadius={cornerRadius}
+ backFace={
+ <svg
+ style={{
+ height: '100%',
+ width: '100%',
+ border: '4px solid black',
+ borderRadius: cornerRadius,
+ }}
+ viewBox="0 0 12 12"
+ fill="none"
+ xmlns="http://www.w3.org/2000/svg"
+ >
+ <circle cx="6" cy="6" r="6" fill="#D9D9D9" />
+ <path
+ d="M6.29386 4.43263L6.32673 5.43569C6.33097 5.56525 6.44102 5.66787 6.57057 5.66307L7.57347 5.62589C7.63615 5.62357 7.69592 5.59624 7.73836 5.55073L9.11055 4.07924C9.2217 3.96004 9.41711 3.98627 9.47441 4.13898C9.84428 5.11594 9.65493 6.26427 8.89103 7.08346C8.1726 7.85388 7.12965 8.13734 6.17487 7.91957L4.94515 9.23828C4.67738 9.52543 4.22817 9.54208 3.94225 9.27546L2.90647 8.30959C2.62056 8.04297 2.60583 7.59368 2.8736 7.30653L4.10332 5.98782C3.81949 5.05056 4.0295 3.99038 4.74793 3.21995C5.51183 2.40077 6.64416 2.13178 7.64556 2.43151C7.80188 2.47802 7.84168 2.67112 7.72952 2.7914L6.35733 4.2629C6.31489 4.30841 6.2918 4.36994 6.29386 4.43263Z"
+ fill="#333"
+ />
+ </svg>
+ }
+ >
+ <div
+ style={{
+ backgroundColor: '#0B84F3',
+ flex: 1,
+ borderRadius: '50%',
+ border: '4px solid black',
+ display: 'flex',
+ justifyContent: 'center',
+ alignItems: 'center',
+ }}
+ >
+ <svg
+ xmlns="http://www.w3.org/2000/svg"
+ viewBox="0 0 448 512"
+ style={{
+ height: '70%',
+ marginTop: 4,
+ }}
+ >
+ <path
+ fill="white"
+ d="M438.6 105.4c12.5 12.5 12.5 32.8 0 45.3l-256 256c-12.5 12.5-32.8 12.5-45.3 0l-128-128c-12.5-12.5-12.5-32.8 0-45.3s32.8-12.5 45.3 0L160 338.7 393.4 105.4c12.5-12.5 32.8-12.5 45.3 0z"
+ />
+ </svg>
+ </div>
+ </Div3D>
+ </RotateY>
+ </AbsoluteFill>
+ );
+};
+
+export const ThreeDCheck = Comp;
diff --git a/packages/example/src/3DContext/Div3D.tsx b/packages/example/src/3DContext/Div3D.tsx
index 37d8ab0d5a..131f8cff59 100644
--- a/packages/example/src/3DContext/Div3D.tsx
+++ b/packages/example/src/3DContext/Div3D.tsx
@@ -24,7 +24,7 @@ export const Div3D: React.FC<{
}}
>
<DivExtrusion depth={depth} />
- {frontFace ? (
+ {!frontFace ? (
<Face type="front" depth={depth}>
{children}
</Face>
diff --git a/packages/example/src/3DContext/FrontFace.tsx b/packages/example/src/3DContext/FrontFace.tsx
index 9e08b1da5d..06ec809948 100644
--- a/packages/example/src/3DContext/FrontFace.tsx
+++ b/packages/example/src/3DContext/FrontFace.tsx
@@ -1,7 +1,7 @@
import {
makeMatrix3dTransform,
reduceMatrices,
- scaleY,
+ scaleX,
translateZ,
} from '@remotion/svg-3d-engine';
import {useRect} from './path-context';
@@ -12,12 +12,13 @@ export const Face: React.FC<{
depth: number;
type: 'front' | 'back';
}> = ({children, depth, type}) => {
+ const {width, height} = useRect();
+
const frontFace = reduceMatrices([
- type === 'back' ? scaleY(-1) : null,
- translateZ(type === 'front' ? -depth : 0.01 * depth),
+ type === 'back' ? scaleX(-1) : null,
+ translateZ(type === 'front' ? -depth * 0.49 : 0.49 * depth),
useTransformations(),
]);
- const {height, width} = useRect();
return (
<div
diff --git a/packages/example/src/3DSvgContent/G3D.tsx b/packages/example/src/3DSvgContent/G3D.tsx
index 1db6699b7f..9e53bb33e1 100644
--- a/packages/example/src/3DSvgContent/G3D.tsx
+++ b/packages/example/src/3DSvgContent/G3D.tsx
@@ -33,7 +33,6 @@ export const G3D: React.FC<
-(strokeWidth ?? 0) / 2,
);
}, [d, strokeWidth]);
- console.log({isFrontFacing});
return (
<PathProvider d={scaledPath}>
diff --git a/packages/example/src/Root.tsx b/packages/example/src/Root.tsx
index 63dceac205..051bff695e 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 {ThreeDCheck} from './3DCheck';
import {ThreeDContext} from './3DContext';
import {ThreeDEngine} from './3DEngine';
import {ThreeDSvgContent} from './3DSvgContent';
@@ -1489,6 +1490,14 @@ export const Index: React.FC = () => {
<Still id="HugeImage" component={HugeImage} height={9000} width={9000} />
<Folder name="3DEngine">
<ThreeDEngine />
+ <Composition
+ id="3DCheck"
+ component={ThreeDCheck}
+ width={1080}
+ height={1080}
+ fps={30}
+ durationInFrames={1000}
+ />
<Composition
id="3DContext"
component={ThreeDContext}
diff --git a/packages/svg-3d-engine/src/extrude-element.ts b/packages/svg-3d-engine/src/extrude-element.ts
index 9c23892754..d2da3b99a7 100644
--- a/packages/svg-3d-engine/src/extrude-element.ts
+++ b/packages/svg-3d-engine/src/extrude-element.ts
@@ -65,7 +65,6 @@ export const extrudeElement = ({
depth,
sideColor,
points,
- description = 'extruded',
crispEdges,
}: ExtrudeElementOptions): FaceType[] => {
const threeD = turnInto3D(points);
@@ -75,11 +74,10 @@ export const extrudeElement = ({
subdivideInstructions(subdivideInstructions(threeD)),
),
color: 'black',
- description: description ?? 'extruded',
crispEdges,
};
- const unscaledBackFace = transformFace(instructions, [translateZ(0)]);
+ const unscaledBackFace = transformFace(instructions, [translateZ(depth / 2)]);
const inbetween = unscaledBackFace.points.map((t, i): FaceType => {
const nextInstruction =
@@ -129,7 +127,6 @@ export const extrudeElement = ({
points: newInstructions,
color: sideColor,
centerPoint: [0, 0, 0, 1],
- description: description + 'inbetween',
crispEdges: true,
};
});
diff --git a/packages/svg-3d-engine/src/map-face.ts b/packages/svg-3d-engine/src/map-face.ts
index 09970e94c2..5b0aa32d66 100644
--- a/packages/svg-3d-engine/src/map-face.ts
+++ b/packages/svg-3d-engine/src/map-face.ts
@@ -8,7 +8,6 @@ export type FaceType = {
color: string;
points: ThreeDReducedInstruction[];
centerPoint: Vector4D;
- description: string;
crispEdges: boolean;
};
@@ -149,14 +148,12 @@ export const transformFaces = ({
export const makeFace = ({
points,
fill,
- description,
crispEdges,
}: {
points: string | ThreeDReducedInstruction[];
strokeWidth: number;
strokeColor: string;
fill: string;
- description: string;
crispEdges: boolean;
}): FaceType => {
const centerPoint: Vector4D = [0, 0, 0, 1];
@@ -165,7 +162,6 @@ export const makeFace = ({
centerPoint,
color: fill,
points: typeof points === 'string' ? turnInto3D(parsePath(points)) : points,
- description,
crispEdges,
};
};