rapid-render 1.0.26 → 1.0.28

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -101,80 +101,48 @@ stack.restore(); // 1.root
101
101
  rapid.drawSprite(ui);
102
102
  ```
103
103
 
104
- ## Use Saved World Matrices
104
+ ## Reuse MatrixStack Transforms
105
105
 
106
- `save()` returns the current `parent`, `local`, and `world` matrix IDs. The IDs can be used with `customMatrix` after their stack scope has been restored, as long as they belong to the current frame.
107
-
108
- ```ts
109
- rapid.clear();
110
-
111
- const stack = rapid.matrixStack;
112
- // Build the hierarchy for this frame.
113
- const world = stack.save();
114
- stack.translate(200, 200);
115
-
116
- const enemyNode = stack.save();
117
- stack.translate(80, 0);
118
-
119
- stack.restore(); // enemyNode
120
- stack.restore(); // world
121
-
122
- // The stack has been restored, but enemyNode.world is still available during
123
- // this frame. Build the hierarchy again next frame with the latest game state.
124
- rapid.drawSprite({
125
- texture: enemy,
126
- customMatrix: enemyNode.world,
127
- });
128
-
129
- rapid.flush();
130
- ```
131
-
132
- ## Cache MatrixStack Transforms
133
-
134
- Use `retainMatrix()` to keep a matrix from the current stack across `rapid.clear()` / `stack.reset()`. It returns the supplied matrix ID or save state. Pass cached IDs to `save(world, local)` to reuse them directly:
106
+ `save()` returns `parent`, `local`, and `world` matrix IDs. Saved world IDs can be used with `customMatrix` within the current frame. Use `retainMatrix()` to cache transforms across frames and reuse them with `save(cached)` or `cached.world`:
135
107
 
136
108
  ```ts
137
109
  const stack = rapid.matrixStack;
110
+ const matrix = rapid.matrix;
138
111
 
139
- // Calculate and retain the transform once.
140
- stack.save();
141
- stack.translate(100, 80);
142
- const world = stack.retainMatrix(stack.curWorldM);
143
- const local = stack.retainMatrix(stack.curLocalM);
112
+ const cached = stack.retainMatrix(stack.save());
113
+ matrix.translate(cached.local, 100, 80);
114
+ matrix.multiplyOut(cached.world, cached.parent, cached.local);
144
115
  stack.restore();
145
116
 
146
117
  function drawFrame() {
147
118
  rapid.clear();
148
- stack.save(world, local);
119
+ stack.save(cached);
149
120
  rapid.drawSprite({ texture });
150
121
  stack.restore();
122
+ rapid.drawSprite({ texture, customMatrix: cached.world });
151
123
  rapid.flush();
152
124
  }
153
125
  ```
154
126
 
155
- Alternatively, `const cached = stack.retainMatrix(stack.save())` retains both matrices; reuse them with `stack.save(cached.world, cached.local)`.
156
-
157
- `save()` accepts either or both IDs:
127
+ `save()` accepts a saved state or individual matrix IDs:
158
128
 
159
129
  ```ts
160
- stack.save(); // Identity local; world inherits the parent.
161
- stack.save(world, local); // Use both IDs directly without allocating matrices.
162
- stack.save(world); // Use world; create an identity local.
163
- stack.save(undefined, local); // Use local; calculate world = parent world * local.
130
+ stack.save(); // Identity local; inherit parent world.
131
+ stack.save(cached); // Reuse the saved world and local IDs.
132
+ stack.save(cached.world, cached.local); // Pass both IDs explicitly.
133
+ stack.save(cached.world); // Create an identity local.
134
+ stack.save(undefined, cached.local); // Calculate world = parent world * local.
164
135
  ```
165
136
 
166
137
  Matrices retained with `stack.retainMatrix()` or created with `rapid.matrix.alloc()` are managed by you. Release them when no longer needed:
167
138
 
168
139
  ```ts
169
- rapid.matrix.free(world);
170
- rapid.matrix.free(local);
140
+ rapid.matrix.free(cached);
171
141
 
172
142
  const m = rapid.matrix.alloc();
173
143
  rapid.matrix.free(m);
174
144
  ```
175
145
 
176
- With this flexible matrix stack, you can build your own architecture with minimal friction. It doesn't care how you organize your game logic. You can use ECS, scene graphs, components, or any hybrid approach you prefer.
177
-
178
146
  For more information about matrix transformations, see the [Transformations](https://nightre.github.io/Rapid.js/docs.html#transformations).
179
147
 
180
148
  ## Benchmark
@@ -20,6 +20,7 @@ export interface ITransformOptions {
20
20
  * Matrices are stored flat in a dynamic Float32Array to improve memory locality and cache performance.
21
21
  */
22
22
  export declare class MatrixStore {
23
+ private readonly transformValues;
23
24
  /** Number of matrix slots created, including released slots available for reuse. */
24
25
  matrixCount: number;
25
26
  /** The dynamic buffer used to hold matrix data. */
@@ -48,7 +49,7 @@ export declare class MatrixStore {
48
49
  */
49
50
  allocDirty(): number;
50
51
  /** Releases a matrix index so it can be reused by a later allocation. */
51
- free(index: number): void;
52
+ free(index: number | MatrixSaveState): void;
52
53
  /**
53
54
  * Resets the store, clearing all allocated matrices.
54
55
  */
@@ -212,6 +213,8 @@ export declare class MatrixStore {
212
213
  multiplyAffine(indexIn: number, indexOut: number, a: number, b: number, c: number, dValue: number, tx: number, ty: number): void;
213
214
  clampBounds(index: number, viewWidth: number, viewHeight: number, limitLeft?: number, limitRight?: number, limitTop?: number, limitBottom?: number): void;
214
215
  applyTransform(transform: ITransformOptions, width?: number, height?: number): number[];
216
+ /** Numeric results reuse an internal array; consume them before the next call. */
217
+ applyTransform(x: number, y: number, scaleX?: number, scaleY?: number, rotation?: number, originX?: number, originY?: number, width?: number, height?: number, offsetX?: number, offsetY?: number): number[];
215
218
  }
216
219
  export interface MatrixSaveState {
217
220
  world: number;
@@ -242,11 +245,11 @@ export declare class MatrixStack {
242
245
  * Equivalent to context.save().
243
246
  * Provided matrix IDs are used directly and remain caller-owned; only matrices
244
247
  * created by this call are queued for recycling by reset().
245
- * @param matrixWorld Optional world matrix ID from this store; defaults to parent * local.
248
+ * @param matrixWorld Optional world matrix ID or saved state from this store; defaults to parent * local.
246
249
  * @param matrixLocal Optional local matrix ID from this store; defaults to identity.
247
250
  * @returns Matrix IDs for the saved parent, local, and world transforms.
248
251
  */
249
- save(matrixWorld?: number, matrixLocal?: number): MatrixSaveState;
252
+ save(matrixWorld?: number | MatrixSaveState, matrixLocal?: number): MatrixSaveState;
250
253
  /**
251
254
  * Restores the matrix state from the top of the stack.
252
255
  * Equivalent to context.restore().
@@ -365,4 +368,5 @@ export declare class MatrixStack {
365
368
  y: number;
366
369
  };
367
370
  applyTransform(transform: ITransformOptions, width?: number, height?: number, customMatrix?: number, worldMatrix?: number, localMatrix?: number): void;
371
+ applyTransform(x: number, y: number, scaleX?: number, scaleY?: number, rotation?: number, originX?: number, originY?: number, width?: number, height?: number, offsetX?: number, offsetY?: number, customMatrix?: number, worldMatrix?: number, localMatrix?: number): void;
368
372
  }
@@ -179,7 +179,7 @@ class Ct {
179
179
  return this.addTexture(new W(r));
180
180
  try {
181
181
  const i = await this._fetchImage(t);
182
- return r = z.fromSource(this.render, i, e), r.uid = t, this.cache.set(t, r), this.addTexture(new W(r));
182
+ return r = Y.fromSource(this.render, i, e), r.uid = t, this.cache.set(t, r), this.addTexture(new W(r));
183
183
  } catch (i) {
184
184
  throw console.error(`[TextureManager] Failed to load: ${t}`, i), i;
185
185
  }
@@ -187,7 +187,7 @@ class Ct {
187
187
  create(t, e) {
188
188
  if (e?.key && this.cache.has(e.key))
189
189
  return this.addTexture(new W(this.cache.get(e.key)));
190
- const r = z.fromSource(this.render, t, e);
190
+ const r = Y.fromSource(this.render, t, e);
191
191
  return e?.key && (r.uid = e.key, this.cache.set(e.key, r)), this.addTexture(new W(r));
192
192
  }
193
193
  createRenderTexture(t) {
@@ -212,7 +212,7 @@ class Ct {
212
212
  });
213
213
  }
214
214
  }
215
- class z {
215
+ class Y {
216
216
  glTexture = null;
217
217
  width;
218
218
  height;
@@ -223,7 +223,7 @@ class z {
223
223
  }
224
224
  static fromSource(t, e, r = {}) {
225
225
  const i = nt(t, e, r);
226
- return new z(i, e.width, e.height);
226
+ return new Y(i, e.width, e.height);
227
227
  }
228
228
  updateSource(t, e, r = {}) {
229
229
  this.glTexture && (t.bindTexture(t.TEXTURE_2D, this.glTexture), r.textureFilter !== void 0 && ht(t, r.textureFilter), r.wrap !== void 0 && ot(t, r.wrap), t.pixelStorei(t.UNPACK_FLIP_Y_WEBGL, 1), t.pixelStorei(t.UNPACK_ALIGNMENT, 1), r.premultipliedAlpha !== void 0 && t.pixelStorei(t.UNPACK_PREMULTIPLY_ALPHA_WEBGL, r.premultipliedAlpha), this.width === e.width && this.height === e.height ? t.texSubImage2D(t.TEXTURE_2D, 0, 0, 0, t.RGBA, t.UNSIGNED_BYTE, e) : (t.texImage2D(t.TEXTURE_2D, 0, t.RGBA, t.RGBA, t.UNSIGNED_BYTE, e), this.width = e.width, this.height = e.height), r.premultipliedAlpha !== void 0 && t.pixelStorei(t.UNPACK_PREMULTIPLY_ALPHA_WEBGL, !1));
@@ -292,7 +292,7 @@ class W {
292
292
  }
293
293
  static fromImageSource(t, e, r = {}) {
294
294
  const i = nt(t, e, r);
295
- return new W(new z(i, e.width, e.height));
295
+ return new W(new Y(i, e.width, e.height));
296
296
  }
297
297
  destroy() {
298
298
  this.base && (this.base.refCount--, this.base = void 0), this.glTexture = null;
@@ -309,7 +309,7 @@ class _t extends W {
309
309
  constructor(t, e) {
310
310
  super(), this.gl = t.gl, this.premultipliedAlpha = t.premultipliedAlpha, this.clearColor = e.clearColor ?? g.Black;
311
311
  const r = Math.max(Math.round(e.width), 1), i = Math.max(Math.round(e.height), 1), a = nt(t, { width: r, height: i }, { ...e, onlySize: !0 });
312
- if (this.setBase(new z(a, r, i)), this.framebuffer = this.gl.createFramebuffer(), this.renderbuffer = this.gl.createRenderbuffer(), !this.framebuffer || !this.renderbuffer)
312
+ if (this.setBase(new Y(a, r, i)), this.framebuffer = this.gl.createFramebuffer(), this.renderbuffer = this.gl.createRenderbuffer(), !this.framebuffer || !this.renderbuffer)
313
313
  throw new Error("Failed to create Framebuffer or Renderbuffer");
314
314
  this.resize(r, i, !0);
315
315
  }
@@ -381,7 +381,7 @@ class Lt extends W {
381
381
  if (!r) throw new Error("Failed to get 2d context for TextTexture");
382
382
  this.ctx = r, this.canvas.width = 1, this.canvas.height = 1;
383
383
  const i = nt(t, this.canvas, this.options);
384
- this.setBase(new z(i, 1, 1)), this.updateResolution(), this.update();
384
+ this.setBase(new Y(i, 1, 1)), this.updateResolution(), this.update();
385
385
  }
386
386
  get text() {
387
387
  return this._text;
@@ -411,30 +411,30 @@ class Lt extends W {
411
411
  }
412
412
  update() {
413
413
  const t = this.ctx, e = this.style, r = e.fontSize, i = e.fontWeight, s = e.fontFamily, a = e.lineHeight, h = Math.max(r, 32), c = r / h, u = `${i} ${r}px ${s}`, l = `${i} ${h}px ${s}`, d = this.resolution, f = e.baseline ?? "top", m = e.align ?? "left", x = e.strokeThickness ?? 0, y = x > 0, v = this._text.split(`
414
- `), T = "国g", w = v[0] || T;
414
+ `), w = "国g", R = v[0] || w;
415
415
  t.font = l, t.textBaseline = "alphabetic", t.textAlign = "left";
416
- let U = 0, P = 0, O = 0;
416
+ let U = 0, C = 0, I = 0;
417
417
  for (const B of v) {
418
- const X = t.measureText(B || T);
419
- U = Math.max(U, B ? X.width * c : 0), B == v.at(0) && (P = X.actualBoundingBoxAscent * c), B == v.at(-1) && (O = X.actualBoundingBoxDescent * c);
418
+ const X = t.measureText(B || w);
419
+ U = Math.max(U, B ? X.width * c : 0), B == v.at(0) && (C = X.actualBoundingBoxAscent * c), B == v.at(-1) && (I = X.actualBoundingBoxDescent * c);
420
420
  }
421
421
  t.textBaseline = "alphabetic";
422
- const L = t.measureText(w).fontBoundingBoxDescent * c;
422
+ const b = t.measureText(R).fontBoundingBoxDescent * c;
423
423
  t.textBaseline = f;
424
- const S = t.measureText(w).fontBoundingBoxDescent * c, E = r * a, b = P + O + (v.length - 1) * E, I = x / 2 + 2 / d, k = U + I * 2, G = b + I * 2, N = Math.max(1, Math.ceil(k * d)), F = Math.max(1, Math.ceil(G * d)), V = N / d;
425
- (this.canvas.width !== N || this.canvas.height !== F) && (this.canvas.width = N, this.canvas.height = F), t.setTransform(d, 0, 0, -d, 0, F), t.clearRect(
424
+ const T = t.measureText(R).fontBoundingBoxDescent * c, E = r * a, M = C + I + (v.length - 1) * E, F = x / 2 + 2 / d, k = U + F * 2, G = M + F * 2, N = Math.max(1, Math.ceil(k * d)), O = Math.max(1, Math.ceil(G * d)), V = N / d;
425
+ (this.canvas.width !== N || this.canvas.height !== O) && (this.canvas.width = N, this.canvas.height = O), t.setTransform(d, 0, 0, -d, 0, O), t.clearRect(
426
426
  0,
427
427
  0,
428
428
  N / d,
429
- F / d
429
+ O / d
430
430
  ), t.font = u, t.textBaseline = "alphabetic", t.textAlign = m;
431
- const H = this.updateOffset(V, I);
431
+ const H = this.updateOffset(V, F);
432
432
  y && (t.lineWidth = x, t.strokeStyle = e.stroke ?? "#000000", t.lineJoin = "round"), e.fill && (t.fillStyle = e.fill);
433
- let A = I + P;
433
+ let _ = F + C;
434
434
  for (const B of v)
435
- y && t.strokeText(B, H, A), e.fill && t.fillText(B, H, A), A += E;
436
- const $ = S - L;
437
- this.scale = 1 / this.resolution, this.offsetX = -H, this.offsetY = -I - P + $, this.base?.updateSource(this.render.gl, this.canvas, this.options), this.setRegion(0, 0, N, F);
435
+ y && t.strokeText(B, H, _), e.fill && t.fillText(B, H, _), _ += E;
436
+ const $ = T - b;
437
+ this.scale = 1 / this.resolution, this.offsetX = -H, this.offsetY = -F - C + $, this.base?.updateSource(this.render.gl, this.canvas, this.options), this.setRegion(0, 0, N, O);
438
438
  }
439
439
  }
440
440
  const Nt = (n, t = !1, e = !0) => {
@@ -803,8 +803,8 @@ class Rt {
803
803
  return !1;
804
804
  }
805
805
  }
