rapid-render 1.0.19 → 1.0.22

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.
@@ -153,7 +153,7 @@ class m {
153
153
  static Black = new m(0, 0, 0, 255);
154
154
  static Transparent = new m(0, 0, 0, 0);
155
155
  }
156
- var tt = /* @__PURE__ */ ((n) => (n[n.LINEAR = 0] = "LINEAR", n[n.NEAREST = 1] = "NEAREST", n))(tt || {}), q = /* @__PURE__ */ ((n) => (n[n.REPEAT = 0] = "REPEAT", n[n.CLAMP = 1] = "CLAMP", n[n.MIRRORED_REPEAT = 2] = "MIRRORED_REPEAT", n))(q || {});
156
+ var Q = /* @__PURE__ */ ((n) => (n[n.LINEAR = 0] = "LINEAR", n[n.NEAREST = 1] = "NEAREST", n))(Q || {}), q = /* @__PURE__ */ ((n) => (n[n.REPEAT = 0] = "REPEAT", n[n.CLAMP = 1] = "CLAMP", n[n.MIRRORED_REPEAT = 2] = "MIRRORED_REPEAT", n))(q || {});
157
157
  class bt {
158
158
  render;
159
159
  cache = /* @__PURE__ */ new Map();
@@ -163,19 +163,19 @@ class bt {
163
163
  async load(t, e) {
164
164
  let r = this.cache.get(t);
165
165
  if (r)
166
- return new D(r);
166
+ return new F(r);
167
167
  try {
168
168
  const i = await this._fetchImage(t);
169
- return r = X.fromSource(this.render, i, e), r.uid = t, this.cache.set(t, r), new D(r);
169
+ return r = X.fromSource(this.render, i, e), r.uid = t, this.cache.set(t, r), new F(r);
170
170
  } catch (i) {
171
171
  throw console.error(`[TextureManager] Failed to load: ${t}`, i), i;
172
172
  }
173
173
  }
174
174
  create(t, e) {
175
175
  if (e?.key && this.cache.has(e.key))
176
- return new D(this.cache.get(e.key));
176
+ return new F(this.cache.get(e.key));
177
177
  const r = X.fromSource(this.render, t, e);
178
- return e?.key && (r.uid = e.key, this.cache.set(e.key, r)), new D(r);
178
+ return e?.key && (r.uid = e.key, this.cache.set(e.key, r)), new F(r);
179
179
  }
180
180
  createRenderTexture(t) {
181
181
  return new Ut(this.render, t);
@@ -185,7 +185,7 @@ class bt {
185
185
  }
186
186
  destroy(t, e = !1) {
187
187
  let r, i;
188
- typeof t == "string" ? (i = t, r = this.cache.get(i)) : t instanceof D ? (r = t.base, i = r?.uid, t.destroy()) : (r = t, i = r.uid), r && (r.refCount <= 0 || e) && (r.destroy(this.render.gl), i && this.cache.delete(i));
188
+ typeof t == "string" ? (i = t, r = this.cache.get(i)) : t instanceof F ? (r = t.base, i = r?.uid, t.destroy()) : (r = t, i = r.uid), r && (r.refCount <= 0 || e) && (r.destroy(this.render.gl), i && this.cache.delete(i));
189
189
  }
190
190
  destroyAll() {
191
191
  for (const t of this.cache.values())
@@ -225,7 +225,7 @@ class X {
225
225
  this.glTexture && (t.deleteTexture(this.glTexture), this.glTexture = null);
226
226
  }
227
227
  }
228
- class D {
228
+ class F {
229
229
  base;
230
230
  uvX = 0;
231
231
  uvY = 0;
@@ -259,12 +259,12 @@ class D {
259
259
  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;
260
260
  }
261
261
  getSubTexture(t, e, r, i) {
262
- if (!this.base) return new D();
262
+ if (!this.base) return new F();
263
263
  const s = this.clone(), a = this._px, h = this._py;
264
264
  return s.setRegion(a + t, h + e, r, i), s;
265
265
  }
266
266
  clone(t) {
267
- const e = t ?? new D(this.base);
267
+ const e = t ?? new F(this.base);
268
268
  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;
269
269
  }
270
270
  splitGrid(t, e, r, i, s = 0) {
@@ -277,13 +277,13 @@ class D {
277
277
  }
278
278
  static fromImageSource(t, e, r = {}) {
279
279
  const i = rt(t, e, r);
280
- return new D(new X(i, e.width, e.height));
280
+ return new F(new X(i, e.width, e.height));
281
281
  }
282
282
  destroy() {
283
283
  this.base && (this.base.refCount--, this.base = void 0), this.glTexture = null;
284
284
  }
285
285
  }
286
- class Ut extends D {
286
+ class Ut extends F {
287
287
  framebuffer;
288
288
  renderbuffer;
289
289
  gl;
@@ -346,7 +346,7 @@ const Ct = {
346
346
  align: "left",
347
347
  lineHeight: 1
348
348
  };
349
- class _t extends D {
349
+ class _t extends F {
350
350
  canvas;
351
351
  ctx;
352
352
  render;
@@ -355,7 +355,11 @@ class _t extends D {
355
355
  options;
356
356
  flipY = !0;
357
357
  constructor(t, e) {
358
- super(), this.render = t, this._style = { ...Ct, ...e }, this._text = e?.text ?? "", this.options = { premultipliedAlpha: t.premultipliedAlpha, ...e }, this.scale = 1 / t.dpr, this.canvas = document.createElement("canvas");
358
+ super(), this.render = t, this._style = { ...Ct, ...e }, this._text = e?.text ?? "", this.options = {
359
+ premultipliedAlpha: t.premultipliedAlpha,
360
+ textureFilter: Q.LINEAR,
361
+ ...e
362
+ }, this.canvas = document.createElement("canvas");
359
363
  const r = this.canvas.getContext("2d", { willReadFrequently: !0 });
360
364
  if (!r) throw new Error("Failed to get 2d context for TextTexture");
361
365
  this.ctx = r, this.canvas.width = 1, this.canvas.height = 1;
@@ -385,33 +389,40 @@ class _t extends D {
385
389
  }
386
390
  }
387
391
  update() {
388
- const t = this.ctx, e = this.style, r = this.render.dpr, i = e.fontSize, s = e.fontWeight, a = e.fontFamily, h = e.lineHeight, o = `${s} ${i}px ${a}`, c = e.baseline ?? "middle", u = e.align ?? "left", l = e.strokeThickness ?? 0, f = l > 0, d = this._text.split(`
392
+ const t = this.ctx, e = this.style, r = this.render.dpr;
393
+ this.scale = 1 / r;
394
+ const i = e.fontSize, s = e.fontWeight, a = e.fontFamily, h = e.lineHeight, o = `${s} ${i}px ${a}`, c = e.baseline ?? "top", u = e.align ?? "left", l = e.strokeThickness ?? 0, f = l > 0, d = this._text.split(`
389
395
  `), x = "国g", p = d[0] || x;
390
396
  t.font = o, t.textBaseline = "alphabetic", t.textAlign = "left";
391
397
  let y = 0, v = 0, E = 0;
392
- for (const F of d) {
393
- const H = t.measureText(F || x);
394
- y = Math.max(y, F ? H.width : 0), v = Math.max(v, H.actualBoundingBoxAscent), E = Math.max(E, H.actualBoundingBoxDescent);
398
+ for (const D of d) {
399
+ const H = t.measureText(D || x);
400
+ y = Math.max(y, D ? H.width : 0), v = Math.max(v, H.actualBoundingBoxAscent), E = Math.max(E, H.actualBoundingBoxDescent);
395
401
  }
396
402
  t.textBaseline = "alphabetic";
397
403
  const S = t.measureText(p).fontBoundingBoxDescent;
398
404
  t.textBaseline = c;
399
- const _ = t.measureText(p).fontBoundingBoxDescent, P = i * h, k = v + E + (d.length - 1) * P, I = l / 2 + 1, C = Math.max(
405
+ const _ = t.measureText(p).fontBoundingBoxDescent, P = i * h, k = v + E + (d.length - 1) * P, I = Math.ceil(l / 2) + 2, C = Math.max(
400
406
  1,
401
407
  Math.ceil(y + I * 2)
402
408
  ), R = Math.max(
403
409
  1,
404
410
  Math.ceil(k + I * 2)
405
- ), w = Math.ceil(C * r), W = Math.ceil(R * r);
406
- (this.canvas.width !== w || this.canvas.height !== W) && (this.canvas.width = w, this.canvas.height = W), t.setTransform(r, 0, 0, r, 0, 0), t.clearRect(0, 0, w / r, W / r), t.font = o, t.textBaseline = "alphabetic", t.textAlign = u;
411
+ ), w = Math.ceil(C * r), O = Math.ceil(R * r);
412
+ (this.canvas.width !== w || this.canvas.height !== O) && (this.canvas.width = w, this.canvas.height = O), t.setTransform(r, 0, 0, r, 0, 0), t.clearRect(
413
+ 0,
414
+ 0,
415
+ w / r,
416
+ O / r
417
+ ), t.font = o, t.textBaseline = "alphabetic", t.textAlign = u;
407
418
  const B = this.updateOffset(C, I);
408
419
  f && (t.lineWidth = l, t.strokeStyle = e.stroke ?? "#000000", t.lineJoin = "round"), e.fill && (t.fillStyle = e.fill);
409
420
  let L = I + v;
410
- for (const F of d)
411
- f && t.strokeText(F, B, L), e.fill && t.fillText(F, B, L), L += P;
412
- this.base?.updateSource(this.render.gl, this.canvas, this.options), this.setRegion(0, 0, w, W);
413
- const O = S - _;
414
- this.offsetY = -I - v - O;
421
+ for (const D of d)
422
+ f && t.strokeText(D, B, L), e.fill && t.fillText(D, B, L), L += P;
423
+ this.base?.updateSource(this.render.gl, this.canvas, this.options), this.setRegion(0, 0, w, O);
424
+ const W = _ - S;
425
+ this.offsetY = -I - v + W;
415
426
  }
416
427
  }
417
428
  const Lt = (n, t = !1, e = !0) => {
@@ -448,8 +459,8 @@ function at(n, t) {
448
459
  }
449
460
  function ht(n, t) {
450
461
  const e = {
451
- [tt.LINEAR]: n.LINEAR,
452
- [tt.NEAREST]: n.NEAREST
462
+ [Q.LINEAR]: n.LINEAR,
463
+ [Q.NEAREST]: n.NEAREST
453
464
  }[t] ?? n.NEAREST;
454
465
  n.texParameteri(n.TEXTURE_2D, n.TEXTURE_MIN_FILTER, e), n.texParameteri(n.TEXTURE_2D, n.TEXTURE_MAG_FILTER, e);
455
466
  }
@@ -482,7 +493,7 @@ function It(n, t, e) {
482
493
  function yt(n, t, e, r, i) {
483
494
  n.drawArraysInstanced(t, e, r, i);
484
495
  }
485
- let Wt = 0;
496
+ let Ot = 0;
486
497
  class Tt {
487
498
  program;
488
499
  attributeLoc = {};
@@ -492,7 +503,7 @@ class Tt {
492
503
  uniformType = {};
493
504
  uniformIsArray = {};
494
505
  gl;
495
- shaderId = Wt++;
506
+ shaderId = Ot++;
496
507
  // for debug
497
508
  vao;
498
509
  constructor(t, e, r, i) {
@@ -512,7 +523,7 @@ class Tt {
512
523
  const r = this.uniformLoc[t];
513
524
  if (r === void 0) return;
514
525
  const i = this.uniformType[t], s = this.uniformIsArray[t];
515
- Ot(this.gl, r, i, e, s);
526
+ Wt(this.gl, r, i, e, s);
516
527
  }
517
528
  setUniforms(t) {
518
529
  for (const e in t)
@@ -556,7 +567,7 @@ class Tt {
556
567
  }
557
568
  }
558
569
  }
559
- function Ot(n, t, e, r, i = !1) {
570
+ function Wt(n, t, e, r, i = !1) {
560
571
  switch (e) {
561
572
  // scalars
562
573
  case "float":
@@ -594,10 +605,10 @@ function Ot(n, t, e, r, i = !1) {
594
605
  return n.uniformMatrix3x2fv(t, !1, r);
595
606
  // fallback: guess by value shape
596
607
  default:
597
- return Ft(n, t, r);
608
+ return Dt(n, t, r);
598
609
  }
599
610
  }
600
- function Ft(n, t, e) {
611
+ function Dt(n, t, e) {
601
612
  if (typeof e == "number")
602
613
  return n.uniform1f(t, e);
603
614
  const r = e;
@@ -987,7 +998,7 @@ void main(void) {\r
987
998
  \r
988
999
  // CUSTOM_CODE_CALL\r
989
1000
  }\r
990
- `, z = 48, Dt = new Float32Array([0, 0, 1, 0, 0, 1, 1, 1]);
1001
+ `, z = 48, Ft = new Float32Array([0, 0, 1, 0, 0, 1, 1, 1]);
991
1002
  class lt extends Et {
992
1003
  instanceBuffer;
993
1004
  quadBuffer;
@@ -1001,7 +1012,7 @@ class lt extends Et {
1001
1012
  createBuffer() {
1002
1013
  const t = this.gl;
1003
1014
  this.quadBuffer = new ut(t, A.Float32, t.ARRAY_BUFFER, t.STATIC_DRAW), this.quadBuffer.bindBuffer();
1004
- for (const e of Dt) this.quadBuffer.pushFloat32(e);
1015
+ for (const e of Ft) this.quadBuffer.pushFloat32(e);
1005
1016
  this.quadBuffer.makeDirty(), this.quadBuffer.bufferData(), this.instanceBuffer = new ut(t, A.Uint32, t.ARRAY_BUFFER, t.DYNAMIC_DRAW);
1006
1017
  }
1007
1018
  createDefaultShader() {
@@ -1048,10 +1059,10 @@ class lt extends Et {
1048
1059
  x / E.height
1049
1060
  ), C = this.instanceBuffer, R = C.usedElemNum;
1050
1061
  C.resize(12);
1051
- const w = C.float32, W = C.uint32, B = v ? _ : S, G = v ? S : _, L = B + 2 * d, O = G + 2 * x, F = P - d, H = k - x, M = this.localSpriteMatrix;
1052
- v ? (M[0] = 0, M[1] = y ? O : -O, M[2] = p ? -L : L, M[3] = 0, M[4] = F + (p ? L : 0), M[5] = H + (y ? 0 : O)) : (M[0] = p ? -L : L, M[1] = 0, M[2] = 0, M[3] = y ? -O : O, M[4] = F + (p ? L : 0), M[5] = H + (y ? O : 0));
1062
+ const w = C.float32, O = C.uint32, B = v ? _ : S, G = v ? S : _, L = B + 2 * d, W = G + 2 * x, D = P - d, H = k - x, M = this.localSpriteMatrix;
1063
+ v ? (M[0] = 0, M[1] = y ? W : -W, M[2] = p ? -L : L, M[3] = 0, M[4] = D + (p ? L : 0), M[5] = H + (y ? 0 : W)) : (M[0] = p ? -L : L, M[1] = 0, M[2] = 0, M[3] = y ? -W : W, M[4] = D + (p ? L : 0), M[5] = H + (y ? W : 0));
1053
1064
  const U = this.worldSpriteMatrix;
1054
- U[0] = e * M[0] + i * M[1], U[1] = r * M[0] + s * M[1], U[2] = e * M[2] + i * M[3], U[3] = r * M[2] + s * M[3], U[4] = a + e * M[4] + i * M[5], U[5] = h + r * M[4] + s * M[5], w[R + 6] = o, w[R + 7] = c, w[R + 8] = u, w[R + 9] = l, W[R + 10] = f, W[R + 11] = I, this.rapid.roundPixels && (U[4] = Math.round(U[4]), U[5] = Math.round(U[5])), w[R] = U[0], w[R + 1] = U[2], w[R + 2] = U[4], w[R + 3] = U[1], w[R + 4] = U[3], w[R + 5] = U[5], C.usedElemNum += 12, C.makeDirty(), this.instanceCount++;
1065
+ U[0] = e * M[0] + i * M[1], U[1] = r * M[0] + s * M[1], U[2] = e * M[2] + i * M[3], U[3] = r * M[2] + s * M[3], U[4] = a + e * M[4] + i * M[5], U[5] = h + r * M[4] + s * M[5], w[R + 6] = o, w[R + 7] = c, w[R + 8] = u, w[R + 9] = l, O[R + 10] = f, O[R + 11] = I, this.rapid.roundPixels && (U[4] = Math.round(U[4]), U[5] = Math.round(U[5])), w[R] = U[0], w[R + 1] = U[2], w[R + 2] = U[4], w[R + 3] = U[1], w[R + 4] = U[3], w[R + 5] = U[5], C.usedElemNum += 12, C.makeDirty(), this.instanceCount++;
1055
1066
  }
1056
1067
  render() {
1057
1068
  if (this.instanceCount === 0) return;
@@ -1721,8 +1732,8 @@ const nt = 10, mt = (n, t, e, r, i, s) => {
1721
1732
  const d = t[f], x = e[f].normal, p = e[f].miters, y = (f + 1) % t.length, v = t[y], E = e[y].normal, S = e[y].miters, _ = d.add(x.multiply(p * i)), P = d.subtract(x.multiply(p * i)), k = v.add(E.multiply(S * i)), I = v.subtract(E.multiply(S * i)), C = d.distanceTo(v);
1722
1733
  let R = 0, w = 0;
1723
1734
  o === 0 ? (R = c / r, w = (c + C) / r) : (R = c / u, w = R + C / u);
1724
- const W = new g(R, 0), B = new g(R, 1), G = new g(w, 0), L = new g(w, 1);
1725
- s.push(_), a.push(W), s.push(P), a.push(B), s.push(k), a.push(G), s.push(k), a.push(G), s.push(I), a.push(L), s.push(P), a.push(B), c += C;
1735
+ const O = new g(R, 0), B = new g(R, 1), G = new g(w, 0), L = new g(w, 1);
1736
+ s.push(_), a.push(O), s.push(P), a.push(B), s.push(k), a.push(G), s.push(k), a.push(G), s.push(I), a.push(L), s.push(P), a.push(B), c += C;
1726
1737
  }
1727
1738
  if (h && !n.closed) {
1728
1739
  const f = t[0], d = e[0].normal, x = o === 0 ? r > 0 ? 1 / r : 0 : 1 / u, p = mt(f, d, i, !0, 0, x);
@@ -1731,7 +1742,7 @@ const nt = 10, mt = (n, t, e, r, i, s) => {
1731
1742
  s.push(..._.vertices), a.push(..._.uv);
1732
1743
  }
1733
1744
  return { vertices: s, uv: a };
1734
- }, Q = (n, t, e, r) => {
1745
+ }, Z = (n, t, e, r) => {
1735
1746
  if (t.customMatrix !== void 0)
1736
1747
  return t.customMatrix;
1737
1748
  if (t.modifyStack) {
@@ -1751,7 +1762,7 @@ const nt = 10, mt = (n, t, e, r, i, s) => {
1751
1762
  ), i;
1752
1763
  }
1753
1764
  }, Qt = (n, t) => t ? n ? t.premultipliedUint32 : t.uint32 : 4294967295, Zt = 2, Jt = (n, t) => {
1754
- const e = Q(n, t, t.texture.rawWidth, t.texture.rawHeight), r = t.texture;
1765
+ const e = Z(n, t, t.texture.rawWidth, t.texture.rawHeight), r = t.texture;
1755
1766
  if (!r?.base || n.inCreateMask)
1756
1767
  return;
1757
1768
  const i = r.isAtlas ? n.atlasSpriteRegion : n.spriteRegion;
@@ -1803,7 +1814,7 @@ const nt = 10, mt = (n, t, e, r, i, s) => {
1803
1814
  );
1804
1815
  }, Mt = (n, t) => {
1805
1816
  if (t.points.length === 0) return;
1806
- const e = Q(n, t, 0, 0);
1817
+ const e = Z(n, t, 0, 0);
1807
1818
  n.startGraphic(
1808
1819
  t.drawMode ?? n.gl.TRIANGLES,
1809
1820
  t.texture,
@@ -1832,7 +1843,7 @@ const nt = 10, mt = (n, t, e, r, i, s) => {
1832
1843
  drawMode: n.gl.TRIANGLES
1833
1844
  });
1834
1845
  }, re = (n, t) => {
1835
- const e = Q(
1846
+ const e = Z(
1836
1847
  n,
1837
1848
  t,
1838
1849
  t.texture.rawWidth,
@@ -1844,7 +1855,7 @@ const nt = 10, mt = (n, t, e, r, i, s) => {
1844
1855
  e ?? t.customMatrix
1845
1856
  ), n.addRectVertex(t.texture.rawWidth, t.texture.rawHeight), n.endGraphic();
1846
1857
  }, ie = (n, t) => {
1847
- const e = Q(n, t, t.width, t.height);
1858
+ const e = Z(n, t, t.width, t.height);
1848
1859
  n.startGraphic(
1849
1860
  n.gl.TRIANGLE_FAN,
1850
1861
  t.texture,
@@ -1852,7 +1863,7 @@ const nt = 10, mt = (n, t, e, r, i, s) => {
1852
1863
  e ?? t.customMatrix
1853
1864
  ), n.addRectVertex(t.width, t.height, t.color), n.endGraphic();
1854
1865
  }, se = (n, t) => {
1855
- const e = t.segments ?? 32, r = Q(n, t, 0, 0);
1866
+ const e = t.segments ?? 32, r = Z(n, t, 0, 0);
1856
1867
  n.startGraphic(
1857
1868
  n.gl.TRIANGLE_FAN,
1858
1869
  void 0,
@@ -2001,11 +2012,11 @@ class ce extends lt {
2001
2012
  drawParticles(t, e, r, i = 0, s = 4294967295, a, h = 1, o = 1, c = 0, u = 0, l = 1, f = 1, d = !1, x = !1, p = !1, y = 0.5, v = 0.5, E, S = !1, _) {
2002
2013
  if (!t.glTexture || a <= 0) return;
2003
2014
  this.texture && t !== this.texture && this.flush();
2004
- const P = typeof h == "number", k = typeof o == "number", I = typeof i == "number", C = typeof s == "number", R = typeof c == "number", w = !C && typeof s[0] == "number", W = t.uvW - t.uvX, B = t.uvH - t.uvY, G = W === 0 ? 0 : 1 / W, L = B === 0 ? 0 : 1 / B;
2005
- let O = t.rawWidth * (d ? -1 : 1);
2006
- P && (O *= h);
2007
- let F = t.rawHeight * (x ? -1 : 1);
2008
- k && (F *= o), R && (O *= (l - c) * G, F *= (f - u) * L);
2015
+ const P = typeof h == "number", k = typeof o == "number", I = typeof i == "number", C = typeof s == "number", R = typeof c == "number", w = !C && typeof s[0] == "number", O = t.uvW - t.uvX, B = t.uvH - t.uvY, G = O === 0 ? 0 : 1 / O, L = B === 0 ? 0 : 1 / B;
2016
+ let W = t.rawWidth * (d ? -1 : 1);
2017
+ P && (W *= h);
2018
+ let D = t.rawHeight * (x ? -1 : 1);
2019
+ k && (D *= o), R && (W *= (l - c) * G, D *= (f - u) * L);
2009
2020
  let H = p ? Math.PI / 2 : 0;
2010
2021
  I && (H += i);
2011
2022
  const M = s ?? 4294967295, U = this.getParticleLoop(
@@ -2036,8 +2047,8 @@ class ce extends lt {
2036
2047
  s,
2037
2048
  At,
2038
2049
  St,
2039
- O,
2040
- F,
2050
+ W,
2051
+ D,
2041
2052
  G,
2042
2053
  L,
2043
2054
  H,
@@ -2136,7 +2147,7 @@ class pe {
2136
2147
  textureFilter;
2137
2148
  scaleMode;
2138
2149
  _filterRT = [null, null];
2139
- _filterInput = new D();
2150
+ _filterInput = new F();
2140
2151
  get height() {
2141
2152
  return this.logicHeight;
2142
2153
  }
@@ -2144,7 +2155,7 @@ class pe {
2144
2155
  return this.logicWidth;
2145
2156
  }
2146
2157
  constructor(t) {
2147
- this.canvas = t.canvas, this.dpr = window.devicePixelRatio || 1, this.antialias = t.antialias ?? !1, this.textureFilter = t.textureFilter ?? tt.NEAREST, this.premultipliedAlpha = t.premultipliedAlpha ?? !0, this.roundPixels = t.roundPixels ?? !1, this.scaleMode = t.scaleMode ?? 0;
2158
+ 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, this.scaleMode = t.scaleMode ?? 0;
2148
2159
  const e = Lt(this.canvas, this.antialias, this.premultipliedAlpha);
2149
2160
  this.gl = e, this.maxTextureUnits = e.getParameter(e.MAX_TEXTURE_IMAGE_UNITS), this.matrix = this.matrixStack.matrix, this.spriteRegion = new lt(this), this.graphicRegion = new Yt(this), this.atlasSpriteRegion = new ne(this), this.particleRegion = new ce(this);
2150
2161
  const r = this.canvas.clientWidth || this.canvas.width, i = this.canvas.clientHeight || this.canvas.height;
@@ -2448,7 +2459,7 @@ class N {
2448
2459
  return typeof t == "number" ? t : t.clone();
2449
2460
  }
2450
2461
  }
2451
- const Z = (n, t) => (Array.isArray(n) ? n : [n]).slice(0, t);
2462
+ const J = (n, t) => (Array.isArray(n) ? n : [n]).slice(0, t);
2452
2463
  var fe = /* @__PURE__ */ ((n) => (n.POINT = "point", n.CIRCLE = "circle", n.RECT = "rect", n))(fe || {});
2453
2464
  const gt = 10, pt = 0, vt = !0;
2454
2465
  class j {
@@ -2717,7 +2728,7 @@ void light_fragment(inout vec4 color, in vec2 vRegion) {
2717
2728
 
2718
2729
  color.rgb = color.rgb * (uAmbientColor + totalDiffuse) + totalSpecular;
2719
2730
  }
2720
- `, xe = "void light_vertex(inout vec4 p, vec2 u) {}", J = 8;
2731
+ `, xe = "void light_vertex(inout vec4 p, vec2 u) {}", tt = 8;
2721
2732
  class ge extends ct {
2722
2733
  constructor(t, e = "", r = "", i = 0, s) {
2723
2734
  super(
@@ -2767,7 +2778,7 @@ class ve {
2767
2778
  }), this.rt;
2768
2779
  }
2769
2780
  setupLight(t) {
2770
- const e = t.customShader ?? this.shader, r = Z(t.lightMatrix, J), i = Z(t.color, J), s = Z(t.lightHeight, J), a = Z(t.lightTexture, J);
2781
+ const e = t.customShader ?? this.shader, r = J(t.lightMatrix, tt), i = J(t.color, tt), s = J(t.lightHeight, tt), a = J(t.lightTexture, tt);
2771
2782
  return e.setUniforms({
2772
2783
  uResolution: [this.rapid.physicsWidth, this.rapid.physicsHeight],
2773
2784
  uLogicalResolution: [this.rapid.logicWidth, this.rapid.logicHeight],
@@ -2799,15 +2810,15 @@ export {
2799
2810
  ve as Light,
2800
2811
  ge as LightShader,
2801
2812
  jt as LineTextureMode,
2802
- J as MAX_LIGHTS,
2813
+ tt as MAX_LIGHTS,
2803
2814
  Rt as MaskType,
2804
2815
  wt as ParticleEmitter,
2805
2816
  fe as ParticleShape,
2806
2817
  pe as Rapid,
2807
2818
  Ut as RenderTexture,
2808
2819
  _t as TextTexture,
2809
- D as Texture,
2810
- tt as TextureFilterMode,
2820
+ F as Texture,
2821
+ Q as TextureFilterMode,
2811
2822
  bt as TextureManager,
2812
2823
  q as TextureWrapMode,
2813
2824
  g as Vec2,
@@ -1,5 +1,5 @@
1
- (function(p,f){typeof exports=="object"&&typeof module<"u"?f(exports):typeof define=="function"&&define.amd?define(["exports"],f):(p=typeof globalThis<"u"?globalThis:p||self,f(p.RapidRender={}))})(this,(function(p){"use strict";class f{_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){t.clearColor(this.r/255,this.g/255,this.b/255,this.a/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=f.clampByte(this._a),e=f.clampByte(this._r*t/255),r=f.clampByte(this._g*t/255),i=f.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 h=0,o=0,c=0;return t<60?[h,o,c]=[i,s,0]:t<120?[h,o,c]=[s,i,0]:t<180?[h,o,c]=[0,i,s]:t<240?[h,o,c]=[0,s,i]:t<300?[h,o,c]=[s,0,i]:[h,o,c]=[i,0,s],this.setRGBA((h+a)*255,(o+a)*255,(c+a)*255,255),this}copy(t){this.setRGBA(t.r,t.g,t.b,t.a)}clone(){return new f(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 f(0,0,0).setHSL(t,e,r)}static FromRGB(t,e,r){return new f(t,e,r)}static fromNorm(t,e,r,i=1){return new f(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=f.clampByte(i),h=s?a/255:1,o=f.clampByte(t*h),c=f.clampByte(e*h),u=f.clampByte(r*h);return(a<<24|u<<16|c<<8|o)>>>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 f(e,r,i,s)}add(t){return new f(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 f(this.r-t.r,this.g-t.g,this.b-t.b,this.a-t.a)}divide(t){return t instanceof f?new f(this.r/t.r,this.g/t.g,this.b/t.b,this.a/t.a):new f(this.r/t,this.g/t,this.b/t,this.a/t)}multiply(t){return t instanceof f?new f(this.r*t.r,this.g*t.g,this.b*t.b,this.a*t.a):new f(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 f(255,0,0,255);static Green=new f(0,255,0,255);static Blue=new f(0,0,255,255);static Yellow=new f(255,255,0,255);static Purple=new f(128,0,128,255);static Orange=new f(255,165,0,255);static Pink=new f(255,192,203,255);static Gray=new f(128,128,128,255);static Brown=new f(139,69,19,255);static Cyan=new f(0,255,255,255);static Magenta=new f(255,0,255,255);static Lime=new f(192,255,0,255);static White=new f(255,255,255,255);static Black=new f(0,0,0,255);static Transparent=new f(0,0,0,0)}var j=(n=>(n[n.LINEAR=0]="LINEAR",n[n.NEAREST=1]="NEAREST",n))(j||{}),z=(n=>(n[n.REPEAT=0]="REPEAT",n[n.CLAMP=1]="CLAMP",n[n.MIRRORED_REPEAT=2]="MIRRORED_REPEAT",n))(z||{});class pt{render;cache=new Map;constructor(t){this.render=t}async load(t,e){let r=this.cache.get(t);if(r)return new I(r);try{const i=await this._fetchImage(t);return r=V.fromSource(this.render,i,e),r.uid=t,this.cache.set(t,r),new I(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 new I(this.cache.get(e.key));const r=V.fromSource(this.render,t,e);return e?.key&&(r.uid=e.key,this.cache.set(e.key,r)),new I(r)}createRenderTexture(t){return new vt(this.render,t)}createTextTexture(t){return new yt(this.render,t)}destroy(t,e=!1){let r,i;typeof t=="string"?(i=t,r=this.cache.get(i)):t instanceof I?(r=t.base,i=r?.uid,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 V{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=et(t,e,r);return new V(i,e.width,e.height)}updateSource(t,e,r={}){this.glTexture&&(t.bindTexture(t.TEXTURE_2D,this.glTexture),r.textureFilter!==void 0&&at(t,r.textureFilter),r.wrap!==void 0&&nt(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),at(t,e),t.bindTexture(t.TEXTURE_2D,null))}setWrapMode(t,e){this.glTexture&&(t.bindTexture(t.TEXTURE_2D,this.glTexture),nt(t,e),t.bindTexture(t.TEXTURE_2D,null))}destroy(t){this.glTexture&&(t.deleteTexture(this.glTexture),this.glTexture=null)}}class I{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)}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 I;const s=this.clone(),a=this._px,h=this._py;return s.setRegion(a+t,h+e,r,i),s}clone(t){const e=t??new I(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=[],h=this.rawWidth,o=this.rawHeight,c=Math.max(0,s),u=t+c,l=e+c,m=r??Math.floor((h+c)/u),d=i??Math.floor((o+c)/l);for(let x=0;x<d;x++)for(let v=0;v<m;v++)a.push(this.getSubTexture(v*u,x*l,t,e));return a}static fromImageSource(t,e,r={}){const i=et(t,e,r);return new I(new V(i,e.width,e.height))}destroy(){this.base&&(this.base.refCount--,this.base=void 0),this.glTexture=null}}class vt extends I{framebuffer;renderbuffer;gl;clearColor;_allocW=0;_allocH=0;constructor(t,e){super(),this.gl=t.gl,this.clearColor=e.clearColor??f.Black;const r=Math.max(Math.round(e.width),1),i=Math.max(Math.round(e.height),1),a=et(t,{width:r,height:i},{...e,onlySize:!0});if(this.setBase(new V(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),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),h=Math.floor(r),o=Math.floor(i);if(h<=0||o<=0)return new Uint8Array(0);if(s<0||a<0||s>=this.rawWidth||a>=this.rawHeight)return new Uint8Array(0);const c=Math.min(h,this.rawWidth-s),u=Math.min(o,this.rawHeight-a),l=this.gl,m=l.getParameter(l.FRAMEBUFFER_BINDING);l.bindFramebuffer(l.FRAMEBUFFER,this.framebuffer);const d=new Uint8Array(c*u*4);return l.readPixels(s,a,c,u,l.RGBA,l.UNSIGNED_BYTE,d),l.bindFramebuffer(l.FRAMEBUFFER,m),d}GetColorAt(t,e){const r=this.GetPixels(t,e,1,1);return r.length<4?new f(0,0,0,0):new f(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 Yt={fontFamily:"Arial",fontSize:24,fontWeight:"normal",fill:"#000000",strokeThickness:0,align:"left",lineHeight:1};class yt extends I{canvas;ctx;render;_text;_style;options;flipY=!0;constructor(t,e){super(),this.render=t,this._style={...Yt,...e},this._text=e?.text??"",this.options={premultipliedAlpha:t.premultipliedAlpha,...e},this.scale=1/t.dpr,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=et(t,this.canvas,this.options);this.setBase(new V(i,1,1)),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}}update(){const t=this.ctx,e=this.style,r=this.render.dpr,i=e.fontSize,s=e.fontWeight,a=e.fontFamily,h=e.lineHeight,o=`${s} ${i}px ${a}`,c=e.baseline??"middle",u=e.align??"left",l=e.strokeThickness??0,m=l>0,d=this._text.split(`
2
- `),x="国g",v=d[0]||x;t.font=o,t.textBaseline="alphabetic",t.textAlign="left";let T=0,y=0,R=0;for(const B of d){const G=t.measureText(B||x);T=Math.max(T,B?G.width:0),y=Math.max(y,G.actualBoundingBoxAscent),R=Math.max(R,G.actualBoundingBoxDescent)}t.textBaseline="alphabetic";const U=t.measureText(v).fontBoundingBoxDescent;t.textBaseline=c;const L=t.measureText(v).fontBoundingBoxDescent,N=i*h,H=y+R+(d.length-1)*N,O=l/2+1,_=Math.max(1,Math.ceil(T+O*2)),w=Math.max(1,Math.ceil(H+O*2)),S=Math.ceil(_*r),F=Math.ceil(w*r);(this.canvas.width!==S||this.canvas.height!==F)&&(this.canvas.width=S,this.canvas.height=F),t.setTransform(r,0,0,r,0,0),t.clearRect(0,0,S/r,F/r),t.font=o,t.textBaseline="alphabetic",t.textAlign=u;const k=this.updateOffset(_,O);m&&(t.lineWidth=l,t.strokeStyle=e.stroke??"#000000",t.lineJoin="round"),e.fill&&(t.fillStyle=e.fill);let P=O+y;for(const B of d)m&&t.strokeText(B,k,P),e.fill&&t.fillText(B,k,P),P+=N;this.base?.updateSource(this.render.gl,this.canvas,this.options),this.setRegion(0,0,S,F);const D=U-L;this.offsetY=-O-y-D}}const zt=(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},Tt=(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},$t=(n,t,e)=>{var r=n.createProgram(),i=Tt(n,t,35633),s=Tt(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 h=n.getProgramInfoLog(r);throw new Error("Unable to link shader program: "+h)}return r};function nt(n,t){const e={[z.CLAMP]:n.CLAMP_TO_EDGE,[z.REPEAT]:n.REPEAT,[z.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 at(n,t){const e={[j.LINEAR]:n.LINEAR,[j.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 et(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),nt(r,e.wrap??z.CLAMP),at(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 K(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 jt=5126,ht=5121;function Kt(n,t,e){n.vertexAttribDivisor(t,e)}function Mt(n,t,e,r,i){n.drawArraysInstanced(t,e,r,i)}let qt=0;class ot{program;attributeLoc={};uniformLoc={};isCustom=!1;padding=0;uniformType={};uniformIsArray={};gl;shaderId=qt++;vao;constructor(t,e,r,i){this.gl=t,this.program=$t(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];Qt(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??jt,t.normalized??!1,t.stride,t.offset??0),r.enableVertexAttribArray(e),Kt(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 h=s.split(/\s+/)[2],o=e.getAttribLocation(this.program,h);o!==-1&&(this.attributeLoc[h]=o)}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],h=s[2],o=e.getUniformLocation(this.program,h);o!==null&&(this.uniformLoc[h]=o,this.uniformType[h]=a,this.uniformIsArray[h]=s[3]!==void 0)}}}function Qt(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 Zt(n,t,r)}}function Zt(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 rt{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}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(h=>h+"vertex(position, vRegion);").join("")),r=this.replaceCustomCode(r,this.vs),i=i.replace("// CUSTOM_CODE_CALL",s.map(h=>h+"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=K(e,r),t=K(t,r),new ot(this.gl,t,e)}createCustomShader(t){const e=this.getMaxTexutreUnit(t),r=K(this.fs,e),i=K(this.vs,e);return t.getGLShader(this,this.KEY,i,r)}getCustomShader(t){if(!t)return this.defaultShader;if(t instanceof rt){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 rt?(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(h=>this.useTexture(h));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 A=(n=>(n[n.Float32=0]="Float32",n[n.Uint32=1]="Uint32",n[n.Uint16=2]="Uint16",n))(A||{});class b{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(h=>h.usedElemNum)),i=[...new Set(t)].filter(h=>h>=0&&h<r).sort((h,o)=>h-o);if(i.length===0)return 0;let s=0,a=0;for(let h=0;h<r;h++){if(a<i.length&&h===i[a]){a++;continue}if(s!==h)for(const o of e)o.typedArray[s]=o.typedArray[h];s++}for(const h of e)h.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 ct extends b{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 it=`#version 300 es\r
1
+ (function(p,f){typeof exports=="object"&&typeof module<"u"?f(exports):typeof define=="function"&&define.amd?define(["exports"],f):(p=typeof globalThis<"u"?globalThis:p||self,f(p.RapidRender={}))})(this,(function(p){"use strict";class f{_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){t.clearColor(this.r/255,this.g/255,this.b/255,this.a/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=f.clampByte(this._a),e=f.clampByte(this._r*t/255),r=f.clampByte(this._g*t/255),i=f.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 h=0,o=0,c=0;return t<60?[h,o,c]=[i,s,0]:t<120?[h,o,c]=[s,i,0]:t<180?[h,o,c]=[0,i,s]:t<240?[h,o,c]=[0,s,i]:t<300?[h,o,c]=[s,0,i]:[h,o,c]=[i,0,s],this.setRGBA((h+a)*255,(o+a)*255,(c+a)*255,255),this}copy(t){this.setRGBA(t.r,t.g,t.b,t.a)}clone(){return new f(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 f(0,0,0).setHSL(t,e,r)}static FromRGB(t,e,r){return new f(t,e,r)}static fromNorm(t,e,r,i=1){return new f(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=f.clampByte(i),h=s?a/255:1,o=f.clampByte(t*h),c=f.clampByte(e*h),u=f.clampByte(r*h);return(a<<24|u<<16|c<<8|o)>>>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 f(e,r,i,s)}add(t){return new f(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 f(this.r-t.r,this.g-t.g,this.b-t.b,this.a-t.a)}divide(t){return t instanceof f?new f(this.r/t.r,this.g/t.g,this.b/t.b,this.a/t.a):new f(this.r/t,this.g/t,this.b/t,this.a/t)}multiply(t){return t instanceof f?new f(this.r*t.r,this.g*t.g,this.b*t.b,this.a*t.a):new f(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 f(255,0,0,255);static Green=new f(0,255,0,255);static Blue=new f(0,0,255,255);static Yellow=new f(255,255,0,255);static Purple=new f(128,0,128,255);static Orange=new f(255,165,0,255);static Pink=new f(255,192,203,255);static Gray=new f(128,128,128,255);static Brown=new f(139,69,19,255);static Cyan=new f(0,255,255,255);static Magenta=new f(255,0,255,255);static Lime=new f(192,255,0,255);static White=new f(255,255,255,255);static Black=new f(0,0,0,255);static Transparent=new f(0,0,0,0)}var z=(n=>(n[n.LINEAR=0]="LINEAR",n[n.NEAREST=1]="NEAREST",n))(z||{}),$=(n=>(n[n.REPEAT=0]="REPEAT",n[n.CLAMP=1]="CLAMP",n[n.MIRRORED_REPEAT=2]="MIRRORED_REPEAT",n))($||{});class pt{render;cache=new Map;constructor(t){this.render=t}async load(t,e){let r=this.cache.get(t);if(r)return new I(r);try{const i=await this._fetchImage(t);return r=V.fromSource(this.render,i,e),r.uid=t,this.cache.set(t,r),new I(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 new I(this.cache.get(e.key));const r=V.fromSource(this.render,t,e);return e?.key&&(r.uid=e.key,this.cache.set(e.key,r)),new I(r)}createRenderTexture(t){return new vt(this.render,t)}createTextTexture(t){return new yt(this.render,t)}destroy(t,e=!1){let r,i;typeof t=="string"?(i=t,r=this.cache.get(i)):t instanceof I?(r=t.base,i=r?.uid,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 V{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=et(t,e,r);return new V(i,e.width,e.height)}updateSource(t,e,r={}){this.glTexture&&(t.bindTexture(t.TEXTURE_2D,this.glTexture),r.textureFilter!==void 0&&at(t,r.textureFilter),r.wrap!==void 0&&nt(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),at(t,e),t.bindTexture(t.TEXTURE_2D,null))}setWrapMode(t,e){this.glTexture&&(t.bindTexture(t.TEXTURE_2D,this.glTexture),nt(t,e),t.bindTexture(t.TEXTURE_2D,null))}destroy(t){this.glTexture&&(t.deleteTexture(this.glTexture),this.glTexture=null)}}class I{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)}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 I;const s=this.clone(),a=this._px,h=this._py;return s.setRegion(a+t,h+e,r,i),s}clone(t){const e=t??new I(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=[],h=this.rawWidth,o=this.rawHeight,c=Math.max(0,s),u=t+c,l=e+c,m=r??Math.floor((h+c)/u),d=i??Math.floor((o+c)/l);for(let x=0;x<d;x++)for(let v=0;v<m;v++)a.push(this.getSubTexture(v*u,x*l,t,e));return a}static fromImageSource(t,e,r={}){const i=et(t,e,r);return new I(new V(i,e.width,e.height))}destroy(){this.base&&(this.base.refCount--,this.base=void 0),this.glTexture=null}}class vt extends I{framebuffer;renderbuffer;gl;clearColor;_allocW=0;_allocH=0;constructor(t,e){super(),this.gl=t.gl,this.clearColor=e.clearColor??f.Black;const r=Math.max(Math.round(e.width),1),i=Math.max(Math.round(e.height),1),a=et(t,{width:r,height:i},{...e,onlySize:!0});if(this.setBase(new V(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),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),h=Math.floor(r),o=Math.floor(i);if(h<=0||o<=0)return new Uint8Array(0);if(s<0||a<0||s>=this.rawWidth||a>=this.rawHeight)return new Uint8Array(0);const c=Math.min(h,this.rawWidth-s),u=Math.min(o,this.rawHeight-a),l=this.gl,m=l.getParameter(l.FRAMEBUFFER_BINDING);l.bindFramebuffer(l.FRAMEBUFFER,this.framebuffer);const d=new Uint8Array(c*u*4);return l.readPixels(s,a,c,u,l.RGBA,l.UNSIGNED_BYTE,d),l.bindFramebuffer(l.FRAMEBUFFER,m),d}GetColorAt(t,e){const r=this.GetPixels(t,e,1,1);return r.length<4?new f(0,0,0,0):new f(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 Yt={fontFamily:"Arial",fontSize:24,fontWeight:"normal",fill:"#000000",strokeThickness:0,align:"left",lineHeight:1};class yt extends I{canvas;ctx;render;_text;_style;options;flipY=!0;constructor(t,e){super(),this.render=t,this._style={...Yt,...e},this._text=e?.text??"",this.options={premultipliedAlpha:t.premultipliedAlpha,textureFilter:z.LINEAR,...e},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=et(t,this.canvas,this.options);this.setBase(new V(i,1,1)),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}}update(){const t=this.ctx,e=this.style,r=this.render.dpr;this.scale=1/r;const i=e.fontSize,s=e.fontWeight,a=e.fontFamily,h=e.lineHeight,o=`${s} ${i}px ${a}`,c=e.baseline??"top",u=e.align??"left",l=e.strokeThickness??0,m=l>0,d=this._text.split(`
2
+ `),x="国g",v=d[0]||x;t.font=o,t.textBaseline="alphabetic",t.textAlign="left";let T=0,y=0,R=0;for(const B of d){const G=t.measureText(B||x);T=Math.max(T,B?G.width:0),y=Math.max(y,G.actualBoundingBoxAscent),R=Math.max(R,G.actualBoundingBoxDescent)}t.textBaseline="alphabetic";const U=t.measureText(v).fontBoundingBoxDescent;t.textBaseline=c;const L=t.measureText(v).fontBoundingBoxDescent,N=i*h,H=y+R+(d.length-1)*N,O=Math.ceil(l/2)+2,_=Math.max(1,Math.ceil(T+O*2)),w=Math.max(1,Math.ceil(H+O*2)),S=Math.ceil(_*r),F=Math.ceil(w*r);(this.canvas.width!==S||this.canvas.height!==F)&&(this.canvas.width=S,this.canvas.height=F),t.setTransform(r,0,0,r,0,0),t.clearRect(0,0,S/r,F/r),t.font=o,t.textBaseline="alphabetic",t.textAlign=u;const k=this.updateOffset(_,O);m&&(t.lineWidth=l,t.strokeStyle=e.stroke??"#000000",t.lineJoin="round"),e.fill&&(t.fillStyle=e.fill);let P=O+y;for(const B of d)m&&t.strokeText(B,k,P),e.fill&&t.fillText(B,k,P),P+=N;this.base?.updateSource(this.render.gl,this.canvas,this.options),this.setRegion(0,0,S,F);const D=L-U;this.offsetY=-O-y+D}}const zt=(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},Tt=(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},$t=(n,t,e)=>{var r=n.createProgram(),i=Tt(n,t,35633),s=Tt(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 h=n.getProgramInfoLog(r);throw new Error("Unable to link shader program: "+h)}return r};function nt(n,t){const e={[$.CLAMP]:n.CLAMP_TO_EDGE,[$.REPEAT]:n.REPEAT,[$.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 at(n,t){const e={[z.LINEAR]:n.LINEAR,[z.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 et(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),nt(r,e.wrap??$.CLAMP),at(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 K(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 jt=5126,ht=5121;function Kt(n,t,e){n.vertexAttribDivisor(t,e)}function Mt(n,t,e,r,i){n.drawArraysInstanced(t,e,r,i)}let qt=0;class ot{program;attributeLoc={};uniformLoc={};isCustom=!1;padding=0;uniformType={};uniformIsArray={};gl;shaderId=qt++;vao;constructor(t,e,r,i){this.gl=t,this.program=$t(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];Qt(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??jt,t.normalized??!1,t.stride,t.offset??0),r.enableVertexAttribArray(e),Kt(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 h=s.split(/\s+/)[2],o=e.getAttribLocation(this.program,h);o!==-1&&(this.attributeLoc[h]=o)}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],h=s[2],o=e.getUniformLocation(this.program,h);o!==null&&(this.uniformLoc[h]=o,this.uniformType[h]=a,this.uniformIsArray[h]=s[3]!==void 0)}}}function Qt(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 Zt(n,t,r)}}function Zt(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 rt{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}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(h=>h+"vertex(position, vRegion);").join("")),r=this.replaceCustomCode(r,this.vs),i=i.replace("// CUSTOM_CODE_CALL",s.map(h=>h+"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=K(e,r),t=K(t,r),new ot(this.gl,t,e)}createCustomShader(t){const e=this.getMaxTexutreUnit(t),r=K(this.fs,e),i=K(this.vs,e);return t.getGLShader(this,this.KEY,i,r)}getCustomShader(t){if(!t)return this.defaultShader;if(t instanceof rt){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 rt?(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(h=>this.useTexture(h));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 A=(n=>(n[n.Float32=0]="Float32",n[n.Uint32=1]="Uint32",n[n.Uint16=2]="Uint16",n))(A||{});class b{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(h=>h.usedElemNum)),i=[...new Set(t)].filter(h=>h>=0&&h<r).sort((h,o)=>h-o);if(i.length===0)return 0;let s=0,a=0;for(let h=0;h<r;h++){if(a<i.length&&h===i[a]){a++;continue}if(s!==h)for(const o of e)o.typedArray[s]=o.typedArray[h];s++}for(const h of e)h.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 ct extends b{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 it=`#version 300 es\r
3
3
  precision highp float;\r
4
4
  \r
5
5
  // per-vertex\r
@@ -302,7 +302,7 @@ void main(void) {\r
302
302
  \r
303
303
  // CUSTOM_CODE_CALL\r
304
304
  }\r
305
- `,Y=48,le=Y/4;class de extends ut{KEY="ParticleSprite";texture;customMatrix;particleLoopCache=new Map;extraTextureUnit=1;constructor(t){super(t)}createDefaultShader(){const t=ue,e=ce;return this.vs=e,this.fs=t,this.defaultShader=this.createShader(e,t),this.defaultShader}enter(t){super.enter(t),this.currentShader.setUniform("u_projection",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:ht,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,h=1,o=1,c=0,u=0,l=1,m=1,d=!1,x=!1,v=!1,T=.5,y=.5,R,U=!1,L){if(!t.glTexture||a<=0)return;this.texture&&t!==this.texture&&this.flush();const N=typeof h=="number",H=typeof o=="number",O=typeof i=="number",_=typeof s=="number",w=typeof c=="number",S=!_&&typeof s[0]=="number",F=t.uvW-t.uvX,k=t.uvH-t.uvY,X=F===0?0:1/F,P=k===0?0:1/k;let D=t.rawWidth*(d?-1:1);N&&(D*=h);let B=t.rawHeight*(x?-1:1);H&&(B*=o),w&&(D*=(l-c)*X,B*=(m-u)*P);let G=v?Math.PI/2:0;O&&(G+=i);const E=s??4294967295,C=this.getParticleLoop(N,H,O,w,_,S,U,R);this.texture||(this.texture=t,this.useTexture(t.glTexture,0,0));const xt=a,$=this.instanceBuffer;let gt=$.usedElemNum;$.resize(xt*le);const ve=$.float32,ye=$.uint32;gt=C(e,r,h,o,i,c,u,l,m,s,ve,ye,D,B,X,P,G,E,T,y,a,xt,gt),$.usedElemNum=gt,$.makeDirty(),this.instanceCount+=xt,this.customMatrix=L,this.flush()}getParticleLoop(t,e,r,i,s,a,h,o){const c=[...arguments].join("_");if(this.particleLoopCache.has(c))return this.particleLoopCache.get(c);let u=`
305
+ `,Y=48,le=Y/4;class de extends ut{KEY="ParticleSprite";texture;customMatrix;particleLoopCache=new Map;extraTextureUnit=1;constructor(t){super(t)}createDefaultShader(){const t=ue,e=ce;return this.vs=e,this.fs=t,this.defaultShader=this.createShader(e,t),this.defaultShader}enter(t){super.enter(t),this.currentShader.setUniform("u_projection",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:ht,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,h=1,o=1,c=0,u=0,l=1,m=1,d=!1,x=!1,v=!1,T=.5,y=.5,R,U=!1,L){if(!t.glTexture||a<=0)return;this.texture&&t!==this.texture&&this.flush();const N=typeof h=="number",H=typeof o=="number",O=typeof i=="number",_=typeof s=="number",w=typeof c=="number",S=!_&&typeof s[0]=="number",F=t.uvW-t.uvX,k=t.uvH-t.uvY,X=F===0?0:1/F,P=k===0?0:1/k;let D=t.rawWidth*(d?-1:1);N&&(D*=h);let B=t.rawHeight*(x?-1:1);H&&(B*=o),w&&(D*=(l-c)*X,B*=(m-u)*P);let G=v?Math.PI/2:0;O&&(G+=i);const E=s??4294967295,C=this.getParticleLoop(N,H,O,w,_,S,U,R);this.texture||(this.texture=t,this.useTexture(t.glTexture,0,0));const xt=a,j=this.instanceBuffer;let gt=j.usedElemNum;j.resize(xt*le);const ve=j.float32,ye=j.uint32;gt=C(e,r,h,o,i,c,u,l,m,s,ve,ye,D,B,X,P,G,E,T,y,a,xt,gt),j.usedElemNum=gt,j.makeDirty(),this.instanceCount+=xt,this.customMatrix=L,this.flush()}getParticleLoop(t,e,r,i,s,a,h,o){const c=[...arguments].join("_");if(this.particleLoopCache.has(c))return this.particleLoopCache.get(c);let u=`
306
306
  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 ) {
307
307
  ${t&&i?"const baseSx = rawWidth;":""}
308
308
  ${e&&i?"const baseSy = rawHeight;":""}
@@ -341,7 +341,7 @@ f32[idx + 11] = originY;
341
341
  }
342
342
  return idx;
343
343
  }
344
- `;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=wt(this.rapid.projection,s);this.currentShader.setUniform("u_projection",a),Mt(t,t.TRIANGLE_STRIP,0,4,this.instanceCount),this.rapid.drawcallCount++,this.texture=void 0,this.customMatrix=void 0}}var ft=(n=>(n[n.EQUAL=0]="EQUAL",n[n.NOT_EQUAL=1]="NOT_EQUAL",n))(ft||{}),Dt=(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))(Dt||{}),Bt=(n=>(n[n.CanvasItem=0]="CanvasItem",n[n.Viewport=1]="Viewport",n))(Bt||{});class fe{gl;canvas;projection=new Float32Array(16);projectionDirty=!0;dpr;backgroundColor=f.Black;logicWidth=0;logicHeight=0;physicsWidth=0;physicsHeight=0;currentRegion=null;maxTextureUnits=0;matrixStack=new ee(this);matrix;spriteRegion;graphicRegion;atlasSpriteRegion;particleRegion;drawcallCount=0;inCreateMask=!1;texture;premultipliedAlpha;antialias;roundPixels;textureFilter;scaleMode;_filterRT=[null,null];_filterInput=new I;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??j.NEAREST,this.premultipliedAlpha=t.premultipliedAlpha??!0,this.roundPixels=t.roundPixels??!1,this.scaleMode=t.scaleMode??0;const e=zt(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 ut(this),this.graphicRegion=new ae(this),this.atlasSpriteRegion=new oe(this),this.particleRegion=new de(this);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.logicWidth||t.width||this.physicsWidth/this.dpr,this.logicHeight=t.logicHeight||t.height||this.physicsHeight/this.dpr,this.resize(this.logicWidth,this.logicHeight,this.physicsWidth,this.physicsHeight),this.texture=new pt(this),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 _t(this.premultipliedAlpha,t)}enterRegion(t,e){this.currentRegion===t&&this.currentRegion.isSameShader(e)||(this.flush(),this.currentRegion=t,t.enter(e))}drawParticles(t){Pt(this,t)}drawSprite(t){Lt(this,t)}drawLine(t){Nt(this,t)}drawGraphic(t){dt(this,t)}drawRect(t){Wt(this,t)}drawCircle(t){Ot(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){It(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 h=i*a,o=Math.cos(h)*t,c=Math.sin(h)*t,u=.5+Math.cos(h)*.5,l=.5+Math.sin(h)*.5;this.addGraphicVertex(o,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.flush();const s=r??this.canvas.clientWidth??this.canvas.width,a=i??this.canvas.clientHeight??this.canvas.height;switch(r!==void 0&&(this.canvas.style.width=s+"px"),i!==void 0&&(this.canvas.style.height=a+"px"),this.physicsWidth=s*this.dpr,this.physicsHeight=a*this.dpr,this.logicWidth=t,this.logicHeight=e,this.scaleMode){case 1:this.canvas.width=t,this.canvas.height=e,this.canvas.style.imageRendering="pixelated",this.gl.viewport(0,0,t,e);break;case 0:this.canvas.width=this.physicsWidth,this.canvas.height=this.physicsHeight,this.canvas.style.imageRendering="auto",this.gl.viewport(0,0,this.physicsWidth,this.physicsHeight);break;default:throw new Error("scaleMode can only be CanvasScaleMode.Viewport or CanvasScaleMode.CanvasItem")}this.updateProjection(0,this.logicWidth,this.logicHeight,0)}updateProjection(t,e,r,i){this.updateOrthMatrix(this.projection,t,e,r,i),this.projectionDirty=!0}clear(){const t=this.gl;this.backgroundColor.setClearColor(t),t.clear(t.COLOR_BUFFER_BIT|t.STENCIL_BUFFER_BIT),this.drawcallCount=0,this.matrixStack.reset()}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}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 h=t.clone(this._filterInput),o=0;this.matrixStack.save(),this.matrixStack.identity();for(let c=0;c<e.length;c++){const u=this._filterRT[o%2];u.offsetX=0,u.offsetY=0,this.enterRenderTexture(u),this.clearRenderTexture(),this.drawSprite({texture:h,shader:e[c],offsetX:c==0?i:0,offsetY:c==0?i:0,padding:r}),this.leaveRenderTexture(),h=u,o++}return this.matrixStack.restore(),h.offsetX=-i,h.offsetY=-i,h}enterRenderTexture(t){this.flush(),t.activate(),this.gl.viewport(0,0,t.rawWidth,t.rawHeight),this.updateProjection(0,t.rawWidth,0,t.rawHeight)}clearRenderTexture(t=new f(0,0,0,0)){this.flush();const e=this.gl;t.setClearColor(e),e.clear(e.COLOR_BUFFER_BIT)}leaveRenderTexture(){this.flush(),this.gl.bindFramebuffer(this.gl.FRAMEBUFFER,null),this.gl.viewport(0,0,this.physicsWidth,this.physicsHeight),this.updateProjection(0,this.logicWidth,this.logicHeight,0)}drawToRenderTexture(t,e,r=new f(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.flush();const s=this.gl,a=this.physicsWidth/this.logicWidth,h=this.physicsHeight/this.logicHeight,o=Math.round(t*a),c=Math.round(this.physicsHeight-(e+i)*h),u=Math.round(r*a),l=Math.round(i*h);s.enable(s.SCISSOR_TEST),s.scissor(o,c,u,l)}endScissor(){this.flush(),this.gl.disable(this.gl.SCISSOR_TEST)}applyCamera(t){this.matrixStack.applyTransform(t);const e=this.matrixStack.curWorldM;this.matrix.invert(e)}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 me(n){return!(n===null||typeof n!="object"||Array.isArray(n)||n instanceof g||n instanceof f)}class W{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(W.float(t,e),W.float(r,i))}static randomColor(t,e){return new f(W.float(t.r,e.r),W.float(t.g,e.g),W.float(t.b,e.b),W.float(t.a,e.a))}static pick(t){return t[W.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 W.float(r,i);if(r instanceof g)return W.vector(r.x,i.x,r.y,i.y);if(r instanceof f)return W.randomColor(r,i)}return typeof t=="number"?t:t.clone()}}const st=(n,t)=>(Array.isArray(n)?n:[n]).slice(0,t);var kt=(n=>(n.POINT="point",n.CIRCLE="circle",n.RECT="rect",n))(kt||{});const Ht=10,Gt=0,Vt=!0;class J{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 b(A.Float32),delta:new b(A.Float32),damping:new b(A.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)?W.float(this.getComponent(t[0],e),this.getComponent(t[1],e)):this.getComponent(t,e)}create(t,e,r=1){const i=me(t)?t:void 0,s=i===void 0?t:void 0,a=r>0?r:1;let h=0;for(let o=0;o<this.components.length;o++){const{value:c,delta:u,damping:l}=this.components[o],m=this.getComponent(e,o),d=s===void 0?this.resolveComponent(i?.start,o,m):this.getComponent(s,o),x=i?.end===void 0?d:this.resolveComponent(i.end,o,m);c.push(d),u.push(i?.delta===void 0?(x-d)/a:this.getComponent(i.delta,o)),l.push(i?.damping??1),o===0&&(h=d)}return h}}class mt{options;emitting=!1;emitTimer=0;emitRate=Ht;emitTime=Gt;emitTimeCounter=0;localSpace=Vt;rapid;x=new b(A.Float32);y=new b(A.Float32);scaleX=new b(A.Float32);scaleY=new b(A.Float32);rotation=new b(A.Float32);color=new b(A.Uint32);remainingLife=new b(A.Float32);animations={speed:new J,rotation:new J,scale:new J,color:new J(4),velocity:new J(2)};count=0;constructor(t,e){this.rapid=t,this.options=e,this.emitRate=e.emitRate??Ht,this.emitTime=e.emitTime??Gt,this.localSpace=e.localSpace??Vt}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 mt(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=W.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,h=a.scale.create(s.scale,1,t),o=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,f.White,t),this.rotation.push(o),this.scaleX.push(h),this.scaleY.push(h),this.color.pushUint32(f.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,b.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),h=r.speed.get(i);this.rotation.typedArray[i]=s,this.scaleX.typedArray[i]=a,this.scaleY.typedArray[i]=a,this.color.uint32[i]=f.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]+=(h*Math.cos(s)+r.velocity.get(i,0))*t,this.y.typedArray[i]+=(h*Math.sin(s)+r.velocity.get(i,1))*t}return e}render(){const t=this.options,e=this.localSpace?void 0:this.rapid.matrix.alloc();this.rapid.drawParticles({texture:t.texture,shader:t.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:t.origin?.x??.5,originY:t.origin?.y??.5,customMatrix:e})}isActive(){return this.emitting||this.count>0}oneShot(){this.emit(this.emitRate)}}const xe=`
344
+ `;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=wt(this.rapid.projection,s);this.currentShader.setUniform("u_projection",a),Mt(t,t.TRIANGLE_STRIP,0,4,this.instanceCount),this.rapid.drawcallCount++,this.texture=void 0,this.customMatrix=void 0}}var ft=(n=>(n[n.EQUAL=0]="EQUAL",n[n.NOT_EQUAL=1]="NOT_EQUAL",n))(ft||{}),Dt=(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))(Dt||{}),Bt=(n=>(n[n.CanvasItem=0]="CanvasItem",n[n.Viewport=1]="Viewport",n))(Bt||{});class fe{gl;canvas;projection=new Float32Array(16);projectionDirty=!0;dpr;backgroundColor=f.Black;logicWidth=0;logicHeight=0;physicsWidth=0;physicsHeight=0;currentRegion=null;maxTextureUnits=0;matrixStack=new ee(this);matrix;spriteRegion;graphicRegion;atlasSpriteRegion;particleRegion;drawcallCount=0;inCreateMask=!1;texture;premultipliedAlpha;antialias;roundPixels;textureFilter;scaleMode;_filterRT=[null,null];_filterInput=new I;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??z.NEAREST,this.premultipliedAlpha=t.premultipliedAlpha??!0,this.roundPixels=t.roundPixels??!1,this.scaleMode=t.scaleMode??0;const e=zt(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 ut(this),this.graphicRegion=new ae(this),this.atlasSpriteRegion=new oe(this),this.particleRegion=new de(this);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.logicWidth||t.width||this.physicsWidth/this.dpr,this.logicHeight=t.logicHeight||t.height||this.physicsHeight/this.dpr,this.resize(this.logicWidth,this.logicHeight,this.physicsWidth,this.physicsHeight),this.texture=new pt(this),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 _t(this.premultipliedAlpha,t)}enterRegion(t,e){this.currentRegion===t&&this.currentRegion.isSameShader(e)||(this.flush(),this.currentRegion=t,t.enter(e))}drawParticles(t){Pt(this,t)}drawSprite(t){Lt(this,t)}drawLine(t){Nt(this,t)}drawGraphic(t){dt(this,t)}drawRect(t){Wt(this,t)}drawCircle(t){Ot(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){It(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 h=i*a,o=Math.cos(h)*t,c=Math.sin(h)*t,u=.5+Math.cos(h)*.5,l=.5+Math.sin(h)*.5;this.addGraphicVertex(o,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.flush();const s=r??this.canvas.clientWidth??this.canvas.width,a=i??this.canvas.clientHeight??this.canvas.height;switch(r!==void 0&&(this.canvas.style.width=s+"px"),i!==void 0&&(this.canvas.style.height=a+"px"),this.physicsWidth=s*this.dpr,this.physicsHeight=a*this.dpr,this.logicWidth=t,this.logicHeight=e,this.scaleMode){case 1:this.canvas.width=t,this.canvas.height=e,this.canvas.style.imageRendering="pixelated",this.gl.viewport(0,0,t,e);break;case 0:this.canvas.width=this.physicsWidth,this.canvas.height=this.physicsHeight,this.canvas.style.imageRendering="auto",this.gl.viewport(0,0,this.physicsWidth,this.physicsHeight);break;default:throw new Error("scaleMode can only be CanvasScaleMode.Viewport or CanvasScaleMode.CanvasItem")}this.updateProjection(0,this.logicWidth,this.logicHeight,0)}updateProjection(t,e,r,i){this.updateOrthMatrix(this.projection,t,e,r,i),this.projectionDirty=!0}clear(){const t=this.gl;this.backgroundColor.setClearColor(t),t.clear(t.COLOR_BUFFER_BIT|t.STENCIL_BUFFER_BIT),this.drawcallCount=0,this.matrixStack.reset()}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}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 h=t.clone(this._filterInput),o=0;this.matrixStack.save(),this.matrixStack.identity();for(let c=0;c<e.length;c++){const u=this._filterRT[o%2];u.offsetX=0,u.offsetY=0,this.enterRenderTexture(u),this.clearRenderTexture(),this.drawSprite({texture:h,shader:e[c],offsetX:c==0?i:0,offsetY:c==0?i:0,padding:r}),this.leaveRenderTexture(),h=u,o++}return this.matrixStack.restore(),h.offsetX=-i,h.offsetY=-i,h}enterRenderTexture(t){this.flush(),t.activate(),this.gl.viewport(0,0,t.rawWidth,t.rawHeight),this.updateProjection(0,t.rawWidth,0,t.rawHeight)}clearRenderTexture(t=new f(0,0,0,0)){this.flush();const e=this.gl;t.setClearColor(e),e.clear(e.COLOR_BUFFER_BIT)}leaveRenderTexture(){this.flush(),this.gl.bindFramebuffer(this.gl.FRAMEBUFFER,null),this.gl.viewport(0,0,this.physicsWidth,this.physicsHeight),this.updateProjection(0,this.logicWidth,this.logicHeight,0)}drawToRenderTexture(t,e,r=new f(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.flush();const s=this.gl,a=this.physicsWidth/this.logicWidth,h=this.physicsHeight/this.logicHeight,o=Math.round(t*a),c=Math.round(this.physicsHeight-(e+i)*h),u=Math.round(r*a),l=Math.round(i*h);s.enable(s.SCISSOR_TEST),s.scissor(o,c,u,l)}endScissor(){this.flush(),this.gl.disable(this.gl.SCISSOR_TEST)}applyCamera(t){this.matrixStack.applyTransform(t);const e=this.matrixStack.curWorldM;this.matrix.invert(e)}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 me(n){return!(n===null||typeof n!="object"||Array.isArray(n)||n instanceof g||n instanceof f)}class W{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(W.float(t,e),W.float(r,i))}static randomColor(t,e){return new f(W.float(t.r,e.r),W.float(t.g,e.g),W.float(t.b,e.b),W.float(t.a,e.a))}static pick(t){return t[W.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 W.float(r,i);if(r instanceof g)return W.vector(r.x,i.x,r.y,i.y);if(r instanceof f)return W.randomColor(r,i)}return typeof t=="number"?t:t.clone()}}const st=(n,t)=>(Array.isArray(n)?n:[n]).slice(0,t);var kt=(n=>(n.POINT="point",n.CIRCLE="circle",n.RECT="rect",n))(kt||{});const Ht=10,Gt=0,Vt=!0;class J{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 b(A.Float32),delta:new b(A.Float32),damping:new b(A.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)?W.float(this.getComponent(t[0],e),this.getComponent(t[1],e)):this.getComponent(t,e)}create(t,e,r=1){const i=me(t)?t:void 0,s=i===void 0?t:void 0,a=r>0?r:1;let h=0;for(let o=0;o<this.components.length;o++){const{value:c,delta:u,damping:l}=this.components[o],m=this.getComponent(e,o),d=s===void 0?this.resolveComponent(i?.start,o,m):this.getComponent(s,o),x=i?.end===void 0?d:this.resolveComponent(i.end,o,m);c.push(d),u.push(i?.delta===void 0?(x-d)/a:this.getComponent(i.delta,o)),l.push(i?.damping??1),o===0&&(h=d)}return h}}class mt{options;emitting=!1;emitTimer=0;emitRate=Ht;emitTime=Gt;emitTimeCounter=0;localSpace=Vt;rapid;x=new b(A.Float32);y=new b(A.Float32);scaleX=new b(A.Float32);scaleY=new b(A.Float32);rotation=new b(A.Float32);color=new b(A.Uint32);remainingLife=new b(A.Float32);animations={speed:new J,rotation:new J,scale:new J,color:new J(4),velocity:new J(2)};count=0;constructor(t,e){this.rapid=t,this.options=e,this.emitRate=e.emitRate??Ht,this.emitTime=e.emitTime??Gt,this.localSpace=e.localSpace??Vt}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 mt(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=W.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,h=a.scale.create(s.scale,1,t),o=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,f.White,t),this.rotation.push(o),this.scaleX.push(h),this.scaleY.push(h),this.color.pushUint32(f.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,b.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),h=r.speed.get(i);this.rotation.typedArray[i]=s,this.scaleX.typedArray[i]=a,this.scaleY.typedArray[i]=a,this.color.uint32[i]=f.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]+=(h*Math.cos(s)+r.velocity.get(i,0))*t,this.y.typedArray[i]+=(h*Math.sin(s)+r.velocity.get(i,1))*t}return e}render(){const t=this.options,e=this.localSpace?void 0:this.rapid.matrix.alloc();this.rapid.drawParticles({texture:t.texture,shader:t.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:t.origin?.x??.5,originY:t.origin?.y??.5,customMatrix:e})}isActive(){return this.emitting||this.count>0}oneShot(){this.emit(this.emitRate)}}const xe=`
345
345
  uniform mediump int uLightTextureId[%TEXTURE_NUM%];
346
346
 
347
347
  uniform sampler2D uNormalMap;
@@ -394,4 +394,4 @@ void light_fragment(inout vec4 color, in vec2 vRegion) {
394
394
 
395
395
  color.rgb = color.rgb * (uAmbientColor + totalDiffuse) + totalSpecular;
396
396
  }
397
- `,ge="void light_vertex(inout vec4 p, vec2 u) {}",tt=8;class Xt extends rt{constructor(t,e="",r="",i=0,s){super(t,e,r,i+2,s),this.prefix.push("light_")}getGLShader(t,e,r,i){const s=K(xe,t.getMaxTexutreUnit(this),"layer","lightUV","lightTex = ");return i=this.replaceCustomCode(i,s),r=this.replaceCustomCode(r,ge),super.getGLShader(t,e,r,i)}}class pe{shader;rapid;rt;constructor(t){this.rapid=t,this.shader=new Xt(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),h=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 o=Math.hypot(t.rawWidth,t.rawHeight)*.5*1.05;return i.drawToRenderTexture(this.rt,()=>{i.withMask(()=>{i.startMaskGraphic(i.gl.TRIANGLES);for(let c=0;c<h.length;c++){const u=h[c],l=h[(c+1)%h.length],m=u.subtract(a).normalized(),d=l.subtract(a).normalized(),x=Math.max(-.999,Math.min(1,m.dot(d))),v=Math.sqrt((1+x)*.5),T=o/v,y=a.add(m.multiply(T)),R=a.add(d.multiply(T));i.addGraphicVertex(u.x,u.y),i.addGraphicVertex(l.x,l.y),i.addGraphicVertex(R.x,R.y),i.addGraphicVertex(u.x,u.y),i.addGraphicVertex(R.x,R.y),i.addGraphicVertex(y.x,y.y)}i.endGraphic()},()=>{i.drawSprite({texture:t})},ft.NOT_EQUAL)}),this.rt}setupLight(t){const e=t.customShader??this.shader,r=st(t.lightMatrix,tt),i=st(t.color,tt),s=st(t.lightHeight,tt),a=st(t.lightTexture,tt);return e.setUniforms({uResolution:[this.rapid.physicsWidth,this.rapid.physicsHeight],uLogicalResolution:[this.rapid.logicWidth,this.rapid.logicHeight],uNormalMap:t.normalMap.glTexture,uLightTextureId:a.map(h=>h.glTexture),uLightCount:r.length,uLightMatrix:Float32Array.from(r.flatMap(h=>Array.from(this.rapid.matrix.getMatrix(typeof h=="number"?h:h.world)))),uLightColor:Float32Array.from(r.flatMap((h,o)=>{const c=i[o]??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((h,o)=>s[o]??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}}p.BaseTexture=V,p.BlendMode=Dt,p.CanvasScaleMode=Bt,p.Color=f,p.CustomGlShader=rt,p.GLShader=ot,p.Light=pe,p.LightShader=Xt,p.LineTextureMode=At,p.MAX_LIGHTS=tt,p.MaskType=ft,p.ParticleEmitter=mt,p.ParticleShape=kt,p.Rapid=fe,p.RenderTexture=vt,p.TextTexture=yt,p.Texture=I,p.TextureFilterMode=j,p.TextureManager=pt,p.TextureWrapMode=z,p.Vec2=g,p.composeProjectionWithAffine=wt,p.drawCircle=Ot,p.drawGraphic=dt,p.drawLine=Nt,p.drawMaskImage=It,p.drawParticles=Pt,p.drawRect=Wt,p.drawSprite=Lt,p.getColorUint32=_t,p.getLineGeometry=Ct,p.getLineNormal=Ut,p.multiplyMat4=St,Object.defineProperty(p,Symbol.toStringTag,{value:"Module"})}));
397
+ `,ge="void light_vertex(inout vec4 p, vec2 u) {}",tt=8;class Xt extends rt{constructor(t,e="",r="",i=0,s){super(t,e,r,i+2,s),this.prefix.push("light_")}getGLShader(t,e,r,i){const s=K(xe,t.getMaxTexutreUnit(this),"layer","lightUV","lightTex = ");return i=this.replaceCustomCode(i,s),r=this.replaceCustomCode(r,ge),super.getGLShader(t,e,r,i)}}class pe{shader;rapid;rt;constructor(t){this.rapid=t,this.shader=new Xt(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),h=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 o=Math.hypot(t.rawWidth,t.rawHeight)*.5*1.05;return i.drawToRenderTexture(this.rt,()=>{i.withMask(()=>{i.startMaskGraphic(i.gl.TRIANGLES);for(let c=0;c<h.length;c++){const u=h[c],l=h[(c+1)%h.length],m=u.subtract(a).normalized(),d=l.subtract(a).normalized(),x=Math.max(-.999,Math.min(1,m.dot(d))),v=Math.sqrt((1+x)*.5),T=o/v,y=a.add(m.multiply(T)),R=a.add(d.multiply(T));i.addGraphicVertex(u.x,u.y),i.addGraphicVertex(l.x,l.y),i.addGraphicVertex(R.x,R.y),i.addGraphicVertex(u.x,u.y),i.addGraphicVertex(R.x,R.y),i.addGraphicVertex(y.x,y.y)}i.endGraphic()},()=>{i.drawSprite({texture:t})},ft.NOT_EQUAL)}),this.rt}setupLight(t){const e=t.customShader??this.shader,r=st(t.lightMatrix,tt),i=st(t.color,tt),s=st(t.lightHeight,tt),a=st(t.lightTexture,tt);return e.setUniforms({uResolution:[this.rapid.physicsWidth,this.rapid.physicsHeight],uLogicalResolution:[this.rapid.logicWidth,this.rapid.logicHeight],uNormalMap:t.normalMap.glTexture,uLightTextureId:a.map(h=>h.glTexture),uLightCount:r.length,uLightMatrix:Float32Array.from(r.flatMap(h=>Array.from(this.rapid.matrix.getMatrix(typeof h=="number"?h:h.world)))),uLightColor:Float32Array.from(r.flatMap((h,o)=>{const c=i[o]??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((h,o)=>s[o]??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}}p.BaseTexture=V,p.BlendMode=Dt,p.CanvasScaleMode=Bt,p.Color=f,p.CustomGlShader=rt,p.GLShader=ot,p.Light=pe,p.LightShader=Xt,p.LineTextureMode=At,p.MAX_LIGHTS=tt,p.MaskType=ft,p.ParticleEmitter=mt,p.ParticleShape=kt,p.Rapid=fe,p.RenderTexture=vt,p.TextTexture=yt,p.Texture=I,p.TextureFilterMode=z,p.TextureManager=pt,p.TextureWrapMode=$,p.Vec2=g,p.composeProjectionWithAffine=wt,p.drawCircle=Ot,p.drawGraphic=dt,p.drawLine=Nt,p.drawMaskImage=It,p.drawParticles=Pt,p.drawRect=Wt,p.drawSprite=Lt,p.getColorUint32=_t,p.getLineGeometry=Ct,p.getLineNormal=Ut,p.multiplyMat4=St,Object.defineProperty(p,Symbol.toStringTag,{value:"Module"})}));
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "rapid-render",
3
3
  "private": false,
4
- "version": "1.0.19",
4
+ "version": "1.0.22",
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",