@remotion/effects 4.0.538 → 4.0.539
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/THIRD_PARTY_LICENSES.md +4 -0
- package/dist/curves.d.ts +65 -0
- package/dist/esm/curves.mjs +373 -0
- package/dist/esm/levels.mjs +80 -10
- package/dist/esm/shatter.mjs +888 -0
- package/dist/levels.d.ts +69 -2
- package/dist/shatter/geometry.d.ts +19 -0
- package/dist/shatter/index.d.ts +39 -0
- package/dist/shatter/runtime.d.ts +19 -0
- package/dist/shatter.d.ts +1 -0
- package/package.json +19 -3
|
@@ -0,0 +1,888 @@
|
|
|
1
|
+
// src/shatter/index.ts
|
|
2
|
+
import { Internals } from "remotion";
|
|
3
|
+
|
|
4
|
+
// src/validate-effect-param.ts
|
|
5
|
+
var assertEffectParamsObject = (params, effectLabel) => {
|
|
6
|
+
if (params === null || typeof params !== "object") {
|
|
7
|
+
throw new TypeError(`${effectLabel} effect requires a parameters object, but got ${JSON.stringify(params)}`);
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
var assertRequiredFiniteNumber = (value, name) => {
|
|
11
|
+
if (typeof value !== "number" || !Number.isFinite(value)) {
|
|
12
|
+
throw new TypeError(`"${name}" must be a finite number, but got ${JSON.stringify(value)}`);
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
var assertRequiredColor = (value, name) => {
|
|
16
|
+
if (typeof value !== "string" || value.length === 0) {
|
|
17
|
+
throw new TypeError(`"${name}" must be a non-empty string, but got ${JSON.stringify(value)}`);
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
var assertOptionalColor = (value, name) => {
|
|
21
|
+
if (value === undefined) {
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
assertRequiredColor(value, name);
|
|
25
|
+
};
|
|
26
|
+
var assertOptionalBoolean = (value, name) => {
|
|
27
|
+
if (value === undefined) {
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
if (typeof value !== "boolean") {
|
|
31
|
+
throw new TypeError(`"${name}" must be a boolean, but got ${JSON.stringify(value)}`);
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
// src/color-utils.ts
|
|
36
|
+
var DEFAULT_AMOUNT = 1;
|
|
37
|
+
var DEFAULT_BRIGHTNESS_AMOUNT = 0;
|
|
38
|
+
var DEFAULT_HUE_DEGREES = 0;
|
|
39
|
+
var colorAmountSchema = {
|
|
40
|
+
type: "number",
|
|
41
|
+
min: 0,
|
|
42
|
+
max: 1,
|
|
43
|
+
step: 0.01,
|
|
44
|
+
default: DEFAULT_AMOUNT,
|
|
45
|
+
description: "Amount",
|
|
46
|
+
hiddenFromList: false
|
|
47
|
+
};
|
|
48
|
+
var colorMultiplierSchema = {
|
|
49
|
+
type: "number",
|
|
50
|
+
min: 0,
|
|
51
|
+
step: 0.01,
|
|
52
|
+
default: DEFAULT_AMOUNT,
|
|
53
|
+
description: "Amount",
|
|
54
|
+
hiddenFromList: false
|
|
55
|
+
};
|
|
56
|
+
var brightnessAmountSchema = {
|
|
57
|
+
type: "number",
|
|
58
|
+
min: -1,
|
|
59
|
+
max: 1,
|
|
60
|
+
step: 0.01,
|
|
61
|
+
default: DEFAULT_BRIGHTNESS_AMOUNT,
|
|
62
|
+
description: "Amount",
|
|
63
|
+
hiddenFromList: false
|
|
64
|
+
};
|
|
65
|
+
var hueDegreesSchema = {
|
|
66
|
+
type: "rotation-degrees",
|
|
67
|
+
step: 1,
|
|
68
|
+
default: DEFAULT_HUE_DEGREES,
|
|
69
|
+
description: "Degrees"
|
|
70
|
+
};
|
|
71
|
+
var assertOptionalFiniteNumber = (value, name) => {
|
|
72
|
+
if (value === undefined) {
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
assertRequiredFiniteNumber(value, name);
|
|
76
|
+
};
|
|
77
|
+
var validateUnitInterval = (value, name) => {
|
|
78
|
+
if (value < 0) {
|
|
79
|
+
throw new TypeError(`"${name}" must be >= 0, but got ${JSON.stringify(value)}`);
|
|
80
|
+
}
|
|
81
|
+
if (value > 1) {
|
|
82
|
+
throw new TypeError(`"${name}" must be <= 1, but got ${JSON.stringify(value)}`);
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
var validateNonNegative = (value, name) => {
|
|
86
|
+
if (value < 0) {
|
|
87
|
+
throw new TypeError(`"${name}" must be >= 0, but got ${JSON.stringify(value)}`);
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
var validateSignedUnitInterval = (value, name) => {
|
|
91
|
+
if (value < -1) {
|
|
92
|
+
throw new TypeError(`"${name}" must be >= -1, but got ${JSON.stringify(value)}`);
|
|
93
|
+
}
|
|
94
|
+
if (value > 1) {
|
|
95
|
+
throw new TypeError(`"${name}" must be <= 1, but got ${JSON.stringify(value)}`);
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
var clampColorChannel = (value) => {
|
|
99
|
+
return Math.max(0, Math.min(255, value));
|
|
100
|
+
};
|
|
101
|
+
var parseColorRgba = (ctx, color) => {
|
|
102
|
+
ctx.clearRect(0, 0, 1, 1);
|
|
103
|
+
ctx.fillStyle = color;
|
|
104
|
+
ctx.fillRect(0, 0, 1, 1);
|
|
105
|
+
const { data } = ctx.getImageData(0, 0, 1, 1);
|
|
106
|
+
return [data[0], data[1], data[2], data[3]];
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
// src/shatter/geometry.ts
|
|
110
|
+
var signedTriangleArea = (a, b, c) => (b[0] - a[0]) * (c[1] - a[1]) - (b[1] - a[1]) * (c[0] - a[0]);
|
|
111
|
+
var hash = (x, y, seed) => {
|
|
112
|
+
let h = Math.imul(x, 2376512323) ^ Math.imul(y, 3625334849) ^ Math.imul(seed, 3407524639);
|
|
113
|
+
h ^= h >>> 13;
|
|
114
|
+
h = Math.imul(h, 1540483477);
|
|
115
|
+
h ^= h >>> 15;
|
|
116
|
+
return (h & 16777215) / 16777216;
|
|
117
|
+
};
|
|
118
|
+
function* patternCells(pattern, cell, x0, y0, x1, y1, seed) {
|
|
119
|
+
const i0 = Math.floor(x0 / cell) - 1;
|
|
120
|
+
const i1 = Math.ceil(x1 / cell) + 1;
|
|
121
|
+
const j0 = Math.floor(y0 / cell) - 1;
|
|
122
|
+
const j1 = Math.ceil(y1 / cell) + 1;
|
|
123
|
+
if (pattern === "bricks") {
|
|
124
|
+
const brickHeight = cell / 2;
|
|
125
|
+
const row0 = Math.floor(y0 / brickHeight) - 1;
|
|
126
|
+
const row1 = Math.ceil(y1 / brickHeight) + 1;
|
|
127
|
+
for (let row = row0;row <= row1; row++) {
|
|
128
|
+
const shift = Math.abs(row % 2) * cell * 0.5;
|
|
129
|
+
for (let i = i0;i <= i1; i++) {
|
|
130
|
+
const x = i * cell + shift;
|
|
131
|
+
const y = row * brickHeight;
|
|
132
|
+
yield [
|
|
133
|
+
[x, y],
|
|
134
|
+
[x + cell * 0.5, y],
|
|
135
|
+
[x + cell, y],
|
|
136
|
+
[x + cell, y + brickHeight],
|
|
137
|
+
[x + cell * 0.5, y + brickHeight],
|
|
138
|
+
[x, y + brickHeight]
|
|
139
|
+
];
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
if (pattern === "hexagons") {
|
|
145
|
+
const radius = cell / Math.sqrt(3);
|
|
146
|
+
const dx = 1.5 * radius;
|
|
147
|
+
const col0 = Math.floor(x0 / dx) - 1;
|
|
148
|
+
const col1 = Math.ceil(x1 / dx) + 1;
|
|
149
|
+
for (let col = col0;col <= col1; col++) {
|
|
150
|
+
for (let row = j0;row <= j1; row++) {
|
|
151
|
+
const cx = col * dx;
|
|
152
|
+
const cy = (row + Math.abs(col % 2) * 0.5) * cell;
|
|
153
|
+
yield Array.from({ length: 6 }, (_, k) => {
|
|
154
|
+
const angle = k * Math.PI / 3;
|
|
155
|
+
return [cx + radius * Math.cos(angle), cy + radius * Math.sin(angle)];
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
const jitter = (i, j) => [
|
|
162
|
+
i * cell + (hash(i, j + 17, seed) * 2 - 1) * cell * 0.35,
|
|
163
|
+
j * cell + (hash(i, j + 91, seed) * 2 - 1) * cell * 0.35
|
|
164
|
+
];
|
|
165
|
+
for (let j = j0;j <= j1; j++) {
|
|
166
|
+
for (let i = i0;i <= i1; i++) {
|
|
167
|
+
const x = i * cell;
|
|
168
|
+
const y = j * cell;
|
|
169
|
+
const a = pattern === "glass" ? jitter(i, j) : [x, y];
|
|
170
|
+
const b = pattern === "glass" ? jitter(i + 1, j) : [x + cell, y];
|
|
171
|
+
const c = pattern === "glass" ? jitter(i + 1, j + 1) : [x + cell, y + cell];
|
|
172
|
+
const d = pattern === "glass" ? jitter(i, j + 1) : [x, y + cell];
|
|
173
|
+
const acValid = signedTriangleArea(a, b, c) > 0 && signedTriangleArea(a, c, d) > 0;
|
|
174
|
+
const bdValid = signedTriangleArea(a, b, d) > 0 && signedTriangleArea(b, c, d) > 0;
|
|
175
|
+
if (pattern === "squares") {
|
|
176
|
+
yield [a, b, c, d];
|
|
177
|
+
} else if (pattern === "glass" ? acValid && (!bdValid || hash(i, j, seed ^ 85) < 0.5) : Math.abs((i + j) % 2) === 1) {
|
|
178
|
+
yield [a, b, c];
|
|
179
|
+
yield [a, c, d];
|
|
180
|
+
} else {
|
|
181
|
+
yield [a, b, d];
|
|
182
|
+
yield [b, c, d];
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
var clipPolygon = (input, width, height) => {
|
|
188
|
+
let polygon = input;
|
|
189
|
+
for (let boundary = 0;boundary < 4; boundary++) {
|
|
190
|
+
const axis = boundary < 2 ? 0 : 1;
|
|
191
|
+
const limit = boundary % 2 === 0 ? 0 : axis === 0 ? width : height;
|
|
192
|
+
const sign = boundary % 2 === 0 ? 1 : -1;
|
|
193
|
+
const output = [];
|
|
194
|
+
for (let i = 0;i < polygon.length; i++) {
|
|
195
|
+
const a = polygon[i];
|
|
196
|
+
const b = polygon[(i + 1) % polygon.length];
|
|
197
|
+
const aInside = (a[axis] - limit) * sign >= 0;
|
|
198
|
+
const bInside = (b[axis] - limit) * sign >= 0;
|
|
199
|
+
if (aInside) {
|
|
200
|
+
output.push(a);
|
|
201
|
+
}
|
|
202
|
+
if (aInside !== bInside) {
|
|
203
|
+
const fraction = (limit - a[axis]) / (b[axis] - a[axis]);
|
|
204
|
+
const intersection = [
|
|
205
|
+
a[0] + (b[0] - a[0]) * fraction,
|
|
206
|
+
a[1] + (b[1] - a[1]) * fraction
|
|
207
|
+
];
|
|
208
|
+
intersection[axis] = limit;
|
|
209
|
+
output.push(intersection);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
polygon = output;
|
|
213
|
+
}
|
|
214
|
+
return polygon;
|
|
215
|
+
};
|
|
216
|
+
var createShatterGeometry = (width, height, params) => {
|
|
217
|
+
const identity = new Float32Array([
|
|
218
|
+
0,
|
|
219
|
+
0,
|
|
220
|
+
0,
|
|
221
|
+
0,
|
|
222
|
+
0,
|
|
223
|
+
1,
|
|
224
|
+
0,
|
|
225
|
+
width,
|
|
226
|
+
0,
|
|
227
|
+
0,
|
|
228
|
+
1,
|
|
229
|
+
0,
|
|
230
|
+
1,
|
|
231
|
+
0,
|
|
232
|
+
width,
|
|
233
|
+
height,
|
|
234
|
+
0,
|
|
235
|
+
1,
|
|
236
|
+
1,
|
|
237
|
+
1,
|
|
238
|
+
0,
|
|
239
|
+
0,
|
|
240
|
+
0,
|
|
241
|
+
0,
|
|
242
|
+
0,
|
|
243
|
+
0,
|
|
244
|
+
1,
|
|
245
|
+
0,
|
|
246
|
+
width,
|
|
247
|
+
height,
|
|
248
|
+
0,
|
|
249
|
+
1,
|
|
250
|
+
1,
|
|
251
|
+
1,
|
|
252
|
+
0,
|
|
253
|
+
0,
|
|
254
|
+
height,
|
|
255
|
+
0,
|
|
256
|
+
0,
|
|
257
|
+
1,
|
|
258
|
+
1,
|
|
259
|
+
0
|
|
260
|
+
]);
|
|
261
|
+
if (params.time === 0 || params.radius <= Math.abs(params.depth) || params.strength === 0 && params.randomness === 0 && params.gravity === 0) {
|
|
262
|
+
return identity;
|
|
263
|
+
}
|
|
264
|
+
const direction = params.direction % 360 * Math.PI / 180;
|
|
265
|
+
const sin = Math.sin(direction);
|
|
266
|
+
const cos = Math.cos(direction);
|
|
267
|
+
const originX = (Math.abs(params.origin[0]) > 1e6 ? params.origin[0] % 1 : params.origin[0]) * width;
|
|
268
|
+
const originY = (Math.abs(params.origin[1]) > 1e6 ? params.origin[1] % 1 : params.origin[1]) * height;
|
|
269
|
+
const corners = [
|
|
270
|
+
[0, 0],
|
|
271
|
+
[width, 0],
|
|
272
|
+
[width, height],
|
|
273
|
+
[0, height]
|
|
274
|
+
].map(([x, y]) => [
|
|
275
|
+
(x - originX) * cos + (y - originY) * sin,
|
|
276
|
+
-(x - originX) * sin + (y - originY) * cos
|
|
277
|
+
]);
|
|
278
|
+
const x0 = Math.min(...corners.map((point) => point[0]));
|
|
279
|
+
const x1 = Math.max(...corners.map((point) => point[0]));
|
|
280
|
+
const y0 = Math.min(...corners.map((point) => point[1]));
|
|
281
|
+
const y1 = Math.max(...corners.map((point) => point[1]));
|
|
282
|
+
let cell = Math.max(width / params.repetitions, Math.sqrt((x1 - x0) * (y1 - y0) / 20000), Math.max(x1 - x0, y1 - y0) / 1e4);
|
|
283
|
+
while (2 * (Math.ceil((x1 - x0) / cell) + 5) * (Math.ceil((y1 - y0) / cell) + 5) > 50000) {
|
|
284
|
+
cell *= 1.1;
|
|
285
|
+
}
|
|
286
|
+
const seed = Math.imul(params.seed, 2654435761);
|
|
287
|
+
const unit = width * 0.1;
|
|
288
|
+
const drag = params.viscosity * 5;
|
|
289
|
+
const { time } = params;
|
|
290
|
+
const velocityFactor = drag < 0.000001 ? time : -Math.expm1(-drag * time) / drag;
|
|
291
|
+
const gravityFactor = drag < 0.000001 ? time * time / 2 : drag * time < 0.001 ? time * time * (0.5 - drag * time / 6 + (drag * time) ** 2 / 24) : (time - velocityFactor) / drag;
|
|
292
|
+
const radius = params.radius * width;
|
|
293
|
+
const depth = params.depth * width;
|
|
294
|
+
const centerX = params.center[0] * width;
|
|
295
|
+
const centerY = params.center[1] * height;
|
|
296
|
+
const extrusion = params.extrusionDepth * cell;
|
|
297
|
+
const cameraDistance = Math.max(width, height) * 2;
|
|
298
|
+
const faces = [];
|
|
299
|
+
let key = 0;
|
|
300
|
+
let anyMoving = false;
|
|
301
|
+
for (const patternPolygon of patternCells(params.pattern, cell, x0, y0, x1, y1, seed)) {
|
|
302
|
+
const index = key++;
|
|
303
|
+
const sourcePolygon = patternPolygon.map(([x, y]) => [
|
|
304
|
+
originX + x * cos - y * sin,
|
|
305
|
+
originY + x * sin + y * cos
|
|
306
|
+
]);
|
|
307
|
+
const polygon = clipPolygon(sourcePolygon, width, height);
|
|
308
|
+
if (polygon.length < 3) {
|
|
309
|
+
continue;
|
|
310
|
+
}
|
|
311
|
+
const cx = sourcePolygon.reduce((sum, point) => sum + point[0], 0) / sourcePolygon.length;
|
|
312
|
+
const cy = sourcePolygon.reduce((sum, point) => sum + point[1], 0) / sourcePolygon.length;
|
|
313
|
+
const dx = cx - centerX;
|
|
314
|
+
const dy = cy - centerY;
|
|
315
|
+
const distance = Math.hypot(dx, dy, depth);
|
|
316
|
+
const broken = distance < radius;
|
|
317
|
+
let px = cx;
|
|
318
|
+
let py = cy;
|
|
319
|
+
let pz = 0;
|
|
320
|
+
let rotation = [1, 0, 0, 0, 1, 0, 0, 0, 1];
|
|
321
|
+
if (broken) {
|
|
322
|
+
const force = params.strength * (1 - distance / radius) * unit / Math.max(distance, 0.000001);
|
|
323
|
+
const mass = Math.max(0.001, 1 + params.massVariance * (hash(index, 1, seed) * 2 - 1));
|
|
324
|
+
const vx = dx * force / mass + params.randomness * unit * (hash(index, 2, seed) * 2 - 1) * 2;
|
|
325
|
+
const vy = dy * force / mass + params.randomness * unit * (hash(index, 3, seed) * 2 - 1) * 2;
|
|
326
|
+
const vz = depth * force / mass + params.randomness * unit * (hash(index, 4, seed) * 2 - 1) * 2;
|
|
327
|
+
px += vx * velocityFactor;
|
|
328
|
+
py += vy * velocityFactor + params.gravity * unit * gravityFactor;
|
|
329
|
+
pz += vz * velocityFactor;
|
|
330
|
+
const axis = [
|
|
331
|
+
hash(index, 6, seed) * 2 - 1,
|
|
332
|
+
hash(index, 7, seed) * 2 - 1,
|
|
333
|
+
hash(index, 8, seed) * 2 - 1
|
|
334
|
+
];
|
|
335
|
+
const axisLength = Math.hypot(...axis);
|
|
336
|
+
const speed = Math.min(10, Math.hypot(vx, vy, vz) / unit);
|
|
337
|
+
const angle = params.rotationSpeed * Math.PI * 2 * (0.5 + hash(index, 9, seed)) * speed * velocityFactor;
|
|
338
|
+
if (axisLength > 0.000000000001 && angle !== 0) {
|
|
339
|
+
const [x, y, z] = axis.map((value) => value / axisLength);
|
|
340
|
+
const s = Math.sin(angle);
|
|
341
|
+
const c = Math.cos(angle);
|
|
342
|
+
const t = 1 - c;
|
|
343
|
+
rotation = [
|
|
344
|
+
t * x * x + c,
|
|
345
|
+
t * x * y - s * z,
|
|
346
|
+
t * x * z + s * y,
|
|
347
|
+
t * x * y + s * z,
|
|
348
|
+
t * y * y + c,
|
|
349
|
+
t * y * z - s * x,
|
|
350
|
+
t * x * z - s * y,
|
|
351
|
+
t * y * z + s * x,
|
|
352
|
+
t * z * z + c
|
|
353
|
+
];
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
const moving = px !== cx || py !== cy || pz !== 0 || rotation.some((value, axisIndex) => value !== (axisIndex % 4 === 0 ? 1 : 0));
|
|
357
|
+
anyMoving ||= moving;
|
|
358
|
+
const shade = moving ? 0.25 + 0.75 * Math.abs(rotation[8]) : 1;
|
|
359
|
+
const vertices = polygon.map(([x, y]) => [
|
|
360
|
+
px + rotation[0] * (x - cx) + rotation[1] * (y - cy),
|
|
361
|
+
py + rotation[3] * (x - cx) + rotation[4] * (y - cy),
|
|
362
|
+
pz + rotation[6] * (x - cx) + rotation[7] * (y - cy),
|
|
363
|
+
x / width,
|
|
364
|
+
y / height,
|
|
365
|
+
shade,
|
|
366
|
+
0
|
|
367
|
+
]);
|
|
368
|
+
if (vertices.some((vertex) => vertex.some((value) => !Number.isFinite(value) || Math.abs(value) > 1000000000000000000000000000000))) {
|
|
369
|
+
continue;
|
|
370
|
+
}
|
|
371
|
+
faces.push({
|
|
372
|
+
depth: vertices.reduce((sum, vertex) => sum + vertex[2], 0) / vertices.length,
|
|
373
|
+
vertices
|
|
374
|
+
});
|
|
375
|
+
if (!moving || extrusion === 0) {
|
|
376
|
+
continue;
|
|
377
|
+
}
|
|
378
|
+
for (let edge = 0;edge < polygon.length; edge++) {
|
|
379
|
+
const a = polygon[edge];
|
|
380
|
+
const b = polygon[(edge + 1) % polygon.length];
|
|
381
|
+
const edgeX = b[0] - a[0];
|
|
382
|
+
const edgeY = b[1] - a[1];
|
|
383
|
+
const length = Math.hypot(edgeX, edgeY);
|
|
384
|
+
if (length < 0.000001) {
|
|
385
|
+
continue;
|
|
386
|
+
}
|
|
387
|
+
const nx = (rotation[0] * edgeY - rotation[1] * edgeX) / length;
|
|
388
|
+
const ny = (rotation[3] * edgeY - rotation[4] * edgeX) / length;
|
|
389
|
+
const nz = (rotation[6] * edgeY - rotation[7] * edgeX) / length;
|
|
390
|
+
const frontA = vertices[edge];
|
|
391
|
+
const frontB = vertices[(edge + 1) % vertices.length];
|
|
392
|
+
const mx = (frontA[0] + frontB[0] + rotation[2] * extrusion) / 2;
|
|
393
|
+
const my = (frontA[1] + frontB[1] + rotation[5] * extrusion) / 2;
|
|
394
|
+
const mz = (frontA[2] + frontB[2] + rotation[8] * extrusion) / 2;
|
|
395
|
+
if (nx * (width / 2 - mx) + ny * (height / 2 - my) + nz * (-cameraDistance - mz) <= 0) {
|
|
396
|
+
continue;
|
|
397
|
+
}
|
|
398
|
+
const au = a[0] / width;
|
|
399
|
+
const av = a[1] / height;
|
|
400
|
+
const bu = b[0] / width;
|
|
401
|
+
const bv = b[1] / height;
|
|
402
|
+
const sideShade = 0.25 + 0.75 * Math.abs(nz);
|
|
403
|
+
const sideVertices = [
|
|
404
|
+
[frontA[0], frontA[1], frontA[2], au, av, sideShade, 1],
|
|
405
|
+
[frontB[0], frontB[1], frontB[2], bu, bv, sideShade, 1],
|
|
406
|
+
[
|
|
407
|
+
frontB[0] + rotation[2] * extrusion,
|
|
408
|
+
frontB[1] + rotation[5] * extrusion,
|
|
409
|
+
frontB[2] + rotation[8] * extrusion,
|
|
410
|
+
bu,
|
|
411
|
+
bv,
|
|
412
|
+
sideShade,
|
|
413
|
+
1
|
|
414
|
+
],
|
|
415
|
+
[
|
|
416
|
+
frontA[0] + rotation[2] * extrusion,
|
|
417
|
+
frontA[1] + rotation[5] * extrusion,
|
|
418
|
+
frontA[2] + rotation[8] * extrusion,
|
|
419
|
+
au,
|
|
420
|
+
av,
|
|
421
|
+
sideShade,
|
|
422
|
+
1
|
|
423
|
+
]
|
|
424
|
+
];
|
|
425
|
+
if (sideVertices.every((vertex) => vertex.every((value) => Number.isFinite(value) && Math.abs(value) < 1000000000000000000000000000000))) {
|
|
426
|
+
faces.push({ depth: mz, vertices: sideVertices });
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
if (!anyMoving) {
|
|
431
|
+
return identity;
|
|
432
|
+
}
|
|
433
|
+
faces.sort((a, b) => b.depth - a.depth);
|
|
434
|
+
const floatCount = faces.reduce((sum, face) => {
|
|
435
|
+
const centered = params.pattern === "bricks" && face.vertices[0][6] === 0;
|
|
436
|
+
return sum + (face.vertices.length - (centered ? 0 : 2)) * 3 * 7;
|
|
437
|
+
}, 0);
|
|
438
|
+
const output = new Float32Array(floatCount);
|
|
439
|
+
let offset = 0;
|
|
440
|
+
for (const face of faces) {
|
|
441
|
+
const centered = params.pattern === "bricks" && face.vertices[0][6] === 0;
|
|
442
|
+
const center = centered ? face.vertices[0].map((_, component) => face.vertices.reduce((sum, vertex) => sum + vertex[component], 0) / face.vertices.length) : face.vertices[0];
|
|
443
|
+
const triangleCount = face.vertices.length - (centered ? 0 : 2);
|
|
444
|
+
for (let i = 0;i < triangleCount; i++) {
|
|
445
|
+
const start = centered ? i : i + 1;
|
|
446
|
+
for (const vertex of [
|
|
447
|
+
center,
|
|
448
|
+
face.vertices[start],
|
|
449
|
+
face.vertices[(start + 1) % face.vertices.length]
|
|
450
|
+
]) {
|
|
451
|
+
output.set(vertex, offset);
|
|
452
|
+
offset += 7;
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
return output;
|
|
457
|
+
};
|
|
458
|
+
|
|
459
|
+
// src/shatter/runtime.ts
|
|
460
|
+
var VERTEX_SHADER = `#version 300 es
|
|
461
|
+
layout(location = 0) in vec3 aPosition;
|
|
462
|
+
layout(location = 1) in vec2 aUv;
|
|
463
|
+
layout(location = 2) in float aShade;
|
|
464
|
+
layout(location = 3) in float aSide;
|
|
465
|
+
|
|
466
|
+
uniform vec2 uResolution;
|
|
467
|
+
|
|
468
|
+
out vec2 vUv;
|
|
469
|
+
out float vShade;
|
|
470
|
+
out float vSide;
|
|
471
|
+
|
|
472
|
+
void main() {
|
|
473
|
+
float distance = max(uResolution.x, uResolution.y) * 2.0;
|
|
474
|
+
float near = distance * 0.05;
|
|
475
|
+
float w = distance + aPosition.z;
|
|
476
|
+
vec2 position = vec2(
|
|
477
|
+
(aPosition.x - uResolution.x * 0.5) * 2.0 / uResolution.x,
|
|
478
|
+
(uResolution.y * 0.5 - aPosition.y) * 2.0 / uResolution.y
|
|
479
|
+
);
|
|
480
|
+
// Keep homogeneous coordinates so WebGL clips shards at the near plane
|
|
481
|
+
// and interpolates their texture coordinates with perspective correction.
|
|
482
|
+
gl_Position = vec4(position * distance, w - 2.0 * near, w);
|
|
483
|
+
vUv = vec2(aUv.x, 1.0 - aUv.y);
|
|
484
|
+
vShade = aShade;
|
|
485
|
+
vSide = aSide;
|
|
486
|
+
}
|
|
487
|
+
`;
|
|
488
|
+
var FRAGMENT_SHADER = `#version 300 es
|
|
489
|
+
precision highp float;
|
|
490
|
+
|
|
491
|
+
uniform sampler2D uSource;
|
|
492
|
+
|
|
493
|
+
in vec2 vUv;
|
|
494
|
+
in float vShade;
|
|
495
|
+
in float vSide;
|
|
496
|
+
out vec4 fragColor;
|
|
497
|
+
|
|
498
|
+
void main() {
|
|
499
|
+
vec4 source = texture(uSource, vUv);
|
|
500
|
+
// The sides inherit the source alpha at the shard edge. A transparent
|
|
501
|
+
// image must not acquire opaque geometry when extrusion is enabled.
|
|
502
|
+
vec3 color = mix(source.rgb, vec3(source.a), vSide);
|
|
503
|
+
fragColor = vec4(color * vShade, source.a);
|
|
504
|
+
}
|
|
505
|
+
`;
|
|
506
|
+
var compileShader = (gl, type, source) => {
|
|
507
|
+
const shader = gl.createShader(type);
|
|
508
|
+
if (!shader) {
|
|
509
|
+
throw new Error("Failed to create Shatter shader");
|
|
510
|
+
}
|
|
511
|
+
gl.shaderSource(shader, source);
|
|
512
|
+
gl.compileShader(shader);
|
|
513
|
+
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
|
|
514
|
+
const log = gl.getShaderInfoLog(shader);
|
|
515
|
+
gl.deleteShader(shader);
|
|
516
|
+
throw new Error(`Shatter shader compile failed: ${log ?? "(no log)"}`);
|
|
517
|
+
}
|
|
518
|
+
return shader;
|
|
519
|
+
};
|
|
520
|
+
var setupShatter = (gl) => {
|
|
521
|
+
const program = gl.createProgram();
|
|
522
|
+
const vao = gl.createVertexArray();
|
|
523
|
+
const vbo = gl.createBuffer();
|
|
524
|
+
const texture = gl.createTexture();
|
|
525
|
+
const shaders = [];
|
|
526
|
+
try {
|
|
527
|
+
if (!program || !vao || !vbo || !texture) {
|
|
528
|
+
throw new Error("Failed to create Shatter WebGL resources");
|
|
529
|
+
}
|
|
530
|
+
shaders.push(compileShader(gl, gl.VERTEX_SHADER, VERTEX_SHADER));
|
|
531
|
+
shaders.push(compileShader(gl, gl.FRAGMENT_SHADER, FRAGMENT_SHADER));
|
|
532
|
+
for (const shader of shaders) {
|
|
533
|
+
gl.attachShader(program, shader);
|
|
534
|
+
}
|
|
535
|
+
gl.linkProgram(program);
|
|
536
|
+
if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
|
|
537
|
+
throw new Error(`Shatter program link failed: ${gl.getProgramInfoLog(program) ?? "(no log)"}`);
|
|
538
|
+
}
|
|
539
|
+
gl.bindVertexArray(vao);
|
|
540
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, vbo);
|
|
541
|
+
gl.enableVertexAttribArray(0);
|
|
542
|
+
gl.vertexAttribPointer(0, 3, gl.FLOAT, false, 28, 0);
|
|
543
|
+
gl.enableVertexAttribArray(1);
|
|
544
|
+
gl.vertexAttribPointer(1, 2, gl.FLOAT, false, 28, 12);
|
|
545
|
+
gl.enableVertexAttribArray(2);
|
|
546
|
+
gl.vertexAttribPointer(2, 1, gl.FLOAT, false, 28, 20);
|
|
547
|
+
gl.enableVertexAttribArray(3);
|
|
548
|
+
gl.vertexAttribPointer(3, 1, gl.FLOAT, false, 28, 24);
|
|
549
|
+
gl.bindVertexArray(null);
|
|
550
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, null);
|
|
551
|
+
gl.bindTexture(gl.TEXTURE_2D, texture);
|
|
552
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
|
|
553
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
|
|
554
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
|
|
555
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
|
|
556
|
+
gl.bindTexture(gl.TEXTURE_2D, null);
|
|
557
|
+
return {
|
|
558
|
+
gl,
|
|
559
|
+
program,
|
|
560
|
+
vao,
|
|
561
|
+
vbo,
|
|
562
|
+
texture,
|
|
563
|
+
uSource: gl.getUniformLocation(program, "uSource"),
|
|
564
|
+
uResolution: gl.getUniformLocation(program, "uResolution")
|
|
565
|
+
};
|
|
566
|
+
} catch (error) {
|
|
567
|
+
gl.deleteTexture(texture);
|
|
568
|
+
gl.deleteBuffer(vbo);
|
|
569
|
+
gl.deleteVertexArray(vao);
|
|
570
|
+
gl.deleteProgram(program);
|
|
571
|
+
throw error;
|
|
572
|
+
} finally {
|
|
573
|
+
for (const shader of shaders) {
|
|
574
|
+
gl.deleteShader(shader);
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
};
|
|
578
|
+
var drawShatter = ({
|
|
579
|
+
state,
|
|
580
|
+
source,
|
|
581
|
+
width,
|
|
582
|
+
height,
|
|
583
|
+
flipSourceY,
|
|
584
|
+
vertices
|
|
585
|
+
}) => {
|
|
586
|
+
const { gl, program, vao, vbo, texture, uSource, uResolution } = state;
|
|
587
|
+
gl.viewport(0, 0, width, height);
|
|
588
|
+
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
|
|
589
|
+
gl.disable(gl.DEPTH_TEST);
|
|
590
|
+
gl.disable(gl.CULL_FACE);
|
|
591
|
+
gl.clearColor(0, 0, 0, 0);
|
|
592
|
+
gl.clear(gl.COLOR_BUFFER_BIT);
|
|
593
|
+
gl.enable(gl.BLEND);
|
|
594
|
+
gl.blendEquation(gl.FUNC_ADD);
|
|
595
|
+
gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
|
|
596
|
+
gl.useProgram(program);
|
|
597
|
+
gl.bindVertexArray(vao);
|
|
598
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, vbo);
|
|
599
|
+
gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.DYNAMIC_DRAW);
|
|
600
|
+
gl.activeTexture(gl.TEXTURE0);
|
|
601
|
+
gl.bindTexture(gl.TEXTURE_2D, texture);
|
|
602
|
+
gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, true);
|
|
603
|
+
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, flipSourceY);
|
|
604
|
+
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, source);
|
|
605
|
+
gl.uniform1i(uSource, 0);
|
|
606
|
+
gl.uniform2f(uResolution, width, height);
|
|
607
|
+
gl.drawArrays(gl.TRIANGLES, 0, vertices.length / 7);
|
|
608
|
+
gl.bindVertexArray(null);
|
|
609
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, null);
|
|
610
|
+
gl.bindTexture(gl.TEXTURE_2D, null);
|
|
611
|
+
gl.useProgram(null);
|
|
612
|
+
gl.disable(gl.BLEND);
|
|
613
|
+
};
|
|
614
|
+
var cleanupShatter = ({
|
|
615
|
+
gl,
|
|
616
|
+
program,
|
|
617
|
+
vao,
|
|
618
|
+
vbo,
|
|
619
|
+
texture
|
|
620
|
+
}) => {
|
|
621
|
+
gl.deleteTexture(texture);
|
|
622
|
+
gl.deleteBuffer(vbo);
|
|
623
|
+
gl.deleteVertexArray(vao);
|
|
624
|
+
gl.deleteProgram(program);
|
|
625
|
+
};
|
|
626
|
+
|
|
627
|
+
// src/shatter/index.ts
|
|
628
|
+
var { createEffect, createWebGL2ContextError } = Internals;
|
|
629
|
+
var DEFAULT_TIME = 0.5;
|
|
630
|
+
var DEFAULT_PATTERN = "bricks";
|
|
631
|
+
var DEFAULT_REPETITIONS = 10;
|
|
632
|
+
var DEFAULT_DIRECTION = 0;
|
|
633
|
+
var DEFAULT_ORIGIN = [0.5, 0.5];
|
|
634
|
+
var DEFAULT_CENTER = [0.5, 0.5];
|
|
635
|
+
var DEFAULT_RADIUS = 0.4;
|
|
636
|
+
var DEFAULT_DEPTH = 0.1;
|
|
637
|
+
var DEFAULT_STRENGTH = 5;
|
|
638
|
+
var DEFAULT_ROTATION_SPEED = 0.2;
|
|
639
|
+
var DEFAULT_RANDOMNESS = 0.1;
|
|
640
|
+
var DEFAULT_VISCOSITY = 0.1;
|
|
641
|
+
var DEFAULT_MASS_VARIANCE = 0.3;
|
|
642
|
+
var DEFAULT_GRAVITY = 3;
|
|
643
|
+
var DEFAULT_SEED = 0;
|
|
644
|
+
var DEFAULT_EXTRUSION_DEPTH = 0.05;
|
|
645
|
+
var shatterSchema = {
|
|
646
|
+
time: {
|
|
647
|
+
type: "number",
|
|
648
|
+
min: 0,
|
|
649
|
+
max: 5,
|
|
650
|
+
step: 0.01,
|
|
651
|
+
default: DEFAULT_TIME,
|
|
652
|
+
description: "Time",
|
|
653
|
+
hiddenFromList: false
|
|
654
|
+
},
|
|
655
|
+
pattern: {
|
|
656
|
+
type: "enum",
|
|
657
|
+
variants: { bricks: {}, glass: {}, hexagons: {}, squares: {}, triangles: {} },
|
|
658
|
+
default: DEFAULT_PATTERN,
|
|
659
|
+
description: "Pattern"
|
|
660
|
+
},
|
|
661
|
+
repetitions: {
|
|
662
|
+
type: "number",
|
|
663
|
+
min: 1,
|
|
664
|
+
max: 100,
|
|
665
|
+
step: 1,
|
|
666
|
+
default: DEFAULT_REPETITIONS,
|
|
667
|
+
description: "Repetitions",
|
|
668
|
+
hiddenFromList: false
|
|
669
|
+
},
|
|
670
|
+
direction: {
|
|
671
|
+
type: "rotation-degrees",
|
|
672
|
+
step: 1,
|
|
673
|
+
default: DEFAULT_DIRECTION,
|
|
674
|
+
description: "Pattern direction"
|
|
675
|
+
},
|
|
676
|
+
origin: {
|
|
677
|
+
type: "uv-coordinate",
|
|
678
|
+
step: 0.01,
|
|
679
|
+
default: DEFAULT_ORIGIN,
|
|
680
|
+
description: "Pattern origin"
|
|
681
|
+
},
|
|
682
|
+
center: {
|
|
683
|
+
type: "uv-coordinate",
|
|
684
|
+
step: 0.01,
|
|
685
|
+
default: DEFAULT_CENTER,
|
|
686
|
+
description: "Force center"
|
|
687
|
+
},
|
|
688
|
+
radius: {
|
|
689
|
+
type: "number",
|
|
690
|
+
min: 0,
|
|
691
|
+
max: 2,
|
|
692
|
+
step: 0.01,
|
|
693
|
+
default: DEFAULT_RADIUS,
|
|
694
|
+
description: "Force radius",
|
|
695
|
+
hiddenFromList: false
|
|
696
|
+
},
|
|
697
|
+
depth: {
|
|
698
|
+
type: "number",
|
|
699
|
+
min: -1,
|
|
700
|
+
max: 1,
|
|
701
|
+
step: 0.01,
|
|
702
|
+
default: DEFAULT_DEPTH,
|
|
703
|
+
description: "Force depth",
|
|
704
|
+
hiddenFromList: false
|
|
705
|
+
},
|
|
706
|
+
strength: {
|
|
707
|
+
type: "number",
|
|
708
|
+
min: -20,
|
|
709
|
+
max: 20,
|
|
710
|
+
step: 0.1,
|
|
711
|
+
default: DEFAULT_STRENGTH,
|
|
712
|
+
description: "Force strength",
|
|
713
|
+
hiddenFromList: false
|
|
714
|
+
},
|
|
715
|
+
rotationSpeed: {
|
|
716
|
+
type: "number",
|
|
717
|
+
min: 0,
|
|
718
|
+
max: 5,
|
|
719
|
+
step: 0.01,
|
|
720
|
+
default: DEFAULT_ROTATION_SPEED,
|
|
721
|
+
description: "Rotation speed",
|
|
722
|
+
hiddenFromList: false
|
|
723
|
+
},
|
|
724
|
+
randomness: {
|
|
725
|
+
type: "number",
|
|
726
|
+
min: 0,
|
|
727
|
+
max: 1,
|
|
728
|
+
step: 0.01,
|
|
729
|
+
default: DEFAULT_RANDOMNESS,
|
|
730
|
+
description: "Randomness",
|
|
731
|
+
hiddenFromList: false
|
|
732
|
+
},
|
|
733
|
+
viscosity: {
|
|
734
|
+
type: "number",
|
|
735
|
+
min: 0,
|
|
736
|
+
max: 1,
|
|
737
|
+
step: 0.01,
|
|
738
|
+
default: DEFAULT_VISCOSITY,
|
|
739
|
+
description: "Viscosity",
|
|
740
|
+
hiddenFromList: false
|
|
741
|
+
},
|
|
742
|
+
massVariance: {
|
|
743
|
+
type: "number",
|
|
744
|
+
min: 0,
|
|
745
|
+
max: 1,
|
|
746
|
+
step: 0.01,
|
|
747
|
+
default: DEFAULT_MASS_VARIANCE,
|
|
748
|
+
description: "Mass variance",
|
|
749
|
+
hiddenFromList: false
|
|
750
|
+
},
|
|
751
|
+
gravity: {
|
|
752
|
+
type: "number",
|
|
753
|
+
min: 0,
|
|
754
|
+
max: 10,
|
|
755
|
+
step: 0.1,
|
|
756
|
+
default: DEFAULT_GRAVITY,
|
|
757
|
+
description: "Gravity",
|
|
758
|
+
hiddenFromList: false
|
|
759
|
+
},
|
|
760
|
+
seed: {
|
|
761
|
+
type: "number",
|
|
762
|
+
integer: true,
|
|
763
|
+
min: 0,
|
|
764
|
+
max: 4294967295,
|
|
765
|
+
step: 1,
|
|
766
|
+
default: DEFAULT_SEED,
|
|
767
|
+
description: "Random seed",
|
|
768
|
+
hiddenFromList: false
|
|
769
|
+
},
|
|
770
|
+
extrusionDepth: {
|
|
771
|
+
type: "number",
|
|
772
|
+
min: 0,
|
|
773
|
+
max: 1,
|
|
774
|
+
step: 0.01,
|
|
775
|
+
default: DEFAULT_EXTRUSION_DEPTH,
|
|
776
|
+
description: "Extrusion depth",
|
|
777
|
+
hiddenFromList: false
|
|
778
|
+
}
|
|
779
|
+
};
|
|
780
|
+
var resolve = (params) => ({
|
|
781
|
+
time: params.time ?? DEFAULT_TIME,
|
|
782
|
+
pattern: params.pattern ?? DEFAULT_PATTERN,
|
|
783
|
+
repetitions: params.repetitions ?? DEFAULT_REPETITIONS,
|
|
784
|
+
direction: params.direction ?? DEFAULT_DIRECTION,
|
|
785
|
+
origin: params.origin ?? DEFAULT_ORIGIN,
|
|
786
|
+
center: params.center ?? DEFAULT_CENTER,
|
|
787
|
+
radius: params.radius ?? DEFAULT_RADIUS,
|
|
788
|
+
depth: params.depth ?? DEFAULT_DEPTH,
|
|
789
|
+
strength: params.strength ?? DEFAULT_STRENGTH,
|
|
790
|
+
rotationSpeed: params.rotationSpeed ?? DEFAULT_ROTATION_SPEED,
|
|
791
|
+
randomness: params.randomness ?? DEFAULT_RANDOMNESS,
|
|
792
|
+
viscosity: params.viscosity ?? DEFAULT_VISCOSITY,
|
|
793
|
+
massVariance: params.massVariance ?? DEFAULT_MASS_VARIANCE,
|
|
794
|
+
gravity: params.gravity ?? DEFAULT_GRAVITY,
|
|
795
|
+
seed: params.seed ?? DEFAULT_SEED,
|
|
796
|
+
extrusionDepth: params.extrusionDepth ?? DEFAULT_EXTRUSION_DEPTH
|
|
797
|
+
});
|
|
798
|
+
var shatter = createEffect({
|
|
799
|
+
type: "remotion/shatter",
|
|
800
|
+
label: "shatter()",
|
|
801
|
+
documentationLink: "https://www.remotion.dev/docs/effects/shatter",
|
|
802
|
+
backend: "webgl2",
|
|
803
|
+
schema: shatterSchema,
|
|
804
|
+
validateParams: (params) => {
|
|
805
|
+
assertEffectParamsObject(params, "Shatter");
|
|
806
|
+
for (const name of [
|
|
807
|
+
"time",
|
|
808
|
+
"repetitions",
|
|
809
|
+
"direction",
|
|
810
|
+
"radius",
|
|
811
|
+
"depth",
|
|
812
|
+
"strength",
|
|
813
|
+
"rotationSpeed",
|
|
814
|
+
"randomness",
|
|
815
|
+
"viscosity",
|
|
816
|
+
"massVariance",
|
|
817
|
+
"gravity",
|
|
818
|
+
"seed",
|
|
819
|
+
"extrusionDepth"
|
|
820
|
+
]) {
|
|
821
|
+
assertOptionalFiniteNumber(params[name], name);
|
|
822
|
+
}
|
|
823
|
+
for (const name of ["origin", "center"]) {
|
|
824
|
+
const value = params[name];
|
|
825
|
+
if (value !== undefined && (!Array.isArray(value) || value.length !== 2 || !Number.isFinite(value[0]) || !Number.isFinite(value[1]))) {
|
|
826
|
+
throw new TypeError(`"${name}" must contain two finite numbers`);
|
|
827
|
+
}
|
|
828
|
+
}
|
|
829
|
+
const r = resolve(params);
|
|
830
|
+
if (params.pattern !== undefined && (typeof params.pattern !== "string" || !Object.hasOwn(shatterSchema.pattern.variants, params.pattern))) {
|
|
831
|
+
throw new TypeError('"pattern" must be "bricks", "glass", "hexagons", "squares", or "triangles"');
|
|
832
|
+
}
|
|
833
|
+
if (r.repetitions < 1 || r.repetitions > 100) {
|
|
834
|
+
throw new TypeError('"repetitions" must be between 1 and 100');
|
|
835
|
+
}
|
|
836
|
+
for (const name of [
|
|
837
|
+
"time",
|
|
838
|
+
"radius",
|
|
839
|
+
"rotationSpeed",
|
|
840
|
+
"gravity"
|
|
841
|
+
]) {
|
|
842
|
+
validateNonNegative(r[name], name);
|
|
843
|
+
}
|
|
844
|
+
for (const name of [
|
|
845
|
+
"randomness",
|
|
846
|
+
"viscosity",
|
|
847
|
+
"massVariance",
|
|
848
|
+
"extrusionDepth"
|
|
849
|
+
]) {
|
|
850
|
+
validateUnitInterval(r[name], name);
|
|
851
|
+
}
|
|
852
|
+
if (!Number.isInteger(r.seed) || r.seed < 0 || r.seed > 4294967295) {
|
|
853
|
+
throw new TypeError('"seed" must be an integer from 0 to 4294967295');
|
|
854
|
+
}
|
|
855
|
+
},
|
|
856
|
+
calculateKey: (params) => `shatter-${JSON.stringify(resolve(params))}`,
|
|
857
|
+
setup: (target) => {
|
|
858
|
+
const gl = target.getContext("webgl2", {
|
|
859
|
+
premultipliedAlpha: true,
|
|
860
|
+
alpha: true,
|
|
861
|
+
preserveDrawingBuffer: true
|
|
862
|
+
});
|
|
863
|
+
if (!gl) {
|
|
864
|
+
throw createWebGL2ContextError("shatter effect");
|
|
865
|
+
}
|
|
866
|
+
return { ...setupShatter(gl), geometryKey: null, vertices: null };
|
|
867
|
+
},
|
|
868
|
+
apply: ({ source, width, height, params, state, flipSourceY }) => {
|
|
869
|
+
const r = resolve(params);
|
|
870
|
+
const geometryKey = `${width}:${height}:${JSON.stringify(r)}`;
|
|
871
|
+
if (state.geometryKey !== geometryKey || state.vertices === null) {
|
|
872
|
+
state.vertices = createShatterGeometry(width, height, r);
|
|
873
|
+
state.geometryKey = geometryKey;
|
|
874
|
+
}
|
|
875
|
+
drawShatter({
|
|
876
|
+
state,
|
|
877
|
+
source,
|
|
878
|
+
width,
|
|
879
|
+
height,
|
|
880
|
+
flipSourceY,
|
|
881
|
+
vertices: state.vertices
|
|
882
|
+
});
|
|
883
|
+
},
|
|
884
|
+
cleanup: cleanupShatter
|
|
885
|
+
});
|
|
886
|
+
export {
|
|
887
|
+
shatter
|
|
888
|
+
};
|