@remotion/svg-3d-engine 4.0.363 → 4.0.365

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/matrix.ts DELETED
@@ -1,425 +0,0 @@
1
- /* eslint-disable @typescript-eslint/no-use-before-define */
2
- import type {ThreeDReducedInstruction} from './3d-svg';
3
- import {truthy} from './truthy';
4
-
5
- export const stride = function ({
6
- v,
7
- m,
8
- width,
9
- offset,
10
- colStride,
11
- }: {
12
- v: Vector;
13
- m: MatrixTransform4D;
14
- width: number;
15
- offset: number;
16
- colStride: number;
17
- }) {
18
- for (let i = 0; i < v.length; i++) {
19
- m[
20
- i * width + // Column
21
- ((i * colStride + offset + width) % width) // Row
22
- ] = v[i];
23
- }
24
-
25
- return m;
26
- };
27
-
28
- export const identity4 = function (): MatrixTransform4D {
29
- const n = 4;
30
- let size = n * n;
31
- const m = new Array(size) as MatrixTransform4D;
32
- while (size--) {
33
- m[size] = size % (n + 1) === 0 ? 1.0 : 0.0;
34
- }
35
-
36
- return m;
37
- };
38
-
39
- export const m44multiply = (
40
- ...matrices: MatrixTransform4D[]
41
- ): MatrixTransform4D => {
42
- return multiplyMany(4, matrices);
43
- };
44
-
45
- // Accept an integer indicating the size of the matrices being multiplied (3 for 3x3), and any
46
- // number of matrices following it.
47
- function multiplyMany(
48
- size: number,
49
- listOfMatrices: MatrixTransform4D[],
50
- ): MatrixTransform4D {
51
- if (listOfMatrices.length < 2) {
52
- throw new Error('multiplication expected two or more matrices');
53
- }
54
-
55
- let result = mul(listOfMatrices[0], listOfMatrices[1], size);
56
- let next = 2;
57
- while (next < listOfMatrices.length) {
58
- result = mul(result, listOfMatrices[next], size);
59
- next++;
60
- }
61
-
62
- return result as MatrixTransform4D;
63
- }
64
-
65
- function mul(m1: number[], m2: number[], size: number): number[] {
66
- if (m1.length !== m2.length) {
67
- throw new Error(
68
- `Undefined for matrices of different sizes. m1.length=${m1.length}, m2.length=${m2.length}`,
69
- );
70
- }
71
-
72
- if (size * size !== m1.length) {
73
- throw new Error(
74
- `Undefined for non-square matrices. array size was ${size}`,
75
- );
76
- }
77
-
78
- const result = Array(m1.length);
79
- for (let r = 0; r < size; r++) {
80
- for (let c = 0; c < size; c++) {
81
- // Accumulate a sum of m1[r,k]*m2[k, c]
82
- let acc = 0;
83
- for (let k = 0; k < size; k++) {
84
- acc += m1[size * r + k] * m2[size * k + c];
85
- }
86
-
87
- result[r * size + c] = acc;
88
- }
89
- }
90
-
91
- return result;
92
- }
93
-
94
- export type Vector2D = [number, number];
95
- export type Vector = [number, number, number];
96
- export type Vector4D = [number, number, number, number];
97
-
98
- const rotated = function (axisVec: Vector, radians: number): MatrixTransform4D {
99
- return rotatedUnitSinCos(
100
- normalize(axisVec),
101
- Math.sin(radians),
102
- Math.cos(radians),
103
- );
104
- };
105
-
106
- export const rotateX = (radians: number) => {
107
- return rotated([1, 0, 0], radians);
108
- };
109
-
110
- export const rotateY = (radians: number) => {
111
- return rotated([0, 1, 0], radians);
112
- };
113
-
114
- export const translated4d = function (vec: Vector) {
115
- return stride({v: vec, m: identity4(), width: 4, offset: 3, colStride: 0});
116
- };
117
-
118
- export const rotateZ = (radians: number) => {
119
- return rotated([0, 0, 1], radians);
120
- };
121
-
122
- export type MatrixTransform4D = [
123
- number,
124
- number,
125
- number,
126
- number,
127
- number,
128
- number,
129
- number,
130
- number,
131
- number,
132
- number,
133
- number,
134
- number,
135
- number,
136
- number,
137
- number,
138
- number,
139
- ];
140
-
141
- export const multiplyMatrices = (
142
- matrix1: MatrixTransform4D,
143
- matrix2: MatrixTransform4D,
144
- ): MatrixTransform4D => {
145
- const result: MatrixTransform4D = new Array(16).fill(0) as MatrixTransform4D;
146
-
147
- for (let i = 0; i < 4; i++) {
148
- for (let j = 0; j < 4; j++) {
149
- for (let k = 0; k < 4; k++) {
150
- result[i * 4 + j] += matrix1[i * 4 + k] * matrix2[k * 4 + j];
151
- }
152
- }
153
- }
154
-
155
- return result;
156
- };
157
-
158
- export const reduceMatrices = (
159
- matrices: (MatrixTransform4D | null)[],
160
- ): MatrixTransform4D => {
161
- return matrices
162
- .filter(truthy)
163
- .slice()
164
- .reverse()
165
- .reduce((acc, cur) => {
166
- return multiplyMatrices(acc, cur);
167
- }, identity4());
168
- };
169
-
170
- export const aroundCenterPoint = ({
171
- matrix,
172
- x,
173
- y,
174
- z,
175
- }: {
176
- matrix: MatrixTransform4D;
177
- x: number;
178
- y: number;
179
- z: number;
180
- }) => {
181
- return reduceMatrices([
182
- translateX(-x),
183
- translateY(-y),
184
- translateZ(-z),
185
- matrix,
186
- translateX(x),
187
- translateY(y),
188
- translateZ(z),
189
- ]);
190
- };
191
-
192
- export const makeMatrix3dTransform = function (
193
- matrix: MatrixTransform4D,
194
- ): string {
195
- return `matrix3d(${[
196
- matrix[0],
197
- matrix[4],
198
- matrix[8],
199
- matrix[12], // First column
200
- matrix[1],
201
- matrix[5],
202
- matrix[9],
203
- matrix[13], // Second column
204
- matrix[2],
205
- matrix[6],
206
- matrix[10],
207
- matrix[14], // Third column
208
- matrix[3],
209
- matrix[7],
210
- matrix[11],
211
- matrix[15], // Fourth column
212
- ].join(', ')}`;
213
- };
214
-
215
- const interpolate = (a: number, b: number, t: number) => {
216
- return a + (b - a) * t;
217
- };
218
-
219
- export const interpolateMatrix4d = (
220
- input: number,
221
- matrix1: MatrixTransform4D,
222
- matrix2: MatrixTransform4D,
223
- ): MatrixTransform4D => {
224
- return [
225
- interpolate(matrix1[0], matrix2[0], input),
226
- interpolate(matrix1[1], matrix2[1], input),
227
- interpolate(matrix1[2], matrix2[2], input),
228
- interpolate(matrix1[3], matrix2[3], input),
229
- interpolate(matrix1[4], matrix2[4], input),
230
- interpolate(matrix1[5], matrix2[5], input),
231
- interpolate(matrix1[6], matrix2[6], input),
232
- interpolate(matrix1[7], matrix2[7], input),
233
- interpolate(matrix1[8], matrix2[8], input),
234
- interpolate(matrix1[9], matrix2[9], input),
235
- interpolate(matrix1[10], matrix2[10], input),
236
- interpolate(matrix1[11], matrix2[11], input),
237
- interpolate(matrix1[12], matrix2[12], input),
238
- interpolate(matrix1[13], matrix2[13], input),
239
- interpolate(matrix1[14], matrix2[14], input),
240
- interpolate(matrix1[15], matrix2[15], input),
241
- ] as const;
242
- };
243
-
244
- export const scaled = function (value: number | Vector) {
245
- const vec: Vector = typeof value === 'number' ? [value, value, value] : value;
246
- return stride({v: vec, m: identity4(), width: 4, offset: 0, colStride: 1});
247
- };
248
-
249
- export const scaleX = (x: number) => {
250
- return scaled([x, 1, 1]);
251
- };
252
-
253
- export const scaleY = (y: number) => {
254
- return scaled([1, y, 1]);
255
- };
256
-
257
- const rotatedUnitSinCos = function (
258
- axisVec: Vector,
259
- sinAngle: number,
260
- cosAngle: number,
261
- ): MatrixTransform4D {
262
- const x = axisVec[0];
263
- const y = axisVec[1];
264
- const z = axisVec[2];
265
- const c = cosAngle;
266
- const s = sinAngle;
267
- const t = 1 - c;
268
- return [
269
- t * x * x + c,
270
- t * x * y - s * z,
271
- t * x * z + s * y,
272
- 0,
273
- t * x * y + s * z,
274
- t * y * y + c,
275
- t * y * z - s * x,
276
- 0,
277
- t * x * z - s * y,
278
- t * y * z + s * x,
279
- t * z * z + c,
280
- 0,
281
- 0,
282
- 0,
283
- 0,
284
- 1,
285
- ];
286
- };
287
-
288
- export const normalize = function (v: Vector): Vector {
289
- return mulScalar(v, 1 / vectorLength(v));
290
- };
291
-
292
- export const normalize4d = function (v: Vector4D): Vector4D {
293
- return mulScalar(v, 1 / vectorLength(v));
294
- };
295
-
296
- const vectorLength = function (v: number[]) {
297
- return Math.sqrt(lengthSquared(v));
298
- };
299
-
300
- const lengthSquared = function (v: number[]) {
301
- return dot(v, v);
302
- };
303
-
304
- export const dot = function (a: number[], b: number[]) {
305
- if (a.length !== b.length) {
306
- throw new Error(
307
- `Cannot perform dot product on arrays of different length (${a.length} vs ${b.length})`,
308
- );
309
- }
310
-
311
- return a
312
- .map((v, i) => {
313
- return v * b[i];
314
- })
315
- .reduce((acc, cur) => {
316
- return acc + cur;
317
- });
318
- };
319
-
320
- const translated = function (vec: Vector) {
321
- return stride({
322
- v: vec,
323
- m: identity4(),
324
- width: 4,
325
- offset: 3,
326
- colStride: 0,
327
- });
328
- };
329
-
330
- export const translateX = (x: number) => {
331
- return translated([x, 0, 0]);
332
- };
333
-
334
- export const translateY = (y: number) => {
335
- return translated([0, y, 0]);
336
- };
337
-
338
- export const translateZ = (z: number) => {
339
- return translated([0, 0, z]);
340
- };
341
-
342
- export type Camera = {
343
- near: number;
344
- far: number;
345
- angle: number;
346
- eye: Vector;
347
- coa: Vector;
348
- up: Vector;
349
- };
350
-
351
- export const mulScalar = function <T extends number[]>(v: T, s: number): T {
352
- return v.map((i) => {
353
- return i * s;
354
- }) as T;
355
- };
356
-
357
- export function multiplyMatrixAndSvgInstruction(
358
- matrix: MatrixTransform4D,
359
- point: ThreeDReducedInstruction,
360
- ): ThreeDReducedInstruction {
361
- if (point.type === 'C') {
362
- return {
363
- type: 'C',
364
- cp1: multiplyMatrix(matrix, point.cp1),
365
- cp2: multiplyMatrix(matrix, point.cp2),
366
- point: multiplyMatrix(matrix, point.point),
367
- };
368
- }
369
-
370
- if (point.type === 'Q') {
371
- return {
372
- type: 'Q',
373
- cp: multiplyMatrix(matrix, point.cp),
374
- point: multiplyMatrix(matrix, point.point),
375
- };
376
- }
377
-
378
- if (point.type === 'M') {
379
- return {
380
- type: 'M',
381
- point: multiplyMatrix(matrix, point.point),
382
- };
383
- }
384
-
385
- if (point.type === 'L') {
386
- return {
387
- type: 'L',
388
- point: multiplyMatrix(matrix, point.point),
389
- };
390
- }
391
-
392
- if (point.type === 'Z') {
393
- return {
394
- type: 'Z',
395
- point: multiplyMatrix(matrix, point.point),
396
- };
397
- }
398
-
399
- throw new Error('Unknown instruction type: ' + JSON.stringify(point));
400
- }
401
-
402
- export const multiplyMatrix = (
403
- matrix: MatrixTransform4D,
404
- point: Vector4D,
405
- ): Vector4D => {
406
- if (matrix.length !== 16 || point.length !== 4) {
407
- throw new Error('Invalid matrix or vector dimension');
408
- }
409
-
410
- const result: Vector4D = [0, 0, 0, 0];
411
-
412
- for (let i = 0; i < 4; i++) {
413
- for (let j = 0; j < 4; j++) {
414
- result[i] += matrix[i * 4 + j] * point[j];
415
- }
416
- }
417
-
418
- return result;
419
- };
420
-
421
- export const sub4d = function (a: Vector4D, b: Vector4D): Vector4D {
422
- return a.map((v, i) => {
423
- return v - b[i];
424
- }) as Vector4D;
425
- };
@@ -1,238 +0,0 @@
1
- import type {Instruction} from '@remotion/paths';
2
- import type {ThreeDReducedInstruction} from './3d-svg';
3
- import type {Vector2D, Vector4D} from './matrix';
4
-
5
- const subdivideLOrMInstruction = (
6
- from: Vector4D,
7
- instruction: ThreeDReducedInstruction,
8
- ) => {
9
- if (instruction.type !== 'L' && instruction.type !== 'M') {
10
- throw new Error('Expected L or M instruction');
11
- }
12
-
13
- const t = 0.5;
14
- const q0: Vector4D = [
15
- (1 - t) * from[0] + t * instruction.point[0],
16
- (1 - t) * from[1] + t * instruction.point[1],
17
- (1 - t) * from[2] + t * instruction.point[2],
18
- (1 - t) * from[3] + t * instruction.point[3],
19
- ];
20
-
21
- const curves: [ThreeDReducedInstruction, ThreeDReducedInstruction] = [
22
- {type: instruction.type, point: q0},
23
- {type: instruction.type, point: instruction.point},
24
- ];
25
-
26
- return curves;
27
- };
28
-
29
- export const subdivide2DCInstruction = (
30
- fromX: number,
31
- fromY: number,
32
- instruction: Instruction,
33
- t: number,
34
- ) => {
35
- if (instruction.type !== 'C') {
36
- throw new Error('Expected C instruction');
37
- }
38
-
39
- const q0: Vector2D = [
40
- (1 - t) * fromX + t * instruction.cp1x,
41
- (1 - t) * fromY + t * instruction.cp1y,
42
- ];
43
-
44
- const q1: Vector2D = [
45
- (1 - t) * instruction.cp1x + t * instruction.cp2x,
46
- (1 - t) * instruction.cp1y + t * instruction.cp2y,
47
- ];
48
-
49
- const q2: Vector2D = [
50
- (1 - t) * instruction.cp2x + t * instruction.x,
51
- (1 - t) * instruction.cp2y + t * instruction.y,
52
- ];
53
-
54
- const r0: Vector2D = [
55
- (1 - t) * q0[0] + t * q1[0],
56
- (1 - t) * q0[1] + t * q1[1],
57
- ];
58
-
59
- const r1: Vector2D = [
60
- (1 - t) * q1[0] + t * q2[0],
61
- (1 - t) * q1[1] + t * q2[1],
62
- ];
63
-
64
- const s0: Vector2D = [
65
- (1 - t) * r0[0] + t * r1[0],
66
- (1 - t) * r0[1] + t * r1[1],
67
- ];
68
-
69
- const curves: [Instruction, Instruction] = [
70
- {
71
- type: 'C',
72
- x: s0[0],
73
- y: s0[1],
74
- cp1x: q0[0],
75
- cp1y: q0[1],
76
- cp2x: r0[0],
77
- cp2y: r0[1],
78
- },
79
- {
80
- type: 'C',
81
- x: instruction.x,
82
- y: instruction.y,
83
- cp1x: r1[0],
84
- cp1y: r1[1],
85
- cp2x: q2[0],
86
- cp2y: q2[1],
87
- },
88
- ];
89
-
90
- return curves;
91
- };
92
-
93
- const subdivide3DCInstruction = (
94
- from: Vector4D,
95
- instruction: ThreeDReducedInstruction,
96
- ) => {
97
- if (instruction.type !== 'C') {
98
- throw new Error('Expected C instruction');
99
- }
100
-
101
- const t = 0.5;
102
-
103
- const q0: Vector4D = [
104
- (1 - t) * from[0] + t * instruction.cp1[0],
105
- (1 - t) * from[1] + t * instruction.cp1[1],
106
- (1 - t) * from[2] + t * instruction.cp1[2],
107
- (1 - t) * from[3] + t * instruction.cp1[3],
108
- ];
109
-
110
- const q1: Vector4D = [
111
- (1 - t) * instruction.cp1[0] + t * instruction.cp2[0],
112
- (1 - t) * instruction.cp1[1] + t * instruction.cp2[1],
113
- (1 - t) * instruction.cp1[2] + t * instruction.cp2[2],
114
- (1 - t) * instruction.cp1[3] + t * instruction.cp2[3],
115
- ];
116
-
117
- const q2: Vector4D = [
118
- (1 - t) * instruction.cp2[0] + t * instruction.point[0],
119
- (1 - t) * instruction.cp2[1] + t * instruction.point[1],
120
- (1 - t) * instruction.cp2[2] + t * instruction.point[2],
121
- (1 - t) * instruction.cp2[3] + t * instruction.point[3],
122
- ];
123
-
124
- const r0: Vector4D = [
125
- (1 - t) * q0[0] + t * q1[0],
126
- (1 - t) * q0[1] + t * q1[1],
127
- (1 - t) * q0[2] + t * q1[2],
128
- (1 - t) * q0[3] + t * q1[3],
129
- ];
130
-
131
- const r1: Vector4D = [
132
- (1 - t) * q1[0] + t * q2[0],
133
- (1 - t) * q1[1] + t * q2[1],
134
- (1 - t) * q1[2] + t * q2[2],
135
- (1 - t) * q1[3] + t * q2[3],
136
- ];
137
-
138
- const s0: Vector4D = [
139
- (1 - t) * r0[0] + t * r1[0],
140
- (1 - t) * r0[1] + t * r1[1],
141
- (1 - t) * r0[2] + t * r1[2],
142
- (1 - t) * r0[3] + t * r1[3],
143
- ];
144
-
145
- const curves: [ThreeDReducedInstruction, ThreeDReducedInstruction] = [
146
- {type: 'C', point: s0, cp1: q0, cp2: r0},
147
- {type: 'C', point: instruction.point, cp1: r1, cp2: q2},
148
- ];
149
-
150
- return curves;
151
- };
152
-
153
- const subdivideQInstruction = (
154
- from: Vector4D,
155
- instruction: ThreeDReducedInstruction,
156
- ) => {
157
- if (instruction.type !== 'Q') {
158
- throw new Error('Expected Q instruction');
159
- }
160
-
161
- const t = 0.5;
162
- const q0: Vector4D = [
163
- (1 - t) * from[0] + t * instruction.cp[0],
164
- (1 - t) * from[1] + t * instruction.cp[1],
165
- (1 - t) * from[2] + t * instruction.cp[2],
166
- (1 - t) * from[3] + t * instruction.cp[3],
167
- ];
168
-
169
- const q1: Vector4D = [
170
- (1 - t) * instruction.cp[0] + t * instruction.point[0],
171
- (1 - t) * instruction.cp[1] + t * instruction.point[1],
172
- (1 - t) * instruction.cp[2] + t * instruction.point[2],
173
- (1 - t) * instruction.cp[3] + t * instruction.point[3],
174
- ];
175
-
176
- const r0: Vector4D = [
177
- (1 - t) * q0[0] + t * q1[0],
178
- (1 - t) * q0[1] + t * q1[1],
179
- (1 - t) * q0[2] + t * q1[2],
180
- (1 - t) * q0[3] + t * q1[3],
181
- ];
182
-
183
- const newInstructions: [ThreeDReducedInstruction, ThreeDReducedInstruction] =
184
- [
185
- {type: 'Q', point: r0, cp: q0},
186
- {type: 'Q', point: instruction.point, cp: q1},
187
- ];
188
-
189
- return newInstructions;
190
- };
191
-
192
- const subdivideInstruction = (
193
- from: Vector4D,
194
- instruction: ThreeDReducedInstruction,
195
- ): ThreeDReducedInstruction[] => {
196
- if (instruction.type === 'C') {
197
- return subdivide3DCInstruction(from, instruction);
198
- }
199
-
200
- if (instruction.type === 'L' || instruction.type === 'M') {
201
- return subdivideLOrMInstruction(from, instruction);
202
- }
203
-
204
- if (instruction.type === 'Q') {
205
- return subdivideQInstruction(from, instruction);
206
- }
207
-
208
- if (instruction.type === 'Z') {
209
- return [instruction];
210
- }
211
-
212
- throw new Error('Cannot subdivide instruction');
213
- };
214
-
215
- export const subdivideInstructions = (
216
- instructions: ThreeDReducedInstruction[],
217
- ): ThreeDReducedInstruction[] => {
218
- const newInstructions: ThreeDReducedInstruction[] = [];
219
- instructions.forEach((instruction, i) => {
220
- if (instruction.type === 'M') {
221
- newInstructions.push(instruction);
222
- return;
223
- }
224
-
225
- if (instruction.type === 'Z') {
226
- newInstructions.push(instruction);
227
- return;
228
- }
229
-
230
- const previousInstruction = instructions[i - 1];
231
- const subdivided = subdivideInstruction(
232
- previousInstruction.point,
233
- instruction,
234
- );
235
- newInstructions.push(...subdivided);
236
- });
237
- return newInstructions;
238
- };
package/src/truthy.ts DELETED
@@ -1,4 +0,0 @@
1
- type Truthy<T> = T extends false | '' | 0 | null | undefined ? never : T;
2
- export function truthy<T>(value: T): value is Truthy<T> {
3
- return Boolean(value);
4
- }
package/tsconfig.json DELETED
@@ -1,14 +0,0 @@
1
- {
2
- "extends": "../tsconfig.settings.json",
3
- "compilerOptions": {
4
- "rootDir": "./src",
5
- "outDir": "./dist/cjs",
6
- "skipLibCheck": true
7
- },
8
- "include": ["src"],
9
- "references": [
10
- {
11
- "path": "../paths"
12
- }
13
- ]
14
- }