rapid-render 1.0.16 → 1.0.18

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.
@@ -1,130 +1,4 @@
1
- var S = /* @__PURE__ */ ((n) => (n[n.Float32 = 0] = "Float32", n[n.Uint32 = 1] = "Uint32", n[n.Uint16 = 2] = "Uint16", n))(S || {});
2
- class A {
3
- usedElemNum;
4
- typedArray;
5
- arrayType;
6
- maxElemNum;
7
- bytePerElem;
8
- arraybuffer;
9
- uint32;
10
- float32;
11
- uint16;
12
- constructor(t) {
13
- 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();
14
- }
15
- getArrayType(t) {
16
- switch (t) {
17
- case 0:
18
- return Float32Array;
19
- case 1:
20
- return Uint32Array;
21
- case 2:
22
- return Uint16Array;
23
- default:
24
- throw new Error("Unsupported ArrayType");
25
- }
26
- }
27
- updateTypedArray() {
28
- switch (this.uint32 = new Uint32Array(this.arraybuffer), this.float32 = new Float32Array(this.arraybuffer), this.uint16 = new Uint16Array(this.arraybuffer), this.arrayType) {
29
- case 0:
30
- this.typedArray = this.float32;
31
- break;
32
- case 1:
33
- this.typedArray = this.uint32;
34
- break;
35
- case 2:
36
- this.typedArray = this.uint16;
37
- break;
38
- }
39
- }
40
- clear() {
41
- this.usedElemNum = 0;
42
- }
43
- static removeAtIndices(t, e) {
44
- const r = Math.max(...e.map((h) => h.usedElemNum)), i = [...new Set(t)].filter(
45
- (h) => h >= 0 && h < r
46
- ).sort((h, o) => h - o);
47
- if (i.length === 0)
48
- return 0;
49
- let s = 0, a = 0;
50
- for (let h = 0; h < r; h++) {
51
- if (a < i.length && h === i[a]) {
52
- a++;
53
- continue;
54
- }
55
- if (s !== h)
56
- for (const o of e)
57
- o.typedArray[s] = o.typedArray[h];
58
- s++;
59
- }
60
- for (const h of e)
61
- h.usedElemNum = s;
62
- return r - s;
63
- }
64
- resize(t = 0) {
65
- if (t += this.usedElemNum, t > this.maxElemNum) {
66
- for (; t > this.maxElemNum; )
67
- this.maxElemNum *= 2;
68
- return this.setMaxSize(this.maxElemNum), !0;
69
- }
70
- return !1;
71
- }
72
- setMaxSize(t = this.maxElemNum) {
73
- const e = this.typedArray;
74
- this.maxElemNum = t, this.arraybuffer = new ArrayBuffer(t * this.bytePerElem), this.updateTypedArray(), this.typedArray.set(e);
75
- }
76
- pushUint32(t) {
77
- this.resize(1), this.uint32[this.usedElemNum++] = t;
78
- }
79
- pushFloat32(t) {
80
- this.resize(1), this.float32[this.usedElemNum++] = t;
81
- }
82
- push(t) {
83
- this.resize(1), this.typedArray[this.usedElemNum++] = t;
84
- }
85
- pop() {
86
- return this.typedArray[--this.usedElemNum];
87
- }
88
- top() {
89
- return this.typedArray[this.usedElemNum - 1];
90
- }
91
- get(t) {
92
- return this.typedArray[t];
93
- }
94
- getArray(t = 0, e) {
95
- return e === void 0 ? this.typedArray : this.typedArray.subarray(t, e);
96
- }
97
- get length() {
98
- return this.usedElemNum;
99
- }
100
- }
101
- class it extends A {
102
- buffer;
103
- gl;
104
- dirty = !0;
105
- type;
106
- webglBufferSize = 0;
107
- usage;
108
- constructor(t, e, r = t.ARRAY_BUFFER, i = t.DYNAMIC_DRAW) {
109
- super(e), this.gl = t, this.buffer = t.createBuffer(), this.type = r, this.usage = i;
110
- }
111
- makeDirty() {
112
- this.dirty = !0;
113
- }
114
- clear() {
115
- super.clear(), this.dirty = !1;
116
- }
117
- bindBuffer() {
118
- this.gl.bindBuffer(this.type, this.buffer);
119
- }
120
- bufferData() {
121
- if (this.dirty) {
122
- const t = this.gl;
123
- 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;
124
- }
125
- }
126
- }
127
- class x {
1
+ class m {
128
2
  _r;
129
3
  _g;
130
4
  _b;
@@ -166,7 +40,7 @@ class x {
166
40
  }
167
41
  updateUint() {
168
42
  this.uint32 = (this._a << 24 | this._b << 16 | this._g << 8 | this._r) >>> 0;
169
- const t = x.clampByte(this._a), e = x.clampByte(this._r * t / 255), r = x.clampByte(this._g * t / 255), i = x.clampByte(this._b * t / 255);
43
+ const t = m.clampByte(this._a), e = m.clampByte(this._r * t / 255), r = m.clampByte(this._g * t / 255), i = m.clampByte(this._b * t / 255);
170
44
  this.premultipliedUint32 = (t << 24 | i << 16 | r << 8 | e) >>> 0;
171
45
  }
172
46
  reset() {
@@ -185,7 +59,7 @@ class x {
185
59
  this.setRGBA(t.r, t.g, t.b, t.a);
186
60
  }
187
61
  clone() {
188
- return new x(this._r, this._g, this._b, this._a);
62
+ return new m(this._r, this._g, this._b, this._a);
189
63
  }
190
64
  toHexString(t = !0) {
191
65
  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");
@@ -195,13 +69,13 @@ class x {
195
69
  return t.r === this.r && t.g === this.g && t.b === this.b && t.a === this.a;
196
70
  }
197
71
  static FromHSL(t, e, r) {
198
- return new x(0, 0, 0).setHSL(t, e, r);
72
+ return new m(0, 0, 0).setHSL(t, e, r);
199
73
  }
200
74
  static FromRGB(t, e, r) {
201
- return new x(t, e, r);
75
+ return new m(t, e, r);
202
76
  }
203
77
  static fromNorm(t, e, r, i = 1) {
204
- return new x(
78
+ return new m(
205
79
  Math.round(t * 255),
206
80
  Math.round(e * 255),
207
81
  Math.round(r * 255),
@@ -209,17 +83,17 @@ class x {
209
83
  );
210
84
  }
211
85
  static packColor(t, e, r, i, s) {
212
- const a = x.clampByte(i), h = s ? a / 255 : 1, o = x.clampByte(t * h), c = x.clampByte(e * h), u = x.clampByte(r * h);
86
+ const a = m.clampByte(i), h = s ? a / 255 : 1, o = m.clampByte(t * h), c = m.clampByte(e * h), u = m.clampByte(r * h);
213
87
  return (a << 24 | u << 16 | c << 8 | o) >>> 0;
214
88
  }
215
89
  static fromHex(t) {
216
90
  t.startsWith("#") && (t = t.slice(1));
217
91
  const e = parseInt(t.slice(0, 2), 16), r = parseInt(t.slice(2, 4), 16), i = parseInt(t.slice(4, 6), 16);
218
92
  let s = 255;
219
- return t.length >= 8 && (s = parseInt(t.slice(6, 8), 16)), new x(e, r, i, s);
93
+ return t.length >= 8 && (s = parseInt(t.slice(6, 8), 16)), new m(e, r, i, s);
220
94
  }
221
95
  add(t) {
222
- return new x(
96
+ return new m(
223
97
  Math.min(this.r + t.r, 255),
224
98
  Math.min(this.g + t.g, 255),
225
99
  Math.min(this.b + t.b, 255),
@@ -227,7 +101,7 @@ class x {
227
101
  );
228
102
  }
229
103
  subtract(t) {
230
- return new x(
104
+ return new m(
231
105
  this.r - t.r,
232
106
  this.g - t.g,
233
107
  this.b - t.b,
@@ -235,12 +109,12 @@ class x {
235
109
  );
236
110
  }
237
111
  divide(t) {
238
- return t instanceof x ? new x(
112
+ return t instanceof m ? new m(
239
113
  this.r / t.r,
240
114
  this.g / t.g,
241
115
  this.b / t.b,
242
116
  this.a / t.a
243
- ) : new x(
117
+ ) : new m(
244
118
  this.r / t,
245
119
  this.g / t,
246
120
  this.b / t,
@@ -248,12 +122,12 @@ class x {
248
122
  );
249
123
  }
250
124
  multiply(t) {
251
- return t instanceof x ? new x(
125
+ return t instanceof m ? new m(
252
126
  this.r * t.r,
253
127
  this.g * t.g,
254
128
  this.b * t.b,
255
129
  this.a * t.a
256
- ) : new x(
130
+ ) : new m(
257
131
  this.r * t,
258
132
  this.g * t,
259
133
  this.b * t,
@@ -263,393 +137,123 @@ class x {
263
137
  clamp() {
264
138
  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();
265
139
  }
266
- static Red = new x(255, 0, 0, 255);
267
- static Green = new x(0, 255, 0, 255);
268
- static Blue = new x(0, 0, 255, 255);
269
- static Yellow = new x(255, 255, 0, 255);
270
- static Purple = new x(128, 0, 128, 255);
271
- static Orange = new x(255, 165, 0, 255);
272
- static Pink = new x(255, 192, 203, 255);
273
- static Gray = new x(128, 128, 128, 255);
274
- static Brown = new x(139, 69, 19, 255);
275
- static Cyan = new x(0, 255, 255, 255);
276
- static Magenta = new x(255, 0, 255, 255);
277
- static Lime = new x(192, 255, 0, 255);
278
- static White = new x(255, 255, 255, 255);
279
- static Black = new x(0, 0, 0, 255);
280
- static Transparent = new x(0, 0, 0, 0);
140
+ static Red = new m(255, 0, 0, 255);
141
+ static Green = new m(0, 255, 0, 255);
142
+ static Blue = new m(0, 0, 255, 255);
143
+ static Yellow = new m(255, 255, 0, 255);
144
+ static Purple = new m(128, 0, 128, 255);
145
+ static Orange = new m(255, 165, 0, 255);
146
+ static Pink = new m(255, 192, 203, 255);
147
+ static Gray = new m(128, 128, 128, 255);
148
+ static Brown = new m(139, 69, 19, 255);
149
+ static Cyan = new m(0, 255, 255, 255);
150
+ static Magenta = new m(255, 0, 255, 255);
151
+ static Lime = new m(192, 255, 0, 255);
152
+ static White = new m(255, 255, 255, 255);
153
+ static Black = new m(0, 0, 0, 255);
154
+ static Transparent = new m(0, 0, 0, 0);
281
155
  }
282
- const T = 6;
283
- class At {
284
- matrixCount = 0;
285
- buffer;
286
- data;
287
- temporary = -1;
288
- constructor(t = 10) {
289
- this.buffer = new A(S.Float32), this.buffer.resize(t * T), this.data = this.buffer.getArray(), this.reset();
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 || {});
157
+ class bt {
158
+ render;
159
+ cache = /* @__PURE__ */ new Map();
160
+ constructor(t) {
161
+ this.render = t;
290
162
  }
291
- alloc() {
292
- const t = this.allocDirty();
293
- return this.identity(t), t;
163
+ async load(t, e) {
164
+ let r = this.cache.get(t);
165
+ if (r)
166
+ return new N(r);
167
+ try {
168
+ const i = await this._fetchImage(t);
169
+ return r = G.fromSource(this.render, i, e), r.uid = t, this.cache.set(t, r), new N(r);
170
+ } catch (i) {
171
+ throw console.error(`[TextureManager] Failed to load: ${t}`, i), i;
172
+ }
294
173
  }
295
- allocDirty() {
296
- return this.buffer.resize(T) && (this.data = this.buffer.getArray()), this.buffer.usedElemNum += T, this.matrixCount++;
174
+ create(t, e) {
175
+ if (e?.key && this.cache.has(e.key))
176
+ return new N(this.cache.get(e.key));
177
+ const r = G.fromSource(this.render, t, e);
178
+ return e?.key && (r.uid = e.key, this.cache.set(e.key, r)), new N(r);
297
179
  }
298
- reset() {
299
- this.matrixCount = 0, this.buffer.usedElemNum = 0, this.temporary = this.alloc();
180
+ createRenderTexture(t) {
181
+ return new Ut(this.render, t);
300
182
  }
301
- identity(t) {
302
- const e = t * T, r = this.data;
303
- r[e] = 1, r[e + 1] = 0, r[e + 2] = 0, r[e + 3] = 1, r[e + 4] = 0, r[e + 5] = 0;
183
+ createTextTexture(t) {
184
+ return new _t(this.render, t);
304
185
  }
305
- translate(t, e, r) {
306
- const i = t * T, s = this.data;
307
- s[i + 4] = s[i] * e + s[i + 2] * r + s[i + 4], s[i + 5] = s[i + 1] * e + s[i + 3] * r + s[i + 5];
186
+ destroy(t, e = !1) {
187
+ let r, i;
188
+ typeof t == "string" ? (i = t, r = this.cache.get(i)) : t instanceof N ? (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));
308
189
  }
309
- scale(t, e, r) {
310
- const i = t * T, s = this.data;
311
- s[i] *= e, s[i + 1] *= e, s[i + 2] *= r, s[i + 3] *= r;
190
+ destroyAll() {
191
+ for (const t of this.cache.values())
192
+ t.destroy(this.render.gl);
193
+ this.cache.clear();
312
194
  }
313
- rotate(t, e) {
314
- const r = t * T, i = this.data, s = Math.cos(e), a = Math.sin(e), h = i[r], o = i[r + 1], c = i[r + 2], u = i[r + 3];
315
- i[r] = h * s + c * a, i[r + 1] = o * s + u * a, i[r + 2] = h * -a + c * s, i[r + 3] = o * -a + u * s;
195
+ _fetchImage(t) {
196
+ return new Promise((e, r) => {
197
+ const i = new Image();
198
+ i.crossOrigin = "anonymous", i.onload = () => e(i), i.onerror = () => r(new Error(`Failed to load image resource: ${t}`)), i.src = t;
199
+ });
316
200
  }
317
- rotateWithOffset(t, e, r, i) {
318
- this.translate(t, r, i), this.rotate(t, e), this.translate(t, -r, -i);
201
+ }
202
+ class G {
203
+ glTexture = null;
204
+ width;
205
+ height;
206
+ uid;
207
+ refCount = 0;
208
+ constructor(t, e, r) {
209
+ this.glTexture = t, this.width = e, this.height = r;
319
210
  }
320
- copy(t, e) {
321
- const r = t * T, i = e * T, s = this.data;
322
- s[r] = s[i], s[r + 1] = s[i + 1], s[r + 2] = s[i + 2], s[r + 3] = s[i + 3], s[r + 4] = s[i + 4], s[r + 5] = s[i + 5];
211
+ static fromSource(t, e, r = {}) {
212
+ const i = rt(t, e, r);
213
+ return new G(i, e.width, e.height);
323
214
  }
324
- multiply(t, e) {
325
- this.multiplyOut(t, t, e);
215
+ updateSource(t, e, r = {}) {
216
+ this.glTexture && (t.bindTexture(t.TEXTURE_2D, this.glTexture), r.textureFilter !== void 0 && ht(t, r.textureFilter), r.wrap !== void 0 && at(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));
326
217
  }
327
- multiplyOut(t, e, r) {
328
- const i = t * T, s = e * T, a = r * T, h = this.data, o = h[s], c = h[s + 1], u = h[s + 2], l = h[s + 3], d = h[s + 4], f = h[s + 5], m = h[a], g = h[a + 1], v = h[a + 2], y = h[a + 3], w = h[a + 4], M = h[a + 5];
329
- h[i] = o * m + u * g, h[i + 1] = c * m + l * g, h[i + 2] = o * v + u * y, h[i + 3] = c * v + l * y, h[i + 4] = o * w + u * M + d, h[i + 5] = c * w + l * M + f;
218
+ setFilterMode(t, e) {
219
+ this.glTexture && (t.bindTexture(t.TEXTURE_2D, this.glTexture), ht(t, e), t.bindTexture(t.TEXTURE_2D, null));
330
220
  }
331
- invert(t) {
332
- const e = t * T, r = this.data, i = r[e], s = r[e + 1], a = r[e + 2], h = r[e + 3], o = r[e + 4], c = r[e + 5];
333
- let u = i * h - s * a;
334
- if (!u) {
335
- this.identity(t);
336
- return;
337
- }
338
- u = 1 / u, r[e] = h * u, r[e + 1] = -s * u, r[e + 2] = -a * u, r[e + 3] = i * u, r[e + 4] = (a * c - h * o) * u, r[e + 5] = (s * o - i * c) * u;
221
+ setWrapMode(t, e) {
222
+ this.glTexture && (t.bindTexture(t.TEXTURE_2D, this.glTexture), at(t, e), t.bindTexture(t.TEXTURE_2D, null));
339
223
  }
340
- transformPoint(t, e, r) {
341
- const i = t * T, s = this.data;
342
- return {
343
- x: e * s[i] + r * s[i + 2] + s[i + 4],
344
- y: e * s[i + 1] + r * s[i + 3] + s[i + 5]
345
- };
224
+ destroy(t) {
225
+ this.glTexture && (t.deleteTexture(this.glTexture), this.glTexture = null);
346
226
  }
347
- worldToLocal(t, e, r) {
348
- const i = this.allocDirty();
349
- this.copy(i, t), this.invert(i);
350
- const s = this.transformPoint(i, e, r);
351
- return this.matrixCount--, this.buffer.usedElemNum -= T, s;
227
+ }
228
+ class N {
229
+ base;
230
+ uvX = 0;
231
+ uvY = 0;
232
+ uvW = 1;
233
+ uvH = 1;
234
+ rawWidth = 0;
235
+ rawHeight = 0;
236
+ offsetX = 0;
237
+ offsetY = 0;
238
+ scale = 1;
239
+ isAtlas = !1;
240
+ flipY = !1;
241
+ isRotated = !1;
242
+ glTexture = null;
243
+ get width() {
244
+ return this.isRotated ? this.rawHeight : this.rawWidth;
352
245
  }
353
- localToWorld(t, e, r) {
354
- return this.transformPoint(t, e, r);
246
+ get height() {
247
+ return this.isRotated ? this.rawWidth : this.rawHeight;
355
248
  }
356
- getPosition(t) {
357
- const e = t * T, r = this.data;
358
- return { x: r[e + 4], y: r[e + 5] };
249
+ // Stored pixel-space region (before UV conversion), used by getSubTexture()
250
+ _px = 0;
251
+ _py = 0;
252
+ constructor(t) {
253
+ t && this.setBase(t);
359
254
  }
360
- getScale(t) {
361
- const e = t * T, r = this.data, i = Math.sqrt(r[e] * r[e] + r[e + 1] * r[e + 1]), s = Math.sqrt(r[e + 2] * r[e + 2] + r[e + 3] * r[e + 3]);
362
- return { x: i, y: s };
363
- }
364
- getRotation(t) {
365
- const e = t * T, r = this.data;
366
- return Math.atan2(r[e + 1], r[e]);
367
- }
368
- toCSSMatrix(t, e = 1, r = 1) {
369
- const i = t * T, s = this.data;
370
- return `matrix(${s[i] * e}, ${s[i + 1] * r}, ${s[i + 2] * e}, ${s[i + 3] * r}, ${s[i + 4] * e}, ${s[i + 5] * r})`;
371
- }
372
- getMatrix(t) {
373
- const e = t * T;
374
- return this.data.slice(e, e + T);
375
- }
376
- getMatrixRef(t) {
377
- const e = t * T;
378
- return this.data.subarray(e, e + T);
379
- }
380
- setMatrix(t, e) {
381
- const r = t * T, i = this.data;
382
- i[r] = e[0], i[r + 1] = e[1], i[r + 2] = e[2], i[r + 3] = e[3], i[r + 4] = e[4], i[r + 5] = e[5];
383
- }
384
- multiplyAffineInPlace(t, e, r, i, s, a, h) {
385
- const o = t * 6, c = this.data, u = c[o], l = c[o + 1], d = c[o + 2], f = c[o + 3], m = c[o + 4], g = c[o + 5];
386
- c[o] = u * e + d * r, c[o + 1] = l * e + f * r, c[o + 2] = u * i + d * s, c[o + 3] = l * i + f * s, c[o + 4] = u * a + d * h + m, c[o + 5] = l * a + f * h + g;
387
- }
388
- multiplyAffine(t, e, r, i, s, a, h, o) {
389
- const c = t * 6, u = e * 6, l = this.data, d = l[c], f = l[c + 1], m = l[c + 2], g = l[c + 3], v = l[c + 4], y = l[c + 5];
390
- l[u] = d * r + m * i, l[u + 1] = f * r + g * i, l[u + 2] = d * s + m * a, l[u + 3] = f * s + g * a, l[u + 4] = d * h + m * o + v, l[u + 5] = f * h + g * o + y;
391
- }
392
- }
393
- class bt {
394
- matrix = new At();
395
- step = 0;
396
- stack = new A(S.Uint32);
397
- stepWorldM = new A(S.Uint32);
398
- stepAction = new A(S.Uint32);
399
- stepParentM = new A(S.Uint32);
400
- stepClose = new A(S.Uint32);
401
- stepStack = new A(S.Uint32);
402
- curLocalM = -1;
403
- curWorldM = -1;
404
- rapid;
405
- constructor(t) {
406
- this.reset(), this.rapid = t;
407
- }
408
- save() {
409
- this.stack.push(this.curWorldM);
410
- const t = this.curWorldM;
411
- return this.curLocalM = this.matrix.alloc(), this.curWorldM = this.matrix.allocDirty(), this.stepWorldM.push(this.curWorldM), this.stepParentM.push(t), this.stepAction.push(1), this.stepClose.push(0), this.stepStack.push(this.step), this.matrix.copy(this.curWorldM, t), { world: this.curWorldM, local: this.curLocalM, step: this.step++ };
412
- }
413
- restore() {
414
- this.stack.length == 0 || this.stepStack.length == 0 || (this.curWorldM = this.stack.pop(), this.curLocalM = this.curWorldM - 1, this.stepParentM.push(this.stack.get(this.stack.length - 1)), this.stepWorldM.push(this.curWorldM), this.stepAction.push(0), this.stepClose.push(0), this.stepClose.typedArray[this.stepStack.pop()] = this.step, this.step++);
415
- }
416
- restoreAll() {
417
- for (; this.stack.length > 0 && this.stepStack.length > 0; )
418
- this.restore();
419
- }
420
- getStep(t) {
421
- return typeof t == "number" ? t : t.step;
422
- }
423
- walkSubtree(t, e, r = 1 / 0) {
424
- let i = 0, s = t;
425
- for (; s < this.step; ) {
426
- const a = this.stepAction.get(s);
427
- if (a === 1) {
428
- if (i++, i > r) {
429
- const h = this.stepClose.get(s);
430
- if (h > s) {
431
- s = h + 1, i--;
432
- continue;
433
- }
434
- }
435
- } else
436
- i--;
437
- if (e(s, a, i) === !1 || a === 0 && i === 0) return;
438
- s++;
439
- }
440
- }
441
- getParent(t) {
442
- const e = this.getStep(t);
443
- return this.stepParentM.get(e);
444
- }
445
- getChildren(t) {
446
- const e = this.getStep(t), r = this.stepClose.get(e), i = [];
447
- let s = e + 1;
448
- for (; s < r; )
449
- this.stepAction.get(s) === 1 ? (i.push(s), s = this.stepClose.get(s) + 1) : s++;
450
- return i;
451
- }
452
- updateMatrixSubtree(t) {
453
- const e = this.getStep(t);
454
- this.walkSubtree(e, (r, i) => {
455
- if (i === 1) {
456
- const s = this.stepWorldM.get(r), a = s - 1, h = this.stepParentM.get(r);
457
- this.matrix.multiplyOut(s, h, a);
458
- }
459
- });
460
- }
461
- translate(t, e) {
462
- this.matrix.translate(this.curLocalM, t, e), this.matrix.translate(this.curWorldM, t, e);
463
- }
464
- scale(t, e = t) {
465
- this.matrix.scale(this.curLocalM, t, e), this.matrix.scale(this.curWorldM, t, e);
466
- }
467
- rotate(t) {
468
- this.matrix.rotate(this.curLocalM, t), this.matrix.rotate(this.curWorldM, t);
469
- }
470
- rotateWithOffset(t, e, r) {
471
- this.matrix.rotateWithOffset(this.curLocalM, t, e, r), this.matrix.rotateWithOffset(this.curWorldM, t, e, r);
472
- }
473
- identity() {
474
- this.matrix.identity(this.curLocalM), this.matrix.identity(this.curWorldM);
475
- }
476
- reset() {
477
- this.matrix.reset(), this.stack.clear(), this.stepAction.clear(), this.stepWorldM.clear(), this.stepParentM.clear(), this.stepClose.clear(), this.stepStack.clear(), this.step = 0, this.curLocalM = this.matrix.alloc(), this.curWorldM = this.matrix.alloc();
478
- }
479
- localToWorld(t, e) {
480
- return this.matrix.localToWorld(this.curWorldM, t, e);
481
- }
482
- worldToLocal(t, e) {
483
- return this.matrix.worldToLocal(this.curWorldM, t, e);
484
- }
485
- getGlobalPosition() {
486
- return this.matrix.getPosition(this.curWorldM);
487
- }
488
- getGlobalScale() {
489
- return this.matrix.getScale(this.curWorldM);
490
- }
491
- getGlobalRotation() {
492
- return this.matrix.getRotation(this.curWorldM);
493
- }
494
- toCSSMatrix() {
495
- return this.matrix.toCSSMatrix(this.curWorldM);
496
- }
497
- getTransform() {
498
- const t = this.curWorldM * T;
499
- return this.matrix.data.slice(t, t + T);
500
- }
501
- setTransform(t) {
502
- const e = this.curWorldM * T, r = this.matrix.data;
503
- r[e] = t[0], r[e + 1] = t[1], r[e + 2] = t[2], r[e + 3] = t[3], r[e + 4] = t[4], r[e + 5] = t[5];
504
- }
505
- transformPoint(t, e) {
506
- return this.matrix.transformPoint(this.curLocalM, t, e);
507
- }
508
- applyTransform(t, e = 0, r = 0, i = -1) {
509
- let s = t.x ?? 0, a = t.y ?? 0;
510
- t.position && (s += t.position.x, a += t.position.y);
511
- let h = 1, o = 1;
512
- const c = t.scale;
513
- c && (typeof c == "number" ? (h = c, o = c) : (h = c.x, o = c.y));
514
- const u = t.rotation ?? 0;
515
- let l = t.offsetX ?? 0, d = t.offsetY ?? 0;
516
- t.offset && (l += t.offset.x, d += t.offset.y);
517
- const f = t.origin;
518
- f !== void 0 && (typeof f == "number" ? (l -= f * e, d -= f * r) : (l -= f.x * e, d -= f.y * r));
519
- const m = u ? Math.cos(u) : 1, g = u ? Math.sin(u) : 0, v = m * h, y = g * h, w = -g * o, M = m * o, _ = s + v * l + w * d, P = a + y * l + M * d;
520
- if (i !== -1) {
521
- this.matrix.multiplyAffine(
522
- this.curWorldM,
523
- i,
524
- v,
525
- y,
526
- w,
527
- M,
528
- _,
529
- P
530
- );
531
- return;
532
- }
533
- this.matrix.multiplyAffineInPlace(
534
- this.curLocalM,
535
- v,
536
- y,
537
- w,
538
- M,
539
- _,
540
- P
541
- ), this.matrix.multiplyAffineInPlace(
542
- this.curWorldM,
543
- v,
544
- y,
545
- w,
546
- M,
547
- _,
548
- P
549
- );
550
- }
551
- }
552
- var q = /* @__PURE__ */ ((n) => (n[n.REPEAT = 0] = "REPEAT", n[n.CLAMP = 1] = "CLAMP", n[n.MIRRORED_REPEAT = 2] = "MIRRORED_REPEAT", n))(q || {});
553
- class Ut {
554
- render;
555
- cache = /* @__PURE__ */ new Map();
556
- constructor(t) {
557
- this.render = t;
558
- }
559
- async load(t, e) {
560
- let r = this.cache.get(t);
561
- if (r)
562
- return new N(r);
563
- try {
564
- const i = await this._fetchImage(t);
565
- return r = V.fromSource(this.render, i, e), r.uid = t, this.cache.set(t, r), new N(r);
566
- } catch (i) {
567
- throw console.error(`[TextureManager] Failed to load: ${t}`, i), i;
568
- }
569
- }
570
- create(t, e) {
571
- if (e?.key && this.cache.has(e.key))
572
- return new N(this.cache.get(e.key));
573
- const r = V.fromSource(this.render, t, e);
574
- return e?.key && (r.uid = e.key, this.cache.set(e.key, r)), new N(r);
575
- }
576
- createRenderTexture(t) {
577
- return new Ct(this.render, t);
578
- }
579
- createTextTexture(t) {
580
- return new Pt(this.render, t);
581
- }
582
- destroy(t, e = !1) {
583
- let r, i;
584
- typeof t == "string" ? (i = t, r = this.cache.get(i)) : t instanceof N ? (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));
585
- }
586
- destroyAll() {
587
- for (const t of this.cache.values())
588
- t.destroy(this.render.gl);
589
- this.cache.clear();
590
- }
591
- _fetchImage(t) {
592
- return new Promise((e, r) => {
593
- const i = new Image();
594
- i.crossOrigin = "anonymous", i.onload = () => e(i), i.onerror = () => r(new Error(`Failed to load image resource: ${t}`)), i.src = t;
595
- });
596
- }
597
- }
598
- class V {
599
- glTexture = null;
600
- width;
601
- height;
602
- uid;
603
- refCount = 0;
604
- constructor(t, e, r) {
605
- this.glTexture = t, this.width = e, this.height = r;
606
- }
607
- static fromSource(t, e, r = {}) {
608
- const i = J(t, e, r);
609
- return new V(i, e.width, e.height);
610
- }
611
- updateSource(t, e, r = {}) {
612
- this.glTexture && (t.bindTexture(t.TEXTURE_2D, this.glTexture), r.textureFilter !== void 0 && nt(t, r.textureFilter), r.wrap !== void 0 && st(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));
613
- }
614
- setFilterMode(t, e) {
615
- this.glTexture && (t.bindTexture(t.TEXTURE_2D, this.glTexture), nt(t, e), t.bindTexture(t.TEXTURE_2D, null));
616
- }
617
- setWrapMode(t, e) {
618
- this.glTexture && (t.bindTexture(t.TEXTURE_2D, this.glTexture), st(t, e), t.bindTexture(t.TEXTURE_2D, null));
619
- }
620
- destroy(t) {
621
- this.glTexture && (t.deleteTexture(this.glTexture), this.glTexture = null);
622
- }
623
- }
624
- class N {
625
- base;
626
- uvX = 0;
627
- uvY = 0;
628
- uvW = 1;
629
- uvH = 1;
630
- rawWidth = 0;
631
- rawHeight = 0;
632
- offsetX = 0;
633
- offsetY = 0;
634
- scale = 1;
635
- isAtlas = !1;
636
- flipY = !1;
637
- isRotated = !1;
638
- glTexture = null;
639
- get width() {
640
- return this.isRotated ? this.rawHeight : this.rawWidth;
641
- }
642
- get height() {
643
- return this.isRotated ? this.rawWidth : this.rawHeight;
644
- }
645
- // Stored pixel-space region (before UV conversion), used by getSubTexture()
646
- _px = 0;
647
- _py = 0;
648
- constructor(t) {
649
- t && this.setBase(t);
650
- }
651
- setBase(t) {
652
- 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);
255
+ setBase(t) {
256
+ 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);
653
257
  }
654
258
  setRegion(t, e, r, i) {
655
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;
@@ -665,32 +269,31 @@ class N {
665
269
  }
666
270
  splitGrid(t, e, r, i, s = 0) {
667
271
  if (!this.base) return [];
668
- const a = [], h = this.rawWidth, o = this.rawHeight, c = Math.max(0, s), u = t + c, l = e + c, d = r ?? Math.floor((h + c) / u), f = i ?? Math.floor((o + c) / l);
669
- for (let m = 0; m < f; m++)
670
- for (let g = 0; g < d; g++)
671
- a.push(this.getSubTexture(g * u, m * l, t, e));
272
+ const a = [], h = this.rawWidth, o = this.rawHeight, c = Math.max(0, s), u = t + c, l = e + c, f = r ?? Math.floor((h + c) / u), d = i ?? Math.floor((o + c) / l);
273
+ for (let x = 0; x < d; x++)
274
+ for (let p = 0; p < f; p++)
275
+ a.push(this.getSubTexture(p * u, x * l, t, e));
672
276
  return a;
673
277
  }
674
278
  static fromImageSource(t, e, r = {}) {
675
- const i = J(t, e, r);
676
- return new N(new V(i, e.width, e.height));
279
+ const i = rt(t, e, r);
280
+ return new N(new G(i, e.width, e.height));
677
281
  }
678
282
  destroy() {
679
283
  this.base && (this.base.refCount--, this.base = void 0), this.glTexture = null;
680
284
  }
681
285
  }
682
- class Ct extends N {
286
+ class Ut extends N {
683
287
  framebuffer;
684
288
  renderbuffer;
685
289
  gl;
686
- flipY = !0;
687
290
  clearColor;
688
291
  _allocW = 0;
689
292
  _allocH = 0;
690
293
  constructor(t, e) {
691
- super(), this.gl = t.gl, this.clearColor = e.clearColor ?? x.Black;
692
- const r = Math.max(Math.round(e.width), 1), i = Math.max(Math.round(e.height), 1), a = J(t, { width: r, height: i }, { ...e, onlySize: !0 });
693
- if (this.setBase(new V(a, r, i)), this.framebuffer = this.gl.createFramebuffer(), this.renderbuffer = this.gl.createRenderbuffer(), !this.framebuffer || !this.renderbuffer)
294
+ super(), this.gl = t.gl, this.clearColor = e.clearColor ?? m.Black;
295
+ const r = Math.max(Math.round(e.width), 1), i = Math.max(Math.round(e.height), 1), a = rt(t, { width: r, height: i }, { ...e, onlySize: !0 });
296
+ if (this.setBase(new G(a, r, i)), this.framebuffer = this.gl.createFramebuffer(), this.renderbuffer = this.gl.createRenderbuffer(), !this.framebuffer || !this.renderbuffer)
694
297
  throw new Error("Failed to create Framebuffer or Renderbuffer");
695
298
  this.resize(r, i, !0);
696
299
  }
@@ -721,20 +324,20 @@ class Ct extends N {
721
324
  if (h <= 0 || o <= 0) return new Uint8Array(0);
722
325
  if (s < 0 || a < 0 || s >= this.rawWidth || a >= this.rawHeight)
723
326
  return new Uint8Array(0);
724
- const c = Math.min(h, this.rawWidth - s), u = Math.min(o, this.rawHeight - a), l = this.rawHeight - a - u, d = this.gl, f = d.getParameter(d.FRAMEBUFFER_BINDING);
725
- d.bindFramebuffer(d.FRAMEBUFFER, this.framebuffer);
726
- const m = new Uint8Array(c * u * 4);
727
- return d.readPixels(s, l, c, u, d.RGBA, d.UNSIGNED_BYTE, m), d.bindFramebuffer(d.FRAMEBUFFER, f), m;
327
+ const c = Math.min(h, this.rawWidth - s), u = Math.min(o, this.rawHeight - a), l = this.gl, f = l.getParameter(l.FRAMEBUFFER_BINDING);
328
+ l.bindFramebuffer(l.FRAMEBUFFER, this.framebuffer);
329
+ const d = new Uint8Array(c * u * 4);
330
+ return l.readPixels(s, a, c, u, l.RGBA, l.UNSIGNED_BYTE, d), l.bindFramebuffer(l.FRAMEBUFFER, f), d;
728
331
  }
729
332
  GetColorAt(t, e) {
730
333
  const r = this.GetPixels(t, e, 1, 1);
731
- return r.length < 4 ? new x(0, 0, 0, 0) : new x(r[0], r[1], r[2], r[3]);
334
+ return r.length < 4 ? new m(0, 0, 0, 0) : new m(r[0], r[1], r[2], r[3]);
732
335
  }
733
336
  destroy() {
734
337
  super.destroy(), this.framebuffer && this.gl.deleteFramebuffer(this.framebuffer), this.renderbuffer && this.gl.deleteRenderbuffer(this.renderbuffer), this.base?.destroy(this.gl);
735
338
  }
736
339
  }
737
- const _t = {
340
+ const Ct = {
738
341
  fontFamily: "Arial",
739
342
  fontSize: 24,
740
343
  fontWeight: "normal",
@@ -743,7 +346,7 @@ const _t = {
743
346
  align: "left",
744
347
  lineHeight: 1
745
348
  };
746
- class Pt extends N {
349
+ class _t extends N {
747
350
  canvas;
748
351
  ctx;
749
352
  render;
@@ -752,12 +355,12 @@ class Pt extends N {
752
355
  options;
753
356
  flipY = !0;
754
357
  constructor(t, e) {
755
- super(), this.render = t, this._style = { ..._t, ...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 = { premultipliedAlpha: t.premultipliedAlpha, ...e }, this.scale = 1 / t.dpr, this.canvas = document.createElement("canvas");
756
359
  const r = this.canvas.getContext("2d", { willReadFrequently: !0 });
757
360
  if (!r) throw new Error("Failed to get 2d context for TextTexture");
758
361
  this.ctx = r, this.canvas.width = 1, this.canvas.height = 1;
759
- const i = J(t, this.canvas, this.options);
760
- this.setBase(new V(i, 1, 1)), this.update();
362
+ const i = rt(t, this.canvas, this.options);
363
+ this.setBase(new G(i, 1, 1)), this.update();
761
364
  }
762
365
  get text() {
763
366
  return this._text;
@@ -787,20 +390,20 @@ class Pt extends N {
787
390
  const c = this._text.split(`
788
391
  `);
789
392
  let u = 0, l = 0;
790
- const m = t.measureText(c[0] || "M").fontBoundingBoxDescent;
393
+ const x = t.measureText(c[0] || "M").fontBoundingBoxDescent;
791
394
  for (const b of c) {
792
- const R = t.measureText(b);
793
- R.width > u && (u = R.width), l += m * a;
395
+ const M = t.measureText(b);
396
+ M.width > u && (u = M.width), l += x * a;
794
397
  }
795
- l -= m * (a - 1);
796
- const g = this._style.strokeThickness || 0, v = Math.ceil(u + g * 2) || 1, y = Math.ceil(l + g * 2) || 1, w = Math.ceil(v * o), M = Math.ceil(y * o);
797
- (this.canvas.width !== w || this.canvas.height !== M) && (this.canvas.width = w, this.canvas.height = M), t.setTransform(o, 0, 0, o, 0, 0), t.clearRect(0, 0, v, y), t.font = h, t.textBaseline = "top", t.textAlign = this._style.align;
798
- const _ = this.updateOffset(v, g), P = e.strokeThickness, O = P > 0;
799
- O && (t.lineWidth = P, t.strokeStyle = e.stroke, t.lineJoin = "round"), e.fill && (t.fillStyle = e.fill);
800
- let W = g;
398
+ l -= x * (a - 1);
399
+ const p = this._style.strokeThickness || 0, v = Math.ceil(u + p * 2) || 1, y = Math.ceil(l + p * 2) || 1, E = Math.ceil(v * o), w = Math.ceil(y * o);
400
+ (this.canvas.width !== E || this.canvas.height !== w) && (this.canvas.width = E, this.canvas.height = w), t.setTransform(o, 0, 0, o, 0, 0), t.clearRect(0, 0, v, y), t.font = h, t.textBaseline = "top", t.textAlign = this._style.align;
401
+ const _ = this.updateOffset(v, p), L = e.strokeThickness, W = L > 0;
402
+ W && (t.lineWidth = L, t.strokeStyle = e.stroke, t.lineJoin = "round"), e.fill && (t.fillStyle = e.fill);
403
+ let O = p;
801
404
  for (const b of c)
802
- O && t.strokeText(b, _, W), e.fill && t.fillText(b, _, W), W += m * a;
803
- this.base?.updateSource(this.render.gl, this.canvas, this.options), this.setRegion(0, 0, w, M);
405
+ W && t.strokeText(b, _, O), e.fill && t.fillText(b, _, O), O += x * a;
406
+ this.base?.updateSource(this.render.gl, this.canvas, this.options), this.setRegion(0, 0, E, w);
804
407
  }
805
408
  }
806
409
  const Lt = (n, t = !1, e = !0) => {
@@ -808,7 +411,7 @@ const Lt = (n, t = !1, e = !0) => {
808
411
  if (!r)
809
412
  throw new Error("WebGL2 is not supported in this browser.");
810
413
  return r;
811
- }, ct = (n, t, e) => {
414
+ }, dt = (n, t, e) => {
812
415
  const r = n.createShader(e);
813
416
  if (!r)
814
417
  throw new Error("Unable to create webgl shader");
@@ -817,8 +420,8 @@ const Lt = (n, t = !1, e = !0) => {
817
420
  throw console.error("Shader compilation failed:", s), new Error("Unable to compile shader: " + s + t);
818
421
  }
819
422
  return r;
820
- }, Nt = (n, t, e) => {
821
- var r = n.createProgram(), i = ct(n, t, 35633), s = ct(n, e, 35632);
423
+ }, Pt = (n, t, e) => {
424
+ var r = n.createProgram(), i = dt(n, t, 35633), s = dt(n, e, 35632);
822
425
  if (!r)
823
426
  throw new Error("Unable to create program shader");
824
427
  if (n.attachShader(r, i), n.attachShader(r, s), n.linkProgram(r), !n.getProgramParameter(r, n.LINK_STATUS)) {
@@ -827,7 +430,7 @@ const Lt = (n, t = !1, e = !0) => {
827
430
  }
828
431
  return r;
829
432
  };
830
- function st(n, t) {
433
+ function at(n, t) {
831
434
  const e = {
832
435
  [q.CLAMP]: n.CLAMP_TO_EDGE,
833
436
  [q.REPEAT]: n.REPEAT,
@@ -835,18 +438,18 @@ function st(n, t) {
835
438
  }[t] || n.CLAMP_TO_EDGE;
836
439
  n.texParameteri(n.TEXTURE_2D, n.TEXTURE_WRAP_S, e), n.texParameteri(n.TEXTURE_2D, n.TEXTURE_WRAP_T, e);
837
440
  }
838
- function nt(n, t) {
441
+ function ht(n, t) {
839
442
  const e = {
840
- [ht.LINEAR]: n.LINEAR,
841
- [ht.NEAREST]: n.NEAREST
443
+ [tt.LINEAR]: n.LINEAR,
444
+ [tt.NEAREST]: n.NEAREST
842
445
  }[t] ?? n.NEAREST;
843
446
  n.texParameteri(n.TEXTURE_2D, n.TEXTURE_MIN_FILTER, e), n.texParameteri(n.TEXTURE_2D, n.TEXTURE_MAG_FILTER, e);
844
447
  }
845
- function J(n, t, e) {
448
+ function rt(n, t, e) {
846
449
  const r = n.gl, i = r.createTexture();
847
450
  if (!i)
848
451
  throw new Error("Unable to create WebGL texture");
849
- if (r.bindTexture(r.TEXTURE_2D, i), r.pixelStorei(r.UNPACK_FLIP_Y_WEBGL, 0), r.pixelStorei(r.UNPACK_ALIGNMENT, 1), st(r, e.wrap ?? q.CLAMP), nt(r, e.textureFilter ?? n.textureFilter), e.onlySize) {
452
+ if (r.bindTexture(r.TEXTURE_2D, i), r.pixelStorei(r.UNPACK_FLIP_Y_WEBGL, 0), r.pixelStorei(r.UNPACK_ALIGNMENT, 1), at(r, e.wrap ?? q.CLAMP), ht(r, e.textureFilter ?? n.textureFilter), e.onlySize) {
850
453
  const s = t;
851
454
  r.texImage2D(r.TEXTURE_2D, 0, r.RGBA, s.width, s.height, 0, r.RGBA, r.UNSIGNED_BYTE, null);
852
455
  } else {
@@ -855,17 +458,17 @@ function J(n, t, e) {
855
458
  }
856
459
  return r.bindTexture(r.TEXTURE_2D, null), i;
857
460
  }
858
- function B(n, t) {
859
- if (n.includes("%TEXTURE_NUM%") && (n = n.replace("%TEXTURE_NUM%", t.toString())), n.includes("%GET_COLOR%")) {
860
- let e = "";
861
- for (let r = 0; r < t; r++)
862
- r == 0 ? e += `if(vTextureId == ${r})` : r == t - 1 ? e += "else" : e += `else if(vTextureId == ${r})`, e += `{return texture(uTextures[${r}], uv);}`;
863
- n = n.replace("%GET_COLOR%", e);
461
+ function K(n, t, e = "vTextureId", r = "uv", i = "return") {
462
+ if (n.includes("%TEXTURE_NUM%") && (n = n.replaceAll("%TEXTURE_NUM%", t.toString())), n.includes("%GET_COLOR%")) {
463
+ let s = "";
464
+ for (let a = 0; a < t; a++)
465
+ a == 0 ? s += `if(${e} == ${a})` : a == t - 1 ? s += "else" : s += `else if(${e} == ${a})`, s += `{${i} texture(uTextures[${a}], ${r});}`;
466
+ n = n.replaceAll("%GET_COLOR%", s);
864
467
  }
865
468
  return n;
866
469
  }
867
- const It = 5126, at = 5121;
868
- function Ot(n, t, e) {
470
+ const Nt = 5126, ot = 5121;
471
+ function It(n, t, e) {
869
472
  n.vertexAttribDivisor(t, e);
870
473
  }
871
474
  function yt(n, t, e, r, i) {
@@ -885,7 +488,7 @@ class Tt {
885
488
  // for debug
886
489
  vao;
887
490
  constructor(t, e, r, i) {
888
- this.gl = t, this.program = Nt(t, e, r), this.parseShader(e), this.parseShader(r), this.vao = t.createVertexArray(), i && this.setAttributes(i);
491
+ this.gl = t, this.program = Pt(t, e, r), this.parseShader(e), this.parseShader(r), this.vao = t.createVertexArray(), i && this.setAttributes(i);
889
492
  }
890
493
  use() {
891
494
  this.gl.useProgram(this.program);
@@ -901,7 +504,7 @@ class Tt {
901
504
  const r = this.uniformLoc[t];
902
505
  if (r === void 0) return;
903
506
  const i = this.uniformType[t], s = this.uniformIsArray[t];
904
- Ft(this.gl, r, i, e, s);
507
+ Ot(this.gl, r, i, e, s);
905
508
  }
906
509
  setUniforms(t) {
907
510
  for (const e in t)
@@ -915,11 +518,11 @@ class Tt {
915
518
  r.vertexAttribPointer(
916
519
  e,
917
520
  t.size,
918
- t.type ?? It,
521
+ t.type ?? Nt,
919
522
  t.normalized ?? !1,
920
523
  t.stride,
921
524
  t.offset ?? 0
922
- ), r.enableVertexAttribArray(e), Ot(r, e, t.divisor ?? 0);
525
+ ), r.enableVertexAttribArray(e), It(r, e, t.divisor ?? 0);
923
526
  }
924
527
  setAttributes(t) {
925
528
  for (const e of t)
@@ -945,11 +548,11 @@ class Tt {
945
548
  }
946
549
  }
947
550
  }
948
- function Ft(n, t, e, r, i = !1) {
551
+ function Ot(n, t, e, r, i = !1) {
949
552
  switch (e) {
950
553
  // scalars
951
554
  case "float":
952
- return n.uniform1f(t, r);
555
+ return i ? n.uniform1fv(t, r) : n.uniform1f(t, r);
953
556
  case "int":
954
557
  case "bool":
955
558
  case "sampler2D":
@@ -979,12 +582,14 @@ function Ft(n, t, e, r, i = !1) {
979
582
  return n.uniformMatrix3fv(t, !1, r);
980
583
  case "mat4":
981
584
  return n.uniformMatrix4fv(t, !1, r);
585
+ case "mat3x2":
586
+ return n.uniformMatrix3x2fv(t, !1, r);
982
587
  // fallback: guess by value shape
983
588
  default:
984
- return Bt(n, t, r);
589
+ return Ft(n, t, r);
985
590
  }
986
591
  }
987
- function Bt(n, t, e) {
592
+ function Ft(n, t, e) {
988
593
  if (typeof e == "number")
989
594
  return n.uniform1f(t, e);
990
595
  const r = e;
@@ -1003,27 +608,29 @@ function Bt(n, t, e) {
1003
608
  return n.uniform1fv(t, r);
1004
609
  }
1005
610
  }
1006
- class ut {
611
+ class ct {
1007
612
  vs;
1008
613
  fs;
1009
614
  glshader = /* @__PURE__ */ new Map();
1010
615
  uniforms = {};
1011
616
  uniformDirty = /* @__PURE__ */ new Set();
1012
617
  uniformTextures = {};
618
+ uniformArrayTexture = {};
1013
619
  usedTextureUnitNum = 0;
1014
620
  padding = 0;
1015
621
  rapid;
1016
- constructor(t, e, r, i = 0, s) {
622
+ prefix = [];
623
+ constructor(t, e = "", r = "", i = 0, s) {
1017
624
  this.vs = e, this.fs = r, this.rapid = t, this.uniforms = s ?? {}, this.usedTextureUnitNum = i;
1018
625
  }
1019
626
  setUniforms(t) {
1020
627
  this.rapid.flush();
1021
- const e = {}, r = {};
1022
- for (const [i, s] of Object.entries(t))
1023
- s instanceof WebGLTexture ? r[i] = s : e[i] = s;
1024
- Object.assign(this.uniforms, e), Object.assign(this.uniformTextures, r);
1025
- for (const i of this.glshader.keys())
1026
- this.uniformDirty.add(i);
628
+ const e = {}, r = {}, i = {};
629
+ for (const [s, a] of Object.entries(t))
630
+ a instanceof WebGLTexture ? r[s] = a : Array.isArray(a) && a[0] instanceof WebGLTexture ? i[s] = a : e[s] = a;
631
+ Object.assign(this.uniforms, e), Object.assign(this.uniformTextures, r), Object.assign(this.uniformArrayTexture, i);
632
+ for (const s of this.glshader.keys())
633
+ this.uniformDirty.add(s);
1027
634
  }
1028
635
  setPadding(t) {
1029
636
  this.padding = t;
@@ -1035,14 +642,18 @@ class ut {
1035
642
  const r = this.glshader.get(t);
1036
643
  r && (Object.keys(e).length > 0 && r.setUniforms(e), this.uniformDirty.has(t) && (r.setUniforms(this.uniforms), this.uniformDirty.delete(t)));
1037
644
  }
645
+ replaceCustomCode(t, e) {
646
+ return t = t.replace("// CUSTOM_CODE", e + "// CUSTOM_CODE"), t;
647
+ }
1038
648
  getGLShader(t, e, r, i) {
1039
649
  if (this.glshader.has(e))
1040
650
  return this.glshader.get(e);
1041
651
  if (i == null || r == null)
1042
652
  return console.warn("CustomGlShader: missing base shader for " + e), null;
1043
- r = r.replace("// CUSTOM_CODE_CALL", "vertex(position, vRegion);"), i = i.replace("// CUSTOM_CODE_CALL", "fragment(fragColor);"), r = r.replace("// CUSTOM_CODE", this.vs), i = i.replace("// CUSTOM_CODE", this.fs);
1044
- const s = t.createShader(r, i);
1045
- return s.isCustom = !0, s.padding = this.padding, this.glshader.set(e, s), this.uniformDirty.add(e), s;
653
+ const s = this.prefix.length == 0 ? [""] : this.prefix;
654
+ 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);
655
+ const a = t.createShader(r, i);
656
+ return a.isCustom = !0, a.padding = this.padding, this.glshader.set(e, a), this.uniformDirty.add(e), a;
1046
657
  }
1047
658
  destroy() {
1048
659
  this.glshader.forEach((t) => t.destroy());
@@ -1065,6 +676,7 @@ class Et {
1065
676
  fs = "";
1066
677
  usedTextures = [];
1067
678
  usedTexturePadding = [];
679
+ extraTextureUnit = 0;
1068
680
  createBuffer() {
1069
681
  }
1070
682
  createDefaultShader() {
@@ -1078,75 +690,211 @@ class Et {
1078
690
  (t, e) => e
1079
691
  );
1080
692
  }
1081
- findTextureUnit(t, e = 0, r = 0) {
1082
- for (let i = 0; i < this.usedTextures.length; i++) {
1083
- const s = i * 2;
1084
- if (this.usedTextures[i] === t && this.usedTexturePadding[s] === e && this.usedTexturePadding[s + 1] === r)
1085
- return i;
693
+ findTextureUnit(t, e = 0, r = 0) {
694
+ for (let i = 0; i < this.usedTextures.length; i++) {
695
+ const s = i * 2;
696
+ if (this.usedTextures[i] === t && this.usedTexturePadding[s] === e && this.usedTexturePadding[s + 1] === r)
697
+ return i;
698
+ }
699
+ return -1;
700
+ }
701
+ useTexture(t, e = 0, r = 0) {
702
+ const i = this.findTextureUnit(t, e, r);
703
+ 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;
704
+ }
705
+ getMaxTexutreUnit(t) {
706
+ return this.rapid.maxTextureUnits - this.extraTextureUnit - (t?.usedTextureUnitNum ?? 0);
707
+ }
708
+ createShader(t, e) {
709
+ const r = this.getMaxTexutreUnit();
710
+ return e = K(e, r), t = K(t, r), new Tt(this.gl, t, e);
711
+ }
712
+ createCustomShader(t) {
713
+ const e = this.getMaxTexutreUnit(t), r = K(this.fs, e), i = K(this.vs, e);
714
+ return t.getGLShader(this, this.KEY, i, r);
715
+ }
716
+ getCustomShader(t) {
717
+ if (!t)
718
+ return this.defaultShader;
719
+ if (t instanceof ct) {
720
+ const e = this.createCustomShader(t);
721
+ return e ?? this.defaultShader;
722
+ }
723
+ return t;
724
+ }
725
+ isSameShader(t) {
726
+ return this.isDefaultShader && !t ? !0 : this.getCustomShader(t) == this.currentShader;
727
+ }
728
+ enter(t) {
729
+ this.resetRender(), this.currentShader = this.getCustomShader(t), t instanceof ct ? (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);
730
+ }
731
+ exit() {
732
+ this.hasPendingContent() && this.flush(), this.gl.bindVertexArray(null);
733
+ }
734
+ flush() {
735
+ this.render(), this.resetRender();
736
+ }
737
+ render() {
738
+ this.prepareRender();
739
+ }
740
+ prepareRender() {
741
+ const t = this.currentShader;
742
+ if (t.use(), this.customShader) {
743
+ const r = this.customShader, i = {};
744
+ for (const [s, a] of Object.entries(r.uniformTextures))
745
+ i[s] = this.useTexture(a);
746
+ for (const [s, a] of Object.entries(r.uniformArrayTexture))
747
+ i[s] = a.map((h) => this.useTexture(h));
748
+ this.customShader.applyUniform(this.KEY, i);
749
+ }
750
+ const e = this.gl;
751
+ for (let r = 0; r < this.usedTextures.length; r++)
752
+ e.activeTexture(e.TEXTURE0 + r), e.bindTexture(e.TEXTURE_2D, this.usedTextures[r]);
753
+ t.setUniform("uTextures", Int32Array.from(
754
+ { length: this.usedTextures.length },
755
+ (r, i) => i
756
+ )), t.setUniform("uPadding", Float32Array.from(
757
+ { length: this.usedTexturePadding.length },
758
+ (r, i) => this.usedTexturePadding[i]
759
+ ));
760
+ }
761
+ resetRender() {
762
+ const t = this.gl;
763
+ for (let e = 0; e < this.usedTextures.length; e++)
764
+ t.activeTexture(t.TEXTURE0 + e), t.bindTexture(t.TEXTURE_2D, null);
765
+ this.usedTextures.length = 0, this.usedTexturePadding.length = 0;
766
+ }
767
+ hasPendingContent() {
768
+ return !1;
769
+ }
770
+ }
771
+ var A = /* @__PURE__ */ ((n) => (n[n.Float32 = 0] = "Float32", n[n.Uint32 = 1] = "Uint32", n[n.Uint16 = 2] = "Uint16", n))(A || {});
772
+ class S {
773
+ usedElemNum;
774
+ typedArray;
775
+ arrayType;
776
+ maxElemNum;
777
+ bytePerElem;
778
+ arraybuffer;
779
+ uint32;
780
+ float32;
781
+ uint16;
782
+ constructor(t) {
783
+ 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();
784
+ }
785
+ getArrayType(t) {
786
+ switch (t) {
787
+ case 0:
788
+ return Float32Array;
789
+ case 1:
790
+ return Uint32Array;
791
+ case 2:
792
+ return Uint16Array;
793
+ default:
794
+ throw new Error("Unsupported ArrayType");
795
+ }
796
+ }
797
+ updateTypedArray() {
798
+ switch (this.uint32 = new Uint32Array(this.arraybuffer), this.float32 = new Float32Array(this.arraybuffer), this.uint16 = new Uint16Array(this.arraybuffer), this.arrayType) {
799
+ case 0:
800
+ this.typedArray = this.float32;
801
+ break;
802
+ case 1:
803
+ this.typedArray = this.uint32;
804
+ break;
805
+ case 2:
806
+ this.typedArray = this.uint16;
807
+ break;
808
+ }
809
+ }
810
+ clear() {
811
+ this.usedElemNum = 0;
812
+ }
813
+ static removeAtIndices(t, e) {
814
+ const r = Math.max(...e.map((h) => h.usedElemNum)), i = [...new Set(t)].filter(
815
+ (h) => h >= 0 && h < r
816
+ ).sort((h, o) => h - o);
817
+ if (i.length === 0)
818
+ return 0;
819
+ let s = 0, a = 0;
820
+ for (let h = 0; h < r; h++) {
821
+ if (a < i.length && h === i[a]) {
822
+ a++;
823
+ continue;
824
+ }
825
+ if (s !== h)
826
+ for (const o of e)
827
+ o.typedArray[s] = o.typedArray[h];
828
+ s++;
829
+ }
830
+ for (const h of e)
831
+ h.usedElemNum = s;
832
+ return r - s;
833
+ }
834
+ resize(t = 0) {
835
+ if (t += this.usedElemNum, t > this.maxElemNum) {
836
+ for (; t > this.maxElemNum; )
837
+ this.maxElemNum *= 2;
838
+ return this.setMaxSize(this.maxElemNum), !0;
1086
839
  }
1087
- return -1;
840
+ return !1;
1088
841
  }
1089
- useTexture(t, e = 0, r = 0) {
1090
- const i = this.findTextureUnit(t, e, r);
1091
- 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;
842
+ setMaxSize(t = this.maxElemNum) {
843
+ const e = this.typedArray;
844
+ this.maxElemNum = t, this.arraybuffer = new ArrayBuffer(t * this.bytePerElem), this.updateTypedArray(), this.typedArray.set(e);
1092
845
  }
1093
- createShader(t, e) {
1094
- return new Tt(this.gl, t, e);
846
+ pushUint32(t) {
847
+ this.resize(1), this.uint32[this.usedElemNum++] = t;
1095
848
  }
1096
- createCustomShader(t) {
1097
- return t.getGLShader(this, this.KEY, this.vs, this.fs);
849
+ pushFloat32(t) {
850
+ this.resize(1), this.float32[this.usedElemNum++] = t;
1098
851
  }
1099
- getCustomShader(t) {
1100
- if (!t)
1101
- return this.defaultShader;
1102
- if (t instanceof ut) {
1103
- const e = this.createCustomShader(t);
1104
- return e ?? this.defaultShader;
1105
- }
1106
- return t;
852
+ push(t) {
853
+ this.resize(1), this.typedArray[this.usedElemNum++] = t;
1107
854
  }
1108
- isSameShader(t) {
1109
- return this.isDefaultShader && !t ? !0 : this.getCustomShader(t) == this.currentShader;
855
+ pop() {
856
+ return this.typedArray[--this.usedElemNum];
1110
857
  }
1111
- enter(t) {
1112
- this.resetRender(), this.currentShader = this.getCustomShader(t), t instanceof ut ? (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);
858
+ top() {
859
+ return this.typedArray[this.usedElemNum - 1];
1113
860
  }
1114
- exit() {
1115
- this.hasPendingContent() && this.flush(), this.gl.bindVertexArray(null);
861
+ get(t) {
862
+ return this.typedArray[t];
1116
863
  }
1117
- flush() {
1118
- this.render(), this.resetRender();
864
+ getArray(t = 0, e) {
865
+ return e === void 0 ? this.typedArray : this.typedArray.subarray(t, e);
1119
866
  }
1120
- render() {
1121
- this.prepareRender();
867
+ get length() {
868
+ return this.usedElemNum;
1122
869
  }
1123
- prepareRender() {
1124
- const t = this.currentShader;
1125
- if (t.use(), this.customShader) {
1126
- const r = this.customShader, i = {};
1127
- for (const s in r.uniformTextures)
1128
- i[s] = this.useTexture(r.uniformTextures[s]);
1129
- this.customShader.applyUniform(this.KEY, i);
1130
- }
1131
- const e = this.gl;
1132
- for (let r = 0; r < this.usedTextures.length; r++)
1133
- e.activeTexture(e.TEXTURE0 + r), e.bindTexture(e.TEXTURE_2D, this.usedTextures[r]);
1134
- t.setUniform("uTextures", Int32Array.from(
1135
- { length: this.usedTextures.length },
1136
- (r, i) => i
1137
- )), t.setUniform("uPadding", Float32Array.from(
1138
- { length: this.usedTexturePadding.length },
1139
- (r, i) => this.usedTexturePadding[i]
1140
- ));
870
+ }
871
+ class ut extends S {
872
+ buffer;
873
+ gl;
874
+ dirty = !0;
875
+ type;
876
+ webglBufferSize = 0;
877
+ usage;
878
+ constructor(t, e, r = t.ARRAY_BUFFER, i = t.DYNAMIC_DRAW) {
879
+ super(e), this.gl = t, this.buffer = t.createBuffer(), this.type = r, this.usage = i;
1141
880
  }
1142
- resetRender() {
1143
- this.usedTextures.length = 0, this.usedTexturePadding.length = 0;
881
+ makeDirty() {
882
+ this.dirty = !0;
1144
883
  }
1145
- hasPendingContent() {
1146
- return !1;
884
+ clear() {
885
+ super.clear(), this.dirty = !1;
886
+ }
887
+ bindBuffer() {
888
+ this.gl.bindBuffer(this.type, this.buffer);
889
+ }
890
+ bufferData() {
891
+ if (this.dirty) {
892
+ const t = this.gl;
893
+ 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;
894
+ }
1147
895
  }
1148
896
  }
1149
- const Z = `#version 300 es\r
897
+ const et = `#version 300 es\r
1150
898
  precision highp float;\r
1151
899
  \r
1152
900
  // per-vertex\r
@@ -1194,7 +942,7 @@ void main(void) {\r
1194
942
  \r
1195
943
  gl_Position = u_projection * position;\r
1196
944
  }\r
1197
- `, lt = `#version 300 es\r
945
+ `, ft = `#version 300 es\r
1198
946
  precision mediump float;\r
1199
947
  \r
1200
948
  uniform sampler2D uTextures[%TEXTURE_NUM%];\r
@@ -1208,43 +956,31 @@ in vec4 vUVRect;\r
1208
956
  out vec4 fragColor;\r
1209
957
  in vec2 vPadding;\r
1210
958
  \r
1211
- bool clampUV(vec2 uv) {\r
1212
- return uv.x < vUVRect.x || uv.x > vUVRect.z || uv.y < vUVRect.y || uv.y > vUVRect.w;\r
1213
- }\r
1214
- \r
1215
959
  vec4 sampleTexture(vec2 uv) {\r
1216
960
  %GET_COLOR%\r
1217
961
  }\r
1218
962
  \r
1219
963
  vec4 sampleClampTexture(vec2 uv) {\r
1220
- if (clampUV(uv)) {\r
1221
- return vec4(0.0, 0.0, 0.0, 0.0);\r
1222
- }\r
1223
- return sampleTexture(uv);\r
964
+ vec2 inMin = step(vUVRect.xy, uv);\r
965
+ vec2 inMax = step(uv, vUVRect.zw);\r
966
+ \r
967
+ float mask = inMin.x * inMin.y * inMax.x * inMax.y;\r
968
+ \r
969
+ return sampleTexture(uv) * mask;\r
1224
970
  }\r
1225
971
  \r
1226
972
  vec4 sampleTextureLocal(vec2 uv){\r
1227
- vec2 gUV = mix(vUVRect.xy, vUVRect.zw, uv);\r
1228
- if (clampUV(gUV)) {\r
1229
- return vec4(0.0, 0.0, 0.0, 0.0);\r
1230
- }\r
1231
- return sampleTexture(gUV);\r
973
+ return sampleClampTexture(mix(vUVRect.xy, vUVRect.zw, uv));\r
1232
974
  }\r
1233
- \r
1234
975
  // CUSTOM_CODE\r
1235
976
  \r
1236
977
  void main(void) {\r
1237
978
  fragColor = sampleTexture(vRegion) * vColor;\r
1238
- // if (vPadding.x != 0.0) {\r
1239
- // if (clampUV(vRegion)) {\r
1240
- // fragColor = vec4(0.0, 0.0, 0.0, 0.0);\r
1241
- // }\r
1242
- // }\r
1243
979
  \r
1244
980
  // CUSTOM_CODE_CALL\r
1245
981
  }\r
1246
- `, $ = 48, Dt = new Float32Array([0, 0, 1, 0, 0, 1, 1, 1]);
1247
- class ot extends Et {
982
+ `, Y = 48, Dt = new Float32Array([0, 0, 1, 0, 0, 1, 1, 1]);
983
+ class lt extends Et {
1248
984
  instanceBuffer;
1249
985
  quadBuffer;
1250
986
  instanceCount = 0;
@@ -1252,82 +988,347 @@ class ot extends Et {
1252
988
  worldSpriteMatrix = new Float32Array(6);
1253
989
  KEY = "Sprite";
1254
990
  constructor(t) {
1255
- super(t), this.createBuffer(), this.createDefaultShader();
991
+ super(t), this.createBuffer(), this.createDefaultShader();
992
+ }
993
+ createBuffer() {
994
+ const t = this.gl;
995
+ this.quadBuffer = new ut(t, A.Float32, t.ARRAY_BUFFER, t.STATIC_DRAW), this.quadBuffer.bindBuffer();
996
+ for (const e of Dt) this.quadBuffer.pushFloat32(e);
997
+ this.quadBuffer.makeDirty(), this.quadBuffer.bufferData(), this.instanceBuffer = new ut(t, A.Uint32, t.ARRAY_BUFFER, t.DYNAMIC_DRAW);
998
+ }
999
+ createDefaultShader() {
1000
+ return this.vs = et, this.fs = ft, this.defaultShader = this.createShader(et, ft), this.defaultShader;
1001
+ }
1002
+ createShader(t, e) {
1003
+ const r = super.createShader(t, e);
1004
+ return r.use(), r.bindVAO(), this.instanceBuffer.bindBuffer(), r.setAttributes([
1005
+ { name: "aMatrixRow0", size: 3, stride: Y, offset: 0, divisor: 1 },
1006
+ { name: "aMatrixRow1", size: 3, stride: Y, offset: 12, divisor: 1 },
1007
+ { name: "aUVRect", size: 4, stride: Y, offset: 24, divisor: 1 },
1008
+ { name: "aColor", size: 4, type: ot, normalized: !0, stride: Y, offset: 40, divisor: 1 },
1009
+ { name: "aTextureId", size: 1, type: ot, stride: Y, offset: 44, divisor: 1 }
1010
+ ]), this.quadBuffer.bindBuffer(), r.setAttributes([
1011
+ { name: "aVertex", size: 2, stride: 8, offset: 0, divisor: 0 }
1012
+ ]), r.unbindVAO(), this.currentShader && this.currentShader.use(), r;
1013
+ }
1014
+ drawSprite(t, e, r = 0, i = 0, s = 1, a = 1, h = 4294967295, o = 0, c = 0, u = !1, l = !1, f = !1) {
1015
+ const d = e * 6, x = this.matrixStore.data;
1016
+ this.drawSpriteAffine(
1017
+ t,
1018
+ x[d],
1019
+ x[d + 1],
1020
+ x[d + 2],
1021
+ x[d + 3],
1022
+ x[d + 4],
1023
+ x[d + 5],
1024
+ r,
1025
+ i,
1026
+ s,
1027
+ a,
1028
+ h,
1029
+ o,
1030
+ c,
1031
+ u,
1032
+ l,
1033
+ f
1034
+ );
1035
+ }
1036
+ drawSpriteAffine(t, e, r, i, s, a, h, o = 0, c = 0, u = 1, l = 1, f = 4294967295, d = 0, x = 0, p = !1, v = !1, y = !1) {
1037
+ const E = t.base, w = t.rawWidth, _ = t.rawHeight, L = t.offsetX, W = t.offsetY, O = this.useTexture(
1038
+ t.glTexture,
1039
+ d / E.width,
1040
+ x / E.height
1041
+ ), b = this.instanceBuffer, M = b.usedElemNum;
1042
+ b.resize(12);
1043
+ const C = b.float32, D = b.uint32, B = y ? _ : w, k = y ? w : _, I = B + 2 * d, F = k + 2 * x, V = L - d, z = W - x, R = this.localSpriteMatrix;
1044
+ y ? (R[0] = 0, R[1] = v ? F : -F, R[2] = p ? -I : I, R[3] = 0, R[4] = V + (p ? I : 0), R[5] = z + (v ? 0 : F)) : (R[0] = p ? -I : I, R[1] = 0, R[2] = 0, R[3] = v ? -F : F, R[4] = V + (p ? I : 0), R[5] = z + (v ? F : 0));
1045
+ const U = this.worldSpriteMatrix;
1046
+ U[0] = e * R[0] + i * R[1], U[1] = r * R[0] + s * R[1], U[2] = e * R[2] + i * R[3], U[3] = r * R[2] + s * R[3], U[4] = a + e * R[4] + i * R[5], U[5] = h + r * R[4] + s * R[5], C[M + 6] = o, C[M + 7] = c, C[M + 8] = u, C[M + 9] = l, D[M + 10] = f, D[M + 11] = O, this.rapid.roundPixels && (U[4] = Math.round(U[4]), U[5] = Math.round(U[5])), C[M] = U[0], C[M + 1] = U[2], C[M + 2] = U[4], C[M + 3] = U[1], C[M + 4] = U[3], C[M + 5] = U[5], b.usedElemNum += 12, b.makeDirty(), this.instanceCount++;
1047
+ }
1048
+ render() {
1049
+ if (this.instanceCount === 0) return;
1050
+ this.prepareRender();
1051
+ const t = this.gl, e = this.currentShader;
1052
+ this.instanceBuffer.bindBuffer(), this.instanceBuffer.bufferData(), e.bindVAO(), yt(t, t.TRIANGLE_STRIP, 0, 4, this.instanceCount), this.rapid.drawcallCount++;
1053
+ }
1054
+ resetRender() {
1055
+ super.resetRender(), this.instanceBuffer.clear(), this.instanceCount = 0;
1056
+ }
1057
+ hasPendingContent() {
1058
+ return this.instanceCount > 0;
1059
+ }
1060
+ }
1061
+ const T = 6;
1062
+ class Bt {
1063
+ matrixCount = 0;
1064
+ buffer;
1065
+ data;
1066
+ temporary = -1;
1067
+ constructor(t = 10) {
1068
+ this.buffer = new S(A.Float32), this.buffer.resize(t * T), this.data = this.buffer.getArray(), this.reset();
1069
+ }
1070
+ alloc() {
1071
+ const t = this.allocDirty();
1072
+ return this.identity(t), t;
1073
+ }
1074
+ allocDirty() {
1075
+ return this.buffer.resize(T) && (this.data = this.buffer.getArray()), this.buffer.usedElemNum += T, this.matrixCount++;
1076
+ }
1077
+ reset() {
1078
+ this.matrixCount = 0, this.buffer.usedElemNum = 0, this.temporary = this.alloc();
1079
+ }
1080
+ identity(t) {
1081
+ const e = t * T, r = this.data;
1082
+ r[e] = 1, r[e + 1] = 0, r[e + 2] = 0, r[e + 3] = 1, r[e + 4] = 0, r[e + 5] = 0;
1083
+ }
1084
+ translate(t, e, r) {
1085
+ const i = t * T, s = this.data;
1086
+ s[i + 4] = s[i] * e + s[i + 2] * r + s[i + 4], s[i + 5] = s[i + 1] * e + s[i + 3] * r + s[i + 5];
1087
+ }
1088
+ scale(t, e, r) {
1089
+ const i = t * T, s = this.data;
1090
+ s[i] *= e, s[i + 1] *= e, s[i + 2] *= r, s[i + 3] *= r;
1091
+ }
1092
+ rotate(t, e) {
1093
+ const r = t * T, i = this.data, s = Math.cos(e), a = Math.sin(e), h = i[r], o = i[r + 1], c = i[r + 2], u = i[r + 3];
1094
+ i[r] = h * s + c * a, i[r + 1] = o * s + u * a, i[r + 2] = h * -a + c * s, i[r + 3] = o * -a + u * s;
1095
+ }
1096
+ rotateWithOffset(t, e, r, i) {
1097
+ this.translate(t, r, i), this.rotate(t, e), this.translate(t, -r, -i);
1098
+ }
1099
+ copy(t, e) {
1100
+ const r = t * T, i = e * T, s = this.data;
1101
+ s[r] = s[i], s[r + 1] = s[i + 1], s[r + 2] = s[i + 2], s[r + 3] = s[i + 3], s[r + 4] = s[i + 4], s[r + 5] = s[i + 5];
1102
+ }
1103
+ multiply(t, e) {
1104
+ this.multiplyOut(t, t, e);
1105
+ }
1106
+ multiplyOut(t, e, r) {
1107
+ const i = t * T, s = e * T, a = r * T, h = this.data, o = h[s], c = h[s + 1], u = h[s + 2], l = h[s + 3], f = h[s + 4], d = h[s + 5], x = h[a], p = h[a + 1], v = h[a + 2], y = h[a + 3], E = h[a + 4], w = h[a + 5];
1108
+ h[i] = o * x + u * p, h[i + 1] = c * x + l * p, h[i + 2] = o * v + u * y, h[i + 3] = c * v + l * y, h[i + 4] = o * E + u * w + f, h[i + 5] = c * E + l * w + d;
1109
+ }
1110
+ invert(t) {
1111
+ const e = t * T, r = this.data, i = r[e], s = r[e + 1], a = r[e + 2], h = r[e + 3], o = r[e + 4], c = r[e + 5];
1112
+ let u = i * h - s * a;
1113
+ if (!u) {
1114
+ this.identity(t);
1115
+ return;
1116
+ }
1117
+ u = 1 / u, r[e] = h * u, r[e + 1] = -s * u, r[e + 2] = -a * u, r[e + 3] = i * u, r[e + 4] = (a * c - h * o) * u, r[e + 5] = (s * o - i * c) * u;
1118
+ }
1119
+ transformPoint(t, e, r) {
1120
+ const i = t * T, s = this.data;
1121
+ return {
1122
+ x: e * s[i] + r * s[i + 2] + s[i + 4],
1123
+ y: e * s[i + 1] + r * s[i + 3] + s[i + 5]
1124
+ };
1125
+ }
1126
+ worldToLocal(t, e, r) {
1127
+ const i = this.allocDirty();
1128
+ this.copy(i, t), this.invert(i);
1129
+ const s = this.transformPoint(i, e, r);
1130
+ return this.matrixCount--, this.buffer.usedElemNum -= T, s;
1131
+ }
1132
+ localToWorld(t, e, r) {
1133
+ return this.transformPoint(t, e, r);
1134
+ }
1135
+ getPosition(t) {
1136
+ const e = t * T, r = this.data;
1137
+ return { x: r[e + 4], y: r[e + 5] };
1138
+ }
1139
+ getScale(t) {
1140
+ const e = t * T, r = this.data, i = Math.sqrt(r[e] * r[e] + r[e + 1] * r[e + 1]), s = Math.sqrt(r[e + 2] * r[e + 2] + r[e + 3] * r[e + 3]);
1141
+ return { x: i, y: s };
1142
+ }
1143
+ getRotation(t) {
1144
+ const e = t * T, r = this.data;
1145
+ return Math.atan2(r[e + 1], r[e]);
1146
+ }
1147
+ toCSSMatrix(t, e = 1, r = 1) {
1148
+ const i = t * T, s = this.data;
1149
+ return `matrix(${s[i] * e}, ${s[i + 1] * r}, ${s[i + 2] * e}, ${s[i + 3] * r}, ${s[i + 4] * e}, ${s[i + 5] * r})`;
1150
+ }
1151
+ getMatrix(t) {
1152
+ const e = t * T;
1153
+ return this.data.slice(e, e + T);
1154
+ }
1155
+ getMatrixRef(t) {
1156
+ const e = t * T;
1157
+ return this.data.subarray(e, e + T);
1158
+ }
1159
+ setMatrix(t, e) {
1160
+ const r = t * T, i = this.data;
1161
+ i[r] = e[0], i[r + 1] = e[1], i[r + 2] = e[2], i[r + 3] = e[3], i[r + 4] = e[4], i[r + 5] = e[5];
1162
+ }
1163
+ multiplyAffineInPlace(t, e, r, i, s, a, h) {
1164
+ const o = t * 6, c = this.data, u = c[o], l = c[o + 1], f = c[o + 2], d = c[o + 3], x = c[o + 4], p = c[o + 5];
1165
+ c[o] = u * e + f * r, c[o + 1] = l * e + d * r, c[o + 2] = u * i + f * s, c[o + 3] = l * i + d * s, c[o + 4] = u * a + f * h + x, c[o + 5] = l * a + d * h + p;
1166
+ }
1167
+ multiplyAffine(t, e, r, i, s, a, h, o) {
1168
+ const c = t * 6, u = e * 6, l = this.data, f = l[c], d = l[c + 1], x = l[c + 2], p = l[c + 3], v = l[c + 4], y = l[c + 5];
1169
+ l[u] = f * r + x * i, l[u + 1] = d * r + p * i, l[u + 2] = f * s + x * a, l[u + 3] = d * s + p * a, l[u + 4] = f * h + x * o + v, l[u + 5] = d * h + p * o + y;
1170
+ }
1171
+ }
1172
+ class kt {
1173
+ matrix = new Bt();
1174
+ step = 0;
1175
+ stack = new S(A.Uint32);
1176
+ stepWorldM = new S(A.Uint32);
1177
+ stepAction = new S(A.Uint32);
1178
+ stepParentM = new S(A.Uint32);
1179
+ stepClose = new S(A.Uint32);
1180
+ stepStack = new S(A.Uint32);
1181
+ curLocalM = -1;
1182
+ curWorldM = -1;
1183
+ rapid;
1184
+ constructor(t) {
1185
+ this.reset(), this.rapid = t;
1256
1186
  }
1257
- createBuffer() {
1258
- const t = this.gl;
1259
- this.quadBuffer = new it(t, S.Float32, t.ARRAY_BUFFER, t.STATIC_DRAW), this.quadBuffer.bindBuffer();
1260
- for (const e of Dt) this.quadBuffer.pushFloat32(e);
1261
- this.quadBuffer.makeDirty(), this.quadBuffer.bufferData(), this.instanceBuffer = new it(t, S.Uint32, t.ARRAY_BUFFER, t.DYNAMIC_DRAW);
1187
+ save() {
1188
+ this.stack.push(this.curWorldM);
1189
+ const t = this.curWorldM;
1190
+ return this.curLocalM = this.matrix.alloc(), this.curWorldM = this.matrix.allocDirty(), this.stepWorldM.push(this.curWorldM), this.stepParentM.push(t), this.stepAction.push(1), this.stepClose.push(0), this.stepStack.push(this.step), this.matrix.copy(this.curWorldM, t), { world: this.curWorldM, local: this.curLocalM, step: this.step++ };
1262
1191
  }
1263
- createDefaultShader() {
1264
- const t = this.rapid, e = B(lt, t.maxTextureUnits), r = B(Z, t.maxTextureUnits);
1265
- return this.vs = r, this.fs = e, this.defaultShader = this.createShader(r, e), this.defaultShader;
1192
+ restore() {
1193
+ this.stack.length == 0 || this.stepStack.length == 0 || (this.curWorldM = this.stack.pop(), this.curLocalM = this.curWorldM - 1, this.stepParentM.push(this.stack.get(this.stack.length - 1)), this.stepWorldM.push(this.curWorldM), this.stepAction.push(0), this.stepClose.push(0), this.stepClose.typedArray[this.stepStack.pop()] = this.step, this.step++);
1266
1194
  }
1267
- createShader(t, e) {
1268
- const r = super.createShader(t, e);
1269
- return r.use(), r.bindVAO(), this.instanceBuffer.bindBuffer(), r.setAttributes([
1270
- { name: "aMatrixRow0", size: 3, stride: $, offset: 0, divisor: 1 },
1271
- { name: "aMatrixRow1", size: 3, stride: $, offset: 12, divisor: 1 },
1272
- { name: "aUVRect", size: 4, stride: $, offset: 24, divisor: 1 },
1273
- { name: "aColor", size: 4, type: at, normalized: !0, stride: $, offset: 40, divisor: 1 },
1274
- { name: "aTextureId", size: 1, type: at, stride: $, offset: 44, divisor: 1 }
1275
- ]), this.quadBuffer.bindBuffer(), r.setAttributes([
1276
- { name: "aVertex", size: 2, stride: 8, offset: 0, divisor: 0 }
1277
- ]), r.unbindVAO(), this.currentShader && this.currentShader.use(), r;
1195
+ restoreAll() {
1196
+ for (; this.stack.length > 0 && this.stepStack.length > 0; )
1197
+ this.restore();
1278
1198
  }
1279
- createCustomShader(t) {
1280
- const e = B(lt, this.rapid.maxTextureUnits - t.usedTextureUnitNum), r = B(Z, this.rapid.maxTextureUnits - t.usedTextureUnitNum);
1281
- return t.getGLShader(this, this.KEY, r, e);
1199
+ getStep(t) {
1200
+ return typeof t == "number" ? t : t.step;
1282
1201
  }
1283
- drawSprite(t, e, r = 0, i = 0, s = 1, a = 1, h = 4294967295, o = 0, c = 0, u = !1, l = !1, d = !1) {
1284
- const f = e * 6, m = this.matrixStore.data;
1285
- this.drawSpriteAffine(
1286
- t,
1287
- m[f],
1288
- m[f + 1],
1289
- m[f + 2],
1290
- m[f + 3],
1291
- m[f + 4],
1292
- m[f + 5],
1293
- r,
1294
- i,
1295
- s,
1296
- a,
1297
- h,
1298
- o,
1299
- c,
1300
- u,
1301
- l,
1302
- d
1303
- );
1202
+ walkSubtree(t, e, r = 1 / 0) {
1203
+ let i = 0, s = t;
1204
+ for (; s < this.step; ) {
1205
+ const a = this.stepAction.get(s);
1206
+ if (a === 1) {
1207
+ if (i++, i > r) {
1208
+ const h = this.stepClose.get(s);
1209
+ if (h > s) {
1210
+ s = h + 1, i--;
1211
+ continue;
1212
+ }
1213
+ }
1214
+ } else
1215
+ i--;
1216
+ if (e(s, a, i) === !1 || a === 0 && i === 0) return;
1217
+ s++;
1218
+ }
1304
1219
  }
1305
- drawSpriteAffine(t, e, r, i, s, a, h, o = 0, c = 0, u = 1, l = 1, d = 4294967295, f = 0, m = 0, g = !1, v = !1, y = !1) {
1306
- const w = t.base, M = t.rawWidth, _ = t.rawHeight, P = t.offsetX, O = t.offsetY, W = this.useTexture(
1307
- t.glTexture,
1308
- f / w.width,
1309
- m / w.height
1310
- ), b = this.instanceBuffer, R = b.usedElemNum;
1311
- b.resize(12);
1312
- const C = b.float32, D = b.uint32, k = y ? _ : M, H = y ? M : _, I = k + 2 * f, F = H + 2 * m, Y = P - f, z = O - m, E = this.localSpriteMatrix;
1313
- y ? (E[0] = 0, E[1] = v ? F : -F, E[2] = g ? -I : I, E[3] = 0, E[4] = Y + (g ? I : 0), E[5] = z + (v ? 0 : F)) : (E[0] = g ? -I : I, E[1] = 0, E[2] = 0, E[3] = v ? -F : F, E[4] = Y + (g ? I : 0), E[5] = z + (v ? F : 0));
1314
- const U = this.worldSpriteMatrix;
1315
- U[0] = e * E[0] + i * E[1], U[1] = r * E[0] + s * E[1], U[2] = e * E[2] + i * E[3], U[3] = r * E[2] + s * E[3], U[4] = a + e * E[4] + i * E[5], U[5] = h + r * E[4] + s * E[5], C[R + 6] = o, C[R + 7] = c, C[R + 8] = u, C[R + 9] = l, D[R + 10] = d, D[R + 11] = W, this.rapid.roundPixels && (U[4] = Math.round(U[4]), U[5] = Math.round(U[5])), C[R] = U[0], C[R + 1] = U[2], C[R + 2] = U[4], C[R + 3] = U[1], C[R + 4] = U[3], C[R + 5] = U[5], b.usedElemNum += 12, b.makeDirty(), this.instanceCount++;
1220
+ getParent(t) {
1221
+ const e = this.getStep(t);
1222
+ return this.stepParentM.get(e);
1316
1223
  }
1317
- render() {
1318
- if (this.instanceCount === 0) return;
1319
- this.prepareRender();
1320
- const t = this.gl, e = this.currentShader;
1321
- this.instanceBuffer.bindBuffer(), this.instanceBuffer.bufferData(), e.bindVAO(), yt(t, t.TRIANGLE_STRIP, 0, 4, this.instanceCount), this.rapid.drawcallCount++;
1224
+ getChildren(t) {
1225
+ const e = this.getStep(t), r = this.stepClose.get(e), i = [];
1226
+ let s = e + 1;
1227
+ for (; s < r; )
1228
+ this.stepAction.get(s) === 1 ? (i.push(s), s = this.stepClose.get(s) + 1) : s++;
1229
+ return i;
1322
1230
  }
1323
- resetRender() {
1324
- super.resetRender(), this.instanceBuffer.clear(), this.instanceCount = 0;
1231
+ updateMatrixSubtree(t) {
1232
+ const e = this.getStep(t);
1233
+ this.walkSubtree(e, (r, i) => {
1234
+ if (i === 1) {
1235
+ const s = this.stepWorldM.get(r), a = s - 1, h = this.stepParentM.get(r);
1236
+ this.matrix.multiplyOut(s, h, a);
1237
+ }
1238
+ });
1325
1239
  }
1326
- hasPendingContent() {
1327
- return this.instanceCount > 0;
1240
+ translate(t, e) {
1241
+ this.matrix.translate(this.curLocalM, t, e), this.matrix.translate(this.curWorldM, t, e);
1242
+ }
1243
+ scale(t, e = t) {
1244
+ this.matrix.scale(this.curLocalM, t, e), this.matrix.scale(this.curWorldM, t, e);
1245
+ }
1246
+ rotate(t) {
1247
+ this.matrix.rotate(this.curLocalM, t), this.matrix.rotate(this.curWorldM, t);
1248
+ }
1249
+ rotateWithOffset(t, e, r) {
1250
+ this.matrix.rotateWithOffset(this.curLocalM, t, e, r), this.matrix.rotateWithOffset(this.curWorldM, t, e, r);
1251
+ }
1252
+ identity() {
1253
+ this.matrix.identity(this.curLocalM), this.matrix.identity(this.curWorldM);
1254
+ }
1255
+ reset() {
1256
+ this.matrix.reset(), this.stack.clear(), this.stepAction.clear(), this.stepWorldM.clear(), this.stepParentM.clear(), this.stepClose.clear(), this.stepStack.clear(), this.step = 0, this.curLocalM = this.matrix.alloc(), this.curWorldM = this.matrix.alloc();
1257
+ }
1258
+ localToWorld(t, e) {
1259
+ return this.matrix.localToWorld(this.curWorldM, t, e);
1260
+ }
1261
+ worldToLocal(t, e) {
1262
+ return this.matrix.worldToLocal(this.curWorldM, t, e);
1263
+ }
1264
+ getGlobalPosition() {
1265
+ return this.matrix.getPosition(this.curWorldM);
1266
+ }
1267
+ getGlobalScale() {
1268
+ return this.matrix.getScale(this.curWorldM);
1269
+ }
1270
+ getGlobalRotation() {
1271
+ return this.matrix.getRotation(this.curWorldM);
1272
+ }
1273
+ toCSSMatrix() {
1274
+ return this.matrix.toCSSMatrix(this.curWorldM);
1275
+ }
1276
+ getTransform() {
1277
+ const t = this.curWorldM * T;
1278
+ return this.matrix.data.slice(t, t + T);
1279
+ }
1280
+ setTransform(t) {
1281
+ const e = this.curWorldM * T, r = this.matrix.data;
1282
+ r[e] = t[0], r[e + 1] = t[1], r[e + 2] = t[2], r[e + 3] = t[3], r[e + 4] = t[4], r[e + 5] = t[5];
1283
+ }
1284
+ transformPoint(t, e) {
1285
+ return this.matrix.transformPoint(this.curLocalM, t, e);
1286
+ }
1287
+ applyTransform(t, e = 0, r = 0, i = -1) {
1288
+ let s = t.x ?? 0, a = t.y ?? 0;
1289
+ t.position && (s += t.position.x, a += t.position.y);
1290
+ let h = 1, o = 1;
1291
+ const c = t.scale;
1292
+ c && (typeof c == "number" ? (h = c, o = c) : (h = c.x, o = c.y));
1293
+ const u = t.rotation ?? 0;
1294
+ let l = t.offsetX ?? 0, f = t.offsetY ?? 0;
1295
+ t.offset && (l += t.offset.x, f += t.offset.y);
1296
+ const d = t.origin;
1297
+ d !== void 0 && (typeof d == "number" ? (l -= d * e, f -= d * r) : (l -= d.x * e, f -= d.y * r));
1298
+ const x = u ? Math.cos(u) : 1, p = u ? Math.sin(u) : 0, v = x * h, y = p * h, E = -p * o, w = x * o, _ = s + v * l + E * f, L = a + y * l + w * f;
1299
+ if (i !== -1) {
1300
+ this.matrix.multiplyAffine(
1301
+ this.curWorldM,
1302
+ i,
1303
+ v,
1304
+ y,
1305
+ E,
1306
+ w,
1307
+ _,
1308
+ L
1309
+ );
1310
+ return;
1311
+ }
1312
+ this.matrix.multiplyAffineInPlace(
1313
+ this.curLocalM,
1314
+ v,
1315
+ y,
1316
+ E,
1317
+ w,
1318
+ _,
1319
+ L
1320
+ ), this.matrix.multiplyAffineInPlace(
1321
+ this.curWorldM,
1322
+ v,
1323
+ y,
1324
+ E,
1325
+ w,
1326
+ _,
1327
+ L
1328
+ );
1328
1329
  }
1329
1330
  }
1330
- const kt = `#version 300 es\r
1331
+ const Ht = `#version 300 es\r
1331
1332
  precision highp float;\r
1332
1333
  \r
1333
1334
  in vec2 aPosition;\r
@@ -1358,7 +1359,7 @@ void main(void) {\r
1358
1359
  // CUSTOM_CODE_CALL\r
1359
1360
  gl_Position = u_projection * position;\r
1360
1361
  }\r
1361
- `, Ht = `#version 300 es\r
1362
+ `, Gt = `#version 300 es\r
1362
1363
  precision mediump float;\r
1363
1364
  \r
1364
1365
  in vec4 vColor;\r
@@ -1366,6 +1367,7 @@ in vec2 vRegion;\r
1366
1367
  in vec4 vUVRect;\r
1367
1368
  \r
1368
1369
  uniform sampler2D uTexture;\r
1370
+ uniform sampler2D uTextures[%TEXTURE_NUM%];\r
1369
1371
  uniform int uUseTexture;\r
1370
1372
  \r
1371
1373
  out vec4 fragColor;\r
@@ -1404,7 +1406,7 @@ void main(void) {\r
1404
1406
  fragColor = sampleTexture(vRegion) * vColor;\r
1405
1407
  // CUSTOM_CODE_CALL\r
1406
1408
  }\r
1407
- `, Gt = `#version 300 es\r
1409
+ `, Vt = `#version 300 es\r
1408
1410
  precision highp float;\r
1409
1411
  \r
1410
1412
  in vec2 aPosition;\r
@@ -1427,7 +1429,7 @@ void main(void) {\r
1427
1429
  );\r
1428
1430
  gl_Position = u_projection * position;\r
1429
1431
  }\r
1430
- `, Vt = `#version 300 es\r
1432
+ `, Xt = `#version 300 es\r
1431
1433
  precision mediump float;\r
1432
1434
  \r
1433
1435
  in vec2 vRegion;\r
@@ -1447,10 +1449,11 @@ void main(void) {\r
1447
1449
  discard;\r
1448
1450
  }\r
1449
1451
  }\r
1450
- `, j = 20;
1451
- class Yt extends Et {
1452
+ `, $ = 20;
1453
+ class zt extends Et {
1452
1454
  vertexBuffer;
1453
1455
  vertexCount = 0;
1456
+ extraTextureUnit = 1;
1454
1457
  matrixIndex = -1;
1455
1458
  drawMode = this.gl.TRIANGLES;
1456
1459
  texture;
@@ -1461,24 +1464,24 @@ class Yt extends Et {
1461
1464
  }
1462
1465
  createBuffer() {
1463
1466
  const t = this.gl;
1464
- this.vertexBuffer = new it(t, S.Float32, t.ARRAY_BUFFER, t.DYNAMIC_DRAW), this.maskShader = this.createMaskShader(Gt, Vt);
1467
+ this.vertexBuffer = new ut(t, A.Float32, t.ARRAY_BUFFER, t.DYNAMIC_DRAW), this.maskShader = this.createMaskShader(Vt, Xt);
1465
1468
  }
1466
1469
  createDefaultShader() {
1467
- return this.vs = kt, this.fs = Ht, this.defaultShader = this.createShader(this.vs, this.fs), this.defaultShader;
1470
+ return this.vs = Ht, this.fs = Gt, this.defaultShader = this.createShader(this.vs, this.fs), this.defaultShader;
1468
1471
  }
1469
1472
  createMaskShader(t, e) {
1470
1473
  const r = this.gl, i = new Tt(r, t, e);
1471
1474
  return i.use(), i.bindVAO(), this.vertexBuffer.bindBuffer(), i.setAttributes([
1472
- { name: "aPosition", size: 2, type: r.FLOAT, stride: j - 4, offset: 0, divisor: 0 },
1473
- { name: "aUV", size: 2, type: r.FLOAT, stride: j - 4, offset: 8, divisor: 0 }
1475
+ { name: "aPosition", size: 2, type: r.FLOAT, stride: $ - 4, offset: 0, divisor: 0 },
1476
+ { name: "aUV", size: 2, type: r.FLOAT, stride: $ - 4, offset: 8, divisor: 0 }
1474
1477
  ]), i.unbindVAO(), this.currentShader && this.currentShader.use(), i;
1475
1478
  }
1476
1479
  createShader(t, e) {
1477
1480
  const r = this.gl, i = super.createShader(t, e);
1478
1481
  return i.use(), i.bindVAO(), this.vertexBuffer.bindBuffer(), i.setAttributes([
1479
- { name: "aPosition", size: 2, type: r.FLOAT, stride: j, offset: 0, divisor: 0 },
1480
- { name: "aColor", size: 4, type: r.UNSIGNED_BYTE, normalized: !0, stride: j, offset: 8, divisor: 0 },
1481
- { name: "aUV", size: 2, type: r.FLOAT, stride: j, offset: 12, divisor: 0 }
1482
+ { name: "aPosition", size: 2, type: r.FLOAT, stride: $, offset: 0, divisor: 0 },
1483
+ { name: "aColor", size: 4, type: r.UNSIGNED_BYTE, normalized: !0, stride: $, offset: 8, divisor: 0 },
1484
+ { name: "aUV", size: 2, type: r.FLOAT, stride: $, offset: 12, divisor: 0 }
1482
1485
  ]), i.unbindVAO(), this.currentShader && this.currentShader.use(), i;
1483
1486
  }
1484
1487
  startGraphic(t, e = this.gl.TRIANGLES, r) {
@@ -1505,15 +1508,15 @@ class Yt extends Et {
1505
1508
  return !1;
1506
1509
  }
1507
1510
  }
1508
- class p {
1511
+ class g {
1509
1512
  x;
1510
1513
  y;
1511
- static ZERO = new p(0, 0);
1512
- static ONE = new p(1, 1);
1513
- static UP = new p(0, 1);
1514
- static DOWN = new p(0, -1);
1515
- static LEFT = new p(-1, 0);
1516
- static RIGHT = new p(1, 0);
1514
+ static ZERO = new g(0, 0);
1515
+ static ONE = new g(1, 1);
1516
+ static UP = new g(0, 1);
1517
+ static DOWN = new g(0, -1);
1518
+ static LEFT = new g(-1, 0);
1519
+ static RIGHT = new g(1, 0);
1517
1520
  constructor(t, e) {
1518
1521
  this.x = t !== void 0 ? t : 0, this.y = e !== void 0 ? e : 0;
1519
1522
  }
@@ -1521,16 +1524,16 @@ class p {
1521
1524
  return e === void 0 ? (this.x = t, this.y = t) : (this.x = t, this.y = e), this;
1522
1525
  }
1523
1526
  add(t) {
1524
- return new p(this.x + t.x, this.y + t.y);
1527
+ return new g(this.x + t.x, this.y + t.y);
1525
1528
  }
1526
1529
  subtract(t) {
1527
- return new p(this.x - t.x, this.y - t.y);
1530
+ return new g(this.x - t.x, this.y - t.y);
1528
1531
  }
1529
1532
  multiply(t) {
1530
- return t instanceof p ? new p(this.x * t.x, this.y * t.y) : new p(this.x * t, this.y * t);
1533
+ return t instanceof g ? new g(this.x * t.x, this.y * t.y) : new g(this.x * t, this.y * t);
1531
1534
  }
1532
1535
  divide(t) {
1533
- return t instanceof p ? new p(this.x / t.x, this.y / t.y) : new p(this.x / t, this.y / t);
1536
+ return t instanceof g ? new g(this.x / t.x, this.y / t.y) : new g(this.x / t, this.y / t);
1534
1537
  }
1535
1538
  dot(t) {
1536
1539
  return this.x * t.x + this.y * t.y;
@@ -1543,7 +1546,7 @@ class p {
1543
1546
  return Math.sqrt(e * e + r * r);
1544
1547
  }
1545
1548
  clone() {
1546
- return new p(this.x, this.y);
1549
+ return new g(this.x, this.y);
1547
1550
  }
1548
1551
  to(t) {
1549
1552
  this.x = t.x, this.y = t.y;
@@ -1559,7 +1562,7 @@ class p {
1559
1562
  return this.x = -this.x, this.y = -this.y, this;
1560
1563
  }
1561
1564
  inverted() {
1562
- return new p(this.x * -1, this.y * -1);
1565
+ return new g(this.x * -1, this.y * -1);
1563
1566
  }
1564
1567
  length() {
1565
1568
  return Math.sqrt(this.x * this.x + this.y * this.y);
@@ -1573,13 +1576,13 @@ class p {
1573
1576
  }
1574
1577
  normalized() {
1575
1578
  const t = this.length();
1576
- return new p(this.x / t || 0, this.y / t || 0);
1579
+ return new g(this.x / t || 0, this.y / t || 0);
1577
1580
  }
1578
1581
  fix(t = 1e-13) {
1579
1582
  this.x = Math.abs(this.x) < t ? 0 : this.x, this.y = Math.abs(this.y) < t ? 0 : this.y;
1580
1583
  }
1581
1584
  fixed(t = 1e-13) {
1582
- return new p(
1585
+ return new g(
1583
1586
  Math.abs(this.x) < t ? 0 : this.x,
1584
1587
  Math.abs(this.y) < t ? 0 : this.y
1585
1588
  );
@@ -1588,13 +1591,13 @@ class p {
1588
1591
  return Math.atan2(this.y, this.x);
1589
1592
  }
1590
1593
  abs() {
1591
- return new p(Math.abs(this.x), Math.abs(this.y));
1594
+ return new g(Math.abs(this.x), Math.abs(this.y));
1592
1595
  }
1593
1596
  floor() {
1594
- return new p(Math.floor(this.x), Math.floor(this.y));
1597
+ return new g(Math.floor(this.x), Math.floor(this.y));
1595
1598
  }
1596
1599
  ceil() {
1597
- return new p(Math.ceil(this.x), Math.ceil(this.y));
1600
+ return new g(Math.ceil(this.x), Math.ceil(this.y));
1598
1601
  }
1599
1602
  stringify() {
1600
1603
  return `Vec2(${this.x}, ${this.y})`;
@@ -1615,13 +1618,13 @@ class p {
1615
1618
  return this.x = Math.cos(t) * e, this.y = Math.sin(t) * e, this;
1616
1619
  }
1617
1620
  static FromArray(t) {
1618
- return t.map((e) => new p(e[0], e[1]));
1621
+ return t.map((e) => new g(e[0], e[1]));
1619
1622
  }
1620
1623
  static fromAngle(t) {
1621
- return new p(Math.cos(t), Math.sin(t));
1624
+ return new g(Math.cos(t), Math.sin(t));
1622
1625
  }
1623
1626
  }
1624
- const Xt = (n, t) => {
1627
+ const Yt = (n, t) => {
1625
1628
  const [e, r, i, s, a, h] = t, o = new Float32Array([
1626
1629
  e,
1627
1630
  i,
@@ -1640,8 +1643,8 @@ const Xt = (n, t) => {
1640
1643
  0,
1641
1644
  1
1642
1645
  ]);
1643
- return zt(o, n);
1644
- }, zt = (n, t) => {
1646
+ return $t(o, n);
1647
+ }, $t = (n, t) => {
1645
1648
  const e = new Float32Array(16);
1646
1649
  for (let r = 0; r < 4; r++)
1647
1650
  for (let i = 0; i < 4; i++) {
@@ -1652,15 +1655,15 @@ const Xt = (n, t) => {
1652
1655
  }
1653
1656
  return e;
1654
1657
  };
1655
- var $t = /* @__PURE__ */ ((n) => (n[n.STRETCH = 0] = "STRETCH", n[n.REPEAT = 1] = "REPEAT", n))($t || {});
1656
- const rt = 10, dt = (n, t, e, r, i, s) => {
1657
- const a = [], h = [], o = r ? Math.atan2(t.y, t.x) : Math.atan2(-t.y, -t.x), c = Math.PI, u = new p(t.y, -t.x);
1658
- for (let l = 0; l < rt; l++) {
1659
- const d = l / rt, f = (l + 1) / rt, m = o + d * c, g = o + f * c, v = new p(Math.cos(m) * e, Math.sin(m) * e), y = new p(Math.cos(g) * e, Math.sin(g) * e);
1660
- a.push(n), h.push(new p(i, 0.5)), a.push(n.add(v)), h.push(new p(i + v.dot(u) * s, 0.5 - 0.5 * v.dot(t) / e)), a.push(n.add(y)), h.push(new p(i + y.dot(u) * s, 0.5 - 0.5 * y.dot(t) / e));
1658
+ var jt = /* @__PURE__ */ ((n) => (n[n.STRETCH = 0] = "STRETCH", n[n.REPEAT = 1] = "REPEAT", n))(jt || {});
1659
+ const nt = 10, mt = (n, t, e, r, i, s) => {
1660
+ const a = [], h = [], o = r ? Math.atan2(t.y, t.x) : Math.atan2(-t.y, -t.x), c = Math.PI, u = new g(t.y, -t.x);
1661
+ for (let l = 0; l < nt; l++) {
1662
+ const f = l / nt, d = (l + 1) / nt, x = o + f * c, p = o + d * c, v = new g(Math.cos(x) * e, Math.sin(x) * e), y = new g(Math.cos(p) * e, Math.sin(p) * e);
1663
+ a.push(n), h.push(new g(i, 0.5)), a.push(n.add(v)), h.push(new g(i + v.dot(u) * s, 0.5 - 0.5 * v.dot(t) / e)), a.push(n.add(y)), h.push(new g(i + y.dot(u) * s, 0.5 - 0.5 * y.dot(t) / e));
1661
1664
  }
1662
1665
  return { vertices: a, uv: h };
1663
- }, jt = (n, t = !1) => {
1666
+ }, Kt = (n, t = !1) => {
1664
1667
  const e = [];
1665
1668
  if (n.length < 2 || t && n.length < 3) return { normals: e, length: 0 };
1666
1669
  const r = 4, i = 1e-3, s = n.length;
@@ -1674,14 +1677,14 @@ const rt = 10, dt = (n, t, e, r, i, s) => {
1674
1677
  for (let o = 0; o < s - 1; o++)
1675
1678
  a += n[o].distanceTo(n[o + 1]);
1676
1679
  const h = (o, c, u) => {
1677
- const l = c.subtract(o).normalize(), d = c.subtract(u).normalize(), f = d.dot(l);
1678
- if (f < -1 + i)
1680
+ const l = c.subtract(o).normalize(), f = c.subtract(u).normalize(), d = f.dot(l);
1681
+ if (d < -1 + i)
1679
1682
  return { normal: l.perpendicular(), miters: 1 };
1680
1683
  {
1681
- let m = d.add(l).normalize();
1682
- l.cross(d) < 0 && (m = m.multiply(-1));
1683
- let g = 1 / Math.sqrt((1 - f) / 2);
1684
- return { normal: m, miters: Math.min(g, r) };
1684
+ let x = f.add(l).normalize();
1685
+ l.cross(f) < 0 && (x = x.multiply(-1));
1686
+ let p = 1 / Math.sqrt((1 - d) / 2);
1687
+ return { normal: x, miters: Math.min(p, r) };
1685
1688
  }
1686
1689
  };
1687
1690
  if (t)
@@ -1700,23 +1703,23 @@ const rt = 10, dt = (n, t, e, r, i, s) => {
1700
1703
  } else
1701
1704
  e.push(h(n[o - 1], n[o], n[o + 1]));
1702
1705
  return { normals: e, length: a };
1703
- }, Kt = (n) => {
1706
+ }, qt = (n) => {
1704
1707
  const t = n.points;
1705
1708
  if (t.length < 2) return { vertices: [], uv: [] };
1706
- const { normals: e, length: r } = jt(t, n.closed), i = (n.width || 1) / 2, s = [], a = [], h = n.roundCap || !1, o = n.textureMode || 0;
1709
+ const { normals: e, length: r } = Kt(t, n.closed), i = (n.width || 1) / 2, s = [], a = [], h = n.roundCap || !1, o = n.textureMode || 0;
1707
1710
  let c = 0;
1708
1711
  const u = n.texture?.rawWidth || 1, l = n.closed ? t.length : t.length - 1;
1709
- for (let d = 0; d < l; d++) {
1710
- const f = t[d], m = e[d].normal, g = e[d].miters, v = (d + 1) % t.length, y = t[v], w = e[v].normal, M = e[v].miters, _ = f.add(m.multiply(g * i)), P = f.subtract(m.multiply(g * i)), O = y.add(w.multiply(M * i)), W = y.subtract(w.multiply(M * i)), b = f.distanceTo(y);
1711
- let R = 0, C = 0;
1712
- o === 0 ? (R = c / r, C = (c + b) / r) : (R = c / u, C = R + b / u);
1713
- const D = new p(R, 0), k = new p(R, 1), H = new p(C, 0), I = new p(C, 1);
1714
- s.push(_), a.push(D), s.push(P), a.push(k), s.push(O), a.push(H), s.push(O), a.push(H), s.push(W), a.push(I), s.push(P), a.push(k), c += b;
1712
+ for (let f = 0; f < l; f++) {
1713
+ const d = t[f], x = e[f].normal, p = e[f].miters, v = (f + 1) % t.length, y = t[v], E = e[v].normal, w = e[v].miters, _ = d.add(x.multiply(p * i)), L = d.subtract(x.multiply(p * i)), W = y.add(E.multiply(w * i)), O = y.subtract(E.multiply(w * i)), b = d.distanceTo(y);
1714
+ let M = 0, C = 0;
1715
+ o === 0 ? (M = c / r, C = (c + b) / r) : (M = c / u, C = M + b / u);
1716
+ const D = new g(M, 0), B = new g(M, 1), k = new g(C, 0), I = new g(C, 1);
1717
+ s.push(_), a.push(D), s.push(L), a.push(B), s.push(W), a.push(k), s.push(W), a.push(k), s.push(O), a.push(I), s.push(L), a.push(B), c += b;
1715
1718
  }
1716
1719
  if (h && !n.closed) {
1717
- const d = t[0], f = e[0].normal, m = o === 0 ? r > 0 ? 1 / r : 0 : 1 / u, g = dt(d, f, i, !0, 0, m);
1718
- s.push(...g.vertices), a.push(...g.uv);
1719
- const v = t[t.length - 1], y = e[t.length - 1].normal, w = o === 0 ? 1 : c / u, M = o === 0 ? r > 0 ? 1 / r : 0 : 1 / u, _ = dt(v, y, i, !1, w, M);
1720
+ 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);
1721
+ s.push(...p.vertices), a.push(...p.uv);
1722
+ const v = t[t.length - 1], y = e[t.length - 1].normal, E = o === 0 ? 1 : c / u, w = o === 0 ? r > 0 ? 1 / r : 0 : 1 / u, _ = mt(v, y, i, !1, E, w);
1720
1723
  s.push(..._.vertices), a.push(..._.uv);
1721
1724
  }
1722
1725
  return { vertices: s, uv: a };
@@ -1739,7 +1742,7 @@ const rt = 10, dt = (n, t, e, r, i, s) => {
1739
1742
  i
1740
1743
  ), i;
1741
1744
  }
1742
- }, qt = (n, t) => t ? n ? t.premultipliedUint32 : t.uint32 : 4294967295, Qt = 2, Zt = (n, t) => {
1745
+ }, Qt = (n, t) => t ? n ? t.premultipliedUint32 : t.uint32 : 4294967295, Zt = 2, Jt = (n, t) => {
1743
1746
  const e = Q(n, t, t.texture.rawWidth, t.texture.rawHeight), r = t.texture;
1744
1747
  if (!r?.base || n.inCreateMask)
1745
1748
  return;
@@ -1748,8 +1751,8 @@ const rt = 10, dt = (n, t, e, r, i, s) => {
1748
1751
  let s = r.uvX, a = r.uvY, h = r.uvW, o = r.uvH;
1749
1752
  const c = !!t.flipX, u = !!t.flipY != !!r.flipY;
1750
1753
  let l = t.padding ?? i.currentShader.padding;
1751
- r.isAtlas && (l += Qt);
1752
- const d = s <= h ? l : -l, f = a <= o ? l : -l;
1754
+ r.isAtlas && (l += Zt);
1755
+ const f = s <= h ? l : -l, d = a <= o ? l : -l;
1753
1756
  i.drawSprite(
1754
1757
  r,
1755
1758
  e ?? t.customMatrix ?? n.matrixStack.curWorldM,
@@ -1758,13 +1761,13 @@ const rt = 10, dt = (n, t, e, r, i, s) => {
1758
1761
  h,
1759
1762
  o,
1760
1763
  n.getColorUint32(t.color),
1761
- d,
1762
1764
  f,
1765
+ d,
1763
1766
  c,
1764
1767
  u,
1765
1768
  r.isRotated
1766
1769
  );
1767
- }, Jt = (n, t) => {
1770
+ }, te = (n, t) => {
1768
1771
  const e = n.particleRegion;
1769
1772
  n.enterRegion(e, t.shader);
1770
1773
  const r = t.texture, i = t.count ?? t.x.length, s = t.color;
@@ -1810,17 +1813,17 @@ const rt = 10, dt = (n, t, e, r, i, s) => {
1810
1813
  );
1811
1814
  }
1812
1815
  n.endGraphic();
1813
- }, te = (n, t) => {
1816
+ }, ee = (n, t) => {
1814
1817
  if (n.inCreateMask)
1815
1818
  return;
1816
- const { vertices: e, uv: r } = Kt(t);
1819
+ const { vertices: e, uv: r } = qt(t);
1817
1820
  Rt(n, {
1818
1821
  ...t,
1819
1822
  points: e,
1820
1823
  uv: r,
1821
1824
  drawMode: n.gl.TRIANGLES
1822
1825
  });
1823
- }, ee = (n, t) => {
1826
+ }, re = (n, t) => {
1824
1827
  const e = Q(
1825
1828
  n,
1826
1829
  t,
@@ -1832,7 +1835,7 @@ const rt = 10, dt = (n, t, e, r, i, s) => {
1832
1835
  t.texture,
1833
1836
  e ?? t.customMatrix
1834
1837
  ), n.addRectVertex(t.texture.rawWidth, t.texture.rawHeight), n.endGraphic();
1835
- }, re = (n, t) => {
1838
+ }, ie = (n, t) => {
1836
1839
  const e = Q(n, t, t.width, t.height);
1837
1840
  n.startGraphic(
1838
1841
  n.gl.TRIANGLE_FAN,
@@ -1840,7 +1843,7 @@ const rt = 10, dt = (n, t, e, r, i, s) => {
1840
1843
  t.shader,
1841
1844
  e ?? t.customMatrix
1842
1845
  ), n.addRectVertex(t.width, t.height, t.color), n.endGraphic();
1843
- }, ie = (n, t) => {
1846
+ }, se = (n, t) => {
1844
1847
  const e = t.segments ?? 32, r = Q(n, t, 0, 0);
1845
1848
  n.startGraphic(
1846
1849
  n.gl.TRIANGLE_FAN,
@@ -1850,7 +1853,7 @@ const rt = 10, dt = (n, t, e, r, i, s) => {
1850
1853
  );
1851
1854
  const i = n.getColorUint32(t.color);
1852
1855
  n.addGraphicVertex(0, 0, 0.5, 0.5, i), n.addCircleVertex(t.radius, t.color, e), n.addGraphicVertex(t.radius, 0, 1, 0.5, i), n.endGraphic();
1853
- }, ft = `#version 300 es\r
1856
+ }, xt = `#version 300 es\r
1854
1857
  precision mediump float;\r
1855
1858
  \r
1856
1859
  uniform sampler2D uTextures[%TEXTURE_NUM%];\r
@@ -1864,27 +1867,21 @@ in vec4 vUVRect;\r
1864
1867
  out vec4 fragColor;\r
1865
1868
  in vec2 vPadding;\r
1866
1869
  \r
1867
- bool clampUV(vec2 uv) {\r
1868
- return uv.x < vUVRect.x || uv.x > vUVRect.z || uv.y < vUVRect.y || uv.y > vUVRect.w;\r
1869
- }\r
1870
- \r
1871
1870
  vec4 sampleTexture(vec2 uv) {\r
1872
1871
  %GET_COLOR%\r
1873
1872
  }\r
1874
1873
  \r
1875
1874
  vec4 sampleClampTexture(vec2 uv) {\r
1876
- if (clampUV(uv)) {\r
1877
- return vec4(0.0, 0.0, 0.0, 0.0);\r
1878
- }\r
1879
- return sampleTexture(uv);\r
1875
+ vec2 inMin = step(vUVRect.xy, uv);\r
1876
+ vec2 inMax = step(uv, vUVRect.zw);\r
1877
+ \r
1878
+ float mask = inMin.x * inMin.y * inMax.x * inMax.y;\r
1879
+ \r
1880
+ return sampleTexture(uv) * mask;\r
1880
1881
  }\r
1881
1882
  \r
1882
1883
  vec4 sampleTextureLocal(vec2 uv){\r
1883
- vec2 gUV = mix(vUVRect.xy, vUVRect.zw, uv);\r
1884
- if (clampUV(gUV)) {\r
1885
- return vec4(0.0, 0.0, 0.0, 0.0);\r
1886
- }\r
1887
- return sampleTexture(gUV);\r
1884
+ return sampleClampTexture(mix(vUVRect.xy, vUVRect.zw, uv));\r
1888
1885
  }\r
1889
1886
  \r
1890
1887
  // CUSTOM_CODE\r
@@ -1900,21 +1897,16 @@ void main(void) {\r
1900
1897
  // CUSTOM_CODE_CALL\r
1901
1898
  }\r
1902
1899
  `;
1903
- class se extends ot {
1900
+ class ne extends lt {
1904
1901
  KEY = "AtlasSprite";
1905
1902
  constructor(t) {
1906
1903
  super(t);
1907
1904
  }
1908
- createCustomShader(t) {
1909
- const e = B(ft, this.rapid.maxTextureUnits - t.usedTextureUnitNum), r = B(Z, this.rapid.maxTextureUnits - t.usedTextureUnitNum);
1910
- return t.getGLShader(this, this.KEY, r, e);
1911
- }
1912
1905
  createDefaultShader() {
1913
- const t = this.rapid, e = B(ft, t.maxTextureUnits), r = B(Z, t.maxTextureUnits);
1914
- return this.vs = r, this.fs = e, this.defaultShader = this.createShader(r, e), this.defaultShader;
1906
+ return this.vs = et, this.fs = xt, this.defaultShader = this.createShader(et, xt), this.defaultShader;
1915
1907
  }
1916
1908
  }
1917
- const mt = `#version 300 es\r
1909
+ const ae = `#version 300 es\r
1918
1910
  precision highp float;\r
1919
1911
  \r
1920
1912
  // per-vertex\r
@@ -1926,7 +1918,7 @@ in vec2 aScale;\r
1926
1918
  in float aRotation;\r
1927
1919
  in vec2 aOrigin;\r
1928
1920
  \r
1929
- // per-instance:UV 区域 (u0, v0, u1, v1)\r
1921
+ // per-instance:UV\r
1930
1922
  in vec4 aUVRect;\r
1931
1923
  \r
1932
1924
  // per-instance:tint color\r
@@ -1940,25 +1932,22 @@ out vec4 vColor;\r
1940
1932
  void main(void) {\r
1941
1933
  vColor = aColor;\r
1942
1934
  \r
1943
- //vRegion = mix(aUVRect.xy, aUVRect.zw, vertex.xy);\r
1944
1935
  vRegion = aUVRect.xy + aVertex * (aUVRect.zw - aUVRect.xy);\r
1945
- // CUSTOM_CODE_CALL\r
1946
1936
  \r
1947
1937
  vec2 scaled = (aVertex - aOrigin) * aScale;\r
1948
1938
  float c = cos(aRotation);\r
1949
1939
  float s = sin(aRotation);\r
1950
1940
  \r
1951
- vec2 v = vec2(\r
1952
- scaled.x * c - scaled.y * s,\r
1953
- scaled.x * s + scaled.y * c\r
1954
- ) + aPosition;\r
1941
+ vec4 position = vec4(mat2(c, s, -s, c) * scaled + aPosition, 0.0, 1.0);\r
1942
+ // CUSTOM_CODE_CALL\r
1955
1943
  \r
1956
- gl_Position = u_projection * vec4(v, 0.0, 1.0);\r
1944
+ gl_Position = u_projection * position;\r
1957
1945
  }\r
1958
- `, xt = `#version 300 es\r
1946
+ `, he = `#version 300 es\r
1959
1947
  precision mediump float;\r
1960
1948
  \r
1961
1949
  uniform sampler2D uTexture;\r
1950
+ uniform sampler2D uTextures[%TEXTURE_NUM%];\r
1962
1951
  \r
1963
1952
  in vec2 vRegion;\r
1964
1953
  in vec4 vColor;\r
@@ -1971,20 +1960,18 @@ void main(void) {\r
1971
1960
  \r
1972
1961
  // CUSTOM_CODE_CALL\r
1973
1962
  }\r
1974
- `, G = 48, ne = G / 4;
1975
- class ae extends ot {
1963
+ `, H = 48, oe = H / 4;
1964
+ class ce extends lt {
1976
1965
  KEY = "ParticleSprite";
1977
1966
  texture;
1978
1967
  customMatrix;
1979
1968
  particleLoopCache = /* @__PURE__ */ new Map();
1969
+ extraTextureUnit = 1;
1980
1970
  constructor(t) {
1981
1971
  super(t);
1982
1972
  }
1983
- createCustomShader(t) {
1984
- return t.getGLShader(this, this.KEY, mt, xt);
1985
- }
1986
1973
  createDefaultShader() {
1987
- const t = xt, e = mt;
1974
+ const t = he, e = ae;
1988
1975
  return this.vs = e, this.fs = t, this.defaultShader = this.createShader(e, t), this.defaultShader;
1989
1976
  }
1990
1977
  enter(t) {
@@ -1993,42 +1980,42 @@ class ae extends ot {
1993
1980
  createShader(t, e) {
1994
1981
  const r = super.createShader(t, e);
1995
1982
  return r.use(), r.bindVAO(), this.instanceBuffer.bindBuffer(), r.setAttributes([
1996
- { name: "aPosition", size: 2, stride: G, offset: 0, divisor: 1 },
1997
- { name: "aScale", size: 2, stride: G, offset: 8, divisor: 1 },
1998
- { name: "aRotation", size: 1, stride: G, offset: 16, divisor: 1 },
1999
- { name: "aUVRect", size: 4, stride: G, offset: 20, divisor: 1 },
2000
- { name: "aColor", size: 4, type: at, normalized: !0, stride: G, offset: 36, divisor: 1 },
2001
- { name: "aOrigin", size: 2, stride: G, offset: 40, divisor: 1 }
1983
+ { name: "aPosition", size: 2, stride: H, offset: 0, divisor: 1 },
1984
+ { name: "aScale", size: 2, stride: H, offset: 8, divisor: 1 },
1985
+ { name: "aRotation", size: 1, stride: H, offset: 16, divisor: 1 },
1986
+ { name: "aUVRect", size: 4, stride: H, offset: 20, divisor: 1 },
1987
+ { name: "aColor", size: 4, type: ot, normalized: !0, stride: H, offset: 36, divisor: 1 },
1988
+ { name: "aOrigin", size: 2, stride: H, offset: 40, divisor: 1 }
2002
1989
  ]), this.quadBuffer.bindBuffer(), r.setAttributes([
2003
1990
  { name: "aVertex", size: 2, stride: 8, offset: 0, divisor: 0 }
2004
1991
  ]), r.unbindVAO(), this.currentShader && this.currentShader.use(), r;
2005
1992
  }
2006
- drawParticles(t, e, r, i = 0, s = 4294967295, a, h = 1, o = 1, c = 0, u = 0, l = 1, d = 1, f = !1, m = !1, g = !1, v = 0.5, y = 0.5, w, M = !1, _) {
1993
+ 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, v = 0.5, y = 0.5, E, w = !1, _) {
2007
1994
  if (!t.glTexture || a <= 0) return;
2008
1995
  this.texture && t !== this.texture && this.flush();
2009
- const P = typeof h == "number", O = typeof o == "number", W = typeof i == "number", b = typeof s == "number", R = typeof c == "number", C = !b && typeof s[0] == "number", D = t.uvW - t.uvX, k = t.uvH - t.uvY, H = D === 0 ? 0 : 1 / D, I = k === 0 ? 0 : 1 / k;
2010
- let F = t.rawWidth * (f ? -1 : 1);
2011
- P && (F *= h);
2012
- let Y = t.rawHeight * (m ? -1 : 1);
2013
- O && (Y *= o), R && (F *= (l - c) * H, Y *= (d - u) * I);
2014
- let z = g ? Math.PI / 2 : 0;
2015
- W && (z += i);
2016
- const E = s ?? 4294967295, U = this.getParticleLoop(
2017
- P,
2018
- O,
1996
+ const L = typeof h == "number", W = typeof o == "number", O = typeof i == "number", b = typeof s == "number", M = typeof c == "number", C = !b && typeof s[0] == "number", D = t.uvW - t.uvX, B = t.uvH - t.uvY, k = D === 0 ? 0 : 1 / D, I = B === 0 ? 0 : 1 / B;
1997
+ let F = t.rawWidth * (d ? -1 : 1);
1998
+ L && (F *= h);
1999
+ let V = t.rawHeight * (x ? -1 : 1);
2000
+ W && (V *= o), M && (F *= (l - c) * k, V *= (f - u) * I);
2001
+ let z = p ? Math.PI / 2 : 0;
2002
+ O && (z += i);
2003
+ const R = s ?? 4294967295, U = this.getParticleLoop(
2004
+ L,
2019
2005
  W,
2020
- R,
2006
+ O,
2007
+ M,
2021
2008
  b,
2022
2009
  C,
2023
- M,
2024
- w
2010
+ w,
2011
+ E
2025
2012
  );
2026
2013
  this.texture || (this.texture = t, this.useTexture(t.glTexture, 0, 0));
2027
- const tt = a, X = this.instanceBuffer;
2028
- let et = X.usedElemNum;
2029
- X.resize(tt * ne);
2030
- const Mt = X.float32, St = X.uint32;
2031
- et = U(
2014
+ const it = a, X = this.instanceBuffer;
2015
+ let st = X.usedElemNum;
2016
+ X.resize(it * oe);
2017
+ const At = X.float32, St = X.uint32;
2018
+ st = U(
2032
2019
  e,
2033
2020
  r,
2034
2021
  h,
@@ -2037,22 +2024,22 @@ class ae extends ot {
2037
2024
  c,
2038
2025
  u,
2039
2026
  l,
2040
- d,
2027
+ f,
2041
2028
  s,
2042
- Mt,
2029
+ At,
2043
2030
  St,
2044
2031
  F,
2045
- Y,
2046
- H,
2032
+ V,
2033
+ k,
2047
2034
  I,
2048
2035
  z,
2049
- E,
2036
+ R,
2050
2037
  v,
2051
2038
  y,
2052
2039
  a,
2053
- tt,
2054
- et
2055
- ), X.usedElemNum = et, X.makeDirty(), this.instanceCount += tt, this.customMatrix = _, this.flush();
2040
+ it,
2041
+ st
2042
+ ), X.usedElemNum = st, X.makeDirty(), this.instanceCount += it, this.customMatrix = _, this.flush();
2056
2043
  }
2057
2044
  getParticleLoop(t, e, r, i, s, a, h, o) {
2058
2045
  const c = [...arguments].join("_");
@@ -2108,25 +2095,25 @@ return idx;
2108
2095
  this.texture && (t.activeTexture(t.TEXTURE0), t.bindTexture(t.TEXTURE_2D, this.texture.glTexture));
2109
2096
  const e = this.currentShader;
2110
2097
  this.instanceBuffer.bindBuffer(), this.instanceBuffer.bufferData(), e.bindVAO();
2111
- const r = this.rapid.matrixStack, s = this.rapid.matrix.getMatrix(this.customMatrix ?? r.curWorldM), a = Xt(this.rapid.projection, s);
2098
+ const r = this.rapid.matrixStack, s = this.rapid.matrix.getMatrix(this.customMatrix ?? r.curWorldM), a = Yt(this.rapid.projection, s);
2112
2099
  this.currentShader.setUniform("u_projection", a), yt(t, t.TRIANGLE_STRIP, 0, 4, this.instanceCount), this.rapid.drawcallCount++, this.texture = void 0, this.customMatrix = void 0;
2113
2100
  }
2114
2101
  }
2115
- var he = /* @__PURE__ */ ((n) => (n[n.EQUAL = 0] = "EQUAL", n[n.NOT_EQUAL = 1] = "NOT_EQUAL", n))(he || {}), oe = /* @__PURE__ */ ((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))(oe || {}), ht = /* @__PURE__ */ ((n) => (n[n.LINEAR = 0] = "LINEAR", n[n.NEAREST = 1] = "NEAREST", n))(ht || {}), ce = /* @__PURE__ */ ((n) => (n[n.CanvasItem = 0] = "CanvasItem", n[n.Viewport = 1] = "Viewport", n))(ce || {});
2116
- class de {
2102
+ var Mt = /* @__PURE__ */ ((n) => (n[n.EQUAL = 0] = "EQUAL", n[n.NOT_EQUAL = 1] = "NOT_EQUAL", n))(Mt || {}), ue = /* @__PURE__ */ ((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))(ue || {}), le = /* @__PURE__ */ ((n) => (n[n.CanvasItem = 0] = "CanvasItem", n[n.Viewport = 1] = "Viewport", n))(le || {});
2103
+ class pe {
2117
2104
  gl;
2118
2105
  canvas;
2119
2106
  projection = new Float32Array(16);
2120
2107
  projectionDirty = !0;
2121
2108
  dpr;
2122
- backgroundColor = x.Black;
2109
+ backgroundColor = m.Black;
2123
2110
  logicWidth = 0;
2124
2111
  logicHeight = 0;
2125
2112
  physicsWidth = 0;
2126
2113
  physicsHeight = 0;
2127
2114
  currentRegion = null;
2128
2115
  maxTextureUnits = 0;
2129
- matrixStack = new bt(this);
2116
+ matrixStack = new kt(this);
2130
2117
  matrix;
2131
2118
  spriteRegion;
2132
2119
  graphicRegion;
@@ -2149,38 +2136,38 @@ class de {
2149
2136
  return this.logicWidth;
2150
2137
  }
2151
2138
  constructor(t) {
2152
- this.canvas = t.canvas, this.dpr = window.devicePixelRatio || 1, this.antialias = t.antialias ?? !1, this.textureFilter = t.textureFilter ?? 1, this.premultipliedAlpha = t.premultipliedAlpha ?? !0, this.roundPixels = t.roundPixels ?? !1, this.scaleMode = t.scaleMode ?? 0;
2139
+ 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;
2153
2140
  const e = Lt(this.canvas, this.antialias, this.premultipliedAlpha);
2154
- this.gl = e, this.maxTextureUnits = e.getParameter(e.MAX_TEXTURE_IMAGE_UNITS), this.matrix = this.matrixStack.matrix, this.spriteRegion = new ot(this), this.graphicRegion = new Yt(this), this.atlasSpriteRegion = new se(this), this.particleRegion = new ae(this);
2141
+ 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 zt(this), this.atlasSpriteRegion = new ne(this), this.particleRegion = new ce(this);
2155
2142
  const r = this.canvas.clientWidth || this.canvas.width, i = this.canvas.clientHeight || this.canvas.height;
2156
- 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 Ut(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);
2143
+ 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 bt(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);
2157
2144
  }
2158
2145
  setTextureFilter(t) {
2159
2146
  this.textureFilter = t;
2160
2147
  }
2161
2148
  getColorUint32(t) {
2162
- return qt(this.premultipliedAlpha, t);
2149
+ return Qt(this.premultipliedAlpha, t);
2163
2150
  }
2164
2151
  enterRegion(t, e) {
2165
2152
  this.currentRegion === t && this.currentRegion.isSameShader(e) || (this.flush(), this.currentRegion = t, t.enter(e));
2166
2153
  }
2167
2154
  drawParticles(t) {
2168
- Jt(this, t);
2155
+ te(this, t);
2169
2156
  }
2170
2157
  drawSprite(t) {
2171
- Zt(this, t);
2158
+ Jt(this, t);
2172
2159
  }
2173
2160
  drawLine(t) {
2174
- te(this, t);
2161
+ ee(this, t);
2175
2162
  }
2176
2163
  drawGraphic(t) {
2177
2164
  Rt(this, t);
2178
2165
  }
2179
2166
  drawRect(t) {
2180
- re(this, t);
2167
+ ie(this, t);
2181
2168
  }
2182
2169
  drawCircle(t) {
2183
- ie(this, t);
2170
+ se(this, t);
2184
2171
  }
2185
2172
  startGraphic(t = this.gl.TRIANGLES, e, r, i) {
2186
2173
  this.enterRegion(this.graphicRegion, r), this.graphicRegion.startGraphic(i ?? this.matrixStack.curWorldM, t, e);
@@ -2189,7 +2176,7 @@ class de {
2189
2176
  this.startGraphic(t, e, this.graphicRegion.maskShader, r);
2190
2177
  }
2191
2178
  drawMaskImage(t) {
2192
- ee(this, t);
2179
+ re(this, t);
2193
2180
  }
2194
2181
  addRectVertex(t, e, r) {
2195
2182
  const i = this.getColorUint32(r);
@@ -2202,7 +2189,7 @@ class de {
2202
2189
  this.addGraphicVertex(o, c, u, l, s);
2203
2190
  }
2204
2191
  }
2205
- addGraphicVertex(t, e, r = 0, i = 0, s) {
2192
+ addGraphicVertex(t, e, r = 0, i = 0, s = 4294967295) {
2206
2193
  this.graphicRegion.addVertex(t, e, r, i, typeof s == "number" ? s : this.getColorUint32(s));
2207
2194
  }
2208
2195
  endGraphic() {
@@ -2258,9 +2245,9 @@ class de {
2258
2245
  return this.matrixStack.restore(), h.offsetX = -i, h.offsetY = -i, h;
2259
2246
  }
2260
2247
  enterRenderTexture(t) {
2261
- this.flush(), t.activate(), this.gl.viewport(0, 0, t.rawWidth, t.rawHeight), this.updateProjection(0, t.rawWidth, t.rawHeight, 0);
2248
+ this.flush(), t.activate(), this.gl.viewport(0, 0, t.rawWidth, t.rawHeight), this.updateProjection(0, t.rawWidth, 0, t.rawHeight);
2262
2249
  }
2263
- clearRenderTexture(t = new x(0, 0, 0, 0)) {
2250
+ clearRenderTexture(t = new m(0, 0, 0, 0)) {
2264
2251
  this.flush();
2265
2252
  const e = this.gl;
2266
2253
  t.setClearColor(e), e.clear(e.COLOR_BUFFER_BIT);
@@ -2268,7 +2255,7 @@ class de {
2268
2255
  leaveRenderTexture() {
2269
2256
  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);
2270
2257
  }
2271
- drawToRenderTexture(t, e, r = new x(0, 0, 0, 0)) {
2258
+ drawToRenderTexture(t, e, r = new m(0, 0, 0, 0)) {
2272
2259
  this.enterRenderTexture(t);
2273
2260
  try {
2274
2261
  r !== null && this.clearRenderTexture(r), e();
@@ -2377,13 +2364,13 @@ class de {
2377
2364
  this.matrix.invert(e);
2378
2365
  }
2379
2366
  logicToPhysics(t) {
2380
- return t.multiply(new p(
2367
+ return t.multiply(new g(
2381
2368
  this.physicsWidth / this.logicWidth,
2382
2369
  this.physicsHeight / this.logicHeight
2383
2370
  ));
2384
2371
  }
2385
2372
  physicsToLogic(t) {
2386
- return t.multiply(new p(
2373
+ return t.multiply(new g(
2387
2374
  this.logicWidth / this.physicsWidth,
2388
2375
  this.logicHeight / this.physicsHeight
2389
2376
  ));
@@ -2403,10 +2390,10 @@ class de {
2403
2390
  return this.matrix.toCSSMatrix(t ?? this.matrixStack.curWorldM, e, r);
2404
2391
  }
2405
2392
  }
2406
- function ue(n) {
2407
- return !(n === null || typeof n != "object" || Array.isArray(n) || n instanceof p || n instanceof x);
2393
+ function de(n) {
2394
+ return !(n === null || typeof n != "object" || Array.isArray(n) || n instanceof g || n instanceof m);
2408
2395
  }
2409
- class L {
2396
+ class P {
2410
2397
  static float(t, e) {
2411
2398
  return Math.random() * (e - t) + t;
2412
2399
  }
@@ -2417,18 +2404,18 @@ class L {
2417
2404
  return Math.random() * Math.PI * 2;
2418
2405
  }
2419
2406
  static vector(t, e, r, i) {
2420
- return new p(L.float(t, e), L.float(r, i));
2407
+ return new g(P.float(t, e), P.float(r, i));
2421
2408
  }
2422
2409
  static randomColor(t, e) {
2423
- return new x(
2424
- L.float(t.r, e.r),
2425
- L.float(t.g, e.g),
2426
- L.float(t.b, e.b),
2427
- L.float(t.a, e.a)
2410
+ return new m(
2411
+ P.float(t.r, e.r),
2412
+ P.float(t.g, e.g),
2413
+ P.float(t.b, e.b),
2414
+ P.float(t.a, e.a)
2428
2415
  );
2429
2416
  }
2430
2417
  static pick(t) {
2431
- return t[L.int(0, t.length - 1)];
2418
+ return t[P.int(0, t.length - 1)];
2432
2419
  }
2433
2420
  static pickWeight(t) {
2434
2421
  if (!t || t.length === 0) return null;
@@ -2444,26 +2431,27 @@ class L {
2444
2431
  if (Array.isArray(t)) {
2445
2432
  const [r, i] = t;
2446
2433
  if (typeof r == "number")
2447
- return L.float(r, i);
2448
- if (r instanceof p)
2449
- return L.vector(r.x, i.x, r.y, i.y);
2450
- if (r instanceof x)
2451
- return L.randomColor(r, i);
2434
+ return P.float(r, i);
2435
+ if (r instanceof g)
2436
+ return P.vector(r.x, i.x, r.y, i.y);
2437
+ if (r instanceof m)
2438
+ return P.randomColor(r, i);
2452
2439
  }
2453
2440
  return typeof t == "number" ? t : t.clone();
2454
2441
  }
2455
2442
  }
2456
- var le = /* @__PURE__ */ ((n) => (n.POINT = "point", n.CIRCLE = "circle", n.RECT = "rect", n))(le || {});
2457
- const pt = 10, gt = 0, vt = !0;
2458
- class K {
2443
+ const Z = (n, t) => (Array.isArray(n) ? n : [n]).slice(0, t);
2444
+ var fe = /* @__PURE__ */ ((n) => (n.POINT = "point", n.CIRCLE = "circle", n.RECT = "rect", n))(fe || {});
2445
+ const gt = 10, pt = 0, vt = !0;
2446
+ class j {
2459
2447
  components;
2460
2448
  constructor(t = 1) {
2461
2449
  if (!Number.isInteger(t) || t <= 0)
2462
2450
  throw new RangeError("ParticleAttributeStore size must be a positive integer");
2463
2451
  this.components = Array.from({ length: t }, () => ({
2464
- value: new A(S.Float32),
2465
- delta: new A(S.Float32),
2466
- damping: new A(S.Float32)
2452
+ value: new S(A.Float32),
2453
+ delta: new S(A.Float32),
2454
+ damping: new S(A.Float32)
2467
2455
  }));
2468
2456
  }
2469
2457
  getArrayBuffer() {
@@ -2480,7 +2468,7 @@ class K {
2480
2468
  getComponent(t, e) {
2481
2469
  if (typeof t == "number")
2482
2470
  return t;
2483
- if (t instanceof p)
2471
+ if (t instanceof g)
2484
2472
  return e === 0 ? t.x : t.y;
2485
2473
  switch (e) {
2486
2474
  case 0:
@@ -2496,17 +2484,17 @@ class K {
2496
2484
  }
2497
2485
  }
2498
2486
  resolveComponent(t, e, r) {
2499
- return t === void 0 ? r : Array.isArray(t) ? L.float(
2487
+ return t === void 0 ? r : Array.isArray(t) ? P.float(
2500
2488
  this.getComponent(t[0], e),
2501
2489
  this.getComponent(t[1], e)
2502
2490
  ) : this.getComponent(t, e);
2503
2491
  }
2504
2492
  create(t, e, r = 1) {
2505
- const i = ue(t) ? t : void 0, s = i === void 0 ? t : void 0, a = r > 0 ? r : 1;
2493
+ const i = de(t) ? t : void 0, s = i === void 0 ? t : void 0, a = r > 0 ? r : 1;
2506
2494
  let h = 0;
2507
2495
  for (let o = 0; o < this.components.length; o++) {
2508
- const { value: c, delta: u, damping: l } = this.components[o], d = this.getComponent(e, o), f = s === void 0 ? this.resolveComponent(i?.start, o, d) : this.getComponent(s, o), m = i?.end === void 0 ? f : this.resolveComponent(i.end, o, d);
2509
- c.push(f), u.push(i?.delta === void 0 ? (m - f) / a : this.getComponent(i.delta, o)), l.push(i?.damping ?? 1), o === 0 && (h = f);
2496
+ const { value: c, delta: u, damping: l } = this.components[o], f = this.getComponent(e, o), d = s === void 0 ? this.resolveComponent(i?.start, o, f) : this.getComponent(s, o), x = i?.end === void 0 ? d : this.resolveComponent(i.end, o, f);
2497
+ 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);
2510
2498
  }
2511
2499
  return h;
2512
2500
  }
@@ -2515,28 +2503,28 @@ class wt {
2515
2503
  options;
2516
2504
  emitting = !1;
2517
2505
  emitTimer = 0;
2518
- emitRate = pt;
2519
- emitTime = gt;
2506
+ emitRate = gt;
2507
+ emitTime = pt;
2520
2508
  emitTimeCounter = 0;
2521
2509
  localSpace = vt;
2522
2510
  rapid;
2523
- x = new A(S.Float32);
2524
- y = new A(S.Float32);
2525
- scaleX = new A(S.Float32);
2526
- scaleY = new A(S.Float32);
2527
- rotation = new A(S.Float32);
2528
- color = new A(S.Uint32);
2529
- remainingLife = new A(S.Float32);
2511
+ x = new S(A.Float32);
2512
+ y = new S(A.Float32);
2513
+ scaleX = new S(A.Float32);
2514
+ scaleY = new S(A.Float32);
2515
+ rotation = new S(A.Float32);
2516
+ color = new S(A.Uint32);
2517
+ remainingLife = new S(A.Float32);
2530
2518
  animations = {
2531
- speed: new K(),
2532
- rotation: new K(),
2533
- scale: new K(),
2534
- color: new K(4),
2535
- velocity: new K(2)
2519
+ speed: new j(),
2520
+ rotation: new j(),
2521
+ scale: new j(),
2522
+ color: new j(4),
2523
+ velocity: new j(2)
2536
2524
  };
2537
2525
  count = 0;
2538
2526
  constructor(t, e) {
2539
- this.rapid = t, this.options = e, this.emitRate = e.emitRate ?? pt, this.emitTime = e.emitTime ?? gt, this.localSpace = e.localSpace ?? vt;
2527
+ this.rapid = t, this.options = e, this.emitRate = e.emitRate ?? gt, this.emitTime = e.emitTime ?? pt, this.localSpace = e.localSpace ?? vt;
2540
2528
  }
2541
2529
  getAllArrayBuffer() {
2542
2530
  return [
@@ -2579,7 +2567,7 @@ class wt {
2579
2567
  this.createParticle();
2580
2568
  }
2581
2569
  createParticle() {
2582
- const t = L.scalarOrRange(this.options.life, 1);
2570
+ const t = P.scalarOrRange(this.options.life, 1);
2583
2571
  this.remainingLife.push(t);
2584
2572
  let e = 0, r = 0;
2585
2573
  switch (this.options.emitShape) {
@@ -2601,7 +2589,7 @@ class wt {
2601
2589
  this.x.push(c), this.y.push(u);
2602
2590
  }
2603
2591
  const s = this.options.animation, a = this.animations, h = a.scale.create(s.scale, 1, t), o = a.rotation.create(s.rotation, 0, t);
2604
- a.speed.create(s.speed, 0, t), a.velocity.create(s.velocity, p.ZERO, t), a.color.create(s.color, x.White, t), this.rotation.push(o), this.scaleX.push(h), this.scaleY.push(h), this.color.pushUint32(x.packColor(
2592
+ a.speed.create(s.speed, 0, t), a.velocity.create(s.velocity, g.ZERO, t), a.color.create(s.color, m.White, t), this.rotation.push(o), this.scaleX.push(h), this.scaleY.push(h), this.color.pushUint32(m.packColor(
2605
2593
  a.color.get(this.count, 0),
2606
2594
  a.color.get(this.count, 1),
2607
2595
  a.color.get(this.count, 2),
@@ -2622,7 +2610,7 @@ class wt {
2622
2610
  r > 0 && (this.emit(r), this.emitTimer -= r / this.emitRate);
2623
2611
  }
2624
2612
  const e = this.updateParticle(t);
2625
- this.count -= e.length, A.removeAtIndices(e, this.getAllArrayBuffer());
2613
+ this.count -= e.length, S.removeAtIndices(e, this.getAllArrayBuffer());
2626
2614
  }
2627
2615
  updateParticle(t) {
2628
2616
  const e = [], r = this.animations;
@@ -2633,7 +2621,7 @@ class wt {
2633
2621
  continue;
2634
2622
  }
2635
2623
  const s = r.rotation.get(i), a = r.scale.get(i), h = r.speed.get(i);
2636
- this.rotation.typedArray[i] = s, this.scaleX.typedArray[i] = a, this.scaleY.typedArray[i] = a, this.color.uint32[i] = x.packColor(
2624
+ this.rotation.typedArray[i] = s, this.scaleX.typedArray[i] = a, this.scaleY.typedArray[i] = a, this.color.uint32[i] = m.packColor(
2637
2625
  r.color.get(i, 0),
2638
2626
  r.color.get(i, 1),
2639
2627
  r.color.get(i, 2),
@@ -2668,43 +2656,163 @@ class wt {
2668
2656
  this.emit(this.emitRate);
2669
2657
  }
2670
2658
  }
2659
+ const me = `
2660
+ uniform mediump int uLightTextureId[%TEXTURE_NUM%];
2661
+
2662
+ uniform sampler2D uNormalMap;
2663
+ uniform vec2 uResolution;
2664
+ uniform vec2 uLogicalResolution;
2665
+ uniform mediump int uLightCount;
2666
+ uniform vec4 uLightColor[%TEXTURE_NUM%];
2667
+ uniform mat3x2 uLightMatrix[%TEXTURE_NUM%];
2668
+ uniform float uLightHeight[%TEXTURE_NUM%];
2669
+ uniform float uMetallic;
2670
+ uniform float uRoughness;
2671
+ uniform vec3 uAmbientColor;
2672
+ uniform vec2 uNormalScale;
2673
+
2674
+ void light_fragment(inout vec4 color, in vec2 vRegion) {
2675
+ if (color.a < 0.005) return;
2676
+
2677
+ vec2 worldPos = vec2(gl_FragCoord.x, uResolution.y - gl_FragCoord.y) * (uLogicalResolution / uResolution);
2678
+ vec3 normal = normalize(texture(uNormalMap, vRegion).rgb * 2.0 - 1.0);
2679
+ normal = normalize(normal * vec3(uNormalScale, 1.0));
2680
+
2681
+ vec3 totalDiffuse = vec3(0.0);
2682
+ vec3 totalSpecular = vec3(0.0);
2683
+ vec3 viewDir = vec3(0.0, 0.0, 1.0);
2684
+ vec3 specColor = mix(vec3(1.0), color.rgb, uMetallic);
2685
+ float shininess = exp2((1.0 - uRoughness) * 7.0 + 1.0);
2686
+
2687
+ for (int i = 0; i < %TEXTURE_NUM%; i++) {
2688
+ if (i >= uLightCount) break;
2689
+
2690
+ vec2 lightPos = uLightMatrix[i][2];
2691
+ mat2 lightBasis = mat2(uLightMatrix[i][0], uLightMatrix[i][1]);
2692
+ vec2 lightUV = inverse(lightBasis) * (worldPos - lightPos) + 0.5;
2693
+
2694
+ if (lightUV.x >= 0.0 && lightUV.x <= 1.0 && lightUV.y >= 0.0 && lightUV.y <= 1.0) {
2695
+
2696
+ int layer = uLightTextureId[i];
2697
+ vec4 lightTex;
2698
+ %GET_COLOR%
2699
+
2700
+ vec3 lightDir = normalize(vec3(lightPos - worldPos, uLightHeight[i]));
2701
+ float diff = max(dot(normal, lightDir), 0.0);
2702
+ float spec = pow(max(dot(normal, normalize(lightDir + viewDir)), 0.0), shininess);
2703
+ vec3 attenuation = lightTex.rgb * lightTex.a;
2704
+
2705
+ totalDiffuse += uLightColor[i].rgb * diff * attenuation * (1.0 - uMetallic);
2706
+ totalSpecular += specColor * uLightColor[i].rgb * spec * attenuation * color.a;
2707
+ }
2708
+ }
2709
+
2710
+ color.rgb = color.rgb * (uAmbientColor + totalDiffuse) + totalSpecular;
2711
+ }
2712
+ `, xe = "void light_vertex(inout vec4 p, vec2 u) {}", J = 8;
2713
+ class ge extends ct {
2714
+ constructor(t, e = "", r = "", i = 0, s) {
2715
+ super(
2716
+ t,
2717
+ e,
2718
+ r,
2719
+ i + 2,
2720
+ s
2721
+ ), this.prefix.push("light_");
2722
+ }
2723
+ getGLShader(t, e, r, i) {
2724
+ const s = K(me, t.getMaxTexutreUnit(this), "layer", "lightUV", "lightTex = ");
2725
+ return i = this.replaceCustomCode(i, s), r = this.replaceCustomCode(r, xe), super.getGLShader(t, e, r, i);
2726
+ }
2727
+ }
2728
+ class ve {
2729
+ shader;
2730
+ rapid;
2731
+ rt;
2732
+ constructor(t) {
2733
+ this.rapid = t, this.shader = new ge(t), this.rt = t.texture.createRenderTexture({ width: 0, height: 0 });
2734
+ }
2735
+ lightShadow(t, e, r) {
2736
+ if (r.length === 0) return;
2737
+ const i = this.rapid, s = typeof e == "number" ? e : e.world, a = new g(t.rawWidth * 0.5, t.rawHeight * 0.5), h = r.map((c) => {
2738
+ const u = i.matrixStack.localToWorld(c.x, c.y), l = i.matrix.worldToLocal(s, u.x, u.y);
2739
+ return new g(
2740
+ // 要把LightTexture的基向量转化为RenderTexture的坐标
2741
+ // 纹理是从左上角开始的,所以要 + 0.5
2742
+ (l.x + 0.5) * t.rawWidth,
2743
+ (l.y + 0.5) * t.rawHeight
2744
+ );
2745
+ });
2746
+ this.rt.resize(t.rawWidth, t.rawHeight);
2747
+ const o = Math.hypot(t.rawWidth, t.rawHeight) * 0.5 * 1.05;
2748
+ return i.drawToRenderTexture(this.rt, () => {
2749
+ i.withMask(() => {
2750
+ i.startMaskGraphic(i.gl.TRIANGLES);
2751
+ for (let c = 0; c < h.length; c++) {
2752
+ const u = h[c], l = h[(c + 1) % h.length], f = u.subtract(a).normalized(), d = l.subtract(a).normalized(), x = Math.max(-0.999, Math.min(1, f.dot(d))), p = Math.sqrt((1 + x) * 0.5), v = o / p, y = a.add(f.multiply(v)), E = a.add(d.multiply(v));
2753
+ i.addGraphicVertex(u.x, u.y), i.addGraphicVertex(l.x, l.y), i.addGraphicVertex(E.x, E.y), i.addGraphicVertex(u.x, u.y), i.addGraphicVertex(E.x, E.y), i.addGraphicVertex(y.x, y.y);
2754
+ }
2755
+ i.endGraphic();
2756
+ }, () => {
2757
+ i.drawSprite({ texture: t });
2758
+ }, Mt.NOT_EQUAL);
2759
+ }), this.rt;
2760
+ }
2761
+ setupLight(t) {
2762
+ 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);
2763
+ return e.setUniforms({
2764
+ uResolution: [this.rapid.physicsWidth, this.rapid.physicsHeight],
2765
+ uLogicalResolution: [this.rapid.logicWidth, this.rapid.logicHeight],
2766
+ uNormalMap: t.normalMap.glTexture,
2767
+ uLightTextureId: a.map((h) => h.glTexture),
2768
+ uLightCount: r.length,
2769
+ uLightMatrix: Float32Array.from(r.flatMap(
2770
+ (h) => Array.from(this.rapid.matrix.getMatrix(typeof h == "number" ? h : h.world))
2771
+ )),
2772
+ uLightColor: Float32Array.from(r.flatMap((h, o) => {
2773
+ const c = i[o] ?? i[0];
2774
+ return c ? [c.r / 255, c.g / 255, c.b / 255, c.a / 255] : [1, 1, 1, 1];
2775
+ })),
2776
+ uLightHeight: Float32Array.from(r.map((h, o) => s[o] ?? s[0] ?? 80)),
2777
+ uMetallic: t.metallic ?? 0.8,
2778
+ uRoughness: t.roughness ?? 0.15,
2779
+ uAmbientColor: t.ambientColor ? [t.ambientColor.r / 255, t.ambientColor.g / 255, t.ambientColor.b / 255] : [0.1, 0.1, 0.15],
2780
+ uNormalScale: [t.normalScaleX ?? 1, t.normalScaleY ?? 1]
2781
+ }), e;
2782
+ }
2783
+ }
2671
2784
  export {
2672
- S as ArrayType,
2673
- V as BaseTexture,
2674
- oe as BlendMode,
2675
- ce as CanvasScaleMode,
2676
- x as Color,
2677
- ut as CustomGlShader,
2678
- A as DynamicArrayBuffer,
2785
+ G as BaseTexture,
2786
+ ue as BlendMode,
2787
+ le as CanvasScaleMode,
2788
+ m as Color,
2789
+ ct as CustomGlShader,
2679
2790
  Tt as GLShader,
2680
- Yt as GraphicRegion,
2681
- $t as LineTextureMode,
2682
- he as MaskType,
2683
- bt as MatrixStack,
2684
- At as MatrixStore,
2791
+ ve as Light,
2792
+ ge as LightShader,
2793
+ jt as LineTextureMode,
2794
+ J as MAX_LIGHTS,
2795
+ Mt as MaskType,
2685
2796
  wt as ParticleEmitter,
2686
- ae as ParticleRegion,
2687
- le as ParticleShape,
2688
- Dt as QUAD_VERTICES,
2689
- de as Rapid,
2690
- Et as Region,
2691
- Ct as RenderTexture,
2692
- ot as SpriteRegion,
2693
- Pt as TextTexture,
2797
+ fe as ParticleShape,
2798
+ pe as Rapid,
2799
+ Ut as RenderTexture,
2800
+ _t as TextTexture,
2694
2801
  N as Texture,
2695
- ht as TextureFilterMode,
2696
- Ut as TextureManager,
2802
+ tt as TextureFilterMode,
2803
+ bt as TextureManager,
2697
2804
  q as TextureWrapMode,
2698
- p as Vec2,
2699
- it as WebglBufferArray,
2700
- Xt as composeProjectionWithAffine,
2701
- ie as drawCircle,
2805
+ g as Vec2,
2806
+ Yt as composeProjectionWithAffine,
2807
+ se as drawCircle,
2702
2808
  Rt as drawGraphic,
2703
- te as drawLine,
2704
- ee as drawMaskImage,
2705
- Jt as drawParticles,
2706
- re as drawRect,
2707
- Zt as drawSprite,
2708
- qt as getColorUint32,
2709
- zt as multiplyMat4
2809
+ ee as drawLine,
2810
+ re as drawMaskImage,
2811
+ te as drawParticles,
2812
+ ie as drawRect,
2813
+ Jt as drawSprite,
2814
+ Qt as getColorUint32,
2815
+ qt as getLineGeometry,
2816
+ Kt as getLineNormal,
2817
+ $t as multiplyMat4
2710
2818
  };