@vunk/plus 1.3.11 → 1.3.12

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.
Files changed (43) hide show
  1. package/components/SharedSystems.mjs +3195 -0
  2. package/components/WebGLRenderer.mjs +3617 -0
  3. package/components/WebGPURenderer.mjs +2103 -0
  4. package/components/_commonjsHelpers.mjs +7 -0
  5. package/components/agent-chat-provider/src/types.d.ts +2 -1
  6. package/components/broadcasting-markdown/index.d.ts +2 -0
  7. package/components/broadcasting-markdown/index.mjs +238 -306
  8. package/components/broadcasting-markdown/src/const.d.ts +2 -7
  9. package/components/broadcasting-markdown/src/ctx.d.ts +28 -15
  10. package/components/broadcasting-markdown/src/howler-speech.vue.d.ts +24 -44
  11. package/components/broadcasting-markdown/src/index.vue.d.ts +31 -167
  12. package/components/broadcasting-markdown/src/paragraph.vue.d.ts +1 -14
  13. package/components/broadcasting-markdown/src/types.d.ts +10 -2
  14. package/components/broadcasting-markdown/src/use.d.ts +18 -0
  15. package/components/browserAll.mjs +3187 -0
  16. package/components/bubble-list/index.mjs +2 -1
  17. package/components/bubble-list/src/index.vue.d.ts +27 -161
  18. package/components/bubble-templates/src/index.vue.d.ts +27 -161
  19. package/components/bubble-templates/src/types.d.ts +6 -0
  20. package/components/chat-independent/index.mjs +3 -0
  21. package/components/chat-independent/src/index.vue.d.ts +27 -161
  22. package/components/colorToUniform.mjs +266 -0
  23. package/components/index2.mjs +21227 -0
  24. package/components/init.mjs +3779 -0
  25. package/components/pixi-frame/index.css +4 -0
  26. package/components/pixi-frame/index.d.ts +7 -0
  27. package/components/pixi-frame/index.mjs +6 -0
  28. package/components/pixi-frame/src/app.vue.d.ts +29 -0
  29. package/components/pixi-frame/src/core.vue.d.ts +122 -0
  30. package/components/pixi-frame/src/ctx.d.ts +40 -0
  31. package/components/pixi-frame/src/index.vue.d.ts +218 -0
  32. package/components/pixi-frame/src/types.d.ts +1 -0
  33. package/components/pixi-frame/src/use.d.ts +3 -0
  34. package/components/tables-v1/src/ctx.d.ts +1 -1
  35. package/components/tables-v1/src/el-ctx.d.ts +1 -1
  36. package/components/tables-v1-checkbox/src/ctx.d.ts +1 -1
  37. package/components/tables-v1-checkbox/src/index.vue.d.ts +1 -1
  38. package/components/webworkerAll.mjs +8 -0
  39. package/index.css +5 -4
  40. package/package.json +8 -4
  41. package/components/broadcasting-markdown/index.css +0 -3
  42. package/components/broadcasting-markdown/src/custom-speech.vue.d.ts +0 -117
  43. package/components/broadcasting-markdown/src/web-speech.vue.d.ts +0 -110
