@remotion/effects 4.0.461 → 4.0.462

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,341 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __export = (target, all) => {
3
+ for (var name in all)
4
+ __defProp(target, name, {
5
+ get: all[name],
6
+ enumerable: true,
7
+ configurable: true,
8
+ set: (newValue) => all[name] = () => newValue
9
+ });
10
+ };
11
+
12
+ // src/halftone.ts
13
+ import { Internals } from "remotion";
14
+ var { createEffect } = Internals;
15
+ var SHADE_OUTSIDE_DOT_SCALE = 0.5;
16
+ var halftoneSchema = {
17
+ dotSize: {
18
+ type: "number",
19
+ min: 1,
20
+ max: 200,
21
+ step: 1,
22
+ default: 20,
23
+ description: "Dot size"
24
+ },
25
+ dotSpacing: {
26
+ type: "number",
27
+ min: 1,
28
+ max: 200,
29
+ step: 1,
30
+ default: 20,
31
+ description: "Dot spacing"
32
+ },
33
+ rotation: {
34
+ type: "number",
35
+ min: -180,
36
+ max: 180,
37
+ step: 1,
38
+ default: 0,
39
+ description: "Rotation"
40
+ },
41
+ offsetX: {
42
+ type: "number",
43
+ step: 1,
44
+ default: 0,
45
+ description: "Offset X"
46
+ },
47
+ offsetY: {
48
+ type: "number",
49
+ step: 1,
50
+ default: 0,
51
+ description: "Offset Y"
52
+ }
53
+ };
54
+ var resolve = (p) => ({
55
+ shape: p.shape ?? "circle",
56
+ dotSize: p.dotSize ?? 20,
57
+ dotSpacing: p.dotSpacing ?? p.dotSize ?? 20,
58
+ rotation: p.rotation ?? 0,
59
+ offsetX: p.offsetX ?? 0,
60
+ offsetY: p.offsetY ?? 0,
61
+ sampling: p.sampling ?? "bilinear",
62
+ color: p.color ?? "black",
63
+ shadeOutside: p.shadeOutside ?? false
64
+ });
65
+ var HALFTONE_VS = `#version 300 es
66
+ in vec2 aPos;
67
+ in vec2 aUv;
68
+ out vec2 vUv;
69
+ void main() {
70
+ vUv = aUv;
71
+ gl_Position = vec4(aPos, 0.0, 1.0);
72
+ }
73
+ `;
74
+ var HALFTONE_FS = `#version 300 es
75
+ precision highp float;
76
+
77
+ in vec2 vUv;
78
+ out vec4 fragColor;
79
+
80
+ uniform sampler2D uSource;
81
+ uniform vec2 uResolution;
82
+ uniform float uDotSize;
83
+ uniform float uDotSpacing;
84
+ uniform float uRotation;
85
+ uniform vec2 uOffset;
86
+ uniform vec4 uColor;
87
+ uniform int uShape;
88
+ uniform bool uShadeOutside;
89
+
90
+ const float SHADE_OUTSIDE_SCALE = ${SHADE_OUTSIDE_DOT_SCALE.toFixed(1)};
91
+
92
+ void main() {
93
+ vec2 fragPos = vUv * uResolution;
94
+ vec2 center = uResolution * 0.5;
95
+
96
+ vec2 d = fragPos - center;
97
+ float cosR = cos(uRotation);
98
+ float sinR = sin(uRotation);
99
+
100
+ vec2 gridPos = vec2(
101
+ d.x * cosR + d.y * sinR,
102
+ -d.x * sinR + d.y * cosR
103
+ );
104
+
105
+ float spacing = max(uDotSpacing, 0.001);
106
+ vec2 cellIndex = floor((gridPos + uOffset) / spacing + 0.5);
107
+ vec2 gridCenter = cellIndex * spacing - uOffset;
108
+
109
+ vec2 canvasPos = center + vec2(
110
+ gridCenter.x * cosR - gridCenter.y * sinR,
111
+ gridCenter.x * sinR + gridCenter.y * cosR
112
+ );
113
+
114
+ vec2 sampleUv = clamp(canvasPos / uResolution, vec2(0.0), vec2(1.0));
115
+ vec4 texColor = texture(uSource, sampleUv);
116
+
117
+ float alpha = texColor.a;
118
+ vec3 rgb = alpha > 0.001 ? texColor.rgb / alpha : vec3(0.0);
119
+ float lum = dot(rgb, vec3(0.299, 0.587, 0.114));
120
+
121
+ float lumDefault = lum * alpha + (1.0 - alpha);
122
+ float dotScale = uShadeOutside
123
+ ? (1.0 - alpha) * SHADE_OUTSIDE_SCALE
124
+ : 1.0 - lumDefault;
125
+
126
+ if (dotScale <= 0.01) {
127
+ fragColor = vec4(0.0);
128
+ return;
129
+ }
130
+
131
+ vec2 diff = gridPos - gridCenter;
132
+ float halfSize = uDotSize * 0.5;
133
+ float coverage = 0.0;
134
+
135
+ if (uShape == 0) {
136
+ float radius = halfSize * dotScale;
137
+ float dist = length(diff);
138
+ coverage = 1.0 - smoothstep(radius - 0.75, radius + 0.75, dist);
139
+ } else if (uShape == 1) {
140
+ float s = uDotSize * dotScale * 0.5;
141
+ coverage = (1.0 - smoothstep(s - 0.5, s + 0.5, abs(diff.x)))
142
+ * (1.0 - smoothstep(s - 0.5, s + 0.5, abs(diff.y)));
143
+ } else {
144
+ float lineHalf = uDotSize * dotScale * 0.5;
145
+ coverage = (1.0 - smoothstep(halfSize - 0.5, halfSize + 0.5, abs(diff.x)))
146
+ * (1.0 - smoothstep(lineHalf - 0.5, lineHalf + 0.5, abs(diff.y)));
147
+ }
148
+
149
+ fragColor = uColor * coverage;
150
+ }
151
+ `;
152
+ var SHAPE_INDEX = {
153
+ circle: 0,
154
+ square: 1,
155
+ line: 2
156
+ };
157
+ var compileShader = (gl, type, source) => {
158
+ const shader = gl.createShader(type);
159
+ if (!shader) {
160
+ throw new Error("Failed to create WebGL shader");
161
+ }
162
+ gl.shaderSource(shader, source);
163
+ gl.compileShader(shader);
164
+ if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
165
+ const log = gl.getShaderInfoLog(shader);
166
+ gl.deleteShader(shader);
167
+ throw new Error(`Halftone shader compile failed: ${log ?? "(no log)"}`);
168
+ }
169
+ return shader;
170
+ };
171
+ var linkProgram = (gl, vs, fs) => {
172
+ const program = gl.createProgram();
173
+ if (!program) {
174
+ throw new Error("Failed to create WebGL program");
175
+ }
176
+ gl.attachShader(program, vs);
177
+ gl.attachShader(program, fs);
178
+ gl.linkProgram(program);
179
+ if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
180
+ const log = gl.getProgramInfoLog(program);
181
+ gl.deleteProgram(program);
182
+ throw new Error(`Halftone program link failed: ${log ?? "(no log)"}`);
183
+ }
184
+ return program;
185
+ };
186
+ var parseColorRgba = (ctx, color) => {
187
+ ctx.clearRect(0, 0, 1, 1);
188
+ ctx.fillStyle = color;
189
+ ctx.fillRect(0, 0, 1, 1);
190
+ const { data } = ctx.getImageData(0, 0, 1, 1);
191
+ return [data[0] / 255, data[1] / 255, data[2] / 255, data[3] / 255];
192
+ };
193
+ var halftone = createEffect({
194
+ type: "remotion/halftone",
195
+ label: "Halftone",
196
+ backend: "webgl2",
197
+ calculateKey: (params) => {
198
+ const r = resolve(params);
199
+ return `halftone-${r.shape}-${r.dotSize}-${r.dotSpacing}-${r.rotation}-${r.offsetX}-${r.offsetY}-${r.sampling}-${r.color}-${r.shadeOutside ? 1 : 0}`;
200
+ },
201
+ setup: (target) => {
202
+ const gl = target.getContext("webgl2", {
203
+ premultipliedAlpha: true,
204
+ alpha: true,
205
+ preserveDrawingBuffer: true
206
+ });
207
+ if (!gl) {
208
+ throw new Error("Failed to acquire WebGL2 context for halftone effect");
209
+ }
210
+ gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, true);
211
+ gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true);
212
+ const vs = compileShader(gl, gl.VERTEX_SHADER, HALFTONE_VS);
213
+ const fs = compileShader(gl, gl.FRAGMENT_SHADER, HALFTONE_FS);
214
+ const program = linkProgram(gl, vs, fs);
215
+ gl.deleteShader(vs);
216
+ gl.deleteShader(fs);
217
+ const vao = gl.createVertexArray();
218
+ if (!vao) {
219
+ throw new Error("Failed to create WebGL vertex array");
220
+ }
221
+ gl.bindVertexArray(vao);
222
+ const data = new Float32Array([
223
+ -1,
224
+ -1,
225
+ 0,
226
+ 0,
227
+ 1,
228
+ -1,
229
+ 1,
230
+ 0,
231
+ -1,
232
+ 1,
233
+ 0,
234
+ 1,
235
+ 1,
236
+ 1,
237
+ 1,
238
+ 1
239
+ ]);
240
+ const vbo = gl.createBuffer();
241
+ if (!vbo) {
242
+ throw new Error("Failed to create WebGL buffer");
243
+ }
244
+ gl.bindBuffer(gl.ARRAY_BUFFER, vbo);
245
+ gl.bufferData(gl.ARRAY_BUFFER, data, gl.STATIC_DRAW);
246
+ const aPos = gl.getAttribLocation(program, "aPos");
247
+ const aUv = gl.getAttribLocation(program, "aUv");
248
+ gl.enableVertexAttribArray(aPos);
249
+ gl.vertexAttribPointer(aPos, 2, gl.FLOAT, false, 16, 0);
250
+ gl.enableVertexAttribArray(aUv);
251
+ gl.vertexAttribPointer(aUv, 2, gl.FLOAT, false, 16, 8);
252
+ gl.bindVertexArray(null);
253
+ const texture = gl.createTexture();
254
+ if (!texture) {
255
+ throw new Error("Failed to create WebGL texture");
256
+ }
257
+ gl.bindTexture(gl.TEXTURE_2D, texture);
258
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
259
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
260
+ gl.bindTexture(gl.TEXTURE_2D, null);
261
+ const colorCanvas = document.createElement("canvas");
262
+ colorCanvas.width = 1;
263
+ colorCanvas.height = 1;
264
+ const colorCtx = colorCanvas.getContext("2d", { willReadFrequently: true });
265
+ if (!colorCtx) {
266
+ throw new Error("Failed to acquire 2D context for color parsing");
267
+ }
268
+ return {
269
+ gl,
270
+ program,
271
+ vao,
272
+ vbo,
273
+ texture,
274
+ uSource: gl.getUniformLocation(program, "uSource"),
275
+ uResolution: gl.getUniformLocation(program, "uResolution"),
276
+ uDotSize: gl.getUniformLocation(program, "uDotSize"),
277
+ uDotSpacing: gl.getUniformLocation(program, "uDotSpacing"),
278
+ uRotation: gl.getUniformLocation(program, "uRotation"),
279
+ uOffset: gl.getUniformLocation(program, "uOffset"),
280
+ uColor: gl.getUniformLocation(program, "uColor"),
281
+ uShape: gl.getUniformLocation(program, "uShape"),
282
+ uShadeOutside: gl.getUniformLocation(program, "uShadeOutside"),
283
+ colorCtx,
284
+ cachedColorStr: "",
285
+ cachedColorRgba: [0, 0, 0, 1]
286
+ };
287
+ },
288
+ apply: ({ source, width, height, params, state }) => {
289
+ const r = resolve(params);
290
+ const { gl, program, vao, texture } = state;
291
+ if (state.cachedColorStr !== r.color) {
292
+ state.cachedColorStr = r.color;
293
+ state.cachedColorRgba = parseColorRgba(state.colorCtx, r.color);
294
+ }
295
+ const [cr, cg, cb, ca] = state.cachedColorRgba;
296
+ const filter = r.sampling === "nearest" ? gl.NEAREST : gl.LINEAR;
297
+ gl.viewport(0, 0, width, height);
298
+ gl.clearColor(0, 0, 0, 0);
299
+ gl.clear(gl.COLOR_BUFFER_BIT);
300
+ gl.useProgram(program);
301
+ gl.bindVertexArray(vao);
302
+ gl.activeTexture(gl.TEXTURE0);
303
+ gl.bindTexture(gl.TEXTURE_2D, texture);
304
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, filter);
305
+ gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, filter);
306
+ gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, source);
307
+ if (state.uSource)
308
+ gl.uniform1i(state.uSource, 0);
309
+ if (state.uResolution)
310
+ gl.uniform2f(state.uResolution, width, height);
311
+ if (state.uDotSize)
312
+ gl.uniform1f(state.uDotSize, r.dotSize);
313
+ if (state.uDotSpacing)
314
+ gl.uniform1f(state.uDotSpacing, r.dotSpacing);
315
+ if (state.uRotation)
316
+ gl.uniform1f(state.uRotation, r.rotation * Math.PI / 180);
317
+ if (state.uOffset)
318
+ gl.uniform2f(state.uOffset, r.offsetX, r.offsetY);
319
+ if (state.uColor)
320
+ gl.uniform4f(state.uColor, cr * ca, cg * ca, cb * ca, ca);
321
+ if (state.uShape)
322
+ gl.uniform1i(state.uShape, SHAPE_INDEX[r.shape]);
323
+ if (state.uShadeOutside)
324
+ gl.uniform1i(state.uShadeOutside, r.shadeOutside ? 1 : 0);
325
+ gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
326
+ gl.bindVertexArray(null);
327
+ gl.bindTexture(gl.TEXTURE_2D, null);
328
+ gl.useProgram(null);
329
+ },
330
+ cleanup: ({ gl, program, vao, vbo, texture }) => {
331
+ gl.deleteBuffer(vbo);
332
+ gl.deleteProgram(program);
333
+ gl.deleteVertexArray(vao);
334
+ gl.deleteTexture(texture);
335
+ },
336
+ schema: halftoneSchema
337
+ });
338
+ export {
339
+ halftoneSchema,
340
+ halftone
341
+ };