@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.
@@ -0,0 +1,308 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.sub4d = exports.multiplyMatrix = exports.mulScalar = exports.translateZ = exports.translateY = exports.translateX = exports.dot = exports.normalize4d = exports.normalize = exports.scaleY = exports.scaleX = exports.scaled = exports.interpolateMatrix4d = exports.makeMatrix3dTransform = exports.aroundCenterPoint = exports.reduceMatrices = exports.multiplyMatrices = exports.rotateZ = exports.translated4d = exports.rotateY = exports.rotateX = exports.m44multiply = exports.identity4 = exports.stride = void 0;
4
+ exports.multiplyMatrixAndSvgInstruction = multiplyMatrixAndSvgInstruction;
5
+ const truthy_1 = require("./truthy");
6
+ const stride = function ({ v, m, width, offset, colStride, }) {
7
+ for (let i = 0; i < v.length; i++) {
8
+ m[i * width + // Column
9
+ ((i * colStride + offset + width) % width) // Row
10
+ ] = v[i];
11
+ }
12
+ return m;
13
+ };
14
+ exports.stride = stride;
15
+ const identity4 = function () {
16
+ const n = 4;
17
+ let size = n * n;
18
+ const m = new Array(size);
19
+ while (size--) {
20
+ m[size] = size % (n + 1) === 0 ? 1.0 : 0.0;
21
+ }
22
+ return m;
23
+ };
24
+ exports.identity4 = identity4;
25
+ const m44multiply = (...matrices) => {
26
+ return multiplyMany(4, matrices);
27
+ };
28
+ exports.m44multiply = m44multiply;
29
+ // Accept an integer indicating the size of the matrices being multiplied (3 for 3x3), and any
30
+ // number of matrices following it.
31
+ function multiplyMany(size, listOfMatrices) {
32
+ if (listOfMatrices.length < 2) {
33
+ throw new Error('multiplication expected two or more matrices');
34
+ }
35
+ let result = mul(listOfMatrices[0], listOfMatrices[1], size);
36
+ let next = 2;
37
+ while (next < listOfMatrices.length) {
38
+ result = mul(result, listOfMatrices[next], size);
39
+ next++;
40
+ }
41
+ return result;
42
+ }
43
+ function mul(m1, m2, size) {
44
+ if (m1.length !== m2.length) {
45
+ throw new Error(`Undefined for matrices of different sizes. m1.length=${m1.length}, m2.length=${m2.length}`);
46
+ }
47
+ if (size * size !== m1.length) {
48
+ throw new Error(`Undefined for non-square matrices. array size was ${size}`);
49
+ }
50
+ const result = Array(m1.length);
51
+ for (let r = 0; r < size; r++) {
52
+ for (let c = 0; c < size; c++) {
53
+ // Accumulate a sum of m1[r,k]*m2[k, c]
54
+ let acc = 0;
55
+ for (let k = 0; k < size; k++) {
56
+ acc += m1[size * r + k] * m2[size * k + c];
57
+ }
58
+ result[r * size + c] = acc;
59
+ }
60
+ }
61
+ return result;
62
+ }
63
+ const rotated = function (axisVec, radians) {
64
+ return rotatedUnitSinCos((0, exports.normalize)(axisVec), Math.sin(radians), Math.cos(radians));
65
+ };
66
+ const rotateX = (radians) => {
67
+ return rotated([1, 0, 0], radians);
68
+ };
69
+ exports.rotateX = rotateX;
70
+ const rotateY = (radians) => {
71
+ return rotated([0, 1, 0], radians);
72
+ };
73
+ exports.rotateY = rotateY;
74
+ const translated4d = function (vec) {
75
+ return (0, exports.stride)({ v: vec, m: (0, exports.identity4)(), width: 4, offset: 3, colStride: 0 });
76
+ };
77
+ exports.translated4d = translated4d;
78
+ const rotateZ = (radians) => {
79
+ return rotated([0, 0, 1], radians);
80
+ };
81
+ exports.rotateZ = rotateZ;
82
+ const multiplyMatrices = (matrix1, matrix2) => {
83
+ const result = new Array(16).fill(0);
84
+ for (let i = 0; i < 4; i++) {
85
+ for (let j = 0; j < 4; j++) {
86
+ for (let k = 0; k < 4; k++) {
87
+ result[i * 4 + j] += matrix1[i * 4 + k] * matrix2[k * 4 + j];
88
+ }
89
+ }
90
+ }
91
+ return result;
92
+ };
93
+ exports.multiplyMatrices = multiplyMatrices;
94
+ const reduceMatrices = (matrices) => {
95
+ return matrices
96
+ .filter(truthy_1.truthy)
97
+ .slice()
98
+ .reverse()
99
+ .reduce((acc, cur) => {
100
+ return (0, exports.multiplyMatrices)(acc, cur);
101
+ }, (0, exports.identity4)());
102
+ };
103
+ exports.reduceMatrices = reduceMatrices;
104
+ const aroundCenterPoint = ({ matrix, x, y, z, }) => {
105
+ return (0, exports.reduceMatrices)([
106
+ (0, exports.translateX)(-x),
107
+ (0, exports.translateY)(-y),
108
+ (0, exports.translateZ)(-z),
109
+ matrix,
110
+ (0, exports.translateX)(x),
111
+ (0, exports.translateY)(y),
112
+ (0, exports.translateZ)(z),
113
+ ]);
114
+ };
115
+ exports.aroundCenterPoint = aroundCenterPoint;
116
+ const makeMatrix3dTransform = function (matrix) {
117
+ return `matrix3d(${[
118
+ matrix[0],
119
+ matrix[4],
120
+ matrix[8],
121
+ matrix[12], // First column
122
+ matrix[1],
123
+ matrix[5],
124
+ matrix[9],
125
+ matrix[13], // Second column
126
+ matrix[2],
127
+ matrix[6],
128
+ matrix[10],
129
+ matrix[14], // Third column
130
+ matrix[3],
131
+ matrix[7],
132
+ matrix[11],
133
+ matrix[15], // Fourth column
134
+ ].join(', ')}`;
135
+ };
136
+ exports.makeMatrix3dTransform = makeMatrix3dTransform;
137
+ const interpolate = (a, b, t) => {
138
+ return a + (b - a) * t;
139
+ };
140
+ const interpolateMatrix4d = (input, matrix1, matrix2) => {
141
+ return [
142
+ interpolate(matrix1[0], matrix2[0], input),
143
+ interpolate(matrix1[1], matrix2[1], input),
144
+ interpolate(matrix1[2], matrix2[2], input),
145
+ interpolate(matrix1[3], matrix2[3], input),
146
+ interpolate(matrix1[4], matrix2[4], input),
147
+ interpolate(matrix1[5], matrix2[5], input),
148
+ interpolate(matrix1[6], matrix2[6], input),
149
+ interpolate(matrix1[7], matrix2[7], input),
150
+ interpolate(matrix1[8], matrix2[8], input),
151
+ interpolate(matrix1[9], matrix2[9], input),
152
+ interpolate(matrix1[10], matrix2[10], input),
153
+ interpolate(matrix1[11], matrix2[11], input),
154
+ interpolate(matrix1[12], matrix2[12], input),
155
+ interpolate(matrix1[13], matrix2[13], input),
156
+ interpolate(matrix1[14], matrix2[14], input),
157
+ interpolate(matrix1[15], matrix2[15], input),
158
+ ];
159
+ };
160
+ exports.interpolateMatrix4d = interpolateMatrix4d;
161
+ const scaled = function (value) {
162
+ const vec = typeof value === 'number' ? [value, value, value] : value;
163
+ return (0, exports.stride)({ v: vec, m: (0, exports.identity4)(), width: 4, offset: 0, colStride: 1 });
164
+ };
165
+ exports.scaled = scaled;
166
+ const scaleX = (x) => {
167
+ return (0, exports.scaled)([x, 1, 1]);
168
+ };
169
+ exports.scaleX = scaleX;
170
+ const scaleY = (y) => {
171
+ return (0, exports.scaled)([1, y, 1]);
172
+ };
173
+ exports.scaleY = scaleY;
174
+ const rotatedUnitSinCos = function (axisVec, sinAngle, cosAngle) {
175
+ const x = axisVec[0];
176
+ const y = axisVec[1];
177
+ const z = axisVec[2];
178
+ const c = cosAngle;
179
+ const s = sinAngle;
180
+ const t = 1 - c;
181
+ return [
182
+ t * x * x + c,
183
+ t * x * y - s * z,
184
+ t * x * z + s * y,
185
+ 0,
186
+ t * x * y + s * z,
187
+ t * y * y + c,
188
+ t * y * z - s * x,
189
+ 0,
190
+ t * x * z - s * y,
191
+ t * y * z + s * x,
192
+ t * z * z + c,
193
+ 0,
194
+ 0,
195
+ 0,
196
+ 0,
197
+ 1,
198
+ ];
199
+ };
200
+ const normalize = function (v) {
201
+ return (0, exports.mulScalar)(v, 1 / vectorLength(v));
202
+ };
203
+ exports.normalize = normalize;
204
+ const normalize4d = function (v) {
205
+ return (0, exports.mulScalar)(v, 1 / vectorLength(v));
206
+ };
207
+ exports.normalize4d = normalize4d;
208
+ const vectorLength = function (v) {
209
+ return Math.sqrt(lengthSquared(v));
210
+ };
211
+ const lengthSquared = function (v) {
212
+ return (0, exports.dot)(v, v);
213
+ };
214
+ const dot = function (a, b) {
215
+ if (a.length !== b.length) {
216
+ throw new Error(`Cannot perform dot product on arrays of different length (${a.length} vs ${b.length})`);
217
+ }
218
+ return a
219
+ .map((v, i) => {
220
+ return v * b[i];
221
+ })
222
+ .reduce((acc, cur) => {
223
+ return acc + cur;
224
+ });
225
+ };
226
+ exports.dot = dot;
227
+ const translated = function (vec) {
228
+ return (0, exports.stride)({
229
+ v: vec,
230
+ m: (0, exports.identity4)(),
231
+ width: 4,
232
+ offset: 3,
233
+ colStride: 0,
234
+ });
235
+ };
236
+ const translateX = (x) => {
237
+ return translated([x, 0, 0]);
238
+ };
239
+ exports.translateX = translateX;
240
+ const translateY = (y) => {
241
+ return translated([0, y, 0]);
242
+ };
243
+ exports.translateY = translateY;
244
+ const translateZ = (z) => {
245
+ return translated([0, 0, z]);
246
+ };
247
+ exports.translateZ = translateZ;
248
+ const mulScalar = function (v, s) {
249
+ return v.map((i) => {
250
+ return i * s;
251
+ });
252
+ };
253
+ exports.mulScalar = mulScalar;
254
+ function multiplyMatrixAndSvgInstruction(matrix, point) {
255
+ if (point.type === 'C') {
256
+ return {
257
+ type: 'C',
258
+ cp1: (0, exports.multiplyMatrix)(matrix, point.cp1),
259
+ cp2: (0, exports.multiplyMatrix)(matrix, point.cp2),
260
+ point: (0, exports.multiplyMatrix)(matrix, point.point),
261
+ };
262
+ }
263
+ if (point.type === 'Q') {
264
+ return {
265
+ type: 'Q',
266
+ cp: (0, exports.multiplyMatrix)(matrix, point.cp),
267
+ point: (0, exports.multiplyMatrix)(matrix, point.point),
268
+ };
269
+ }
270
+ if (point.type === 'M') {
271
+ return {
272
+ type: 'M',
273
+ point: (0, exports.multiplyMatrix)(matrix, point.point),
274
+ };
275
+ }
276
+ if (point.type === 'L') {
277
+ return {
278
+ type: 'L',
279
+ point: (0, exports.multiplyMatrix)(matrix, point.point),
280
+ };
281
+ }
282
+ if (point.type === 'Z') {
283
+ return {
284
+ type: 'Z',
285
+ point: (0, exports.multiplyMatrix)(matrix, point.point),
286
+ };
287
+ }
288
+ throw new Error('Unknown instruction type: ' + JSON.stringify(point));
289
+ }
290
+ const multiplyMatrix = (matrix, point) => {
291
+ if (matrix.length !== 16 || point.length !== 4) {
292
+ throw new Error('Invalid matrix or vector dimension');
293
+ }
294
+ const result = [0, 0, 0, 0];
295
+ for (let i = 0; i < 4; i++) {
296
+ for (let j = 0; j < 4; j++) {
297
+ result[i] += matrix[i * 4 + j] * point[j];
298
+ }
299
+ }
300
+ return result;
301
+ };
302
+ exports.multiplyMatrix = multiplyMatrix;
303
+ const sub4d = function (a, b) {
304
+ return a.map((v, i) => {
305
+ return v - b[i];
306
+ });
307
+ };
308
+ exports.sub4d = sub4d;
@@ -0,0 +1,4 @@
1
+ import type { Instruction } from '@remotion/paths';
2
+ import type { ThreeDReducedInstruction } from './3d-svg';
3
+ export declare const subdivide2DCInstruction: (fromX: number, fromY: number, instruction: Instruction, t: number) => [Instruction, Instruction];
4
+ export declare const subdivideInstructions: (instructions: ThreeDReducedInstruction[]) => ThreeDReducedInstruction[];
@@ -0,0 +1,180 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.subdivideInstructions = exports.subdivide2DCInstruction = void 0;
4
+ const subdivideLOrMInstruction = (from, instruction) => {
5
+ if (instruction.type !== 'L' && instruction.type !== 'M') {
6
+ throw new Error('Expected L or M instruction');
7
+ }
8
+ const t = 0.5;
9
+ const q0 = [
10
+ (1 - t) * from[0] + t * instruction.point[0],
11
+ (1 - t) * from[1] + t * instruction.point[1],
12
+ (1 - t) * from[2] + t * instruction.point[2],
13
+ (1 - t) * from[3] + t * instruction.point[3],
14
+ ];
15
+ const curves = [
16
+ { type: instruction.type, point: q0 },
17
+ { type: instruction.type, point: instruction.point },
18
+ ];
19
+ return curves;
20
+ };
21
+ const subdivide2DCInstruction = (fromX, fromY, instruction, t) => {
22
+ if (instruction.type !== 'C') {
23
+ throw new Error('Expected C instruction');
24
+ }
25
+ const q0 = [
26
+ (1 - t) * fromX + t * instruction.cp1x,
27
+ (1 - t) * fromY + t * instruction.cp1y,
28
+ ];
29
+ const q1 = [
30
+ (1 - t) * instruction.cp1x + t * instruction.cp2x,
31
+ (1 - t) * instruction.cp1y + t * instruction.cp2y,
32
+ ];
33
+ const q2 = [
34
+ (1 - t) * instruction.cp2x + t * instruction.x,
35
+ (1 - t) * instruction.cp2y + t * instruction.y,
36
+ ];
37
+ const r0 = [
38
+ (1 - t) * q0[0] + t * q1[0],
39
+ (1 - t) * q0[1] + t * q1[1],
40
+ ];
41
+ const r1 = [
42
+ (1 - t) * q1[0] + t * q2[0],
43
+ (1 - t) * q1[1] + t * q2[1],
44
+ ];
45
+ const s0 = [
46
+ (1 - t) * r0[0] + t * r1[0],
47
+ (1 - t) * r0[1] + t * r1[1],
48
+ ];
49
+ const curves = [
50
+ {
51
+ type: 'C',
52
+ x: s0[0],
53
+ y: s0[1],
54
+ cp1x: q0[0],
55
+ cp1y: q0[1],
56
+ cp2x: r0[0],
57
+ cp2y: r0[1],
58
+ },
59
+ {
60
+ type: 'C',
61
+ x: instruction.x,
62
+ y: instruction.y,
63
+ cp1x: r1[0],
64
+ cp1y: r1[1],
65
+ cp2x: q2[0],
66
+ cp2y: q2[1],
67
+ },
68
+ ];
69
+ return curves;
70
+ };
71
+ exports.subdivide2DCInstruction = subdivide2DCInstruction;
72
+ const subdivide3DCInstruction = (from, instruction) => {
73
+ if (instruction.type !== 'C') {
74
+ throw new Error('Expected C instruction');
75
+ }
76
+ const t = 0.5;
77
+ const q0 = [
78
+ (1 - t) * from[0] + t * instruction.cp1[0],
79
+ (1 - t) * from[1] + t * instruction.cp1[1],
80
+ (1 - t) * from[2] + t * instruction.cp1[2],
81
+ (1 - t) * from[3] + t * instruction.cp1[3],
82
+ ];
83
+ const q1 = [
84
+ (1 - t) * instruction.cp1[0] + t * instruction.cp2[0],
85
+ (1 - t) * instruction.cp1[1] + t * instruction.cp2[1],
86
+ (1 - t) * instruction.cp1[2] + t * instruction.cp2[2],
87
+ (1 - t) * instruction.cp1[3] + t * instruction.cp2[3],
88
+ ];
89
+ const q2 = [
90
+ (1 - t) * instruction.cp2[0] + t * instruction.point[0],
91
+ (1 - t) * instruction.cp2[1] + t * instruction.point[1],
92
+ (1 - t) * instruction.cp2[2] + t * instruction.point[2],
93
+ (1 - t) * instruction.cp2[3] + t * instruction.point[3],
94
+ ];
95
+ const r0 = [
96
+ (1 - t) * q0[0] + t * q1[0],
97
+ (1 - t) * q0[1] + t * q1[1],
98
+ (1 - t) * q0[2] + t * q1[2],
99
+ (1 - t) * q0[3] + t * q1[3],
100
+ ];
101
+ const r1 = [
102
+ (1 - t) * q1[0] + t * q2[0],
103
+ (1 - t) * q1[1] + t * q2[1],
104
+ (1 - t) * q1[2] + t * q2[2],
105
+ (1 - t) * q1[3] + t * q2[3],
106
+ ];
107
+ const s0 = [
108
+ (1 - t) * r0[0] + t * r1[0],
109
+ (1 - t) * r0[1] + t * r1[1],
110
+ (1 - t) * r0[2] + t * r1[2],
111
+ (1 - t) * r0[3] + t * r1[3],
112
+ ];
113
+ const curves = [
114
+ { type: 'C', point: s0, cp1: q0, cp2: r0 },
115
+ { type: 'C', point: instruction.point, cp1: r1, cp2: q2 },
116
+ ];
117
+ return curves;
118
+ };
119
+ const subdivideQInstruction = (from, instruction) => {
120
+ if (instruction.type !== 'Q') {
121
+ throw new Error('Expected Q instruction');
122
+ }
123
+ const t = 0.5;
124
+ const q0 = [
125
+ (1 - t) * from[0] + t * instruction.cp[0],
126
+ (1 - t) * from[1] + t * instruction.cp[1],
127
+ (1 - t) * from[2] + t * instruction.cp[2],
128
+ (1 - t) * from[3] + t * instruction.cp[3],
129
+ ];
130
+ const q1 = [
131
+ (1 - t) * instruction.cp[0] + t * instruction.point[0],
132
+ (1 - t) * instruction.cp[1] + t * instruction.point[1],
133
+ (1 - t) * instruction.cp[2] + t * instruction.point[2],
134
+ (1 - t) * instruction.cp[3] + t * instruction.point[3],
135
+ ];
136
+ const r0 = [
137
+ (1 - t) * q0[0] + t * q1[0],
138
+ (1 - t) * q0[1] + t * q1[1],
139
+ (1 - t) * q0[2] + t * q1[2],
140
+ (1 - t) * q0[3] + t * q1[3],
141
+ ];
142
+ const newInstructions = [
143
+ { type: 'Q', point: r0, cp: q0 },
144
+ { type: 'Q', point: instruction.point, cp: q1 },
145
+ ];
146
+ return newInstructions;
147
+ };
148
+ const subdivideInstruction = (from, instruction) => {
149
+ if (instruction.type === 'C') {
150
+ return subdivide3DCInstruction(from, instruction);
151
+ }
152
+ if (instruction.type === 'L' || instruction.type === 'M') {
153
+ return subdivideLOrMInstruction(from, instruction);
154
+ }
155
+ if (instruction.type === 'Q') {
156
+ return subdivideQInstruction(from, instruction);
157
+ }
158
+ if (instruction.type === 'Z') {
159
+ return [instruction];
160
+ }
161
+ throw new Error('Cannot subdivide instruction');
162
+ };
163
+ const subdivideInstructions = (instructions) => {
164
+ const newInstructions = [];
165
+ instructions.forEach((instruction, i) => {
166
+ if (instruction.type === 'M') {
167
+ newInstructions.push(instruction);
168
+ return;
169
+ }
170
+ if (instruction.type === 'Z') {
171
+ newInstructions.push(instruction);
172
+ return;
173
+ }
174
+ const previousInstruction = instructions[i - 1];
175
+ const subdivided = subdivideInstruction(previousInstruction.point, instruction);
176
+ newInstructions.push(...subdivided);
177
+ });
178
+ return newInstructions;
179
+ };
180
+ exports.subdivideInstructions = subdivideInstructions;
@@ -0,0 +1,3 @@
1
+ type Truthy<T> = T extends false | '' | 0 | null | undefined ? never : T;
2
+ export declare function truthy<T>(value: T): value is Truthy<T>;
3
+ export {};
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.truthy = truthy;
4
+ function truthy(value) {
5
+ return Boolean(value);
6
+ }