@@ -0,0 +1,3195 @@
1
+ import { s as Shader, G as GpuProgram, u as GlProgram, v as TextureMatrix, k as UniformGroup, M as Matrix, x as DefaultBatcher, E as ExtensionType, e as extensions, y as BigPool, z as getGlobalBounds, F as Bounds, H as TexturePool, R as RendererType, I as FilterEffect, J as Sprite, t as Texture, S as STENCIL_MODES, h as CLEAR, w as warn, K as getAttributeInfoFromFormat, L as unsafeEvalSupported, b as Buffer, B as BufferUsage, i as CanvasSource, N as uid$1, j as TextureSource, O as Rectangle, Q as SystemRunner, a as EventEmitter, V as multiplyColors, W as UPDATE_VISIBLE, X as UPDATE_COLOR, Y as UPDATE_BLEND, C as Container, Z as Color, _ as getLocalBounds, P as Point, c as BindGroup, T as Ticker, D as DOMAdapter, $ as VERSION, a0 as deprecation, a1 as v8_0_0, a2 as RendererInitHook } from './index2.mjs';
2
+ import { S as State, B as BatchableSprite, c as color32BitToUniform } from './colorToUniform.mjs';
3
+
4
+ const _Filter = class _Filter extends Shader {
5
+ /**
6
+ * @param options - The optional parameters of this filter.
7
+ */
8
+ constructor(options) {
9
+ options = { ..._Filter.defaultOptions, ...options };
10
+ super(options);
11
+ /** If enabled is true the filter is applied, if false it will not. */
12
+ this.enabled = true;
13
+ /**
14
+ * The gpu state the filter requires to render.
15
+ * @internal
16
+ * @ignore
17
+ */
18
+ this._state = State.for2d();
19
+ this.blendMode = options.blendMode;
20
+ this.padding = options.padding;
21
+ if (typeof options.antialias === "boolean") {
22
+ this.antialias = options.antialias ? "on" : "off";
23
+ } else {
24
+ this.antialias = options.antialias;
25
+ }
26
+ this.resolution = options.resolution;
27
+ this.blendRequired = options.blendRequired;
28
+ this.clipToViewport = options.clipToViewport;
29
+ this.addResource("uTexture", 0, 1);
30
+ }
31
+ /**
32
+ * Applies the filter
33
+ * @param filterManager - The renderer to retrieve the filter from
34
+ * @param input - The input render target.
35
+ * @param output - The target to output to.
36
+ * @param clearMode - Should the output be cleared before rendering to it
37
+ */
38
+ apply(filterManager, input, output, clearMode) {
39
+ filterManager.applyFilter(this, input, output, clearMode);
40
+ }
41
+ /**
42
+ * Get the blend mode of the filter.
43
+ * @default "normal"
44
+ */
45
+ get blendMode() {
46
+ return this._state.blendMode;
47
+ }
48
+ /** Sets the blend mode of the filter. */
49
+ set blendMode(value) {
50
+ this._state.blendMode = value;
51
+ }
52
+ /**
53
+ * A short hand function to create a filter based of a vertex and fragment shader src.
54
+ * @param options
55
+ * @returns A shiny new PixiJS filter!
56
+ */
57
+ static from(options) {
58
+ const { gpu, gl, ...rest } = options;
59
+ let gpuProgram;
60
+ let glProgram;
61
+ if (gpu) {
62
+ gpuProgram = GpuProgram.from(gpu);
63
+ }
64
+ if (gl) {
65
+ glProgram = GlProgram.from(gl);
66
+ }
67
+ return new _Filter({
68
+ gpuProgram,
69
+ glProgram,
70
+ ...rest
71
+ });
72
+ }
73
+ };
74
+ /**
75
+ * The default filter settings
76
+ * @static
77
+ */
78
+ _Filter.defaultOptions = {
79
+ blendMode: "normal",
80
+ resolution: 1,
81
+ padding: 0,
82
+ antialias: "off",
83
+ blendRequired: false,
84
+ clipToViewport: true
85
+ };
86
+ let Filter = _Filter;
87
+
88
+ var fragment = "in vec2 vMaskCoord;\nin vec2 vTextureCoord;\n\nuniform sampler2D uTexture;\nuniform sampler2D uMaskTexture;\n\nuniform float uAlpha;\nuniform vec4 uMaskClamp;\nuniform float uInverse;\n\nout vec4 finalColor;\n\nvoid main(void)\n{\n float clip = step(3.5,\n step(uMaskClamp.x, vMaskCoord.x) +\n step(uMaskClamp.y, vMaskCoord.y) +\n step(vMaskCoord.x, uMaskClamp.z) +\n step(vMaskCoord.y, uMaskClamp.w));\n\n // TODO look into why this is needed\n float npmAlpha = uAlpha;\n vec4 original = texture(uTexture, vTextureCoord);\n vec4 masky = texture(uMaskTexture, vMaskCoord);\n float alphaMul = 1.0 - npmAlpha * (1.0 - masky.a);\n\n float a = alphaMul * masky.r * npmAlpha * clip;\n\n if (uInverse == 1.0) {\n a = 1.0 - a;\n }\n\n finalColor = original * a;\n}\n";
89
+
90
+ var vertex = "in vec2 aPosition;\n\nout vec2 vTextureCoord;\nout vec2 vMaskCoord;\n\n\nuniform vec4 uInputSize;\nuniform vec4 uOutputFrame;\nuniform vec4 uOutputTexture;\nuniform mat3 uFilterMatrix;\n\nvec4 filterVertexPosition( vec2 aPosition )\n{\n vec2 position = aPosition * uOutputFrame.zw + uOutputFrame.xy;\n \n position.x = position.x * (2.0 / uOutputTexture.x) - 1.0;\n position.y = position.y * (2.0*uOutputTexture.z / uOutputTexture.y) - uOutputTexture.z;\n\n return vec4(position, 0.0, 1.0);\n}\n\nvec2 filterTextureCoord( vec2 aPosition )\n{\n return aPosition * (uOutputFrame.zw * uInputSize.zw);\n}\n\nvec2 getFilterCoord( vec2 aPosition )\n{\n return ( uFilterMatrix * vec3( filterTextureCoord(aPosition), 1.0) ).xy;\n} \n\nvoid main(void)\n{\n gl_Position = filterVertexPosition(aPosition);\n vTextureCoord = filterTextureCoord(aPosition);\n vMaskCoord = getFilterCoord(aPosition);\n}\n";
91
+
92
+ var source = "struct GlobalFilterUniforms {\n uInputSize:vec4<f32>,\n uInputPixel:vec4<f32>,\n uInputClamp:vec4<f32>,\n uOutputFrame:vec4<f32>,\n uGlobalFrame:vec4<f32>,\n uOutputTexture:vec4<f32>,\n};\n\nstruct MaskUniforms {\n uFilterMatrix:mat3x3<f32>,\n uMaskClamp:vec4<f32>,\n uAlpha:f32,\n uInverse:f32,\n};\n\n@group(0) @binding(0) var<uniform> gfu: GlobalFilterUniforms;\n@group(0) @binding(1) var uTexture: texture_2d<f32>;\n@group(0) @binding(2) var uSampler : sampler;\n\n@group(1) @binding(0) var<uniform> filterUniforms : MaskUniforms;\n@group(1) @binding(1) var uMaskTexture: texture_2d<f32>;\n\nstruct VSOutput {\n @builtin(position) position: vec4<f32>,\n @location(0) uv : vec2<f32>,\n @location(1) filterUv : vec2<f32>,\n};\n\nfn filterVertexPosition(aPosition:vec2<f32>) -> vec4<f32>\n{\n var position = aPosition * gfu.uOutputFrame.zw + gfu.uOutputFrame.xy;\n\n position.x = position.x * (2.0 / gfu.uOutputTexture.x) - 1.0;\n position.y = position.y * (2.0*gfu.uOutputTexture.z / gfu.uOutputTexture.y) - gfu.uOutputTexture.z;\n\n return vec4(position, 0.0, 1.0);\n}\n\nfn filterTextureCoord( aPosition:vec2<f32> ) -> vec2<f32>\n{\n return aPosition * (gfu.uOutputFrame.zw * gfu.uInputSize.zw);\n}\n\nfn globalTextureCoord( aPosition:vec2<f32> ) -> vec2<f32>\n{\n return (aPosition.xy / gfu.uGlobalFrame.zw) + (gfu.uGlobalFrame.xy / gfu.uGlobalFrame.zw);\n}\n\nfn getFilterCoord(aPosition:vec2<f32> ) -> vec2<f32>\n{\n return ( filterUniforms.uFilterMatrix * vec3( filterTextureCoord(aPosition), 1.0) ).xy;\n}\n\nfn getSize() -> vec2<f32>\n{\n return gfu.uGlobalFrame.zw;\n}\n\n@vertex\nfn mainVertex(\n @location(0) aPosition : vec2<f32>,\n) -> VSOutput {\n return VSOutput(\n filterVertexPosition(aPosition),\n filterTextureCoord(aPosition),\n getFilterCoord(aPosition)\n );\n}\n\n@fragment\nfn mainFragment(\n @location(0) uv: vec2<f32>,\n @location(1) filterUv: vec2<f32>,\n @builtin(position) position: vec4<f32>\n) -> @location(0) vec4<f32> {\n\n var maskClamp = filterUniforms.uMaskClamp;\n var uAlpha = filterUniforms.uAlpha;\n\n var clip = step(3.5,\n step(maskClamp.x, filterUv.x) +\n step(maskClamp.y, filterUv.y) +\n step(filterUv.x, maskClamp.z) +\n step(filterUv.y, maskClamp.w));\n\n var mask = textureSample(uMaskTexture, uSampler, filterUv);\n var source = textureSample(uTexture, uSampler, uv);\n var alphaMul = 1.0 - uAlpha * (1.0 - mask.a);\n\n var a: f32 = alphaMul * mask.r * uAlpha * clip;\n\n if (filterUniforms.uInverse == 1.0) {\n a = 1.0 - a;\n }\n\n return source * a;\n}\n";
93
+
94
+ class MaskFilter extends Filter {
95
+ constructor(options) {
96
+ const { sprite, ...rest } = options;
97
+ const textureMatrix = new TextureMatrix(sprite.texture);
98
+ const filterUniforms = new UniformGroup({
99
+ uFilterMatrix: { value: new Matrix(), type: "mat3x3<f32>" },
100
+ uMaskClamp: { value: textureMatrix.uClampFrame, type: "vec4<f32>" },
101
+ uAlpha: { value: 1, type: "f32" },
102
+ uInverse: { value: options.inverse ? 1 : 0, type: "f32" }
103
+ });
104
+ const gpuProgram = GpuProgram.from({
105
+ vertex: {
106
+ source,
107
+ entryPoint: "mainVertex"
108
+ },
109
+ fragment: {
110
+ source,
111
+ entryPoint: "mainFragment"
112
+ }
113
+ });
114
+ const glProgram = GlProgram.from({
115
+ vertex,
116
+ fragment,
117
+ name: "mask-filter"
118
+ });
119
+ super({
120
+ ...rest,
121
+ gpuProgram,
122
+ glProgram,
123
+ resources: {
124
+ filterUniforms,
125
+ uMaskTexture: sprite.texture.source
126
+ }
127
+ });
128
+ this.sprite = sprite;
129
+ this._textureMatrix = textureMatrix;
130
+ }
131
+ set inverse(value) {
132
+ this.resources.filterUniforms.uniforms.uInverse = value ? 1 : 0;
133
+ }
134
+ get inverse() {
135
+ return this.resources.filterUniforms.uniforms.uInverse === 1;
136
+ }
137
+ apply(filterManager, input, output, clearMode) {
138
+ this._textureMatrix.texture = this.sprite.texture;
139
+ filterManager.calculateSpriteMatrix(
140
+ this.resources.filterUniforms.uniforms.uFilterMatrix,
141
+ this.sprite
142
+ ).prepend(this._textureMatrix.mapCoord);
143
+ this.resources.uMaskTexture = this.sprite.texture.source;
144
+ filterManager.applyFilter(this, input, output, clearMode);
145
+ }
146
+ }
147
+
148
+ const _BatcherPipe = class _BatcherPipe {
149
+ constructor(renderer, adaptor) {
150
+ this.state = State.for2d();
151
+ this._batchersByInstructionSet = /* @__PURE__ */ Object.create(null);
152
+ /** A record of all active batchers, keyed by their names */
153
+ this._activeBatches = /* @__PURE__ */ Object.create(null);
154
+ this.renderer = renderer;
155
+ this._adaptor = adaptor;
156
+ this._adaptor.init?.(this);
157
+ }
158
+ static getBatcher(name) {
159
+ return new this._availableBatchers[name]();
160
+ }
161
+ buildStart(instructionSet) {
162
+ let batchers = this._batchersByInstructionSet[instructionSet.uid];
163
+ if (!batchers) {
164
+ batchers = this._batchersByInstructionSet[instructionSet.uid] = /* @__PURE__ */ Object.create(null);
165
+ batchers.default || (batchers.default = new DefaultBatcher());
166
+ }
167
+ this._activeBatches = batchers;
168
+ this._activeBatch = this._activeBatches.default;
169
+ for (const i in this._activeBatches) {
170
+ this._activeBatches[i].begin();
171
+ }
172
+ }
173
+ addToBatch(batchableObject, instructionSet) {
174
+ if (this._activeBatch.name !== batchableObject.batcherName) {
175
+ this._activeBatch.break(instructionSet);
176
+ let batch = this._activeBatches[batchableObject.batcherName];
177
+ if (!batch) {
178
+ batch = this._activeBatches[batchableObject.batcherName] = _BatcherPipe.getBatcher(batchableObject.batcherName);
179
+ batch.begin();
180
+ }
181
+ this._activeBatch = batch;
182
+ }
183
+ this._activeBatch.add(batchableObject);
184
+ }
185
+ break(instructionSet) {
186
+ this._activeBatch.break(instructionSet);
187
+ }
188
+ buildEnd(instructionSet) {
189
+ this._activeBatch.break(instructionSet);
190
+ const batches = this._activeBatches;
191
+ for (const i in batches) {
192
+ const batch = batches[i];
193
+ const geometry = batch.geometry;
194
+ geometry.indexBuffer.setDataWithSize(batch.indexBuffer, batch.indexSize, true);
195
+ geometry.buffers[0].setDataWithSize(batch.attributeBuffer.float32View, batch.attributeSize, false);
196
+ }
197
+ }
198
+ upload(instructionSet) {
199
+ const batchers = this._batchersByInstructionSet[instructionSet.uid];
200
+ for (const i in batchers) {
201
+ const batcher = batchers[i];
202
+ const geometry = batcher.geometry;
203
+ if (batcher.dirty) {
204
+ batcher.dirty = false;
205
+ geometry.buffers[0].update(batcher.attributeSize * 4);
206
+ }
207
+ }
208
+ }
209
+ execute(batch) {
210
+ if (batch.action === "startBatch") {
211
+ const batcher = batch.batcher;
212
+ const geometry = batcher.geometry;
213
+ const shader = batcher.shader;
214
+ this._adaptor.start(this, geometry, shader);
215
+ }
216
+ this._adaptor.execute(this, batch);
217
+ }
218
+ destroy() {
219
+ this.state = null;
220
+ this.renderer = null;
221
+ this._adaptor = null;
222
+ for (const i in this._activeBatches) {
223
+ this._activeBatches[i].destroy();
224
+ }
225
+ this._activeBatches = null;
226
+ }
227
+ };
228
+ /** @ignore */
229
+ _BatcherPipe.extension = {
230
+ type: [
231
+ ExtensionType.WebGLPipes,
232
+ ExtensionType.WebGPUPipes,
233
+ ExtensionType.CanvasPipes
234
+ ],
235
+ name: "batch"
236
+ };
237
+ _BatcherPipe._availableBatchers = /* @__PURE__ */ Object.create(null);
238
+ let BatcherPipe = _BatcherPipe;
239
+ extensions.handleByMap(ExtensionType.Batcher, BatcherPipe._availableBatchers);
240
+ extensions.add(DefaultBatcher);
241
+
242
+ const textureBit = {
243
+ name: "texture-bit",
244
+ vertex: {
245
+ header: (
246
+ /* wgsl */
247
+ `
248
+
249
+ struct TextureUniforms {
250
+ uTextureMatrix:mat3x3<f32>,
251
+ }
252
+
253
+ @group(2) @binding(2) var<uniform> textureUniforms : TextureUniforms;
254
+ `
255
+ ),
256
+ main: (
257
+ /* wgsl */
258
+ `
259
+ uv = (textureUniforms.uTextureMatrix * vec3(uv, 1.0)).xy;
260
+ `
261
+ )
262
+ },
263
+ fragment: {
264
+ header: (
265
+ /* wgsl */
266
+ `
267
+ @group(2) @binding(0) var uTexture: texture_2d<f32>;
268
+ @group(2) @binding(1) var uSampler: sampler;
269
+
270
+
271
+ `
272
+ ),
273
+ main: (
274
+ /* wgsl */
275
+ `
276
+ outColor = textureSample(uTexture, uSampler, vUV);
277
+ `
278
+ )
279
+ }
280
+ };
281
+ const textureBitGl = {
282
+ name: "texture-bit",
283
+ vertex: {
284
+ header: (
285
+ /* glsl */
286
+ `
287
+ uniform mat3 uTextureMatrix;
288
+ `
289
+ ),
290
+ main: (
291
+ /* glsl */
292
+ `
293
+ uv = (uTextureMatrix * vec3(uv, 1.0)).xy;
294
+ `
295
+ )
296
+ },
297
+ fragment: {
298
+ header: (
299
+ /* glsl */
300
+ `
301
+ uniform sampler2D uTexture;
302
+
303
+
304
+ `
305
+ ),
306
+ main: (
307
+ /* glsl */
308
+ `
309
+ outColor = texture(uTexture, vUV);
310
+ `
311
+ )
312
+ }
313
+ };
314
+
315
+ const tempBounds$1 = new Bounds();
316
+ class AlphaMaskEffect extends FilterEffect {
317
+ constructor() {
318
+ super();
319
+ this.filters = [new MaskFilter({
320
+ sprite: new Sprite(Texture.EMPTY),
321
+ inverse: false,
322
+ resolution: "inherit",
323
+ antialias: "inherit"
324
+ })];
325
+ }
326
+ get sprite() {
327
+ return this.filters[0].sprite;
328
+ }
329
+ set sprite(value) {
330
+ this.filters[0].sprite = value;
331
+ }
332
+ get inverse() {
333
+ return this.filters[0].inverse;
334
+ }
335
+ set inverse(value) {
336
+ this.filters[0].inverse = value;
337
+ }
338
+ }
339
+ class AlphaMaskPipe {
340
+ constructor(renderer) {
341
+ this._activeMaskStage = [];
342
+ this._renderer = renderer;
343
+ }
344
+ push(mask, maskedContainer, instructionSet) {
345
+ const renderer = this._renderer;
346
+ renderer.renderPipes.batch.break(instructionSet);
347
+ instructionSet.add({
348
+ renderPipeId: "alphaMask",
349
+ action: "pushMaskBegin",
350
+ mask,
351
+ inverse: maskedContainer._maskOptions.inverse,
352
+ canBundle: false,
353
+ maskedContainer
354
+ });
355
+ mask.inverse = maskedContainer._maskOptions.inverse;
356
+ if (mask.renderMaskToTexture) {
357
+ const maskContainer = mask.mask;
358
+ maskContainer.includeInBuild = true;
359
+ maskContainer.collectRenderables(
360
+ instructionSet,
361
+ renderer,
362
+ null
363
+ );
364
+ maskContainer.includeInBuild = false;
365
+ }
366
+ renderer.renderPipes.batch.break(instructionSet);
367
+ instructionSet.add({
368
+ renderPipeId: "alphaMask",
369
+ action: "pushMaskEnd",
370
+ mask,
371
+ maskedContainer,
372
+ inverse: maskedContainer._maskOptions.inverse,
373
+ canBundle: false
374
+ });
375
+ }
376
+ pop(mask, _maskedContainer, instructionSet) {
377
+ const renderer = this._renderer;
378
+ renderer.renderPipes.batch.break(instructionSet);
379
+ instructionSet.add({
380
+ renderPipeId: "alphaMask",
381
+ action: "popMaskEnd",
382
+ mask,
383
+ inverse: _maskedContainer._maskOptions.inverse,
384
+ canBundle: false
385
+ });
386
+ }
387
+ execute(instruction) {
388
+ const renderer = this._renderer;
389
+ const renderMask = instruction.mask.renderMaskToTexture;
390
+ if (instruction.action === "pushMaskBegin") {
391
+ const filterEffect = BigPool.get(AlphaMaskEffect);
392
+ filterEffect.inverse = instruction.inverse;
393
+ if (renderMask) {
394
+ instruction.mask.mask.measurable = true;
395
+ const bounds = getGlobalBounds(instruction.mask.mask, true, tempBounds$1);
396
+ instruction.mask.mask.measurable = false;
397
+ bounds.ceil();
398
+ const colorTextureSource = renderer.renderTarget.renderTarget.colorTexture.source;
399
+ const filterTexture = TexturePool.getOptimalTexture(
400
+ bounds.width,
401
+ bounds.height,
402
+ colorTextureSource._resolution,
403
+ colorTextureSource.antialias
404
+ );
405
+ renderer.renderTarget.push(filterTexture, true);
406
+ renderer.globalUniforms.push({
407
+ offset: bounds,
408
+ worldColor: 4294967295
409
+ });
410
+ const sprite = filterEffect.sprite;
411
+ sprite.texture = filterTexture;
412
+ sprite.worldTransform.tx = bounds.minX;
413
+ sprite.worldTransform.ty = bounds.minY;
414
+ this._activeMaskStage.push({
415
+ filterEffect,
416
+ maskedContainer: instruction.maskedContainer,
417
+ filterTexture
418
+ });
419
+ } else {
420
+ filterEffect.sprite = instruction.mask.mask;
421
+ this._activeMaskStage.push({
422
+ filterEffect,
423
+ maskedContainer: instruction.maskedContainer
424
+ });
425
+ }
426
+ } else if (instruction.action === "pushMaskEnd") {
427
+ const maskData = this._activeMaskStage[this._activeMaskStage.length - 1];
428
+ if (renderMask) {
429
+ if (renderer.type === RendererType.WEBGL) {
430
+ renderer.renderTarget.finishRenderPass();
431
+ }
432
+ renderer.renderTarget.pop();
433
+ renderer.globalUniforms.pop();
434
+ }
435
+ renderer.filter.push({
436
+ renderPipeId: "filter",
437
+ action: "pushFilter",
438
+ container: maskData.maskedContainer,
439
+ filterEffect: maskData.filterEffect,
440
+ canBundle: false
441
+ });
442
+ } else if (instruction.action === "popMaskEnd") {
443
+ renderer.filter.pop();
444
+ const maskData = this._activeMaskStage.pop();
445
+ if (renderMask) {
446
+ TexturePool.returnTexture(maskData.filterTexture);
447
+ }
448
+ BigPool.return(maskData.filterEffect);
449
+ }
450
+ }
451
+ destroy() {
452
+ this._renderer = null;
453
+ this._activeMaskStage = null;
454
+ }
455
+ }
456
+ /** @ignore */
457
+ AlphaMaskPipe.extension = {
458
+ type: [
459
+ ExtensionType.WebGLPipes,
460
+ ExtensionType.WebGPUPipes,
461
+ ExtensionType.CanvasPipes
462
+ ],
463
+ name: "alphaMask"
464
+ };
465
+
466
+ class ColorMaskPipe {
467
+ constructor(renderer) {
468
+ this._colorStack = [];
469
+ this._colorStackIndex = 0;
470
+ this._currentColor = 0;
471
+ this._renderer = renderer;
472
+ }
473
+ buildStart() {
474
+ this._colorStack[0] = 15;
475
+ this._colorStackIndex = 1;
476
+ this._currentColor = 15;
477
+ }
478
+ push(mask, _container, instructionSet) {
479
+ const renderer = this._renderer;
480
+ renderer.renderPipes.batch.break(instructionSet);
481
+ const colorStack = this._colorStack;
482
+ colorStack[this._colorStackIndex] = colorStack[this._colorStackIndex - 1] & mask.mask;
483
+ const currentColor = this._colorStack[this._colorStackIndex];
484
+ if (currentColor !== this._currentColor) {
485
+ this._currentColor = currentColor;
486
+ instructionSet.add({
487
+ renderPipeId: "colorMask",
488
+ colorMask: currentColor,
489
+ canBundle: false
490
+ });
491
+ }
492
+ this._colorStackIndex++;
493
+ }
494
+ pop(_mask, _container, instructionSet) {
495
+ const renderer = this._renderer;
496
+ renderer.renderPipes.batch.break(instructionSet);
497
+ const colorStack = this._colorStack;
498
+ this._colorStackIndex--;
499
+ const currentColor = colorStack[this._colorStackIndex - 1];
500
+ if (currentColor !== this._currentColor) {
501
+ this._currentColor = currentColor;
502
+ instructionSet.add({
503
+ renderPipeId: "colorMask",
504
+ colorMask: currentColor,
505
+ canBundle: false
506
+ });
507
+ }
508
+ }
509
+ execute(instruction) {
510
+ const renderer = this._renderer;
511
+ renderer.colorMask.setMask(instruction.colorMask);
512
+ }
513
+ destroy() {
514
+ this._colorStack = null;
515
+ }
516
+ }
517
+ /** @ignore */
518
+ ColorMaskPipe.extension = {
519
+ type: [
520
+ ExtensionType.WebGLPipes,
521
+ ExtensionType.WebGPUPipes,
522
+ ExtensionType.CanvasPipes
523
+ ],
524
+ name: "colorMask"
525
+ };
526
+
527
+ class StencilMaskPipe {
528
+ constructor(renderer) {
529
+ // used when building and also when executing..
530
+ this._maskStackHash = {};
531
+ this._maskHash = /* @__PURE__ */ new WeakMap();
532
+ this._renderer = renderer;
533
+ }
534
+ push(mask, _container, instructionSet) {
535
+ var _a;
536
+ const effect = mask;
537
+ const renderer = this._renderer;
538
+ renderer.renderPipes.batch.break(instructionSet);
539
+ renderer.renderPipes.blendMode.setBlendMode(effect.mask, "none", instructionSet);
540
+ instructionSet.add({
541
+ renderPipeId: "stencilMask",
542
+ action: "pushMaskBegin",
543
+ mask,
544
+ inverse: _container._maskOptions.inverse,
545
+ canBundle: false
546
+ });
547
+ const maskContainer = effect.mask;
548
+ maskContainer.includeInBuild = true;
549
+ if (!this._maskHash.has(effect)) {
550
+ this._maskHash.set(effect, {
551
+ instructionsStart: 0,
552
+ instructionsLength: 0
553
+ });
554
+ }
555
+ const maskData = this._maskHash.get(effect);
556
+ maskData.instructionsStart = instructionSet.instructionSize;
557
+ maskContainer.collectRenderables(
558
+ instructionSet,
559
+ renderer,
560
+ null
561
+ );
562
+ maskContainer.includeInBuild = false;
563
+ renderer.renderPipes.batch.break(instructionSet);
564
+ instructionSet.add({
565
+ renderPipeId: "stencilMask",
566
+ action: "pushMaskEnd",
567
+ mask,
568
+ inverse: _container._maskOptions.inverse,
569
+ canBundle: false
570
+ });
571
+ const instructionsLength = instructionSet.instructionSize - maskData.instructionsStart - 1;
572
+ maskData.instructionsLength = instructionsLength;
573
+ const renderTargetUid = renderer.renderTarget.renderTarget.uid;
574
+ (_a = this._maskStackHash)[renderTargetUid] ?? (_a[renderTargetUid] = 0);
575
+ }
576
+ pop(mask, _container, instructionSet) {
577
+ const effect = mask;
578
+ const renderer = this._renderer;
579
+ renderer.renderPipes.batch.break(instructionSet);
580
+ renderer.renderPipes.blendMode.setBlendMode(effect.mask, "none", instructionSet);
581
+ instructionSet.add({
582
+ renderPipeId: "stencilMask",
583
+ action: "popMaskBegin",
584
+ inverse: _container._maskOptions.inverse,
585
+ canBundle: false
586
+ });
587
+ const maskData = this._maskHash.get(mask);
588
+ for (let i = 0; i < maskData.instructionsLength; i++) {
589
+ instructionSet.instructions[instructionSet.instructionSize++] = instructionSet.instructions[maskData.instructionsStart++];
590
+ }
591
+ instructionSet.add({
592
+ renderPipeId: "stencilMask",
593
+ action: "popMaskEnd",
594
+ canBundle: false
595
+ });
596
+ }
597
+ execute(instruction) {
598
+ var _a;
599
+ const renderer = this._renderer;
600
+ const renderTargetUid = renderer.renderTarget.renderTarget.uid;
601
+ let maskStackIndex = (_a = this._maskStackHash)[renderTargetUid] ?? (_a[renderTargetUid] = 0);
602
+ if (instruction.action === "pushMaskBegin") {
603
+ renderer.renderTarget.ensureDepthStencil();
604
+ renderer.stencil.setStencilMode(STENCIL_MODES.RENDERING_MASK_ADD, maskStackIndex);
605
+ maskStackIndex++;
606
+ renderer.colorMask.setMask(0);
607
+ } else if (instruction.action === "pushMaskEnd") {
608
+ if (instruction.inverse) {
609
+ renderer.stencil.setStencilMode(STENCIL_MODES.INVERSE_MASK_ACTIVE, maskStackIndex);
610
+ } else {
611
+ renderer.stencil.setStencilMode(STENCIL_MODES.MASK_ACTIVE, maskStackIndex);
612
+ }
613
+ renderer.colorMask.setMask(15);
614
+ } else if (instruction.action === "popMaskBegin") {
615
+ renderer.colorMask.setMask(0);
616
+ if (maskStackIndex !== 0) {
617
+ renderer.stencil.setStencilMode(STENCIL_MODES.RENDERING_MASK_REMOVE, maskStackIndex);
618
+ } else {
619
+ renderer.renderTarget.clear(null, CLEAR.STENCIL);
620
+ renderer.stencil.setStencilMode(STENCIL_MODES.DISABLED, maskStackIndex);
621
+ }
622
+ maskStackIndex--;
623
+ } else if (instruction.action === "popMaskEnd") {
624
+ if (instruction.inverse) {
625
+ renderer.stencil.setStencilMode(STENCIL_MODES.INVERSE_MASK_ACTIVE, maskStackIndex);
626
+ } else {
627
+ renderer.stencil.setStencilMode(STENCIL_MODES.MASK_ACTIVE, maskStackIndex);
628
+ }
629
+ renderer.colorMask.setMask(15);
630
+ }
631
+ this._maskStackHash[renderTargetUid] = maskStackIndex;
632
+ }
633
+ destroy() {
634
+ this._renderer = null;
635
+ this._maskStackHash = null;
636
+ this._maskHash = null;
637
+ }
638
+ }
639
+ StencilMaskPipe.extension = {
640
+ type: [
641
+ ExtensionType.WebGLPipes,
642
+ ExtensionType.WebGPUPipes,
643
+ ExtensionType.CanvasPipes
644
+ ],
645
+ name: "stencilMask"
646
+ };
647
+
648
+ function ensureAttributes(geometry, extractedData) {
649
+ for (const i in geometry.attributes) {
650
+ const attribute = geometry.attributes[i];
651
+ const attributeData = extractedData[i];
652
+ if (attributeData) {
653
+ attribute.format ?? (attribute.format = attributeData.format);
654
+ attribute.offset ?? (attribute.offset = attributeData.offset);
655
+ attribute.instance ?? (attribute.instance = attributeData.instance);
656
+ } else {
657
+ warn(`Attribute ${i} is not present in the shader, but is present in the geometry. Unable to infer attribute details.`);
658
+ }
659
+ }
660
+ ensureStartAndStride(geometry);
661
+ }
662
+ function ensureStartAndStride(geometry) {
663
+ const { buffers, attributes } = geometry;
664
+ const tempStride = {};
665
+ const tempStart = {};
666
+ for (const j in buffers) {
667
+ const buffer = buffers[j];
668
+ tempStride[buffer.uid] = 0;
669
+ tempStart[buffer.uid] = 0;
670
+ }
671
+ for (const j in attributes) {
672
+ const attribute = attributes[j];
673
+ tempStride[attribute.buffer.uid] += getAttributeInfoFromFormat(attribute.format).stride;
674
+ }
675
+ for (const j in attributes) {
676
+ const attribute = attributes[j];
677
+ attribute.stride ?? (attribute.stride = tempStride[attribute.buffer.uid]);
678
+ attribute.start ?? (attribute.start = tempStart[attribute.buffer.uid]);
679
+ tempStart[attribute.buffer.uid] += getAttributeInfoFromFormat(attribute.format).stride;
680
+ }
681
+ }
682
+
683
+ const GpuStencilModesToPixi = [];
684
+ GpuStencilModesToPixi[STENCIL_MODES.NONE] = void 0;
685
+ GpuStencilModesToPixi[STENCIL_MODES.DISABLED] = {
686
+ stencilWriteMask: 0,
687
+ stencilReadMask: 0
688
+ };
689
+ GpuStencilModesToPixi[STENCIL_MODES.RENDERING_MASK_ADD] = {
690
+ stencilFront: {
691
+ compare: "equal",
692
+ passOp: "increment-clamp"
693
+ },
694
+ stencilBack: {
695
+ compare: "equal",
696
+ passOp: "increment-clamp"
697
+ }
698
+ };
699
+ GpuStencilModesToPixi[STENCIL_MODES.RENDERING_MASK_REMOVE] = {
700
+ stencilFront: {
701
+ compare: "equal",
702
+ passOp: "decrement-clamp"
703
+ },
704
+ stencilBack: {
705
+ compare: "equal",
706
+ passOp: "decrement-clamp"
707
+ }
708
+ };
709
+ GpuStencilModesToPixi[STENCIL_MODES.MASK_ACTIVE] = {
710
+ stencilWriteMask: 0,
711
+ stencilFront: {
712
+ compare: "equal",
713
+ passOp: "keep"
714
+ },
715
+ stencilBack: {
716
+ compare: "equal",
717
+ passOp: "keep"
718
+ }
719
+ };
720
+ GpuStencilModesToPixi[STENCIL_MODES.INVERSE_MASK_ACTIVE] = {
721
+ stencilWriteMask: 0,
722
+ stencilFront: {
723
+ compare: "not-equal",
724
+ passOp: "replace"
725
+ },
726
+ stencilBack: {
727
+ compare: "not-equal",
728
+ passOp: "replace"
729
+ }
730
+ };
731
+
732
+ class UboSystem {
733
+ constructor(adaptor) {
734
+ /** Cache of uniform buffer layouts and sync functions, so we don't have to re-create them */
735
+ this._syncFunctionHash = /* @__PURE__ */ Object.create(null);
736
+ this._adaptor = adaptor;
737
+ this._systemCheck();
738
+ }
739
+ /**
740
+ * Overridable function by `pixi.js/unsafe-eval` to silence
741
+ * throwing an error if platform doesn't support unsafe-evals.
742
+ * @private
743
+ */
744
+ _systemCheck() {
745
+ if (!unsafeEvalSupported()) {
746
+ throw new Error("Current environment does not allow unsafe-eval, please use pixi.js/unsafe-eval module to enable support.");
747
+ }
748
+ }
749
+ ensureUniformGroup(uniformGroup) {
750
+ const uniformData = this.getUniformGroupData(uniformGroup);
751
+ uniformGroup.buffer || (uniformGroup.buffer = new Buffer({
752
+ data: new Float32Array(uniformData.layout.size / 4),
753
+ usage: BufferUsage.UNIFORM | BufferUsage.COPY_DST
754
+ }));
755
+ }
756
+ getUniformGroupData(uniformGroup) {
757
+ return this._syncFunctionHash[uniformGroup._signature] || this._initUniformGroup(uniformGroup);
758
+ }
759
+ _initUniformGroup(uniformGroup) {
760
+ const uniformGroupSignature = uniformGroup._signature;
761
+ let uniformData = this._syncFunctionHash[uniformGroupSignature];
762
+ if (!uniformData) {
763
+ const elements = Object.keys(uniformGroup.uniformStructures).map((i) => uniformGroup.uniformStructures[i]);
764
+ const layout = this._adaptor.createUboElements(elements);
765
+ const syncFunction = this._generateUboSync(layout.uboElements);
766
+ uniformData = this._syncFunctionHash[uniformGroupSignature] = {
767
+ layout,
768
+ syncFunction
769
+ };
770
+ }
771
+ return this._syncFunctionHash[uniformGroupSignature];
772
+ }
773
+ _generateUboSync(uboElements) {
774
+ return this._adaptor.generateUboSync(uboElements);
775
+ }
776
+ syncUniformGroup(uniformGroup, data, offset) {
777
+ const uniformGroupData = this.getUniformGroupData(uniformGroup);
778
+ uniformGroup.buffer || (uniformGroup.buffer = new Buffer({
779
+ data: new Float32Array(uniformGroupData.layout.size / 4),
780
+ usage: BufferUsage.UNIFORM | BufferUsage.COPY_DST
781
+ }));
782
+ let dataInt32 = null;
783
+ if (!data) {
784
+ data = uniformGroup.buffer.data;
785
+ dataInt32 = uniformGroup.buffer.dataInt32;
786
+ }
787
+ offset || (offset = 0);
788
+ uniformGroupData.syncFunction(uniformGroup.uniforms, data, dataInt32, offset);
789
+ return true;
790
+ }
791
+ updateUniformGroup(uniformGroup) {
792
+ if (uniformGroup.isStatic && !uniformGroup._dirtyId)
793
+ return false;
794
+ uniformGroup._dirtyId = 0;
795
+ const synced = this.syncUniformGroup(uniformGroup);
796
+ uniformGroup.buffer.update();
797
+ return synced;
798
+ }
799
+ destroy() {
800
+ this._syncFunctionHash = null;
801
+ }
802
+ }
803
+
804
+ const uniformParsers = [
805
+ // uploading pixi matrix object to mat3
806
+ {
807
+ type: "mat3x3<f32>",
808
+ test: (data) => {
809
+ const value = data.value;
810
+ return value.a !== void 0;
811
+ },
812
+ ubo: `
813
+ var matrix = uv[name].toArray(true);
814
+ data[offset] = matrix[0];
815
+ data[offset + 1] = matrix[1];
816
+ data[offset + 2] = matrix[2];
817
+ data[offset + 4] = matrix[3];
818
+ data[offset + 5] = matrix[4];
819
+ data[offset + 6] = matrix[5];
820
+ data[offset + 8] = matrix[6];
821
+ data[offset + 9] = matrix[7];
822
+ data[offset + 10] = matrix[8];
823
+ `,
824
+ uniform: `
825
+ gl.uniformMatrix3fv(ud[name].location, false, uv[name].toArray(true));
826
+ `
827
+ },
828
+ // uploading a pixi rectangle as a vec4
829
+ {
830
+ type: "vec4<f32>",
831
+ test: (data) => data.type === "vec4<f32>" && data.size === 1 && data.value.width !== void 0,
832
+ ubo: `
833
+ v = uv[name];
834
+ data[offset] = v.x;
835
+ data[offset + 1] = v.y;
836
+ data[offset + 2] = v.width;
837
+ data[offset + 3] = v.height;
838
+ `,
839
+ uniform: `
840
+ cv = ud[name].value;
841
+ v = uv[name];
842
+ if (cv[0] !== v.x || cv[1] !== v.y || cv[2] !== v.width || cv[3] !== v.height) {
843
+ cv[0] = v.x;
844
+ cv[1] = v.y;
845
+ cv[2] = v.width;
846
+ cv[3] = v.height;
847
+ gl.uniform4f(ud[name].location, v.x, v.y, v.width, v.height);
848
+ }
849
+ `
850
+ },
851
+ // uploading a pixi point as a vec2
852
+ {
853
+ type: "vec2<f32>",
854
+ test: (data) => data.type === "vec2<f32>" && data.size === 1 && data.value.x !== void 0,
855
+ ubo: `
856
+ v = uv[name];
857
+ data[offset] = v.x;
858
+ data[offset + 1] = v.y;
859
+ `,
860
+ uniform: `
861
+ cv = ud[name].value;
862
+ v = uv[name];
863
+ if (cv[0] !== v.x || cv[1] !== v.y) {
864
+ cv[0] = v.x;
865
+ cv[1] = v.y;
866
+ gl.uniform2f(ud[name].location, v.x, v.y);
867
+ }
868
+ `
869
+ },
870
+ // uploading a pixi color as a vec4
871
+ {
872
+ type: "vec4<f32>",
873
+ test: (data) => data.type === "vec4<f32>" && data.size === 1 && data.value.red !== void 0,
874
+ ubo: `
875
+ v = uv[name];
876
+ data[offset] = v.red;
877
+ data[offset + 1] = v.green;
878
+ data[offset + 2] = v.blue;
879
+ data[offset + 3] = v.alpha;
880
+ `,
881
+ uniform: `
882
+ cv = ud[name].value;
883
+ v = uv[name];
884
+ if (cv[0] !== v.red || cv[1] !== v.green || cv[2] !== v.blue || cv[3] !== v.alpha) {
885
+ cv[0] = v.red;
886
+ cv[1] = v.green;
887
+ cv[2] = v.blue;
888
+ cv[3] = v.alpha;
889
+ gl.uniform4f(ud[name].location, v.red, v.green, v.blue, v.alpha);
890
+ }
891
+ `
892
+ },
893
+ // uploading a pixi color as a vec3
894
+ {
895
+ type: "vec3<f32>",
896
+ test: (data) => data.type === "vec3<f32>" && data.size === 1 && data.value.red !== void 0,
897
+ ubo: `
898
+ v = uv[name];
899
+ data[offset] = v.red;
900
+ data[offset + 1] = v.green;
901
+ data[offset + 2] = v.blue;
902
+ `,
903
+ uniform: `
904
+ cv = ud[name].value;
905
+ v = uv[name];
906
+ if (cv[0] !== v.red || cv[1] !== v.green || cv[2] !== v.blue) {
907
+ cv[0] = v.red;
908
+ cv[1] = v.green;
909
+ cv[2] = v.blue;
910
+ gl.uniform3f(ud[name].location, v.red, v.green, v.blue);
911
+ }
912
+ `
913
+ }
914
+ ];
915
+
916
+ function createUboSyncFunction(uboElements, parserCode, arrayGenerationFunction, singleSettersMap) {
917
+ const funcFragments = [`
918
+ var v = null;
919
+ var v2 = null;
920
+ var t = 0;
921
+ var index = 0;
922
+ var name = null;
923
+ var arrayOffset = null;
924
+ `];
925
+ let prev = 0;
926
+ for (let i = 0; i < uboElements.length; i++) {
927
+ const uboElement = uboElements[i];
928
+ const name = uboElement.data.name;
929
+ let parsed = false;
930
+ let offset = 0;
931
+ for (let j = 0; j < uniformParsers.length; j++) {
932
+ const uniformParser = uniformParsers[j];
933
+ if (uniformParser.test(uboElement.data)) {
934
+ offset = uboElement.offset / 4;
935
+ funcFragments.push(
936
+ `name = "${name}";`,
937
+ `offset += ${offset - prev};`,
938
+ uniformParsers[j][parserCode] || uniformParsers[j].ubo
939
+ );
940
+ parsed = true;
941
+ break;
942
+ }
943
+ }
944
+ if (!parsed) {
945
+ if (uboElement.data.size > 1) {
946
+ offset = uboElement.offset / 4;
947
+ funcFragments.push(arrayGenerationFunction(uboElement, offset - prev));
948
+ } else {
949
+ const template = singleSettersMap[uboElement.data.type];
950
+ offset = uboElement.offset / 4;
951
+ funcFragments.push(
952
+ /* wgsl */
953
+ `
954
+ v = uv.${name};
955
+ offset += ${offset - prev};
956
+ ${template};
957
+ `
958
+ );
959
+ }
960
+ }
961
+ prev = offset;
962
+ }
963
+ const fragmentSrc = funcFragments.join("\n");
964
+ return new Function(
965
+ "uv",
966
+ "data",
967
+ "dataInt32",
968
+ "offset",
969
+ fragmentSrc
970
+ );
971
+ }
972
+
973
+ function loopMatrix(col, row) {
974
+ const total = col * row;
975
+ return `
976
+ for (let i = 0; i < ${total}; i++) {
977
+ data[offset + (((i / ${col})|0) * 4) + (i % ${col})] = v[i];
978
+ }
979
+ `;
980
+ }
981
+ const uboSyncFunctionsSTD40 = {
982
+ f32: `
983
+ data[offset] = v;`,
984
+ i32: `
985
+ dataInt32[offset] = v;`,
986
+ "vec2<f32>": `
987
+ data[offset] = v[0];
988
+ data[offset + 1] = v[1];`,
989
+ "vec3<f32>": `
990
+ data[offset] = v[0];
991
+ data[offset + 1] = v[1];
992
+ data[offset + 2] = v[2];`,
993
+ "vec4<f32>": `
994
+ data[offset] = v[0];
995
+ data[offset + 1] = v[1];
996
+ data[offset + 2] = v[2];
997
+ data[offset + 3] = v[3];`,
998
+ "vec2<i32>": `
999
+ dataInt32[offset] = v[0];
1000
+ dataInt32[offset + 1] = v[1];`,
1001
+ "vec3<i32>": `
1002
+ dataInt32[offset] = v[0];
1003
+ dataInt32[offset + 1] = v[1];
1004
+ dataInt32[offset + 2] = v[2];`,
1005
+ "vec4<i32>": `
1006
+ dataInt32[offset] = v[0];
1007
+ dataInt32[offset + 1] = v[1];
1008
+ dataInt32[offset + 2] = v[2];
1009
+ dataInt32[offset + 3] = v[3];`,
1010
+ "mat2x2<f32>": `
1011
+ data[offset] = v[0];
1012
+ data[offset + 1] = v[1];
1013
+ data[offset + 4] = v[2];
1014
+ data[offset + 5] = v[3];`,
1015
+ "mat3x3<f32>": `
1016
+ data[offset] = v[0];
1017
+ data[offset + 1] = v[1];
1018
+ data[offset + 2] = v[2];
1019
+ data[offset + 4] = v[3];
1020
+ data[offset + 5] = v[4];
1021
+ data[offset + 6] = v[5];
1022
+ data[offset + 8] = v[6];
1023
+ data[offset + 9] = v[7];
1024
+ data[offset + 10] = v[8];`,
1025
+ "mat4x4<f32>": `
1026
+ for (let i = 0; i < 16; i++) {
1027
+ data[offset + i] = v[i];
1028
+ }`,
1029
+ "mat3x2<f32>": loopMatrix(3, 2),
1030
+ "mat4x2<f32>": loopMatrix(4, 2),
1031
+ "mat2x3<f32>": loopMatrix(2, 3),
1032
+ "mat4x3<f32>": loopMatrix(4, 3),
1033
+ "mat2x4<f32>": loopMatrix(2, 4),
1034
+ "mat3x4<f32>": loopMatrix(3, 4)
1035
+ };
1036
+ const uboSyncFunctionsWGSL = {
1037
+ ...uboSyncFunctionsSTD40,
1038
+ "mat2x2<f32>": `
1039
+ data[offset] = v[0];
1040
+ data[offset + 1] = v[1];
1041
+ data[offset + 2] = v[2];
1042
+ data[offset + 3] = v[3];
1043
+ `
1044
+ };
1045
+
1046
+ function calculateProjection(pm, x, y, width, height, flipY) {
1047
+ const sign = flipY ? 1 : -1;
1048
+ pm.identity();
1049
+ pm.a = 1 / width * 2;
1050
+ pm.d = sign * (1 / height * 2);
1051
+ pm.tx = -1 - x * pm.a;
1052
+ pm.ty = -sign - y * pm.d;
1053
+ return pm;
1054
+ }
1055
+
1056
+ const canvasCache = /* @__PURE__ */ new Map();
1057
+ function getCanvasTexture(canvas, options) {
1058
+ if (!canvasCache.has(canvas)) {
1059
+ const texture = new Texture({
1060
+ source: new CanvasSource({
1061
+ resource: canvas,
1062
+ ...options
1063
+ })
1064
+ });
1065
+ const onDestroy = () => {
1066
+ if (canvasCache.get(canvas) === texture) {
1067
+ canvasCache.delete(canvas);
1068
+ }
1069
+ };
1070
+ texture.once("destroy", onDestroy);
1071
+ texture.source.once("destroy", onDestroy);
1072
+ canvasCache.set(canvas, texture);
1073
+ }
1074
+ return canvasCache.get(canvas);
1075
+ }
1076
+
1077
+ function isRenderingToScreen(renderTarget) {
1078
+ const resource = renderTarget.colorTexture.source.resource;
1079
+ return globalThis.HTMLCanvasElement && resource instanceof HTMLCanvasElement && document.body.contains(resource);
1080
+ }
1081
+
1082
+ const _RenderTarget = class _RenderTarget {
1083
+ /**
1084
+ * @param [descriptor] - Options for creating a render target.
1085
+ */
1086
+ constructor(descriptor = {}) {
1087
+ /** unique id for this render target */
1088
+ this.uid = uid$1("renderTarget");
1089
+ /**
1090
+ * An array of textures that can be written to by the GPU - mostly this has one texture in Pixi, but you could
1091
+ * write to multiple if required! (eg deferred lighting)
1092
+ */
1093
+ this.colorTextures = [];
1094
+ this.dirtyId = 0;
1095
+ this.isRoot = false;
1096
+ this._size = new Float32Array(2);
1097
+ /** if true, then when the render target is destroyed, it will destroy all the textures that were created for it. */
1098
+ this._managedColorTextures = false;
1099
+ descriptor = { ..._RenderTarget.defaultOptions, ...descriptor };
1100
+ this.stencil = descriptor.stencil;
1101
+ this.depth = descriptor.depth;
1102
+ this.isRoot = descriptor.isRoot;
1103
+ if (typeof descriptor.colorTextures === "number") {
1104
+ this._managedColorTextures = true;
1105
+ for (let i = 0; i < descriptor.colorTextures; i++) {
1106
+ this.colorTextures.push(
1107
+ new TextureSource({
1108
+ width: descriptor.width,
1109
+ height: descriptor.height,
1110
+ resolution: descriptor.resolution,
1111
+ antialias: descriptor.antialias
1112
+ })
1113
+ );
1114
+ }
1115
+ } else {
1116
+ this.colorTextures = [...descriptor.colorTextures.map((texture) => texture.source)];
1117
+ const colorSource = this.colorTexture.source;
1118
+ this.resize(colorSource.width, colorSource.height, colorSource._resolution);
1119
+ }
1120
+ this.colorTexture.source.on("resize", this.onSourceResize, this);
1121
+ if (descriptor.depthStencilTexture || this.stencil) {
1122
+ if (descriptor.depthStencilTexture instanceof Texture || descriptor.depthStencilTexture instanceof TextureSource) {
1123
+ this.depthStencilTexture = descriptor.depthStencilTexture.source;
1124
+ } else {
1125
+ this.ensureDepthStencilTexture();
1126
+ }
1127
+ }
1128
+ }
1129
+ get size() {
1130
+ const _size = this._size;
1131
+ _size[0] = this.pixelWidth;
1132
+ _size[1] = this.pixelHeight;
1133
+ return _size;
1134
+ }
1135
+ get width() {
1136
+ return this.colorTexture.source.width;
1137
+ }
1138
+ get height() {
1139
+ return this.colorTexture.source.height;
1140
+ }
1141
+ get pixelWidth() {
1142
+ return this.colorTexture.source.pixelWidth;
1143
+ }
1144
+ get pixelHeight() {
1145
+ return this.colorTexture.source.pixelHeight;
1146
+ }
1147
+ get resolution() {
1148
+ return this.colorTexture.source._resolution;
1149
+ }
1150
+ get colorTexture() {
1151
+ return this.colorTextures[0];
1152
+ }
1153
+ onSourceResize(source) {
1154
+ this.resize(source.width, source.height, source._resolution, true);
1155
+ }
1156
+ /**
1157
+ * This will ensure a depthStencil texture is created for this render target.
1158
+ * Most likely called by the mask system to make sure we have stencil buffer added.
1159
+ * @internal
1160
+ * @ignore
1161
+ */
1162
+ ensureDepthStencilTexture() {
1163
+ if (!this.depthStencilTexture) {
1164
+ this.depthStencilTexture = new TextureSource({
1165
+ width: this.width,
1166
+ height: this.height,
1167
+ resolution: this.resolution,
1168
+ format: "depth24plus-stencil8",
1169
+ autoGenerateMipmaps: false,
1170
+ antialias: false,
1171
+ mipLevelCount: 1
1172
+ // sampleCount: handled by the render target system..
1173
+ });
1174
+ }
1175
+ }
1176
+ resize(width, height, resolution = this.resolution, skipColorTexture = false) {
1177
+ this.dirtyId++;
1178
+ this.colorTextures.forEach((colorTexture, i) => {
1179
+ if (skipColorTexture && i === 0)
1180
+ return;
1181
+ colorTexture.source.resize(width, height, resolution);
1182
+ });
1183
+ if (this.depthStencilTexture) {
1184
+ this.depthStencilTexture.source.resize(width, height, resolution);
1185
+ }
1186
+ }
1187
+ destroy() {
1188
+ this.colorTexture.source.off("resize", this.onSourceResize, this);
1189
+ if (this._managedColorTextures) {
1190
+ this.colorTextures.forEach((texture) => {
1191
+ texture.destroy();
1192
+ });
1193
+ }
1194
+ if (this.depthStencilTexture) {
1195
+ this.depthStencilTexture.destroy();
1196
+ delete this.depthStencilTexture;
1197
+ }
1198
+ }
1199
+ };
1200
+ /** The default options for a render target */
1201
+ _RenderTarget.defaultOptions = {
1202
+ /** the width of the RenderTarget */
1203
+ width: 0,
1204
+ /** the height of the RenderTarget */
1205
+ height: 0,
1206
+ /** the resolution of the RenderTarget */
1207
+ resolution: 1,
1208
+ /** an array of textures, or a number indicating how many color textures there should be */
1209
+ colorTextures: 1,
1210
+ /** should this render target have a stencil buffer? */
1211
+ stencil: false,
1212
+ /** should this render target have a depth buffer? */
1213
+ depth: false,
1214
+ /** should this render target be antialiased? */
1215
+ antialias: false,
1216
+ // save on perf by default!
1217
+ /** is this a root element, true if this is gl context owners render target */
1218
+ isRoot: false
1219
+ };
1220
+ let RenderTarget = _RenderTarget;
1221
+
1222
+ class RenderTargetSystem {
1223
+ constructor(renderer) {
1224
+ /** This is the root viewport for the render pass*/
1225
+ this.rootViewPort = new Rectangle();
1226
+ /** the current viewport that the gpu is using */
1227
+ this.viewport = new Rectangle();
1228
+ /**
1229
+ * a runner that lets systems know if the active render target has changed.
1230
+ * Eg the Stencil System needs to know so it can manage the stencil buffer
1231
+ */
1232
+ this.onRenderTargetChange = new SystemRunner("onRenderTargetChange");
1233
+ /** the projection matrix that is used by the shaders based on the active render target and the viewport */
1234
+ this.projectionMatrix = new Matrix();
1235
+ /** the default clear color for render targets */
1236
+ this.defaultClearColor = [0, 0, 0, 0];
1237
+ /**
1238
+ * a hash that stores the render target for a given render surface. When you pass in a texture source,
1239
+ * a render target is created for it. This map stores and makes it easy to retrieve the render target
1240
+ */
1241
+ this._renderSurfaceToRenderTargetHash = /* @__PURE__ */ new Map();
1242
+ /** A hash that stores a gpu render target for a given render target. */
1243
+ this._gpuRenderTargetHash = /* @__PURE__ */ Object.create(null);
1244
+ /**
1245
+ * A stack that stores the render target and frame that is currently being rendered to.
1246
+ * When push is called, the current render target is stored in this stack.
1247
+ * When pop is called, the previous render target is restored.
1248
+ */
1249
+ this._renderTargetStack = [];
1250
+ this._renderer = renderer;
1251
+ renderer.renderableGC.addManagedHash(this, "_gpuRenderTargetHash");
1252
+ }
1253
+ /** called when dev wants to finish a render pass */
1254
+ finishRenderPass() {
1255
+ this.adaptor.finishRenderPass(this.renderTarget);
1256
+ }
1257
+ /**
1258
+ * called when the renderer starts to render a scene.
1259
+ * @param options
1260
+ * @param options.target - the render target to render to
1261
+ * @param options.clear - the clear mode to use. Can be true or a CLEAR number 'COLOR | DEPTH | STENCIL' 0b111
1262
+ * @param options.clearColor - the color to clear to
1263
+ * @param options.frame - the frame to render to
1264
+ */
1265
+ renderStart({
1266
+ target,
1267
+ clear,
1268
+ clearColor,
1269
+ frame
1270
+ }) {
1271
+ this._renderTargetStack.length = 0;
1272
+ this.push(
1273
+ target,
1274
+ clear,
1275
+ clearColor,
1276
+ frame
1277
+ );
1278
+ this.rootViewPort.copyFrom(this.viewport);
1279
+ this.rootRenderTarget = this.renderTarget;
1280
+ this.renderingToScreen = isRenderingToScreen(this.rootRenderTarget);
1281
+ this.adaptor.prerender?.(this.rootRenderTarget);
1282
+ }
1283
+ postrender() {
1284
+ this.adaptor.postrender?.(this.rootRenderTarget);
1285
+ }
1286
+ /**
1287
+ * Binding a render surface! This is the main function of the render target system.
1288
+ * It will take the RenderSurface (which can be a texture, canvas, or render target) and bind it to the renderer.
1289
+ * Once bound all draw calls will be rendered to the render surface.
1290
+ *
1291
+ * If a frame is not provide and the render surface is a texture, the frame of the texture will be used.
1292
+ * @param renderSurface - the render surface to bind
1293
+ * @param clear - the clear mode to use. Can be true or a CLEAR number 'COLOR | DEPTH | STENCIL' 0b111
1294
+ * @param clearColor - the color to clear to
1295
+ * @param frame - the frame to render to
1296
+ * @returns the render target that was bound
1297
+ */
1298
+ bind(renderSurface, clear = true, clearColor, frame) {
1299
+ const renderTarget = this.getRenderTarget(renderSurface);
1300
+ const didChange = this.renderTarget !== renderTarget;
1301
+ this.renderTarget = renderTarget;
1302
+ this.renderSurface = renderSurface;
1303
+ const gpuRenderTarget = this.getGpuRenderTarget(renderTarget);
1304
+ if (renderTarget.pixelWidth !== gpuRenderTarget.width || renderTarget.pixelHeight !== gpuRenderTarget.height) {
1305
+ this.adaptor.resizeGpuRenderTarget(renderTarget);
1306
+ gpuRenderTarget.width = renderTarget.pixelWidth;
1307
+ gpuRenderTarget.height = renderTarget.pixelHeight;
1308
+ }
1309
+ const source = renderTarget.colorTexture;
1310
+ const viewport = this.viewport;
1311
+ const pixelWidth = source.pixelWidth;
1312
+ const pixelHeight = source.pixelHeight;
1313
+ if (!frame && renderSurface instanceof Texture) {
1314
+ frame = renderSurface.frame;
1315
+ }
1316
+ if (frame) {
1317
+ const resolution = source._resolution;
1318
+ viewport.x = frame.x * resolution + 0.5 | 0;
1319
+ viewport.y = frame.y * resolution + 0.5 | 0;
1320
+ viewport.width = frame.width * resolution + 0.5 | 0;
1321
+ viewport.height = frame.height * resolution + 0.5 | 0;
1322
+ } else {
1323
+ viewport.x = 0;
1324
+ viewport.y = 0;
1325
+ viewport.width = pixelWidth;
1326
+ viewport.height = pixelHeight;
1327
+ }
1328
+ calculateProjection(
1329
+ this.projectionMatrix,
1330
+ 0,
1331
+ 0,
1332
+ viewport.width / source.resolution,
1333
+ viewport.height / source.resolution,
1334
+ !renderTarget.isRoot
1335
+ );
1336
+ this.adaptor.startRenderPass(renderTarget, clear, clearColor, viewport);
1337
+ if (didChange) {
1338
+ this.onRenderTargetChange.emit(renderTarget);
1339
+ }
1340
+ return renderTarget;
1341
+ }
1342
+ clear(target, clear = CLEAR.ALL, clearColor) {
1343
+ if (!clear)
1344
+ return;
1345
+ if (target) {
1346
+ target = this.getRenderTarget(target);
1347
+ }
1348
+ this.adaptor.clear(
1349
+ target || this.renderTarget,
1350
+ clear,
1351
+ clearColor,
1352
+ this.viewport
1353
+ );
1354
+ }
1355
+ contextChange() {
1356
+ this._gpuRenderTargetHash = /* @__PURE__ */ Object.create(null);
1357
+ }
1358
+ /**
1359
+ * Push a render surface to the renderer. This will bind the render surface to the renderer,
1360
+ * @param renderSurface - the render surface to push
1361
+ * @param clear - the clear mode to use. Can be true or a CLEAR number 'COLOR | DEPTH | STENCIL' 0b111
1362
+ * @param clearColor - the color to clear to
1363
+ * @param frame - the frame to use when rendering to the render surface
1364
+ */
1365
+ push(renderSurface, clear = CLEAR.ALL, clearColor, frame) {
1366
+ const renderTarget = this.bind(renderSurface, clear, clearColor, frame);
1367
+ this._renderTargetStack.push({
1368
+ renderTarget,
1369
+ frame
1370
+ });
1371
+ return renderTarget;
1372
+ }
1373
+ /** Pops the current render target from the renderer and restores the previous render target. */
1374
+ pop() {
1375
+ this._renderTargetStack.pop();
1376
+ const currentRenderTargetData = this._renderTargetStack[this._renderTargetStack.length - 1];
1377
+ this.bind(currentRenderTargetData.renderTarget, false, null, currentRenderTargetData.frame);
1378
+ }
1379
+ /**
1380
+ * Gets the render target from the provide render surface. Eg if its a texture,
1381
+ * it will return the render target for the texture.
1382
+ * If its a render target, it will return the same render target.
1383
+ * @param renderSurface - the render surface to get the render target for
1384
+ * @returns the render target for the render surface
1385
+ */
1386
+ getRenderTarget(renderSurface) {
1387
+ if (renderSurface.isTexture) {
1388
+ renderSurface = renderSurface.source;
1389
+ }
1390
+ return this._renderSurfaceToRenderTargetHash.get(renderSurface) ?? this._initRenderTarget(renderSurface);
1391
+ }
1392
+ /**
1393
+ * Copies a render surface to another texture.
1394
+ *
1395
+ * NOTE:
1396
+ * for sourceRenderSurfaceTexture, The render target must be something that is written too by the renderer
1397
+ *
1398
+ * The following is not valid:
1399
+ * @example
1400
+ * const canvas = document.createElement('canvas')
1401
+ * canvas.width = 200;
1402
+ * canvas.height = 200;
1403
+ *
1404
+ * const ctx = canvas2.getContext('2d')!
1405
+ * ctx.fillStyle = 'red'
1406
+ * ctx.fillRect(0, 0, 200, 200);
1407
+ *
1408
+ * const texture = RenderTexture.create({
1409
+ * width: 200,
1410
+ * height: 200,
1411
+ * })
1412
+ * const renderTarget = renderer.renderTarget.getRenderTarget(canvas2);
1413
+ *
1414
+ * renderer.renderTarget.copyToTexture(renderTarget,texture, {x:0,y:0},{width:200,height:200},{x:0,y:0});
1415
+ *
1416
+ * The best way to copy a canvas is to create a texture from it. Then render with that.
1417
+ *
1418
+ * Parsing in a RenderTarget canvas context (with a 2d context)
1419
+ * @param sourceRenderSurfaceTexture - the render surface to copy from
1420
+ * @param destinationTexture - the texture to copy to
1421
+ * @param originSrc - the origin of the copy
1422
+ * @param originSrc.x - the x origin of the copy
1423
+ * @param originSrc.y - the y origin of the copy
1424
+ * @param size - the size of the copy
1425
+ * @param size.width - the width of the copy
1426
+ * @param size.height - the height of the copy
1427
+ * @param originDest - the destination origin (top left to paste from!)
1428
+ * @param originDest.x - the x origin of the paste
1429
+ * @param originDest.y - the y origin of the paste
1430
+ */
1431
+ copyToTexture(sourceRenderSurfaceTexture, destinationTexture, originSrc, size, originDest) {
1432
+ if (originSrc.x < 0) {
1433
+ size.width += originSrc.x;
1434
+ originDest.x -= originSrc.x;
1435
+ originSrc.x = 0;
1436
+ }
1437
+ if (originSrc.y < 0) {
1438
+ size.height += originSrc.y;
1439
+ originDest.y -= originSrc.y;
1440
+ originSrc.y = 0;
1441
+ }
1442
+ const { pixelWidth, pixelHeight } = sourceRenderSurfaceTexture;
1443
+ size.width = Math.min(size.width, pixelWidth - originSrc.x);
1444
+ size.height = Math.min(size.height, pixelHeight - originSrc.y);
1445
+ return this.adaptor.copyToTexture(
1446
+ sourceRenderSurfaceTexture,
1447
+ destinationTexture,
1448
+ originSrc,
1449
+ size,
1450
+ originDest
1451
+ );
1452
+ }
1453
+ /**
1454
+ * ensures that we have a depth stencil buffer available to render to
1455
+ * This is used by the mask system to make sure we have a stencil buffer.
1456
+ */
1457
+ ensureDepthStencil() {
1458
+ if (!this.renderTarget.stencil) {
1459
+ this.renderTarget.stencil = true;
1460
+ this.adaptor.startRenderPass(this.renderTarget, false, null, this.viewport);
1461
+ }
1462
+ }
1463
+ /** nukes the render target system */
1464
+ destroy() {
1465
+ this._renderer = null;
1466
+ this._renderSurfaceToRenderTargetHash.forEach((renderTarget, key) => {
1467
+ if (renderTarget !== key) {
1468
+ renderTarget.destroy();
1469
+ }
1470
+ });
1471
+ this._renderSurfaceToRenderTargetHash.clear();
1472
+ this._gpuRenderTargetHash = /* @__PURE__ */ Object.create(null);
1473
+ }
1474
+ _initRenderTarget(renderSurface) {
1475
+ let renderTarget = null;
1476
+ if (CanvasSource.test(renderSurface)) {
1477
+ renderSurface = getCanvasTexture(renderSurface).source;
1478
+ }
1479
+ if (renderSurface instanceof RenderTarget) {
1480
+ renderTarget = renderSurface;
1481
+ } else if (renderSurface instanceof TextureSource) {
1482
+ renderTarget = new RenderTarget({
1483
+ colorTextures: [renderSurface]
1484
+ });
1485
+ if (CanvasSource.test(renderSurface.source.resource)) {
1486
+ renderTarget.isRoot = true;
1487
+ }
1488
+ renderSurface.once("destroy", () => {
1489
+ renderTarget.destroy();
1490
+ this._renderSurfaceToRenderTargetHash.delete(renderSurface);
1491
+ const gpuRenderTarget = this._gpuRenderTargetHash[renderTarget.uid];
1492
+ if (gpuRenderTarget) {
1493
+ this._gpuRenderTargetHash[renderTarget.uid] = null;
1494
+ this.adaptor.destroyGpuRenderTarget(gpuRenderTarget);
1495
+ }
1496
+ });
1497
+ }
1498
+ this._renderSurfaceToRenderTargetHash.set(renderSurface, renderTarget);
1499
+ return renderTarget;
1500
+ }
1501
+ getGpuRenderTarget(renderTarget) {
1502
+ return this._gpuRenderTargetHash[renderTarget.uid] || (this._gpuRenderTargetHash[renderTarget.uid] = this.adaptor.initGpuRenderTarget(renderTarget));
1503
+ }
1504
+ resetState() {
1505
+ this.renderTarget = null;
1506
+ this.renderSurface = null;
1507
+ }
1508
+ }
1509
+
1510
+ class BufferResource extends EventEmitter {
1511
+ /**
1512
+ * Create a new Buffer Resource.
1513
+ * @param options - The options for the buffer resource
1514
+ * @param options.buffer - The underlying buffer that this resource is using
1515
+ * @param options.offset - The offset of the buffer this resource is using.
1516
+ * If not provided, then it will use the offset of the buffer.
1517
+ * @param options.size - The size of the buffer this resource is using.
1518
+ * If not provided, then it will use the size of the buffer.
1519
+ */
1520
+ constructor({ buffer, offset, size }) {
1521
+ super();
1522
+ /**
1523
+ * emits when the underlying buffer has changed shape (i.e. resized)
1524
+ * letting the renderer know that it needs to discard the old buffer on the GPU and create a new one
1525
+ * @event change
1526
+ */
1527
+ /** a unique id for this uniform group used through the renderer */
1528
+ this.uid = uid$1("buffer");
1529
+ /**
1530
+ * a resource type, used to identify how to handle it when its in a bind group / shader resource
1531
+ * @internal
1532
+ * @ignore
1533
+ */
1534
+ this._resourceType = "bufferResource";
1535
+ /**
1536
+ * used internally to know if a uniform group was used in the last render pass
1537
+ * @internal
1538
+ * @ignore
1539
+ */
1540
+ this._touched = 0;
1541
+ /**
1542
+ * the resource id used internally by the renderer to build bind group keys
1543
+ * @internal
1544
+ * @ignore
1545
+ */
1546
+ this._resourceId = uid$1("resource");
1547
+ /**
1548
+ * A cheeky hint to the GL renderer to let it know this is a BufferResource
1549
+ * @internal
1550
+ * @ignore
1551
+ */
1552
+ this._bufferResource = true;
1553
+ /**
1554
+ * Has the Buffer resource been destroyed?
1555
+ * @readonly
1556
+ */
1557
+ this.destroyed = false;
1558
+ this.buffer = buffer;
1559
+ this.offset = offset | 0;
1560
+ this.size = size;
1561
+ this.buffer.on("change", this.onBufferChange, this);
1562
+ }
1563
+ onBufferChange() {
1564
+ this._resourceId = uid$1("resource");
1565
+ this.emit("change", this);
1566
+ }
1567
+ /**
1568
+ * Destroys this resource. Make sure the underlying buffer is not used anywhere else
1569
+ * if you want to destroy it as well, or code will explode
1570
+ * @param destroyBuffer - Should the underlying buffer be destroyed as well?
1571
+ */
1572
+ destroy(destroyBuffer = false) {
1573
+ this.destroyed = true;
1574
+ if (destroyBuffer) {
1575
+ this.buffer.destroy();
1576
+ }
1577
+ this.emit("change", this);
1578
+ this.buffer = null;
1579
+ }
1580
+ }
1581
+
1582
+ class CustomRenderPipe {
1583
+ constructor(renderer) {
1584
+ this._renderer = renderer;
1585
+ }
1586
+ updateRenderable() {
1587
+ }
1588
+ destroyRenderable() {
1589
+ }
1590
+ validateRenderable() {
1591
+ return false;
1592
+ }
1593
+ addRenderable(container, instructionSet) {
1594
+ this._renderer.renderPipes.batch.break(instructionSet);
1595
+ instructionSet.add(container);
1596
+ }
1597
+ execute(container) {
1598
+ if (!container.isRenderable)
1599
+ return;
1600
+ container.render(this._renderer);
1601
+ }
1602
+ destroy() {
1603
+ this._renderer = null;
1604
+ }
1605
+ }
1606
+ CustomRenderPipe.extension = {
1607
+ type: [
1608
+ ExtensionType.WebGLPipes,
1609
+ ExtensionType.WebGPUPipes,
1610
+ ExtensionType.CanvasPipes
1611
+ ],
1612
+ name: "customRender"
1613
+ };
1614
+
1615
+ function executeInstructions(renderGroup, renderer) {
1616
+ const instructionSet = renderGroup.instructionSet;
1617
+ const instructions = instructionSet.instructions;
1618
+ for (let i = 0; i < instructionSet.instructionSize; i++) {
1619
+ const instruction = instructions[i];
1620
+ renderer[instruction.renderPipeId].execute(instruction);
1621
+ }
1622
+ }
1623
+
1624
+ const tempMatrix$1 = new Matrix();
1625
+ class RenderGroupPipe {
1626
+ constructor(renderer) {
1627
+ this._renderer = renderer;
1628
+ }
1629
+ addRenderGroup(renderGroup, instructionSet) {
1630
+ if (renderGroup.isCachedAsTexture) {
1631
+ this._addRenderableCacheAsTexture(renderGroup, instructionSet);
1632
+ } else {
1633
+ this._addRenderableDirect(renderGroup, instructionSet);
1634
+ }
1635
+ }
1636
+ execute(renderGroup) {
1637
+ if (!renderGroup.isRenderable)
1638
+ return;
1639
+ if (renderGroup.isCachedAsTexture) {
1640
+ this._executeCacheAsTexture(renderGroup);
1641
+ } else {
1642
+ this._executeDirect(renderGroup);
1643
+ }
1644
+ }
1645
+ destroy() {
1646
+ this._renderer = null;
1647
+ }
1648
+ _addRenderableDirect(renderGroup, instructionSet) {
1649
+ this._renderer.renderPipes.batch.break(instructionSet);
1650
+ if (renderGroup._batchableRenderGroup) {
1651
+ BigPool.return(renderGroup._batchableRenderGroup);
1652
+ renderGroup._batchableRenderGroup = null;
1653
+ }
1654
+ instructionSet.add(renderGroup);
1655
+ }
1656
+ _addRenderableCacheAsTexture(renderGroup, instructionSet) {
1657
+ const batchableRenderGroup = renderGroup._batchableRenderGroup ?? (renderGroup._batchableRenderGroup = BigPool.get(BatchableSprite));
1658
+ batchableRenderGroup.renderable = renderGroup.root;
1659
+ batchableRenderGroup.transform = renderGroup.root.relativeGroupTransform;
1660
+ batchableRenderGroup.texture = renderGroup.texture;
1661
+ batchableRenderGroup.bounds = renderGroup._textureBounds;
1662
+ instructionSet.add(renderGroup);
1663
+ this._renderer.renderPipes.batch.addToBatch(batchableRenderGroup, instructionSet);
1664
+ }
1665
+ _executeCacheAsTexture(renderGroup) {
1666
+ if (renderGroup.textureNeedsUpdate) {
1667
+ renderGroup.textureNeedsUpdate = false;
1668
+ const worldTransformMatrix = tempMatrix$1.identity().translate(
1669
+ -renderGroup._textureBounds.x,
1670
+ -renderGroup._textureBounds.y
1671
+ );
1672
+ this._renderer.renderTarget.push(renderGroup.texture, true, null, renderGroup.texture.frame);
1673
+ this._renderer.globalUniforms.push({
1674
+ worldTransformMatrix,
1675
+ worldColor: 4294967295
1676
+ });
1677
+ executeInstructions(renderGroup, this._renderer.renderPipes);
1678
+ this._renderer.renderTarget.finishRenderPass();
1679
+ this._renderer.renderTarget.pop();
1680
+ this._renderer.globalUniforms.pop();
1681
+ }
1682
+ renderGroup._batchableRenderGroup._batcher.updateElement(renderGroup._batchableRenderGroup);
1683
+ renderGroup._batchableRenderGroup._batcher.geometry.buffers[0].update();
1684
+ }
1685
+ _executeDirect(renderGroup) {
1686
+ this._renderer.globalUniforms.push({
1687
+ worldTransformMatrix: renderGroup.inverseParentTextureTransform,
1688
+ worldColor: renderGroup.worldColorAlpha
1689
+ });
1690
+ executeInstructions(renderGroup, this._renderer.renderPipes);
1691
+ this._renderer.globalUniforms.pop();
1692
+ }
1693
+ }
1694
+ RenderGroupPipe.extension = {
1695
+ type: [
1696
+ ExtensionType.WebGLPipes,
1697
+ ExtensionType.WebGPUPipes,
1698
+ ExtensionType.CanvasPipes
1699
+ ],
1700
+ name: "renderGroup"
1701
+ };
1702
+
1703
+ function clearList(list, index) {
1704
+ index || (index = 0);
1705
+ for (let j = index; j < list.length; j++) {
1706
+ if (list[j]) {
1707
+ list[j] = null;
1708
+ } else {
1709
+ break;
1710
+ }
1711
+ }
1712
+ }
1713
+
1714
+ const tempContainer = new Container();
1715
+ const UPDATE_BLEND_COLOR_VISIBLE = UPDATE_VISIBLE | UPDATE_COLOR | UPDATE_BLEND;
1716
+ function updateRenderGroupTransforms(renderGroup, updateChildRenderGroups = false) {
1717
+ updateRenderGroupTransform(renderGroup);
1718
+ const childrenToUpdate = renderGroup.childrenToUpdate;
1719
+ const updateTick = renderGroup.updateTick++;
1720
+ for (const j in childrenToUpdate) {
1721
+ const renderGroupDepth = Number(j);
1722
+ const childrenAtDepth = childrenToUpdate[j];
1723
+ const list = childrenAtDepth.list;
1724
+ const index = childrenAtDepth.index;
1725
+ for (let i = 0; i < index; i++) {
1726
+ const child = list[i];
1727
+ if (child.parentRenderGroup === renderGroup && child.relativeRenderGroupDepth === renderGroupDepth) {
1728
+ updateTransformAndChildren(child, updateTick, 0);
1729
+ }
1730
+ }
1731
+ clearList(list, index);
1732
+ childrenAtDepth.index = 0;
1733
+ }
1734
+ if (updateChildRenderGroups) {
1735
+ for (let i = 0; i < renderGroup.renderGroupChildren.length; i++) {
1736
+ updateRenderGroupTransforms(renderGroup.renderGroupChildren[i], updateChildRenderGroups);
1737
+ }
1738
+ }
1739
+ }
1740
+ function updateRenderGroupTransform(renderGroup) {
1741
+ const root = renderGroup.root;
1742
+ let worldAlpha;
1743
+ if (renderGroup.renderGroupParent) {
1744
+ const renderGroupParent = renderGroup.renderGroupParent;
1745
+ renderGroup.worldTransform.appendFrom(
1746
+ root.relativeGroupTransform,
1747
+ renderGroupParent.worldTransform
1748
+ );
1749
+ renderGroup.worldColor = multiplyColors(
1750
+ root.groupColor,
1751
+ renderGroupParent.worldColor
1752
+ );
1753
+ worldAlpha = root.groupAlpha * renderGroupParent.worldAlpha;
1754
+ } else {
1755
+ renderGroup.worldTransform.copyFrom(root.localTransform);
1756
+ renderGroup.worldColor = root.localColor;
1757
+ worldAlpha = root.localAlpha;
1758
+ }
1759
+ worldAlpha = worldAlpha < 0 ? 0 : worldAlpha > 1 ? 1 : worldAlpha;
1760
+ renderGroup.worldAlpha = worldAlpha;
1761
+ renderGroup.worldColorAlpha = renderGroup.worldColor + ((worldAlpha * 255 | 0) << 24);
1762
+ }
1763
+ function updateTransformAndChildren(container, updateTick, updateFlags) {
1764
+ if (updateTick === container.updateTick)
1765
+ return;
1766
+ container.updateTick = updateTick;
1767
+ container.didChange = false;
1768
+ const localTransform = container.localTransform;
1769
+ container.updateLocalTransform();
1770
+ const parent = container.parent;
1771
+ if (parent && !parent.renderGroup) {
1772
+ updateFlags |= container._updateFlags;
1773
+ container.relativeGroupTransform.appendFrom(
1774
+ localTransform,
1775
+ parent.relativeGroupTransform
1776
+ );
1777
+ if (updateFlags & UPDATE_BLEND_COLOR_VISIBLE) {
1778
+ updateColorBlendVisibility(container, parent, updateFlags);
1779
+ }
1780
+ } else {
1781
+ updateFlags = container._updateFlags;
1782
+ container.relativeGroupTransform.copyFrom(localTransform);
1783
+ if (updateFlags & UPDATE_BLEND_COLOR_VISIBLE) {
1784
+ updateColorBlendVisibility(container, tempContainer, updateFlags);
1785
+ }
1786
+ }
1787
+ if (!container.renderGroup) {
1788
+ const children = container.children;
1789
+ const length = children.length;
1790
+ for (let i = 0; i < length; i++) {
1791
+ updateTransformAndChildren(children[i], updateTick, updateFlags);
1792
+ }
1793
+ const renderGroup = container.parentRenderGroup;
1794
+ const renderable = container;
1795
+ if (renderable.renderPipeId && !renderGroup.structureDidChange) {
1796
+ renderGroup.updateRenderable(renderable);
1797
+ }
1798
+ }
1799
+ }
1800
+ function updateColorBlendVisibility(container, parent, updateFlags) {
1801
+ if (updateFlags & UPDATE_COLOR) {
1802
+ container.groupColor = multiplyColors(
1803
+ container.localColor,
1804
+ parent.groupColor
1805
+ );
1806
+ let groupAlpha = container.localAlpha * parent.groupAlpha;
1807
+ groupAlpha = groupAlpha < 0 ? 0 : groupAlpha > 1 ? 1 : groupAlpha;
1808
+ container.groupAlpha = groupAlpha;
1809
+ container.groupColorAlpha = container.groupColor + ((groupAlpha * 255 | 0) << 24);
1810
+ }
1811
+ if (updateFlags & UPDATE_BLEND) {
1812
+ container.groupBlendMode = container.localBlendMode === "inherit" ? parent.groupBlendMode : container.localBlendMode;
1813
+ }
1814
+ if (updateFlags & UPDATE_VISIBLE) {
1815
+ container.globalDisplayStatus = container.localDisplayStatus & parent.globalDisplayStatus;
1816
+ }
1817
+ container._updateFlags = 0;
1818
+ }
1819
+
1820
+ function validateRenderables(renderGroup, renderPipes) {
1821
+ const { list, index } = renderGroup.childrenRenderablesToUpdate;
1822
+ let rebuildRequired = false;
1823
+ for (let i = 0; i < index; i++) {
1824
+ const container = list[i];
1825
+ const renderable = container;
1826
+ const pipe = renderPipes[renderable.renderPipeId];
1827
+ rebuildRequired = pipe.validateRenderable(container);
1828
+ if (rebuildRequired) {
1829
+ break;
1830
+ }
1831
+ }
1832
+ renderGroup.structureDidChange = rebuildRequired;
1833
+ return rebuildRequired;
1834
+ }
1835
+
1836
+ const tempMatrix = new Matrix();
1837
+ class RenderGroupSystem {
1838
+ constructor(renderer) {
1839
+ this._renderer = renderer;
1840
+ }
1841
+ render({ container, transform }) {
1842
+ const parent = container.parent;
1843
+ const renderGroupParent = container.renderGroup.renderGroupParent;
1844
+ container.parent = null;
1845
+ container.renderGroup.renderGroupParent = null;
1846
+ const renderer = this._renderer;
1847
+ let originalLocalTransform = tempMatrix;
1848
+ if (transform) {
1849
+ originalLocalTransform = originalLocalTransform.copyFrom(container.renderGroup.localTransform);
1850
+ container.renderGroup.localTransform.copyFrom(transform);
1851
+ }
1852
+ const renderPipes = renderer.renderPipes;
1853
+ this._updateCachedRenderGroups(container.renderGroup, null);
1854
+ this._updateRenderGroups(container.renderGroup);
1855
+ renderer.globalUniforms.start({
1856
+ worldTransformMatrix: transform ? container.renderGroup.localTransform : container.renderGroup.worldTransform,
1857
+ worldColor: container.renderGroup.worldColorAlpha
1858
+ });
1859
+ executeInstructions(container.renderGroup, renderPipes);
1860
+ if (renderPipes.uniformBatch) {
1861
+ renderPipes.uniformBatch.renderEnd();
1862
+ }
1863
+ if (transform) {
1864
+ container.renderGroup.localTransform.copyFrom(originalLocalTransform);
1865
+ }
1866
+ container.parent = parent;
1867
+ container.renderGroup.renderGroupParent = renderGroupParent;
1868
+ }
1869
+ destroy() {
1870
+ this._renderer = null;
1871
+ }
1872
+ _updateCachedRenderGroups(renderGroup, closestCacheAsTexture) {
1873
+ if (renderGroup.isCachedAsTexture) {
1874
+ if (!renderGroup.updateCacheTexture)
1875
+ return;
1876
+ closestCacheAsTexture = renderGroup;
1877
+ }
1878
+ renderGroup._parentCacheAsTextureRenderGroup = closestCacheAsTexture;
1879
+ for (let i = renderGroup.renderGroupChildren.length - 1; i >= 0; i--) {
1880
+ this._updateCachedRenderGroups(renderGroup.renderGroupChildren[i], closestCacheAsTexture);
1881
+ }
1882
+ renderGroup.invalidateMatrices();
1883
+ if (renderGroup.isCachedAsTexture) {
1884
+ if (renderGroup.textureNeedsUpdate) {
1885
+ const bounds = renderGroup.root.getLocalBounds();
1886
+ bounds.ceil();
1887
+ const lastTexture = renderGroup.texture;
1888
+ if (renderGroup.texture) {
1889
+ TexturePool.returnTexture(renderGroup.texture);
1890
+ }
1891
+ const renderer = this._renderer;
1892
+ const resolution = renderGroup.textureOptions.resolution || renderer.view.resolution;
1893
+ const antialias = renderGroup.textureOptions.antialias ?? renderer.view.antialias;
1894
+ renderGroup.texture = TexturePool.getOptimalTexture(
1895
+ bounds.width,
1896
+ bounds.height,
1897
+ resolution,
1898
+ antialias
1899
+ );
1900
+ renderGroup._textureBounds || (renderGroup._textureBounds = new Bounds());
1901
+ renderGroup._textureBounds.copyFrom(bounds);
1902
+ if (lastTexture !== renderGroup.texture) {
1903
+ if (renderGroup.renderGroupParent) {
1904
+ renderGroup.renderGroupParent.structureDidChange = true;
1905
+ }
1906
+ }
1907
+ }
1908
+ } else if (renderGroup.texture) {
1909
+ TexturePool.returnTexture(renderGroup.texture);
1910
+ renderGroup.texture = null;
1911
+ }
1912
+ }
1913
+ _updateRenderGroups(renderGroup) {
1914
+ const renderer = this._renderer;
1915
+ const renderPipes = renderer.renderPipes;
1916
+ renderGroup.runOnRender(renderer);
1917
+ renderGroup.instructionSet.renderPipes = renderPipes;
1918
+ if (!renderGroup.structureDidChange) {
1919
+ validateRenderables(renderGroup, renderPipes);
1920
+ } else {
1921
+ clearList(renderGroup.childrenRenderablesToUpdate.list, 0);
1922
+ }
1923
+ updateRenderGroupTransforms(renderGroup);
1924
+ if (renderGroup.structureDidChange) {
1925
+ renderGroup.structureDidChange = false;
1926
+ this._buildInstructions(renderGroup, renderer);
1927
+ } else {
1928
+ this._updateRenderables(renderGroup);
1929
+ }
1930
+ renderGroup.childrenRenderablesToUpdate.index = 0;
1931
+ renderer.renderPipes.batch.upload(renderGroup.instructionSet);
1932
+ if (renderGroup.isCachedAsTexture && !renderGroup.textureNeedsUpdate)
1933
+ return;
1934
+ for (let i = 0; i < renderGroup.renderGroupChildren.length; i++) {
1935
+ this._updateRenderGroups(renderGroup.renderGroupChildren[i]);
1936
+ }
1937
+ }
1938
+ _updateRenderables(renderGroup) {
1939
+ const { list, index } = renderGroup.childrenRenderablesToUpdate;
1940
+ for (let i = 0; i < index; i++) {
1941
+ const container = list[i];
1942
+ if (container.didViewUpdate) {
1943
+ renderGroup.updateRenderable(container);
1944
+ }
1945
+ }
1946
+ clearList(list, index);
1947
+ }
1948
+ _buildInstructions(renderGroup, rendererOrPipes) {
1949
+ const root = renderGroup.root;
1950
+ const instructionSet = renderGroup.instructionSet;
1951
+ instructionSet.reset();
1952
+ const renderer = rendererOrPipes.renderPipes ? rendererOrPipes : rendererOrPipes.batch.renderer;
1953
+ const renderPipes = renderer.renderPipes;
1954
+ renderPipes.batch.buildStart(instructionSet);
1955
+ renderPipes.blendMode.buildStart();
1956
+ renderPipes.colorMask.buildStart();
1957
+ if (root.sortableChildren) {
1958
+ root.sortChildren();
1959
+ }
1960
+ root.collectRenderablesWithEffects(instructionSet, renderer, null);
1961
+ renderPipes.batch.buildEnd(instructionSet);
1962
+ renderPipes.blendMode.buildEnd(instructionSet);
1963
+ }
1964
+ }
1965
+ /** @ignore */
1966
+ RenderGroupSystem.extension = {
1967
+ type: [
1968
+ ExtensionType.WebGLSystem,
1969
+ ExtensionType.WebGPUSystem,
1970
+ ExtensionType.CanvasSystem
1971
+ ],
1972
+ name: "renderGroup"
1973
+ };
1974
+
1975
+ class SpritePipe {
1976
+ constructor(renderer) {
1977
+ this._gpuSpriteHash = /* @__PURE__ */ Object.create(null);
1978
+ this._destroyRenderableBound = this.destroyRenderable.bind(this);
1979
+ this._renderer = renderer;
1980
+ this._renderer.renderableGC.addManagedHash(this, "_gpuSpriteHash");
1981
+ }
1982
+ addRenderable(sprite, instructionSet) {
1983
+ const gpuSprite = this._getGpuSprite(sprite);
1984
+ if (sprite.didViewUpdate)
1985
+ this._updateBatchableSprite(sprite, gpuSprite);
1986
+ this._renderer.renderPipes.batch.addToBatch(gpuSprite, instructionSet);
1987
+ }
1988
+ updateRenderable(sprite) {
1989
+ const gpuSprite = this._gpuSpriteHash[sprite.uid];
1990
+ if (sprite.didViewUpdate)
1991
+ this._updateBatchableSprite(sprite, gpuSprite);
1992
+ gpuSprite._batcher.updateElement(gpuSprite);
1993
+ }
1994
+ validateRenderable(sprite) {
1995
+ const gpuSprite = this._getGpuSprite(sprite);
1996
+ return !gpuSprite._batcher.checkAndUpdateTexture(
1997
+ gpuSprite,
1998
+ sprite._texture
1999
+ );
2000
+ }
2001
+ destroyRenderable(sprite) {
2002
+ const batchableSprite = this._gpuSpriteHash[sprite.uid];
2003
+ BigPool.return(batchableSprite);
2004
+ this._gpuSpriteHash[sprite.uid] = null;
2005
+ sprite.off("destroyed", this._destroyRenderableBound);
2006
+ }
2007
+ _updateBatchableSprite(sprite, batchableSprite) {
2008
+ batchableSprite.bounds = sprite.visualBounds;
2009
+ batchableSprite.texture = sprite._texture;
2010
+ }
2011
+ _getGpuSprite(sprite) {
2012
+ return this._gpuSpriteHash[sprite.uid] || this._initGPUSprite(sprite);
2013
+ }
2014
+ _initGPUSprite(sprite) {
2015
+ const batchableSprite = BigPool.get(BatchableSprite);
2016
+ batchableSprite.renderable = sprite;
2017
+ batchableSprite.transform = sprite.groupTransform;
2018
+ batchableSprite.texture = sprite._texture;
2019
+ batchableSprite.bounds = sprite.visualBounds;
2020
+ batchableSprite.roundPixels = this._renderer._roundPixels | sprite._roundPixels;
2021
+ this._gpuSpriteHash[sprite.uid] = batchableSprite;
2022
+ sprite.on("destroyed", this._destroyRenderableBound);
2023
+ return batchableSprite;
2024
+ }
2025
+ destroy() {
2026
+ for (const i in this._gpuSpriteHash) {
2027
+ BigPool.return(this._gpuSpriteHash[i]);
2028
+ }
2029
+ this._gpuSpriteHash = null;
2030
+ this._renderer = null;
2031
+ }
2032
+ }
2033
+ /** @ignore */
2034
+ SpritePipe.extension = {
2035
+ type: [
2036
+ ExtensionType.WebGLPipes,
2037
+ ExtensionType.WebGPUPipes,
2038
+ ExtensionType.CanvasPipes
2039
+ ],
2040
+ name: "sprite"
2041
+ };
2042
+
2043
+ const _BackgroundSystem = class _BackgroundSystem {
2044
+ constructor() {
2045
+ this.clearBeforeRender = true;
2046
+ this._backgroundColor = new Color(0);
2047
+ this.color = this._backgroundColor;
2048
+ this.alpha = 1;
2049
+ }
2050
+ /**
2051
+ * initiates the background system
2052
+ * @param options - the options for the background colors
2053
+ */
2054
+ init(options) {
2055
+ options = { ..._BackgroundSystem.defaultOptions, ...options };
2056
+ this.clearBeforeRender = options.clearBeforeRender;
2057
+ this.color = options.background || options.backgroundColor || this._backgroundColor;
2058
+ this.alpha = options.backgroundAlpha;
2059
+ this._backgroundColor.setAlpha(options.backgroundAlpha);
2060
+ }
2061
+ /** The background color to fill if not transparent */
2062
+ get color() {
2063
+ return this._backgroundColor;
2064
+ }
2065
+ set color(value) {
2066
+ this._backgroundColor.setValue(value);
2067
+ }
2068
+ /** The background color alpha. Setting this to 0 will make the canvas transparent. */
2069
+ get alpha() {
2070
+ return this._backgroundColor.alpha;
2071
+ }
2072
+ set alpha(value) {
2073
+ this._backgroundColor.setAlpha(value);
2074
+ }
2075
+ /** The background color as an [R, G, B, A] array. */
2076
+ get colorRgba() {
2077
+ return this._backgroundColor.toArray();
2078
+ }
2079
+ /**
2080
+ * destroys the background system
2081
+ * @internal
2082
+ * @ignore
2083
+ */
2084
+ destroy() {
2085
+ }
2086
+ };
2087
+ /** @ignore */
2088
+ _BackgroundSystem.extension = {
2089
+ type: [
2090
+ ExtensionType.WebGLSystem,
2091
+ ExtensionType.WebGPUSystem,
2092
+ ExtensionType.CanvasSystem
2093
+ ],
2094
+ name: "background",
2095
+ priority: 0
2096
+ };
2097
+ /** default options used by the system */
2098
+ _BackgroundSystem.defaultOptions = {
2099
+ /**
2100
+ * {@link WebGLOptions.backgroundAlpha}
2101
+ * @default 1
2102
+ */
2103
+ backgroundAlpha: 1,
2104
+ /**
2105
+ * {@link WebGLOptions.backgroundColor}
2106
+ * @default 0x000000
2107
+ */
2108
+ backgroundColor: 0,
2109
+ /**
2110
+ * {@link WebGLOptions.clearBeforeRender}
2111
+ * @default true
2112
+ */
2113
+ clearBeforeRender: true
2114
+ };
2115
+ let BackgroundSystem = _BackgroundSystem;
2116
+
2117
+ const BLEND_MODE_FILTERS = {};
2118
+ extensions.handle(ExtensionType.BlendMode, (value) => {
2119
+ if (!value.name) {
2120
+ throw new Error("BlendMode extension must have a name property");
2121
+ }
2122
+ BLEND_MODE_FILTERS[value.name] = value.ref;
2123
+ }, (value) => {
2124
+ delete BLEND_MODE_FILTERS[value.name];
2125
+ });
2126
+ class BlendModePipe {
2127
+ constructor(renderer) {
2128
+ this._isAdvanced = false;
2129
+ this._filterHash = /* @__PURE__ */ Object.create(null);
2130
+ this._renderer = renderer;
2131
+ this._renderer.runners.prerender.add(this);
2132
+ }
2133
+ prerender() {
2134
+ this._activeBlendMode = "normal";
2135
+ this._isAdvanced = false;
2136
+ }
2137
+ /**
2138
+ * This ensures that a blendMode switch is added to the instruction set if the blend mode has changed.
2139
+ * @param renderable - The renderable we are adding to the instruction set
2140
+ * @param blendMode - The blend mode of the renderable
2141
+ * @param instructionSet - The instruction set we are adding to
2142
+ */
2143
+ setBlendMode(renderable, blendMode, instructionSet) {
2144
+ if (this._activeBlendMode === blendMode) {
2145
+ if (this._isAdvanced)
2146
+ this._renderableList.push(renderable);
2147
+ return;
2148
+ }
2149
+ this._activeBlendMode = blendMode;
2150
+ if (this._isAdvanced) {
2151
+ this._endAdvancedBlendMode(instructionSet);
2152
+ }
2153
+ this._isAdvanced = !!BLEND_MODE_FILTERS[blendMode];
2154
+ if (this._isAdvanced) {
2155
+ this._beginAdvancedBlendMode(instructionSet);
2156
+ this._renderableList.push(renderable);
2157
+ }
2158
+ }
2159
+ _beginAdvancedBlendMode(instructionSet) {
2160
+ this._renderer.renderPipes.batch.break(instructionSet);
2161
+ const blendMode = this._activeBlendMode;
2162
+ if (!BLEND_MODE_FILTERS[blendMode]) {
2163
+ warn(`Unable to assign BlendMode: '${blendMode}'. You may want to include: import 'pixi.js/advanced-blend-modes'`);
2164
+ return;
2165
+ }
2166
+ let filterEffect = this._filterHash[blendMode];
2167
+ if (!filterEffect) {
2168
+ filterEffect = this._filterHash[blendMode] = new FilterEffect();
2169
+ filterEffect.filters = [new BLEND_MODE_FILTERS[blendMode]()];
2170
+ }
2171
+ const instruction = {
2172
+ renderPipeId: "filter",
2173
+ action: "pushFilter",
2174
+ renderables: [],
2175
+ filterEffect,
2176
+ canBundle: false
2177
+ };
2178
+ this._renderableList = instruction.renderables;
2179
+ instructionSet.add(instruction);
2180
+ }
2181
+ _endAdvancedBlendMode(instructionSet) {
2182
+ this._renderableList = null;
2183
+ this._renderer.renderPipes.batch.break(instructionSet);
2184
+ instructionSet.add({
2185
+ renderPipeId: "filter",
2186
+ action: "popFilter",
2187
+ canBundle: false
2188
+ });
2189
+ }
2190
+ /**
2191
+ * called when the instruction build process is starting this will reset internally to the default blend mode
2192
+ * @internal
2193
+ * @ignore
2194
+ */
2195
+ buildStart() {
2196
+ this._isAdvanced = false;
2197
+ }
2198
+ /**
2199
+ * called when the instruction build process is finished, ensuring that if there is an advanced blend mode
2200
+ * active, we add the final render instructions added to the instruction set
2201
+ * @param instructionSet - The instruction set we are adding to
2202
+ * @internal
2203
+ * @ignore
2204
+ */
2205
+ buildEnd(instructionSet) {
2206
+ if (this._isAdvanced) {
2207
+ this._endAdvancedBlendMode(instructionSet);
2208
+ }
2209
+ }
2210
+ /**
2211
+ * @internal
2212
+ * @ignore
2213
+ */
2214
+ destroy() {
2215
+ this._renderer = null;
2216
+ this._renderableList = null;
2217
+ for (const i in this._filterHash) {
2218
+ this._filterHash[i].destroy();
2219
+ }
2220
+ this._filterHash = null;
2221
+ }
2222
+ }
2223
+ /** @ignore */
2224
+ BlendModePipe.extension = {
2225
+ type: [
2226
+ ExtensionType.WebGLPipes,
2227
+ ExtensionType.WebGPUPipes,
2228
+ ExtensionType.CanvasPipes
2229
+ ],
2230
+ name: "blendMode"
2231
+ };
2232
+
2233
+ const imageTypes = {
2234
+ png: "image/png",
2235
+ jpg: "image/jpeg",
2236
+ webp: "image/webp"
2237
+ };
2238
+ const _ExtractSystem = class _ExtractSystem {
2239
+ /** @param renderer - The renderer this System works for. */
2240
+ constructor(renderer) {
2241
+ this._renderer = renderer;
2242
+ }
2243
+ _normalizeOptions(options, defaults = {}) {
2244
+ if (options instanceof Container || options instanceof Texture) {
2245
+ return {
2246
+ target: options,
2247
+ ...defaults
2248
+ };
2249
+ }
2250
+ return {
2251
+ ...defaults,
2252
+ ...options
2253
+ };
2254
+ }
2255
+ /**
2256
+ * Will return a HTML Image of the target
2257
+ * @param options - The options for creating the image, or the target to extract
2258
+ * @returns - HTML Image of the target
2259
+ */
2260
+ async image(options) {
2261
+ const image = new Image();
2262
+ image.src = await this.base64(options);
2263
+ return image;
2264
+ }
2265
+ /**
2266
+ * Will return a base64 encoded string of this target. It works by calling
2267
+ * `Extract.canvas` and then running toDataURL on that.
2268
+ * @param options - The options for creating the image, or the target to extract
2269
+ */
2270
+ async base64(options) {
2271
+ options = this._normalizeOptions(
2272
+ options,
2273
+ _ExtractSystem.defaultImageOptions
2274
+ );
2275
+ const { format, quality } = options;
2276
+ const canvas = this.canvas(options);
2277
+ if (canvas.toBlob !== void 0) {
2278
+ return new Promise((resolve, reject) => {
2279
+ canvas.toBlob((blob) => {
2280
+ if (!blob) {
2281
+ reject(new Error("ICanvas.toBlob failed!"));
2282
+ return;
2283
+ }
2284
+ const reader = new FileReader();
2285
+ reader.onload = () => resolve(reader.result);
2286
+ reader.onerror = reject;
2287
+ reader.readAsDataURL(blob);
2288
+ }, imageTypes[format], quality);
2289
+ });
2290
+ }
2291
+ if (canvas.toDataURL !== void 0) {
2292
+ return canvas.toDataURL(imageTypes[format], quality);
2293
+ }
2294
+ if (canvas.convertToBlob !== void 0) {
2295
+ const blob = await canvas.convertToBlob({ type: imageTypes[format], quality });
2296
+ return new Promise((resolve, reject) => {
2297
+ const reader = new FileReader();
2298
+ reader.onload = () => resolve(reader.result);
2299
+ reader.onerror = reject;
2300
+ reader.readAsDataURL(blob);
2301
+ });
2302
+ }
2303
+ throw new Error("Extract.base64() requires ICanvas.toDataURL, ICanvas.toBlob, or ICanvas.convertToBlob to be implemented");
2304
+ }
2305
+ /**
2306
+ * Creates a Canvas element, renders this target to it and then returns it.
2307
+ * @param options - The options for creating the canvas, or the target to extract
2308
+ * @returns - A Canvas element with the texture rendered on.
2309
+ */
2310
+ canvas(options) {
2311
+ options = this._normalizeOptions(options);
2312
+ const target = options.target;
2313
+ const renderer = this._renderer;
2314
+ if (target instanceof Texture) {
2315
+ return renderer.texture.generateCanvas(target);
2316
+ }
2317
+ const texture = renderer.textureGenerator.generateTexture(options);
2318
+ const canvas = renderer.texture.generateCanvas(texture);
2319
+ texture.destroy(true);
2320
+ return canvas;
2321
+ }
2322
+ /**
2323
+ * Will return a one-dimensional array containing the pixel data of the entire texture in RGBA
2324
+ * order, with integer values between 0 and 255 (included).
2325
+ * @param options - The options for extracting the image, or the target to extract
2326
+ * @returns - One-dimensional array containing the pixel data of the entire texture
2327
+ */
2328
+ pixels(options) {
2329
+ options = this._normalizeOptions(options);
2330
+ const target = options.target;
2331
+ const renderer = this._renderer;
2332
+ const texture = target instanceof Texture ? target : renderer.textureGenerator.generateTexture(options);
2333
+ const pixelInfo = renderer.texture.getPixels(texture);
2334
+ if (target instanceof Container) {
2335
+ texture.destroy(true);
2336
+ }
2337
+ return pixelInfo;
2338
+ }
2339
+ /**
2340
+ * Will return a texture of the target
2341
+ * @param options - The options for creating the texture, or the target to extract
2342
+ * @returns - A texture of the target
2343
+ */
2344
+ texture(options) {
2345
+ options = this._normalizeOptions(options);
2346
+ if (options.target instanceof Texture)
2347
+ return options.target;
2348
+ return this._renderer.textureGenerator.generateTexture(options);
2349
+ }
2350
+ /**
2351
+ * Will extract a HTMLImage of the target and download it
2352
+ * @param options - The options for downloading and extracting the image, or the target to extract
2353
+ */
2354
+ download(options) {
2355
+ options = this._normalizeOptions(options);
2356
+ const canvas = this.canvas(options);
2357
+ const link = document.createElement("a");
2358
+ link.download = options.filename ?? "image.png";
2359
+ link.href = canvas.toDataURL("image/png");
2360
+ document.body.appendChild(link);
2361
+ link.click();
2362
+ document.body.removeChild(link);
2363
+ }
2364
+ /**
2365
+ * Logs the target to the console as an image. This is a useful way to debug what's happening in the renderer.
2366
+ * @param options - The options for logging the image, or the target to log
2367
+ */
2368
+ log(options) {
2369
+ const width = options.width ?? 200;
2370
+ options = this._normalizeOptions(options);
2371
+ const canvas = this.canvas(options);
2372
+ const base64 = canvas.toDataURL();
2373
+ console.log(`[Pixi Texture] ${canvas.width}px ${canvas.height}px`);
2374
+ const style = [
2375
+ "font-size: 1px;",
2376
+ `padding: ${width}px ${300}px;`,
2377
+ `background: url(${base64}) no-repeat;`,
2378
+ "background-size: contain;"
2379
+ ].join(" ");
2380
+ console.log("%c ", style);
2381
+ }
2382
+ destroy() {
2383
+ this._renderer = null;
2384
+ }
2385
+ };
2386
+ /** @ignore */
2387
+ _ExtractSystem.extension = {
2388
+ type: [
2389
+ ExtensionType.WebGLSystem,
2390
+ ExtensionType.WebGPUSystem
2391
+ ],
2392
+ name: "extract"
2393
+ };
2394
+ /** Default options for creating an image. */
2395
+ _ExtractSystem.defaultImageOptions = {
2396
+ /** The format of the image. */
2397
+ format: "png",
2398
+ /** The quality of the image. */
2399
+ quality: 1
2400
+ };
2401
+ let ExtractSystem = _ExtractSystem;
2402
+
2403
+ class RenderTexture extends Texture {
2404
+ static create(options) {
2405
+ return new RenderTexture({
2406
+ source: new TextureSource(options)
2407
+ });
2408
+ }
2409
+ /**
2410
+ * Resizes the render texture.
2411
+ * @param width - The new width of the render texture.
2412
+ * @param height - The new height of the render texture.
2413
+ * @param resolution - The new resolution of the render texture.
2414
+ * @returns This texture.
2415
+ */
2416
+ resize(width, height, resolution) {
2417
+ this.source.resize(width, height, resolution);
2418
+ return this;
2419
+ }
2420
+ }
2421
+
2422
+ const tempRect = new Rectangle();
2423
+ const tempBounds = new Bounds();
2424
+ const noColor = [0, 0, 0, 0];
2425
+ class GenerateTextureSystem {
2426
+ constructor(renderer) {
2427
+ this._renderer = renderer;
2428
+ }
2429
+ /**
2430
+ * A Useful function that returns a texture of the display object that can then be used to create sprites
2431
+ * This can be quite useful if your container is complicated and needs to be reused multiple times.
2432
+ * @param {GenerateTextureOptions | Container} options - Generate texture options.
2433
+ * @param {Container} [options.container] - If not given, the renderer's resolution is used.
2434
+ * @param {Rectangle} options.region - The region of the container, that shall be rendered,
2435
+ * @param {number} [options.resolution] - The resolution of the texture being generated.
2436
+ * if no region is specified, defaults to the local bounds of the container.
2437
+ * @param {GenerateTextureSourceOptions} [options.textureSourceOptions] - Texture options for GPU.
2438
+ * @returns a shiny new texture of the container passed in
2439
+ */
2440
+ generateTexture(options) {
2441
+ if (options instanceof Container) {
2442
+ options = {
2443
+ target: options,
2444
+ frame: void 0,
2445
+ textureSourceOptions: {},
2446
+ resolution: void 0
2447
+ };
2448
+ }
2449
+ const resolution = options.resolution || this._renderer.resolution;
2450
+ const antialias = options.antialias || this._renderer.view.antialias;
2451
+ const container = options.target;
2452
+ let clearColor = options.clearColor;
2453
+ if (clearColor) {
2454
+ const isRGBAArray = Array.isArray(clearColor) && clearColor.length === 4;
2455
+ clearColor = isRGBAArray ? clearColor : Color.shared.setValue(clearColor).toArray();
2456
+ } else {
2457
+ clearColor = noColor;
2458
+ }
2459
+ const region = options.frame?.copyTo(tempRect) || getLocalBounds(container, tempBounds).rectangle;
2460
+ region.width = Math.max(region.width, 1 / resolution) | 0;
2461
+ region.height = Math.max(region.height, 1 / resolution) | 0;
2462
+ const target = RenderTexture.create({
2463
+ ...options.textureSourceOptions,
2464
+ width: region.width,
2465
+ height: region.height,
2466
+ resolution,
2467
+ antialias
2468
+ });
2469
+ const transform = Matrix.shared.translate(-region.x, -region.y);
2470
+ this._renderer.render({
2471
+ container,
2472
+ transform,
2473
+ target,
2474
+ clearColor
2475
+ });
2476
+ target.source.updateMipmaps();
2477
+ return target;
2478
+ }
2479
+ destroy() {
2480
+ this._renderer = null;
2481
+ }
2482
+ }
2483
+ /** @ignore */
2484
+ GenerateTextureSystem.extension = {
2485
+ type: [
2486
+ ExtensionType.WebGLSystem,
2487
+ ExtensionType.WebGPUSystem
2488
+ ],
2489
+ name: "textureGenerator"
2490
+ };
2491
+
2492
+ class GlobalUniformSystem {
2493
+ constructor(renderer) {
2494
+ this._stackIndex = 0;
2495
+ this._globalUniformDataStack = [];
2496
+ this._uniformsPool = [];
2497
+ this._activeUniforms = [];
2498
+ this._bindGroupPool = [];
2499
+ this._activeBindGroups = [];
2500
+ this._renderer = renderer;
2501
+ }
2502
+ reset() {
2503
+ this._stackIndex = 0;
2504
+ for (let i = 0; i < this._activeUniforms.length; i++) {
2505
+ this._uniformsPool.push(this._activeUniforms[i]);
2506
+ }
2507
+ for (let i = 0; i < this._activeBindGroups.length; i++) {
2508
+ this._bindGroupPool.push(this._activeBindGroups[i]);
2509
+ }
2510
+ this._activeUniforms.length = 0;
2511
+ this._activeBindGroups.length = 0;
2512
+ }
2513
+ start(options) {
2514
+ this.reset();
2515
+ this.push(options);
2516
+ }
2517
+ bind({
2518
+ size,
2519
+ projectionMatrix,
2520
+ worldTransformMatrix,
2521
+ worldColor,
2522
+ offset
2523
+ }) {
2524
+ const renderTarget = this._renderer.renderTarget.renderTarget;
2525
+ const currentGlobalUniformData = this._stackIndex ? this._globalUniformDataStack[this._stackIndex - 1] : {
2526
+ worldTransformMatrix: new Matrix(),
2527
+ worldColor: 4294967295,
2528
+ offset: new Point()
2529
+ };
2530
+ const globalUniformData = {
2531
+ projectionMatrix: projectionMatrix || this._renderer.renderTarget.projectionMatrix,
2532
+ resolution: size || renderTarget.size,
2533
+ worldTransformMatrix: worldTransformMatrix || currentGlobalUniformData.worldTransformMatrix,
2534
+ worldColor: worldColor || currentGlobalUniformData.worldColor,
2535
+ offset: offset || currentGlobalUniformData.offset,
2536
+ bindGroup: null
2537
+ };
2538
+ const uniformGroup = this._uniformsPool.pop() || this._createUniforms();
2539
+ this._activeUniforms.push(uniformGroup);
2540
+ const uniforms = uniformGroup.uniforms;
2541
+ uniforms.uProjectionMatrix = globalUniformData.projectionMatrix;
2542
+ uniforms.uResolution = globalUniformData.resolution;
2543
+ uniforms.uWorldTransformMatrix.copyFrom(globalUniformData.worldTransformMatrix);
2544
+ uniforms.uWorldTransformMatrix.tx -= globalUniformData.offset.x;
2545
+ uniforms.uWorldTransformMatrix.ty -= globalUniformData.offset.y;
2546
+ color32BitToUniform(
2547
+ globalUniformData.worldColor,
2548
+ uniforms.uWorldColorAlpha,
2549
+ 0
2550
+ );
2551
+ uniformGroup.update();
2552
+ let bindGroup;
2553
+ if (this._renderer.renderPipes.uniformBatch) {
2554
+ bindGroup = this._renderer.renderPipes.uniformBatch.getUniformBindGroup(uniformGroup, false);
2555
+ } else {
2556
+ bindGroup = this._bindGroupPool.pop() || new BindGroup();
2557
+ this._activeBindGroups.push(bindGroup);
2558
+ bindGroup.setResource(uniformGroup, 0);
2559
+ }
2560
+ globalUniformData.bindGroup = bindGroup;
2561
+ this._currentGlobalUniformData = globalUniformData;
2562
+ }
2563
+ push(options) {
2564
+ this.bind(options);
2565
+ this._globalUniformDataStack[this._stackIndex++] = this._currentGlobalUniformData;
2566
+ }
2567
+ pop() {
2568
+ this._currentGlobalUniformData = this._globalUniformDataStack[--this._stackIndex - 1];
2569
+ if (this._renderer.type === RendererType.WEBGL) {
2570
+ this._currentGlobalUniformData.bindGroup.resources[0].update();
2571
+ }
2572
+ }
2573
+ get bindGroup() {
2574
+ return this._currentGlobalUniformData.bindGroup;
2575
+ }
2576
+ get globalUniformData() {
2577
+ return this._currentGlobalUniformData;
2578
+ }
2579
+ get uniformGroup() {
2580
+ return this._currentGlobalUniformData.bindGroup.resources[0];
2581
+ }
2582
+ _createUniforms() {
2583
+ const globalUniforms = new UniformGroup({
2584
+ uProjectionMatrix: { value: new Matrix(), type: "mat3x3<f32>" },
2585
+ uWorldTransformMatrix: { value: new Matrix(), type: "mat3x3<f32>" },
2586
+ // TODO - someone smart - set this to be a unorm8x4 rather than a vec4<f32>
2587
+ uWorldColorAlpha: { value: new Float32Array(4), type: "vec4<f32>" },
2588
+ uResolution: { value: [0, 0], type: "vec2<f32>" }
2589
+ }, {
2590
+ isStatic: true
2591
+ });
2592
+ return globalUniforms;
2593
+ }
2594
+ destroy() {
2595
+ this._renderer = null;
2596
+ }
2597
+ }
2598
+ /** @ignore */
2599
+ GlobalUniformSystem.extension = {
2600
+ type: [
2601
+ ExtensionType.WebGLSystem,
2602
+ ExtensionType.WebGPUSystem,
2603
+ ExtensionType.CanvasSystem
2604
+ ],
2605
+ name: "globalUniforms"
2606
+ };
2607
+
2608
+ let uid = 1;
2609
+ class SchedulerSystem {
2610
+ constructor() {
2611
+ this._tasks = [];
2612
+ /** a small off set to apply to the repeat schedules. This is just to make sure they run at slightly different times */
2613
+ this._offset = 0;
2614
+ }
2615
+ /** Initializes the scheduler system and starts the ticker. */
2616
+ init() {
2617
+ Ticker.system.add(this._update, this);
2618
+ }
2619
+ /**
2620
+ * Schedules a repeating task.
2621
+ * @param func - The function to execute.
2622
+ * @param duration - The interval duration in milliseconds.
2623
+ * @param useOffset - this will spread out tasks so that they do not all run at the same time
2624
+ * @returns The unique identifier for the scheduled task.
2625
+ */
2626
+ repeat(func, duration, useOffset = true) {
2627
+ const id = uid++;
2628
+ let offset = 0;
2629
+ if (useOffset) {
2630
+ this._offset += 1e3;
2631
+ offset = this._offset;
2632
+ }
2633
+ this._tasks.push({
2634
+ func,
2635
+ duration,
2636
+ start: performance.now(),
2637
+ offset,
2638
+ last: performance.now(),
2639
+ repeat: true,
2640
+ id
2641
+ });
2642
+ return id;
2643
+ }
2644
+ /**
2645
+ * Cancels a scheduled task.
2646
+ * @param id - The unique identifier of the task to cancel.
2647
+ */
2648
+ cancel(id) {
2649
+ for (let i = 0; i < this._tasks.length; i++) {
2650
+ if (this._tasks[i].id === id) {
2651
+ this._tasks.splice(i, 1);
2652
+ return;
2653
+ }
2654
+ }
2655
+ }
2656
+ /**
2657
+ * Updates and executes the scheduled tasks.
2658
+ * @private
2659
+ */
2660
+ _update() {
2661
+ const now = performance.now();
2662
+ for (let i = 0; i < this._tasks.length; i++) {
2663
+ const task = this._tasks[i];
2664
+ if (now - task.offset - task.last >= task.duration) {
2665
+ const elapsed = now - task.start;
2666
+ task.func(elapsed);
2667
+ task.last = now;
2668
+ }
2669
+ }
2670
+ }
2671
+ /**
2672
+ * Destroys the scheduler system and removes all tasks.
2673
+ * @internal
2674
+ * @ignore
2675
+ */
2676
+ destroy() {
2677
+ Ticker.system.remove(this._update, this);
2678
+ this._tasks.length = 0;
2679
+ }
2680
+ }
2681
+ /** @ignore */
2682
+ SchedulerSystem.extension = {
2683
+ type: [
2684
+ ExtensionType.WebGLSystem,
2685
+ ExtensionType.WebGPUSystem,
2686
+ ExtensionType.CanvasSystem
2687
+ ],
2688
+ name: "scheduler",
2689
+ priority: 0
2690
+ };
2691
+
2692
+ let saidHello = false;
2693
+ function sayHello(type) {
2694
+ if (saidHello) {
2695
+ return;
2696
+ }
2697
+ if (DOMAdapter.get().getNavigator().userAgent.toLowerCase().indexOf("chrome") > -1) {
2698
+ const args = [
2699
+ `%c %c %c %c %c PixiJS %c v${VERSION} (${type}) http://www.pixijs.com/
2700
+
2701
+ `,
2702
+ "background: #E72264; padding:5px 0;",
2703
+ "background: #6CA2EA; padding:5px 0;",
2704
+ "background: #B5D33D; padding:5px 0;",
2705
+ "background: #FED23F; padding:5px 0;",
2706
+ "color: #FFFFFF; background: #E72264; padding:5px 0;",
2707
+ "color: #E72264; background: #FFFFFF; padding:5px 0;"
2708
+ ];
2709
+ globalThis.console.log(...args);
2710
+ } else if (globalThis.console) {
2711
+ globalThis.console.log(`PixiJS ${VERSION} - ${type} - http://www.pixijs.com/`);
2712
+ }
2713
+ saidHello = true;
2714
+ }
2715
+
2716
+ class HelloSystem {
2717
+ constructor(renderer) {
2718
+ this._renderer = renderer;
2719
+ }
2720
+ /**
2721
+ * It all starts here! This initiates every system, passing in the options for any system by name.
2722
+ * @param options - the config for the renderer and all its systems
2723
+ */
2724
+ init(options) {
2725
+ if (options.hello) {
2726
+ let name = this._renderer.name;
2727
+ if (this._renderer.type === RendererType.WEBGL) {
2728
+ name += ` ${this._renderer.context.webGLVersion}`;
2729
+ }
2730
+ sayHello(name);
2731
+ }
2732
+ }
2733
+ }
2734
+ /** @ignore */
2735
+ HelloSystem.extension = {
2736
+ type: [
2737
+ ExtensionType.WebGLSystem,
2738
+ ExtensionType.WebGPUSystem,
2739
+ ExtensionType.CanvasSystem
2740
+ ],
2741
+ name: "hello",
2742
+ priority: -2
2743
+ };
2744
+ /** The default options for the system. */
2745
+ HelloSystem.defaultOptions = {
2746
+ /** {@link WebGLOptions.hello} */
2747
+ hello: false
2748
+ };
2749
+
2750
+ function cleanHash(hash) {
2751
+ let clean = false;
2752
+ for (const i in hash) {
2753
+ if (hash[i] == void 0) {
2754
+ clean = true;
2755
+ break;
2756
+ }
2757
+ }
2758
+ if (!clean)
2759
+ return hash;
2760
+ const cleanHash2 = /* @__PURE__ */ Object.create(null);
2761
+ for (const i in hash) {
2762
+ const value = hash[i];
2763
+ if (value) {
2764
+ cleanHash2[i] = value;
2765
+ }
2766
+ }
2767
+ return cleanHash2;
2768
+ }
2769
+ function cleanArray(arr) {
2770
+ let offset = 0;
2771
+ for (let i = 0; i < arr.length; i++) {
2772
+ if (arr[i] == void 0) {
2773
+ offset++;
2774
+ } else {
2775
+ arr[i - offset] = arr[i];
2776
+ }
2777
+ }
2778
+ arr.length -= offset;
2779
+ return arr;
2780
+ }
2781
+
2782
+ let renderableGCTick = 0;
2783
+ const _RenderableGCSystem = class _RenderableGCSystem {
2784
+ /**
2785
+ * Creates a new RenderableGCSystem instance.
2786
+ * @param renderer - The renderer this garbage collection system works for
2787
+ */
2788
+ constructor(renderer) {
2789
+ /** Array of renderables being tracked for garbage collection */
2790
+ this._managedRenderables = [];
2791
+ /** Array of hash objects being tracked for cleanup */
2792
+ this._managedHashes = [];
2793
+ /** Array of arrays being tracked for cleanup */
2794
+ this._managedArrays = [];
2795
+ this._renderer = renderer;
2796
+ }
2797
+ /**
2798
+ * Initializes the garbage collection system with the provided options.
2799
+ * @param options - Configuration options for the renderer
2800
+ */
2801
+ init(options) {
2802
+ options = { ..._RenderableGCSystem.defaultOptions, ...options };
2803
+ this.maxUnusedTime = options.renderableGCMaxUnusedTime;
2804
+ this._frequency = options.renderableGCFrequency;
2805
+ this.enabled = options.renderableGCActive;
2806
+ }
2807
+ /**
2808
+ * Gets whether the garbage collection system is currently enabled.
2809
+ * @returns True if GC is enabled, false otherwise
2810
+ */
2811
+ get enabled() {
2812
+ return !!this._handler;
2813
+ }
2814
+ /**
2815
+ * Enables or disables the garbage collection system.
2816
+ * When enabled, schedules periodic cleanup of resources.
2817
+ * When disabled, cancels all scheduled cleanups.
2818
+ */
2819
+ set enabled(value) {
2820
+ if (this.enabled === value)
2821
+ return;
2822
+ if (value) {
2823
+ this._handler = this._renderer.scheduler.repeat(
2824
+ () => this.run(),
2825
+ this._frequency,
2826
+ false
2827
+ );
2828
+ this._hashHandler = this._renderer.scheduler.repeat(
2829
+ () => {
2830
+ for (const hash of this._managedHashes) {
2831
+ hash.context[hash.hash] = cleanHash(hash.context[hash.hash]);
2832
+ }
2833
+ },
2834
+ this._frequency
2835
+ );
2836
+ this._arrayHandler = this._renderer.scheduler.repeat(
2837
+ () => {
2838
+ for (const array of this._managedArrays) {
2839
+ cleanArray(array.context[array.hash]);
2840
+ }
2841
+ },
2842
+ this._frequency
2843
+ );
2844
+ } else {
2845
+ this._renderer.scheduler.cancel(this._handler);
2846
+ this._renderer.scheduler.cancel(this._hashHandler);
2847
+ this._renderer.scheduler.cancel(this._arrayHandler);
2848
+ }
2849
+ }
2850
+ /**
2851
+ * Adds a hash table to be managed by the garbage collector.
2852
+ * @param context - The object containing the hash table
2853
+ * @param hash - The property name of the hash table
2854
+ */
2855
+ addManagedHash(context, hash) {
2856
+ this._managedHashes.push({ context, hash });
2857
+ }
2858
+ /**
2859
+ * Adds an array to be managed by the garbage collector.
2860
+ * @param context - The object containing the array
2861
+ * @param hash - The property name of the array
2862
+ */
2863
+ addManagedArray(context, hash) {
2864
+ this._managedArrays.push({ context, hash });
2865
+ }
2866
+ /**
2867
+ * Updates the GC timestamp and tracking before rendering.
2868
+ * @param options - The render options
2869
+ * @param options.container - The container to render
2870
+ */
2871
+ prerender({
2872
+ container
2873
+ }) {
2874
+ this._now = performance.now();
2875
+ container.renderGroup.gcTick = renderableGCTick++;
2876
+ this._updateInstructionGCTick(container.renderGroup, container.renderGroup.gcTick);
2877
+ }
2878
+ /**
2879
+ * Starts tracking a renderable for garbage collection.
2880
+ * @param renderable - The renderable to track
2881
+ */
2882
+ addRenderable(renderable) {
2883
+ if (!this.enabled)
2884
+ return;
2885
+ if (renderable._lastUsed === -1) {
2886
+ this._managedRenderables.push(renderable);
2887
+ renderable.once("destroyed", this._removeRenderable, this);
2888
+ }
2889
+ renderable._lastUsed = this._now;
2890
+ }
2891
+ /**
2892
+ * Performs garbage collection by cleaning up unused renderables.
2893
+ * Removes renderables that haven't been used for longer than maxUnusedTime.
2894
+ */
2895
+ run() {
2896
+ const now = this._now;
2897
+ const managedRenderables = this._managedRenderables;
2898
+ const renderPipes = this._renderer.renderPipes;
2899
+ let offset = 0;
2900
+ for (let i = 0; i < managedRenderables.length; i++) {
2901
+ const renderable = managedRenderables[i];
2902
+ if (renderable === null) {
2903
+ offset++;
2904
+ continue;
2905
+ }
2906
+ const renderGroup = renderable.renderGroup ?? renderable.parentRenderGroup;
2907
+ const currentTick = renderGroup?.instructionSet?.gcTick ?? -1;
2908
+ if ((renderGroup?.gcTick ?? 0) === currentTick) {
2909
+ renderable._lastUsed = now;
2910
+ }
2911
+ if (now - renderable._lastUsed > this.maxUnusedTime) {
2912
+ if (!renderable.destroyed) {
2913
+ const rp = renderPipes;
2914
+ if (renderGroup)
2915
+ renderGroup.structureDidChange = true;
2916
+ rp[renderable.renderPipeId].destroyRenderable(renderable);
2917
+ }
2918
+ renderable._lastUsed = -1;
2919
+ offset++;
2920
+ renderable.off("destroyed", this._removeRenderable, this);
2921
+ } else {
2922
+ managedRenderables[i - offset] = renderable;
2923
+ }
2924
+ }
2925
+ managedRenderables.length -= offset;
2926
+ }
2927
+ /** Cleans up the garbage collection system. Disables GC and removes all tracked resources. */
2928
+ destroy() {
2929
+ this.enabled = false;
2930
+ this._renderer = null;
2931
+ this._managedRenderables.length = 0;
2932
+ this._managedHashes.length = 0;
2933
+ this._managedArrays.length = 0;
2934
+ }
2935
+ /**
2936
+ * Removes a renderable from being tracked when it's destroyed.
2937
+ * @param renderable - The renderable to stop tracking
2938
+ */
2939
+ _removeRenderable(renderable) {
2940
+ const index = this._managedRenderables.indexOf(renderable);
2941
+ if (index >= 0) {
2942
+ renderable.off("destroyed", this._removeRenderable, this);
2943
+ this._managedRenderables[index] = null;
2944
+ }
2945
+ }
2946
+ /**
2947
+ * Updates the GC tick counter for a render group and its children.
2948
+ * @param renderGroup - The render group to update
2949
+ * @param gcTick - The new tick value
2950
+ */
2951
+ _updateInstructionGCTick(renderGroup, gcTick) {
2952
+ renderGroup.instructionSet.gcTick = gcTick;
2953
+ for (const child of renderGroup.renderGroupChildren) {
2954
+ this._updateInstructionGCTick(child, gcTick);
2955
+ }
2956
+ }
2957
+ };
2958
+ /**
2959
+ * Extension metadata for registering this system with the renderer.
2960
+ * @ignore
2961
+ */
2962
+ _RenderableGCSystem.extension = {
2963
+ type: [
2964
+ ExtensionType.WebGLSystem,
2965
+ ExtensionType.WebGPUSystem
2966
+ ],
2967
+ name: "renderableGC",
2968
+ priority: 0
2969
+ };
2970
+ /**
2971
+ * Default configuration options for the garbage collection system.
2972
+ * These can be overridden when initializing the renderer.
2973
+ */
2974
+ _RenderableGCSystem.defaultOptions = {
2975
+ /** Enable/disable the garbage collector */
2976
+ renderableGCActive: true,
2977
+ /** Time in ms before an unused resource is collected (default 1 minute) */
2978
+ renderableGCMaxUnusedTime: 6e4,
2979
+ /** How often to run garbage collection in ms (default 30 seconds) */
2980
+ renderableGCFrequency: 3e4
2981
+ };
2982
+ let RenderableGCSystem = _RenderableGCSystem;
2983
+
2984
+ const _TextureGCSystem = class _TextureGCSystem {
2985
+ /** @param renderer - The renderer this System works for. */
2986
+ constructor(renderer) {
2987
+ this._renderer = renderer;
2988
+ this.count = 0;
2989
+ this.checkCount = 0;
2990
+ }
2991
+ init(options) {
2992
+ options = { ..._TextureGCSystem.defaultOptions, ...options };
2993
+ this.checkCountMax = options.textureGCCheckCountMax;
2994
+ this.maxIdle = options.textureGCAMaxIdle ?? options.textureGCMaxIdle;
2995
+ this.active = options.textureGCActive;
2996
+ }
2997
+ /**
2998
+ * Checks to see when the last time a texture was used.
2999
+ * If the texture has not been used for a specified amount of time, it will be removed from the GPU.
3000
+ */
3001
+ postrender() {
3002
+ if (!this._renderer.renderingToScreen) {
3003
+ return;
3004
+ }
3005
+ this.count++;
3006
+ if (!this.active)
3007
+ return;
3008
+ this.checkCount++;
3009
+ if (this.checkCount > this.checkCountMax) {
3010
+ this.checkCount = 0;
3011
+ this.run();
3012
+ }
3013
+ }
3014
+ /**
3015
+ * Checks to see when the last time a texture was used.
3016
+ * If the texture has not been used for a specified amount of time, it will be removed from the GPU.
3017
+ */
3018
+ run() {
3019
+ const managedTextures = this._renderer.texture.managedTextures;
3020
+ for (let i = 0; i < managedTextures.length; i++) {
3021
+ const texture = managedTextures[i];
3022
+ if (texture.autoGarbageCollect && texture.resource && texture._touched > -1 && this.count - texture._touched > this.maxIdle) {
3023
+ texture._touched = -1;
3024
+ texture.unload();
3025
+ }
3026
+ }
3027
+ }
3028
+ destroy() {
3029
+ this._renderer = null;
3030
+ }
3031
+ };
3032
+ /** @ignore */
3033
+ _TextureGCSystem.extension = {
3034
+ type: [
3035
+ ExtensionType.WebGLSystem,
3036
+ ExtensionType.WebGPUSystem
3037
+ ],
3038
+ name: "textureGC"
3039
+ };
3040
+ /** default options for the TextureGCSystem */
3041
+ _TextureGCSystem.defaultOptions = {
3042
+ /**
3043
+ * If set to true, this will enable the garbage collector on the GPU.
3044
+ * @default true
3045
+ */
3046
+ textureGCActive: true,
3047
+ /**
3048
+ * @deprecated since 8.3.0
3049
+ * @see {@link TextureGCSystem.textureGCMaxIdle}
3050
+ */
3051
+ textureGCAMaxIdle: null,
3052
+ /**
3053
+ * The maximum idle frames before a texture is destroyed by garbage collection.
3054
+ * @default 60 * 60
3055
+ */
3056
+ textureGCMaxIdle: 60 * 60,
3057
+ /**
3058
+ * Frames between two garbage collections.
3059
+ * @default 600
3060
+ */
3061
+ textureGCCheckCountMax: 600
3062
+ };
3063
+ let TextureGCSystem = _TextureGCSystem;
3064
+
3065
+ const _ViewSystem = class _ViewSystem {
3066
+ /**
3067
+ * Whether CSS dimensions of canvas view should be resized to screen dimensions automatically.
3068
+ * This is only supported for HTMLCanvasElement and will be ignored if the canvas is an OffscreenCanvas.
3069
+ * @member {boolean}
3070
+ */
3071
+ get autoDensity() {
3072
+ return this.texture.source.autoDensity;
3073
+ }
3074
+ set autoDensity(value) {
3075
+ this.texture.source.autoDensity = value;
3076
+ }
3077
+ /** The resolution / device pixel ratio of the renderer. */
3078
+ get resolution() {
3079
+ return this.texture.source._resolution;
3080
+ }
3081
+ set resolution(value) {
3082
+ this.texture.source.resize(
3083
+ this.texture.source.width,
3084
+ this.texture.source.height,
3085
+ value
3086
+ );
3087
+ }
3088
+ /**
3089
+ * initiates the view system
3090
+ * @param options - the options for the view
3091
+ */
3092
+ init(options) {
3093
+ options = {
3094
+ ..._ViewSystem.defaultOptions,
3095
+ ...options
3096
+ };
3097
+ if (options.view) {
3098
+ deprecation(v8_0_0, "ViewSystem.view has been renamed to ViewSystem.canvas");
3099
+ options.canvas = options.view;
3100
+ }
3101
+ this.screen = new Rectangle(0, 0, options.width, options.height);
3102
+ this.canvas = options.canvas || DOMAdapter.get().createCanvas();
3103
+ this.antialias = !!options.antialias;
3104
+ this.texture = getCanvasTexture(this.canvas, options);
3105
+ this.renderTarget = new RenderTarget({
3106
+ colorTextures: [this.texture],
3107
+ depth: !!options.depth,
3108
+ isRoot: true
3109
+ });
3110
+ this.texture.source.transparent = options.backgroundAlpha < 1;
3111
+ this.resolution = options.resolution;
3112
+ }
3113
+ /**
3114
+ * Resizes the screen and canvas to the specified dimensions.
3115
+ * @param desiredScreenWidth - The new width of the screen.
3116
+ * @param desiredScreenHeight - The new height of the screen.
3117
+ * @param resolution
3118
+ */
3119
+ resize(desiredScreenWidth, desiredScreenHeight, resolution) {
3120
+ this.texture.source.resize(desiredScreenWidth, desiredScreenHeight, resolution);
3121
+ this.screen.width = this.texture.frame.width;
3122
+ this.screen.height = this.texture.frame.height;
3123
+ }
3124
+ /**
3125
+ * Destroys this System and optionally removes the canvas from the dom.
3126
+ * @param {options | false} options - The options for destroying the view, or "false".
3127
+ * @param options.removeView - Whether to remove the view element from the DOM. Defaults to `false`.
3128
+ */
3129
+ destroy(options = false) {
3130
+ const removeView = typeof options === "boolean" ? options : !!options?.removeView;
3131
+ if (removeView && this.canvas.parentNode) {
3132
+ this.canvas.parentNode.removeChild(this.canvas);
3133
+ }
3134
+ }
3135
+ };
3136
+ /** @ignore */
3137
+ _ViewSystem.extension = {
3138
+ type: [
3139
+ ExtensionType.WebGLSystem,
3140
+ ExtensionType.WebGPUSystem,
3141
+ ExtensionType.CanvasSystem
3142
+ ],
3143
+ name: "view",
3144
+ priority: 0
3145
+ };
3146
+ /** The default options for the view system. */
3147
+ _ViewSystem.defaultOptions = {
3148
+ /**
3149
+ * {@link WebGLOptions.width}
3150
+ * @default 800
3151
+ */
3152
+ width: 800,
3153
+ /**
3154
+ * {@link WebGLOptions.height}
3155
+ * @default 600
3156
+ */
3157
+ height: 600,
3158
+ /**
3159
+ * {@link WebGLOptions.autoDensity}
3160
+ * @default false
3161
+ */
3162
+ autoDensity: false,
3163
+ /**
3164
+ * {@link WebGLOptions.antialias}
3165
+ * @default false
3166
+ */
3167
+ antialias: false
3168
+ };
3169
+ let ViewSystem = _ViewSystem;
3170
+
3171
+ const SharedSystems = [
3172
+ BackgroundSystem,
3173
+ GlobalUniformSystem,
3174
+ HelloSystem,
3175
+ ViewSystem,
3176
+ RenderGroupSystem,
3177
+ TextureGCSystem,
3178
+ GenerateTextureSystem,
3179
+ ExtractSystem,
3180
+ RendererInitHook,
3181
+ RenderableGCSystem,
3182
+ SchedulerSystem
3183
+ ];
3184
+ const SharedRenderPipes = [
3185
+ BlendModePipe,
3186
+ BatcherPipe,
3187
+ SpritePipe,
3188
+ RenderGroupPipe,
3189
+ AlphaMaskPipe,
3190
+ StencilMaskPipe,
3191
+ ColorMaskPipe,
3192
+ CustomRenderPipe
3193
+ ];
3194
+
3195
+ export { BufferResource as B, GpuStencilModesToPixi as G, RenderTargetSystem as R, SharedSystems as S, UboSystem as U, SharedRenderPipes as a, uboSyncFunctionsSTD40 as b, createUboSyncFunction as c, uniformParsers as d, ensureAttributes as e, textureBitGl as f, textureBit as t, uboSyncFunctionsWGSL as u };