@twick/live-player 0.15.20 → 0.15.21
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/index.js +6522 -6163
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +6522 -6163
- package/dist/index.mjs.map +1 -1
- package/dist/{internal-LKI2GAXH-B2uGn4sZ.js → internal-B6KRB6EV-BvBtLd70.mjs} +274 -2
- package/dist/internal-B6KRB6EV-BvBtLd70.mjs.map +1 -0
- package/dist/{internal-LKI2GAXH-C1j65akX.mjs → internal-B6KRB6EV-DXLdD4g5.js} +275 -1
- package/dist/internal-B6KRB6EV-DXLdD4g5.js.map +1 -0
- package/package.json +5 -4
- package/dist/internal-LKI2GAXH-B2uGn4sZ.js.map +0 -1
- package/dist/internal-LKI2GAXH-C1j65akX.mjs.map +0 -1
|
@@ -1,7 +1,190 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
var __defProp = Object.defineProperty;
|
|
3
2
|
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
4
3
|
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
4
|
+
var dist = {};
|
|
5
|
+
var runtime = {};
|
|
6
|
+
Object.defineProperty(runtime, "__esModule", { value: true });
|
|
7
|
+
runtime.createEffectContext = createEffectContext;
|
|
8
|
+
runtime.applyEffects = applyEffects;
|
|
9
|
+
var BASIC_VERTEX_SHADER = "\n attribute vec2 a_position;\n attribute vec2 a_texCoord;\n\n varying vec2 v_texCoord;\n\n void main() {\n v_texCoord = a_texCoord;\n gl_Position = vec4(a_position, 0.0, 1.0);\n }\n";
|
|
10
|
+
function createShader(gl, type, source) {
|
|
11
|
+
var shader = gl.createShader(type);
|
|
12
|
+
if (!shader) {
|
|
13
|
+
throw new Error("Failed to create shader");
|
|
14
|
+
}
|
|
15
|
+
gl.shaderSource(shader, source);
|
|
16
|
+
gl.compileShader(shader);
|
|
17
|
+
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
|
|
18
|
+
var info = gl.getShaderInfoLog(shader) || "Unknown error";
|
|
19
|
+
gl.deleteShader(shader);
|
|
20
|
+
throw new Error("Failed to compile shader: ".concat(info));
|
|
21
|
+
}
|
|
22
|
+
return shader;
|
|
23
|
+
}
|
|
24
|
+
function createProgram(gl, vertexSrc, fragmentSrc) {
|
|
25
|
+
var vertexShader = createShader(gl, gl.VERTEX_SHADER, vertexSrc);
|
|
26
|
+
var fragmentShader = createShader(gl, gl.FRAGMENT_SHADER, fragmentSrc);
|
|
27
|
+
var program = gl.createProgram();
|
|
28
|
+
if (!program) {
|
|
29
|
+
throw new Error("Failed to create program");
|
|
30
|
+
}
|
|
31
|
+
gl.attachShader(program, vertexShader);
|
|
32
|
+
gl.attachShader(program, fragmentShader);
|
|
33
|
+
gl.linkProgram(program);
|
|
34
|
+
if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
|
|
35
|
+
var info = gl.getProgramInfoLog(program) || "Unknown error";
|
|
36
|
+
gl.deleteProgram(program);
|
|
37
|
+
gl.deleteShader(vertexShader);
|
|
38
|
+
gl.deleteShader(fragmentShader);
|
|
39
|
+
throw new Error("Failed to link program: ".concat(info));
|
|
40
|
+
}
|
|
41
|
+
gl.deleteShader(vertexShader);
|
|
42
|
+
gl.deleteShader(fragmentShader);
|
|
43
|
+
return program;
|
|
44
|
+
}
|
|
45
|
+
function createEffectContext(canvas) {
|
|
46
|
+
var gl = canvas.getContext("webgl") || canvas.getContext("experimental-webgl");
|
|
47
|
+
if (!gl) {
|
|
48
|
+
throw new Error("WebGL not supported");
|
|
49
|
+
}
|
|
50
|
+
var quadBuffer = gl.createBuffer();
|
|
51
|
+
if (!quadBuffer) {
|
|
52
|
+
throw new Error("Failed to create quad buffer");
|
|
53
|
+
}
|
|
54
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, quadBuffer);
|
|
55
|
+
var quadVertices = new Float32Array([
|
|
56
|
+
-1,
|
|
57
|
+
-1,
|
|
58
|
+
//
|
|
59
|
+
1,
|
|
60
|
+
-1,
|
|
61
|
+
-1,
|
|
62
|
+
1,
|
|
63
|
+
1,
|
|
64
|
+
1
|
|
65
|
+
]);
|
|
66
|
+
gl.bufferData(gl.ARRAY_BUFFER, quadVertices, gl.STATIC_DRAW);
|
|
67
|
+
var texCoordBuffer = gl.createBuffer();
|
|
68
|
+
if (!texCoordBuffer) {
|
|
69
|
+
throw new Error("Failed to create texCoord buffer");
|
|
70
|
+
}
|
|
71
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, texCoordBuffer);
|
|
72
|
+
var texCoords = new Float32Array([
|
|
73
|
+
0,
|
|
74
|
+
0,
|
|
75
|
+
//
|
|
76
|
+
1,
|
|
77
|
+
0,
|
|
78
|
+
0,
|
|
79
|
+
1,
|
|
80
|
+
1,
|
|
81
|
+
1
|
|
82
|
+
]);
|
|
83
|
+
gl.bufferData(gl.ARRAY_BUFFER, texCoords, gl.STATIC_DRAW);
|
|
84
|
+
var framebuffers = [];
|
|
85
|
+
var textures = [];
|
|
86
|
+
return {
|
|
87
|
+
gl,
|
|
88
|
+
programCache: /* @__PURE__ */ new Map(),
|
|
89
|
+
quadBuffer,
|
|
90
|
+
texCoordBuffer,
|
|
91
|
+
framebuffers,
|
|
92
|
+
textures
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
function ensureRenderTargets(ctx, width, height) {
|
|
96
|
+
var gl = ctx.gl, framebuffers = ctx.framebuffers, textures = ctx.textures;
|
|
97
|
+
while (framebuffers.length < 2) {
|
|
98
|
+
var fb = gl.createFramebuffer();
|
|
99
|
+
var tex = gl.createTexture();
|
|
100
|
+
if (!fb || !tex) {
|
|
101
|
+
throw new Error("Failed to create framebuffer or texture");
|
|
102
|
+
}
|
|
103
|
+
gl.bindTexture(gl.TEXTURE_2D, tex);
|
|
104
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
|
|
105
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
|
|
106
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
|
|
107
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
|
|
108
|
+
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, width, height, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
|
|
109
|
+
gl.bindFramebuffer(gl.FRAMEBUFFER, fb);
|
|
110
|
+
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, tex, 0);
|
|
111
|
+
framebuffers.push(fb);
|
|
112
|
+
textures.push(tex);
|
|
113
|
+
}
|
|
114
|
+
for (var i = 0; i < textures.length; i++) {
|
|
115
|
+
gl.bindTexture(gl.TEXTURE_2D, textures[i]);
|
|
116
|
+
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, width, height, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
function applyEffects(opts) {
|
|
120
|
+
var ctx = opts.ctx, sourceTexture = opts.sourceTexture, width = opts.width, height = opts.height, effects = opts.effects;
|
|
121
|
+
var gl = ctx.gl, programCache = ctx.programCache, quadBuffer = ctx.quadBuffer, texCoordBuffer = ctx.texCoordBuffer, framebuffers = ctx.framebuffers, textures = ctx.textures;
|
|
122
|
+
if (!effects.length) {
|
|
123
|
+
return sourceTexture;
|
|
124
|
+
}
|
|
125
|
+
ensureRenderTargets(ctx, width, height);
|
|
126
|
+
var readTexture = sourceTexture;
|
|
127
|
+
var writeIndex = 0;
|
|
128
|
+
gl.viewport(0, 0, width, height);
|
|
129
|
+
effects.forEach(function(active) {
|
|
130
|
+
if (!active.fragment)
|
|
131
|
+
return;
|
|
132
|
+
var cacheKey = active.fragment;
|
|
133
|
+
var program = programCache.get(cacheKey);
|
|
134
|
+
if (!program) {
|
|
135
|
+
program = createProgram(gl, BASIC_VERTEX_SHADER, active.fragment);
|
|
136
|
+
programCache.set(cacheKey, program);
|
|
137
|
+
}
|
|
138
|
+
gl.useProgram(program);
|
|
139
|
+
var positionLocation = gl.getAttribLocation(program, "a_position");
|
|
140
|
+
var texCoordLocation = gl.getAttribLocation(program, "a_texCoord");
|
|
141
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, quadBuffer);
|
|
142
|
+
gl.enableVertexAttribArray(positionLocation);
|
|
143
|
+
gl.vertexAttribPointer(positionLocation, 2, gl.FLOAT, false, 0, 0);
|
|
144
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, texCoordBuffer);
|
|
145
|
+
gl.enableVertexAttribArray(texCoordLocation);
|
|
146
|
+
gl.vertexAttribPointer(texCoordLocation, 2, gl.FLOAT, false, 0, 0);
|
|
147
|
+
var fb = framebuffers[writeIndex];
|
|
148
|
+
var targetTex = textures[writeIndex];
|
|
149
|
+
gl.bindFramebuffer(gl.FRAMEBUFFER, fb);
|
|
150
|
+
gl.activeTexture(gl.TEXTURE0);
|
|
151
|
+
gl.bindTexture(gl.TEXTURE_2D, readTexture);
|
|
152
|
+
var textureLocation = gl.getUniformLocation(program, "uTexture");
|
|
153
|
+
if (textureLocation) {
|
|
154
|
+
gl.uniform1i(textureLocation, 0);
|
|
155
|
+
}
|
|
156
|
+
var resolutionLocation = gl.getUniformLocation(program, "uResolution");
|
|
157
|
+
if (resolutionLocation) {
|
|
158
|
+
gl.uniform2f(resolutionLocation, width, height);
|
|
159
|
+
}
|
|
160
|
+
var timeLocation = gl.getUniformLocation(program, "uTime");
|
|
161
|
+
if (timeLocation) {
|
|
162
|
+
gl.uniform1f(timeLocation, active.progress);
|
|
163
|
+
}
|
|
164
|
+
var intensityLocation = gl.getUniformLocation(program, "uIntensity");
|
|
165
|
+
if (intensityLocation) {
|
|
166
|
+
gl.uniform1f(intensityLocation, active.intensity);
|
|
167
|
+
}
|
|
168
|
+
gl.clearColor(0, 0, 0, 0);
|
|
169
|
+
gl.clear(gl.COLOR_BUFFER_BIT);
|
|
170
|
+
gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
|
|
171
|
+
readTexture = targetTex;
|
|
172
|
+
writeIndex = (writeIndex + 1) % textures.length;
|
|
173
|
+
});
|
|
174
|
+
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
|
|
175
|
+
return readTexture;
|
|
176
|
+
}
|
|
177
|
+
(function(exports$1) {
|
|
178
|
+
Object.defineProperty(exports$1, "__esModule", { value: true });
|
|
179
|
+
exports$1.applyEffects = exports$1.createEffectContext = void 0;
|
|
180
|
+
var runtime_1 = runtime;
|
|
181
|
+
Object.defineProperty(exports$1, "createEffectContext", { enumerable: true, get: function() {
|
|
182
|
+
return runtime_1.createEffectContext;
|
|
183
|
+
} });
|
|
184
|
+
Object.defineProperty(exports$1, "applyEffects", { enumerable: true, get: function() {
|
|
185
|
+
return runtime_1.applyEffects;
|
|
186
|
+
} });
|
|
187
|
+
})(dist);
|
|
5
188
|
const parseNumber = (color, len) => {
|
|
6
189
|
if (typeof color !== "number") return;
|
|
7
190
|
if (len === 3) {
|
|
@@ -6065,6 +6248,15 @@ var TwickPlayer = class extends HTMLElement {
|
|
|
6065
6248
|
__publicField(this, "_looping", true);
|
|
6066
6249
|
__publicField(this, "_volume", 1);
|
|
6067
6250
|
__publicField(this, "volumeChangeRequested", true);
|
|
6251
|
+
/** WebGL canvas and context for applying effects to the live preview. */
|
|
6252
|
+
__publicField(this, "effectGlCanvas", null);
|
|
6253
|
+
__publicField(this, "effectContext", null);
|
|
6254
|
+
__publicField(this, "effectReadbackFbo", null);
|
|
6255
|
+
/**
|
|
6256
|
+
* Optional resolver for active effects at a given time. Set by the host (e.g. twick) so
|
|
6257
|
+
* effect logic lives outside twick-base. When set, used for live preview effects.
|
|
6258
|
+
*/
|
|
6259
|
+
__publicField(this, "getActiveEffectsForTime");
|
|
6068
6260
|
/**
|
|
6069
6261
|
* Triggered by the timeline.
|
|
6070
6262
|
*/
|
|
@@ -6112,6 +6304,7 @@ var TwickPlayer = class extends HTMLElement {
|
|
|
6112
6304
|
this.player.playback.currentScene,
|
|
6113
6305
|
this.player.playback.previousScene
|
|
6114
6306
|
);
|
|
6307
|
+
this.applyEffectsToFinalBuffer();
|
|
6115
6308
|
this.dispatchEvent(new CustomEvent("timeupdate", { detail: this.time }));
|
|
6116
6309
|
const durationInFrames = this.player.playback.duration;
|
|
6117
6310
|
if (durationInFrames === this.duration) {
|
|
@@ -6309,6 +6502,85 @@ var TwickPlayer = class extends HTMLElement {
|
|
|
6309
6502
|
this.addEventListener("seekto", this.handleSeekTo);
|
|
6310
6503
|
this.addEventListener("volumechange", this.handleVolumeChange);
|
|
6311
6504
|
}
|
|
6505
|
+
/**
|
|
6506
|
+
* Resolve active effects for the given time. Uses host-provided callback when set.
|
|
6507
|
+
*/
|
|
6508
|
+
resolveActiveEffectsForTime(timeInSec) {
|
|
6509
|
+
var _a;
|
|
6510
|
+
if (this.getActiveEffectsForTime) {
|
|
6511
|
+
const fps = ((_a = this.player) == null ? void 0 : _a.playback.fps) ?? 30;
|
|
6512
|
+
return this.getActiveEffectsForTime(this.variables, timeInSec, fps);
|
|
6513
|
+
}
|
|
6514
|
+
return [];
|
|
6515
|
+
}
|
|
6516
|
+
/**
|
|
6517
|
+
* Apply GL effects to the current frame and draw the result back to the stage canvas.
|
|
6518
|
+
*/
|
|
6519
|
+
applyEffectsToFinalBuffer() {
|
|
6520
|
+
const canvas = this.stage.finalBuffer;
|
|
6521
|
+
const w = canvas.width;
|
|
6522
|
+
const h = canvas.height;
|
|
6523
|
+
if (w <= 0 || h <= 0) return;
|
|
6524
|
+
const activeEffects = this.resolveActiveEffectsForTime(this.time);
|
|
6525
|
+
if (activeEffects.length === 0) return;
|
|
6526
|
+
if (!this.effectGlCanvas) {
|
|
6527
|
+
this.effectGlCanvas = typeof OffscreenCanvas !== "undefined" ? new OffscreenCanvas(w, h) : document.createElement("canvas");
|
|
6528
|
+
this.effectGlCanvas.width = w;
|
|
6529
|
+
this.effectGlCanvas.height = h;
|
|
6530
|
+
}
|
|
6531
|
+
const glCanvas = this.effectGlCanvas;
|
|
6532
|
+
if (glCanvas.width !== w || glCanvas.height !== h) {
|
|
6533
|
+
glCanvas.width = w;
|
|
6534
|
+
glCanvas.height = h;
|
|
6535
|
+
}
|
|
6536
|
+
if (!this.effectContext) {
|
|
6537
|
+
this.effectContext = dist.createEffectContext(glCanvas);
|
|
6538
|
+
}
|
|
6539
|
+
const gl = this.effectContext.gl;
|
|
6540
|
+
const sourceTexture = gl.createTexture();
|
|
6541
|
+
if (!sourceTexture) return;
|
|
6542
|
+
gl.bindTexture(gl.TEXTURE_2D, sourceTexture);
|
|
6543
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
|
|
6544
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
|
|
6545
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
|
|
6546
|
+
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
|
|
6547
|
+
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true);
|
|
6548
|
+
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, canvas);
|
|
6549
|
+
const resultTexture = dist.applyEffects({
|
|
6550
|
+
ctx: this.effectContext,
|
|
6551
|
+
sourceTexture,
|
|
6552
|
+
width: w,
|
|
6553
|
+
height: h,
|
|
6554
|
+
effects: activeEffects
|
|
6555
|
+
});
|
|
6556
|
+
gl.deleteTexture(sourceTexture);
|
|
6557
|
+
if (!this.effectReadbackFbo) {
|
|
6558
|
+
this.effectReadbackFbo = gl.createFramebuffer();
|
|
6559
|
+
}
|
|
6560
|
+
gl.bindFramebuffer(gl.FRAMEBUFFER, this.effectReadbackFbo);
|
|
6561
|
+
gl.framebufferTexture2D(
|
|
6562
|
+
gl.FRAMEBUFFER,
|
|
6563
|
+
gl.COLOR_ATTACHMENT0,
|
|
6564
|
+
gl.TEXTURE_2D,
|
|
6565
|
+
resultTexture,
|
|
6566
|
+
0
|
|
6567
|
+
);
|
|
6568
|
+
const pixels = new Uint8Array(w * h * 4);
|
|
6569
|
+
gl.readPixels(0, 0, w, h, gl.RGBA, gl.UNSIGNED_BYTE, pixels);
|
|
6570
|
+
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
|
|
6571
|
+
const ctx2d = canvas.getContext("2d");
|
|
6572
|
+
if (ctx2d) {
|
|
6573
|
+
const imageData = ctx2d.createImageData(w, h);
|
|
6574
|
+
const rowBytes = w * 4;
|
|
6575
|
+
for (let y = 0; y < h; y++) {
|
|
6576
|
+
imageData.data.set(
|
|
6577
|
+
pixels.subarray((h - 1 - y) * rowBytes, (h - y) * rowBytes),
|
|
6578
|
+
y * rowBytes
|
|
6579
|
+
);
|
|
6580
|
+
}
|
|
6581
|
+
ctx2d.putImageData(imageData, 0, 0);
|
|
6582
|
+
}
|
|
6583
|
+
}
|
|
6312
6584
|
updateSettings() {
|
|
6313
6585
|
var _a;
|
|
6314
6586
|
if (!this.defaultSettings) {
|
|
@@ -6328,4 +6600,4 @@ var TwickPlayer = class extends HTMLElement {
|
|
|
6328
6600
|
if (!customElements.get(ID)) {
|
|
6329
6601
|
customElements.define(ID, TwickPlayer);
|
|
6330
6602
|
}
|
|
6331
|
-
//# sourceMappingURL=internal-
|
|
6603
|
+
//# sourceMappingURL=internal-B6KRB6EV-BvBtLd70.mjs.map
|