806
- var C = /* @__PURE__ */ ((n) => (n[n.Float32 = 0] = "Float32", n[n.Uint32 = 1] = "Uint32", n[n.Uint16 = 2] = "Uint16", n))(C || {});
807
- class _ {
806
+ var P = /* @__PURE__ */ ((n) => (n[n.Float32 = 0] = "Float32", n[n.Uint32 = 1] = "Uint32", n[n.Uint16 = 2] = "Uint16", n))(P || {});
807
+ class L {
808
808
  usedElemNum;
809
809
  typedArray;
810
810
  arrayType;
@@ -903,7 +903,7 @@ class _ {
903
903
  return this.usedElemNum;
904
904
  }
905
905
  }
906
- class lt extends _ {
906
+ class lt extends L {
907
907
  buffer;
908
908
  gl;
909
909
  dirty = !0;
@@ -1027,9 +1027,9 @@ class dt extends Rt {
1027
1027
  }
1028
1028
  createBuffer() {
1029
1029
  const t = this.gl;
1030
- this.quadBuffer = new lt(t, C.Float32, t.ARRAY_BUFFER, t.STATIC_DRAW), this.quadBuffer.bindBuffer();
1030
+ this.quadBuffer = new lt(t, P.Float32, t.ARRAY_BUFFER, t.STATIC_DRAW), this.quadBuffer.bindBuffer();
1031
1031
  for (const e of kt) this.quadBuffer.pushFloat32(e);
1032
- this.quadBuffer.makeDirty(), this.quadBuffer.bufferData(), this.instanceBuffer = new lt(t, C.Uint32, t.ARRAY_BUFFER, t.DYNAMIC_DRAW);
1032
+ this.quadBuffer.makeDirty(), this.quadBuffer.bufferData(), this.instanceBuffer = new lt(t, P.Uint32, t.ARRAY_BUFFER, t.DYNAMIC_DRAW);
1033
1033
  }
1034
1034
  createDefaultShader() {
1035
1035
  return this.vs = st, this.fs = mt, this.defaultShader = this.createShader(st, mt), this.defaultShader;
@@ -1069,16 +1069,16 @@ class dt extends Rt {
1069
1069
  );
1070
1070
  }
1071
1071
  drawSpriteAffine(t, e, r, i, s, a, o, h = 0, c = 0, u = 1, l = 1, d = 4294967295, f = 0, m = 0, x = !1, y = !1, v = !1) {
1072
- const T = t.base, w = t.rawWidth, U = t.rawHeight, P = t.offsetX, O = t.offsetY, L = this.useTexture(
1072
+ const w = t.base, R = t.rawWidth, U = t.rawHeight, C = t.offsetX, I = t.offsetY, b = this.useTexture(
1073
1073
  t.glTexture,
1074
- f / T.width,
1075
- m / T.height
1076
- ), S = this.instanceBuffer, E = S.usedElemNum;
1077
- S.resize(12);
1078
- const b = S.float32, I = S.uint32, k = v ? U : w, G = v ? w : U, N = k + 2 * f, F = G + 2 * m, V = P - f, H = O - m, R = this.localSpriteMatrix;
1079
- v ? (R[0] = 0, R[1] = y ? F : -F, R[2] = x ? -N : N, R[3] = 0, R[4] = V + (x ? N : 0), R[5] = H + (y ? 0 : F)) : (R[0] = x ? -N : N, R[1] = 0, R[2] = 0, R[3] = y ? -F : F, R[4] = V + (x ? N : 0), R[5] = H + (y ? F : 0));
1080
- const A = this.worldSpriteMatrix;
1081
- A[0] = e * R[0] + i * R[1], A[1] = r * R[0] + s * R[1], A[2] = e * R[2] + i * R[3], A[3] = r * R[2] + s * R[3], A[4] = a + e * R[4] + i * R[5], A[5] = o + r * R[4] + s * R[5], b[E + 6] = h, b[E + 7] = c, b[E + 8] = u, b[E + 9] = l, I[E + 10] = d, I[E + 11] = L, this.rapid.roundPixels && (A[4] = Math.round(A[4]), A[5] = Math.round(A[5])), b[E] = A[0], b[E + 1] = A[2], b[E + 2] = A[4], b[E + 3] = A[1], b[E + 4] = A[3], b[E + 5] = A[5], S.usedElemNum += 12, S.makeDirty(), this.instanceCount++;
1074
+ f / w.width,
1075
+ m / w.height
1076
+ ), T = this.instanceBuffer, E = T.usedElemNum;
1077
+ T.resize(12);
1078
+ const M = T.float32, F = T.uint32, k = v ? U : R, G = v ? R : U, N = k + 2 * f, O = G + 2 * m, V = C - f, H = I - m, S = this.localSpriteMatrix;
1079
+ v ? (S[0] = 0, S[1] = y ? O : -O, S[2] = x ? -N : N, S[3] = 0, S[4] = V + (x ? N : 0), S[5] = H + (y ? 0 : O)) : (S[0] = x ? -N : N, S[1] = 0, S[2] = 0, S[3] = y ? -O : O, S[4] = V + (x ? N : 0), S[5] = H + (y ? O : 0));
1080
+ const _ = this.worldSpriteMatrix;
1081
+ _[0] = e * S[0] + i * S[1], _[1] = r * S[0] + s * S[1], _[2] = e * S[2] + i * S[3], _[3] = r * S[2] + s * S[3], _[4] = a + e * S[4] + i * S[5], _[5] = o + r * S[4] + s * S[5], M[E + 6] = h, M[E + 7] = c, M[E + 8] = u, M[E + 9] = l, F[E + 10] = d, F[E + 11] = b, this.rapid.roundPixels && (_[4] = Math.round(_[4]), _[5] = Math.round(_[5])), M[E] = _[0], M[E + 1] = _[2], M[E + 2] = _[4], M[E + 3] = _[1], M[E + 4] = _[3], M[E + 5] = _[5], T.usedElemNum += 12, T.makeDirty(), this.instanceCount++;
1082
1082
  }
1083
1083
  render() {
1084
1084
  if (this.instanceCount === 0) return;
@@ -1093,8 +1093,9 @@ class dt extends Rt {
1093
1093
  return this.instanceCount > 0;
1094
1094
  }
1095
1095
  }
1096
- const M = 6;
1096
+ const A = 6;
1097
1097
  class Gt {
1098
+ transformValues = [1, 0, 0, 1, 0, 0];
1098
1099
  matrixCount = 0;
1099
1100
  buffer;
1100
1101
  data;
@@ -1103,7 +1104,7 @@ class Gt {
1103
1104
  temporary = -1;
1104
1105
  identityMatrix = -1;
1105
1106
  constructor(t = 10) {
1106
- this.buffer = new _(C.Float32), this.buffer.resize(t * M), this.data = this.buffer.getArray(), this.freeFlag = new _(C.Uint32), this.freeMatrixStack = new _(C.Uint32), this.reset();
1107
+ this.buffer = new L(P.Float32), this.buffer.resize(t * A), this.data = this.buffer.getArray(), this.freeFlag = new L(P.Uint32), this.freeMatrixStack = new L(P.Uint32), this.reset();
1107
1108
  }
1108
1109
  alloc() {
1109
1110
  const t = this.allocDirty();
@@ -1114,48 +1115,52 @@ class Gt {
1114
1115
  const e = this.freeMatrixStack.pop();
1115
1116
  return this.freeFlag.typedArray[e] = 1, e;
1116
1117
  }
1117
- this.buffer.resize(M) && (this.data = this.buffer.getArray()), this.buffer.usedElemNum += M, this.freeFlag.resize(1);
1118
+ this.buffer.resize(A) && (this.data = this.buffer.getArray()), this.buffer.usedElemNum += A, this.freeFlag.resize(1);
1118
1119
  const t = this.matrixCount++;
1119
1120
  return this.freeFlag.typedArray[this.freeFlag.usedElemNum++] = 1, t;
1120
1121
  }
1121
1122
  free(t) {
1123
+ if (typeof t != "number") {
1124
+ this.free(t.local), this.free(t.world);
1125
+ return;
1126
+ }
1122
1127
  !Number.isInteger(t) || t < 0 || t >= this.matrixCount || this.freeFlag.typedArray[t] !== 0 && (this.freeFlag.typedArray[t] = 0, this.freeMatrixStack.push(t));
1123
1128
  }
1124
1129
  reset() {
1125
1130
  this.matrixCount = 0, this.buffer.usedElemNum = 0, this.freeFlag.clear(), this.freeMatrixStack.clear(), this.temporary = this.alloc(), this.identityMatrix = this.alloc();
1126
1131
  }
1127
1132
  identity(t) {
1128
- const e = t * M, r = this.data;
1133
+ const e = t * A, r = this.data;
1129
1134
  r[e] = 1, r[e + 1] = 0, r[e + 2] = 0, r[e + 3] = 1, r[e + 4] = 0, r[e + 5] = 0;
1130
1135
  }
1131
1136
  translate(t, e, r) {
1132
- const i = t * M, s = this.data;
1137
+ const i = t * A, s = this.data;
1133
1138
  s[i + 4] = s[i] * e + s[i + 2] * r + s[i + 4], s[i + 5] = s[i + 1] * e + s[i + 3] * r + s[i + 5];
1134
1139
  }
1135
1140
  scale(t, e, r) {
1136
- const i = t * M, s = this.data;
1141
+ const i = t * A, s = this.data;
1137
1142
  s[i] *= e, s[i + 1] *= e, s[i + 2] *= r, s[i + 3] *= r;
1138
1143
  }
1139
1144
  rotate(t, e) {
1140
- const r = t * M, i = this.data, s = Math.cos(e), a = Math.sin(e), o = i[r], h = i[r + 1], c = i[r + 2], u = i[r + 3];
1145
+ const r = t * A, i = this.data, s = Math.cos(e), a = Math.sin(e), o = i[r], h = i[r + 1], c = i[r + 2], u = i[r + 3];
1141
1146
  i[r] = o * s + c * a, i[r + 1] = h * s + u * a, i[r + 2] = o * -a + c * s, i[r + 3] = h * -a + u * s;
1142
1147
  }
1143
1148
  rotateWithOffset(t, e, r, i) {
1144
1149
  this.translate(t, r, i), this.rotate(t, e), this.translate(t, -r, -i);
1145
1150
  }
1146
1151
  copy(t, e) {
1147
- const r = t * M, i = e * M, s = this.data;
1152
+ const r = t * A, i = e * A, s = this.data;
1148
1153
  s[r] = s[i], s[r + 1] = s[i + 1], s[r + 2] = s[i + 2], s[r + 3] = s[i + 3], s[r + 4] = s[i + 4], s[r + 5] = s[i + 5];
1149
1154
  }
1150
1155
  multiply(t, e) {
1151
1156
  this.multiplyOut(t, t, e);
1152
1157
  }
1153
1158
  multiplyOut(t, e, r) {
1154
- const i = t * M, s = e * M, a = r * M, o = this.data, h = o[s], c = o[s + 1], u = o[s + 2], l = o[s + 3], d = o[s + 4], f = o[s + 5], m = o[a], x = o[a + 1], y = o[a + 2], v = o[a + 3], T = o[a + 4], w = o[a + 5];
1155
- o[i] = h * m + u * x, o[i + 1] = c * m + l * x, o[i + 2] = h * y + u * v, o[i + 3] = c * y + l * v, o[i + 4] = h * T + u * w + d, o[i + 5] = c * T + l * w + f;
1159
+ const i = t * A, s = e * A, a = r * A, o = this.data, h = o[s], c = o[s + 1], u = o[s + 2], l = o[s + 3], d = o[s + 4], f = o[s + 5], m = o[a], x = o[a + 1], y = o[a + 2], v = o[a + 3], w = o[a + 4], R = o[a + 5];
1160
+ o[i] = h * m + u * x, o[i + 1] = c * m + l * x, o[i + 2] = h * y + u * v, o[i + 3] = c * y + l * v, o[i + 4] = h * w + u * R + d, o[i + 5] = c * w + l * R + f;
1156
1161
  }
1157
1162
  invert(t) {
1158
- const e = t * M, r = this.data, i = r[e], s = r[e + 1], a = r[e + 2], o = r[e + 3], h = r[e + 4], c = r[e + 5];
1163
+ const e = t * A, r = this.data, i = r[e], s = r[e + 1], a = r[e + 2], o = r[e + 3], h = r[e + 4], c = r[e + 5];
1159
1164
  let u = i * o - s * a;
1160
1165
  if (!u) {
1161
1166
  this.identity(t);
@@ -1164,7 +1169,7 @@ class Gt {
1164
1169
  u = 1 / u, r[e] = o * u, r[e + 1] = -s * u, r[e + 2] = -a * u, r[e + 3] = i * u, r[e + 4] = (a * c - o * h) * u, r[e + 5] = (s * h - i * c) * u;
1165
1170
  }
1166
1171
  transformPoint(t, e, r) {
1167
- const i = t * M, s = this.data;
1172
+ const i = t * A, s = this.data;
1168
1173
  return {
1169
1174
  x: e * s[i] + r * s[i + 2] + s[i + 4],
1170
1175
  y: e * s[i + 1] + r * s[i + 3] + s[i + 5]
@@ -1180,31 +1185,31 @@ class Gt {
1180
1185
  return this.transformPoint(t, e, r);
1181
1186
  }
1182
1187
  getPosition(t) {
1183
- const e = t * M, r = this.data;
1188
+ const e = t * A, r = this.data;
1184
1189
  return { x: r[e + 4], y: r[e + 5] };
1185
1190
  }
1186
1191
  getScale(t) {
1187
- const e = t * M, r = this.data, i = Math.sqrt(r[e] * r[e] + r[e + 1] * r[e + 1]), s = Math.sqrt(r[e + 2] * r[e + 2] + r[e + 3] * r[e + 3]);
1192
+ const e = t * A, r = this.data, i = Math.sqrt(r[e] * r[e] + r[e + 1] * r[e + 1]), s = Math.sqrt(r[e + 2] * r[e + 2] + r[e + 3] * r[e + 3]);
1188
1193
  return { x: i, y: s };
1189
1194
  }
1190
1195
  getRotation(t) {
1191
- const e = t * M, r = this.data;
1196
+ const e = t * A, r = this.data;
1192
1197
  return Math.atan2(r[e + 1], r[e]);
1193
1198
  }
1194
1199
  toCSSMatrix(t, e = 1, r = 1) {
1195
- const i = t * M, s = this.data;
1200
+ const i = t * A, s = this.data;
1196
1201
  return `matrix(${s[i] * e}, ${s[i + 1] * r}, ${s[i + 2] * e}, ${s[i + 3] * r}, ${s[i + 4] * e}, ${s[i + 5] * r})`;
1197
1202
  }
1198
1203
  getMatrix(t) {
1199
- const e = t * M;
1200
- return this.data.slice(e, e + M);
1204
+ const e = t * A;
1205
+ return this.data.slice(e, e + A);
1201
1206
  }
1202
1207
  getMatrixRef(t) {
1203
- const e = t * M;
1204
- return this.data.subarray(e, e + M);
1208
+ const e = t * A;
1209
+ return this.data.subarray(e, e + A);
1205
1210
  }
1206
1211
  setMatrix(t, e) {
1207
- const r = t * M, i = this.data;
1212
+ const r = t * A, i = this.data;
1208
1213
  i[r] = e[0], i[r + 1] = e[1], i[r + 2] = e[2], i[r + 3] = e[3], i[r + 4] = e[4], i[r + 5] = e[5];
1209
1214
  }
1210
1215
  multiplyAffineInPlace(t, e, r, i, s, a, o) {
@@ -1218,36 +1223,31 @@ class Gt {
1218
1223
  clampBounds(t, e, r, i, s, a, o) {
1219
1224
  if (i === void 0 && s === void 0 && a === void 0 && o === void 0)
1220
1225
  return;
1221
- const h = t * M, c = this.data, u = Math.sqrt(c[h] * c[h] + c[h + 1] * c[h + 1]) || 1, l = Math.sqrt(c[h + 2] * c[h + 2] + c[h + 3] * c[h + 3]) || 1, d = e * 0.5 * u, f = r * 0.5 * l;
1226
+ const h = t * A, c = this.data, u = Math.sqrt(c[h] * c[h] + c[h + 1] * c[h + 1]) || 1, l = Math.sqrt(c[h + 2] * c[h + 2] + c[h + 3] * c[h + 3]) || 1, d = e * 0.5 * u, f = r * 0.5 * l;
1222
1227
  let m = c[h + 4], x = c[h + 5];
1223
1228
  i !== void 0 && s !== void 0 ? m = s - i < d * 2 ? (i + s) * 0.5 : Math.max(i + d, Math.min(m, s - d)) : i !== void 0 ? m = Math.max(m, i + d) : s !== void 0 && (m = Math.min(m, s - d)), a !== void 0 && o !== void 0 ? x = o - a < f * 2 ? (a + o) * 0.5 : Math.max(a + f, Math.min(x, o - f)) : a !== void 0 ? x = Math.max(x, a + f) : o !== void 0 && (x = Math.min(x, o - f)), c[h + 4] = m, c[h + 5] = x;
1224
1229
  }
1225
- applyTransform(t, e = 0, r = 0) {
1226
- let i = t.x ?? 0, s = t.y ?? 0;
1227
- t.position && (i += t.position.x, s += t.position.y);
1228
- let a = 1, o = 1;
1229
- const h = t.scale;
1230
- h !== void 0 && (typeof h == "number" ? (a = h, o = h) : (a = h.x, o = h.y));
1231
- const c = t.rotation ?? 0;
1232
- let u = t.offsetX ?? 0, l = t.offsetY ?? 0;
1233
- t.offset && (u += t.offset.x, l += t.offset.y);
1234
- const d = t.origin;
1235
- d !== void 0 && (typeof d == "number" ? (u -= d * e, l -= d * r) : (u -= d.x * e, l -= d.y * r));
1236
- const f = c ? Math.cos(c) : 1, m = c ? Math.sin(c) : 0, x = f * a, y = m * a, v = -m * o, T = f * o, w = i + x * u + v * l, U = s + y * u + T * l;
1237
- return [
1238
- x,
1239
- y,
1240
- v,
1241
- T,
1242
- w,
1243
- U
1244
- ];
1230
+ applyTransform(t, e = 0, r, i = 1, s = 0, a = 0, o = 0, h = 0, c = 0, u = 0, l = 0) {
1231
+ let d, f, m = r ?? 1;
1232
+ if (typeof t == "object") {
1233
+ h = e ?? 0, c = r ?? 0;
1234
+ const T = t;
1235
+ d = (T.x ?? 0) + (T.position?.x ?? 0), f = (T.y ?? 0) + (T.position?.y ?? 0);
1236
+ const E = T.scale;
1237
+ m = typeof E == "number" ? E : E?.x ?? 1, i = typeof E == "number" ? E : E?.y ?? 1, s = T.rotation ?? 0;
1238
+ const M = T.origin;
1239
+ a = typeof M == "number" ? M : M?.x ?? 0, o = typeof M == "number" ? M : M?.y ?? 0, u = (T.offsetX ?? 0) + (T.offset?.x ?? 0), l = (T.offsetY ?? 0) + (T.offset?.y ?? 0);
1240
+ } else
1241
+ d = t, f = e;
1242
+ u -= a * h, l -= o * c;
1243
+ const x = s ? Math.cos(s) : 1, y = s ? Math.sin(s) : 0, v = x * m, w = y * m, R = -y * i, U = x * i, C = d + v * u + R * l, I = f + w * u + U * l, b = this.transformValues;
1244
+ return b[0] = v, b[1] = w, b[2] = R, b[3] = U, b[4] = C, b[5] = I, typeof t == "number" ? b : b.slice();
1245
1245
  }
1246
1246
  }
1247
1247
  class Ht {
1248
1248
  matrix = new Gt();
1249
- stack = new _(C.Uint32);
1250
- currStack = new _(C.Uint32);
1249
+ stack = new L(P.Uint32);
1250
+ currStack = new L(P.Uint32);
1251
1251
  curLocalM = -1;
1252
1252
  curWorldM = -1;
1253
1253
  rapid;
@@ -1257,7 +1257,7 @@ class Ht {
1257
1257
  save(t, e) {
1258
1258
  this.stack.push(this.curLocalM), this.stack.push(this.curWorldM);
1259
1259
  const r = this.curWorldM;
1260
- return this.curLocalM = e ?? this.matrix.alloc(), this.curWorldM = t ?? this.matrix.allocDirty(), t === void 0 && (e === void 0 ? this.matrix.copy(this.curWorldM, r) : this.matrix.multiplyOut(this.curWorldM, r, this.curLocalM)), e === void 0 && this.currStack.push(this.curLocalM), t === void 0 && this.currStack.push(this.curWorldM), { parent: r, world: this.curWorldM, local: this.curLocalM };
1260
+ return t !== void 0 && typeof t != "number" && ([t, e] = [t?.world, t?.local]), this.curLocalM = e ?? this.matrix.alloc(), this.curWorldM = t ?? this.matrix.allocDirty(), t === void 0 && (e === void 0 ? this.matrix.copy(this.curWorldM, r) : this.matrix.multiplyOut(this.curWorldM, r, this.curLocalM)), e === void 0 && this.currStack.push(this.curLocalM), t === void 0 && this.currStack.push(this.curWorldM), { parent: r, world: this.curWorldM, local: this.curLocalM };
1261
1261
  }
1262
1262
  restore() {
1263
1263
  this.stack.length != 0 && (this.curWorldM = this.stack.pop(), this.curLocalM = this.stack.pop());
@@ -1272,7 +1272,7 @@ class Ht {
1272
1272
  ), r = [];
1273
1273
  for (let i = 0; i < this.currStack.length; i++)
1274
1274
  e.has(this.currStack.get(i)) && r.push(i);
1275
- return _.removeAtIndices(r, [this.currStack]), t;
1275
+ return L.removeAtIndices(r, [this.currStack]), t;
1276
1276
  }
1277
1277
  translate(t, e) {
1278
1278
  this.matrix.translate(this.curLocalM, t, e), this.matrix.translate(this.curWorldM, t, e);
@@ -1313,51 +1313,40 @@ class Ht {
1313
1313
  return this.matrix.toCSSMatrix(this.curWorldM);
1314
1314
  }
1315
1315
  getTransform() {
1316
- const t = this.curWorldM * M;
1317
- return this.matrix.data.slice(t, t + M);
1316
+ const t = this.curWorldM * A;
1317
+ return this.matrix.data.slice(t, t + A);
1318
1318
  }
1319
1319
  setTransform(t) {
1320
- const e = this.curWorldM * M, r = this.matrix.data;
1320
+ const e = this.curWorldM * A, r = this.matrix.data;
1321
1321
  r[e] = t[0], r[e + 1] = t[1], r[e + 2] = t[2], r[e + 3] = t[3], r[e + 4] = t[4], r[e + 5] = t[5];
1322
1322
  }
1323
1323
  transformPoint(t, e) {
1324
1324
  return this.matrix.transformPoint(this.curLocalM, t, e);
1325
1325
  }
1326
- applyTransform(t, e = 0, r = 0, i, s = this.curWorldM, a = this.curLocalM) {
1327
- const [
1328
- o,
1329
- h,
1330
- c,
1331
- u,
1332
- l,
1333
- d
1334
- ] = this.matrix.applyTransform(t, e, r);
1335
- i != null ? this.matrix.multiplyAffine(
1336
- s,
1326
+ applyTransform(t, e = 0, r, i, s, a, o = 0, h = 0, c = 0, u = 0, l = 0, d, f = this.curWorldM, m = this.curLocalM) {
1327
+ let x;
1328
+ typeof t == "object" ? (x = this.matrix.applyTransform(t, e, r), d = i, f = s ?? this.curWorldM, m = a ?? this.curLocalM) : x = this.matrix.applyTransform(
1329
+ t,
1330
+ e,
1331
+ r,
1337
1332
  i,
1338
- o,
1339
- h,
1340
- c,
1341
- u,
1342
- l,
1343
- d
1344
- ) : (this.matrix.multiplyAffineInPlace(
1345
- a,
1346
- o,
1347
- h,
1348
- c,
1349
- u,
1350
- l,
1351
- d
1352
- ), this.matrix.multiplyAffineInPlace(
1353
1333
  s,
1334
+ a,
1354
1335
  o,
1355
1336
  h,
1356
1337
  c,
1357
1338
  u,
1358
- l,
1359
- d
1360
- ));
1339
+ l
1340
+ );
1341
+ const [
1342
+ y,
1343
+ v,
1344
+ w,
1345
+ R,
1346
+ U,
1347
+ C
1348
+ ] = x;
1349
+ d !== void 0 ? this.matrix.multiplyAffine(f, d, y, v, w, R, U, C) : (this.matrix.multiplyAffineInPlace(m, y, v, w, R, U, C), this.matrix.multiplyAffineInPlace(f, y, v, w, R, U, C));
1361
1350
  }
1362
1351
  }
1363
1352
  const Vt = `#version 300 es\r
@@ -1438,7 +1427,7 @@ void main(void) {\r
1438
1427
  fragColor = sampleTexture(vRegion) * vColor;\r
1439
1428
  // CUSTOM_CODE_CALL\r
1440
1429
  }\r
1441
- `, Yt = `#version 300 es\r
1430
+ `, zt = `#version 300 es\r
1442
1431
  precision highp float;\r
1443
1432
  \r
1444
1433
  in vec2 aPosition;\r
@@ -1461,7 +1450,7 @@ void main(void) {\r
1461
1450
  );\r
1462
1451
  gl_Position = u_projection * position;\r
1463
1452
  }\r
1464
- `, zt = `#version 300 es\r
1453
+ `, Yt = `#version 300 es\r
1465
1454
  precision mediump float;\r
1466
1455
  \r
1467
1456
  in vec2 vRegion;\r
@@ -1496,7 +1485,7 @@ class $t extends Rt {
1496
1485
  }
1497
1486
  createBuffer() {
1498
1487
  const t = this.gl;
1499
- this.vertexBuffer = new lt(t, C.Float32, t.ARRAY_BUFFER, t.DYNAMIC_DRAW), this.maskShader = this.createMaskShader(Yt, zt);
1488
+ this.vertexBuffer = new lt(t, P.Float32, t.ARRAY_BUFFER, t.DYNAMIC_DRAW), this.maskShader = this.createMaskShader(zt, Yt);
1500
1489
  }
1501
1490
  createDefaultShader() {
1502
1491
  return this.vs = Vt, this.fs = Xt, this.defaultShader = this.createShader(this.vs, this.fs), this.defaultShader;
@@ -1742,16 +1731,16 @@ const at = 10, xt = (n, t, e, r, i, s) => {
1742
1731
  let c = 0;
1743
1732
  const u = n.texture?.rawWidth || 1, l = n.closed ? t.length : t.length - 1;
1744
1733
  for (let d = 0; d < l; d++) {
1745
- const f = t[d], m = e[d].normal, x = e[d].miters, y = (d + 1) % t.length, v = t[y], T = e[y].normal, w = e[y].miters, U = f.add(m.multiply(x * i)), P = f.subtract(m.multiply(x * i)), O = v.add(T.multiply(w * i)), L = v.subtract(T.multiply(w * i)), S = f.distanceTo(v);
1746
- let E = 0, b = 0;
1747
- h === 0 ? (E = c / r, b = (c + S) / r) : (E = c / u, b = E + S / u);
1748
- const I = new p(E, 0), k = new p(E, 1), G = new p(b, 0), N = new p(b, 1);
1749
- s.push(U), a.push(I), s.push(P), a.push(k), s.push(O), a.push(G), s.push(O), a.push(G), s.push(L), a.push(N), s.push(P), a.push(k), c += S;
1734
+ const f = t[d], m = e[d].normal, x = e[d].miters, y = (d + 1) % t.length, v = t[y], w = e[y].normal, R = e[y].miters, U = f.add(m.multiply(x * i)), C = f.subtract(m.multiply(x * i)), I = v.add(w.multiply(R * i)), b = v.subtract(w.multiply(R * i)), T = f.distanceTo(v);
1735
+ let E = 0, M = 0;
1736
+ h === 0 ? (E = c / r, M = (c + T) / r) : (E = c / u, M = E + T / u);
1737
+ const F = new p(E, 0), k = new p(E, 1), G = new p(M, 0), N = new p(M, 1);
1738
+ s.push(U), a.push(F), s.push(C), a.push(k), s.push(I), a.push(G), s.push(I), a.push(G), s.push(b), a.push(N), s.push(C), a.push(k), c += T;
1750
1739
  }
1751
1740
  if (o && !n.closed) {
1752
1741
  const d = t[0], f = e[0].normal, m = h === 0 ? r > 0 ? 1 / r : 0 : 1 / u, x = xt(d, f, i, !0, 0, m);
1753
1742
  s.push(...x.vertices), a.push(...x.uv);
1754
- const y = t[t.length - 1], v = e[t.length - 1].normal, T = h === 0 ? 1 : c / u, w = h === 0 ? r > 0 ? 1 / r : 0 : 1 / u, U = xt(y, v, i, !1, T, w);
1743
+ const y = t[t.length - 1], v = e[t.length - 1].normal, w = h === 0 ? 1 : c / u, R = h === 0 ? r > 0 ? 1 / r : 0 : 1 / u, U = xt(y, v, i, !1, w, R);
1755
1744
  s.push(...U.vertices), a.push(...U.uv);
1756
1745
  }
1757
1746
  return { vertices: s, uv: a };
@@ -1999,7 +1988,7 @@ void main(void) {\r
1999
1988
  \r
2000
1989
  // CUSTOM_CODE_CALL\r
2001
1990
  }\r
2002
- `, Y = 48, ue = Y / 4;
1991
+ `, z = 48, ue = z / 4;
2003
1992
  class le extends dt {
2004
1993
  KEY = "ParticleSprite";
2005
1994
  texture;
@@ -2019,42 +2008,42 @@ class le extends dt {
2019
2008
  createShader(t, e) {
2020
2009
  const r = super.createShader(t, e);
2021
2010
  return r.use(), r.bindVAO(), this.instanceBuffer.bindBuffer(), r.setAttributes([
2022
- { name: "aPosition", size: 2, stride: Y, offset: 0, divisor: 1 },
2023
- { name: "aScale", size: 2, stride: Y, offset: 8, divisor: 1 },
2024
- { name: "aRotation", size: 1, stride: Y, offset: 16, divisor: 1 },
2025
- { name: "aUVRect", size: 4, stride: Y, offset: 20, divisor: 1 },
2026
- { name: "aColor", size: 4, type: ct, normalized: !0, stride: Y, offset: 36, divisor: 1 },
2027
- { name: "aOrigin", size: 2, stride: Y, offset: 40, divisor: 1 }
2011
+ { name: "aPosition", size: 2, stride: z, offset: 0, divisor: 1 },
2012
+ { name: "aScale", size: 2, stride: z, offset: 8, divisor: 1 },
2013
+ { name: "aRotation", size: 1, stride: z, offset: 16, divisor: 1 },
2014
+ { name: "aUVRect", size: 4, stride: z, offset: 20, divisor: 1 },
2015
+ { name: "aColor", size: 4, type: ct, normalized: !0, stride: z, offset: 36, divisor: 1 },
2016
+ { name: "aOrigin", size: 2, stride: z, offset: 40, divisor: 1 }
2028
2017
  ]), this.quadBuffer.bindBuffer(), r.setAttributes([
2029
2018
  { name: "aVertex", size: 2, stride: 8, offset: 0, divisor: 0 }
2030
2019
  ]), r.unbindVAO(), this.currentShader && this.currentShader.use(), r;
2031
2020
  }
2032
- drawParticles(t, e, r, i = 0, s = 4294967295, a, o = 1, h = 1, c = 0, u = 0, l = 1, d = 1, f = !1, m = !1, x = !1, y = 0.5, v = 0.5, T, w = !1, U) {
2021
+ drawParticles(t, e, r, i = 0, s = 4294967295, a, o = 1, h = 1, c = 0, u = 0, l = 1, d = 1, f = !1, m = !1, x = !1, y = 0.5, v = 0.5, w, R = !1, U) {
2033
2022
  if (!t.glTexture || a <= 0) return;
2034
2023
  this.texture && t !== this.texture && this.flush();
2035
- const P = typeof o == "number", O = typeof h == "number", L = typeof i == "number", S = typeof s == "number", E = typeof c == "number", b = !S && typeof s[0] == "number", I = t.uvW - t.uvX, k = t.uvH - t.uvY, G = I === 0 ? 0 : 1 / I, N = k === 0 ? 0 : 1 / k;
2036
- let F = t.rawWidth * (f ? -1 : 1);
2037
- P && (F *= o);
2024
+ const C = typeof o == "number", I = typeof h == "number", b = typeof i == "number", T = typeof s == "number", E = typeof c == "number", M = !T && typeof s[0] == "number", F = t.uvW - t.uvX, k = t.uvH - t.uvY, G = F === 0 ? 0 : 1 / F, N = k === 0 ? 0 : 1 / k;
2025
+ let O = t.rawWidth * (f ? -1 : 1);
2026
+ C && (O *= o);
2038
2027
  let V = t.rawHeight * (m ? -1 : 1);
2039
- O && (V *= h), E && (F *= (l - c) * G, V *= (d - u) * N);
2028
+ I && (V *= h), E && (O *= (l - c) * G, V *= (d - u) * N);
2040
2029
  let H = x ? Math.PI / 2 : 0;
2041
- L && (H += i);
2042
- const R = s ?? 4294967295, A = this.getParticleLoop(
2043
- P,
2044
- O,
2045
- L,
2046
- E,
2047
- S,
2030
+ b && (H += i);
2031
+ const S = s ?? 4294967295, _ = this.getParticleLoop(
2032
+ C,
2033
+ I,
2048
2034
  b,
2049
- w,
2050
- T
2035
+ E,
2036
+ T,
2037
+ M,
2038
+ R,
2039
+ w
2051
2040
  );
2052
2041
  this.texture || (this.texture = t, this.useTexture(t.glTexture, 0, 0));
2053
2042
  const $ = a, B = this.instanceBuffer;
2054
2043
  let X = B.usedElemNum;
2055
2044
  B.resize($ * ue);
2056
2045
  const bt = B.float32, Ut = B.uint32;
2057
- X = A(
2046
+ X = _(
2058
2047
  e,
2059
2048
  r,
2060
2049
  o,
@@ -2067,12 +2056,12 @@ class le extends dt {
2067
2056
  s,
2068
2057
  bt,
2069
2058
  Ut,
2070
- F,
2059
+ O,
2071
2060
  V,
2072
2061
  G,
2073
2062
  N,
2074
2063
  H,
2075
- R,
2064
+ S,
2076
2065
  1 - y,
2077
2066
  1 - v,
2078
2067
  a,
@@ -2151,7 +2140,7 @@ const fe = {
2151
2140
  2: new p(0.5, 0.5)
2152
2141
  };
2153
2142
  class me {
2154
- constructor(t, e) {
2143
+ constructor(t, e = {}) {
2155
2144
  this.rapid = t, this.updateWindowOption(e);
2156
2145
  }
2157
2146
  viewLeft = 0;
@@ -2186,17 +2175,17 @@ class me {
2186
2175
  case 0:
2187
2176
  case 1:
2188
2177
  case 2: {
2189
- const T = y, w = v, U = Math.min(
2178
+ const w = y, R = v, U = Math.min(
2190
2179
  s.physicsWidth / t,
2191
2180
  s.physicsHeight / e
2192
- ), P = s.physicsWidth / U, O = s.physicsHeight / U, L = P - t, S = O - e;
2193
- if (u = -L * T.x, l = t + L * (1 - T.x), f = -S * T.y, d = e + S * (1 - T.y), !w.x && L > 0 || !w.y && S > 0) {
2194
- const E = w.x && L > 0 ? u : 0, b = w.x && L > 0 ? l : t, I = w.y && S > 0 ? f : 0, k = w.y && S > 0 ? d : e;
2181
+ ), C = s.physicsWidth / U, I = s.physicsHeight / U, b = C - t, T = I - e;
2182
+ if (u = -b * w.x, l = t + b * (1 - w.x), f = -T * w.y, d = e + T * (1 - w.y), !R.x && b > 0 || !R.y && T > 0) {
2183
+ const E = R.x && b > 0 ? u : 0, M = R.x && b > 0 ? l : t, F = R.y && T > 0 ? f : 0, k = R.y && T > 0 ? d : e;
2195
2184
  this.scissorViewport = {
2196
2185
  x: E,
2197
- y: I,
2198
- width: b - E,
2199
- height: k - I
2186
+ y: F,
2187
+ width: M - E,
2188
+ height: k - F
2200
2189
  };
2201
2190
  }
2202
2191
  break;
@@ -2258,16 +2247,16 @@ class me {
2258
2247
  s.flush();
2259
2248
  const a = s.gl, o = this.scissorViewport;
2260
2249
  if (o && a.getParameter(a.FRAMEBUFFER_BINDING) === null) {
2261
- const P = Math.max(Math.min(t, t + r), o.x), O = Math.min(Math.max(t, t + r), o.x + o.width), L = Math.max(Math.min(e, e + i), o.y), S = Math.min(
2250
+ const C = Math.max(Math.min(t, t + r), o.x), I = Math.min(Math.max(t, t + r), o.x + o.width), b = Math.max(Math.min(e, e + i), o.y), T = Math.min(
2262
2251
  Math.max(e, e + i),
2263
2252
  o.y + o.height
2264
2253
  );
2265
- t = P, e = L, r = Math.max(0, O - P), i = Math.max(0, S - L);
2254
+ t = C, e = b, r = Math.max(0, I - C), i = Math.max(0, T - b);
2266
2255
  }
2267
2256
  const [h, c, u, l] = a.getParameter(
2268
2257
  a.VIEWPORT
2269
- ), d = s.projection, f = h + (d[0] * t + d[12] + 1) * u / 2, m = h + (d[0] * (t + r) + d[12] + 1) * u / 2, x = c + (d[5] * e + d[13] + 1) * l / 2, y = c + (d[5] * (e + i) + d[13] + 1) * l / 2, v = Math.round(Math.min(f, m)), T = Math.round(Math.max(f, m)), w = Math.round(Math.min(x, y)), U = Math.round(Math.max(x, y));
2270
- a.enable(a.SCISSOR_TEST), a.scissor(v, w, T - v, U - w);
2258
+ ), d = s.projection, f = h + (d[0] * t + d[12] + 1) * u / 2, m = h + (d[0] * (t + r) + d[12] + 1) * u / 2, x = c + (d[5] * e + d[13] + 1) * l / 2, y = c + (d[5] * (e + i) + d[13] + 1) * l / 2, v = Math.round(Math.min(f, m)), w = Math.round(Math.max(f, m)), R = Math.round(Math.min(x, y)), U = Math.round(Math.max(x, y));
2259
+ a.enable(a.SCISSOR_TEST), a.scissor(v, R, w - v, U - R);
2271
2260
  }
2272
2261
  restoreViewportScissor() {
2273
2262
  this.rapid.flush();
@@ -2613,9 +2602,9 @@ class q {
2613
2602
  if (!Number.isInteger(t) || t <= 0)
2614
2603
  throw new RangeError("ParticleAttributeStore size must be a positive integer");
2615
2604
  this.components = Array.from({ length: t }, () => ({
2616
- value: new _(C.Float32),
2617
- delta: new _(C.Float32),
2618
- damping: new _(C.Float32)
2605
+ value: new L(P.Float32),
2606
+ delta: new L(P.Float32),
2607
+ damping: new L(P.Float32)
2619
2608
  }));
2620
2609
  }
2621
2610
  getArrayBuffer() {
@@ -2672,13 +2661,13 @@ class At {
2672
2661
  emitTimeCounter = 0;
2673
2662
  localSpace = Tt;
2674
2663
  rapid;
2675
- x = new _(C.Float32);
2676
- y = new _(C.Float32);
2677
- scaleX = new _(C.Float32);
2678
- scaleY = new _(C.Float32);
2679
- rotation = new _(C.Float32);
2680
- color = new _(C.Uint32);
2681
- remainingLife = new _(C.Float32);
2664
+ x = new L(P.Float32);
2665
+ y = new L(P.Float32);
2666
+ scaleX = new L(P.Float32);
2667
+ scaleY = new L(P.Float32);
2668
+ rotation = new L(P.Float32);
2669
+ color = new L(P.Uint32);
2670
+ remainingLife = new L(P.Float32);
2682
2671
  animations = {
2683
2672
  speed: new q(),
2684
2673
  rotation: new q(),
@@ -2774,7 +2763,7 @@ class At {
2774
2763
  r > 0 && (this.emit(r), this.emitTimer -= r / this.emitRate);
2775
2764
  }
2776
2765
  const e = this.updateParticle(t);
2777
- this.count -= e.length, _.removeAtIndices(e, this.getAllArrayBuffer());
2766
+ this.count -= e.length, L.removeAtIndices(e, this.getAllArrayBuffer());
2778
2767
  }
2779
2768
  updateParticle(t) {
2780
2769
  const e = [], r = this.animations;
@@ -2927,8 +2916,8 @@ class Ee {
2927
2916
  i.withMask(() => {
2928
2917
  i.startMaskGraphic(i.gl.TRIANGLES);
2929
2918
  for (let c = 0; c < o.length; c++) {
2930
- const u = o[c], l = o[(c + 1) % o.length], d = u.subtract(a).normalized(), f = l.subtract(a).normalized(), m = Math.max(-0.999, Math.min(1, d.dot(f))), x = Math.sqrt((1 + m) * 0.5), y = h / x, v = a.add(d.multiply(y)), T = a.add(f.multiply(y));
2931
- i.addGraphicVertex(u.x, u.y), i.addGraphicVertex(l.x, l.y), i.addGraphicVertex(T.x, T.y), i.addGraphicVertex(u.x, u.y), i.addGraphicVertex(T.x, T.y), i.addGraphicVertex(v.x, v.y);
2919
+ const u = o[c], l = o[(c + 1) % o.length], d = u.subtract(a).normalized(), f = l.subtract(a).normalized(), m = Math.max(-0.999, Math.min(1, d.dot(f))), x = Math.sqrt((1 + m) * 0.5), y = h / x, v = a.add(d.multiply(y)), w = a.add(f.multiply(y));
2920
+ i.addGraphicVertex(u.x, u.y), i.addGraphicVertex(l.x, l.y), i.addGraphicVertex(w.x, w.y), i.addGraphicVertex(u.x, u.y), i.addGraphicVertex(w.x, w.y), i.addGraphicVertex(v.x, v.y);
2932
2921
  }
2933
2922
  i.endGraphic();
2934
2923
  }, () => {
@@ -2961,7 +2950,7 @@ class Ee {
2961
2950
  }
2962
2951
  }
2963
2952
  export {
2964
- z as BaseTexture,
2953
+ Y as BaseTexture,
2965
2954
  xe as BlendMode,
2966
2955
  Q as CanvasScaleMode,
2967
2956
  g as Color,
@@ -1,5 +1,5 @@
1
- (function(v,p){typeof exports=="object"&&typeof module<"u"?p(exports):typeof define=="function"&&define.amd?define(["exports"],p):(v=typeof globalThis<"u"?globalThis:v||self,p(v.RapidRender={}))})(this,(function(v){"use strict";class p{_r;_g;_b;_a;uint32;premultipliedUint32;static clampByte(t){return t<=0?0:t>=255?255:Math.round(t)}constructor(t,e,r,i=255){this._r=t,this._g=e,this._b=r,this._a=i,this.updateUint()}setClearColor(t,e=!0){const r=e?this.premultipliedUint32:this.uint32;t.clearColor((r&255)/255,(r>>>8&255)/255,(r>>>16&255)/255,(r>>>24)/255)}get r(){return this._r}set r(t){this._r=t,this.updateUint()}get g(){return this._g}set g(t){this._g=t,this.updateUint()}get b(){return this._b}set b(t){this._b=t,this.updateUint()}get a(){return this._a}set a(t){this._a=t,this.updateUint()}updateUint(){this.uint32=(this._a<<24|this._b<<16|this._g<<8|this._r)>>>0;const t=p.clampByte(this._a),e=p.clampByte(this._r*t/255),r=p.clampByte(this._g*t/255),i=p.clampByte(this._b*t/255);this.premultipliedUint32=(t<<24|i<<16|r<<8|e)>>>0}reset(){this._r=0,this._g=0,this._b=0,this._a=255,this.updateUint()}setRGBA(t,e,r,i){this._r=t,this._g=e,this._b=r,this._a=i,this.updateUint()}setHSL(t,e,r){t=(t%360+360)%360,e=Math.max(0,Math.min(100,e))/100,r=Math.max(0,Math.min(100,r))/100;const i=(1-Math.abs(2*r-1))*e,s=i*(1-Math.abs(t/60%2-1)),a=r-i/2;let o=0,h=0,c=0;return t<60?[o,h,c]=[i,s,0]:t<120?[o,h,c]=[s,i,0]:t<180?[o,h,c]=[0,i,s]:t<240?[o,h,c]=[0,s,i]:t<300?[o,h,c]=[s,0,i]:[o,h,c]=[i,0,s],this.setRGBA((o+a)*255,(h+a)*255,(c+a)*255,255),this}copy(t){this.setRGBA(t.r,t.g,t.b,t.a)}clone(){return new p(this._r,this._g,this._b,this._a)}toHexString(t=!0){const e=this.r.toString(16).padStart(2,"0"),r=this.g.toString(16).padStart(2,"0"),i=this.b.toString(16).padStart(2,"0"),s=this.a.toString(16).padStart(2,"0");return`#${e}${r}${i}${t?s:""}`}equals(t){return t.r===this.r&&t.g===this.g&&t.b===this.b&&t.a===this.a}static FromHSL(t,e,r){return new p(0,0,0).setHSL(t,e,r)}static FromRGB(t,e,r){return new p(t,e,r)}static fromNorm(t,e,r,i=1){return new p(Math.round(t*255),Math.round(e*255),Math.round(r*255),Math.round(i*255))}static packColor(t,e,r,i,s){const a=p.clampByte(i),o=s?a/255:1,h=p.clampByte(t*o),c=p.clampByte(e*o),u=p.clampByte(r*o);return(a<<24|u<<16|c<<8|h)>>>0}static fromHex(t){t.startsWith("#")&&(t=t.slice(1));const e=parseInt(t.slice(0,2),16),r=parseInt(t.slice(2,4),16),i=parseInt(t.slice(4,6),16);let s=255;return t.length>=8&&(s=parseInt(t.slice(6,8),16)),new p(e,r,i,s)}add(t){return new p(Math.min(this.r+t.r,255),Math.min(this.g+t.g,255),Math.min(this.b+t.b,255),Math.min(this.a+t.a,255))}subtract(t){return new p(this.r-t.r,this.g-t.g,this.b-t.b,this.a-t.a)}divide(t){return t instanceof p?new p(this.r/t.r,this.g/t.g,this.b/t.b,this.a/t.a):new p(this.r/t,this.g/t,this.b/t,this.a/t)}multiply(t){return t instanceof p?new p(this.r*t.r,this.g*t.g,this.b*t.b,this.a*t.a):new p(this.r*t,this.g*t,this.b*t,this.a*t)}clamp(){this._r=Math.max(0,Math.min(255,this._r)),this._g=Math.max(0,Math.min(255,this._g)),this._b=Math.max(0,Math.min(255,this._b)),this._a=Math.max(0,Math.min(255,this._a)),this.updateUint()}static Red=new p(255,0,0,255);static Green=new p(0,255,0,255);static Blue=new p(0,0,255,255);static Yellow=new p(255,255,0,255);static Purple=new p(128,0,128,255);static Orange=new p(255,165,0,255);static Pink=new p(255,192,203,255);static Gray=new p(128,128,128,255);static Brown=new p(139,69,19,255);static Cyan=new p(0,255,255,255);static Magenta=new p(255,0,255,255);static Lime=new p(192,255,0,255);static White=new p(255,255,255,255);static Black=new p(0,0,0,255);static Transparent=new p(0,0,0,0)}var q=(n=>(n[n.LINEAR=0]="LINEAR",n[n.NEAREST=1]="NEAREST",n))(q||{}),j=(n=>(n[n.REPEAT=0]="REPEAT",n[n.CLAMP=1]="CLAMP",n[n.MIRRORED_REPEAT=2]="MIRRORED_REPEAT",n))(j||{});class vt{render;cache=new Map;texture=new Set;constructor(t){this.render=t}addTexture(t){return this.texture.add(t),t}removeTexture(t){return this.texture.delete(t),t}async load(t,e){let r=this.cache.get(t);if(r)return this.addTexture(new W(r));try{const i=await this._fetchImage(t);return r=X.fromSource(this.render,i,e),r.uid=t,this.cache.set(t,r),this.addTexture(new W(r))}catch(i){throw console.error(`[TextureManager] Failed to load: ${t}`,i),i}}create(t,e){if(e?.key&&this.cache.has(e.key))return this.addTexture(new W(this.cache.get(e.key)));const r=X.fromSource(this.render,t,e);return e?.key&&(r.uid=e.key,this.cache.set(e.key,r)),this.addTexture(new W(r))}createRenderTexture(t){return this.addTexture(new yt(this.render,t))}createTextTexture(t){return this.addTexture(new Tt(this.render,t))}destroy(t,e=!1){let r,i;typeof t=="string"?(i=t,r=this.cache.get(i)):t instanceof W?(r=t.base,i=r?.uid,this.removeTexture(t),t.destroy()):(r=t,i=r.uid),r&&(r.refCount<=0||e)&&(r.destroy(this.render.gl),i&&this.cache.delete(i))}destroyAll(){for(const t of this.cache.values())t.destroy(this.render.gl);this.cache.clear()}_fetchImage(t){return new Promise((e,r)=>{const i=new Image;i.crossOrigin="anonymous",i.onload=()=>e(i),i.onerror=()=>r(new Error(`Failed to load image resource: ${t}`)),i.src=t})}}class X{glTexture=null;width;height;uid;refCount=0;constructor(t,e,r){this.glTexture=t,this.width=e,this.height=r}static fromSource(t,e,r={}){const i=st(t,e,r);return new X(i,e.width,e.height)}updateSource(t,e,r={}){this.glTexture&&(t.bindTexture(t.TEXTURE_2D,this.glTexture),r.textureFilter!==void 0&&ct(t,r.textureFilter),r.wrap!==void 0&&ht(t,r.wrap),t.pixelStorei(t.UNPACK_FLIP_Y_WEBGL,1),t.pixelStorei(t.UNPACK_ALIGNMENT,1),r.premultipliedAlpha!==void 0&&t.pixelStorei(t.UNPACK_PREMULTIPLY_ALPHA_WEBGL,r.premultipliedAlpha),this.width===e.width&&this.height===e.height?t.texSubImage2D(t.TEXTURE_2D,0,0,0,t.RGBA,t.UNSIGNED_BYTE,e):(t.texImage2D(t.TEXTURE_2D,0,t.RGBA,t.RGBA,t.UNSIGNED_BYTE,e),this.width=e.width,this.height=e.height),r.premultipliedAlpha!==void 0&&t.pixelStorei(t.UNPACK_PREMULTIPLY_ALPHA_WEBGL,!1))}setFilterMode(t,e){this.glTexture&&(t.bindTexture(t.TEXTURE_2D,this.glTexture),ct(t,e),t.bindTexture(t.TEXTURE_2D,null))}setWrapMode(t,e){this.glTexture&&(t.bindTexture(t.TEXTURE_2D,this.glTexture),ht(t,e),t.bindTexture(t.TEXTURE_2D,null))}destroy(t){this.glTexture&&(t.deleteTexture(this.glTexture),this.glTexture=null)}}class W{base;uvX=0;uvY=0;uvW=1;uvH=1;rawWidth=0;rawHeight=0;offsetX=0;offsetY=0;scale=1;isAtlas=!1;flipY=!1;isRotated=!1;glTexture=null;get width(){return this.isRotated?this.rawHeight:this.rawWidth}get height(){return this.isRotated?this.rawWidth:this.rawHeight}_px=0;_py=0;constructor(t){t&&this.setBase(t)}updateResolution(){}setBase(t){return this.base===t?this:(this.base&&this.base.refCount--,this.base=t,this.base.refCount++,this.setRegion(0,0,t.width,t.height),this.glTexture=t.glTexture,this)}setRegion(t,e,r,i){return this.base?(this._px=t,this._py=e,this.isAtlas=t!==0||e!==0||r!==this.base.width||i!==this.base.height,this.uvX=t/this.base.width,this.uvY=e/this.base.height,this.uvW=this.uvX+r/this.base.width,this.uvH=this.uvY+i/this.base.height,this.rawWidth=r*this.scale,this.rawHeight=i*this.scale,this):this}getSubTexture(t,e,r,i){if(!this.base)return new W;const s=this.clone(),a=this._px,o=this._py;return s.setRegion(a+t,o+e,r,i),s}clone(t){const e=t??new W(this.base);return t&&this.base&&t.setBase(this.base),e.scale=this.scale,e.uvX=this.uvX,e.uvY=this.uvY,e.uvW=this.uvW,e.uvH=this.uvH,e.rawWidth=this.rawWidth,e.rawHeight=this.rawHeight,e.offsetX=this.offsetX,e.offsetY=this.offsetY,e.flipY=this.flipY,e.isRotated=this.isRotated,e.isAtlas=this.isAtlas,e._px=this._px,e._py=this._py,e}splitGrid(t,e,r,i,s=0){if(!this.base)return[];const a=[],o=this.rawWidth,h=this.rawHeight,c=Math.max(0,s),u=t+c,l=e+c,d=r??Math.floor((o+c)/u),f=i??Math.floor((h+c)/l);for(let m=0;m<f;m++)for(let x=0;x<d;x++)a.push(this.getSubTexture(x*u,m*l,t,e));return a}static fromImageSource(t,e,r={}){const i=st(t,e,r);return new W(new X(i,e.width,e.height))}destroy(){this.base&&(this.base.refCount--,this.base=void 0),this.glTexture=null}}class yt extends W{framebuffer;renderbuffer;gl;premultipliedAlpha;clearColor;_allocW=0;_allocH=0;constructor(t,e){super(),this.gl=t.gl,this.premultipliedAlpha=t.premultipliedAlpha,this.clearColor=e.clearColor??p.Black;const r=Math.max(Math.round(e.width),1),i=Math.max(Math.round(e.height),1),a=st(t,{width:r,height:i},{...e,onlySize:!0});if(this.setBase(new X(a,r,i)),this.framebuffer=this.gl.createFramebuffer(),this.renderbuffer=this.gl.createRenderbuffer(),!this.framebuffer||!this.renderbuffer)throw new Error("Failed to create Framebuffer or Renderbuffer");this.resize(r,i,!0)}resize(t,e,r=!1){t=Math.max(Math.round(t),1),e=Math.max(Math.round(e),1);const i=this.rawWidth===t&&this.rawHeight===e;if(!r&&i)return;const s=this.gl;(r||t>this._allocW||e>this._allocH)&&this.base&&(this._allocW=Math.max(this._allocW,t),this._allocH=Math.max(this._allocH,e),this.base.width=this._allocW,this.base.height=this._allocH,s.bindTexture(s.TEXTURE_2D,this.base.glTexture),s.texImage2D(s.TEXTURE_2D,0,s.RGBA,this._allocW,this._allocH,0,s.RGBA,s.UNSIGNED_BYTE,null),s.bindRenderbuffer(s.RENDERBUFFER,this.renderbuffer),s.renderbufferStorage(s.RENDERBUFFER,s.STENCIL_INDEX8,this._allocW,this._allocH),s.bindFramebuffer(s.FRAMEBUFFER,this.framebuffer),s.framebufferTexture2D(s.FRAMEBUFFER,s.COLOR_ATTACHMENT0,s.TEXTURE_2D,this.base.glTexture,0),s.framebufferRenderbuffer(s.FRAMEBUFFER,s.STENCIL_ATTACHMENT,s.RENDERBUFFER,this.renderbuffer),s.bindFramebuffer(s.FRAMEBUFFER,null),s.bindRenderbuffer(s.RENDERBUFFER,null),s.bindTexture(s.TEXTURE_2D,null)),this.rawWidth=t,this.rawHeight=e,this.uvX=0,this.uvW=t/this._allocW,this.uvY=0,this.uvH=e/this._allocH}activate(t=!1){if(!this.framebuffer)return;const e=this.gl;e.bindFramebuffer(e.FRAMEBUFFER,this.framebuffer),e.viewport(0,0,this.rawWidth,this.rawHeight),t&&this.clear()}clear(t){if(!this.framebuffer)return;const e=this.gl,r=t??this.clearColor;r&&(r.setClearColor(e,this.premultipliedAlpha),e.clear(e.COLOR_BUFFER_BIT|e.STENCIL_BUFFER_BIT))}deactivate(){const t=this.gl;t.bindFramebuffer(t.FRAMEBUFFER,null)}GetPixels(t=0,e=0,r=this.rawWidth,i=this.rawHeight){if(!this.framebuffer)return new Uint8Array(0);const s=Math.floor(t),a=Math.floor(e),o=Math.floor(r),h=Math.floor(i);if(o<=0||h<=0)return new Uint8Array(0);if(s<0||a<0||s>=this.rawWidth||a>=this.rawHeight)return new Uint8Array(0);const c=Math.min(o,this.rawWidth-s),u=Math.min(h,this.rawHeight-a),l=this.gl,d=l.getParameter(l.FRAMEBUFFER_BINDING);l.bindFramebuffer(l.FRAMEBUFFER,this.framebuffer);const f=new Uint8Array(c*u*4);return l.readPixels(s,a,c,u,l.RGBA,l.UNSIGNED_BYTE,f),l.bindFramebuffer(l.FRAMEBUFFER,d),f}GetColorAt(t,e){const r=this.GetPixels(t,e,1,1);return r.length<4?new p(0,0,0,0):new p(r[0],r[1],r[2],r[3])}destroy(){super.destroy(),this.framebuffer&&this.gl.deleteFramebuffer(this.framebuffer),this.renderbuffer&&this.gl.deleteRenderbuffer(this.renderbuffer),this.base?.destroy(this.gl)}}const jt={fontFamily:"Arial",fontSize:24,fontWeight:"normal",fill:"#000000",strokeThickness:0,align:"left",lineHeight:1};class Tt extends W{canvas;ctx;render;_text;_style;options;resolution=1;constructor(t,e){super(),this.render=t,this._style={...jt,...e},this._text=e?.text??"",this.options={premultipliedAlpha:t.premultipliedAlpha,...e,textureFilter:1},this.canvas=document.createElement("canvas");const r=this.canvas.getContext("2d",{willReadFrequently:!0});if(!r)throw new Error("Failed to get 2d context for TextTexture");this.ctx=r,this.canvas.width=1,this.canvas.height=1;const i=st(t,this.canvas,this.options);this.setBase(new X(i,1,1)),this.updateResolution(),this.update()}get text(){return this._text}set text(t){this._text!==t&&(this._text=t,this.update())}get style(){return this._style}set style(t){this._style={...this._style,...t},this.update()}updateOffset(t,e){switch(this._style.align){case"center":return t/2;case"right":return t-e;default:return e}}updateResolution(){let t=1;t*=this.render.viewport.resolution,t!=this.resolution&&(this.resolution=t,this.update())}update(){const t=this.ctx,e=this.style,r=e.fontSize,i=e.fontWeight,s=e.fontFamily,a=e.lineHeight,h=Math.max(r,32),c=r/h,u=`${i} ${r}px ${s}`,l=`${i} ${h}px ${s}`,d=this.resolution,f=e.baseline??"top",m=e.align??"left",x=e.strokeThickness??0,T=x>0,y=this._text.split(`
2
- `),w="国g",R=y[0]||w;t.font=l,t.textBaseline="alphabetic",t.textAlign="left";let C=0,L=0,B=0;for(const D of y){const $=t.measureText(D||w);C=Math.max(C,D?$.width*c:0),D==y.at(0)&&(L=$.actualBoundingBoxAscent*c),D==y.at(-1)&&(B=$.actualBoundingBoxDescent*c)}t.textBaseline="alphabetic";const N=t.measureText(R).fontBoundingBoxDescent*c;t.textBaseline=f;const A=t.measureText(R).fontBoundingBoxDescent*c,M=r*a,U=L+B+(y.length-1)*M,F=x/2+2/d,G=C+F*2,H=U+F*2,I=Math.max(1,Math.ceil(G*d)),O=Math.max(1,Math.ceil(H*d)),Y=I/d;(this.canvas.width!==I||this.canvas.height!==O)&&(this.canvas.width=I,this.canvas.height=O),t.setTransform(d,0,0,-d,0,O),t.clearRect(0,0,I/d,O/d),t.font=u,t.textBaseline="alphabetic",t.textAlign=m;const V=this.updateOffset(Y,F);T&&(t.lineWidth=x,t.strokeStyle=e.stroke??"#000000",t.lineJoin="round"),e.fill&&(t.fillStyle=e.fill);let b=F+L;for(const D of y)T&&t.strokeText(D,V,b),e.fill&&t.fillText(D,V,b),b+=M;const it=A-N;this.scale=1/this.resolution,this.offsetX=-V,this.offsetY=-F-L+it,this.base?.updateSource(this.render.gl,this.canvas,this.options),this.setRegion(0,0,I,O)}}const Kt=(n,t=!1,e=!0)=>{const r=n.getContext("webgl2",{stencil:!0,antialias:t,premultipliedAlpha:e});if(!r)throw new Error("WebGL2 is not supported in this browser.");return r},wt=(n,t,e)=>{const r=n.createShader(e);if(!r)throw new Error("Unable to create webgl shader");if(n.shaderSource(r,t),n.compileShader(r),!n.getShaderParameter(r,n.COMPILE_STATUS)){const s=n.getShaderInfoLog(r);throw console.error("Shader compilation failed:",s),new Error("Unable to compile shader: "+s+t)}return r},qt=(n,t,e)=>{var r=n.createProgram(),i=wt(n,t,35633),s=wt(n,e,35632);if(!r)throw new Error("Unable to create program shader");if(n.attachShader(r,i),n.attachShader(r,s),n.linkProgram(r),!n.getProgramParameter(r,n.LINK_STATUS)){const o=n.getProgramInfoLog(r);throw new Error("Unable to link shader program: "+o)}return r};function ht(n,t){const e={[j.CLAMP]:n.CLAMP_TO_EDGE,[j.REPEAT]:n.REPEAT,[j.MIRRORED_REPEAT]:n.MIRRORED_REPEAT}[t]||n.CLAMP_TO_EDGE;n.texParameteri(n.TEXTURE_2D,n.TEXTURE_WRAP_S,e),n.texParameteri(n.TEXTURE_2D,n.TEXTURE_WRAP_T,e)}function ct(n,t){const e={[q.LINEAR]:n.LINEAR,[q.NEAREST]:n.NEAREST}[t]??n.NEAREST;n.texParameteri(n.TEXTURE_2D,n.TEXTURE_MIN_FILTER,e),n.texParameteri(n.TEXTURE_2D,n.TEXTURE_MAG_FILTER,e)}function st(n,t,e){const r=n.gl,i=r.createTexture();if(!i)throw new Error("Unable to create WebGL texture");if(r.bindTexture(r.TEXTURE_2D,i),r.pixelStorei(r.UNPACK_FLIP_Y_WEBGL,0),r.pixelStorei(r.UNPACK_ALIGNMENT,1),ht(r,e.wrap??j.CLAMP),ct(r,e.textureFilter??n.textureFilter),e.onlySize){const s=t;r.texImage2D(r.TEXTURE_2D,0,r.RGBA,s.width,s.height,0,r.RGBA,r.UNSIGNED_BYTE,null)}else{const s=e.premultipliedAlpha??n.premultipliedAlpha;s&&r.pixelStorei(r.UNPACK_PREMULTIPLY_ALPHA_WEBGL,!0),r.texImage2D(r.TEXTURE_2D,0,r.RGBA,r.RGBA,r.UNSIGNED_BYTE,t),s&&r.pixelStorei(r.UNPACK_PREMULTIPLY_ALPHA_WEBGL,!1)}return r.bindTexture(r.TEXTURE_2D,null),i}function Z(n,t,e="vTextureId",r="uv",i="return"){if(n.includes("%TEXTURE_NUM%")&&(n=n.replaceAll("%TEXTURE_NUM%",t.toString())),n.includes("%GET_COLOR%")){let s="";for(let a=0;a<t;a++)a==0?s+=`if(${e} == ${a})`:a==t-1?s+="else":s+=`else if(${e} == ${a})`,s+=`{${i} texture(uTextures[${a}], ${r});}`;n=n.replaceAll("%GET_COLOR%",s)}return n}const Zt=5126,ut=5121;function Qt(n,t,e){n.vertexAttribDivisor(t,e)}function Et(n,t,e,r,i){n.drawArraysInstanced(t,e,r,i)}let Jt=0;class lt{program;attributeLoc={};uniformLoc={};isCustom=!1;padding=0;uniformType={};uniformIsArray={};gl;shaderId=Jt++;vao;constructor(t,e,r,i){this.gl=t,this.program=qt(t,e,r),this.parseShader(e),this.parseShader(r),this.vao=t.createVertexArray(),i&&this.setAttributes(i)}use(){this.gl.useProgram(this.program)}bindVAO(){this.gl.bindVertexArray(this.vao)}unbindVAO(){this.gl.bindVertexArray(null)}setUniform(t,e){const r=this.uniformLoc[t];if(r===void 0)return;const i=this.uniformType[t],s=this.uniformIsArray[t];te(this.gl,r,i,e,s)}setUniforms(t){for(const e in t)this.setUniform(e,t[e])}setAttribute(t){const e=this.attributeLoc[t.name];if(e===void 0)return;const r=this.gl;r.vertexAttribPointer(e,t.size,t.type??Zt,t.normalized??!1,t.stride,t.offset??0),r.enableVertexAttribArray(e),Qt(r,e,t.divisor??0)}setAttributes(t){for(const e of t)this.setAttribute(e)}destroy(){this.gl.deleteProgram(this.program)}parseShader(t){const e=this.gl,r=t.match(/(?:in|attribute)\s+\w+\s+\w+/g);if(r)for(const s of r){const o=s.split(/\s+/)[2],h=e.getAttribLocation(this.program,o);h!==-1&&(this.attributeLoc[o]=h)}const i=t.matchAll(/uniform\s+(?:(?:lowp|mediump|highp)\s+)?(\w+)\s+(\w+)(\s*\[\s*\d+\s*\])?/g);for(const s of i){const a=s[1],o=s[2],h=e.getUniformLocation(this.program,o);h!==null&&(this.uniformLoc[o]=h,this.uniformType[o]=a,this.uniformIsArray[o]=s[3]!==void 0)}}}function te(n,t,e,r,i=!1){switch(e){case"float":return i?n.uniform1fv(t,r):n.uniform1f(t,r);case"int":case"bool":case"sampler2D":case"samplerCube":return i?n.uniform1iv(t,r):n.uniform1i(t,r);case"vec2":return n.uniform2fv(t,r);case"vec3":return n.uniform3fv(t,r);case"vec4":return n.uniform4fv(t,r);case"ivec2":case"bvec2":return n.uniform2iv(t,r);case"ivec3":case"bvec3":return n.uniform3iv(t,r);case"ivec4":case"bvec4":return n.uniform4iv(t,r);case"mat2":return n.uniformMatrix2fv(t,!1,r);case"mat3":return n.uniformMatrix3fv(t,!1,r);case"mat4":return n.uniformMatrix4fv(t,!1,r);case"mat3x2":return n.uniformMatrix3x2fv(t,!1,r);default:return ee(n,t,r)}}function ee(n,t,e){if(typeof e=="number")return n.uniform1f(t,e);const r=e;switch(r.length){case 2:return n.uniform2fv(t,r);case 3:return n.uniform3fv(t,r);case 4:return n.uniform4fv(t,r);case 9:return n.uniformMatrix3fv(t,!1,r);case 16:return n.uniformMatrix4fv(t,!1,r);default:return n.uniform1fv(t,r)}}class nt{vs;fs;glshader=new Map;uniforms={};uniformDirty=new Set;uniformTextures={};uniformArrayTexture={};usedTextureUnitNum=0;padding=0;rapid;prefix=[];constructor(t,e="",r="",i=0,s){this.vs=e,this.fs=r,this.rapid=t,this.uniforms=s??{},this.usedTextureUnitNum=i,this.beforePreCompile(),this.preCompile()}beforePreCompile(){}preCompile(){}setUniforms(t){this.rapid.flush();const e={},r={},i={};for(const[s,a]of Object.entries(t))a instanceof WebGLTexture?r[s]=a:Array.isArray(a)&&a[0]instanceof WebGLTexture?i[s]=a:e[s]=a;Object.assign(this.uniforms,e),Object.assign(this.uniformTextures,r),Object.assign(this.uniformArrayTexture,i);for(const s of this.glshader.keys())this.uniformDirty.add(s)}setPadding(t){this.padding=t;for(const e of this.glshader.values())e.padding=t;return this}applyUniform(t,e){const r=this.glshader.get(t);r&&(Object.keys(e).length>0&&r.setUniforms(e),this.uniformDirty.has(t)&&(r.setUniforms(this.uniforms),this.uniformDirty.delete(t)))}replaceCustomCode(t,e){return t=t.replace("// CUSTOM_CODE",e+"// CUSTOM_CODE"),t}getGLShader(t,e,r,i){if(this.glshader.has(e))return this.glshader.get(e);if(i==null||r==null)return console.warn("CustomGlShader: missing base shader for "+e),null;const s=this.prefix.length==0?[""]:this.prefix;r=r.replace("// CUSTOM_CODE_CALL",s.map(o=>o+"vertex(position, vRegion);").join("")),r=this.replaceCustomCode(r,this.vs),i=i.replace("// CUSTOM_CODE_CALL",s.map(o=>o+"fragment(fragColor, vRegion);").join("")),i=this.replaceCustomCode(i,this.fs);const a=t.createShader(r,i);return a.isCustom=!0,a.padding=this.padding,this.glshader.set(e,a),this.uniformDirty.add(e),a}destroy(){this.glshader.forEach(t=>t.destroy())}}class Rt{constructor(t){this.rapid=t,this.gl=t.gl,this.maxTextureUnits=t.maxTextureUnits,this.matrixStore=t.matrix}defaultShader;currentShader;customShader=null;customShaderUsedTextureNum=0;gl;maxTextureUnits;matrixStore;isDefaultShader=!0;KEY="default";vs="";fs="";usedTextures=[];usedTexturePadding=[];extraTextureUnit=0;createBuffer(){}createDefaultShader(){}get freeTextureUnitNum(){return this.maxTextureUnits-this.usedTextures.length-this.customShaderUsedTextureNum}getTextureUnitList(){return Array.from({length:this.maxTextureUnits},(t,e)=>e)}findTextureUnit(t,e=0,r=0){for(let i=0;i<this.usedTextures.length;i++){const s=i*2;if(this.usedTextures[i]===t&&this.usedTexturePadding[s]===e&&this.usedTexturePadding[s+1]===r)return i}return-1}useTexture(t,e=0,r=0){const i=this.findTextureUnit(t,e,r);return i==-1?(this.freeTextureUnitNum===0&&this.flush(),this.usedTextures.push(t),this.usedTexturePadding.push(e),this.usedTexturePadding.push(r),this.usedTextures.length-1):i}getMaxTexutreUnit(t){return this.rapid.maxTextureUnits-this.extraTextureUnit-(t?.usedTextureUnitNum??0)}createShader(t,e){const r=this.getMaxTexutreUnit();return e=Z(e,r),t=Z(t,r),new lt(this.gl,t,e)}createCustomShader(t){const e=this.getMaxTexutreUnit(t),r=Z(this.fs,e),i=Z(this.vs,e);return t.getGLShader(this,this.KEY,i,r)}getCustomShader(t){if(!t)return this.defaultShader;if(t instanceof nt){const e=this.createCustomShader(t);return e??this.defaultShader}return t}isSameShader(t){return this.isDefaultShader&&!t?!0:this.getCustomShader(t)==this.currentShader}enter(t){this.resetRender(),this.currentShader=this.getCustomShader(t),t instanceof nt?(this.customShader=t,this.customShaderUsedTextureNum=t.usedTextureUnitNum):(this.customShader=null,this.customShaderUsedTextureNum=0),this.isDefaultShader=this.currentShader==this.defaultShader,this.currentShader.use(),this.currentShader.setUniform("u_projection",this.rapid.projection)}exit(){this.hasPendingContent()&&this.flush(),this.gl.bindVertexArray(null)}flush(){this.render(),this.resetRender()}render(){this.prepareRender()}prepareRender(){const t=this.currentShader;if(t.use(),this.customShader){const r=this.customShader,i={};for(const[s,a]of Object.entries(r.uniformTextures))i[s]=this.useTexture(a);for(const[s,a]of Object.entries(r.uniformArrayTexture))i[s]=a.map(o=>this.useTexture(o));this.customShader.applyUniform(this.KEY,i)}const e=this.gl;for(let r=0;r<this.usedTextures.length;r++)e.activeTexture(e.TEXTURE0+r),e.bindTexture(e.TEXTURE_2D,this.usedTextures[r]);t.setUniform("uTextures",Int32Array.from({length:this.usedTextures.length},(r,i)=>i)),t.setUniform("uPadding",Float32Array.from({length:this.usedTexturePadding.length},(r,i)=>this.usedTexturePadding[i]))}resetRender(){const t=this.gl;for(let e=0;e<this.usedTextures.length;e++)t.activeTexture(t.TEXTURE0+e),t.bindTexture(t.TEXTURE_2D,null);this.usedTextures.length=0,this.usedTexturePadding.length=0}hasPendingContent(){return!1}}var _=(n=>(n[n.Float32=0]="Float32",n[n.Uint32=1]="Uint32",n[n.Uint16=2]="Uint16",n))(_||{});class P{usedElemNum;typedArray;arrayType;maxElemNum;bytePerElem;arraybuffer;uint32;float32;uint16;constructor(t){this.usedElemNum=0,this.maxElemNum=512,this.bytePerElem=this.getArrayType(t).BYTES_PER_ELEMENT,this.arrayType=t,this.arraybuffer=new ArrayBuffer(this.maxElemNum*this.bytePerElem),this.updateTypedArray()}getArrayType(t){switch(t){case 0:return Float32Array;case 1:return Uint32Array;case 2:return Uint16Array;default:throw new Error("Unsupported ArrayType")}}updateTypedArray(){switch(this.uint32=new Uint32Array(this.arraybuffer),this.float32=new Float32Array(this.arraybuffer),this.uint16=new Uint16Array(this.arraybuffer),this.arrayType){case 0:this.typedArray=this.float32;break;case 1:this.typedArray=this.uint32;break;case 2:this.typedArray=this.uint16;break}}clear(){this.usedElemNum=0}static removeAtIndices(t,e){const r=Math.max(...e.map(o=>o.usedElemNum)),i=[...new Set(t)].filter(o=>o>=0&&o<r).sort((o,h)=>o-h);if(i.length===0)return 0;let s=0,a=0;for(let o=0;o<r;o++){if(a<i.length&&o===i[a]){a++;continue}if(s!==o)for(const h of e)h.typedArray[s]=h.typedArray[o];s++}for(const o of e)o.usedElemNum=s;return r-s}resize(t=0){if(t+=this.usedElemNum,t>this.maxElemNum){for(;t>this.maxElemNum;)this.maxElemNum*=2;return this.setMaxSize(this.maxElemNum),!0}return!1}setMaxSize(t=this.maxElemNum){const e=this.typedArray;this.maxElemNum=t,this.arraybuffer=new ArrayBuffer(t*this.bytePerElem),this.updateTypedArray(),this.typedArray.set(e)}pushUint32(t){this.resize(1),this.uint32[this.usedElemNum++]=t}pushFloat32(t){this.resize(1),this.float32[this.usedElemNum++]=t}push(t){this.resize(1),this.typedArray[this.usedElemNum++]=t}pop(){return this.typedArray[--this.usedElemNum]}top(){return this.typedArray[this.usedElemNum-1]}get(t){return this.typedArray[t]}getArray(t=0,e){return e===void 0?this.typedArray:this.typedArray.subarray(t,e)}get length(){return this.usedElemNum}}class dt extends P{buffer;gl;dirty=!0;type;webglBufferSize=0;usage;constructor(t,e,r=t.ARRAY_BUFFER,i=t.DYNAMIC_DRAW){super(e),this.gl=t,this.buffer=t.createBuffer(),this.type=r,this.usage=i}makeDirty(){this.dirty=!0}clear(){super.clear(),this.dirty=!1}bindBuffer(){this.gl.bindBuffer(this.type,this.buffer)}bufferData(){if(this.dirty){const t=this.gl;this.maxElemNum>this.webglBufferSize&&(t.bufferData(this.type,this.maxElemNum*this.bytePerElem,this.usage),this.webglBufferSize=this.maxElemNum),t.bufferSubData(this.type,0,this.getArray(0,this.usedElemNum)),this.dirty=!1}}}const at=`#version 300 es\r
1
+ (function(T,p){typeof exports=="object"&&typeof module<"u"?p(exports):typeof define=="function"&&define.amd?define(["exports"],p):(T=typeof globalThis<"u"?globalThis:T||self,p(T.RapidRender={}))})(this,(function(T){"use strict";class p{_r;_g;_b;_a;uint32;premultipliedUint32;static clampByte(t){return t<=0?0:t>=255?255:Math.round(t)}constructor(t,e,r,i=255){this._r=t,this._g=e,this._b=r,this._a=i,this.updateUint()}setClearColor(t,e=!0){const r=e?this.premultipliedUint32:this.uint32;t.clearColor((r&255)/255,(r>>>8&255)/255,(r>>>16&255)/255,(r>>>24)/255)}get r(){return this._r}set r(t){this._r=t,this.updateUint()}get g(){return this._g}set g(t){this._g=t,this.updateUint()}get b(){return this._b}set b(t){this._b=t,this.updateUint()}get a(){return this._a}set a(t){this._a=t,this.updateUint()}updateUint(){this.uint32=(this._a<<24|this._b<<16|this._g<<8|this._r)>>>0;const t=p.clampByte(this._a),e=p.clampByte(this._r*t/255),r=p.clampByte(this._g*t/255),i=p.clampByte(this._b*t/255);this.premultipliedUint32=(t<<24|i<<16|r<<8|e)>>>0}reset(){this._r=0,this._g=0,this._b=0,this._a=255,this.updateUint()}setRGBA(t,e,r,i){this._r=t,this._g=e,this._b=r,this._a=i,this.updateUint()}setHSL(t,e,r){t=(t%360+360)%360,e=Math.max(0,Math.min(100,e))/100,r=Math.max(0,Math.min(100,r))/100;const i=(1-Math.abs(2*r-1))*e,s=i*(1-Math.abs(t/60%2-1)),a=r-i/2;let o=0,h=0,c=0;return t<60?[o,h,c]=[i,s,0]:t<120?[o,h,c]=[s,i,0]:t<180?[o,h,c]=[0,i,s]:t<240?[o,h,c]=[0,s,i]:t<300?[o,h,c]=[s,0,i]:[o,h,c]=[i,0,s],this.setRGBA((o+a)*255,(h+a)*255,(c+a)*255,255),this}copy(t){this.setRGBA(t.r,t.g,t.b,t.a)}clone(){return new p(this._r,this._g,this._b,this._a)}toHexString(t=!0){const e=this.r.toString(16).padStart(2,"0"),r=this.g.toString(16).padStart(2,"0"),i=this.b.toString(16).padStart(2,"0"),s=this.a.toString(16).padStart(2,"0");return`#${e}${r}${i}${t?s:""}`}equals(t){return t.r===this.r&&t.g===this.g&&t.b===this.b&&t.a===this.a}static FromHSL(t,e,r){return new p(0,0,0).setHSL(t,e,r)}static FromRGB(t,e,r){return new p(t,e,r)}static fromNorm(t,e,r,i=1){return new p(Math.round(t*255),Math.round(e*255),Math.round(r*255),Math.round(i*255))}static packColor(t,e,r,i,s){const a=p.clampByte(i),o=s?a/255:1,h=p.clampByte(t*o),c=p.clampByte(e*o),u=p.clampByte(r*o);return(a<<24|u<<16|c<<8|h)>>>0}static fromHex(t){t.startsWith("#")&&(t=t.slice(1));const e=parseInt(t.slice(0,2),16),r=parseInt(t.slice(2,4),16),i=parseInt(t.slice(4,6),16);let s=255;return t.length>=8&&(s=parseInt(t.slice(6,8),16)),new p(e,r,i,s)}add(t){return new p(Math.min(this.r+t.r,255),Math.min(this.g+t.g,255),Math.min(this.b+t.b,255),Math.min(this.a+t.a,255))}subtract(t){return new p(this.r-t.r,this.g-t.g,this.b-t.b,this.a-t.a)}divide(t){return t instanceof p?new p(this.r/t.r,this.g/t.g,this.b/t.b,this.a/t.a):new p(this.r/t,this.g/t,this.b/t,this.a/t)}multiply(t){return t instanceof p?new p(this.r*t.r,this.g*t.g,this.b*t.b,this.a*t.a):new p(this.r*t,this.g*t,this.b*t,this.a*t)}clamp(){this._r=Math.max(0,Math.min(255,this._r)),this._g=Math.max(0,Math.min(255,this._g)),this._b=Math.max(0,Math.min(255,this._b)),this._a=Math.max(0,Math.min(255,this._a)),this.updateUint()}static Red=new p(255,0,0,255);static Green=new p(0,255,0,255);static Blue=new p(0,0,255,255);static Yellow=new p(255,255,0,255);static Purple=new p(128,0,128,255);static Orange=new p(255,165,0,255);static Pink=new p(255,192,203,255);static Gray=new p(128,128,128,255);static Brown=new p(139,69,19,255);static Cyan=new p(0,255,255,255);static Magenta=new p(255,0,255,255);static Lime=new p(192,255,0,255);static White=new p(255,255,255,255);static Black=new p(0,0,0,255);static Transparent=new p(0,0,0,0)}var q=(n=>(n[n.LINEAR=0]="LINEAR",n[n.NEAREST=1]="NEAREST",n))(q||{}),j=(n=>(n[n.REPEAT=0]="REPEAT",n[n.CLAMP=1]="CLAMP",n[n.MIRRORED_REPEAT=2]="MIRRORED_REPEAT",n))(j||{});class vt{render;cache=new Map;texture=new Set;constructor(t){this.render=t}addTexture(t){return this.texture.add(t),t}removeTexture(t){return this.texture.delete(t),t}async load(t,e){let r=this.cache.get(t);if(r)return this.addTexture(new W(r));try{const i=await this._fetchImage(t);return r=X.fromSource(this.render,i,e),r.uid=t,this.cache.set(t,r),this.addTexture(new W(r))}catch(i){throw console.error(`[TextureManager] Failed to load: ${t}`,i),i}}create(t,e){if(e?.key&&this.cache.has(e.key))return this.addTexture(new W(this.cache.get(e.key)));const r=X.fromSource(this.render,t,e);return e?.key&&(r.uid=e.key,this.cache.set(e.key,r)),this.addTexture(new W(r))}createRenderTexture(t){return this.addTexture(new yt(this.render,t))}createTextTexture(t){return this.addTexture(new Tt(this.render,t))}destroy(t,e=!1){let r,i;typeof t=="string"?(i=t,r=this.cache.get(i)):t instanceof W?(r=t.base,i=r?.uid,this.removeTexture(t),t.destroy()):(r=t,i=r.uid),r&&(r.refCount<=0||e)&&(r.destroy(this.render.gl),i&&this.cache.delete(i))}destroyAll(){for(const t of this.cache.values())t.destroy(this.render.gl);this.cache.clear()}_fetchImage(t){return new Promise((e,r)=>{const i=new Image;i.crossOrigin="anonymous",i.onload=()=>e(i),i.onerror=()=>r(new Error(`Failed to load image resource: ${t}`)),i.src=t})}}class X{glTexture=null;width;height;uid;refCount=0;constructor(t,e,r){this.glTexture=t,this.width=e,this.height=r}static fromSource(t,e,r={}){const i=st(t,e,r);return new X(i,e.width,e.height)}updateSource(t,e,r={}){this.glTexture&&(t.bindTexture(t.TEXTURE_2D,this.glTexture),r.textureFilter!==void 0&&ct(t,r.textureFilter),r.wrap!==void 0&&ht(t,r.wrap),t.pixelStorei(t.UNPACK_FLIP_Y_WEBGL,1),t.pixelStorei(t.UNPACK_ALIGNMENT,1),r.premultipliedAlpha!==void 0&&t.pixelStorei(t.UNPACK_PREMULTIPLY_ALPHA_WEBGL,r.premultipliedAlpha),this.width===e.width&&this.height===e.height?t.texSubImage2D(t.TEXTURE_2D,0,0,0,t.RGBA,t.UNSIGNED_BYTE,e):(t.texImage2D(t.TEXTURE_2D,0,t.RGBA,t.RGBA,t.UNSIGNED_BYTE,e),this.width=e.width,this.height=e.height),r.premultipliedAlpha!==void 0&&t.pixelStorei(t.UNPACK_PREMULTIPLY_ALPHA_WEBGL,!1))}setFilterMode(t,e){this.glTexture&&(t.bindTexture(t.TEXTURE_2D,this.glTexture),ct(t,e),t.bindTexture(t.TEXTURE_2D,null))}setWrapMode(t,e){this.glTexture&&(t.bindTexture(t.TEXTURE_2D,this.glTexture),ht(t,e),t.bindTexture(t.TEXTURE_2D,null))}destroy(t){this.glTexture&&(t.deleteTexture(this.glTexture),this.glTexture=null)}}class W{base;uvX=0;uvY=0;uvW=1;uvH=1;rawWidth=0;rawHeight=0;offsetX=0;offsetY=0;scale=1;isAtlas=!1;flipY=!1;isRotated=!1;glTexture=null;get width(){return this.isRotated?this.rawHeight:this.rawWidth}get height(){return this.isRotated?this.rawWidth:this.rawHeight}_px=0;_py=0;constructor(t){t&&this.setBase(t)}updateResolution(){}setBase(t){return this.base===t?this:(this.base&&this.base.refCount--,this.base=t,this.base.refCount++,this.setRegion(0,0,t.width,t.height),this.glTexture=t.glTexture,this)}setRegion(t,e,r,i){return this.base?(this._px=t,this._py=e,this.isAtlas=t!==0||e!==0||r!==this.base.width||i!==this.base.height,this.uvX=t/this.base.width,this.uvY=e/this.base.height,this.uvW=this.uvX+r/this.base.width,this.uvH=this.uvY+i/this.base.height,this.rawWidth=r*this.scale,this.rawHeight=i*this.scale,this):this}getSubTexture(t,e,r,i){if(!this.base)return new W;const s=this.clone(),a=this._px,o=this._py;return s.setRegion(a+t,o+e,r,i),s}clone(t){const e=t??new W(this.base);return t&&this.base&&t.setBase(this.base),e.scale=this.scale,e.uvX=this.uvX,e.uvY=this.uvY,e.uvW=this.uvW,e.uvH=this.uvH,e.rawWidth=this.rawWidth,e.rawHeight=this.rawHeight,e.offsetX=this.offsetX,e.offsetY=this.offsetY,e.flipY=this.flipY,e.isRotated=this.isRotated,e.isAtlas=this.isAtlas,e._px=this._px,e._py=this._py,e}splitGrid(t,e,r,i,s=0){if(!this.base)return[];const a=[],o=this.rawWidth,h=this.rawHeight,c=Math.max(0,s),u=t+c,l=e+c,d=r??Math.floor((o+c)/u),f=i??Math.floor((h+c)/l);for(let m=0;m<f;m++)for(let x=0;x<d;x++)a.push(this.getSubTexture(x*u,m*l,t,e));return a}static fromImageSource(t,e,r={}){const i=st(t,e,r);return new W(new X(i,e.width,e.height))}destroy(){this.base&&(this.base.refCount--,this.base=void 0),this.glTexture=null}}class yt extends W{framebuffer;renderbuffer;gl;premultipliedAlpha;clearColor;_allocW=0;_allocH=0;constructor(t,e){super(),this.gl=t.gl,this.premultipliedAlpha=t.premultipliedAlpha,this.clearColor=e.clearColor??p.Black;const r=Math.max(Math.round(e.width),1),i=Math.max(Math.round(e.height),1),a=st(t,{width:r,height:i},{...e,onlySize:!0});if(this.setBase(new X(a,r,i)),this.framebuffer=this.gl.createFramebuffer(),this.renderbuffer=this.gl.createRenderbuffer(),!this.framebuffer||!this.renderbuffer)throw new Error("Failed to create Framebuffer or Renderbuffer");this.resize(r,i,!0)}resize(t,e,r=!1){t=Math.max(Math.round(t),1),e=Math.max(Math.round(e),1);const i=this.rawWidth===t&&this.rawHeight===e;if(!r&&i)return;const s=this.gl;(r||t>this._allocW||e>this._allocH)&&this.base&&(this._allocW=Math.max(this._allocW,t),this._allocH=Math.max(this._allocH,e),this.base.width=this._allocW,this.base.height=this._allocH,s.bindTexture(s.TEXTURE_2D,this.base.glTexture),s.texImage2D(s.TEXTURE_2D,0,s.RGBA,this._allocW,this._allocH,0,s.RGBA,s.UNSIGNED_BYTE,null),s.bindRenderbuffer(s.RENDERBUFFER,this.renderbuffer),s.renderbufferStorage(s.RENDERBUFFER,s.STENCIL_INDEX8,this._allocW,this._allocH),s.bindFramebuffer(s.FRAMEBUFFER,this.framebuffer),s.framebufferTexture2D(s.FRAMEBUFFER,s.COLOR_ATTACHMENT0,s.TEXTURE_2D,this.base.glTexture,0),s.framebufferRenderbuffer(s.FRAMEBUFFER,s.STENCIL_ATTACHMENT,s.RENDERBUFFER,this.renderbuffer),s.bindFramebuffer(s.FRAMEBUFFER,null),s.bindRenderbuffer(s.RENDERBUFFER,null),s.bindTexture(s.TEXTURE_2D,null)),this.rawWidth=t,this.rawHeight=e,this.uvX=0,this.uvW=t/this._allocW,this.uvY=0,this.uvH=e/this._allocH}activate(t=!1){if(!this.framebuffer)return;const e=this.gl;e.bindFramebuffer(e.FRAMEBUFFER,this.framebuffer),e.viewport(0,0,this.rawWidth,this.rawHeight),t&&this.clear()}clear(t){if(!this.framebuffer)return;const e=this.gl,r=t??this.clearColor;r&&(r.setClearColor(e,this.premultipliedAlpha),e.clear(e.COLOR_BUFFER_BIT|e.STENCIL_BUFFER_BIT))}deactivate(){const t=this.gl;t.bindFramebuffer(t.FRAMEBUFFER,null)}GetPixels(t=0,e=0,r=this.rawWidth,i=this.rawHeight){if(!this.framebuffer)return new Uint8Array(0);const s=Math.floor(t),a=Math.floor(e),o=Math.floor(r),h=Math.floor(i);if(o<=0||h<=0)return new Uint8Array(0);if(s<0||a<0||s>=this.rawWidth||a>=this.rawHeight)return new Uint8Array(0);const c=Math.min(o,this.rawWidth-s),u=Math.min(h,this.rawHeight-a),l=this.gl,d=l.getParameter(l.FRAMEBUFFER_BINDING);l.bindFramebuffer(l.FRAMEBUFFER,this.framebuffer);const f=new Uint8Array(c*u*4);return l.readPixels(s,a,c,u,l.RGBA,l.UNSIGNED_BYTE,f),l.bindFramebuffer(l.FRAMEBUFFER,d),f}GetColorAt(t,e){const r=this.GetPixels(t,e,1,1);return r.length<4?new p(0,0,0,0):new p(r[0],r[1],r[2],r[3])}destroy(){super.destroy(),this.framebuffer&&this.gl.deleteFramebuffer(this.framebuffer),this.renderbuffer&&this.gl.deleteRenderbuffer(this.renderbuffer),this.base?.destroy(this.gl)}}const jt={fontFamily:"Arial",fontSize:24,fontWeight:"normal",fill:"#000000",strokeThickness:0,align:"left",lineHeight:1};class Tt extends W{canvas;ctx;render;_text;_style;options;resolution=1;constructor(t,e){super(),this.render=t,this._style={...jt,...e},this._text=e?.text??"",this.options={premultipliedAlpha:t.premultipliedAlpha,...e,textureFilter:1},this.canvas=document.createElement("canvas");const r=this.canvas.getContext("2d",{willReadFrequently:!0});if(!r)throw new Error("Failed to get 2d context for TextTexture");this.ctx=r,this.canvas.width=1,this.canvas.height=1;const i=st(t,this.canvas,this.options);this.setBase(new X(i,1,1)),this.updateResolution(),this.update()}get text(){return this._text}set text(t){this._text!==t&&(this._text=t,this.update())}get style(){return this._style}set style(t){this._style={...this._style,...t},this.update()}updateOffset(t,e){switch(this._style.align){case"center":return t/2;case"right":return t-e;default:return e}}updateResolution(){let t=1;t*=this.render.viewport.resolution,t!=this.resolution&&(this.resolution=t,this.update())}update(){const t=this.ctx,e=this.style,r=e.fontSize,i=e.fontWeight,s=e.fontFamily,a=e.lineHeight,h=Math.max(r,32),c=r/h,u=`${i} ${r}px ${s}`,l=`${i} ${h}px ${s}`,d=this.resolution,f=e.baseline??"top",m=e.align??"left",x=e.strokeThickness??0,y=x>0,v=this._text.split(`
2
+ `),E="国g",M=v[0]||E;t.font=l,t.textBaseline="alphabetic",t.textAlign="left";let C=0,_=0,F=0;for(const D of v){const $=t.measureText(D||E);C=Math.max(C,D?$.width*c:0),D==v.at(0)&&(_=$.actualBoundingBoxAscent*c),D==v.at(-1)&&(F=$.actualBoundingBoxDescent*c)}t.textBaseline="alphabetic";const U=t.measureText(M).fontBoundingBoxDescent*c;t.textBaseline=f;const w=t.measureText(M).fontBoundingBoxDescent*c,R=r*a,S=_+F+(v.length-1)*R,O=x/2+2/d,G=C+O*2,H=S+O*2,I=Math.max(1,Math.ceil(G*d)),B=Math.max(1,Math.ceil(H*d)),z=I/d;(this.canvas.width!==I||this.canvas.height!==B)&&(this.canvas.width=I,this.canvas.height=B),t.setTransform(d,0,0,-d,0,B),t.clearRect(0,0,I/d,B/d),t.font=u,t.textBaseline="alphabetic",t.textAlign=m;const V=this.updateOffset(z,O);y&&(t.lineWidth=x,t.strokeStyle=e.stroke??"#000000",t.lineJoin="round"),e.fill&&(t.fillStyle=e.fill);let P=O+_;for(const D of v)y&&t.strokeText(D,V,P),e.fill&&t.fillText(D,V,P),P+=R;const it=w-U;this.scale=1/this.resolution,this.offsetX=-V,this.offsetY=-O-_+it,this.base?.updateSource(this.render.gl,this.canvas,this.options),this.setRegion(0,0,I,B)}}const Kt=(n,t=!1,e=!0)=>{const r=n.getContext("webgl2",{stencil:!0,antialias:t,premultipliedAlpha:e});if(!r)throw new Error("WebGL2 is not supported in this browser.");return r},wt=(n,t,e)=>{const r=n.createShader(e);if(!r)throw new Error("Unable to create webgl shader");if(n.shaderSource(r,t),n.compileShader(r),!n.getShaderParameter(r,n.COMPILE_STATUS)){const s=n.getShaderInfoLog(r);throw console.error("Shader compilation failed:",s),new Error("Unable to compile shader: "+s+t)}return r},qt=(n,t,e)=>{var r=n.createProgram(),i=wt(n,t,35633),s=wt(n,e,35632);if(!r)throw new Error("Unable to create program shader");if(n.attachShader(r,i),n.attachShader(r,s),n.linkProgram(r),!n.getProgramParameter(r,n.LINK_STATUS)){const o=n.getProgramInfoLog(r);throw new Error("Unable to link shader program: "+o)}return r};function ht(n,t){const e={[j.CLAMP]:n.CLAMP_TO_EDGE,[j.REPEAT]:n.REPEAT,[j.MIRRORED_REPEAT]:n.MIRRORED_REPEAT}[t]||n.CLAMP_TO_EDGE;n.texParameteri(n.TEXTURE_2D,n.TEXTURE_WRAP_S,e),n.texParameteri(n.TEXTURE_2D,n.TEXTURE_WRAP_T,e)}function ct(n,t){const e={[q.LINEAR]:n.LINEAR,[q.NEAREST]:n.NEAREST}[t]??n.NEAREST;n.texParameteri(n.TEXTURE_2D,n.TEXTURE_MIN_FILTER,e),n.texParameteri(n.TEXTURE_2D,n.TEXTURE_MAG_FILTER,e)}function st(n,t,e){const r=n.gl,i=r.createTexture();if(!i)throw new Error("Unable to create WebGL texture");if(r.bindTexture(r.TEXTURE_2D,i),r.pixelStorei(r.UNPACK_FLIP_Y_WEBGL,0),r.pixelStorei(r.UNPACK_ALIGNMENT,1),ht(r,e.wrap??j.CLAMP),ct(r,e.textureFilter??n.textureFilter),e.onlySize){const s=t;r.texImage2D(r.TEXTURE_2D,0,r.RGBA,s.width,s.height,0,r.RGBA,r.UNSIGNED_BYTE,null)}else{const s=e.premultipliedAlpha??n.premultipliedAlpha;s&&r.pixelStorei(r.UNPACK_PREMULTIPLY_ALPHA_WEBGL,!0),r.texImage2D(r.TEXTURE_2D,0,r.RGBA,r.RGBA,r.UNSIGNED_BYTE,t),s&&r.pixelStorei(r.UNPACK_PREMULTIPLY_ALPHA_WEBGL,!1)}return r.bindTexture(r.TEXTURE_2D,null),i}function Z(n,t,e="vTextureId",r="uv",i="return"){if(n.includes("%TEXTURE_NUM%")&&(n=n.replaceAll("%TEXTURE_NUM%",t.toString())),n.includes("%GET_COLOR%")){let s="";for(let a=0;a<t;a++)a==0?s+=`if(${e} == ${a})`:a==t-1?s+="else":s+=`else if(${e} == ${a})`,s+=`{${i} texture(uTextures[${a}], ${r});}`;n=n.replaceAll("%GET_COLOR%",s)}return n}const Zt=5126,ut=5121;function Qt(n,t,e){n.vertexAttribDivisor(t,e)}function Et(n,t,e,r,i){n.drawArraysInstanced(t,e,r,i)}let Jt=0;class lt{program;attributeLoc={};uniformLoc={};isCustom=!1;padding=0;uniformType={};uniformIsArray={};gl;shaderId=Jt++;vao;constructor(t,e,r,i){this.gl=t,this.program=qt(t,e,r),this.parseShader(e),this.parseShader(r),this.vao=t.createVertexArray(),i&&this.setAttributes(i)}use(){this.gl.useProgram(this.program)}bindVAO(){this.gl.bindVertexArray(this.vao)}unbindVAO(){this.gl.bindVertexArray(null)}setUniform(t,e){const r=this.uniformLoc[t];if(r===void 0)return;const i=this.uniformType[t],s=this.uniformIsArray[t];te(this.gl,r,i,e,s)}setUniforms(t){for(const e in t)this.setUniform(e,t[e])}setAttribute(t){const e=this.attributeLoc[t.name];if(e===void 0)return;const r=this.gl;r.vertexAttribPointer(e,t.size,t.type??Zt,t.normalized??!1,t.stride,t.offset??0),r.enableVertexAttribArray(e),Qt(r,e,t.divisor??0)}setAttributes(t){for(const e of t)this.setAttribute(e)}destroy(){this.gl.deleteProgram(this.program)}parseShader(t){const e=this.gl,r=t.match(/(?:in|attribute)\s+\w+\s+\w+/g);if(r)for(const s of r){const o=s.split(/\s+/)[2],h=e.getAttribLocation(this.program,o);h!==-1&&(this.attributeLoc[o]=h)}const i=t.matchAll(/uniform\s+(?:(?:lowp|mediump|highp)\s+)?(\w+)\s+(\w+)(\s*\[\s*\d+\s*\])?/g);for(const s of i){const a=s[1],o=s[2],h=e.getUniformLocation(this.program,o);h!==null&&(this.uniformLoc[o]=h,this.uniformType[o]=a,this.uniformIsArray[o]=s[3]!==void 0)}}}function te(n,t,e,r,i=!1){switch(e){case"float":return i?n.uniform1fv(t,r):n.uniform1f(t,r);case"int":case"bool":case"sampler2D":case"samplerCube":return i?n.uniform1iv(t,r):n.uniform1i(t,r);case"vec2":return n.uniform2fv(t,r);case"vec3":return n.uniform3fv(t,r);case"vec4":return n.uniform4fv(t,r);case"ivec2":case"bvec2":return n.uniform2iv(t,r);case"ivec3":case"bvec3":return n.uniform3iv(t,r);case"ivec4":case"bvec4":return n.uniform4iv(t,r);case"mat2":return n.uniformMatrix2fv(t,!1,r);case"mat3":return n.uniformMatrix3fv(t,!1,r);case"mat4":return n.uniformMatrix4fv(t,!1,r);case"mat3x2":return n.uniformMatrix3x2fv(t,!1,r);default:return ee(n,t,r)}}function ee(n,t,e){if(typeof e=="number")return n.uniform1f(t,e);const r=e;switch(r.length){case 2:return n.uniform2fv(t,r);case 3:return n.uniform3fv(t,r);case 4:return n.uniform4fv(t,r);case 9:return n.uniformMatrix3fv(t,!1,r);case 16:return n.uniformMatrix4fv(t,!1,r);default:return n.uniform1fv(t,r)}}class nt{vs;fs;glshader=new Map;uniforms={};uniformDirty=new Set;uniformTextures={};uniformArrayTexture={};usedTextureUnitNum=0;padding=0;rapid;prefix=[];constructor(t,e="",r="",i=0,s){this.vs=e,this.fs=r,this.rapid=t,this.uniforms=s??{},this.usedTextureUnitNum=i,this.beforePreCompile(),this.preCompile()}beforePreCompile(){}preCompile(){}setUniforms(t){this.rapid.flush();const e={},r={},i={};for(const[s,a]of Object.entries(t))a instanceof WebGLTexture?r[s]=a:Array.isArray(a)&&a[0]instanceof WebGLTexture?i[s]=a:e[s]=a;Object.assign(this.uniforms,e),Object.assign(this.uniformTextures,r),Object.assign(this.uniformArrayTexture,i);for(const s of this.glshader.keys())this.uniformDirty.add(s)}setPadding(t){this.padding=t;for(const e of this.glshader.values())e.padding=t;return this}applyUniform(t,e){const r=this.glshader.get(t);r&&(Object.keys(e).length>0&&r.setUniforms(e),this.uniformDirty.has(t)&&(r.setUniforms(this.uniforms),this.uniformDirty.delete(t)))}replaceCustomCode(t,e){return t=t.replace("// CUSTOM_CODE",e+"// CUSTOM_CODE"),t}getGLShader(t,e,r,i){if(this.glshader.has(e))return this.glshader.get(e);if(i==null||r==null)return console.warn("CustomGlShader: missing base shader for "+e),null;const s=this.prefix.length==0?[""]:this.prefix;r=r.replace("// CUSTOM_CODE_CALL",s.map(o=>o+"vertex(position, vRegion);").join("")),r=this.replaceCustomCode(r,this.vs),i=i.replace("// CUSTOM_CODE_CALL",s.map(o=>o+"fragment(fragColor, vRegion);").join("")),i=this.replaceCustomCode(i,this.fs);const a=t.createShader(r,i);return a.isCustom=!0,a.padding=this.padding,this.glshader.set(e,a),this.uniformDirty.add(e),a}destroy(){this.glshader.forEach(t=>t.destroy())}}class Rt{constructor(t){this.rapid=t,this.gl=t.gl,this.maxTextureUnits=t.maxTextureUnits,this.matrixStore=t.matrix}defaultShader;currentShader;customShader=null;customShaderUsedTextureNum=0;gl;maxTextureUnits;matrixStore;isDefaultShader=!0;KEY="default";vs="";fs="";usedTextures=[];usedTexturePadding=[];extraTextureUnit=0;createBuffer(){}createDefaultShader(){}get freeTextureUnitNum(){return this.maxTextureUnits-this.usedTextures.length-this.customShaderUsedTextureNum}getTextureUnitList(){return Array.from({length:this.maxTextureUnits},(t,e)=>e)}findTextureUnit(t,e=0,r=0){for(let i=0;i<this.usedTextures.length;i++){const s=i*2;if(this.usedTextures[i]===t&&this.usedTexturePadding[s]===e&&this.usedTexturePadding[s+1]===r)return i}return-1}useTexture(t,e=0,r=0){const i=this.findTextureUnit(t,e,r);return i==-1?(this.freeTextureUnitNum===0&&this.flush(),this.usedTextures.push(t),this.usedTexturePadding.push(e),this.usedTexturePadding.push(r),this.usedTextures.length-1):i}getMaxTexutreUnit(t){return this.rapid.maxTextureUnits-this.extraTextureUnit-(t?.usedTextureUnitNum??0)}createShader(t,e){const r=this.getMaxTexutreUnit();return e=Z(e,r),t=Z(t,r),new lt(this.gl,t,e)}createCustomShader(t){const e=this.getMaxTexutreUnit(t),r=Z(this.fs,e),i=Z(this.vs,e);return t.getGLShader(this,this.KEY,i,r)}getCustomShader(t){if(!t)return this.defaultShader;if(t instanceof nt){const e=this.createCustomShader(t);return e??this.defaultShader}return t}isSameShader(t){return this.isDefaultShader&&!t?!0:this.getCustomShader(t)==this.currentShader}enter(t){this.resetRender(),this.currentShader=this.getCustomShader(t),t instanceof nt?(this.customShader=t,this.customShaderUsedTextureNum=t.usedTextureUnitNum):(this.customShader=null,this.customShaderUsedTextureNum=0),this.isDefaultShader=this.currentShader==this.defaultShader,this.currentShader.use(),this.currentShader.setUniform("u_projection",this.rapid.projection)}exit(){this.hasPendingContent()&&this.flush(),this.gl.bindVertexArray(null)}flush(){this.render(),this.resetRender()}render(){this.prepareRender()}prepareRender(){const t=this.currentShader;if(t.use(),this.customShader){const r=this.customShader,i={};for(const[s,a]of Object.entries(r.uniformTextures))i[s]=this.useTexture(a);for(const[s,a]of Object.entries(r.uniformArrayTexture))i[s]=a.map(o=>this.useTexture(o));this.customShader.applyUniform(this.KEY,i)}const e=this.gl;for(let r=0;r<this.usedTextures.length;r++)e.activeTexture(e.TEXTURE0+r),e.bindTexture(e.TEXTURE_2D,this.usedTextures[r]);t.setUniform("uTextures",Int32Array.from({length:this.usedTextures.length},(r,i)=>i)),t.setUniform("uPadding",Float32Array.from({length:this.usedTexturePadding.length},(r,i)=>this.usedTexturePadding[i]))}resetRender(){const t=this.gl;for(let e=0;e<this.usedTextures.length;e++)t.activeTexture(t.TEXTURE0+e),t.bindTexture(t.TEXTURE_2D,null);this.usedTextures.length=0,this.usedTexturePadding.length=0}hasPendingContent(){return!1}}var L=(n=>(n[n.Float32=0]="Float32",n[n.Uint32=1]="Uint32",n[n.Uint16=2]="Uint16",n))(L||{});class N{usedElemNum;typedArray;arrayType;maxElemNum;bytePerElem;arraybuffer;uint32;float32;uint16;constructor(t){this.usedElemNum=0,this.maxElemNum=512,this.bytePerElem=this.getArrayType(t).BYTES_PER_ELEMENT,this.arrayType=t,this.arraybuffer=new ArrayBuffer(this.maxElemNum*this.bytePerElem),this.updateTypedArray()}getArrayType(t){switch(t){case 0:return Float32Array;case 1:return Uint32Array;case 2:return Uint16Array;default:throw new Error("Unsupported ArrayType")}}updateTypedArray(){switch(this.uint32=new Uint32Array(this.arraybuffer),this.float32=new Float32Array(this.arraybuffer),this.uint16=new Uint16Array(this.arraybuffer),this.arrayType){case 0:this.typedArray=this.float32;break;case 1:this.typedArray=this.uint32;break;case 2:this.typedArray=this.uint16;break}}clear(){this.usedElemNum=0}static removeAtIndices(t,e){const r=Math.max(...e.map(o=>o.usedElemNum)),i=[...new Set(t)].filter(o=>o>=0&&o<r).sort((o,h)=>o-h);if(i.length===0)return 0;let s=0,a=0;for(let o=0;o<r;o++){if(a<i.length&&o===i[a]){a++;continue}if(s!==o)for(const h of e)h.typedArray[s]=h.typedArray[o];s++}for(const o of e)o.usedElemNum=s;return r-s}resize(t=0){if(t+=this.usedElemNum,t>this.maxElemNum){for(;t>this.maxElemNum;)this.maxElemNum*=2;return this.setMaxSize(this.maxElemNum),!0}return!1}setMaxSize(t=this.maxElemNum){const e=this.typedArray;this.maxElemNum=t,this.arraybuffer=new ArrayBuffer(t*this.bytePerElem),this.updateTypedArray(),this.typedArray.set(e)}pushUint32(t){this.resize(1),this.uint32[this.usedElemNum++]=t}pushFloat32(t){this.resize(1),this.float32[this.usedElemNum++]=t}push(t){this.resize(1),this.typedArray[this.usedElemNum++]=t}pop(){return this.typedArray[--this.usedElemNum]}top(){return this.typedArray[this.usedElemNum-1]}get(t){return this.typedArray[t]}getArray(t=0,e){return e===void 0?this.typedArray:this.typedArray.subarray(t,e)}get length(){return this.usedElemNum}}class dt extends N{buffer;gl;dirty=!0;type;webglBufferSize=0;usage;constructor(t,e,r=t.ARRAY_BUFFER,i=t.DYNAMIC_DRAW){super(e),this.gl=t,this.buffer=t.createBuffer(),this.type=r,this.usage=i}makeDirty(){this.dirty=!0}clear(){super.clear(),this.dirty=!1}bindBuffer(){this.gl.bindBuffer(this.type,this.buffer)}bufferData(){if(this.dirty){const t=this.gl;this.maxElemNum>this.webglBufferSize&&(t.bufferData(this.type,this.maxElemNum*this.bytePerElem,this.usage),this.webglBufferSize=this.maxElemNum),t.bufferSubData(this.type,0,this.getArray(0,this.usedElemNum)),this.dirty=!1}}}const at=`#version 300 es\r
3
3
  precision highp float;\r
4
4
  \r
5
5
  // per-vertex\r
@@ -84,7 +84,7 @@ void main(void) {\r
84
84
  \r
85
85
  // CUSTOM_CODE_CALL\r
86
86
  }\r
87
- `,Q=48,re=new Float32Array([0,0,1,0,0,1,1,1]);class ft extends Rt{instanceBuffer;quadBuffer;instanceCount=0;localSpriteMatrix=new Float32Array(6);worldSpriteMatrix=new Float32Array(6);KEY="Sprite";constructor(t){super(t),this.createBuffer(),this.createDefaultShader()}createBuffer(){const t=this.gl;this.quadBuffer=new dt(t,_.Float32,t.ARRAY_BUFFER,t.STATIC_DRAW),this.quadBuffer.bindBuffer();for(const e of re)this.quadBuffer.pushFloat32(e);this.quadBuffer.makeDirty(),this.quadBuffer.bufferData(),this.instanceBuffer=new dt(t,_.Uint32,t.ARRAY_BUFFER,t.DYNAMIC_DRAW)}createDefaultShader(){return this.vs=at,this.fs=Mt,this.defaultShader=this.createShader(at,Mt),this.defaultShader}createShader(t,e){const r=super.createShader(t,e);return r.use(),r.bindVAO(),this.instanceBuffer.bindBuffer(),r.setAttributes([{name:"aMatrixRow0",size:3,stride:Q,offset:0,divisor:1},{name:"aMatrixRow1",size:3,stride:Q,offset:12,divisor:1},{name:"aUVRect",size:4,stride:Q,offset:24,divisor:1},{name:"aColor",size:4,type:ut,normalized:!0,stride:Q,offset:40,divisor:1},{name:"aTextureId",size:1,type:ut,stride:Q,offset:44,divisor:1}]),this.quadBuffer.bindBuffer(),r.setAttributes([{name:"aVertex",size:2,stride:8,offset:0,divisor:0}]),r.unbindVAO(),this.currentShader&&this.currentShader.use(),r}drawSprite(t,e,r=0,i=0,s=1,a=1,o=4294967295,h=0,c=0,u=!1,l=!1,d=!1){const f=e*6,m=this.matrixStore.data;this.drawSpriteAffine(t,m[f],m[f+1],m[f+2],m[f+3],m[f+4],m[f+5],r,i,s,a,o,h,c,u,l,d)}drawSpriteAffine(t,e,r,i,s,a,o,h=0,c=0,u=1,l=1,d=4294967295,f=0,m=0,x=!1,T=!1,y=!1){const w=t.base,R=t.rawWidth,C=t.rawHeight,L=t.offsetX,B=t.offsetY,N=this.useTexture(t.glTexture,f/w.width,m/w.height),A=this.instanceBuffer,M=A.usedElemNum;A.resize(12);const U=A.float32,F=A.uint32,G=y?C:R,H=y?R:C,I=G+2*f,O=H+2*m,Y=L-f,V=B-m,S=this.localSpriteMatrix;y?(S[0]=0,S[1]=T?O:-O,S[2]=x?-I:I,S[3]=0,S[4]=Y+(x?I:0),S[5]=V+(T?0:O)):(S[0]=x?-I:I,S[1]=0,S[2]=0,S[3]=T?-O:O,S[4]=Y+(x?I:0),S[5]=V+(T?O:0));const b=this.worldSpriteMatrix;b[0]=e*S[0]+i*S[1],b[1]=r*S[0]+s*S[1],b[2]=e*S[2]+i*S[3],b[3]=r*S[2]+s*S[3],b[4]=a+e*S[4]+i*S[5],b[5]=o+r*S[4]+s*S[5],U[M+6]=h,U[M+7]=c,U[M+8]=u,U[M+9]=l,F[M+10]=d,F[M+11]=N,this.rapid.roundPixels&&(b[4]=Math.round(b[4]),b[5]=Math.round(b[5])),U[M]=b[0],U[M+1]=b[2],U[M+2]=b[4],U[M+3]=b[1],U[M+4]=b[3],U[M+5]=b[5],A.usedElemNum+=12,A.makeDirty(),this.instanceCount++}render(){if(this.instanceCount===0)return;this.prepareRender();const t=this.gl,e=this.currentShader;this.instanceBuffer.bindBuffer(),this.instanceBuffer.bufferData(),e.bindVAO(),Et(t,t.TRIANGLE_STRIP,0,4,this.instanceCount),this.rapid.drawcallCount++}resetRender(){super.resetRender(),this.instanceBuffer.clear(),this.instanceCount=0}hasPendingContent(){return this.instanceCount>0}}const E=6;class ie{matrixCount=0;buffer;data;freeFlag;freeMatrixStack;temporary=-1;identityMatrix=-1;constructor(t=10){this.buffer=new P(_.Float32),this.buffer.resize(t*E),this.data=this.buffer.getArray(),this.freeFlag=new P(_.Uint32),this.freeMatrixStack=new P(_.Uint32),this.reset()}alloc(){const t=this.allocDirty();return this.identity(t),t}allocDirty(){if(this.freeMatrixStack.length>0){const e=this.freeMatrixStack.pop();return this.freeFlag.typedArray[e]=1,e}this.buffer.resize(E)&&(this.data=this.buffer.getArray()),this.buffer.usedElemNum+=E,this.freeFlag.resize(1);const t=this.matrixCount++;return this.freeFlag.typedArray[this.freeFlag.usedElemNum++]=1,t}free(t){!Number.isInteger(t)||t<0||t>=this.matrixCount||this.freeFlag.typedArray[t]!==0&&(this.freeFlag.typedArray[t]=0,this.freeMatrixStack.push(t))}reset(){this.matrixCount=0,this.buffer.usedElemNum=0,this.freeFlag.clear(),this.freeMatrixStack.clear(),this.temporary=this.alloc(),this.identityMatrix=this.alloc()}identity(t){const e=t*E,r=this.data;r[e]=1,r[e+1]=0,r[e+2]=0,r[e+3]=1,r[e+4]=0,r[e+5]=0}translate(t,e,r){const i=t*E,s=this.data;s[i+4]=s[i]*e+s[i+2]*r+s[i+4],s[i+5]=s[i+1]*e+s[i+3]*r+s[i+5]}scale(t,e,r){const i=t*E,s=this.data;s[i]*=e,s[i+1]*=e,s[i+2]*=r,s[i+3]*=r}rotate(t,e){const r=t*E,i=this.data,s=Math.cos(e),a=Math.sin(e),o=i[r],h=i[r+1],c=i[r+2],u=i[r+3];i[r]=o*s+c*a,i[r+1]=h*s+u*a,i[r+2]=o*-a+c*s,i[r+3]=h*-a+u*s}rotateWithOffset(t,e,r,i){this.translate(t,r,i),this.rotate(t,e),this.translate(t,-r,-i)}copy(t,e){const r=t*E,i=e*E,s=this.data;s[r]=s[i],s[r+1]=s[i+1],s[r+2]=s[i+2],s[r+3]=s[i+3],s[r+4]=s[i+4],s[r+5]=s[i+5]}multiply(t,e){this.multiplyOut(t,t,e)}multiplyOut(t,e,r){const i=t*E,s=e*E,a=r*E,o=this.data,h=o[s],c=o[s+1],u=o[s+2],l=o[s+3],d=o[s+4],f=o[s+5],m=o[a],x=o[a+1],T=o[a+2],y=o[a+3],w=o[a+4],R=o[a+5];o[i]=h*m+u*x,o[i+1]=c*m+l*x,o[i+2]=h*T+u*y,o[i+3]=c*T+l*y,o[i+4]=h*w+u*R+d,o[i+5]=c*w+l*R+f}invert(t){const e=t*E,r=this.data,i=r[e],s=r[e+1],a=r[e+2],o=r[e+3],h=r[e+4],c=r[e+5];let u=i*o-s*a;if(!u){this.identity(t);return}u=1/u,r[e]=o*u,r[e+1]=-s*u,r[e+2]=-a*u,r[e+3]=i*u,r[e+4]=(a*c-o*h)*u,r[e+5]=(s*h-i*c)*u}transformPoint(t,e,r){const i=t*E,s=this.data;return{x:e*s[i]+r*s[i+2]+s[i+4],y:e*s[i+1]+r*s[i+3]+s[i+5]}}worldToLocal(t,e,r){const i=this.allocDirty();this.copy(i,t),this.invert(i);const s=this.transformPoint(i,e,r);return this.free(i),s}localToWorld(t,e,r){return this.transformPoint(t,e,r)}getPosition(t){const e=t*E,r=this.data;return{x:r[e+4],y:r[e+5]}}getScale(t){const e=t*E,r=this.data,i=Math.sqrt(r[e]*r[e]+r[e+1]*r[e+1]),s=Math.sqrt(r[e+2]*r[e+2]+r[e+3]*r[e+3]);return{x:i,y:s}}getRotation(t){const e=t*E,r=this.data;return Math.atan2(r[e+1],r[e])}toCSSMatrix(t,e=1,r=1){const i=t*E,s=this.data;return`matrix(${s[i]*e}, ${s[i+1]*r}, ${s[i+2]*e}, ${s[i+3]*r}, ${s[i+4]*e}, ${s[i+5]*r})`}getMatrix(t){const e=t*E;return this.data.slice(e,e+E)}getMatrixRef(t){const e=t*E;return this.data.subarray(e,e+E)}setMatrix(t,e){const r=t*E,i=this.data;i[r]=e[0],i[r+1]=e[1],i[r+2]=e[2],i[r+3]=e[3],i[r+4]=e[4],i[r+5]=e[5]}multiplyAffineInPlace(t,e,r,i,s,a,o){const h=t*6,c=this.data,u=c[h],l=c[h+1],d=c[h+2],f=c[h+3],m=c[h+4],x=c[h+5];c[h]=u*e+d*r,c[h+1]=l*e+f*r,c[h+2]=u*i+d*s,c[h+3]=l*i+f*s,c[h+4]=u*a+d*o+m,c[h+5]=l*a+f*o+x}multiplyAffine(t,e,r,i,s,a,o,h){const c=t*6,u=e*6,l=this.data,d=l[c],f=l[c+1],m=l[c+2],x=l[c+3],T=l[c+4],y=l[c+5];l[u]=d*r+m*i,l[u+1]=f*r+x*i,l[u+2]=d*s+m*a,l[u+3]=f*s+x*a,l[u+4]=d*o+m*h+T,l[u+5]=f*o+x*h+y}clampBounds(t,e,r,i,s,a,o){if(i===void 0&&s===void 0&&a===void 0&&o===void 0)return;const h=t*E,c=this.data,u=Math.sqrt(c[h]*c[h]+c[h+1]*c[h+1])||1,l=Math.sqrt(c[h+2]*c[h+2]+c[h+3]*c[h+3])||1,d=e*.5*u,f=r*.5*l;let m=c[h+4],x=c[h+5];i!==void 0&&s!==void 0?m=s-i<d*2?(i+s)*.5:Math.max(i+d,Math.min(m,s-d)):i!==void 0?m=Math.max(m,i+d):s!==void 0&&(m=Math.min(m,s-d)),a!==void 0&&o!==void 0?x=o-a<f*2?(a+o)*.5:Math.max(a+f,Math.min(x,o-f)):a!==void 0?x=Math.max(x,a+f):o!==void 0&&(x=Math.min(x,o-f)),c[h+4]=m,c[h+5]=x}applyTransform(t,e=0,r=0){let i=t.x??0,s=t.y??0;t.position&&(i+=t.position.x,s+=t.position.y);let a=1,o=1;const h=t.scale;h!==void 0&&(typeof h=="number"?(a=h,o=h):(a=h.x,o=h.y));const c=t.rotation??0;let u=t.offsetX??0,l=t.offsetY??0;t.offset&&(u+=t.offset.x,l+=t.offset.y);const d=t.origin;d!==void 0&&(typeof d=="number"?(u-=d*e,l-=d*r):(u-=d.x*e,l-=d.y*r));const f=c?Math.cos(c):1,m=c?Math.sin(c):0,x=f*a,T=m*a,y=-m*o,w=f*o,R=i+x*u+y*l,C=s+T*u+w*l;return[x,T,y,w,R,C]}}class se{matrix=new ie;stack=new P(_.Uint32);currStack=new P(_.Uint32);curLocalM=-1;curWorldM=-1;rapid;constructor(t){this.reset(),this.rapid=t}save(t,e){this.stack.push(this.curLocalM),this.stack.push(this.curWorldM);const r=this.curWorldM;return this.curLocalM=e??this.matrix.alloc(),this.curWorldM=t??this.matrix.allocDirty(),t===void 0&&(e===void 0?this.matrix.copy(this.curWorldM,r):this.matrix.multiplyOut(this.curWorldM,r,this.curLocalM)),e===void 0&&this.currStack.push(this.curLocalM),t===void 0&&this.currStack.push(this.curWorldM),{parent:r,world:this.curWorldM,local:this.curLocalM}}restore(){this.stack.length!=0&&(this.curWorldM=this.stack.pop(),this.curLocalM=this.stack.pop())}restoreAll(){for(;this.stack.length>0;)this.restore()}retainMatrix(t){const e=new Set(typeof t=="number"?[t]:[t.local,t.world]),r=[];for(let i=0;i<this.currStack.length;i++)e.has(this.currStack.get(i))&&r.push(i);return P.removeAtIndices(r,[this.currStack]),t}translate(t,e){this.matrix.translate(this.curLocalM,t,e),this.matrix.translate(this.curWorldM,t,e)}scale(t,e=t){this.matrix.scale(this.curLocalM,t,e),this.matrix.scale(this.curWorldM,t,e)}rotate(t){this.matrix.rotate(this.curLocalM,t),this.matrix.rotate(this.curWorldM,t)}rotateWithOffset(t,e,r){this.matrix.rotateWithOffset(this.curLocalM,t,e,r),this.matrix.rotateWithOffset(this.curWorldM,t,e,r)}identity(){this.matrix.identity(this.curLocalM),this.matrix.identity(this.curWorldM)}reset(){for(let t=0;t<this.currStack.length;t++)this.matrix.free(this.currStack.typedArray[t]);this.currStack.clear(),this.stack.clear(),this.curLocalM=this.matrix.alloc(),this.curWorldM=this.matrix.alloc(),this.currStack.push(this.curLocalM),this.currStack.push(this.curWorldM)}localToWorld(t,e){return this.matrix.localToWorld(this.curWorldM,t,e)}worldToLocal(t,e){return this.matrix.worldToLocal(this.curWorldM,t,e)}getGlobalPosition(){return this.matrix.getPosition(this.curWorldM)}getGlobalScale(){return this.matrix.getScale(this.curWorldM)}getGlobalRotation(){return this.matrix.getRotation(this.curWorldM)}toCSSMatrix(){return this.matrix.toCSSMatrix(this.curWorldM)}getTransform(){const t=this.curWorldM*E;return this.matrix.data.slice(t,t+E)}setTransform(t){const e=this.curWorldM*E,r=this.matrix.data;r[e]=t[0],r[e+1]=t[1],r[e+2]=t[2],r[e+3]=t[3],r[e+4]=t[4],r[e+5]=t[5]}transformPoint(t,e){return this.matrix.transformPoint(this.curLocalM,t,e)}applyTransform(t,e=0,r=0,i,s=this.curWorldM,a=this.curLocalM){const[o,h,c,u,l,d]=this.matrix.applyTransform(t,e,r);i!=null?this.matrix.multiplyAffine(s,i,o,h,c,u,l,d):(this.matrix.multiplyAffineInPlace(a,o,h,c,u,l,d),this.matrix.multiplyAffineInPlace(s,o,h,c,u,l,d))}}const ne=`#version 300 es\r
87
+ `,Q=48,re=new Float32Array([0,0,1,0,0,1,1,1]);class ft extends Rt{instanceBuffer;quadBuffer;instanceCount=0;localSpriteMatrix=new Float32Array(6);worldSpriteMatrix=new Float32Array(6);KEY="Sprite";constructor(t){super(t),this.createBuffer(),this.createDefaultShader()}createBuffer(){const t=this.gl;this.quadBuffer=new dt(t,L.Float32,t.ARRAY_BUFFER,t.STATIC_DRAW),this.quadBuffer.bindBuffer();for(const e of re)this.quadBuffer.pushFloat32(e);this.quadBuffer.makeDirty(),this.quadBuffer.bufferData(),this.instanceBuffer=new dt(t,L.Uint32,t.ARRAY_BUFFER,t.DYNAMIC_DRAW)}createDefaultShader(){return this.vs=at,this.fs=Mt,this.defaultShader=this.createShader(at,Mt),this.defaultShader}createShader(t,e){const r=super.createShader(t,e);return r.use(),r.bindVAO(),this.instanceBuffer.bindBuffer(),r.setAttributes([{name:"aMatrixRow0",size:3,stride:Q,offset:0,divisor:1},{name:"aMatrixRow1",size:3,stride:Q,offset:12,divisor:1},{name:"aUVRect",size:4,stride:Q,offset:24,divisor:1},{name:"aColor",size:4,type:ut,normalized:!0,stride:Q,offset:40,divisor:1},{name:"aTextureId",size:1,type:ut,stride:Q,offset:44,divisor:1}]),this.quadBuffer.bindBuffer(),r.setAttributes([{name:"aVertex",size:2,stride:8,offset:0,divisor:0}]),r.unbindVAO(),this.currentShader&&this.currentShader.use(),r}drawSprite(t,e,r=0,i=0,s=1,a=1,o=4294967295,h=0,c=0,u=!1,l=!1,d=!1){const f=e*6,m=this.matrixStore.data;this.drawSpriteAffine(t,m[f],m[f+1],m[f+2],m[f+3],m[f+4],m[f+5],r,i,s,a,o,h,c,u,l,d)}drawSpriteAffine(t,e,r,i,s,a,o,h=0,c=0,u=1,l=1,d=4294967295,f=0,m=0,x=!1,y=!1,v=!1){const E=t.base,M=t.rawWidth,C=t.rawHeight,_=t.offsetX,F=t.offsetY,U=this.useTexture(t.glTexture,f/E.width,m/E.height),w=this.instanceBuffer,R=w.usedElemNum;w.resize(12);const S=w.float32,O=w.uint32,G=v?C:M,H=v?M:C,I=G+2*f,B=H+2*m,z=_-f,V=F-m,b=this.localSpriteMatrix;v?(b[0]=0,b[1]=y?B:-B,b[2]=x?-I:I,b[3]=0,b[4]=z+(x?I:0),b[5]=V+(y?0:B)):(b[0]=x?-I:I,b[1]=0,b[2]=0,b[3]=y?-B:B,b[4]=z+(x?I:0),b[5]=V+(y?B:0));const P=this.worldSpriteMatrix;P[0]=e*b[0]+i*b[1],P[1]=r*b[0]+s*b[1],P[2]=e*b[2]+i*b[3],P[3]=r*b[2]+s*b[3],P[4]=a+e*b[4]+i*b[5],P[5]=o+r*b[4]+s*b[5],S[R+6]=h,S[R+7]=c,S[R+8]=u,S[R+9]=l,O[R+10]=d,O[R+11]=U,this.rapid.roundPixels&&(P[4]=Math.round(P[4]),P[5]=Math.round(P[5])),S[R]=P[0],S[R+1]=P[2],S[R+2]=P[4],S[R+3]=P[1],S[R+4]=P[3],S[R+5]=P[5],w.usedElemNum+=12,w.makeDirty(),this.instanceCount++}render(){if(this.instanceCount===0)return;this.prepareRender();const t=this.gl,e=this.currentShader;this.instanceBuffer.bindBuffer(),this.instanceBuffer.bufferData(),e.bindVAO(),Et(t,t.TRIANGLE_STRIP,0,4,this.instanceCount),this.rapid.drawcallCount++}resetRender(){super.resetRender(),this.instanceBuffer.clear(),this.instanceCount=0}hasPendingContent(){return this.instanceCount>0}}const A=6;class ie{transformValues=[1,0,0,1,0,0];matrixCount=0;buffer;data;freeFlag;freeMatrixStack;temporary=-1;identityMatrix=-1;constructor(t=10){this.buffer=new N(L.Float32),this.buffer.resize(t*A),this.data=this.buffer.getArray(),this.freeFlag=new N(L.Uint32),this.freeMatrixStack=new N(L.Uint32),this.reset()}alloc(){const t=this.allocDirty();return this.identity(t),t}allocDirty(){if(this.freeMatrixStack.length>0){const e=this.freeMatrixStack.pop();return this.freeFlag.typedArray[e]=1,e}this.buffer.resize(A)&&(this.data=this.buffer.getArray()),this.buffer.usedElemNum+=A,this.freeFlag.resize(1);const t=this.matrixCount++;return this.freeFlag.typedArray[this.freeFlag.usedElemNum++]=1,t}free(t){if(typeof t!="number"){this.free(t.local),this.free(t.world);return}!Number.isInteger(t)||t<0||t>=this.matrixCount||this.freeFlag.typedArray[t]!==0&&(this.freeFlag.typedArray[t]=0,this.freeMatrixStack.push(t))}reset(){this.matrixCount=0,this.buffer.usedElemNum=0,this.freeFlag.clear(),this.freeMatrixStack.clear(),this.temporary=this.alloc(),this.identityMatrix=this.alloc()}identity(t){const e=t*A,r=this.data;r[e]=1,r[e+1]=0,r[e+2]=0,r[e+3]=1,r[e+4]=0,r[e+5]=0}translate(t,e,r){const i=t*A,s=this.data;s[i+4]=s[i]*e+s[i+2]*r+s[i+4],s[i+5]=s[i+1]*e+s[i+3]*r+s[i+5]}scale(t,e,r){const i=t*A,s=this.data;s[i]*=e,s[i+1]*=e,s[i+2]*=r,s[i+3]*=r}rotate(t,e){const r=t*A,i=this.data,s=Math.cos(e),a=Math.sin(e),o=i[r],h=i[r+1],c=i[r+2],u=i[r+3];i[r]=o*s+c*a,i[r+1]=h*s+u*a,i[r+2]=o*-a+c*s,i[r+3]=h*-a+u*s}rotateWithOffset(t,e,r,i){this.translate(t,r,i),this.rotate(t,e),this.translate(t,-r,-i)}copy(t,e){const r=t*A,i=e*A,s=this.data;s[r]=s[i],s[r+1]=s[i+1],s[r+2]=s[i+2],s[r+3]=s[i+3],s[r+4]=s[i+4],s[r+5]=s[i+5]}multiply(t,e){this.multiplyOut(t,t,e)}multiplyOut(t,e,r){const i=t*A,s=e*A,a=r*A,o=this.data,h=o[s],c=o[s+1],u=o[s+2],l=o[s+3],d=o[s+4],f=o[s+5],m=o[a],x=o[a+1],y=o[a+2],v=o[a+3],E=o[a+4],M=o[a+5];o[i]=h*m+u*x,o[i+1]=c*m+l*x,o[i+2]=h*y+u*v,o[i+3]=c*y+l*v,o[i+4]=h*E+u*M+d,o[i+5]=c*E+l*M+f}invert(t){const e=t*A,r=this.data,i=r[e],s=r[e+1],a=r[e+2],o=r[e+3],h=r[e+4],c=r[e+5];let u=i*o-s*a;if(!u){this.identity(t);return}u=1/u,r[e]=o*u,r[e+1]=-s*u,r[e+2]=-a*u,r[e+3]=i*u,r[e+4]=(a*c-o*h)*u,r[e+5]=(s*h-i*c)*u}transformPoint(t,e,r){const i=t*A,s=this.data;return{x:e*s[i]+r*s[i+2]+s[i+4],y:e*s[i+1]+r*s[i+3]+s[i+5]}}worldToLocal(t,e,r){const i=this.allocDirty();this.copy(i,t),this.invert(i);const s=this.transformPoint(i,e,r);return this.free(i),s}localToWorld(t,e,r){return this.transformPoint(t,e,r)}getPosition(t){const e=t*A,r=this.data;return{x:r[e+4],y:r[e+5]}}getScale(t){const e=t*A,r=this.data,i=Math.sqrt(r[e]*r[e]+r[e+1]*r[e+1]),s=Math.sqrt(r[e+2]*r[e+2]+r[e+3]*r[e+3]);return{x:i,y:s}}getRotation(t){const e=t*A,r=this.data;return Math.atan2(r[e+1],r[e])}toCSSMatrix(t,e=1,r=1){const i=t*A,s=this.data;return`matrix(${s[i]*e}, ${s[i+1]*r}, ${s[i+2]*e}, ${s[i+3]*r}, ${s[i+4]*e}, ${s[i+5]*r})`}getMatrix(t){const e=t*A;return this.data.slice(e,e+A)}getMatrixRef(t){const e=t*A;return this.data.subarray(e,e+A)}setMatrix(t,e){const r=t*A,i=this.data;i[r]=e[0],i[r+1]=e[1],i[r+2]=e[2],i[r+3]=e[3],i[r+4]=e[4],i[r+5]=e[5]}multiplyAffineInPlace(t,e,r,i,s,a,o){const h=t*6,c=this.data,u=c[h],l=c[h+1],d=c[h+2],f=c[h+3],m=c[h+4],x=c[h+5];c[h]=u*e+d*r,c[h+1]=l*e+f*r,c[h+2]=u*i+d*s,c[h+3]=l*i+f*s,c[h+4]=u*a+d*o+m,c[h+5]=l*a+f*o+x}multiplyAffine(t,e,r,i,s,a,o,h){const c=t*6,u=e*6,l=this.data,d=l[c],f=l[c+1],m=l[c+2],x=l[c+3],y=l[c+4],v=l[c+5];l[u]=d*r+m*i,l[u+1]=f*r+x*i,l[u+2]=d*s+m*a,l[u+3]=f*s+x*a,l[u+4]=d*o+m*h+y,l[u+5]=f*o+x*h+v}clampBounds(t,e,r,i,s,a,o){if(i===void 0&&s===void 0&&a===void 0&&o===void 0)return;const h=t*A,c=this.data,u=Math.sqrt(c[h]*c[h]+c[h+1]*c[h+1])||1,l=Math.sqrt(c[h+2]*c[h+2]+c[h+3]*c[h+3])||1,d=e*.5*u,f=r*.5*l;let m=c[h+4],x=c[h+5];i!==void 0&&s!==void 0?m=s-i<d*2?(i+s)*.5:Math.max(i+d,Math.min(m,s-d)):i!==void 0?m=Math.max(m,i+d):s!==void 0&&(m=Math.min(m,s-d)),a!==void 0&&o!==void 0?x=o-a<f*2?(a+o)*.5:Math.max(a+f,Math.min(x,o-f)):a!==void 0?x=Math.max(x,a+f):o!==void 0&&(x=Math.min(x,o-f)),c[h+4]=m,c[h+5]=x}applyTransform(t,e=0,r,i=1,s=0,a=0,o=0,h=0,c=0,u=0,l=0){let d,f,m=r??1;if(typeof t=="object"){h=e??0,c=r??0;const w=t;d=(w.x??0)+(w.position?.x??0),f=(w.y??0)+(w.position?.y??0);const R=w.scale;m=typeof R=="number"?R:R?.x??1,i=typeof R=="number"?R:R?.y??1,s=w.rotation??0;const S=w.origin;a=typeof S=="number"?S:S?.x??0,o=typeof S=="number"?S:S?.y??0,u=(w.offsetX??0)+(w.offset?.x??0),l=(w.offsetY??0)+(w.offset?.y??0)}else d=t,f=e;u-=a*h,l-=o*c;const x=s?Math.cos(s):1,y=s?Math.sin(s):0,v=x*m,E=y*m,M=-y*i,C=x*i,_=d+v*u+M*l,F=f+E*u+C*l,U=this.transformValues;return U[0]=v,U[1]=E,U[2]=M,U[3]=C,U[4]=_,U[5]=F,typeof t=="number"?U:U.slice()}}class se{matrix=new ie;stack=new N(L.Uint32);currStack=new N(L.Uint32);curLocalM=-1;curWorldM=-1;rapid;constructor(t){this.reset(),this.rapid=t}save(t,e){this.stack.push(this.curLocalM),this.stack.push(this.curWorldM);const r=this.curWorldM;return t!==void 0&&typeof t!="number"&&([t,e]=[t?.world,t?.local]),this.curLocalM=e??this.matrix.alloc(),this.curWorldM=t??this.matrix.allocDirty(),t===void 0&&(e===void 0?this.matrix.copy(this.curWorldM,r):this.matrix.multiplyOut(this.curWorldM,r,this.curLocalM)),e===void 0&&this.currStack.push(this.curLocalM),t===void 0&&this.currStack.push(this.curWorldM),{parent:r,world:this.curWorldM,local:this.curLocalM}}restore(){this.stack.length!=0&&(this.curWorldM=this.stack.pop(),this.curLocalM=this.stack.pop())}restoreAll(){for(;this.stack.length>0;)this.restore()}retainMatrix(t){const e=new Set(typeof t=="number"?[t]:[t.local,t.world]),r=[];for(let i=0;i<this.currStack.length;i++)e.has(this.currStack.get(i))&&r.push(i);return N.removeAtIndices(r,[this.currStack]),t}translate(t,e){this.matrix.translate(this.curLocalM,t,e),this.matrix.translate(this.curWorldM,t,e)}scale(t,e=t){this.matrix.scale(this.curLocalM,t,e),this.matrix.scale(this.curWorldM,t,e)}rotate(t){this.matrix.rotate(this.curLocalM,t),this.matrix.rotate(this.curWorldM,t)}rotateWithOffset(t,e,r){this.matrix.rotateWithOffset(this.curLocalM,t,e,r),this.matrix.rotateWithOffset(this.curWorldM,t,e,r)}identity(){this.matrix.identity(this.curLocalM),this.matrix.identity(this.curWorldM)}reset(){for(let t=0;t<this.currStack.length;t++)this.matrix.free(this.currStack.typedArray[t]);this.currStack.clear(),this.stack.clear(),this.curLocalM=this.matrix.alloc(),this.curWorldM=this.matrix.alloc(),this.currStack.push(this.curLocalM),this.currStack.push(this.curWorldM)}localToWorld(t,e){return this.matrix.localToWorld(this.curWorldM,t,e)}worldToLocal(t,e){return this.matrix.worldToLocal(this.curWorldM,t,e)}getGlobalPosition(){return this.matrix.getPosition(this.curWorldM)}getGlobalScale(){return this.matrix.getScale(this.curWorldM)}getGlobalRotation(){return this.matrix.getRotation(this.curWorldM)}toCSSMatrix(){return this.matrix.toCSSMatrix(this.curWorldM)}getTransform(){const t=this.curWorldM*A;return this.matrix.data.slice(t,t+A)}setTransform(t){const e=this.curWorldM*A,r=this.matrix.data;r[e]=t[0],r[e+1]=t[1],r[e+2]=t[2],r[e+3]=t[3],r[e+4]=t[4],r[e+5]=t[5]}transformPoint(t,e){return this.matrix.transformPoint(this.curLocalM,t,e)}applyTransform(t,e=0,r,i,s,a,o=0,h=0,c=0,u=0,l=0,d,f=this.curWorldM,m=this.curLocalM){let x;typeof t=="object"?(x=this.matrix.applyTransform(t,e,r),d=i,f=s??this.curWorldM,m=a??this.curLocalM):x=this.matrix.applyTransform(t,e,r,i,s,a,o,h,c,u,l);const[y,v,E,M,C,_]=x;d!==void 0?this.matrix.multiplyAffine(f,d,y,v,E,M,C,_):(this.matrix.multiplyAffineInPlace(m,y,v,E,M,C,_),this.matrix.multiplyAffineInPlace(f,y,v,E,M,C,_))}}const ne=`#version 300 es\r
88
88
  precision highp float;\r
89
89
  \r
90
90
  in vec2 aPosition;\r
@@ -205,7 +205,7 @@ void main(void) {\r
205
205
  discard;\r
206
206
  }\r
207
207
  }\r
208
- `,J=20;class ce extends Rt{vertexBuffer;vertexCount=0;extraTextureUnit=1;matrixIndex=-1;drawMode=this.gl.TRIANGLES;texture;maskShader;KEY="Graphic";constructor(t){super(t),this.createBuffer(),this.createDefaultShader()}createBuffer(){const t=this.gl;this.vertexBuffer=new dt(t,_.Float32,t.ARRAY_BUFFER,t.DYNAMIC_DRAW),this.maskShader=this.createMaskShader(oe,he)}createDefaultShader(){return this.vs=ne,this.fs=ae,this.defaultShader=this.createShader(this.vs,this.fs),this.defaultShader}createMaskShader(t,e){const r=this.gl,i=new lt(r,t,e);return i.use(),i.bindVAO(),this.vertexBuffer.bindBuffer(),i.setAttributes([{name:"aPosition",size:2,type:r.FLOAT,stride:J-4,offset:0,divisor:0},{name:"aUV",size:2,type:r.FLOAT,stride:J-4,offset:8,divisor:0}]),i.unbindVAO(),this.currentShader&&this.currentShader.use(),i}createShader(t,e){const r=this.gl,i=super.createShader(t,e);return i.use(),i.bindVAO(),this.vertexBuffer.bindBuffer(),i.setAttributes([{name:"aPosition",size:2,type:r.FLOAT,stride:J,offset:0,divisor:0},{name:"aColor",size:4,type:r.UNSIGNED_BYTE,normalized:!0,stride:J,offset:8,divisor:0},{name:"aUV",size:2,type:r.FLOAT,stride:J,offset:12,divisor:0}]),i.unbindVAO(),this.currentShader&&this.currentShader.use(),i}startGraphic(t,e=this.gl.TRIANGLES,r){this.vertexBuffer.clear(),this.vertexCount=0,this.matrixIndex=t,this.drawMode=e,this.texture=r,this.texture?.glTexture&&this.useTexture(this.texture.glTexture,0,0)}addVertex(t,e,r=0,i=0,s=4294967295){const a=this.vertexBuffer;a.pushFloat32(t),a.pushFloat32(e),this.currentShader!=this.maskShader&&a.pushUint32(s),a.pushFloat32(r),a.pushFloat32(i),this.vertexCount++,this.vertexBuffer.makeDirty()}endGraphic(){this.flush()}render(){if(this.vertexCount===0)return;this.prepareRender();const t=this.gl,e=this.currentShader,r=this.matrixIndex*6,i=this.matrixStore.data;let s=i[r+4],a=i[r+5];this.rapid.roundPixels&&(s=Math.round(s),a=Math.round(a)),e.setUniform("uMatrixRow0",[i[r],i[r+2],s]),e.setUniform("uMatrixRow1",[i[r+1],i[r+3],a]),e.setUniform("uUVRect",this.texture?[this.texture.uvX,this.texture.uvY,this.texture.uvW,this.texture.uvH]:[0,0,1,1]),this.texture?.glTexture?(t.activeTexture(t.TEXTURE0),t.bindTexture(t.TEXTURE_2D,this.texture.glTexture),e.setUniform("uTexture",0),e.setUniform("uUseTexture",1)):e.setUniform("uUseTexture",0),this.vertexBuffer.bindBuffer(),this.vertexBuffer.bufferData(),e.bindVAO(),t.drawArrays(this.drawMode,0,this.vertexCount),this.rapid.drawcallCount++,this.texture=void 0}hasPendingContent(){return!1}}class g{x;y;static ZERO=new g(0,0);static ONE=new g(1,1);static UP=new g(0,1);static DOWN=new g(0,-1);static LEFT=new g(-1,0);static RIGHT=new g(1,0);constructor(t,e){this.x=t!==void 0?t:0,this.y=e!==void 0?e:this.x}set(t,e){return e===void 0?(this.x=t,this.y=t):(this.x=t,this.y=e),this}add(t){return new g(this.x+t.x,this.y+t.y)}subtract(t){return new g(this.x-t.x,this.y-t.y)}multiply(t){return t instanceof g?new g(this.x*t.x,this.y*t.y):new g(this.x*t,this.y*t)}divide(t){return t instanceof g?new g(this.x/t.x,this.y/t.y):new g(this.x/t,this.y/t)}dot(t){return this.x*t.x+this.y*t.y}cross(t){return this.x*t.y-this.y*t.x}distanceTo(t){const e=this.x-t.x,r=this.y-t.y;return Math.sqrt(e*e+r*r)}clone(){return new g(this.x,this.y)}to(t){this.x=t.x,this.y=t.y}equals(t){return t.x==this.x&&t.y==this.y}perpendicular(){const t=this.x;return this.x=-this.y,this.y=t,this}invert(){return this.x=-this.x,this.y=-this.y,this}inverted(){return new g(this.x*-1,this.y*-1)}length(){return Math.sqrt(this.x*this.x+this.y*this.y)}squaredLength(){return this.x*this.x+this.y*this.y}normalize(){const t=this.length();return this.x=this.x/t||0,this.y=this.y/t||0,this}normalized(){const t=this.length();return new g(this.x/t||0,this.y/t||0)}fix(t=1e-13){this.x=Math.abs(this.x)<t?0:this.x,this.y=Math.abs(this.y)<t?0:this.y}fixed(t=1e-13){return new g(Math.abs(this.x)<t?0:this.x,Math.abs(this.y)<t?0:this.y)}angle(){return Math.atan2(this.y,this.x)}abs(){return new g(Math.abs(this.x),Math.abs(this.y))}floor(){return new g(Math.floor(this.x),Math.floor(this.y))}ceil(){return new g(Math.ceil(this.x),Math.ceil(this.y))}stringify(){return`Vec2(${this.x}, ${this.y})`}clear(){this.x=0,this.y=0}isPrettyMuchZero(){return Math.abs(this.x)<1e-4&&Math.abs(this.y)<1e-4}isZero(){return this.x==0&&this.y==0}isNotZero(){return this.x!==0||this.y!==0}setToPolar(t,e=1){return this.x=Math.cos(t)*e,this.y=Math.sin(t)*e,this}static FromArray(t){return t.map(e=>new g(e[0],e[1]))}static fromAngle(t){return new g(Math.cos(t),Math.sin(t))}}const St=(n,t)=>{const[e,r,i,s,a,o]=t,h=new Float32Array([e,i,0,0,r,s,0,0,0,0,1,0,a,o,0,1]);return At(h,n)},At=(n,t)=>{const e=new Float32Array(16);for(let r=0;r<4;r++)for(let i=0;i<4;i++){let s=0;for(let a=0;a<4;a++)s+=n[i*4+a]*t[a*4+r];e[i*4+r]=s}return e};var bt=(n=>(n[n.STRETCH=0]="STRETCH",n[n.REPEAT=1]="REPEAT",n))(bt||{});const mt=10,Ut=(n,t,e,r,i,s)=>{const a=[],o=[],h=r?Math.atan2(t.y,t.x):Math.atan2(-t.y,-t.x),c=Math.PI,u=new g(t.y,-t.x);for(let l=0;l<mt;l++){const d=l/mt,f=(l+1)/mt,m=h+d*c,x=h+f*c,T=new g(Math.cos(m)*e,Math.sin(m)*e),y=new g(Math.cos(x)*e,Math.sin(x)*e);a.push(n),o.push(new g(i,.5)),a.push(n.add(T)),o.push(new g(i+T.dot(u)*s,.5-.5*T.dot(t)/e)),a.push(n.add(y)),o.push(new g(i+y.dot(u)*s,.5-.5*y.dot(t)/e))}return{vertices:a,uv:o}},Ct=(n,t=!1)=>{const e=[];if(n.length<2||t&&n.length<3)return{normals:e,length:0};const r=4,i=.001,s=n.length;let a=0;if(t)for(let h=0;h<s;h++){const c=n[h],u=n[(h+1)%s];a+=c.distanceTo(u)}else for(let h=0;h<s-1;h++)a+=n[h].distanceTo(n[h+1]);const o=(h,c,u)=>{const l=c.subtract(h).normalize(),d=c.subtract(u).normalize(),f=d.dot(l);if(f<-1+i)return{normal:l.perpendicular(),miters:1};{let m=d.add(l).normalize();l.cross(d)<0&&(m=m.multiply(-1));let x=1/Math.sqrt((1-f)/2);return{normal:m,miters:Math.min(x,r)}}};if(t)for(let h=0;h<s;h++){const c=h===0?n[s-1]:n[h-1],u=n[h],l=h===s-1?n[0]:n[h+1];e.push(o(c,u,l))}else for(let h=0;h<s;h++)if(h===0){const c=n[1].subtract(n[0]).normalize();e.push({normal:c.perpendicular(),miters:1})}else if(h===s-1){const c=n[h].subtract(n[h-1]).normalize();e.push({normal:c.perpendicular(),miters:1})}else e.push(o(n[h-1],n[h],n[h+1]));return{normals:e,length:a}},_t=n=>{const t=n.points;if(t.length<2)return{vertices:[],uv:[]};const{normals:e,length:r}=Ct(t,n.closed),i=(n.width||1)/2,s=[],a=[],o=n.roundCap||!1,h=n.textureMode||0;let c=0;const u=n.texture?.rawWidth||1,l=n.closed?t.length:t.length-1;for(let d=0;d<l;d++){const f=t[d],m=e[d].normal,x=e[d].miters,T=(d+1)%t.length,y=t[T],w=e[T].normal,R=e[T].miters,C=f.add(m.multiply(x*i)),L=f.subtract(m.multiply(x*i)),B=y.add(w.multiply(R*i)),N=y.subtract(w.multiply(R*i)),A=f.distanceTo(y);let M=0,U=0;h===0?(M=c/r,U=(c+A)/r):(M=c/u,U=M+A/u);const F=new g(M,0),G=new g(M,1),H=new g(U,0),I=new g(U,1);s.push(C),a.push(F),s.push(L),a.push(G),s.push(B),a.push(H),s.push(B),a.push(H),s.push(N),a.push(I),s.push(L),a.push(G),c+=A}if(o&&!n.closed){const d=t[0],f=e[0].normal,m=h===0?r>0?1/r:0:1/u,x=Ut(d,f,i,!0,0,m);s.push(...x.vertices),a.push(...x.uv);const T=t[t.length-1],y=e[t.length-1].normal,w=h===0?1:c/u,R=h===0?r>0?1/r:0:1/u,C=Ut(T,y,i,!1,w,R);s.push(...C.vertices),a.push(...C.uv)}return{vertices:s,uv:a}},tt=(n,t,e,r)=>{const i=n.matrix.temporary;if(t.customMatrix!==void 0)return n.matrixStack.applyTransform(t,e,r,i,t.customMatrix),i;if(t.modifyStack){n.matrixStack.applyTransform(t,e,r);return}else return n.matrixStack.applyTransform(t,e,r,i,n.matrixStack.curWorldM),i},Pt=(n,t)=>t?n?t.premultipliedUint32:t.uint32:4294967295,ue=2,Lt=(n,t)=>{const e=tt(n,t,t.texture.rawWidth,t.texture.rawHeight),r=t.texture;if(!r?.base||n.inCreateMask)return;const i=r.isAtlas?n.atlasSpriteRegion:n.spriteRegion;n.enterRegion(i,t.shader);let s=r.uvX,a=r.uvY,o=r.uvW,h=r.uvH;const c=!!t.flipX,u=!!t.flipY!=!!r.flipY;let l=t.padding??i.currentShader.padding;r.isAtlas&&(l+=ue);const d=s<=o?l:-l,f=a<=h?l:-l;i.drawSprite(r,e??t.customMatrix??n.matrixStack.curWorldM,s,a,o,h,n.getColorUint32(t.color),d,f,c,u,r.isRotated)},Nt=(n,t)=>{const e=n.particleRegion;n.enterRegion(e,t.shader);const r=t.texture,i=t.count??t.x.length,s=t.color;e.drawParticles(r,t.x,t.y,t.rotation,s,i,t.scaleX,t.scaleY,t.u0??r.uvX,t.v0??r.uvY,t.u1??r.uvW,t.v1??r.uvH,t.flipX,t.flipY,r.isRotated,t.originX,t.originY,n.premultipliedAlpha,t.reverseOrder,t.customMatrix)},xt=(n,t)=>{if(t.points.length===0)return;const e=tt(n,t,0,0);n.startGraphic(t.drawMode??n.gl.TRIANGLES,t.texture,t.shader,e??t.customMatrix);for(let r=0;r<t.points.length;r++){const i=t.points[r],s=t.uv?.[r],a=Array.isArray(t.color)?t.color[r]:t.color;n.addGraphicVertex(i.x,i.y,s?.x??0,s?.y??0,n.getColorUint32(a))}n.endGraphic()},It=(n,t)=>{if(n.inCreateMask)return;const{vertices:e,uv:r}=_t(t);xt(n,{...t,points:e,uv:r,drawMode:n.gl.TRIANGLES})},Ft=(n,t)=>{const e=tt(n,t,t.texture.rawWidth,t.texture.rawHeight);n.startMaskGraphic(n.gl.TRIANGLE_FAN,t.texture,e??t.customMatrix),n.addRectVertex(t.texture.rawWidth,t.texture.rawHeight),n.endGraphic()},Ot=(n,t)=>{const e=tt(n,t,t.width,t.height);n.startGraphic(n.gl.TRIANGLE_FAN,t.texture,t.shader,e??t.customMatrix),n.addRectVertex(t.width,t.height,t.color),n.endGraphic()},Bt=(n,t)=>{const e=t.segments??32,r=tt(n,t,0,0);n.startGraphic(n.gl.TRIANGLE_FAN,void 0,t.shader,r??t.customMatrix);const i=n.getColorUint32(t.color);n.addGraphicVertex(0,0,.5,.5,i),n.addCircleVertex(t.radius,t.color,e),n.addGraphicVertex(t.radius,0,1,.5,i),n.endGraphic()},Dt=`#version 300 es\r
208
+ `,J=20;class ce extends Rt{vertexBuffer;vertexCount=0;extraTextureUnit=1;matrixIndex=-1;drawMode=this.gl.TRIANGLES;texture;maskShader;KEY="Graphic";constructor(t){super(t),this.createBuffer(),this.createDefaultShader()}createBuffer(){const t=this.gl;this.vertexBuffer=new dt(t,L.Float32,t.ARRAY_BUFFER,t.DYNAMIC_DRAW),this.maskShader=this.createMaskShader(oe,he)}createDefaultShader(){return this.vs=ne,this.fs=ae,this.defaultShader=this.createShader(this.vs,this.fs),this.defaultShader}createMaskShader(t,e){const r=this.gl,i=new lt(r,t,e);return i.use(),i.bindVAO(),this.vertexBuffer.bindBuffer(),i.setAttributes([{name:"aPosition",size:2,type:r.FLOAT,stride:J-4,offset:0,divisor:0},{name:"aUV",size:2,type:r.FLOAT,stride:J-4,offset:8,divisor:0}]),i.unbindVAO(),this.currentShader&&this.currentShader.use(),i}createShader(t,e){const r=this.gl,i=super.createShader(t,e);return i.use(),i.bindVAO(),this.vertexBuffer.bindBuffer(),i.setAttributes([{name:"aPosition",size:2,type:r.FLOAT,stride:J,offset:0,divisor:0},{name:"aColor",size:4,type:r.UNSIGNED_BYTE,normalized:!0,stride:J,offset:8,divisor:0},{name:"aUV",size:2,type:r.FLOAT,stride:J,offset:12,divisor:0}]),i.unbindVAO(),this.currentShader&&this.currentShader.use(),i}startGraphic(t,e=this.gl.TRIANGLES,r){this.vertexBuffer.clear(),this.vertexCount=0,this.matrixIndex=t,this.drawMode=e,this.texture=r,this.texture?.glTexture&&this.useTexture(this.texture.glTexture,0,0)}addVertex(t,e,r=0,i=0,s=4294967295){const a=this.vertexBuffer;a.pushFloat32(t),a.pushFloat32(e),this.currentShader!=this.maskShader&&a.pushUint32(s),a.pushFloat32(r),a.pushFloat32(i),this.vertexCount++,this.vertexBuffer.makeDirty()}endGraphic(){this.flush()}render(){if(this.vertexCount===0)return;this.prepareRender();const t=this.gl,e=this.currentShader,r=this.matrixIndex*6,i=this.matrixStore.data;let s=i[r+4],a=i[r+5];this.rapid.roundPixels&&(s=Math.round(s),a=Math.round(a)),e.setUniform("uMatrixRow0",[i[r],i[r+2],s]),e.setUniform("uMatrixRow1",[i[r+1],i[r+3],a]),e.setUniform("uUVRect",this.texture?[this.texture.uvX,this.texture.uvY,this.texture.uvW,this.texture.uvH]:[0,0,1,1]),this.texture?.glTexture?(t.activeTexture(t.TEXTURE0),t.bindTexture(t.TEXTURE_2D,this.texture.glTexture),e.setUniform("uTexture",0),e.setUniform("uUseTexture",1)):e.setUniform("uUseTexture",0),this.vertexBuffer.bindBuffer(),this.vertexBuffer.bufferData(),e.bindVAO(),t.drawArrays(this.drawMode,0,this.vertexCount),this.rapid.drawcallCount++,this.texture=void 0}hasPendingContent(){return!1}}class g{x;y;static ZERO=new g(0,0);static ONE=new g(1,1);static UP=new g(0,1);static DOWN=new g(0,-1);static LEFT=new g(-1,0);static RIGHT=new g(1,0);constructor(t,e){this.x=t!==void 0?t:0,this.y=e!==void 0?e:this.x}set(t,e){return e===void 0?(this.x=t,this.y=t):(this.x=t,this.y=e),this}add(t){return new g(this.x+t.x,this.y+t.y)}subtract(t){return new g(this.x-t.x,this.y-t.y)}multiply(t){return t instanceof g?new g(this.x*t.x,this.y*t.y):new g(this.x*t,this.y*t)}divide(t){return t instanceof g?new g(this.x/t.x,this.y/t.y):new g(this.x/t,this.y/t)}dot(t){return this.x*t.x+this.y*t.y}cross(t){return this.x*t.y-this.y*t.x}distanceTo(t){const e=this.x-t.x,r=this.y-t.y;return Math.sqrt(e*e+r*r)}clone(){return new g(this.x,this.y)}to(t){this.x=t.x,this.y=t.y}equals(t){return t.x==this.x&&t.y==this.y}perpendicular(){const t=this.x;return this.x=-this.y,this.y=t,this}invert(){return this.x=-this.x,this.y=-this.y,this}inverted(){return new g(this.x*-1,this.y*-1)}length(){return Math.sqrt(this.x*this.x+this.y*this.y)}squaredLength(){return this.x*this.x+this.y*this.y}normalize(){const t=this.length();return this.x=this.x/t||0,this.y=this.y/t||0,this}normalized(){const t=this.length();return new g(this.x/t||0,this.y/t||0)}fix(t=1e-13){this.x=Math.abs(this.x)<t?0:this.x,this.y=Math.abs(this.y)<t?0:this.y}fixed(t=1e-13){return new g(Math.abs(this.x)<t?0:this.x,Math.abs(this.y)<t?0:this.y)}angle(){return Math.atan2(this.y,this.x)}abs(){return new g(Math.abs(this.x),Math.abs(this.y))}floor(){return new g(Math.floor(this.x),Math.floor(this.y))}ceil(){return new g(Math.ceil(this.x),Math.ceil(this.y))}stringify(){return`Vec2(${this.x}, ${this.y})`}clear(){this.x=0,this.y=0}isPrettyMuchZero(){return Math.abs(this.x)<1e-4&&Math.abs(this.y)<1e-4}isZero(){return this.x==0&&this.y==0}isNotZero(){return this.x!==0||this.y!==0}setToPolar(t,e=1){return this.x=Math.cos(t)*e,this.y=Math.sin(t)*e,this}static FromArray(t){return t.map(e=>new g(e[0],e[1]))}static fromAngle(t){return new g(Math.cos(t),Math.sin(t))}}const St=(n,t)=>{const[e,r,i,s,a,o]=t,h=new Float32Array([e,i,0,0,r,s,0,0,0,0,1,0,a,o,0,1]);return At(h,n)},At=(n,t)=>{const e=new Float32Array(16);for(let r=0;r<4;r++)for(let i=0;i<4;i++){let s=0;for(let a=0;a<4;a++)s+=n[i*4+a]*t[a*4+r];e[i*4+r]=s}return e};var bt=(n=>(n[n.STRETCH=0]="STRETCH",n[n.REPEAT=1]="REPEAT",n))(bt||{});const mt=10,Ut=(n,t,e,r,i,s)=>{const a=[],o=[],h=r?Math.atan2(t.y,t.x):Math.atan2(-t.y,-t.x),c=Math.PI,u=new g(t.y,-t.x);for(let l=0;l<mt;l++){const d=l/mt,f=(l+1)/mt,m=h+d*c,x=h+f*c,y=new g(Math.cos(m)*e,Math.sin(m)*e),v=new g(Math.cos(x)*e,Math.sin(x)*e);a.push(n),o.push(new g(i,.5)),a.push(n.add(y)),o.push(new g(i+y.dot(u)*s,.5-.5*y.dot(t)/e)),a.push(n.add(v)),o.push(new g(i+v.dot(u)*s,.5-.5*v.dot(t)/e))}return{vertices:a,uv:o}},Ct=(n,t=!1)=>{const e=[];if(n.length<2||t&&n.length<3)return{normals:e,length:0};const r=4,i=.001,s=n.length;let a=0;if(t)for(let h=0;h<s;h++){const c=n[h],u=n[(h+1)%s];a+=c.distanceTo(u)}else for(let h=0;h<s-1;h++)a+=n[h].distanceTo(n[h+1]);const o=(h,c,u)=>{const l=c.subtract(h).normalize(),d=c.subtract(u).normalize(),f=d.dot(l);if(f<-1+i)return{normal:l.perpendicular(),miters:1};{let m=d.add(l).normalize();l.cross(d)<0&&(m=m.multiply(-1));let x=1/Math.sqrt((1-f)/2);return{normal:m,miters:Math.min(x,r)}}};if(t)for(let h=0;h<s;h++){const c=h===0?n[s-1]:n[h-1],u=n[h],l=h===s-1?n[0]:n[h+1];e.push(o(c,u,l))}else for(let h=0;h<s;h++)if(h===0){const c=n[1].subtract(n[0]).normalize();e.push({normal:c.perpendicular(),miters:1})}else if(h===s-1){const c=n[h].subtract(n[h-1]).normalize();e.push({normal:c.perpendicular(),miters:1})}else e.push(o(n[h-1],n[h],n[h+1]));return{normals:e,length:a}},_t=n=>{const t=n.points;if(t.length<2)return{vertices:[],uv:[]};const{normals:e,length:r}=Ct(t,n.closed),i=(n.width||1)/2,s=[],a=[],o=n.roundCap||!1,h=n.textureMode||0;let c=0;const u=n.texture?.rawWidth||1,l=n.closed?t.length:t.length-1;for(let d=0;d<l;d++){const f=t[d],m=e[d].normal,x=e[d].miters,y=(d+1)%t.length,v=t[y],E=e[y].normal,M=e[y].miters,C=f.add(m.multiply(x*i)),_=f.subtract(m.multiply(x*i)),F=v.add(E.multiply(M*i)),U=v.subtract(E.multiply(M*i)),w=f.distanceTo(v);let R=0,S=0;h===0?(R=c/r,S=(c+w)/r):(R=c/u,S=R+w/u);const O=new g(R,0),G=new g(R,1),H=new g(S,0),I=new g(S,1);s.push(C),a.push(O),s.push(_),a.push(G),s.push(F),a.push(H),s.push(F),a.push(H),s.push(U),a.push(I),s.push(_),a.push(G),c+=w}if(o&&!n.closed){const d=t[0],f=e[0].normal,m=h===0?r>0?1/r:0:1/u,x=Ut(d,f,i,!0,0,m);s.push(...x.vertices),a.push(...x.uv);const y=t[t.length-1],v=e[t.length-1].normal,E=h===0?1:c/u,M=h===0?r>0?1/r:0:1/u,C=Ut(y,v,i,!1,E,M);s.push(...C.vertices),a.push(...C.uv)}return{vertices:s,uv:a}},tt=(n,t,e,r)=>{const i=n.matrix.temporary;if(t.customMatrix!==void 0)return n.matrixStack.applyTransform(t,e,r,i,t.customMatrix),i;if(t.modifyStack){n.matrixStack.applyTransform(t,e,r);return}else return n.matrixStack.applyTransform(t,e,r,i,n.matrixStack.curWorldM),i},Pt=(n,t)=>t?n?t.premultipliedUint32:t.uint32:4294967295,ue=2,Lt=(n,t)=>{const e=tt(n,t,t.texture.rawWidth,t.texture.rawHeight),r=t.texture;if(!r?.base||n.inCreateMask)return;const i=r.isAtlas?n.atlasSpriteRegion:n.spriteRegion;n.enterRegion(i,t.shader);let s=r.uvX,a=r.uvY,o=r.uvW,h=r.uvH;const c=!!t.flipX,u=!!t.flipY!=!!r.flipY;let l=t.padding??i.currentShader.padding;r.isAtlas&&(l+=ue);const d=s<=o?l:-l,f=a<=h?l:-l;i.drawSprite(r,e??t.customMatrix??n.matrixStack.curWorldM,s,a,o,h,n.getColorUint32(t.color),d,f,c,u,r.isRotated)},Nt=(n,t)=>{const e=n.particleRegion;n.enterRegion(e,t.shader);const r=t.texture,i=t.count??t.x.length,s=t.color;e.drawParticles(r,t.x,t.y,t.rotation,s,i,t.scaleX,t.scaleY,t.u0??r.uvX,t.v0??r.uvY,t.u1??r.uvW,t.v1??r.uvH,t.flipX,t.flipY,r.isRotated,t.originX,t.originY,n.premultipliedAlpha,t.reverseOrder,t.customMatrix)},xt=(n,t)=>{if(t.points.length===0)return;const e=tt(n,t,0,0);n.startGraphic(t.drawMode??n.gl.TRIANGLES,t.texture,t.shader,e??t.customMatrix);for(let r=0;r<t.points.length;r++){const i=t.points[r],s=t.uv?.[r],a=Array.isArray(t.color)?t.color[r]:t.color;n.addGraphicVertex(i.x,i.y,s?.x??0,s?.y??0,n.getColorUint32(a))}n.endGraphic()},It=(n,t)=>{if(n.inCreateMask)return;const{vertices:e,uv:r}=_t(t);xt(n,{...t,points:e,uv:r,drawMode:n.gl.TRIANGLES})},Ft=(n,t)=>{const e=tt(n,t,t.texture.rawWidth,t.texture.rawHeight);n.startMaskGraphic(n.gl.TRIANGLE_FAN,t.texture,e??t.customMatrix),n.addRectVertex(t.texture.rawWidth,t.texture.rawHeight),n.endGraphic()},Ot=(n,t)=>{const e=tt(n,t,t.width,t.height);n.startGraphic(n.gl.TRIANGLE_FAN,t.texture,t.shader,e??t.customMatrix),n.addRectVertex(t.width,t.height,t.color),n.endGraphic()},Bt=(n,t)=>{const e=t.segments??32,r=tt(n,t,0,0);n.startGraphic(n.gl.TRIANGLE_FAN,void 0,t.shader,r??t.customMatrix);const i=n.getColorUint32(t.color);n.addGraphicVertex(0,0,.5,.5,i),n.addCircleVertex(t.radius,t.color,e),n.addGraphicVertex(t.radius,0,1,.5,i),n.endGraphic()},Dt=`#version 300 es\r
209
209
  precision mediump float;\r
210
210
  \r
211
211
  uniform sampler2D uTextures[%TEXTURE_NUM%];\r
@@ -303,7 +303,7 @@ void main(void) {\r
303
303
  \r
304
304
  // CUSTOM_CODE_CALL\r
305
305
  }\r
306
- `,z=48,me=z/4;class xe extends ft{KEY="ParticleSprite";texture;customMatrix;particleLoopCache=new Map;extraTextureUnit=1;constructor(t){super(t)}createDefaultShader(){const t=fe,e=de;return this.vs=e,this.fs=t,this.defaultShader=this.createShader(e,t),this.defaultShader}enter(t){super.enter(t),this.currentShader.setUniform("u_matrix",this.rapid.projection)}createShader(t,e){const r=super.createShader(t,e);return r.use(),r.bindVAO(),this.instanceBuffer.bindBuffer(),r.setAttributes([{name:"aPosition",size:2,stride:z,offset:0,divisor:1},{name:"aScale",size:2,stride:z,offset:8,divisor:1},{name:"aRotation",size:1,stride:z,offset:16,divisor:1},{name:"aUVRect",size:4,stride:z,offset:20,divisor:1},{name:"aColor",size:4,type:ut,normalized:!0,stride:z,offset:36,divisor:1},{name:"aOrigin",size:2,stride:z,offset:40,divisor:1}]),this.quadBuffer.bindBuffer(),r.setAttributes([{name:"aVertex",size:2,stride:8,offset:0,divisor:0}]),r.unbindVAO(),this.currentShader&&this.currentShader.use(),r}drawParticles(t,e,r,i=0,s=4294967295,a,o=1,h=1,c=0,u=0,l=1,d=1,f=!1,m=!1,x=!1,T=.5,y=.5,w,R=!1,C){if(!t.glTexture||a<=0)return;this.texture&&t!==this.texture&&this.flush();const L=typeof o=="number",B=typeof h=="number",N=typeof i=="number",A=typeof s=="number",M=typeof c=="number",U=!A&&typeof s[0]=="number",F=t.uvW-t.uvX,G=t.uvH-t.uvY,H=F===0?0:1/F,I=G===0?0:1/G;let O=t.rawWidth*(f?-1:1);L&&(O*=o);let Y=t.rawHeight*(m?-1:1);B&&(Y*=h),M&&(O*=(l-c)*H,Y*=(d-u)*I);let V=x?Math.PI/2:0;N&&(V+=i);const S=s??4294967295,b=this.getParticleLoop(L,B,N,M,A,U,R,w);this.texture||(this.texture=t,this.useTexture(t.glTexture,0,0));const it=a,D=this.instanceBuffer;let $=D.usedElemNum;D.resize(it*me);const Ee=D.float32,Re=D.uint32;$=b(e,r,o,h,i,c,u,l,d,s,Ee,Re,O,Y,H,I,V,S,1-T,1-y,a,it,$),D.usedElemNum=$,D.makeDirty(),this.instanceCount+=it,this.customMatrix=C,this.flush()}getParticleLoop(t,e,r,i,s,a,o,h){const c=[...arguments].join("_");if(this.particleLoopCache.has(c))return this.particleLoopCache.get(c);let u=`
306
+ `,Y=48,me=Y/4;class xe extends ft{KEY="ParticleSprite";texture;customMatrix;particleLoopCache=new Map;extraTextureUnit=1;constructor(t){super(t)}createDefaultShader(){const t=fe,e=de;return this.vs=e,this.fs=t,this.defaultShader=this.createShader(e,t),this.defaultShader}enter(t){super.enter(t),this.currentShader.setUniform("u_matrix",this.rapid.projection)}createShader(t,e){const r=super.createShader(t,e);return r.use(),r.bindVAO(),this.instanceBuffer.bindBuffer(),r.setAttributes([{name:"aPosition",size:2,stride:Y,offset:0,divisor:1},{name:"aScale",size:2,stride:Y,offset:8,divisor:1},{name:"aRotation",size:1,stride:Y,offset:16,divisor:1},{name:"aUVRect",size:4,stride:Y,offset:20,divisor:1},{name:"aColor",size:4,type:ut,normalized:!0,stride:Y,offset:36,divisor:1},{name:"aOrigin",size:2,stride:Y,offset:40,divisor:1}]),this.quadBuffer.bindBuffer(),r.setAttributes([{name:"aVertex",size:2,stride:8,offset:0,divisor:0}]),r.unbindVAO(),this.currentShader&&this.currentShader.use(),r}drawParticles(t,e,r,i=0,s=4294967295,a,o=1,h=1,c=0,u=0,l=1,d=1,f=!1,m=!1,x=!1,y=.5,v=.5,E,M=!1,C){if(!t.glTexture||a<=0)return;this.texture&&t!==this.texture&&this.flush();const _=typeof o=="number",F=typeof h=="number",U=typeof i=="number",w=typeof s=="number",R=typeof c=="number",S=!w&&typeof s[0]=="number",O=t.uvW-t.uvX,G=t.uvH-t.uvY,H=O===0?0:1/O,I=G===0?0:1/G;let B=t.rawWidth*(f?-1:1);_&&(B*=o);let z=t.rawHeight*(m?-1:1);F&&(z*=h),R&&(B*=(l-c)*H,z*=(d-u)*I);let V=x?Math.PI/2:0;U&&(V+=i);const b=s??4294967295,P=this.getParticleLoop(_,F,U,R,w,S,M,E);this.texture||(this.texture=t,this.useTexture(t.glTexture,0,0));const it=a,D=this.instanceBuffer;let $=D.usedElemNum;D.resize(it*me);const Ee=D.float32,Re=D.uint32;$=P(e,r,o,h,i,c,u,l,d,s,Ee,Re,B,z,H,I,V,b,1-y,1-v,a,it,$),D.usedElemNum=$,D.makeDirty(),this.instanceCount+=it,this.customMatrix=C,this.flush()}getParticleLoop(t,e,r,i,s,a,o,h){const c=[...arguments].join("_");if(this.particleLoopCache.has(c))return this.particleLoopCache.get(c);let u=`
307
307
  return function( x, y, scaleX, scaleY, rotation, u0, v0, u1, v1, color, f32, u32, rawWidth, rawHeight, uv2texW, uv2texH, rotationOffset, staticColor, originX, originY, count, batchCount, index ) {
308
308
  ${t&&i?"const baseSx = rawWidth;":""}
309
309
  ${e&&i?"const baseSy = rawHeight;":""}
@@ -342,7 +342,7 @@ f32[idx + 11] = originY;
342
342
  }
343
343
  return idx;
344
344
  }
345
- `;const l=new Function(u)();return this.particleLoopCache.set(c,l),l}render(){if(this.instanceCount===0)return;this.prepareRender();const t=this.gl;this.texture&&(t.activeTexture(t.TEXTURE0),t.bindTexture(t.TEXTURE_2D,this.texture.glTexture));const e=this.currentShader;this.instanceBuffer.bindBuffer(),this.instanceBuffer.bufferData(),e.bindVAO();const r=this.rapid.matrixStack,s=this.rapid.matrix.getMatrix(this.customMatrix??r.curWorldM),a=St(this.rapid.projection,s);this.currentShader.setUniform("u_matrix",a),Et(t,t.TRIANGLE_STRIP,0,4,this.instanceCount),this.rapid.drawcallCount++,this.texture=void 0,this.customMatrix=void 0}}var Wt=(n=>(n[n.KEEP_W=0]="KEEP_W",n[n.KEEP_H=1]="KEEP_H",n[n.KEEP=2]="KEEP",n[n.IGNORE=3]="IGNORE",n[n.EXPAND=4]="EXPAND",n[n.NONE=5]="NONE",n))(Wt||{});const pe={4:new g(1,1),0:new g(0,1),1:new g(1,0),2:new g(0,0)},kt={4:new g(.5,.5),0:new g(.5,0),1:new g(0,.5),2:new g(.5,.5)};class Gt{constructor(t,e){this.rapid=t,this.updateWindowOption(e)}viewLeft=0;viewRight=0;viewTop=0;viewBottom=0;window={};scissorViewport=null;resolution=0;updateWindowOption(t){Object.assign(this.window,t)}resize(t,e,r,i){const s=this.rapid,a=this.window,o=a.scale??1;t/=o,e/=o,s.flush();const h=r??s.canvas.clientWidth??s.canvas.width,c=i??s.canvas.clientHeight??s.canvas.height;r!==void 0&&(s.canvas.style.width=h+"px"),i!==void 0&&(s.canvas.style.height=c+"px"),s.physicsWidth=h*s.dpr,s.physicsHeight=c*s.dpr,s.logicWidth=t,s.logicHeight=e;let u=0,l=t,d=e,f=0;const m=a.scaleMode??K.CanvasItem,x=a.expand??2;let T=g.ZERO,y=g.ZERO;switch(x in kt&&(T=a.expandPosition??kt[x],y=pe[x]),this.scissorViewport=null,x){case 3:{u=0,l=t,d=e,f=0;break}case 5:{u=0,l=s.physicsWidth,d=s.physicsHeight,f=0;break}default:case 4:case 0:case 1:case 2:{const w=T,R=y,C=Math.min(s.physicsWidth/t,s.physicsHeight/e),L=s.physicsWidth/C,B=s.physicsHeight/C,N=L-t,A=B-e;if(u=-N*w.x,l=t+N*(1-w.x),f=-A*w.y,d=e+A*(1-w.y),!R.x&&N>0||!R.y&&A>0){const M=R.x&&N>0?u:0,U=R.x&&N>0?l:t,F=R.y&&A>0?f:0,G=R.y&&A>0?d:e;this.scissorViewport={x:M,y:F,width:U-M,height:G-F}}break}}switch(m){case K.Viewport:s.canvas.width=t,s.canvas.height=e,s.canvas.style.imageRendering="pixelated",s.gl.viewport(0,0,t,e);break;default:case K.CanvasItem:s.canvas.width=s.physicsWidth,s.canvas.height=s.physicsHeight,s.canvas.style.imageRendering="auto",s.gl.viewport(0,0,s.physicsWidth,s.physicsHeight);break}this.viewLeft=u,this.viewRight=l,this.viewTop=f,this.viewBottom=d,this.updateProjection(),this.restoreViewportScissor(),this.updateResolution(m)}updateResolution(t){let e=1;const r=this.rapid.texture;if(t!==K.Viewport){const i=this.viewRight-this.viewLeft,s=this.viewBottom-this.viewTop;e=Math.max(this.rapid.physicsWidth/i,this.rapid.physicsHeight/s)}e!=this.resolution&&(this.resolution=e,r.texture.forEach(i=>i.updateResolution()))}updateProjection(){this.rapid.updateProjection(this.viewLeft,this.viewRight,this.viewBottom,this.viewTop)}updateOrthMatrix(t,e,r,i,s){t[0]=2/(r-e),t[1]=0,t[2]=0,t[3]=0,t[4]=0,t[5]=2/(s-i),t[6]=0,t[7]=0,t[8]=0,t[9]=0,t[10]=-1,t[11]=0,t[12]=-(r+e)/(r-e),t[13]=-(s+i)/(s-i),t[14]=0,t[15]=1}applyCamera(t){const e=this.rapid,r=e.matrix,i=e.matrixStack,s=this.scissorViewport&&e.gl.getParameter(e.gl.FRAMEBUFFER_BINDING)===null?this.scissorViewport:null,a=s?.width??this.viewRight-this.viewLeft,o=s?.height??this.viewBottom-this.viewTop,h=s?s.x+a/2:(this.viewLeft+this.viewRight)/2,c=s?s.y+o/2:(this.viewTop+this.viewBottom)/2,u=i.curWorldM,l=r.temporary,d=r.applyTransform(t,0,0);r.setMatrix(l,d),r.clampBounds(l,a,o,t.limitLeft,t.limitRight,t.limitTop,t.limitBottom),i.translate(h,c),r.invert(l),r.multiplyOut(u,u,l)}startScissor(t,e,r,i){const s=this.rapid;s.flush();const a=s.gl,o=this.scissorViewport;if(o&&a.getParameter(a.FRAMEBUFFER_BINDING)===null){const L=Math.max(Math.min(t,t+r),o.x),B=Math.min(Math.max(t,t+r),o.x+o.width),N=Math.max(Math.min(e,e+i),o.y),A=Math.min(Math.max(e,e+i),o.y+o.height);t=L,e=N,r=Math.max(0,B-L),i=Math.max(0,A-N)}const[h,c,u,l]=a.getParameter(a.VIEWPORT),d=s.projection,f=h+(d[0]*t+d[12]+1)*u/2,m=h+(d[0]*(t+r)+d[12]+1)*u/2,x=c+(d[5]*e+d[13]+1)*l/2,T=c+(d[5]*(e+i)+d[13]+1)*l/2,y=Math.round(Math.min(f,m)),w=Math.round(Math.max(f,m)),R=Math.round(Math.min(x,T)),C=Math.round(Math.max(x,T));a.enable(a.SCISSOR_TEST),a.scissor(y,R,w-y,C-R)}restoreViewportScissor(){this.rapid.flush();const t=this.rapid.gl,e=this.scissorViewport;e&&t.getParameter(t.FRAMEBUFFER_BINDING)===null?this.startScissor(e.x,e.y,e.width,e.height):t.disable(t.SCISSOR_TEST)}endScissor(){this.restoreViewportScissor()}}var pt=(n=>(n[n.EQUAL=0]="EQUAL",n[n.NOT_EQUAL=1]="NOT_EQUAL",n))(pt||{}),Ht=(n=>(n[n.NORMAL=0]="NORMAL",n[n.ADD=1]="ADD",n[n.MULTIPLY=2]="MULTIPLY",n[n.SCREEN=3]="SCREEN",n[n.ERASE=4]="ERASE",n))(Ht||{}),K=(n=>(n[n.CanvasItem=0]="CanvasItem",n[n.Viewport=1]="Viewport",n))(K||{});class ge{gl;canvas;projection=new Float32Array(16);projectionDirty=!0;dpr;backgroundColor=p.Black;logicWidth=0;logicHeight=0;physicsWidth=0;physicsHeight=0;currentRegion=null;maxTextureUnits=0;matrixStack=new se(this);matrix;spriteRegion;graphicRegion;atlasSpriteRegion;particleRegion;regions;drawcallCount=0;inCreateMask=!1;texture;premultipliedAlpha;antialias;roundPixels;textureFilter;viewport;_filterRT=[null,null];_filterInput=new W;get height(){return this.logicHeight}get width(){return this.logicWidth}constructor(t){this.canvas=t.canvas,this.dpr=window.devicePixelRatio||1,this.antialias=t.antialias??!1,this.textureFilter=t.textureFilter??q.NEAREST,this.premultipliedAlpha=t.premultipliedAlpha??!0,this.roundPixels=t.roundPixels??!1;const e=Kt(this.canvas,this.antialias,this.premultipliedAlpha);this.gl=e,this.maxTextureUnits=e.getParameter(e.MAX_TEXTURE_IMAGE_UNITS),this.matrix=this.matrixStack.matrix,this.spriteRegion=new ft(this),this.graphicRegion=new ce(this),this.atlasSpriteRegion=new le(this),this.particleRegion=new xe(this),this.viewport=new Gt(this,t.window),this.texture=new vt(this),this.regions=[this.spriteRegion,this.atlasSpriteRegion];const r=this.canvas.clientWidth??this.canvas.width,i=this.canvas.clientHeight??this.canvas.height;this.physicsWidth=t.physicsWidth??Math.round(r*this.dpr),this.physicsHeight=t.physicsHeight??Math.round(i*this.dpr),this.logicWidth=t.width??this.physicsWidth/this.dpr,this.logicHeight=t.height??this.physicsHeight/this.dpr,this.resize(this.logicWidth,this.logicHeight,this.physicsWidth,this.physicsHeight),t.backgroundColor&&(this.backgroundColor=t.backgroundColor),e.enable(e.BLEND),this.premultipliedAlpha?e.blendFunc(e.ONE,e.ONE_MINUS_SRC_ALPHA):e.blendFunc(e.SRC_ALPHA,e.ONE_MINUS_SRC_ALPHA),e.enable(e.STENCIL_TEST),e.stencilFunc(e.ALWAYS,1,255),e.stencilOp(e.KEEP,e.KEEP,e.KEEP)}setTextureFilter(t){this.textureFilter=t}getColorUint32(t){return Pt(this.premultipliedAlpha,t)}enterRegion(t,e){this.currentRegion===t&&this.currentRegion.isSameShader(e)||(this.flush(),this.currentRegion=t,t.enter(e))}drawParticles(t){Nt(this,t)}drawSprite(t){Lt(this,t)}drawLine(t){It(this,t)}drawGraphic(t){xt(this,t)}drawRect(t){Ot(this,t)}drawCircle(t){Bt(this,t)}startGraphic(t=this.gl.TRIANGLES,e,r,i){this.enterRegion(this.graphicRegion,r),this.graphicRegion.startGraphic(i??this.matrixStack.curWorldM,t,e)}startMaskGraphic(t=this.gl.TRIANGLES,e,r){this.startGraphic(t,e,this.graphicRegion.maskShader,r)}drawMaskImage(t){Ft(this,t)}addRectVertex(t,e,r){const i=this.getColorUint32(r);this.addGraphicVertex(0,0,0,0,i),this.addGraphicVertex(t,0,1,0,i),this.addGraphicVertex(t,e,1,1,i),this.addGraphicVertex(0,e,0,1,i)}addCircleVertex(t,e,r=32){const i=Math.PI*2/r,s=this.getColorUint32(e);for(let a=0;a<r;a++){const o=i*a,h=Math.cos(o)*t,c=Math.sin(o)*t,u=.5+Math.cos(o)*.5,l=.5+Math.sin(o)*.5;this.addGraphicVertex(h,c,u,l,s)}}addGraphicVertex(t,e,r=0,i=0,s=4294967295){this.graphicRegion.addVertex(t,e,r,i,typeof s=="number"?s:this.getColorUint32(s))}endGraphic(){this.graphicRegion.endGraphic()}resize(t,e,r,i){this.viewport.resize(t,e,r,i)}updateProjection(t,e,r,i){this.viewport.updateOrthMatrix(this.projection,t,e,r,i),this.projectionDirty=!0}clear(){const t=this.gl;this.flush(),t.disable(t.SCISSOR_TEST),this.viewport.scissorViewport&&t.getParameter(t.FRAMEBUFFER_BINDING)===null?(t.clearColor(0,0,0,1),t.clear(t.COLOR_BUFFER_BIT|t.STENCIL_BUFFER_BIT),this.backgroundColor.setClearColor(t,this.premultipliedAlpha),this.viewport.restoreViewportScissor(),t.clear(t.COLOR_BUFFER_BIT)):(this.backgroundColor.setClearColor(t,this.premultipliedAlpha),t.clear(t.COLOR_BUFFER_BIT|t.STENCIL_BUFFER_BIT)),this.drawcallCount=0,this.matrixStack.reset()}flush(){this.currentRegion&&(this.currentRegion.exit(),this.currentRegion=null)}applyFilters(t,e){if(e.length===0)throw new Error("applyFilters: shaders array must not be empty.");const r=Math.max(...e.map(c=>c.padding)),i=r,s=t.rawWidth+r*2,a=t.rawHeight+r*2;for(let c=0;c<2;c++)this._filterRT[c]?this._filterRT[c].resize(s,a):this._filterRT[c]=this.texture.createRenderTexture({width:s,height:a});let o=t.clone(this._filterInput),h=0;this.matrixStack.save(),this.matrixStack.identity();for(let c=0;c<e.length;c++){const u=this._filterRT[h%2];u.offsetX=0,u.offsetY=0,this.enterRenderTexture(u),this.clearRenderTexture(),this.drawSprite({texture:o,shader:e[c],offsetX:c==0?i:0,offsetY:c==0?i:0,padding:r}),this.leaveRenderTexture(),o=u,h++}return this.matrixStack.restore(),o.offsetX=-i,o.offsetY=-i,o}enterRenderTexture(t){this.flush(),t.activate(),this.viewport.restoreViewportScissor(),this.gl.viewport(0,0,t.rawWidth,t.rawHeight),this.updateProjection(0,t.rawWidth,0,t.rawHeight)}clearRenderTexture(t=new p(0,0,0,0)){this.flush();const e=this.gl;t.setClearColor(e,this.premultipliedAlpha),e.clear(e.COLOR_BUFFER_BIT)}leaveRenderTexture(){this.flush(),this.gl.bindFramebuffer(this.gl.FRAMEBUFFER,null),this.gl.viewport(0,0,this.canvas.width,this.canvas.height),this.viewport.updateProjection(),this.viewport.restoreViewportScissor()}drawToRenderTexture(t,e,r=new p(0,0,0,0)){this.enterRenderTexture(t);try{r!==null&&this.clearRenderTexture(r),e()}finally{this.leaveRenderTexture()}}withMask(t,e,r=0,i=1){this.clearMask(),this.startDrawMask(i);try{t()}finally{this.endDrawMask()}this.enterMask(r,i);try{e()}finally{this.exitMask()}}withTransform(t,e,r=0,i=0){this.matrixStack.save();try{e&&this.matrixStack.applyTransform(e,r,i),t()}finally{this.matrixStack.restore()}}withScissor(t,e,r,i,s){this.startScissor(t,e,r,i);try{s()}finally{this.endScissor()}}withBlendMode(t,e){this.setBlendMode(t);try{e()}finally{this.setBlendMode(0)}}startDrawMask(t=1,e=255){this.flush();const r=this.gl;r.stencilMask(e),r.colorMask(!1,!1,!1,!1),r.stencilFunc(r.ALWAYS,t,e),r.stencilOp(r.KEEP,r.KEEP,r.REPLACE),this.enterRegion(this.graphicRegion,this.graphicRegion.maskShader),this.inCreateMask=!0}endDrawMask(){this.flush();const t=this.gl;t.colorMask(!0,!0,!0,!0),t.stencilMask(0),this.inCreateMask=!1}clearMask(t=255){this.flush();const e=this.gl;e.stencilMask(t),e.clear(e.STENCIL_BUFFER_BIT),this.inCreateMask=!1}enterMask(t,e=1,r=255){this.flush();const i=this.gl;i.colorMask(!0,!0,!0,!0),i.stencilMask(0),i.stencilFunc(t===0?i.EQUAL:i.NOTEQUAL,e,r),i.stencilOp(i.KEEP,i.KEEP,i.KEEP),this.inCreateMask=!1}exitMask(){this.flush();const t=this.gl;t.colorMask(!0,!0,!0,!0),t.stencilMask(255),t.stencilFunc(t.ALWAYS,1,255),t.stencilOp(t.KEEP,t.KEEP,t.KEEP),this.inCreateMask=!1}setBlendMode(t){this.flush();const e=this.gl;switch(t){case 0:this.premultipliedAlpha?e.blendFuncSeparate(e.ONE,e.ONE_MINUS_SRC_ALPHA,e.ONE,e.ONE_MINUS_SRC_ALPHA):e.blendFuncSeparate(e.SRC_ALPHA,e.ONE_MINUS_SRC_ALPHA,e.ONE,e.ONE_MINUS_SRC_ALPHA);break;case 1:e.blendFuncSeparate(e.ONE,e.ONE,e.ONE,e.ONE);break;case 2:e.blendFuncSeparate(e.DST_COLOR,e.ONE_MINUS_SRC_ALPHA,e.ONE,e.ONE_MINUS_SRC_ALPHA);break;case 3:e.blendFuncSeparate(e.ONE,e.ONE_MINUS_SRC_COLOR,e.ONE,e.ONE_MINUS_SRC_ALPHA);break;case 4:e.blendFuncSeparate(e.ZERO,e.ONE_MINUS_SRC_ALPHA,e.ZERO,e.ONE_MINUS_SRC_ALPHA);break}}startScissor(t,e,r,i){this.viewport.startScissor(t,e,r,i)}endScissor(){this.viewport.endScissor()}applyCamera(t){this.viewport.applyCamera(t)}logicToPhysics(t){return t.multiply(new g(this.physicsWidth/this.logicWidth,this.physicsHeight/this.logicHeight))}physicsToLogic(t){return t.multiply(new g(this.logicWidth/this.physicsWidth,this.logicHeight/this.physicsHeight))}cssToDevicePixel(t){return t.multiply(this.dpr)}cssToLogic(t){const e=this.cssToDevicePixel(t);return this.physicsToLogic(e)}devicePixelToCss(t){return t.divide(this.dpr)}toCSSMatrix(t){const e=this.physicsWidth/this.dpr/this.logicWidth,r=this.physicsHeight/this.dpr/this.logicHeight;return this.matrix.toCSSMatrix(t??this.matrixStack.curWorldM,e,r)}}function ve(n){return!(n===null||typeof n!="object"||Array.isArray(n)||n instanceof g||n instanceof p)}class k{static float(t,e){return Math.random()*(e-t)+t}static int(t,e){return Math.floor(Math.random()*(e-t+1))+t}static angle(){return Math.random()*Math.PI*2}static vector(t,e,r,i){return new g(k.float(t,e),k.float(r,i))}static randomColor(t,e){return new p(k.float(t.r,e.r),k.float(t.g,e.g),k.float(t.b,e.b),k.float(t.a,e.a))}static pick(t){return t[k.int(0,t.length-1)]}static pickWeight(t){if(!t||t.length===0)return null;let e=0;for(const[,i]of t)e+=i;let r=Math.random()*e;for(const[i,s]of t)if(r-=s,r<=0)return i;return t[t.length-1][0]}static scalarOrRange(t,e){if(t==null)return e;if(Array.isArray(t)){const[r,i]=t;if(typeof r=="number")return k.float(r,i);if(r instanceof g)return k.vector(r.x,i.x,r.y,i.y);if(r instanceof p)return k.randomColor(r,i)}return typeof t=="number"?t:t.clone()}}const ot=(n,t)=>(Array.isArray(n)?n:[n]).slice(0,t);var Vt=(n=>(n.POINT="point",n.CIRCLE="circle",n.RECT="rect",n))(Vt||{});const Xt=10,Yt=0,zt=!0;class et{components;constructor(t=1){if(!Number.isInteger(t)||t<=0)throw new RangeError("ParticleAttributeStore size must be a positive integer");this.components=Array.from({length:t},()=>({value:new P(_.Float32),delta:new P(_.Float32),damping:new P(_.Float32)}))}getArrayBuffer(){return this.components.flatMap(({value:t,delta:e,damping:r})=>[t,e,r])}update(t){for(const{value:e,delta:r,damping:i}of this.components)for(let s=0;s<e.usedElemNum;s++)e.typedArray[s]*=Math.pow(i.get(s),t),e.typedArray[s]+=r.get(s)*t}get(t,e=0){return this.components[e].value.get(t)}getComponent(t,e){if(typeof t=="number")return t;if(t instanceof g)return e===0?t.x:t.y;switch(e){case 0:return t.r;case 1:return t.g;case 2:return t.b;case 3:return t.a;default:return 0}}resolveComponent(t,e,r){return t===void 0?r:Array.isArray(t)?k.float(this.getComponent(t[0],e),this.getComponent(t[1],e)):this.getComponent(t,e)}create(t,e,r=1){const i=ve(t)?t:void 0,s=i===void 0?t:void 0,a=r>0?r:1;let o=0;for(let h=0;h<this.components.length;h++){const{value:c,delta:u,damping:l}=this.components[h],d=this.getComponent(e,h),f=s===void 0?this.resolveComponent(i?.start,h,d):this.getComponent(s,h),m=i?.end===void 0?f:this.resolveComponent(i.end,h,d);c.push(f),u.push(i?.delta===void 0?(m-f)/a:this.getComponent(i.delta,h)),l.push(i?.damping??1),h===0&&(o=f)}return o}}class gt{options;emitting=!1;emitTimer=0;emitRate=Xt;emitTime=Yt;emitTimeCounter=0;localSpace=zt;rapid;x=new P(_.Float32);y=new P(_.Float32);scaleX=new P(_.Float32);scaleY=new P(_.Float32);rotation=new P(_.Float32);color=new P(_.Uint32);remainingLife=new P(_.Float32);animations={speed:new et,rotation:new et,scale:new et,color:new et(4),velocity:new et(2)};count=0;constructor(t,e){this.rapid=t,this.options=e,this.emitRate=e.emitRate??Xt,this.emitTime=e.emitTime??Yt,this.localSpace=e.localSpace??zt}getAllArrayBuffer(){return[this.x,this.y,this.scaleX,this.scaleY,this.rotation,this.color,this.remainingLife,...Object.values(this.animations).flatMap(t=>t.getArrayBuffer())]}setTexture(t){this.options.texture=t}clone(){return new gt(this.rapid,{...this.options})}setEmitRate(t){this.emitRate=t}setEmitTime(t){this.emitTime=t}start(){this.emitting=!0,this.emitTimeCounter=0}stop(){this.emitting=!1}clear(){for(const t of this.getAllArrayBuffer())t.clear();this.count=0,this.emitTimeCounter=0}emit(t){const e=this.options.maxParticles??1/0,r=Math.min(t,e-this.count);for(let i=0;i<r;i++)this.createParticle()}createParticle(){const t=k.scalarOrRange(this.options.life,1);this.remainingLife.push(t);let e=0,r=0;switch(this.options.emitShape){case"circle":{const c=Math.random()*Math.PI*2,u=(this.options.emitRadius??0)*Math.sqrt(Math.random());e=Math.cos(c)*u,r=Math.sin(c)*u;break}case"rect":{e=(Math.random()-.5)*(this.options.emitRect?.width??0),r=(Math.random()-.5)*(this.options.emitRect?.height??0);break}}const i=this.rapid.matrixStack;if(this.localSpace)this.x.push(e),this.y.push(r);else{const{x:c,y:u}=i.localToWorld(e,r);this.x.push(c),this.y.push(u)}const s=this.options.animation,a=this.animations,o=a.scale.create(s.scale,1,t),h=a.rotation.create(s.rotation,0,t);a.speed.create(s.speed,0,t),a.velocity.create(s.velocity,g.ZERO,t),a.color.create(s.color,p.White,t),this.rotation.push(h),this.scaleX.push(o),this.scaleY.push(o),this.color.pushUint32(p.packColor(a.color.get(this.count,0),a.color.get(this.count,1),a.color.get(this.count,2),a.color.get(this.count,3),this.rapid.premultipliedAlpha)),this.count+=1}update(t){if(this.emitting&&this.emitRate>0)if(this.emitTime>0){if(this.emitTimeCounter+=t,this.emitTimeCounter>=this.emitTime){const r=Math.floor(this.emitTimeCounter/this.emitTime);this.emit(this.emitRate*r),this.emitTimeCounter-=r*this.emitTime}}else{this.emitTimer+=t;const r=Math.floor(this.emitTimer*this.emitRate);r>0&&(this.emit(r),this.emitTimer-=r/this.emitRate)}const e=this.updateParticle(t);this.count-=e.length,P.removeAtIndices(e,this.getAllArrayBuffer())}updateParticle(t){const e=[],r=this.animations;Object.values(r).forEach(i=>i.update(t));for(let i=0;i<this.count;i++){if(this.remainingLife.typedArray[i]-=t,this.remainingLife.typedArray[i]<=0){e.push(i);continue}const s=r.rotation.get(i),a=r.scale.get(i),o=r.speed.get(i);this.rotation.typedArray[i]=s,this.scaleX.typedArray[i]=a,this.scaleY.typedArray[i]=a,this.color.uint32[i]=p.packColor(r.color.get(i,0),r.color.get(i,1),r.color.get(i,2),r.color.get(i,3),this.rapid.premultipliedAlpha),this.x.typedArray[i]+=(o*Math.cos(s)+r.velocity.get(i,0))*t,this.y.typedArray[i]+=(o*Math.sin(s)+r.velocity.get(i,1))*t}return e}render(t){const e=this.options,r=t??(this.localSpace?void 0:this.rapid.matrix.alloc());try{this.rapid.drawParticles({texture:e.texture,shader:e.shader,x:this.x.typedArray,y:this.y.typedArray,scaleX:this.scaleX.typedArray,scaleY:this.scaleY.typedArray,rotation:this.rotation.typedArray,color:this.color.typedArray,count:this.count,reverseOrder:!0,originX:e.origin?.x??.5,originY:e.origin?.y??.5,customMatrix:r})}finally{t===void 0&&r!==void 0&&!this.localSpace&&this.rapid.matrix.free(r)}}isActive(){return this.emitting||this.count>0}oneShot(){this.emit(this.emitRate)}}const ye=`
345
+ `;const l=new Function(u)();return this.particleLoopCache.set(c,l),l}render(){if(this.instanceCount===0)return;this.prepareRender();const t=this.gl;this.texture&&(t.activeTexture(t.TEXTURE0),t.bindTexture(t.TEXTURE_2D,this.texture.glTexture));const e=this.currentShader;this.instanceBuffer.bindBuffer(),this.instanceBuffer.bufferData(),e.bindVAO();const r=this.rapid.matrixStack,s=this.rapid.matrix.getMatrix(this.customMatrix??r.curWorldM),a=St(this.rapid.projection,s);this.currentShader.setUniform("u_matrix",a),Et(t,t.TRIANGLE_STRIP,0,4,this.instanceCount),this.rapid.drawcallCount++,this.texture=void 0,this.customMatrix=void 0}}var Wt=(n=>(n[n.KEEP_W=0]="KEEP_W",n[n.KEEP_H=1]="KEEP_H",n[n.KEEP=2]="KEEP",n[n.IGNORE=3]="IGNORE",n[n.EXPAND=4]="EXPAND",n[n.NONE=5]="NONE",n))(Wt||{});const pe={4:new g(1,1),0:new g(0,1),1:new g(1,0),2:new g(0,0)},kt={4:new g(.5,.5),0:new g(.5,0),1:new g(0,.5),2:new g(.5,.5)};class Gt{constructor(t,e={}){this.rapid=t,this.updateWindowOption(e)}viewLeft=0;viewRight=0;viewTop=0;viewBottom=0;window={};scissorViewport=null;resolution=0;updateWindowOption(t){Object.assign(this.window,t)}resize(t,e,r,i){const s=this.rapid,a=this.window,o=a.scale??1;t/=o,e/=o,s.flush();const h=r??s.canvas.clientWidth??s.canvas.width,c=i??s.canvas.clientHeight??s.canvas.height;r!==void 0&&(s.canvas.style.width=h+"px"),i!==void 0&&(s.canvas.style.height=c+"px"),s.physicsWidth=h*s.dpr,s.physicsHeight=c*s.dpr,s.logicWidth=t,s.logicHeight=e;let u=0,l=t,d=e,f=0;const m=a.scaleMode??K.CanvasItem,x=a.expand??2;let y=g.ZERO,v=g.ZERO;switch(x in kt&&(y=a.expandPosition??kt[x],v=pe[x]),this.scissorViewport=null,x){case 3:{u=0,l=t,d=e,f=0;break}case 5:{u=0,l=s.physicsWidth,d=s.physicsHeight,f=0;break}default:case 4:case 0:case 1:case 2:{const E=y,M=v,C=Math.min(s.physicsWidth/t,s.physicsHeight/e),_=s.physicsWidth/C,F=s.physicsHeight/C,U=_-t,w=F-e;if(u=-U*E.x,l=t+U*(1-E.x),f=-w*E.y,d=e+w*(1-E.y),!M.x&&U>0||!M.y&&w>0){const R=M.x&&U>0?u:0,S=M.x&&U>0?l:t,O=M.y&&w>0?f:0,G=M.y&&w>0?d:e;this.scissorViewport={x:R,y:O,width:S-R,height:G-O}}break}}switch(m){case K.Viewport:s.canvas.width=t,s.canvas.height=e,s.canvas.style.imageRendering="pixelated",s.gl.viewport(0,0,t,e);break;default:case K.CanvasItem:s.canvas.width=s.physicsWidth,s.canvas.height=s.physicsHeight,s.canvas.style.imageRendering="auto",s.gl.viewport(0,0,s.physicsWidth,s.physicsHeight);break}this.viewLeft=u,this.viewRight=l,this.viewTop=f,this.viewBottom=d,this.updateProjection(),this.restoreViewportScissor(),this.updateResolution(m)}updateResolution(t){let e=1;const r=this.rapid.texture;if(t!==K.Viewport){const i=this.viewRight-this.viewLeft,s=this.viewBottom-this.viewTop;e=Math.max(this.rapid.physicsWidth/i,this.rapid.physicsHeight/s)}e!=this.resolution&&(this.resolution=e,r.texture.forEach(i=>i.updateResolution()))}updateProjection(){this.rapid.updateProjection(this.viewLeft,this.viewRight,this.viewBottom,this.viewTop)}updateOrthMatrix(t,e,r,i,s){t[0]=2/(r-e),t[1]=0,t[2]=0,t[3]=0,t[4]=0,t[5]=2/(s-i),t[6]=0,t[7]=0,t[8]=0,t[9]=0,t[10]=-1,t[11]=0,t[12]=-(r+e)/(r-e),t[13]=-(s+i)/(s-i),t[14]=0,t[15]=1}applyCamera(t){const e=this.rapid,r=e.matrix,i=e.matrixStack,s=this.scissorViewport&&e.gl.getParameter(e.gl.FRAMEBUFFER_BINDING)===null?this.scissorViewport:null,a=s?.width??this.viewRight-this.viewLeft,o=s?.height??this.viewBottom-this.viewTop,h=s?s.x+a/2:(this.viewLeft+this.viewRight)/2,c=s?s.y+o/2:(this.viewTop+this.viewBottom)/2,u=i.curWorldM,l=r.temporary,d=r.applyTransform(t,0,0);r.setMatrix(l,d),r.clampBounds(l,a,o,t.limitLeft,t.limitRight,t.limitTop,t.limitBottom),i.translate(h,c),r.invert(l),r.multiplyOut(u,u,l)}startScissor(t,e,r,i){const s=this.rapid;s.flush();const a=s.gl,o=this.scissorViewport;if(o&&a.getParameter(a.FRAMEBUFFER_BINDING)===null){const _=Math.max(Math.min(t,t+r),o.x),F=Math.min(Math.max(t,t+r),o.x+o.width),U=Math.max(Math.min(e,e+i),o.y),w=Math.min(Math.max(e,e+i),o.y+o.height);t=_,e=U,r=Math.max(0,F-_),i=Math.max(0,w-U)}const[h,c,u,l]=a.getParameter(a.VIEWPORT),d=s.projection,f=h+(d[0]*t+d[12]+1)*u/2,m=h+(d[0]*(t+r)+d[12]+1)*u/2,x=c+(d[5]*e+d[13]+1)*l/2,y=c+(d[5]*(e+i)+d[13]+1)*l/2,v=Math.round(Math.min(f,m)),E=Math.round(Math.max(f,m)),M=Math.round(Math.min(x,y)),C=Math.round(Math.max(x,y));a.enable(a.SCISSOR_TEST),a.scissor(v,M,E-v,C-M)}restoreViewportScissor(){this.rapid.flush();const t=this.rapid.gl,e=this.scissorViewport;e&&t.getParameter(t.FRAMEBUFFER_BINDING)===null?this.startScissor(e.x,e.y,e.width,e.height):t.disable(t.SCISSOR_TEST)}endScissor(){this.restoreViewportScissor()}}var pt=(n=>(n[n.EQUAL=0]="EQUAL",n[n.NOT_EQUAL=1]="NOT_EQUAL",n))(pt||{}),Ht=(n=>(n[n.NORMAL=0]="NORMAL",n[n.ADD=1]="ADD",n[n.MULTIPLY=2]="MULTIPLY",n[n.SCREEN=3]="SCREEN",n[n.ERASE=4]="ERASE",n))(Ht||{}),K=(n=>(n[n.CanvasItem=0]="CanvasItem",n[n.Viewport=1]="Viewport",n))(K||{});class ge{gl;canvas;projection=new Float32Array(16);projectionDirty=!0;dpr;backgroundColor=p.Black;logicWidth=0;logicHeight=0;physicsWidth=0;physicsHeight=0;currentRegion=null;maxTextureUnits=0;matrixStack=new se(this);matrix;spriteRegion;graphicRegion;atlasSpriteRegion;particleRegion;regions;drawcallCount=0;inCreateMask=!1;texture;premultipliedAlpha;antialias;roundPixels;textureFilter;viewport;_filterRT=[null,null];_filterInput=new W;get height(){return this.logicHeight}get width(){return this.logicWidth}constructor(t){this.canvas=t.canvas,this.dpr=window.devicePixelRatio||1,this.antialias=t.antialias??!1,this.textureFilter=t.textureFilter??q.NEAREST,this.premultipliedAlpha=t.premultipliedAlpha??!0,this.roundPixels=t.roundPixels??!1;const e=Kt(this.canvas,this.antialias,this.premultipliedAlpha);this.gl=e,this.maxTextureUnits=e.getParameter(e.MAX_TEXTURE_IMAGE_UNITS),this.matrix=this.matrixStack.matrix,this.spriteRegion=new ft(this),this.graphicRegion=new ce(this),this.atlasSpriteRegion=new le(this),this.particleRegion=new xe(this),this.viewport=new Gt(this,t.window),this.texture=new vt(this),this.regions=[this.spriteRegion,this.atlasSpriteRegion];const r=this.canvas.clientWidth??this.canvas.width,i=this.canvas.clientHeight??this.canvas.height;this.physicsWidth=t.physicsWidth??Math.round(r*this.dpr),this.physicsHeight=t.physicsHeight??Math.round(i*this.dpr),this.logicWidth=t.width??this.physicsWidth/this.dpr,this.logicHeight=t.height??this.physicsHeight/this.dpr,this.resize(this.logicWidth,this.logicHeight,this.physicsWidth,this.physicsHeight),t.backgroundColor&&(this.backgroundColor=t.backgroundColor),e.enable(e.BLEND),this.premultipliedAlpha?e.blendFunc(e.ONE,e.ONE_MINUS_SRC_ALPHA):e.blendFunc(e.SRC_ALPHA,e.ONE_MINUS_SRC_ALPHA),e.enable(e.STENCIL_TEST),e.stencilFunc(e.ALWAYS,1,255),e.stencilOp(e.KEEP,e.KEEP,e.KEEP)}setTextureFilter(t){this.textureFilter=t}getColorUint32(t){return Pt(this.premultipliedAlpha,t)}enterRegion(t,e){this.currentRegion===t&&this.currentRegion.isSameShader(e)||(this.flush(),this.currentRegion=t,t.enter(e))}drawParticles(t){Nt(this,t)}drawSprite(t){Lt(this,t)}drawLine(t){It(this,t)}drawGraphic(t){xt(this,t)}drawRect(t){Ot(this,t)}drawCircle(t){Bt(this,t)}startGraphic(t=this.gl.TRIANGLES,e,r,i){this.enterRegion(this.graphicRegion,r),this.graphicRegion.startGraphic(i??this.matrixStack.curWorldM,t,e)}startMaskGraphic(t=this.gl.TRIANGLES,e,r){this.startGraphic(t,e,this.graphicRegion.maskShader,r)}drawMaskImage(t){Ft(this,t)}addRectVertex(t,e,r){const i=this.getColorUint32(r);this.addGraphicVertex(0,0,0,0,i),this.addGraphicVertex(t,0,1,0,i),this.addGraphicVertex(t,e,1,1,i),this.addGraphicVertex(0,e,0,1,i)}addCircleVertex(t,e,r=32){const i=Math.PI*2/r,s=this.getColorUint32(e);for(let a=0;a<r;a++){const o=i*a,h=Math.cos(o)*t,c=Math.sin(o)*t,u=.5+Math.cos(o)*.5,l=.5+Math.sin(o)*.5;this.addGraphicVertex(h,c,u,l,s)}}addGraphicVertex(t,e,r=0,i=0,s=4294967295){this.graphicRegion.addVertex(t,e,r,i,typeof s=="number"?s:this.getColorUint32(s))}endGraphic(){this.graphicRegion.endGraphic()}resize(t,e,r,i){this.viewport.resize(t,e,r,i)}updateProjection(t,e,r,i){this.viewport.updateOrthMatrix(this.projection,t,e,r,i),this.projectionDirty=!0}clear(){const t=this.gl;this.flush(),t.disable(t.SCISSOR_TEST),this.viewport.scissorViewport&&t.getParameter(t.FRAMEBUFFER_BINDING)===null?(t.clearColor(0,0,0,1),t.clear(t.COLOR_BUFFER_BIT|t.STENCIL_BUFFER_BIT),this.backgroundColor.setClearColor(t,this.premultipliedAlpha),this.viewport.restoreViewportScissor(),t.clear(t.COLOR_BUFFER_BIT)):(this.backgroundColor.setClearColor(t,this.premultipliedAlpha),t.clear(t.COLOR_BUFFER_BIT|t.STENCIL_BUFFER_BIT)),this.drawcallCount=0,this.matrixStack.reset()}flush(){this.currentRegion&&(this.currentRegion.exit(),this.currentRegion=null)}applyFilters(t,e){if(e.length===0)throw new Error("applyFilters: shaders array must not be empty.");const r=Math.max(...e.map(c=>c.padding)),i=r,s=t.rawWidth+r*2,a=t.rawHeight+r*2;for(let c=0;c<2;c++)this._filterRT[c]?this._filterRT[c].resize(s,a):this._filterRT[c]=this.texture.createRenderTexture({width:s,height:a});let o=t.clone(this._filterInput),h=0;this.matrixStack.save(),this.matrixStack.identity();for(let c=0;c<e.length;c++){const u=this._filterRT[h%2];u.offsetX=0,u.offsetY=0,this.enterRenderTexture(u),this.clearRenderTexture(),this.drawSprite({texture:o,shader:e[c],offsetX:c==0?i:0,offsetY:c==0?i:0,padding:r}),this.leaveRenderTexture(),o=u,h++}return this.matrixStack.restore(),o.offsetX=-i,o.offsetY=-i,o}enterRenderTexture(t){this.flush(),t.activate(),this.viewport.restoreViewportScissor(),this.gl.viewport(0,0,t.rawWidth,t.rawHeight),this.updateProjection(0,t.rawWidth,0,t.rawHeight)}clearRenderTexture(t=new p(0,0,0,0)){this.flush();const e=this.gl;t.setClearColor(e,this.premultipliedAlpha),e.clear(e.COLOR_BUFFER_BIT)}leaveRenderTexture(){this.flush(),this.gl.bindFramebuffer(this.gl.FRAMEBUFFER,null),this.gl.viewport(0,0,this.canvas.width,this.canvas.height),this.viewport.updateProjection(),this.viewport.restoreViewportScissor()}drawToRenderTexture(t,e,r=new p(0,0,0,0)){this.enterRenderTexture(t);try{r!==null&&this.clearRenderTexture(r),e()}finally{this.leaveRenderTexture()}}withMask(t,e,r=0,i=1){this.clearMask(),this.startDrawMask(i);try{t()}finally{this.endDrawMask()}this.enterMask(r,i);try{e()}finally{this.exitMask()}}withTransform(t,e,r=0,i=0){this.matrixStack.save();try{e&&this.matrixStack.applyTransform(e,r,i),t()}finally{this.matrixStack.restore()}}withScissor(t,e,r,i,s){this.startScissor(t,e,r,i);try{s()}finally{this.endScissor()}}withBlendMode(t,e){this.setBlendMode(t);try{e()}finally{this.setBlendMode(0)}}startDrawMask(t=1,e=255){this.flush();const r=this.gl;r.stencilMask(e),r.colorMask(!1,!1,!1,!1),r.stencilFunc(r.ALWAYS,t,e),r.stencilOp(r.KEEP,r.KEEP,r.REPLACE),this.enterRegion(this.graphicRegion,this.graphicRegion.maskShader),this.inCreateMask=!0}endDrawMask(){this.flush();const t=this.gl;t.colorMask(!0,!0,!0,!0),t.stencilMask(0),this.inCreateMask=!1}clearMask(t=255){this.flush();const e=this.gl;e.stencilMask(t),e.clear(e.STENCIL_BUFFER_BIT),this.inCreateMask=!1}enterMask(t,e=1,r=255){this.flush();const i=this.gl;i.colorMask(!0,!0,!0,!0),i.stencilMask(0),i.stencilFunc(t===0?i.EQUAL:i.NOTEQUAL,e,r),i.stencilOp(i.KEEP,i.KEEP,i.KEEP),this.inCreateMask=!1}exitMask(){this.flush();const t=this.gl;t.colorMask(!0,!0,!0,!0),t.stencilMask(255),t.stencilFunc(t.ALWAYS,1,255),t.stencilOp(t.KEEP,t.KEEP,t.KEEP),this.inCreateMask=!1}setBlendMode(t){this.flush();const e=this.gl;switch(t){case 0:this.premultipliedAlpha?e.blendFuncSeparate(e.ONE,e.ONE_MINUS_SRC_ALPHA,e.ONE,e.ONE_MINUS_SRC_ALPHA):e.blendFuncSeparate(e.SRC_ALPHA,e.ONE_MINUS_SRC_ALPHA,e.ONE,e.ONE_MINUS_SRC_ALPHA);break;case 1:e.blendFuncSeparate(e.ONE,e.ONE,e.ONE,e.ONE);break;case 2:e.blendFuncSeparate(e.DST_COLOR,e.ONE_MINUS_SRC_ALPHA,e.ONE,e.ONE_MINUS_SRC_ALPHA);break;case 3:e.blendFuncSeparate(e.ONE,e.ONE_MINUS_SRC_COLOR,e.ONE,e.ONE_MINUS_SRC_ALPHA);break;case 4:e.blendFuncSeparate(e.ZERO,e.ONE_MINUS_SRC_ALPHA,e.ZERO,e.ONE_MINUS_SRC_ALPHA);break}}startScissor(t,e,r,i){this.viewport.startScissor(t,e,r,i)}endScissor(){this.viewport.endScissor()}applyCamera(t){this.viewport.applyCamera(t)}logicToPhysics(t){return t.multiply(new g(this.physicsWidth/this.logicWidth,this.physicsHeight/this.logicHeight))}physicsToLogic(t){return t.multiply(new g(this.logicWidth/this.physicsWidth,this.logicHeight/this.physicsHeight))}cssToDevicePixel(t){return t.multiply(this.dpr)}cssToLogic(t){const e=this.cssToDevicePixel(t);return this.physicsToLogic(e)}devicePixelToCss(t){return t.divide(this.dpr)}toCSSMatrix(t){const e=this.physicsWidth/this.dpr/this.logicWidth,r=this.physicsHeight/this.dpr/this.logicHeight;return this.matrix.toCSSMatrix(t??this.matrixStack.curWorldM,e,r)}}function ve(n){return!(n===null||typeof n!="object"||Array.isArray(n)||n instanceof g||n instanceof p)}class k{static float(t,e){return Math.random()*(e-t)+t}static int(t,e){return Math.floor(Math.random()*(e-t+1))+t}static angle(){return Math.random()*Math.PI*2}static vector(t,e,r,i){return new g(k.float(t,e),k.float(r,i))}static randomColor(t,e){return new p(k.float(t.r,e.r),k.float(t.g,e.g),k.float(t.b,e.b),k.float(t.a,e.a))}static pick(t){return t[k.int(0,t.length-1)]}static pickWeight(t){if(!t||t.length===0)return null;let e=0;for(const[,i]of t)e+=i;let r=Math.random()*e;for(const[i,s]of t)if(r-=s,r<=0)return i;return t[t.length-1][0]}static scalarOrRange(t,e){if(t==null)return e;if(Array.isArray(t)){const[r,i]=t;if(typeof r=="number")return k.float(r,i);if(r instanceof g)return k.vector(r.x,i.x,r.y,i.y);if(r instanceof p)return k.randomColor(r,i)}return typeof t=="number"?t:t.clone()}}const ot=(n,t)=>(Array.isArray(n)?n:[n]).slice(0,t);var Vt=(n=>(n.POINT="point",n.CIRCLE="circle",n.RECT="rect",n))(Vt||{});const Xt=10,zt=0,Yt=!0;class et{components;constructor(t=1){if(!Number.isInteger(t)||t<=0)throw new RangeError("ParticleAttributeStore size must be a positive integer");this.components=Array.from({length:t},()=>({value:new N(L.Float32),delta:new N(L.Float32),damping:new N(L.Float32)}))}getArrayBuffer(){return this.components.flatMap(({value:t,delta:e,damping:r})=>[t,e,r])}update(t){for(const{value:e,delta:r,damping:i}of this.components)for(let s=0;s<e.usedElemNum;s++)e.typedArray[s]*=Math.pow(i.get(s),t),e.typedArray[s]+=r.get(s)*t}get(t,e=0){return this.components[e].value.get(t)}getComponent(t,e){if(typeof t=="number")return t;if(t instanceof g)return e===0?t.x:t.y;switch(e){case 0:return t.r;case 1:return t.g;case 2:return t.b;case 3:return t.a;default:return 0}}resolveComponent(t,e,r){return t===void 0?r:Array.isArray(t)?k.float(this.getComponent(t[0],e),this.getComponent(t[1],e)):this.getComponent(t,e)}create(t,e,r=1){const i=ve(t)?t:void 0,s=i===void 0?t:void 0,a=r>0?r:1;let o=0;for(let h=0;h<this.components.length;h++){const{value:c,delta:u,damping:l}=this.components[h],d=this.getComponent(e,h),f=s===void 0?this.resolveComponent(i?.start,h,d):this.getComponent(s,h),m=i?.end===void 0?f:this.resolveComponent(i.end,h,d);c.push(f),u.push(i?.delta===void 0?(m-f)/a:this.getComponent(i.delta,h)),l.push(i?.damping??1),h===0&&(o=f)}return o}}class gt{options;emitting=!1;emitTimer=0;emitRate=Xt;emitTime=zt;emitTimeCounter=0;localSpace=Yt;rapid;x=new N(L.Float32);y=new N(L.Float32);scaleX=new N(L.Float32);scaleY=new N(L.Float32);rotation=new N(L.Float32);color=new N(L.Uint32);remainingLife=new N(L.Float32);animations={speed:new et,rotation:new et,scale:new et,color:new et(4),velocity:new et(2)};count=0;constructor(t,e){this.rapid=t,this.options=e,this.emitRate=e.emitRate??Xt,this.emitTime=e.emitTime??zt,this.localSpace=e.localSpace??Yt}getAllArrayBuffer(){return[this.x,this.y,this.scaleX,this.scaleY,this.rotation,this.color,this.remainingLife,...Object.values(this.animations).flatMap(t=>t.getArrayBuffer())]}setTexture(t){this.options.texture=t}clone(){return new gt(this.rapid,{...this.options})}setEmitRate(t){this.emitRate=t}setEmitTime(t){this.emitTime=t}start(){this.emitting=!0,this.emitTimeCounter=0}stop(){this.emitting=!1}clear(){for(const t of this.getAllArrayBuffer())t.clear();this.count=0,this.emitTimeCounter=0}emit(t){const e=this.options.maxParticles??1/0,r=Math.min(t,e-this.count);for(let i=0;i<r;i++)this.createParticle()}createParticle(){const t=k.scalarOrRange(this.options.life,1);this.remainingLife.push(t);let e=0,r=0;switch(this.options.emitShape){case"circle":{const c=Math.random()*Math.PI*2,u=(this.options.emitRadius??0)*Math.sqrt(Math.random());e=Math.cos(c)*u,r=Math.sin(c)*u;break}case"rect":{e=(Math.random()-.5)*(this.options.emitRect?.width??0),r=(Math.random()-.5)*(this.options.emitRect?.height??0);break}}const i=this.rapid.matrixStack;if(this.localSpace)this.x.push(e),this.y.push(r);else{const{x:c,y:u}=i.localToWorld(e,r);this.x.push(c),this.y.push(u)}const s=this.options.animation,a=this.animations,o=a.scale.create(s.scale,1,t),h=a.rotation.create(s.rotation,0,t);a.speed.create(s.speed,0,t),a.velocity.create(s.velocity,g.ZERO,t),a.color.create(s.color,p.White,t),this.rotation.push(h),this.scaleX.push(o),this.scaleY.push(o),this.color.pushUint32(p.packColor(a.color.get(this.count,0),a.color.get(this.count,1),a.color.get(this.count,2),a.color.get(this.count,3),this.rapid.premultipliedAlpha)),this.count+=1}update(t){if(this.emitting&&this.emitRate>0)if(this.emitTime>0){if(this.emitTimeCounter+=t,this.emitTimeCounter>=this.emitTime){const r=Math.floor(this.emitTimeCounter/this.emitTime);this.emit(this.emitRate*r),this.emitTimeCounter-=r*this.emitTime}}else{this.emitTimer+=t;const r=Math.floor(this.emitTimer*this.emitRate);r>0&&(this.emit(r),this.emitTimer-=r/this.emitRate)}const e=this.updateParticle(t);this.count-=e.length,N.removeAtIndices(e,this.getAllArrayBuffer())}updateParticle(t){const e=[],r=this.animations;Object.values(r).forEach(i=>i.update(t));for(let i=0;i<this.count;i++){if(this.remainingLife.typedArray[i]-=t,this.remainingLife.typedArray[i]<=0){e.push(i);continue}const s=r.rotation.get(i),a=r.scale.get(i),o=r.speed.get(i);this.rotation.typedArray[i]=s,this.scaleX.typedArray[i]=a,this.scaleY.typedArray[i]=a,this.color.uint32[i]=p.packColor(r.color.get(i,0),r.color.get(i,1),r.color.get(i,2),r.color.get(i,3),this.rapid.premultipliedAlpha),this.x.typedArray[i]+=(o*Math.cos(s)+r.velocity.get(i,0))*t,this.y.typedArray[i]+=(o*Math.sin(s)+r.velocity.get(i,1))*t}return e}render(t){const e=this.options,r=t??(this.localSpace?void 0:this.rapid.matrix.alloc());try{this.rapid.drawParticles({texture:e.texture,shader:e.shader,x:this.x.typedArray,y:this.y.typedArray,scaleX:this.scaleX.typedArray,scaleY:this.scaleY.typedArray,rotation:this.rotation.typedArray,color:this.color.typedArray,count:this.count,reverseOrder:!0,originX:e.origin?.x??.5,originY:e.origin?.y??.5,customMatrix:r})}finally{t===void 0&&r!==void 0&&!this.localSpace&&this.rapid.matrix.free(r)}}isActive(){return this.emitting||this.count>0}oneShot(){this.emit(this.emitRate)}}const ye=`
346
346
  uniform mediump int uLightTextureId[%TEXTURE_NUM%];
347
347
 
348
348
  uniform sampler2D uNormalMap;
@@ -400,4 +400,4 @@ void light_fragment(inout vec4 color, in vec2 vRegion) {
400
400
  void light_vertex(inout vec4 p, vec2 u) {
401
401
  light_worldPos = p.xy;
402
402
  }
403
- `,rt=8;class $t extends nt{constructor(t,e="",r="",i=0,s){super(t,e,r,i+2,s)}beforePreCompile(){this.prefix.push("light_")}getGLShader(t,e,r,i){const s=Z(ye,t.getMaxTexutreUnit(this),"layer","lightUV","lightTex = ");return i=this.replaceCustomCode(i,s),r=this.replaceCustomCode(r,Te),super.getGLShader(t,e,r,i)}}class we{shader;rapid;rt;constructor(t){this.rapid=t,this.shader=new $t(t),this.rt=t.texture.createRenderTexture({width:0,height:0})}lightShadow(t,e,r){if(r.length===0)return;const i=this.rapid,s=typeof e=="number"?e:e.world,a=new g(t.rawWidth*.5,t.rawHeight*.5),o=r.map(c=>{const u=i.matrixStack.localToWorld(c.x,c.y),l=i.matrix.worldToLocal(s,u.x,u.y);return new g((l.x+.5)*t.rawWidth,(l.y+.5)*t.rawHeight)});this.rt.resize(t.rawWidth,t.rawHeight);const h=Math.hypot(t.rawWidth,t.rawHeight)*.5*1.05;return i.drawToRenderTexture(this.rt,()=>{i.matrixStack.save(),i.matrixStack.identity();try{i.withMask(()=>{i.startMaskGraphic(i.gl.TRIANGLES);for(let c=0;c<o.length;c++){const u=o[c],l=o[(c+1)%o.length],d=u.subtract(a).normalized(),f=l.subtract(a).normalized(),m=Math.max(-.999,Math.min(1,d.dot(f))),x=Math.sqrt((1+m)*.5),T=h/x,y=a.add(d.multiply(T)),w=a.add(f.multiply(T));i.addGraphicVertex(u.x,u.y),i.addGraphicVertex(l.x,l.y),i.addGraphicVertex(w.x,w.y),i.addGraphicVertex(u.x,u.y),i.addGraphicVertex(w.x,w.y),i.addGraphicVertex(y.x,y.y)}i.endGraphic()},()=>{i.drawSprite({texture:t})},pt.NOT_EQUAL)}finally{i.matrixStack.restore()}}),this.rt}setupLight(t){const e=t.customShader??this.shader,r=ot(t.lightMatrix,rt),i=ot(t.color,rt),s=ot(t.lightHeight,rt),a=ot(t.lightTexture,rt);return e.setUniforms({uNormalMap:t.normalMap.glTexture,uLightTextureId:a.map(o=>o.glTexture),uLightCount:r.length,uLightMatrix:Float32Array.from(r.flatMap(o=>Array.from(this.rapid.matrix.getMatrix(typeof o=="number"?o:o.world)))),uLightColor:Float32Array.from(r.flatMap((o,h)=>{const c=i[h]??i[0];return c?[c.r/255,c.g/255,c.b/255,c.a/255]:[1,1,1,1]})),uLightHeight:Float32Array.from(r.map((o,h)=>s[h]??s[0]??80)),uMetallic:t.metallic??.8,uRoughness:t.roughness??.15,uAmbientColor:t.ambientColor?[t.ambientColor.r/255,t.ambientColor.g/255,t.ambientColor.b/255]:[.1,.1,.15],uNormalScale:[t.normalScaleX??1,t.normalScaleY??1]}),e}}v.BaseTexture=X,v.BlendMode=Ht,v.CanvasScaleMode=K,v.Color=p,v.CustomGlShader=nt,v.ExpandMode=Wt,v.GLShader=lt,v.Light=we,v.LightShader=$t,v.LineTextureMode=bt,v.MAX_LIGHTS=rt,v.MaskType=pt,v.ParticleEmitter=gt,v.ParticleShape=Vt,v.Rapid=ge,v.RenderTexture=yt,v.TextTexture=Tt,v.Texture=W,v.TextureFilterMode=q,v.TextureManager=vt,v.TextureWrapMode=j,v.Vec2=g,v.ViewPort=Gt,v.composeProjectionWithAffine=St,v.drawCircle=Bt,v.drawGraphic=xt,v.drawLine=It,v.drawMaskImage=Ft,v.drawParticles=Nt,v.drawRect=Ot,v.drawSprite=Lt,v.getColorUint32=Pt,v.getLineGeometry=_t,v.getLineNormal=Ct,v.multiplyMat4=At,Object.defineProperty(v,Symbol.toStringTag,{value:"Module"})}));
403
+ `,rt=8;class $t extends nt{constructor(t,e="",r="",i=0,s){super(t,e,r,i+2,s)}beforePreCompile(){this.prefix.push("light_")}getGLShader(t,e,r,i){const s=Z(ye,t.getMaxTexutreUnit(this),"layer","lightUV","lightTex = ");return i=this.replaceCustomCode(i,s),r=this.replaceCustomCode(r,Te),super.getGLShader(t,e,r,i)}}class we{shader;rapid;rt;constructor(t){this.rapid=t,this.shader=new $t(t),this.rt=t.texture.createRenderTexture({width:0,height:0})}lightShadow(t,e,r){if(r.length===0)return;const i=this.rapid,s=typeof e=="number"?e:e.world,a=new g(t.rawWidth*.5,t.rawHeight*.5),o=r.map(c=>{const u=i.matrixStack.localToWorld(c.x,c.y),l=i.matrix.worldToLocal(s,u.x,u.y);return new g((l.x+.5)*t.rawWidth,(l.y+.5)*t.rawHeight)});this.rt.resize(t.rawWidth,t.rawHeight);const h=Math.hypot(t.rawWidth,t.rawHeight)*.5*1.05;return i.drawToRenderTexture(this.rt,()=>{i.matrixStack.save(),i.matrixStack.identity();try{i.withMask(()=>{i.startMaskGraphic(i.gl.TRIANGLES);for(let c=0;c<o.length;c++){const u=o[c],l=o[(c+1)%o.length],d=u.subtract(a).normalized(),f=l.subtract(a).normalized(),m=Math.max(-.999,Math.min(1,d.dot(f))),x=Math.sqrt((1+m)*.5),y=h/x,v=a.add(d.multiply(y)),E=a.add(f.multiply(y));i.addGraphicVertex(u.x,u.y),i.addGraphicVertex(l.x,l.y),i.addGraphicVertex(E.x,E.y),i.addGraphicVertex(u.x,u.y),i.addGraphicVertex(E.x,E.y),i.addGraphicVertex(v.x,v.y)}i.endGraphic()},()=>{i.drawSprite({texture:t})},pt.NOT_EQUAL)}finally{i.matrixStack.restore()}}),this.rt}setupLight(t){const e=t.customShader??this.shader,r=ot(t.lightMatrix,rt),i=ot(t.color,rt),s=ot(t.lightHeight,rt),a=ot(t.lightTexture,rt);return e.setUniforms({uNormalMap:t.normalMap.glTexture,uLightTextureId:a.map(o=>o.glTexture),uLightCount:r.length,uLightMatrix:Float32Array.from(r.flatMap(o=>Array.from(this.rapid.matrix.getMatrix(typeof o=="number"?o:o.world)))),uLightColor:Float32Array.from(r.flatMap((o,h)=>{const c=i[h]??i[0];return c?[c.r/255,c.g/255,c.b/255,c.a/255]:[1,1,1,1]})),uLightHeight:Float32Array.from(r.map((o,h)=>s[h]??s[0]??80)),uMetallic:t.metallic??.8,uRoughness:t.roughness??.15,uAmbientColor:t.ambientColor?[t.ambientColor.r/255,t.ambientColor.g/255,t.ambientColor.b/255]:[.1,.1,.15],uNormalScale:[t.normalScaleX??1,t.normalScaleY??1]}),e}}T.BaseTexture=X,T.BlendMode=Ht,T.CanvasScaleMode=K,T.Color=p,T.CustomGlShader=nt,T.ExpandMode=Wt,T.GLShader=lt,T.Light=we,T.LightShader=$t,T.LineTextureMode=bt,T.MAX_LIGHTS=rt,T.MaskType=pt,T.ParticleEmitter=gt,T.ParticleShape=Vt,T.Rapid=ge,T.RenderTexture=yt,T.TextTexture=Tt,T.Texture=W,T.TextureFilterMode=q,T.TextureManager=vt,T.TextureWrapMode=j,T.Vec2=g,T.ViewPort=Gt,T.composeProjectionWithAffine=St,T.drawCircle=Bt,T.drawGraphic=xt,T.drawLine=It,T.drawMaskImage=Ft,T.drawParticles=Nt,T.drawRect=Ot,T.drawSprite=Lt,T.getColorUint32=Pt,T.getLineGeometry=_t,T.getLineNormal=Ct,T.multiplyMat4=At,Object.defineProperty(T,Symbol.toStringTag,{value:"Module"})}));
package/dist/render.d.ts CHANGED
@@ -33,7 +33,7 @@ export interface IAppOptions {
33
33
  /** Whether textures are uploaded with premultiplied alpha. Default: true. */
34
34
  premultipliedAlpha?: boolean;
35
35
  roundPixels?: boolean;
36
- window: IWindowsOptions;
36
+ window?: IWindowsOptions;
37
37
  }
38
38
  export interface IWindowsOptions {
39
39
  scaleMode?: CanvasScaleMode;
@@ -29,7 +29,7 @@ export declare class ViewPort {
29
29
  private window;
30
30
  scissorViewport: ScissorViewport | null;
31
31
  resolution: number;
32
- constructor(rapid: Rapid, options: IWindowsOptions);
32
+ constructor(rapid: Rapid, options?: IWindowsOptions);
33
33
  updateWindowOption(window: IWindowsOptions): void;
34
34
  /**
35
35
  * Resizes the canvas, updates internal viewport values, and recreates projection boundaries.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "rapid-render",
3
3
  "private": false,
4
- "version": "1.0.26",
4
+ "version": "1.0.28",
5
5
  "type": "module",
6
6
  "description": "A high-performance lightweight WebGL 2D renderer for browser games.",
7
7
  "repository": "https://github.com/Nightre/Rapid.js",