@remotion/effects 4.0.479 → 4.0.481
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/dist/checkerboard.d.ts +75 -0
- package/dist/emboss.d.ts +76 -0
- package/dist/esm/blur.mjs +1 -1
- package/dist/esm/checkerboard.mjs +516 -0
- package/dist/esm/emboss.mjs +477 -0
- package/dist/esm/gridlines.mjs +577 -0
- package/dist/esm/index.mjs +1059 -184
- package/dist/esm/pixelate.mjs +3 -3
- package/dist/esm/vignette.mjs +5 -10
- package/dist/esm/zoom-blur.mjs +419 -0
- package/dist/gridlines.d.ts +113 -0
- package/dist/index.d.ts +2 -0
- package/dist/visual-test/visual-utils.d.ts +4 -2
- package/dist/zoom-blur/index.d.ts +12 -0
- package/dist/zoom-blur/zoom-blur-runtime.d.ts +26 -0
- package/dist/zoom-blur/zoom-blur-shaders.d.ts +2 -0
- package/dist/zoom-blur.d.ts +1 -0
- package/package.json +35 -3
|
@@ -0,0 +1,577 @@
|
|
|
1
|
+
// src/gridlines.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/gridlines.ts
|
|
110
|
+
var { createEffect, createWebGL2ContextError } = Internals;
|
|
111
|
+
var DEFAULT_GRID_SIZE = 64;
|
|
112
|
+
var DEFAULT_LINE_WIDTH = 2;
|
|
113
|
+
var DEFAULT_LINE_COLOR = "#ffffff";
|
|
114
|
+
var DEFAULT_BACKGROUND_COLOR = "transparent";
|
|
115
|
+
var DEFAULT_ROTATION = 0;
|
|
116
|
+
var DEFAULT_ROTATION_X = 0;
|
|
117
|
+
var DEFAULT_ROTATION_Y = 0;
|
|
118
|
+
var DEFAULT_PERSPECTIVE = 0;
|
|
119
|
+
var DEFAULT_OFFSET_X = 0;
|
|
120
|
+
var DEFAULT_OFFSET_Y = 0;
|
|
121
|
+
var DEFAULT_MASK_TO_SOURCE_ALPHA = false;
|
|
122
|
+
var gridlinesSchema = {
|
|
123
|
+
gridSize: {
|
|
124
|
+
type: "number",
|
|
125
|
+
min: 1,
|
|
126
|
+
max: 400,
|
|
127
|
+
step: 1,
|
|
128
|
+
default: DEFAULT_GRID_SIZE,
|
|
129
|
+
description: "Grid size",
|
|
130
|
+
hiddenFromList: false
|
|
131
|
+
},
|
|
132
|
+
lineWidth: {
|
|
133
|
+
type: "number",
|
|
134
|
+
min: 0,
|
|
135
|
+
max: 100,
|
|
136
|
+
step: 0.1,
|
|
137
|
+
default: DEFAULT_LINE_WIDTH,
|
|
138
|
+
description: "Line width",
|
|
139
|
+
hiddenFromList: false
|
|
140
|
+
},
|
|
141
|
+
lineColor: {
|
|
142
|
+
type: "color",
|
|
143
|
+
default: DEFAULT_LINE_COLOR,
|
|
144
|
+
description: "Line color"
|
|
145
|
+
},
|
|
146
|
+
backgroundColor: {
|
|
147
|
+
type: "color",
|
|
148
|
+
default: DEFAULT_BACKGROUND_COLOR,
|
|
149
|
+
description: "Background color"
|
|
150
|
+
},
|
|
151
|
+
rotation: {
|
|
152
|
+
type: "rotation-degrees",
|
|
153
|
+
min: -180,
|
|
154
|
+
max: 180,
|
|
155
|
+
step: 1,
|
|
156
|
+
default: DEFAULT_ROTATION,
|
|
157
|
+
description: "Rotation"
|
|
158
|
+
},
|
|
159
|
+
rotationX: {
|
|
160
|
+
type: "rotation-degrees",
|
|
161
|
+
min: -180,
|
|
162
|
+
max: 180,
|
|
163
|
+
step: 1,
|
|
164
|
+
default: DEFAULT_ROTATION_X,
|
|
165
|
+
description: "Rotation X"
|
|
166
|
+
},
|
|
167
|
+
rotationY: {
|
|
168
|
+
type: "rotation-degrees",
|
|
169
|
+
min: -180,
|
|
170
|
+
max: 180,
|
|
171
|
+
step: 1,
|
|
172
|
+
default: DEFAULT_ROTATION_Y,
|
|
173
|
+
description: "Rotation Y"
|
|
174
|
+
},
|
|
175
|
+
perspective: {
|
|
176
|
+
type: "number",
|
|
177
|
+
min: 0,
|
|
178
|
+
max: 4000,
|
|
179
|
+
step: 1,
|
|
180
|
+
default: DEFAULT_PERSPECTIVE,
|
|
181
|
+
description: "Perspective",
|
|
182
|
+
hiddenFromList: false
|
|
183
|
+
},
|
|
184
|
+
offsetX: {
|
|
185
|
+
type: "number",
|
|
186
|
+
min: -400,
|
|
187
|
+
max: 400,
|
|
188
|
+
step: 0.1,
|
|
189
|
+
default: DEFAULT_OFFSET_X,
|
|
190
|
+
description: "Offset X",
|
|
191
|
+
hiddenFromList: false
|
|
192
|
+
},
|
|
193
|
+
offsetY: {
|
|
194
|
+
type: "number",
|
|
195
|
+
min: -400,
|
|
196
|
+
max: 400,
|
|
197
|
+
step: 0.1,
|
|
198
|
+
default: DEFAULT_OFFSET_Y,
|
|
199
|
+
description: "Offset Y",
|
|
200
|
+
hiddenFromList: false
|
|
201
|
+
},
|
|
202
|
+
maskToSourceAlpha: {
|
|
203
|
+
type: "boolean",
|
|
204
|
+
default: DEFAULT_MASK_TO_SOURCE_ALPHA,
|
|
205
|
+
description: "Mask to source alpha"
|
|
206
|
+
}
|
|
207
|
+
};
|
|
208
|
+
var resolve = (p) => ({
|
|
209
|
+
gridSize: p.gridSize ?? DEFAULT_GRID_SIZE,
|
|
210
|
+
lineWidth: p.lineWidth ?? DEFAULT_LINE_WIDTH,
|
|
211
|
+
lineColor: p.lineColor ?? DEFAULT_LINE_COLOR,
|
|
212
|
+
backgroundColor: p.backgroundColor ?? DEFAULT_BACKGROUND_COLOR,
|
|
213
|
+
rotation: p.rotation ?? DEFAULT_ROTATION,
|
|
214
|
+
rotationX: p.rotationX ?? DEFAULT_ROTATION_X,
|
|
215
|
+
rotationY: p.rotationY ?? DEFAULT_ROTATION_Y,
|
|
216
|
+
perspective: p.perspective ?? DEFAULT_PERSPECTIVE,
|
|
217
|
+
offsetX: p.offsetX ?? DEFAULT_OFFSET_X,
|
|
218
|
+
offsetY: p.offsetY ?? DEFAULT_OFFSET_Y,
|
|
219
|
+
maskToSourceAlpha: p.maskToSourceAlpha ?? DEFAULT_MASK_TO_SOURCE_ALPHA
|
|
220
|
+
});
|
|
221
|
+
var validatePositive = (value, name) => {
|
|
222
|
+
if (value <= 0) {
|
|
223
|
+
throw new TypeError(`"${name}" must be greater than 0, but got ${JSON.stringify(value)}`);
|
|
224
|
+
}
|
|
225
|
+
};
|
|
226
|
+
var validateNonNegative2 = (value, name) => {
|
|
227
|
+
if (value < 0) {
|
|
228
|
+
throw new TypeError(`"${name}" must be greater than or equal to 0, but got ${JSON.stringify(value)}`);
|
|
229
|
+
}
|
|
230
|
+
};
|
|
231
|
+
var validateGridlinesParams = (params) => {
|
|
232
|
+
assertEffectParamsObject(params, "Gridlines");
|
|
233
|
+
assertOptionalFiniteNumber(params.gridSize, "gridSize");
|
|
234
|
+
assertOptionalFiniteNumber(params.lineWidth, "lineWidth");
|
|
235
|
+
assertOptionalFiniteNumber(params.rotation, "rotation");
|
|
236
|
+
assertOptionalFiniteNumber(params.rotationX, "rotationX");
|
|
237
|
+
assertOptionalFiniteNumber(params.rotationY, "rotationY");
|
|
238
|
+
assertOptionalFiniteNumber(params.perspective, "perspective");
|
|
239
|
+
assertOptionalFiniteNumber(params.offsetX, "offsetX");
|
|
240
|
+
assertOptionalFiniteNumber(params.offsetY, "offsetY");
|
|
241
|
+
assertOptionalColor(params.lineColor, "lineColor");
|
|
242
|
+
assertOptionalColor(params.backgroundColor, "backgroundColor");
|
|
243
|
+
assertOptionalBoolean(params.maskToSourceAlpha, "maskToSourceAlpha");
|
|
244
|
+
validatePositive(params.gridSize ?? DEFAULT_GRID_SIZE, "gridSize");
|
|
245
|
+
validateNonNegative2(params.lineWidth ?? DEFAULT_LINE_WIDTH, "lineWidth");
|
|
246
|
+
validateNonNegative2(params.perspective ?? DEFAULT_PERSPECTIVE, "perspective");
|
|
247
|
+
};
|
|
248
|
+
var GRIDLINES_VS = `#version 300 es
|
|
249
|
+
in vec2 aPos;
|
|
250
|
+
in vec2 aUv;
|
|
251
|
+
out vec2 vUv;
|
|
252
|
+
|
|
253
|
+
void main() {
|
|
254
|
+
vUv = aUv;
|
|
255
|
+
gl_Position = vec4(aPos, 0.0, 1.0);
|
|
256
|
+
}
|
|
257
|
+
`;
|
|
258
|
+
var GRIDLINES_FS = `#version 300 es
|
|
259
|
+
precision highp float;
|
|
260
|
+
|
|
261
|
+
in vec2 vUv;
|
|
262
|
+
out vec4 fragColor;
|
|
263
|
+
|
|
264
|
+
uniform sampler2D uSource;
|
|
265
|
+
uniform vec2 uResolution;
|
|
266
|
+
uniform float uGridSize;
|
|
267
|
+
uniform float uLineWidth;
|
|
268
|
+
uniform vec4 uLineColor;
|
|
269
|
+
uniform vec4 uBackgroundColor;
|
|
270
|
+
uniform float uRotation;
|
|
271
|
+
uniform float uRotationX;
|
|
272
|
+
uniform float uRotationY;
|
|
273
|
+
uniform float uPerspective;
|
|
274
|
+
uniform vec2 uOffset;
|
|
275
|
+
uniform bool uMaskToSourceAlpha;
|
|
276
|
+
|
|
277
|
+
vec4 sourceOver(vec4 backdrop, vec4 overlay) {
|
|
278
|
+
return overlay + backdrop * (1.0 - overlay.a);
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
vec4 sourceAtop(vec4 backdrop, vec4 overlay) {
|
|
282
|
+
return vec4(
|
|
283
|
+
overlay.rgb * backdrop.a + backdrop.rgb * (1.0 - overlay.a),
|
|
284
|
+
backdrop.a
|
|
285
|
+
);
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
mat3 rotationMatrix(float rotationX, float rotationY, float rotationZ) {
|
|
289
|
+
float sx = sin(rotationX);
|
|
290
|
+
float cx = cos(rotationX);
|
|
291
|
+
float sy = sin(rotationY);
|
|
292
|
+
float cy = cos(rotationY);
|
|
293
|
+
float sz = sin(rotationZ);
|
|
294
|
+
float cz = cos(rotationZ);
|
|
295
|
+
|
|
296
|
+
mat3 rx = mat3(
|
|
297
|
+
1.0, 0.0, 0.0,
|
|
298
|
+
0.0, cx, sx,
|
|
299
|
+
0.0, -sx, cx
|
|
300
|
+
);
|
|
301
|
+
mat3 ry = mat3(
|
|
302
|
+
cy, 0.0, -sy,
|
|
303
|
+
0.0, 1.0, 0.0,
|
|
304
|
+
sy, 0.0, cy
|
|
305
|
+
);
|
|
306
|
+
mat3 rz = mat3(
|
|
307
|
+
cz, sz, 0.0,
|
|
308
|
+
-sz, cz, 0.0,
|
|
309
|
+
0.0, 0.0, 1.0
|
|
310
|
+
);
|
|
311
|
+
|
|
312
|
+
return rz * ry * rx;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
void main() {
|
|
316
|
+
vec4 texColor = texture(uSource, vUv);
|
|
317
|
+
vec2 centered = vUv * uResolution - uResolution * 0.5;
|
|
318
|
+
mat3 rotation = rotationMatrix(uRotationX, uRotationY, uRotation);
|
|
319
|
+
vec2 gridCoord;
|
|
320
|
+
|
|
321
|
+
if (uPerspective <= 0.0) {
|
|
322
|
+
vec3 rotated = rotation * vec3(centered, 0.0);
|
|
323
|
+
gridCoord = rotated.xy + uOffset;
|
|
324
|
+
} else {
|
|
325
|
+
vec3 camera = vec3(0.0, 0.0, uPerspective);
|
|
326
|
+
vec3 ray = normalize(vec3(centered, -uPerspective));
|
|
327
|
+
vec3 normal = rotation * vec3(0.0, 0.0, 1.0);
|
|
328
|
+
float denominator = dot(ray, normal);
|
|
329
|
+
float t = -dot(camera, normal) / denominator;
|
|
330
|
+
|
|
331
|
+
if (abs(denominator) < 0.0001 || t <= 0.0) {
|
|
332
|
+
fragColor = sourceOver(texColor, uBackgroundColor);
|
|
333
|
+
return;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
vec3 worldPosition = camera + ray * t;
|
|
337
|
+
vec3 localPosition = transpose(rotation) * worldPosition;
|
|
338
|
+
gridCoord = localPosition.xy + uOffset;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
float gridSize = max(uGridSize, 0.001);
|
|
342
|
+
vec2 cell = abs(fract(gridCoord / gridSize + 0.5) - 0.5) * gridSize;
|
|
343
|
+
float halfLineWidth = uLineWidth * 0.5;
|
|
344
|
+
vec2 gridUnitsPerPixel = max(fwidth(gridCoord), vec2(0.001));
|
|
345
|
+
vec2 halfWidthInGridUnits = halfLineWidth * gridUnitsPerPixel;
|
|
346
|
+
vec2 antialiasInGridUnits = gridUnitsPerPixel;
|
|
347
|
+
vec2 projectedCellSize = gridSize / gridUnitsPerPixel;
|
|
348
|
+
vec2 densityFade = smoothstep(
|
|
349
|
+
vec2(max(uLineWidth * 1.5, 1.0)),
|
|
350
|
+
vec2(max(uLineWidth * 4.0, 2.0)),
|
|
351
|
+
projectedCellSize
|
|
352
|
+
);
|
|
353
|
+
vec2 lineCoverage = halfLineWidth <= 0.001
|
|
354
|
+
? vec2(0.0)
|
|
355
|
+
: 1.0 - smoothstep(
|
|
356
|
+
halfWidthInGridUnits - antialiasInGridUnits,
|
|
357
|
+
halfWidthInGridUnits + antialiasInGridUnits,
|
|
358
|
+
cell
|
|
359
|
+
);
|
|
360
|
+
lineCoverage *= densityFade;
|
|
361
|
+
float coverage = max(lineCoverage.x, lineCoverage.y);
|
|
362
|
+
|
|
363
|
+
vec4 background = uBackgroundColor;
|
|
364
|
+
vec4 line = uLineColor * coverage;
|
|
365
|
+
|
|
366
|
+
if (uMaskToSourceAlpha) {
|
|
367
|
+
fragColor = sourceAtop(sourceAtop(texColor, background), line);
|
|
368
|
+
return;
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
fragColor = sourceOver(sourceOver(texColor, background), line);
|
|
372
|
+
}
|
|
373
|
+
`;
|
|
374
|
+
var compileShader = (gl, type, source) => {
|
|
375
|
+
const shader = gl.createShader(type);
|
|
376
|
+
if (!shader) {
|
|
377
|
+
throw new Error("Failed to create WebGL shader");
|
|
378
|
+
}
|
|
379
|
+
gl.shaderSource(shader, source);
|
|
380
|
+
gl.compileShader(shader);
|
|
381
|
+
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
|
|
382
|
+
const log = gl.getShaderInfoLog(shader);
|
|
383
|
+
gl.deleteShader(shader);
|
|
384
|
+
throw new Error(`Gridlines shader compile failed: ${log ?? "(no log)"}`);
|
|
385
|
+
}
|
|
386
|
+
return shader;
|
|
387
|
+
};
|
|
388
|
+
var linkProgram = (gl, vs, fs) => {
|
|
389
|
+
const program = gl.createProgram();
|
|
390
|
+
if (!program) {
|
|
391
|
+
throw new Error("Failed to create WebGL program");
|
|
392
|
+
}
|
|
393
|
+
gl.attachShader(program, vs);
|
|
394
|
+
gl.attachShader(program, fs);
|
|
395
|
+
gl.linkProgram(program);
|
|
396
|
+
if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
|
|
397
|
+
const log = gl.getProgramInfoLog(program);
|
|
398
|
+
gl.deleteProgram(program);
|
|
399
|
+
throw new Error(`Gridlines program link failed: ${log ?? "(no log)"}`);
|
|
400
|
+
}
|
|
401
|
+
return program;
|
|
402
|
+
};
|
|
403
|
+
var rgbaToUniform = (rgba) => {
|
|
404
|
+
const [r, g, b, a] = rgba;
|
|
405
|
+
const alpha = a / 255;
|
|
406
|
+
return [r / 255 * alpha, g / 255 * alpha, b / 255 * alpha, alpha];
|
|
407
|
+
};
|
|
408
|
+
var gridlines = createEffect({
|
|
409
|
+
type: "remotion/gridlines",
|
|
410
|
+
label: "gridlines()",
|
|
411
|
+
documentationLink: "https://www.remotion.dev/docs/effects/gridlines",
|
|
412
|
+
backend: "webgl2",
|
|
413
|
+
calculateKey: (params) => {
|
|
414
|
+
const r = resolve(params);
|
|
415
|
+
const maskSuffix = r.maskToSourceAlpha ? "-mask-to-source-alpha" : "";
|
|
416
|
+
return `gridlines-${r.gridSize}-${r.lineWidth}-${r.lineColor}-${r.backgroundColor}-${r.rotation}-${r.rotationX}-${r.rotationY}-${r.perspective}-${r.offsetX}-${r.offsetY}${maskSuffix}`;
|
|
417
|
+
},
|
|
418
|
+
setup: (target) => {
|
|
419
|
+
const gl = target.getContext("webgl2", {
|
|
420
|
+
premultipliedAlpha: true,
|
|
421
|
+
alpha: true,
|
|
422
|
+
preserveDrawingBuffer: true
|
|
423
|
+
});
|
|
424
|
+
if (!gl) {
|
|
425
|
+
throw createWebGL2ContextError("gridlines effect");
|
|
426
|
+
}
|
|
427
|
+
gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, true);
|
|
428
|
+
const vs = compileShader(gl, gl.VERTEX_SHADER, GRIDLINES_VS);
|
|
429
|
+
const fs = compileShader(gl, gl.FRAGMENT_SHADER, GRIDLINES_FS);
|
|
430
|
+
const program = linkProgram(gl, vs, fs);
|
|
431
|
+
gl.deleteShader(vs);
|
|
432
|
+
gl.deleteShader(fs);
|
|
433
|
+
const vao = gl.createVertexArray();
|
|
434
|
+
if (!vao) {
|
|
435
|
+
throw new Error("Failed to create WebGL vertex array");
|
|
436
|
+
}
|
|
437
|
+
gl.bindVertexArray(vao);
|
|
438
|
+
const data = new Float32Array([
|
|
439
|
+
-1,
|
|
440
|
+
-1,
|
|
441
|
+
0,
|
|
442
|
+
0,
|
|
443
|
+
1,
|
|
444
|
+
-1,
|
|
445
|
+
1,
|
|
446
|
+
0,
|
|
447
|
+
-1,
|
|
448
|
+
1,
|
|
449
|
+
0,
|
|
450
|
+
1,
|
|
451
|
+
1,
|
|
452
|
+
1,
|
|
453
|
+
1,
|
|
454
|
+
1
|
|
455
|
+
]);
|
|
456
|
+
const vbo = gl.createBuffer();
|
|
457
|
+
if (!vbo) {
|
|
458
|
+
throw new Error("Failed to create WebGL buffer");
|
|
459
|
+
}
|
|
460
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, vbo);
|
|
461
|
+
gl.bufferData(gl.ARRAY_BUFFER, data, gl.STATIC_DRAW);
|
|
462
|
+
const aPos = gl.getAttribLocation(program, "aPos");
|
|
463
|
+
const aUv = gl.getAttribLocation(program, "aUv");
|
|
464
|
+
gl.enableVertexAttribArray(aPos);
|
|
465
|
+
gl.vertexAttribPointer(aPos, 2, gl.FLOAT, false, 16, 0);
|
|
466
|
+
gl.enableVertexAttribArray(aUv);
|
|
467
|
+
gl.vertexAttribPointer(aUv, 2, gl.FLOAT, false, 16, 8);
|
|
468
|
+
gl.bindVertexArray(null);
|
|
469
|
+
const texture = gl.createTexture();
|
|
470
|
+
if (!texture) {
|
|
471
|
+
throw new Error("Failed to create WebGL texture");
|
|
472
|
+
}
|
|
473
|
+
gl.bindTexture(gl.TEXTURE_2D, texture);
|
|
474
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
|
|
475
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
|
|
476
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
|
|
477
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
|
|
478
|
+
gl.bindTexture(gl.TEXTURE_2D, null);
|
|
479
|
+
const colorCanvas = document.createElement("canvas");
|
|
480
|
+
colorCanvas.width = 1;
|
|
481
|
+
colorCanvas.height = 1;
|
|
482
|
+
const colorCtx = colorCanvas.getContext("2d", { willReadFrequently: true });
|
|
483
|
+
if (!colorCtx) {
|
|
484
|
+
throw new Error("Failed to acquire 2D context for color parsing");
|
|
485
|
+
}
|
|
486
|
+
return {
|
|
487
|
+
gl,
|
|
488
|
+
program,
|
|
489
|
+
vao,
|
|
490
|
+
vbo,
|
|
491
|
+
texture,
|
|
492
|
+
uniforms: {
|
|
493
|
+
uSource: gl.getUniformLocation(program, "uSource"),
|
|
494
|
+
uResolution: gl.getUniformLocation(program, "uResolution"),
|
|
495
|
+
uGridSize: gl.getUniformLocation(program, "uGridSize"),
|
|
496
|
+
uLineWidth: gl.getUniformLocation(program, "uLineWidth"),
|
|
497
|
+
uLineColor: gl.getUniformLocation(program, "uLineColor"),
|
|
498
|
+
uBackgroundColor: gl.getUniformLocation(program, "uBackgroundColor"),
|
|
499
|
+
uRotation: gl.getUniformLocation(program, "uRotation"),
|
|
500
|
+
uRotationX: gl.getUniformLocation(program, "uRotationX"),
|
|
501
|
+
uRotationY: gl.getUniformLocation(program, "uRotationY"),
|
|
502
|
+
uPerspective: gl.getUniformLocation(program, "uPerspective"),
|
|
503
|
+
uOffset: gl.getUniformLocation(program, "uOffset"),
|
|
504
|
+
uMaskToSourceAlpha: gl.getUniformLocation(program, "uMaskToSourceAlpha")
|
|
505
|
+
},
|
|
506
|
+
colorCtx,
|
|
507
|
+
cachedLineColorStr: "",
|
|
508
|
+
cachedLineColorRgba: [255, 255, 255, 255],
|
|
509
|
+
cachedBackgroundColorStr: "",
|
|
510
|
+
cachedBackgroundColorRgba: [0, 0, 0, 0]
|
|
511
|
+
};
|
|
512
|
+
},
|
|
513
|
+
apply: ({ source, width, height, params, state, flipSourceY }) => {
|
|
514
|
+
const r = resolve(params);
|
|
515
|
+
const { gl, program, vao, texture, uniforms } = state;
|
|
516
|
+
if (state.cachedLineColorStr !== r.lineColor) {
|
|
517
|
+
state.cachedLineColorStr = r.lineColor;
|
|
518
|
+
state.cachedLineColorRgba = parseColorRgba(state.colorCtx, r.lineColor);
|
|
519
|
+
}
|
|
520
|
+
if (state.cachedBackgroundColorStr !== r.backgroundColor) {
|
|
521
|
+
state.cachedBackgroundColorStr = r.backgroundColor;
|
|
522
|
+
state.cachedBackgroundColorRgba = parseColorRgba(state.colorCtx, r.backgroundColor);
|
|
523
|
+
}
|
|
524
|
+
const [lineR, lineG, lineB, lineA] = rgbaToUniform(state.cachedLineColorRgba);
|
|
525
|
+
const [bgR, bgG, bgB, bgA] = rgbaToUniform(state.cachedBackgroundColorRgba);
|
|
526
|
+
gl.viewport(0, 0, width, height);
|
|
527
|
+
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
|
|
528
|
+
gl.clearColor(0, 0, 0, 0);
|
|
529
|
+
gl.clear(gl.COLOR_BUFFER_BIT);
|
|
530
|
+
gl.useProgram(program);
|
|
531
|
+
gl.bindVertexArray(vao);
|
|
532
|
+
gl.activeTexture(gl.TEXTURE0);
|
|
533
|
+
gl.bindTexture(gl.TEXTURE_2D, texture);
|
|
534
|
+
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, flipSourceY);
|
|
535
|
+
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, source);
|
|
536
|
+
if (uniforms.uSource)
|
|
537
|
+
gl.uniform1i(uniforms.uSource, 0);
|
|
538
|
+
if (uniforms.uResolution)
|
|
539
|
+
gl.uniform2f(uniforms.uResolution, width, height);
|
|
540
|
+
if (uniforms.uGridSize)
|
|
541
|
+
gl.uniform1f(uniforms.uGridSize, r.gridSize);
|
|
542
|
+
if (uniforms.uLineWidth)
|
|
543
|
+
gl.uniform1f(uniforms.uLineWidth, r.lineWidth);
|
|
544
|
+
if (uniforms.uLineColor)
|
|
545
|
+
gl.uniform4f(uniforms.uLineColor, lineR, lineG, lineB, lineA);
|
|
546
|
+
if (uniforms.uBackgroundColor)
|
|
547
|
+
gl.uniform4f(uniforms.uBackgroundColor, bgR, bgG, bgB, bgA);
|
|
548
|
+
if (uniforms.uRotation)
|
|
549
|
+
gl.uniform1f(uniforms.uRotation, r.rotation * Math.PI / 180);
|
|
550
|
+
if (uniforms.uRotationX)
|
|
551
|
+
gl.uniform1f(uniforms.uRotationX, r.rotationX * Math.PI / 180);
|
|
552
|
+
if (uniforms.uRotationY)
|
|
553
|
+
gl.uniform1f(uniforms.uRotationY, r.rotationY * Math.PI / 180);
|
|
554
|
+
if (uniforms.uPerspective)
|
|
555
|
+
gl.uniform1f(uniforms.uPerspective, r.perspective);
|
|
556
|
+
if (uniforms.uOffset)
|
|
557
|
+
gl.uniform2f(uniforms.uOffset, r.offsetX, r.offsetY);
|
|
558
|
+
if (uniforms.uMaskToSourceAlpha)
|
|
559
|
+
gl.uniform1i(uniforms.uMaskToSourceAlpha, r.maskToSourceAlpha ? 1 : 0);
|
|
560
|
+
gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
|
|
561
|
+
gl.bindVertexArray(null);
|
|
562
|
+
gl.bindTexture(gl.TEXTURE_2D, null);
|
|
563
|
+
gl.useProgram(null);
|
|
564
|
+
},
|
|
565
|
+
cleanup: ({ gl, program, vao, vbo, texture }) => {
|
|
566
|
+
gl.deleteBuffer(vbo);
|
|
567
|
+
gl.deleteProgram(program);
|
|
568
|
+
gl.deleteVertexArray(vao);
|
|
569
|
+
gl.deleteTexture(texture);
|
|
570
|
+
},
|
|
571
|
+
schema: gridlinesSchema,
|
|
572
|
+
validateParams: validateGridlinesParams
|
|
573
|
+
});
|
|
574
|
+
export {
|
|
575
|
+
gridlinesSchema,
|
|
576
|
+
gridlines
|
|
577
|
+
};
|