rapid-render 1.0.9 → 1.0.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/rapid-render.js +142 -141
- package/dist/rapid-render.umd.cjs +6 -6
- package/dist/webgl/glshader.d.ts +1 -0
- package/package.json +1 -1
package/dist/rapid-render.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
var
|
|
2
|
-
class
|
|
1
|
+
var b = /* @__PURE__ */ ((n) => (n[n.Float32 = 0] = "Float32", n[n.Uint32 = 1] = "Uint32", n[n.Uint16 = 2] = "Uint16", n))(b || {});
|
|
2
|
+
class _ {
|
|
3
3
|
/** The number of elements currently used in the buffer. */
|
|
4
4
|
usedElemNum;
|
|
5
5
|
/** The main typed array view corresponding to the specified ArrayType. */
|
|
@@ -172,7 +172,7 @@ class P {
|
|
|
172
172
|
this.usedElemNum = 0;
|
|
173
173
|
}
|
|
174
174
|
}
|
|
175
|
-
class et extends
|
|
175
|
+
class et extends _ {
|
|
176
176
|
/** The actual WebGLBuffer object maintained by this instance. */
|
|
177
177
|
buffer;
|
|
178
178
|
/** The related WebGL rendering context. */
|
|
@@ -480,7 +480,7 @@ class x {
|
|
|
480
480
|
static TRANSPARENT = new x(0, 0, 0, 0);
|
|
481
481
|
}
|
|
482
482
|
const E = 6;
|
|
483
|
-
class
|
|
483
|
+
class wt {
|
|
484
484
|
/** Total number of matrices currently allocated in the store. */
|
|
485
485
|
matrixCount = 0;
|
|
486
486
|
/** The dynamic buffer used to hold matrix data. */
|
|
@@ -493,7 +493,7 @@ class Mt {
|
|
|
493
493
|
* @param capacity - The initial capacity of the matrix store (default is 10).
|
|
494
494
|
*/
|
|
495
495
|
constructor(t = 10) {
|
|
496
|
-
this.buffer = new
|
|
496
|
+
this.buffer = new _(b.Float32), this.buffer.resize(t * E), this.data = this.buffer.getArray(), this.reset();
|
|
497
497
|
}
|
|
498
498
|
/**
|
|
499
499
|
* Allocates a new matrix and initializes it to the identity matrix.
|
|
@@ -731,19 +731,19 @@ class Mt {
|
|
|
731
731
|
}
|
|
732
732
|
class At {
|
|
733
733
|
/** The underlying store for matrices. */
|
|
734
|
-
matrix = new
|
|
734
|
+
matrix = new wt();
|
|
735
735
|
/** The current step or depth in the transformation hierarchy. */
|
|
736
736
|
step = 0;
|
|
737
737
|
/** Internal stack maintaining structural information. */
|
|
738
|
-
stack = new
|
|
738
|
+
stack = new _(b.Uint32);
|
|
739
739
|
/** Records the world matrices generated at each step. */
|
|
740
|
-
stepWorldM = new
|
|
740
|
+
stepWorldM = new _(b.Uint32);
|
|
741
741
|
/** Records actions (push/pop) taken during the traversal. */
|
|
742
|
-
stepAction = new
|
|
742
|
+
stepAction = new _(b.Uint32);
|
|
743
743
|
/** Records the parent world matrix for each step (used by updateMatrix). */
|
|
744
|
-
stepParentM = new
|
|
744
|
+
stepParentM = new _(b.Uint32);
|
|
745
745
|
/** Buffer used during hierarchy traversal updates. */
|
|
746
|
-
parentStack = new
|
|
746
|
+
parentStack = new _(b.Uint32);
|
|
747
747
|
/** The index of the current local matrix in the matrix store. */
|
|
748
748
|
curLocalM = -1;
|
|
749
749
|
/** The index of the current world matrix in the matrix store. */
|
|
@@ -763,30 +763,29 @@ class At {
|
|
|
763
763
|
save() {
|
|
764
764
|
this.stack.push(this.curWorldM);
|
|
765
765
|
const t = this.curWorldM;
|
|
766
|
-
return this.
|
|
766
|
+
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.matrix.copy(this.curWorldM, t), { world: this.curWorldM, local: this.curLocalM, step: this.step++ };
|
|
767
767
|
}
|
|
768
768
|
/**
|
|
769
769
|
* Restores the matrix state from the top of the stack.
|
|
770
770
|
* Equivalent to context.restore().
|
|
771
771
|
*/
|
|
772
772
|
restore() {
|
|
773
|
-
this.stack.length != 0 && (this.curWorldM = this.stack.pop(), this.curLocalM = this.curWorldM - 1, this.stepAction.push(0));
|
|
773
|
+
this.stack.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.step++);
|
|
774
774
|
}
|
|
775
775
|
/**
|
|
776
776
|
* Evaluates and updates matrices from the given step. Used primarily for deferred transformation.
|
|
777
777
|
* @param step - The initial step to update matrices from.
|
|
778
778
|
*/
|
|
779
779
|
updateMatrix(t) {
|
|
780
|
-
const e =
|
|
781
|
-
let
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
} else if (a.pop(), i--, i === 0)
|
|
780
|
+
const e = typeof t == "number" ? t : t.step;
|
|
781
|
+
let r = 0, i = e;
|
|
782
|
+
for (; i < this.step; ) {
|
|
783
|
+
if (this.stepAction.get(i) === 1) {
|
|
784
|
+
const a = this.stepWorldM.get(i), o = a - 1, h = this.stepParentM.get(i);
|
|
785
|
+
this.matrix.multiplyOut(a, h, o), r++;
|
|
786
|
+
} else if (r--, r === 0)
|
|
788
787
|
break;
|
|
789
|
-
|
|
788
|
+
i++;
|
|
790
789
|
}
|
|
791
790
|
}
|
|
792
791
|
/**
|
|
@@ -920,7 +919,7 @@ class At {
|
|
|
920
919
|
t.offset && (l += t.offset.x, d += t.offset.y);
|
|
921
920
|
const f = t.origin;
|
|
922
921
|
f !== void 0 && (typeof f == "number" ? (l -= f * e, d -= f * r) : (l -= f.x * e, d -= f.y * r));
|
|
923
|
-
const m = u ? Math.cos(u) : 1, g = u ? Math.sin(u) : 0, v = m * o, y = g * o, R = -g * h, A = m * h,
|
|
922
|
+
const m = u ? Math.cos(u) : 1, g = u ? Math.sin(u) : 0, v = m * o, y = g * o, R = -g * h, A = m * h, P = s + v * l + R * d, U = a + y * l + A * d;
|
|
924
923
|
if (i !== -1) {
|
|
925
924
|
this.matrix.multiplyAffine(
|
|
926
925
|
this.curWorldM,
|
|
@@ -929,8 +928,8 @@ class At {
|
|
|
929
928
|
y,
|
|
930
929
|
R,
|
|
931
930
|
A,
|
|
932
|
-
|
|
933
|
-
|
|
931
|
+
P,
|
|
932
|
+
U
|
|
934
933
|
);
|
|
935
934
|
return;
|
|
936
935
|
}
|
|
@@ -940,16 +939,16 @@ class At {
|
|
|
940
939
|
y,
|
|
941
940
|
R,
|
|
942
941
|
A,
|
|
943
|
-
|
|
944
|
-
|
|
942
|
+
P,
|
|
943
|
+
U
|
|
945
944
|
), this.matrix.multiplyAffineInPlace(
|
|
946
945
|
this.curWorldM,
|
|
947
946
|
v,
|
|
948
947
|
y,
|
|
949
948
|
R,
|
|
950
949
|
A,
|
|
951
|
-
|
|
952
|
-
|
|
950
|
+
P,
|
|
951
|
+
U
|
|
953
952
|
);
|
|
954
953
|
}
|
|
955
954
|
}
|
|
@@ -969,10 +968,10 @@ class St {
|
|
|
969
968
|
async load(t, e) {
|
|
970
969
|
let r = this.cache.get(t);
|
|
971
970
|
if (r)
|
|
972
|
-
return new
|
|
971
|
+
return new W(r);
|
|
973
972
|
try {
|
|
974
973
|
const i = await this._fetchImage(t);
|
|
975
|
-
return r =
|
|
974
|
+
return r = X.fromSource(this.render, i, e), r.uid = t, this.cache.set(t, r), new W(r);
|
|
976
975
|
} catch (i) {
|
|
977
976
|
throw console.error(`[TextureManager] Failed to load: ${t}`, i), i;
|
|
978
977
|
}
|
|
@@ -985,9 +984,9 @@ class St {
|
|
|
985
984
|
*/
|
|
986
985
|
create(t, e) {
|
|
987
986
|
if (e?.key && this.cache.has(e.key))
|
|
988
|
-
return new
|
|
989
|
-
const r =
|
|
990
|
-
return e?.key && (r.uid = e.key, this.cache.set(e.key, r)), new
|
|
987
|
+
return new W(this.cache.get(e.key));
|
|
988
|
+
const r = X.fromSource(this.render, t, e);
|
|
989
|
+
return e?.key && (r.uid = e.key, this.cache.set(e.key, r)), new W(r);
|
|
991
990
|
}
|
|
992
991
|
/**
|
|
993
992
|
* Creates a generic Render Texture (FrameBuffer).
|
|
@@ -1016,7 +1015,7 @@ class St {
|
|
|
1016
1015
|
*/
|
|
1017
1016
|
destroy(t, e = !1) {
|
|
1018
1017
|
let r, i;
|
|
1019
|
-
typeof t == "string" ? (i = t, r = this.cache.get(i)) : t instanceof
|
|
1018
|
+
typeof t == "string" ? (i = t, r = this.cache.get(i)) : t instanceof W ? (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));
|
|
1020
1019
|
}
|
|
1021
1020
|
/**
|
|
1022
1021
|
* Destroys all cached textures and clears the cache, ignoring reference counts.
|
|
@@ -1033,7 +1032,7 @@ class St {
|
|
|
1033
1032
|
});
|
|
1034
1033
|
}
|
|
1035
1034
|
}
|
|
1036
|
-
class
|
|
1035
|
+
class X {
|
|
1037
1036
|
glTexture = null;
|
|
1038
1037
|
width;
|
|
1039
1038
|
height;
|
|
@@ -1051,7 +1050,7 @@ class z {
|
|
|
1051
1050
|
*/
|
|
1052
1051
|
static fromSource(t, e, r = {}) {
|
|
1053
1052
|
const i = J(t, e, r);
|
|
1054
|
-
return new
|
|
1053
|
+
return new X(i, e.width, e.height);
|
|
1055
1054
|
}
|
|
1056
1055
|
/**
|
|
1057
1056
|
* Updates the content of the existing texture (e.g. for Video or dynamic Canvas).
|
|
@@ -1082,7 +1081,7 @@ class z {
|
|
|
1082
1081
|
this.glTexture && (t.deleteTexture(this.glTexture), this.glTexture = null);
|
|
1083
1082
|
}
|
|
1084
1083
|
}
|
|
1085
|
-
class
|
|
1084
|
+
class W {
|
|
1086
1085
|
base;
|
|
1087
1086
|
uvX = 0;
|
|
1088
1087
|
uvY = 0;
|
|
@@ -1139,7 +1138,7 @@ class B {
|
|
|
1139
1138
|
* @returns A new Texture pointing to the sub-region.
|
|
1140
1139
|
*/
|
|
1141
1140
|
getSubTexture(t, e, r, i) {
|
|
1142
|
-
if (!this.base) return new
|
|
1141
|
+
if (!this.base) return new W();
|
|
1143
1142
|
const s = this.clone(), a = this._px, o = this._py;
|
|
1144
1143
|
return s.setRegion(a + t, o + e, r, i), s;
|
|
1145
1144
|
}
|
|
@@ -1148,7 +1147,7 @@ class B {
|
|
|
1148
1147
|
* @returns A new Texture instance.
|
|
1149
1148
|
*/
|
|
1150
1149
|
clone(t) {
|
|
1151
|
-
const e = t ?? new
|
|
1150
|
+
const e = t ?? new W(this.base);
|
|
1152
1151
|
return t && this.base && t.setBase(this.base), e.scale = this.scale, e.uvX = this.uvX, e.uvY = this.uvY, e.uvW = this.uvW, e.uvH = this.uvH, e.rawWidth = this.rawWidth, e.rawHeight = this.rawHeight, e.offsetX = this.offsetX, e.offsetY = this.offsetY, e.flipY = this.flipY, e.isRotated = this.isRotated, e.isAtlas = this.isAtlas, e._px = this._px, e._py = this._py, e._pw = this._pw, e._ph = this._ph, e;
|
|
1153
1152
|
}
|
|
1154
1153
|
/**
|
|
@@ -1176,7 +1175,7 @@ class B {
|
|
|
1176
1175
|
*/
|
|
1177
1176
|
static fromImageSource(t, e, r = {}) {
|
|
1178
1177
|
const i = J(t, e, r);
|
|
1179
|
-
return new
|
|
1178
|
+
return new W(new X(i, e.width, e.height));
|
|
1180
1179
|
}
|
|
1181
1180
|
/**
|
|
1182
1181
|
* Marks this Texture as destroyed, decrementing the BaseTexture reference count.
|
|
@@ -1185,7 +1184,7 @@ class B {
|
|
|
1185
1184
|
this.base && (this.base.refCount--, this.base = void 0), this.glTexture = null;
|
|
1186
1185
|
}
|
|
1187
1186
|
}
|
|
1188
|
-
class bt extends
|
|
1187
|
+
class bt extends W {
|
|
1189
1188
|
framebuffer;
|
|
1190
1189
|
renderbuffer;
|
|
1191
1190
|
gl;
|
|
@@ -1197,7 +1196,7 @@ class bt extends B {
|
|
|
1197
1196
|
constructor(t, e) {
|
|
1198
1197
|
super(), this.gl = t.gl, this.clearColor = e.clearColor ?? x.Black;
|
|
1199
1198
|
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 });
|
|
1200
|
-
if (this.setBase(new
|
|
1199
|
+
if (this.setBase(new X(a, r, i)), this.framebuffer = this.gl.createFramebuffer(), this.renderbuffer = this.gl.createRenderbuffer(), !this.framebuffer || !this.renderbuffer)
|
|
1201
1200
|
throw new Error("Failed to create Framebuffer or Renderbuffer");
|
|
1202
1201
|
this.resize(r, i, !0);
|
|
1203
1202
|
}
|
|
@@ -1280,11 +1279,11 @@ const Ut = {
|
|
|
1280
1279
|
fontSize: 24,
|
|
1281
1280
|
fontWeight: "normal",
|
|
1282
1281
|
fill: "#000000",
|
|
1283
|
-
strokeThickness:
|
|
1282
|
+
strokeThickness: 0,
|
|
1284
1283
|
align: "left",
|
|
1285
1284
|
lineHeight: 1
|
|
1286
1285
|
};
|
|
1287
|
-
class Ct extends
|
|
1286
|
+
class Ct extends W {
|
|
1288
1287
|
canvas;
|
|
1289
1288
|
ctx;
|
|
1290
1289
|
render;
|
|
@@ -1298,7 +1297,7 @@ class Ct extends B {
|
|
|
1298
1297
|
if (!r) throw new Error("Failed to get 2d context for TextTexture");
|
|
1299
1298
|
this.ctx = r, this.canvas.width = 1, this.canvas.height = 1;
|
|
1300
1299
|
const i = J(t, this.canvas, this.options);
|
|
1301
|
-
this.setBase(new
|
|
1300
|
+
this.setBase(new X(i, 1, 1)), this.update();
|
|
1302
1301
|
}
|
|
1303
1302
|
get text() {
|
|
1304
1303
|
return this._text;
|
|
@@ -1337,20 +1336,19 @@ class Ct extends B {
|
|
|
1337
1336
|
const c = this._text.split(`
|
|
1338
1337
|
`);
|
|
1339
1338
|
let u = 0, l = 0;
|
|
1340
|
-
const d = t.measureText(c[0]), f = d.fontBoundingBoxAscent + d.fontBoundingBoxDescent;
|
|
1341
|
-
for (const
|
|
1342
|
-
const
|
|
1343
|
-
|
|
1339
|
+
const d = t.measureText(c[0] || "M"), f = (d.fontBoundingBoxAscent || r) + (d.fontBoundingBoxDescent || r * 0.2);
|
|
1340
|
+
for (const D of c) {
|
|
1341
|
+
const N = t.measureText(D);
|
|
1342
|
+
N.width > u && (u = N.width), l += f * a;
|
|
1344
1343
|
}
|
|
1345
1344
|
l -= f * (a - 1);
|
|
1346
|
-
const m = this._style.strokeThickness || 0, g = Math.ceil(u + m * 2) || 1, v = Math.ceil(l + m * 2) || 1, y = g * h, R = v * h;
|
|
1345
|
+
const m = this._style.strokeThickness || 0, g = Math.ceil(u + m * 2) || 1, v = Math.ceil(l + m * 2) || 1, y = Math.ceil(g * h), R = Math.ceil(v * h);
|
|
1347
1346
|
(this.canvas.width !== y || this.canvas.height !== R) && (this.canvas.width = y, this.canvas.height = R), t.setTransform(h, 0, 0, h, 0, 0), t.clearRect(0, 0, g, v), t.font = o, t.textBaseline = "top", t.textAlign = this._style.align;
|
|
1348
|
-
const A = this.updateOffset(g, m);
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
}
|
|
1347
|
+
const A = this.updateOffset(g, m), P = e.strokeThickness, U = P > 0;
|
|
1348
|
+
U && (t.lineWidth = P, t.strokeStyle = e.stroke, t.lineJoin = "round"), e.fill && (t.fillStyle = e.fill);
|
|
1349
|
+
let B = m;
|
|
1350
|
+
for (const D of c)
|
|
1351
|
+
U && t.strokeText(D, A, B), e.fill && t.fillText(D, A, B), B += f * a;
|
|
1354
1352
|
this.base?.updateSource(this.render.gl, this.canvas, this.options), this.setRegion(0, 0, y, R);
|
|
1355
1353
|
}
|
|
1356
1354
|
}
|
|
@@ -1406,7 +1404,7 @@ function J(n, t, e) {
|
|
|
1406
1404
|
}
|
|
1407
1405
|
return r.bindTexture(r.TEXTURE_2D, null), i;
|
|
1408
1406
|
}
|
|
1409
|
-
function
|
|
1407
|
+
function V(n, t) {
|
|
1410
1408
|
if (n.includes("%TEXTURE_NUM%") && (n = n.replace("%TEXTURE_NUM%", t.toString())), n.includes("%GET_COLOR%")) {
|
|
1411
1409
|
let e = "";
|
|
1412
1410
|
for (let r = 0; r < t; r++)
|
|
@@ -1653,6 +1651,9 @@ class ct {
|
|
|
1653
1651
|
const s = t.createShader(r, i);
|
|
1654
1652
|
return s.isCustom = !0, s.padding = this.padding, this.glshader.set(e, s), this.uniformDirty.add(e), s;
|
|
1655
1653
|
}
|
|
1654
|
+
destroy() {
|
|
1655
|
+
this.glshader.forEach((t) => t.destroy());
|
|
1656
|
+
}
|
|
1656
1657
|
}
|
|
1657
1658
|
class Et {
|
|
1658
1659
|
constructor(t) {
|
|
@@ -1867,12 +1868,12 @@ class at extends Et {
|
|
|
1867
1868
|
}
|
|
1868
1869
|
createBuffer() {
|
|
1869
1870
|
const t = this.gl;
|
|
1870
|
-
this.quadBuffer = new et(t,
|
|
1871
|
+
this.quadBuffer = new et(t, b.Float32, t.ARRAY_BUFFER, t.STATIC_DRAW), this.quadBuffer.bindBuffer();
|
|
1871
1872
|
for (const e of Wt) this.quadBuffer.pushFloat32(e);
|
|
1872
|
-
this.quadBuffer.makeDirty(), this.quadBuffer.bufferData(), this.instanceBuffer = new et(t,
|
|
1873
|
+
this.quadBuffer.makeDirty(), this.quadBuffer.bufferData(), this.instanceBuffer = new et(t, b.Uint32, t.ARRAY_BUFFER, t.DYNAMIC_DRAW);
|
|
1873
1874
|
}
|
|
1874
1875
|
createDefaultShader() {
|
|
1875
|
-
const t = this.rapid, e =
|
|
1876
|
+
const t = this.rapid, e = V(ut, t.maxTextureUnits), r = V(Z, t.maxTextureUnits);
|
|
1876
1877
|
return this.vs = r, this.fs = e, this.defaultShader = this.createShader(r, e), this.defaultShader;
|
|
1877
1878
|
}
|
|
1878
1879
|
createShader(t, e) {
|
|
@@ -1888,7 +1889,7 @@ class at extends Et {
|
|
|
1888
1889
|
]), r.unbindVAO(), this.currentShader && this.currentShader.use(), r;
|
|
1889
1890
|
}
|
|
1890
1891
|
createCustomShader(t) {
|
|
1891
|
-
const e =
|
|
1892
|
+
const e = V(ut, this.rapid.maxTextureUnits - t.usedTextureUnitNum), r = V(Z, this.rapid.maxTextureUnits - t.usedTextureUnitNum);
|
|
1892
1893
|
return t.getGLShader(this, this.KEY, r, e);
|
|
1893
1894
|
}
|
|
1894
1895
|
/**
|
|
@@ -1926,16 +1927,16 @@ class at extends Et {
|
|
|
1926
1927
|
);
|
|
1927
1928
|
}
|
|
1928
1929
|
drawSpriteAffine(t, e, r, i, s, a, o, h = 0, c = 0, u = 1, l = 1, d = 4294967295, f = 0, m = 0, g = !1, v = !1, y = !1) {
|
|
1929
|
-
const R = t.base, A = t.rawWidth,
|
|
1930
|
+
const R = t.base, A = t.rawWidth, P = t.rawHeight, U = t.offsetX, B = t.offsetY, D = this.useTexture(
|
|
1930
1931
|
t.glTexture,
|
|
1931
1932
|
f / R.width,
|
|
1932
1933
|
m / R.height
|
|
1933
|
-
),
|
|
1934
|
-
|
|
1935
|
-
const
|
|
1936
|
-
y ? (T[0] = 0, T[1] = v ?
|
|
1937
|
-
const
|
|
1938
|
-
|
|
1934
|
+
), N = this.instanceBuffer, M = N.usedElemNum;
|
|
1935
|
+
N.resize(12);
|
|
1936
|
+
const C = N.float32, k = N.uint32, G = y ? P : A, H = y ? A : P, I = G + 2 * f, O = H + 2 * m, S = U - f, L = B - m, T = this.localSpriteMatrix;
|
|
1937
|
+
y ? (T[0] = 0, T[1] = v ? O : -O, T[2] = g ? -I : I, T[3] = 0, T[4] = S + (g ? I : 0), T[5] = L + (v ? 0 : O)) : (T[0] = g ? -I : I, T[1] = 0, T[2] = 0, T[3] = v ? -O : O, T[4] = S + (g ? I : 0), T[5] = L + (v ? O : 0));
|
|
1938
|
+
const w = this.worldSpriteMatrix;
|
|
1939
|
+
w[0] = e * T[0] + i * T[1], w[1] = r * T[0] + s * T[1], w[2] = e * T[2] + i * T[3], w[3] = r * T[2] + s * T[3], w[4] = a + e * T[4] + i * T[5], w[5] = o + r * T[4] + s * T[5], C[M + 6] = h, C[M + 7] = c, C[M + 8] = u, C[M + 9] = l, k[M + 10] = d, k[M + 11] = D, this.rapid.roundPixels && (w[4] = Math.round(w[4]), w[5] = Math.round(w[5])), C[M] = w[0], C[M + 1] = w[2], C[M + 2] = w[4], C[M + 3] = w[1], C[M + 4] = w[3], C[M + 5] = w[5], N.usedElemNum += 12, N.makeDirty(), this.instanceCount++;
|
|
1939
1940
|
}
|
|
1940
1941
|
render() {
|
|
1941
1942
|
if (this.instanceCount === 0) return;
|
|
@@ -2084,7 +2085,7 @@ class Ht extends Et {
|
|
|
2084
2085
|
}
|
|
2085
2086
|
createBuffer() {
|
|
2086
2087
|
const t = this.gl;
|
|
2087
|
-
this.vertexBuffer = new et(t,
|
|
2088
|
+
this.vertexBuffer = new et(t, b.Float32, t.ARRAY_BUFFER, t.DYNAMIC_DRAW), this.maskShader = this.createMaskShader(kt, Gt);
|
|
2088
2089
|
}
|
|
2089
2090
|
createDefaultShader() {
|
|
2090
2091
|
return this.vs = Bt, this.fs = Dt, this.defaultShader = this.createShader(this.vs, this.fs), this.defaultShader;
|
|
@@ -2356,17 +2357,17 @@ const tt = 10, lt = (n, t, e, r, i, s) => {
|
|
|
2356
2357
|
let c = 0;
|
|
2357
2358
|
const u = n.texture?.rawWidth || 1, l = n.closed ? t.length : t.length - 1;
|
|
2358
2359
|
for (let d = 0; d < l; d++) {
|
|
2359
|
-
const f = t[d], m = e[d].normal, g = e[d].miters, v = (d + 1) % t.length, y = t[v], R = e[v].normal, A = e[v].miters,
|
|
2360
|
-
let
|
|
2361
|
-
h === 0 ? (
|
|
2362
|
-
const
|
|
2363
|
-
s.push(
|
|
2360
|
+
const f = t[d], m = e[d].normal, g = e[d].miters, v = (d + 1) % t.length, y = t[v], R = e[v].normal, A = e[v].miters, P = f.add(m.multiply(g * i)), U = f.subtract(m.multiply(g * i)), B = y.add(R.multiply(A * i)), D = y.subtract(R.multiply(A * i)), N = f.distanceTo(y);
|
|
2361
|
+
let M = 0, C = 0;
|
|
2362
|
+
h === 0 ? (M = c / r, C = (c + N) / r) : (M = c / u, C = M + N / u);
|
|
2363
|
+
const k = new p(M, 0), G = new p(M, 1), H = new p(C, 0), I = new p(C, 1);
|
|
2364
|
+
s.push(P), a.push(k), s.push(U), a.push(G), s.push(B), a.push(H), s.push(B), a.push(H), s.push(D), a.push(I), s.push(U), a.push(G), c += N;
|
|
2364
2365
|
}
|
|
2365
2366
|
if (o && !n.closed) {
|
|
2366
2367
|
const d = t[0], f = e[0].normal, m = h === 0 ? r > 0 ? 1 / r : 0 : 1 / u, g = lt(d, f, i, !0, 0, m);
|
|
2367
2368
|
s.push(...g.vertices), a.push(...g.uv);
|
|
2368
|
-
const v = t[t.length - 1], y = e[t.length - 1].normal, R = h === 0 ? 1 : c / u, A = h === 0 ? r > 0 ? 1 / r : 0 : 1 / u,
|
|
2369
|
-
s.push(...
|
|
2369
|
+
const v = t[t.length - 1], y = e[t.length - 1].normal, R = h === 0 ? 1 : c / u, A = h === 0 ? r > 0 ? 1 / r : 0 : 1 / u, P = lt(v, y, i, !1, R, A);
|
|
2370
|
+
s.push(...P.vertices), a.push(...P.uv);
|
|
2370
2371
|
}
|
|
2371
2372
|
return { vertices: s, uv: a };
|
|
2372
2373
|
}, Q = (n, t, e, r) => {
|
|
@@ -2555,11 +2556,11 @@ class re extends at {
|
|
|
2555
2556
|
super(t);
|
|
2556
2557
|
}
|
|
2557
2558
|
createCustomShader(t) {
|
|
2558
|
-
const e =
|
|
2559
|
+
const e = V(dt, this.rapid.maxTextureUnits - t.usedTextureUnitNum), r = V(Z, this.rapid.maxTextureUnits - t.usedTextureUnitNum);
|
|
2559
2560
|
return t.getGLShader(this, this.KEY, r, e);
|
|
2560
2561
|
}
|
|
2561
2562
|
createDefaultShader() {
|
|
2562
|
-
const t = this.rapid, e =
|
|
2563
|
+
const t = this.rapid, e = V(dt, t.maxTextureUnits), r = V(Z, t.maxTextureUnits);
|
|
2563
2564
|
return this.vs = r, this.fs = e, this.defaultShader = this.createShader(r, e), this.defaultShader;
|
|
2564
2565
|
}
|
|
2565
2566
|
}
|
|
@@ -2620,7 +2621,7 @@ void main(void) {\r
|
|
|
2620
2621
|
\r
|
|
2621
2622
|
// CUSTOM_CODE_CALL\r
|
|
2622
2623
|
}\r
|
|
2623
|
-
`,
|
|
2624
|
+
`, z = 48, xt = z / 4;
|
|
2624
2625
|
class ie extends at {
|
|
2625
2626
|
KEY = "ParticleSprite";
|
|
2626
2627
|
texture;
|
|
@@ -2641,46 +2642,46 @@ class ie extends at {
|
|
|
2641
2642
|
createShader(t, e) {
|
|
2642
2643
|
const r = super.createShader(t, e);
|
|
2643
2644
|
return r.use(), r.bindVAO(), this.instanceBuffer.bindBuffer(), r.setAttributes([
|
|
2644
|
-
{ name: "aPosition", size: 2, stride:
|
|
2645
|
-
{ name: "aScale", size: 2, stride:
|
|
2646
|
-
{ name: "aRotation", size: 1, stride:
|
|
2647
|
-
{ name: "aUVRect", size: 4, stride:
|
|
2648
|
-
{ name: "aColor", size: 4, type: st, normalized: !0, stride:
|
|
2649
|
-
{ name: "aOrigin", size: 2, stride:
|
|
2645
|
+
{ name: "aPosition", size: 2, stride: z, offset: 0, divisor: 1 },
|
|
2646
|
+
{ name: "aScale", size: 2, stride: z, offset: 8, divisor: 1 },
|
|
2647
|
+
{ name: "aRotation", size: 1, stride: z, offset: 16, divisor: 1 },
|
|
2648
|
+
{ name: "aUVRect", size: 4, stride: z, offset: 20, divisor: 1 },
|
|
2649
|
+
{ name: "aColor", size: 4, type: st, normalized: !0, stride: z, offset: 36, divisor: 1 },
|
|
2650
|
+
{ name: "aOrigin", size: 2, stride: z, offset: 40, divisor: 1 }
|
|
2650
2651
|
]), this.quadBuffer.bindBuffer(), r.setAttributes([
|
|
2651
2652
|
{ name: "aVertex", size: 2, stride: 8, offset: 0, divisor: 0 }
|
|
2652
2653
|
]), r.unbindVAO(), this.currentShader && this.currentShader.use(), r;
|
|
2653
2654
|
}
|
|
2654
|
-
drawParticles(t, e, r, i = 0, s = 4294967295, a, o = 1, h = 1, c = 0, u = 0, l = 1, d = 1, f = !1, m = !1, g = !1, v = 0.5, y = 0.5, R, A = !1,
|
|
2655
|
+
drawParticles(t, e, r, i = 0, s = 4294967295, a, o = 1, h = 1, c = 0, u = 0, l = 1, d = 1, f = !1, m = !1, g = !1, v = 0.5, y = 0.5, R, A = !1, P) {
|
|
2655
2656
|
if (!t.glTexture || a <= 0) return;
|
|
2656
|
-
let
|
|
2657
|
-
const
|
|
2658
|
-
let
|
|
2659
|
-
|
|
2660
|
-
let
|
|
2661
|
-
|
|
2662
|
-
let
|
|
2663
|
-
for (
|
|
2657
|
+
let U = 0;
|
|
2658
|
+
const B = typeof o == "number", D = typeof h == "number", N = typeof i == "number", M = typeof s == "number", C = !M && typeof s[0] == "number";
|
|
2659
|
+
let k = t.rawWidth * (f ? -1 : 1);
|
|
2660
|
+
B && (k *= o);
|
|
2661
|
+
let G = t.rawHeight * (m ? -1 : 1);
|
|
2662
|
+
D && (G *= h);
|
|
2663
|
+
let H = g ? Math.PI / 2 : 0;
|
|
2664
|
+
for (N && (H += i); U < a; ) {
|
|
2664
2665
|
this.texture && t !== this.texture && this.flush(), this.texture || (this.texture = t, this.useTexture(t.glTexture, 0, 0));
|
|
2665
|
-
const
|
|
2666
|
-
let
|
|
2667
|
-
|
|
2668
|
-
const
|
|
2669
|
-
for (let
|
|
2670
|
-
const
|
|
2671
|
-
if (
|
|
2672
|
-
T[
|
|
2673
|
-
else if (
|
|
2674
|
-
T[
|
|
2666
|
+
const I = a - U, O = this.instanceBuffer;
|
|
2667
|
+
let S = O.usedElemNum;
|
|
2668
|
+
O.resize(I * xt);
|
|
2669
|
+
const L = O.float32, T = O.uint32;
|
|
2670
|
+
for (let w = 0; w < I; w++) {
|
|
2671
|
+
const Y = A ? a - 1 - U - w : U + w;
|
|
2672
|
+
if (L[S] = e[Y], L[S + 1] = r[Y], L[S + 2] = B ? k : k * o[Y], L[S + 3] = D ? G : G * h[Y], L[S + 4] = N ? H : i[Y] + H, L[S + 5] = c, L[S + 6] = u, L[S + 7] = l, L[S + 8] = d, M)
|
|
2673
|
+
T[S + 9] = 4294967295;
|
|
2674
|
+
else if (C)
|
|
2675
|
+
T[S + 9] = s[Y];
|
|
2675
2676
|
else {
|
|
2676
|
-
const ht = s[
|
|
2677
|
-
T[
|
|
2677
|
+
const ht = s[Y];
|
|
2678
|
+
T[S + 9] = R ? ht.premultipliedUint32 : ht.uint32;
|
|
2678
2679
|
}
|
|
2679
|
-
|
|
2680
|
+
L[S + 10] = v, L[S + 11] = y, S += xt;
|
|
2680
2681
|
}
|
|
2681
|
-
|
|
2682
|
+
O.usedElemNum = S, O.makeDirty(), this.instanceCount += I, U += I;
|
|
2682
2683
|
}
|
|
2683
|
-
this.customMatrix =
|
|
2684
|
+
this.customMatrix = P, this.flush();
|
|
2684
2685
|
}
|
|
2685
2686
|
render() {
|
|
2686
2687
|
if (this.instanceCount === 0) return;
|
|
@@ -2745,7 +2746,7 @@ class ce {
|
|
|
2745
2746
|
scaleMode;
|
|
2746
2747
|
/** Internal ping-pong RenderTextures for multi-filter chains. */
|
|
2747
2748
|
_filterRT = [null, null];
|
|
2748
|
-
_filterInput = new
|
|
2749
|
+
_filterInput = new W();
|
|
2749
2750
|
get height() {
|
|
2750
2751
|
return this.logicHeight;
|
|
2751
2752
|
}
|
|
@@ -3219,7 +3220,7 @@ class ce {
|
|
|
3219
3220
|
function he(n) {
|
|
3220
3221
|
return !(n === null || typeof n != "object" || Array.isArray(n) || n instanceof p || n instanceof x);
|
|
3221
3222
|
}
|
|
3222
|
-
class
|
|
3223
|
+
class F {
|
|
3223
3224
|
/** Random float in [min, max). */
|
|
3224
3225
|
static float(t, e) {
|
|
3225
3226
|
return Math.random() * (e - t) + t;
|
|
@@ -3234,20 +3235,20 @@ class W {
|
|
|
3234
3235
|
}
|
|
3235
3236
|
/** Random Vec2 with components in the supplied per-axis ranges. */
|
|
3236
3237
|
static vector(t, e, r, i) {
|
|
3237
|
-
return new p(
|
|
3238
|
+
return new p(F.float(t, e), F.float(r, i));
|
|
3238
3239
|
}
|
|
3239
3240
|
/** Random Color with each component interpolated between minColor and maxColor. */
|
|
3240
3241
|
static randomColor(t, e) {
|
|
3241
3242
|
return new x(
|
|
3242
|
-
|
|
3243
|
-
|
|
3244
|
-
|
|
3245
|
-
|
|
3243
|
+
F.float(t.r, e.r),
|
|
3244
|
+
F.float(t.g, e.g),
|
|
3245
|
+
F.float(t.b, e.b),
|
|
3246
|
+
F.float(t.a, e.a)
|
|
3246
3247
|
);
|
|
3247
3248
|
}
|
|
3248
3249
|
/** Pick a random element from an array with uniform probability. */
|
|
3249
3250
|
static pick(t) {
|
|
3250
|
-
return t[
|
|
3251
|
+
return t[F.int(0, t.length - 1)];
|
|
3251
3252
|
}
|
|
3252
3253
|
/**
|
|
3253
3254
|
* Pick a random element using weighted probability.
|
|
@@ -3273,11 +3274,11 @@ class W {
|
|
|
3273
3274
|
if (Array.isArray(t)) {
|
|
3274
3275
|
const [r, i] = t;
|
|
3275
3276
|
if (typeof r == "number")
|
|
3276
|
-
return
|
|
3277
|
+
return F.float(r, i);
|
|
3277
3278
|
if (r instanceof p)
|
|
3278
|
-
return
|
|
3279
|
+
return F.vector(r.x, i.x, r.y, i.y);
|
|
3279
3280
|
if (r instanceof x)
|
|
3280
|
-
return
|
|
3281
|
+
return F.randomColor(r, i);
|
|
3281
3282
|
}
|
|
3282
3283
|
return typeof t == "number" ? t : t.clone();
|
|
3283
3284
|
}
|
|
@@ -3290,9 +3291,9 @@ class K {
|
|
|
3290
3291
|
if (!Number.isInteger(t) || t <= 0)
|
|
3291
3292
|
throw new RangeError("ParticleAttributeStore size must be a positive integer");
|
|
3292
3293
|
this.components = Array.from({ length: t }, () => ({
|
|
3293
|
-
value: new
|
|
3294
|
-
delta: new
|
|
3295
|
-
damping: new
|
|
3294
|
+
value: new _(b.Float32),
|
|
3295
|
+
delta: new _(b.Float32),
|
|
3296
|
+
damping: new _(b.Float32)
|
|
3296
3297
|
}));
|
|
3297
3298
|
}
|
|
3298
3299
|
getArrayBuffer() {
|
|
@@ -3325,7 +3326,7 @@ class K {
|
|
|
3325
3326
|
}
|
|
3326
3327
|
}
|
|
3327
3328
|
resolveComponent(t, e, r) {
|
|
3328
|
-
return t === void 0 ? r : Array.isArray(t) ?
|
|
3329
|
+
return t === void 0 ? r : Array.isArray(t) ? F.float(
|
|
3329
3330
|
this.getComponent(t[0], e),
|
|
3330
3331
|
this.getComponent(t[1], e)
|
|
3331
3332
|
) : this.getComponent(t, e);
|
|
@@ -3340,7 +3341,7 @@ class K {
|
|
|
3340
3341
|
return o;
|
|
3341
3342
|
}
|
|
3342
3343
|
}
|
|
3343
|
-
class
|
|
3344
|
+
class Mt {
|
|
3344
3345
|
options;
|
|
3345
3346
|
emitting = !1;
|
|
3346
3347
|
emitTimer = 0;
|
|
@@ -3350,12 +3351,12 @@ class wt {
|
|
|
3350
3351
|
/** Whether spawned particles are positioned in local emitter space (default: true). */
|
|
3351
3352
|
localSpace = vt;
|
|
3352
3353
|
rapid;
|
|
3353
|
-
x = new
|
|
3354
|
-
y = new
|
|
3355
|
-
scaleX = new
|
|
3356
|
-
scaleY = new
|
|
3357
|
-
rotation = new
|
|
3358
|
-
color = new
|
|
3354
|
+
x = new _(b.Float32);
|
|
3355
|
+
y = new _(b.Float32);
|
|
3356
|
+
scaleX = new _(b.Float32);
|
|
3357
|
+
scaleY = new _(b.Float32);
|
|
3358
|
+
rotation = new _(b.Float32);
|
|
3359
|
+
color = new _(b.Uint32);
|
|
3359
3360
|
animations = {
|
|
3360
3361
|
speed: new K(),
|
|
3361
3362
|
rotation: new K(),
|
|
@@ -3392,7 +3393,7 @@ class wt {
|
|
|
3392
3393
|
* Particle state is NOT copied.
|
|
3393
3394
|
*/
|
|
3394
3395
|
clone() {
|
|
3395
|
-
return new
|
|
3396
|
+
return new Mt(this.rapid, { ...this.options });
|
|
3396
3397
|
}
|
|
3397
3398
|
/** Sets particles-per-second emission rate (continuous mode). */
|
|
3398
3399
|
setEmitRate(t) {
|
|
@@ -3426,7 +3427,7 @@ class wt {
|
|
|
3426
3427
|
this.createParticle();
|
|
3427
3428
|
}
|
|
3428
3429
|
createParticle() {
|
|
3429
|
-
const t =
|
|
3430
|
+
const t = F.scalarOrRange(this.options.life, 1);
|
|
3430
3431
|
let e = 0, r = 0;
|
|
3431
3432
|
switch (this.options.emitShape) {
|
|
3432
3433
|
case "circle": {
|
|
@@ -3472,7 +3473,7 @@ class wt {
|
|
|
3472
3473
|
r > 0 && (this.emit(r), this.emitTimer -= r / this.emitRate);
|
|
3473
3474
|
}
|
|
3474
3475
|
const e = this.updateParticle(t);
|
|
3475
|
-
this.count -= e.length,
|
|
3476
|
+
this.count -= e.length, _.removeAtIndices(e, this.getAllArrayBuffer());
|
|
3476
3477
|
}
|
|
3477
3478
|
updateParticle(t) {
|
|
3478
3479
|
const e = [], r = this.animations;
|
|
@@ -3523,20 +3524,20 @@ class wt {
|
|
|
3523
3524
|
}
|
|
3524
3525
|
}
|
|
3525
3526
|
export {
|
|
3526
|
-
|
|
3527
|
-
|
|
3527
|
+
b as ArrayType,
|
|
3528
|
+
X as BaseTexture,
|
|
3528
3529
|
ne as BlendMode,
|
|
3529
3530
|
ae as CanvasScaleMode,
|
|
3530
3531
|
x as Color,
|
|
3531
3532
|
ct as CustomGlShader,
|
|
3532
|
-
|
|
3533
|
+
_ as DynamicArrayBuffer,
|
|
3533
3534
|
Tt as GLShader,
|
|
3534
3535
|
Ht as GraphicRegion,
|
|
3535
3536
|
zt as LineTextureMode,
|
|
3536
3537
|
se as MaskType,
|
|
3537
3538
|
At as MatrixStack,
|
|
3538
|
-
|
|
3539
|
-
|
|
3539
|
+
wt as MatrixStore,
|
|
3540
|
+
Mt as ParticleEmitter,
|
|
3540
3541
|
oe as ParticleShape,
|
|
3541
3542
|
Wt as QUAD_VERTICES,
|
|
3542
3543
|
ce as Rapid,
|
|
@@ -3544,7 +3545,7 @@ export {
|
|
|
3544
3545
|
bt as RenderTexture,
|
|
3545
3546
|
at as SpriteRegion,
|
|
3546
3547
|
Ct as TextTexture,
|
|
3547
|
-
|
|
3548
|
+
W as Texture,
|
|
3548
3549
|
nt as TextureFilterMode,
|
|
3549
3550
|
St as TextureManager,
|
|
3550
3551
|
q as TextureWrapMode,
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
(function(g,M){typeof exports=="object"&&typeof module<"u"?M(exports):typeof define=="function"&&define.amd?define(["exports"],M):(g=typeof globalThis<"u"?globalThis:g||self,M(g.RapidRender={}))})(this,(function(g){"use strict";var M=(n=>(n[n.Float32=0]="Float32",n[n.Uint32=1]="Uint32",n[n.Uint16=2]="Uint16",n))(M||{});class _{usedElemNum;typedArray;arrayType;maxElemNum;bytePerElem;arraybuffer;uint32;float32;uint16;constructor(t){this.usedElemNum=0,this.maxElemNum=512,this.bytePerElem=this.getArrayType(t).BYTES_PER_ELEMENT,this.arrayType=t,this.arraybuffer=new ArrayBuffer(this.maxElemNum*this.bytePerElem),this.updateTypedArray()}getArrayType(t){switch(t){case 0:return Float32Array;case 1:return Uint32Array;case 2:return Uint16Array;default:throw new Error("Unsupported ArrayType")}}updateTypedArray(){switch(this.uint32=new Uint32Array(this.arraybuffer),this.float32=new Float32Array(this.arraybuffer),this.uint16=new Uint16Array(this.arraybuffer),this.arrayType){case 0:this.typedArray=this.float32;break;case 1:this.typedArray=this.uint32;break;case 2:this.typedArray=this.uint16;break}}clear(){this.usedElemNum=0}static removeAtIndices(t,e){const r=Math.max(...e.map(o=>o.usedElemNum)),i=[...new Set(t)].filter(o=>o>=0&&o<r).sort((o,h)=>o-h);if(i.length===0)return 0;let s=0,a=0;for(let o=0;o<r;o++){if(a<i.length&&o===i[a]){a++;continue}if(s!==o)for(const h of e)h.typedArray[s]=h.typedArray[o];s++}for(const o of e)o.usedElemNum=s;return r-s}resize(t=0){if(t+=this.usedElemNum,t>this.maxElemNum){for(;t>this.maxElemNum;)this.maxElemNum*=2;return this.setMaxSize(this.maxElemNum),!0}return!1}setMaxSize(t=this.maxElemNum){const e=this.typedArray;this.maxElemNum=t,this.arraybuffer=new ArrayBuffer(t*this.bytePerElem),this.updateTypedArray(),this.typedArray.set(e)}pushUint32(t){this.resize(1),this.uint32[this.usedElemNum++]=t}pushFloat32(t){this.resize(1),this.float32[this.usedElemNum++]=t}push(t){this.resize(1),this.typedArray[this.usedElemNum++]=t}pop(){return this.typedArray[--this.usedElemNum]}top(){return this.typedArray[this.usedElemNum-1]}get(t){return this.typedArray[t]}getArray(t=0,e){return e===void 0?this.typedArray:this.typedArray.subarray(t,e)}get length(){return this.usedElemNum}reset(){this.usedElemNum=0}}class J extends _{buffer;gl;dirty=!0;type;webglBufferSize=0;usage;constructor(t,e,r=t.ARRAY_BUFFER,i=t.DYNAMIC_DRAW){super(e),this.gl=t,this.buffer=t.createBuffer(),this.type=r,this.usage=i}makeDirty(){this.dirty=!0}clear(){super.clear(),this.dirty=!1}bindBuffer(){this.gl.bindBuffer(this.type,this.buffer)}bufferData(){if(this.dirty){const t=this.gl;this.maxElemNum>this.webglBufferSize&&(t.bufferData(this.type,this.maxElemNum*this.bytePerElem,this.usage),this.webglBufferSize=this.maxElemNum),t.bufferSubData(this.type,0,this.getArray(0,this.usedElemNum)),this.dirty=!1}}}class m{_r;_g;_b;_a;uint32;premultipliedUint32;static clampByte(t){return t<=0?0:t>=255?255:Math.round(t)}constructor(t,e,r,i=255){this._r=t,this._g=e,this._b=r,this._a=i,this.updateUint()}setClearColor(t){t.clearColor(this.r/255,this.g/255,this.b/255,this.a/255)}get r(){return this._r}set r(t){this._r=t,this.updateUint()}get g(){return this._g}set g(t){this._g=t,this.updateUint()}get b(){return this._b}set b(t){this._b=t,this.updateUint()}get a(){return this._a}set a(t){this._a=t,this.updateUint()}updateUint(){this.uint32=(this._a<<24|this._b<<16|this._g<<8|this._r)>>>0;const t=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);this.premultipliedUint32=(t<<24|i<<16|r<<8|e)>>>0}reset(){this._r=0,this._g=0,this._b=0,this._a=255,this.updateUint()}setRGBA(t,e,r,i){this.r=t,this.g=e,this.b=r,this.a=i,this.updateUint()}setHSL(t,e,r){t=(t%360+360)%360,e=Math.max(0,Math.min(100,e))/100,r=Math.max(0,Math.min(100,r))/100;const i=(1-Math.abs(2*r-1))*e,s=i*(1-Math.abs(t/60%2-1)),a=r-i/2;let o=0,h=0,c=0;return t<60?[o,h,c]=[i,s,0]:t<120?[o,h,c]=[s,i,0]:t<180?[o,h,c]=[0,i,s]:t<240?[o,h,c]=[0,s,i]:t<300?[o,h,c]=[s,0,i]:[o,h,c]=[i,0,s],this.setRGBA((o+a)*255,(h+a)*255,(c+a)*255,255),this}toHex(){const t=e=>Math.max(0,Math.min(255,Math.round(e))).toString(16).padStart(2,"0");return`#${t(this._r)}${t(this._g)}${t(this._b)}${t(this._a)}`}copy(t){this.setRGBA(t.r,t.g,t.b,t.a)}clone(){return new m(this._r,this._g,this._b,this._a)}toHexString(t=!0){const e=this.r.toString(16).padStart(2,"0"),r=this.g.toString(16).padStart(2,"0"),i=this.b.toString(16).padStart(2,"0"),s=this.a.toString(16).padStart(2,"0");return`#${e}${r}${i}${t?s:""}`}equal(t){return t.r===this.r&&t.g===this.g&&t.b===this.b&&t.a===this.a}equals(t){return this.equal(t)}static FromHSL(t,e,r){return new m(0,0,0).setHSL(t,e,r)}static FromRGB(t,e,r){return new m(t,e,r)}static fromNorm(t,e,r,i=1){return new m(Math.round(t*255),Math.round(e*255),Math.round(r*255),Math.round(i*255))}static clampColorByte(t){return t<=0?0:t>=255?255:Math.round(t)}static packColor(t,e,r,i,s){const a=m.clampColorByte(i),o=s?a/255:1,h=m.clampColorByte(t*o),c=m.clampColorByte(e*o),u=m.clampColorByte(r*o);return(a<<24|u<<16|c<<8|h)>>>0}static fromHex(t){t.startsWith("#")&&(t=t.slice(1));const e=parseInt(t.slice(0,2),16),r=parseInt(t.slice(2,4),16),i=parseInt(t.slice(4,6),16);let s=255;return t.length>=8&&(s=parseInt(t.slice(6,8),16)),new m(e,r,i,s)}add(t){return new m(Math.min(this.r+t.r,255),Math.min(this.g+t.g,255),Math.min(this.b+t.b,255),Math.min(this.a+t.a,255))}subtract(t){return new m(this.r-t.r,this.g-t.g,this.b-t.b,this.a-t.a)}divide(t){return t instanceof m?new m(this.r/t.r,this.g/t.g,this.b/t.b,this.a/t.a):new m(this.r/t,this.g/t,this.b/t,this.a/t)}multiply(t){return t instanceof m?new m(this.r*t.r,this.g*t.g,this.b*t.b,this.a*t.a):new m(this.r*t,this.g*t,this.b*t,this.a*t)}clamp(){this.r=Math.max(0,Math.min(255,this.r)),this.g=Math.max(0,Math.min(255,this.g)),this.b=Math.max(0,Math.min(255,this.b)),this.a=Math.max(0,Math.min(255,this.a))}static Red=new m(255,0,0,255);static Green=new m(0,255,0,255);static Blue=new m(0,0,255,255);static Yellow=new m(255,255,0,255);static Purple=new m(128,0,128,255);static Orange=new m(255,165,0,255);static Pink=new m(255,192,203,255);static Gray=new m(128,128,128,255);static Brown=new m(139,69,19,255);static Cyan=new m(0,255,255,255);static Magenta=new m(255,0,255,255);static Lime=new m(192,255,0,255);static White=new m(255,255,255,255);static Black=new m(0,0,0,255);static Transparent=new m(0,0,0,0);static TRANSPARENT=new m(0,0,0,0)}const E=6;class ft{matrixCount=0;buffer;data;temporary=-1;constructor(t=10){this.buffer=new _(M.Float32),this.buffer.resize(t*E),this.data=this.buffer.getArray(),this.reset()}alloc(){const t=this.allocDirty();return this.identity(t),t}allocDirty(){return this.buffer.resize(E)&&(this.data=this.buffer.getArray()),this.buffer.usedElemNum+=E,this.matrixCount++}reset(){this.matrixCount=0,this.buffer.usedElemNum=0,this.temporary=this.alloc()}identity(t){const e=t*E,r=this.data;r[e]=1,r[e+1]=0,r[e+2]=0,r[e+3]=1,r[e+4]=0,r[e+5]=0}translate(t,e,r){const i=t*E,s=this.data;s[i+4]=s[i]*e+s[i+2]*r+s[i+4],s[i+5]=s[i+1]*e+s[i+3]*r+s[i+5]}scale(t,e,r){const i=t*E,s=this.data;s[i]*=e,s[i+1]*=e,s[i+2]*=r,s[i+3]*=r}rotate(t,e){const r=t*E,i=this.data,s=Math.cos(e),a=Math.sin(e),o=i[r],h=i[r+1],c=i[r+2],u=i[r+3];i[r]=o*s+c*a,i[r+1]=h*s+u*a,i[r+2]=o*-a+c*s,i[r+3]=h*-a+u*s}rotateWithOffset(t,e,r,i){this.translate(t,r,i),this.rotate(t,e),this.translate(t,-r,-i)}copy(t,e){const r=t*E,i=e*E,s=this.data;s[r]=s[i],s[r+1]=s[i+1],s[r+2]=s[i+2],s[r+3]=s[i+3],s[r+4]=s[i+4],s[r+5]=s[i+5]}multiply(t,e){this.multiplyOut(t,t,e)}multiplyOut(t,e,r){const i=t*E,s=e*E,a=r*E,o=this.data,h=o[s],c=o[s+1],u=o[s+2],l=o[s+3],d=o[s+4],f=o[s+5],x=o[a],v=o[a+1],y=o[a+2],T=o[a+3],w=o[a+4],b=o[a+5];o[i]=h*x+u*v,o[i+1]=c*x+l*v,o[i+2]=h*y+u*T,o[i+3]=c*y+l*T,o[i+4]=h*w+u*b+d,o[i+5]=c*w+l*b+f}invert(t){const e=t*E,r=this.data,i=r[e],s=r[e+1],a=r[e+2],o=r[e+3],h=r[e+4],c=r[e+5];let u=i*o-s*a;if(!u){this.identity(t);return}u=1/u,r[e]=o*u,r[e+1]=-s*u,r[e+2]=-a*u,r[e+3]=i*u,r[e+4]=(a*c-o*h)*u,r[e+5]=(s*h-i*c)*u}transformPoint(t,e,r){const i=t*E,s=this.data;return{x:e*s[i]+r*s[i+2]+s[i+4],y:e*s[i+1]+r*s[i+3]+s[i+5]}}worldToLocal(t,e,r){const i=this.allocDirty();this.copy(i,t),this.invert(i);const s=this.transformPoint(i,e,r);return this.matrixCount--,this.buffer.usedElemNum-=E,s}localToWorld(t,e,r){return this.transformPoint(t,e,r)}getPosition(t){const e=t*E,r=this.data;return{x:r[e+4],y:r[e+5]}}getScale(t){const e=t*E,r=this.data,i=Math.sqrt(r[e]*r[e]+r[e+1]*r[e+1]),s=Math.sqrt(r[e+2]*r[e+2]+r[e+3]*r[e+3]);return{x:i,y:s}}getRotation(t){const e=t*E,r=this.data;return Math.atan2(r[e+1],r[e])}toCSSMatrix(t,e=1,r=1){const i=t*E,s=this.data;return`matrix(${s[i]*e}, ${s[i+1]*r}, ${s[i+2]*e}, ${s[i+3]*r}, ${s[i+4]*e}, ${s[i+5]*r})`}getMatrix(t){const e=t*E;return this.data.slice(e,e+E)}getMatrixRef(t){const e=t*E;return this.data.subarray(e,e+E)}setMatrix(t,e){const r=t*E,i=this.data;i[r]=e[0],i[r+1]=e[1],i[r+2]=e[2],i[r+3]=e[3],i[r+4]=e[4],i[r+5]=e[5]}multiplyAffineInPlace(t,e,r,i,s,a,o){const h=t*6,c=this.data,u=c[h],l=c[h+1],d=c[h+2],f=c[h+3],x=c[h+4],v=c[h+5];c[h]=u*e+d*r,c[h+1]=l*e+f*r,c[h+2]=u*i+d*s,c[h+3]=l*i+f*s,c[h+4]=u*a+d*o+x,c[h+5]=l*a+f*o+v}multiplyAffine(t,e,r,i,s,a,o,h){const c=t*6,u=e*6,l=this.data,d=l[c],f=l[c+1],x=l[c+2],v=l[c+3],y=l[c+4],T=l[c+5];l[u]=d*r+x*i,l[u+1]=f*r+v*i,l[u+2]=d*s+x*a,l[u+3]=f*s+v*a,l[u+4]=d*o+x*h+y,l[u+5]=f*o+v*h+T}}class mt{matrix=new ft;step=0;stack=new _(M.Uint32);stepWorldM=new _(M.Uint32);stepAction=new _(M.Uint32);stepParentM=new _(M.Uint32);parentStack=new _(M.Uint32);curLocalM=-1;curWorldM=-1;rapid;constructor(t){this.reset(),this.rapid=t}save(){this.stack.push(this.curWorldM);const t=this.curWorldM;return this.step++,this.curLocalM=this.matrix.alloc(),this.curWorldM=this.matrix.allocDirty(),this.stepWorldM.push(this.curWorldM),this.stepParentM.push(t),this.matrix.copy(this.curWorldM,t),this.stepAction.push(1),{world:this.curWorldM,local:this.curLocalM,step:this.step}}restore(){this.stack.length!=0&&(this.curWorldM=this.stack.pop(),this.curLocalM=this.curWorldM-1,this.stepAction.push(0))}updateMatrix(t){const e=(typeof t=="number"?t:t.step)-1,r=this.stepParentM.get(e);let i=0,s=e;const a=this.parentStack;for(a.reset(),a.push(r);s<this.stepAction.length;){if(this.stepAction.get(s)===1){const h=this.stepWorldM.get(s),c=h-1,u=a.top();this.matrix.multiplyOut(h,u,c),a.push(h),i++}else if(a.pop(),i--,i===0)break;s++}}translate(t,e){this.matrix.translate(this.curLocalM,t,e),this.matrix.translate(this.curWorldM,t,e)}scale(t,e=t){this.matrix.scale(this.curLocalM,t,e),this.matrix.scale(this.curWorldM,t,e)}rotate(t){this.matrix.rotate(this.curLocalM,t),this.matrix.rotate(this.curWorldM,t)}rotateWithOffset(t,e,r){this.matrix.rotateWithOffset(this.curLocalM,t,e,r),this.matrix.rotateWithOffset(this.curWorldM,t,e,r)}identity(){this.matrix.identity(this.curLocalM),this.matrix.identity(this.curWorldM)}reset(){this.matrix.reset(),this.stack.reset(),this.stepAction.reset(),this.stepWorldM.reset(),this.stepParentM.reset(),this.step=0,this.curLocalM=this.matrix.alloc(),this.curWorldM=this.matrix.alloc()}localToWorld(t,e){return this.matrix.localToWorld(this.curWorldM,t,e)}worldToLocal(t,e){return this.matrix.worldToLocal(this.curWorldM,t,e)}getGlobalPosition(){return this.matrix.getPosition(this.curWorldM)}getGlobalScale(){return this.matrix.getScale(this.curWorldM)}getGlobalRotation(){return this.matrix.getRotation(this.curWorldM)}toCSSMatrix(){return this.matrix.toCSSMatrix(this.curWorldM)}getTransform(){const t=this.curWorldM*E;return this.matrix.data.slice(t,t+E)}setTransform(t){const e=this.curWorldM*E,r=this.matrix.data;r[e]=t[0],r[e+1]=t[1],r[e+2]=t[2],r[e+3]=t[3],r[e+4]=t[4],r[e+5]=t[5]}transformPoint(t,e){return this.matrix.transformPoint(this.curLocalM,t,e)}applyTransform(t,e=0,r=0,i=-1){let s=t.x??0,a=t.y??0;t.position&&(s+=t.position.x,a+=t.position.y);let o=1,h=1;const c=t.scale;c&&(typeof c=="number"?(o=c,h=c):(o=c.x,h=c.y));const u=t.rotation??0;let l=t.offsetX??0,d=t.offsetY??0;t.offset&&(l+=t.offset.x,d+=t.offset.y);const f=t.origin;f!==void 0&&(typeof f=="number"?(l-=f*e,d-=f*r):(l-=f.x*e,d-=f.y*r));const x=u?Math.cos(u):1,v=u?Math.sin(u):0,y=x*o,T=v*o,w=-v*h,b=x*h,P=s+y*l+w*d,U=a+T*l+b*d;if(i!==-1){this.matrix.multiplyAffine(this.curWorldM,i,y,T,w,b,P,U);return}this.matrix.multiplyAffineInPlace(this.curLocalM,y,T,w,b,P,U),this.matrix.multiplyAffineInPlace(this.curWorldM,y,T,w,b,P,U)}}var j=(n=>(n[n.REPEAT=0]="REPEAT",n[n.CLAMP=1]="CLAMP",n[n.MIRRORED_REPEAT=2]="MIRRORED_REPEAT",n))(j||{});class xt{render;cache=new Map;constructor(t){this.render=t}async load(t,e){let r=this.cache.get(t);if(r)return new I(r);try{const i=await this._fetchImage(t);return r=k.fromSource(this.render,i,e),r.uid=t,this.cache.set(t,r),new I(r)}catch(i){throw console.error(`[TextureManager] Failed to load: ${t}`,i),i}}create(t,e){if(e?.key&&this.cache.has(e.key))return new I(this.cache.get(e.key));const r=k.fromSource(this.render,t,e);return e?.key&&(r.uid=e.key,this.cache.set(e.key,r)),new I(r)}createRenderTexture(t){return new pt(this.render,t)}createTextTexture(t){return new gt(this.render,t)}destroy(t,e=!1){let r,i;typeof t=="string"?(i=t,r=this.cache.get(i)):t instanceof I?(r=t.base,i=r?.uid,t.destroy()):(r=t,i=r.uid),r&&(r.refCount<=0||e)&&(r.destroy(this.render.gl),i&&this.cache.delete(i))}destroyAll(){for(const t of this.cache.values())t.destroy(this.render.gl);this.cache.clear()}_fetchImage(t){return new Promise((e,r)=>{const i=new Image;i.crossOrigin="anonymous",i.onload=()=>e(i),i.onerror=()=>r(new Error(`Failed to load image resource: ${t}`)),i.src=t})}}class k{glTexture=null;width;height;uid;refCount=0;constructor(t,e,r){this.glTexture=t,this.width=e,this.height=r}static fromSource(t,e,r={}){const i=tt(t,e,r);return new k(i,e.width,e.height)}updateSource(t,e,r={}){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))}setFilterMode(t,e){this.glTexture&&(t.bindTexture(t.TEXTURE_2D,this.glTexture),nt(t,e),t.bindTexture(t.TEXTURE_2D,null))}setWrapMode(t,e){this.glTexture&&(t.bindTexture(t.TEXTURE_2D,this.glTexture),st(t,e),t.bindTexture(t.TEXTURE_2D,null))}destroy(t){this.glTexture&&(t.deleteTexture(this.glTexture),this.glTexture=null)}}class I{base;uvX=0;uvY=0;uvW=1;uvH=1;rawWidth=0;rawHeight=0;offsetX=0;offsetY=0;scale=1;isAtlas=!1;flipY=!1;isRotated=!1;glTexture=null;get width(){return(this.isRotated?this.rawHeight:this.rawWidth)*this.scale}get height(){return(this.isRotated?this.rawWidth:this.rawHeight)*this.scale}_px=0;_py=0;_pw=0;_ph=0;constructor(t){t&&this.setBase(t)}setBase(t){return this.base===t?this:(this.base&&this.base.refCount--,this.base=t,this.base.refCount++,this.setRegion(0,0,t.width,t.height),this.glTexture=t.glTexture,this)}setRegion(t,e,r,i){return this.base?(this._px=t,this._py=e,this._pw=r,this._ph=i,this.isAtlas=t!==0||e!==0||r!==this.base.width||i!==this.base.height,this.uvX=t/this.base.width,this.uvY=e/this.base.height,this.uvW=this.uvX+r/this.base.width,this.uvH=this.uvY+i/this.base.height,this.rawWidth=r*this.scale,this.rawHeight=i*this.scale,this):this}getSubTexture(t,e,r,i){if(!this.base)return new I;const s=this.clone(),a=this._px,o=this._py;return s.setRegion(a+t,o+e,r,i),s}clone(t){const e=t??new I(this.base);return t&&this.base&&t.setBase(this.base),e.scale=this.scale,e.uvX=this.uvX,e.uvY=this.uvY,e.uvW=this.uvW,e.uvH=this.uvH,e.rawWidth=this.rawWidth,e.rawHeight=this.rawHeight,e.offsetX=this.offsetX,e.offsetY=this.offsetY,e.flipY=this.flipY,e.isRotated=this.isRotated,e.isAtlas=this.isAtlas,e._px=this._px,e._py=this._py,e._pw=this._pw,e._ph=this._ph,e}splitGrid(t,e,r,i,s=0){if(!this.base)return[];const a=[],o=this.rawWidth,h=this.rawHeight,c=Math.max(0,s),u=t+c,l=e+c,d=r??Math.floor((o+c)/u),f=i??Math.floor((h+c)/l);for(let x=0;x<f;x++)for(let v=0;v<d;v++)a.push(this.getSubTexture(v*u,x*l,t,e));return a}static fromImageSource(t,e,r={}){const i=tt(t,e,r);return new I(new k(i,e.width,e.height))}destroy(){this.base&&(this.base.refCount--,this.base=void 0),this.glTexture=null}}class pt extends I{framebuffer;renderbuffer;gl;flipY=!0;clearColor;_allocW=0;_allocH=0;constructor(t,e){super(),this.gl=t.gl,this.clearColor=e.clearColor??m.Black;const r=Math.max(Math.round(e.width),1),i=Math.max(Math.round(e.height),1),a=tt(t,{width:r,height:i},{...e,onlySize:!0});if(this.setBase(new k(a,r,i)),this.framebuffer=this.gl.createFramebuffer(),this.renderbuffer=this.gl.createRenderbuffer(),!this.framebuffer||!this.renderbuffer)throw new Error("Failed to create Framebuffer or Renderbuffer");this.resize(r,i,!0)}resize(t,e,r=!1){t=Math.max(Math.round(t),1),e=Math.max(Math.round(e),1);const i=this.rawWidth===t&&this.rawHeight===e;if(!r&&i)return;const s=this.gl;(r||t>this._allocW||e>this._allocH)&&this.base&&(this._allocW=Math.max(this._allocW,t),this._allocH=Math.max(this._allocH,e),this.base.width=this._allocW,this.base.height=this._allocH,s.bindTexture(s.TEXTURE_2D,this.base.glTexture),s.texImage2D(s.TEXTURE_2D,0,s.RGBA,this._allocW,this._allocH,0,s.RGBA,s.UNSIGNED_BYTE,null),s.bindRenderbuffer(s.RENDERBUFFER,this.renderbuffer),s.renderbufferStorage(s.RENDERBUFFER,s.STENCIL_INDEX8,this._allocW,this._allocH),s.bindFramebuffer(s.FRAMEBUFFER,this.framebuffer),s.framebufferTexture2D(s.FRAMEBUFFER,s.COLOR_ATTACHMENT0,s.TEXTURE_2D,this.base.glTexture,0),s.framebufferRenderbuffer(s.FRAMEBUFFER,s.STENCIL_ATTACHMENT,s.RENDERBUFFER,this.renderbuffer),s.bindFramebuffer(s.FRAMEBUFFER,null),s.bindRenderbuffer(s.RENDERBUFFER,null),s.bindTexture(s.TEXTURE_2D,null)),this.rawWidth=t,this.rawHeight=e,this.uvX=0,this.uvW=t/this._allocW,this.uvY=0,this.uvH=e/this._allocH}activate(t=!1){if(!this.framebuffer)return;const e=this.gl;e.bindFramebuffer(e.FRAMEBUFFER,this.framebuffer),e.viewport(0,0,this.rawWidth,this.rawHeight),t&&this.clear()}clear(t){if(!this.framebuffer)return;const e=this.gl,r=t??this.clearColor;r&&(r.setClearColor(e),e.clear(e.COLOR_BUFFER_BIT|e.STENCIL_BUFFER_BIT))}deactivate(){const t=this.gl;t.bindFramebuffer(t.FRAMEBUFFER,null)}GetPixels(t=0,e=0,r=this.rawWidth,i=this.rawHeight){if(!this.framebuffer)return new Uint8Array(0);const s=Math.floor(t),a=Math.floor(e),o=Math.floor(r),h=Math.floor(i);if(o<=0||h<=0)return new Uint8Array(0);if(s<0||a<0||s>=this.rawWidth||a>=this.rawHeight)return new Uint8Array(0);const c=Math.min(o,this.rawWidth-s),u=Math.min(h,this.rawHeight-a),l=this.rawHeight-a-u,d=this.gl,f=d.getParameter(d.FRAMEBUFFER_BINDING);d.bindFramebuffer(d.FRAMEBUFFER,this.framebuffer);const x=new Uint8Array(c*u*4);return d.readPixels(s,l,c,u,d.RGBA,d.UNSIGNED_BYTE,x),d.bindFramebuffer(d.FRAMEBUFFER,f),x}GetColorAt(t,e){const r=this.GetPixels(t,e,1,1);return r.length<4?new m(0,0,0,0):new m(r[0],r[1],r[2],r[3])}destroy(){super.destroy(),this.framebuffer&&this.gl.deleteFramebuffer(this.framebuffer),this.renderbuffer&&this.gl.deleteRenderbuffer(this.renderbuffer),this.base?.destroy(this.gl)}}const Xt={fontFamily:"Arial",fontSize:24,fontWeight:"normal",fill:"#000000",strokeThickness:1,align:"left",lineHeight:1};class gt extends I{canvas;ctx;render;_text;_style;options;flipY=!0;constructor(t,e){super(),this.render=t,this._style={...Xt,...e},this._text=e?.text??"",this.options={premultipliedAlpha:t.premultipliedAlpha,...e},this.scale=1/t.dpr,this.canvas=document.createElement("canvas");const r=this.canvas.getContext("2d",{willReadFrequently:!0});if(!r)throw new Error("Failed to get 2d context for TextTexture");this.ctx=r,this.canvas.width=1,this.canvas.height=1;const i=tt(t,this.canvas,this.options);this.setBase(new k(i,1,1)),this.update()}get text(){return this._text}set text(t){this._text!==t&&(this._text=t,this.update())}get style(){return this._style}set style(t){this._style={...this._style,...t},this.update()}updateOffset(t,e){switch(this._style.align){case"center":return t/2;case"right":return t;default:return e}}update(){const t=this.ctx,e=this.style,r=e.fontSize,i=e.fontWeight,s=e.fontFamily,a=e.lineHeight,o=`${i} ${r}px ${s}`,h=this.render.dpr;t.font=o,t.textBaseline="top";const c=this._text.split(`
|
|
2
|
-
`);let u=0,l=0;const d=t.measureText(c[0]),f=d.fontBoundingBoxAscent+d.fontBoundingBoxDescent;for(const
|
|
1
|
+
(function(g,M){typeof exports=="object"&&typeof module<"u"?M(exports):typeof define=="function"&&define.amd?define(["exports"],M):(g=typeof globalThis<"u"?globalThis:g||self,M(g.RapidRender={}))})(this,(function(g){"use strict";var M=(n=>(n[n.Float32=0]="Float32",n[n.Uint32=1]="Uint32",n[n.Uint16=2]="Uint16",n))(M||{});class C{usedElemNum;typedArray;arrayType;maxElemNum;bytePerElem;arraybuffer;uint32;float32;uint16;constructor(t){this.usedElemNum=0,this.maxElemNum=512,this.bytePerElem=this.getArrayType(t).BYTES_PER_ELEMENT,this.arrayType=t,this.arraybuffer=new ArrayBuffer(this.maxElemNum*this.bytePerElem),this.updateTypedArray()}getArrayType(t){switch(t){case 0:return Float32Array;case 1:return Uint32Array;case 2:return Uint16Array;default:throw new Error("Unsupported ArrayType")}}updateTypedArray(){switch(this.uint32=new Uint32Array(this.arraybuffer),this.float32=new Float32Array(this.arraybuffer),this.uint16=new Uint16Array(this.arraybuffer),this.arrayType){case 0:this.typedArray=this.float32;break;case 1:this.typedArray=this.uint32;break;case 2:this.typedArray=this.uint16;break}}clear(){this.usedElemNum=0}static removeAtIndices(t,e){const r=Math.max(...e.map(o=>o.usedElemNum)),i=[...new Set(t)].filter(o=>o>=0&&o<r).sort((o,h)=>o-h);if(i.length===0)return 0;let s=0,a=0;for(let o=0;o<r;o++){if(a<i.length&&o===i[a]){a++;continue}if(s!==o)for(const h of e)h.typedArray[s]=h.typedArray[o];s++}for(const o of e)o.usedElemNum=s;return r-s}resize(t=0){if(t+=this.usedElemNum,t>this.maxElemNum){for(;t>this.maxElemNum;)this.maxElemNum*=2;return this.setMaxSize(this.maxElemNum),!0}return!1}setMaxSize(t=this.maxElemNum){const e=this.typedArray;this.maxElemNum=t,this.arraybuffer=new ArrayBuffer(t*this.bytePerElem),this.updateTypedArray(),this.typedArray.set(e)}pushUint32(t){this.resize(1),this.uint32[this.usedElemNum++]=t}pushFloat32(t){this.resize(1),this.float32[this.usedElemNum++]=t}push(t){this.resize(1),this.typedArray[this.usedElemNum++]=t}pop(){return this.typedArray[--this.usedElemNum]}top(){return this.typedArray[this.usedElemNum-1]}get(t){return this.typedArray[t]}getArray(t=0,e){return e===void 0?this.typedArray:this.typedArray.subarray(t,e)}get length(){return this.usedElemNum}reset(){this.usedElemNum=0}}class J extends C{buffer;gl;dirty=!0;type;webglBufferSize=0;usage;constructor(t,e,r=t.ARRAY_BUFFER,i=t.DYNAMIC_DRAW){super(e),this.gl=t,this.buffer=t.createBuffer(),this.type=r,this.usage=i}makeDirty(){this.dirty=!0}clear(){super.clear(),this.dirty=!1}bindBuffer(){this.gl.bindBuffer(this.type,this.buffer)}bufferData(){if(this.dirty){const t=this.gl;this.maxElemNum>this.webglBufferSize&&(t.bufferData(this.type,this.maxElemNum*this.bytePerElem,this.usage),this.webglBufferSize=this.maxElemNum),t.bufferSubData(this.type,0,this.getArray(0,this.usedElemNum)),this.dirty=!1}}}class m{_r;_g;_b;_a;uint32;premultipliedUint32;static clampByte(t){return t<=0?0:t>=255?255:Math.round(t)}constructor(t,e,r,i=255){this._r=t,this._g=e,this._b=r,this._a=i,this.updateUint()}setClearColor(t){t.clearColor(this.r/255,this.g/255,this.b/255,this.a/255)}get r(){return this._r}set r(t){this._r=t,this.updateUint()}get g(){return this._g}set g(t){this._g=t,this.updateUint()}get b(){return this._b}set b(t){this._b=t,this.updateUint()}get a(){return this._a}set a(t){this._a=t,this.updateUint()}updateUint(){this.uint32=(this._a<<24|this._b<<16|this._g<<8|this._r)>>>0;const t=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);this.premultipliedUint32=(t<<24|i<<16|r<<8|e)>>>0}reset(){this._r=0,this._g=0,this._b=0,this._a=255,this.updateUint()}setRGBA(t,e,r,i){this.r=t,this.g=e,this.b=r,this.a=i,this.updateUint()}setHSL(t,e,r){t=(t%360+360)%360,e=Math.max(0,Math.min(100,e))/100,r=Math.max(0,Math.min(100,r))/100;const i=(1-Math.abs(2*r-1))*e,s=i*(1-Math.abs(t/60%2-1)),a=r-i/2;let o=0,h=0,c=0;return t<60?[o,h,c]=[i,s,0]:t<120?[o,h,c]=[s,i,0]:t<180?[o,h,c]=[0,i,s]:t<240?[o,h,c]=[0,s,i]:t<300?[o,h,c]=[s,0,i]:[o,h,c]=[i,0,s],this.setRGBA((o+a)*255,(h+a)*255,(c+a)*255,255),this}toHex(){const t=e=>Math.max(0,Math.min(255,Math.round(e))).toString(16).padStart(2,"0");return`#${t(this._r)}${t(this._g)}${t(this._b)}${t(this._a)}`}copy(t){this.setRGBA(t.r,t.g,t.b,t.a)}clone(){return new m(this._r,this._g,this._b,this._a)}toHexString(t=!0){const e=this.r.toString(16).padStart(2,"0"),r=this.g.toString(16).padStart(2,"0"),i=this.b.toString(16).padStart(2,"0"),s=this.a.toString(16).padStart(2,"0");return`#${e}${r}${i}${t?s:""}`}equal(t){return t.r===this.r&&t.g===this.g&&t.b===this.b&&t.a===this.a}equals(t){return this.equal(t)}static FromHSL(t,e,r){return new m(0,0,0).setHSL(t,e,r)}static FromRGB(t,e,r){return new m(t,e,r)}static fromNorm(t,e,r,i=1){return new m(Math.round(t*255),Math.round(e*255),Math.round(r*255),Math.round(i*255))}static clampColorByte(t){return t<=0?0:t>=255?255:Math.round(t)}static packColor(t,e,r,i,s){const a=m.clampColorByte(i),o=s?a/255:1,h=m.clampColorByte(t*o),c=m.clampColorByte(e*o),u=m.clampColorByte(r*o);return(a<<24|u<<16|c<<8|h)>>>0}static fromHex(t){t.startsWith("#")&&(t=t.slice(1));const e=parseInt(t.slice(0,2),16),r=parseInt(t.slice(2,4),16),i=parseInt(t.slice(4,6),16);let s=255;return t.length>=8&&(s=parseInt(t.slice(6,8),16)),new m(e,r,i,s)}add(t){return new m(Math.min(this.r+t.r,255),Math.min(this.g+t.g,255),Math.min(this.b+t.b,255),Math.min(this.a+t.a,255))}subtract(t){return new m(this.r-t.r,this.g-t.g,this.b-t.b,this.a-t.a)}divide(t){return t instanceof m?new m(this.r/t.r,this.g/t.g,this.b/t.b,this.a/t.a):new m(this.r/t,this.g/t,this.b/t,this.a/t)}multiply(t){return t instanceof m?new m(this.r*t.r,this.g*t.g,this.b*t.b,this.a*t.a):new m(this.r*t,this.g*t,this.b*t,this.a*t)}clamp(){this.r=Math.max(0,Math.min(255,this.r)),this.g=Math.max(0,Math.min(255,this.g)),this.b=Math.max(0,Math.min(255,this.b)),this.a=Math.max(0,Math.min(255,this.a))}static Red=new m(255,0,0,255);static Green=new m(0,255,0,255);static Blue=new m(0,0,255,255);static Yellow=new m(255,255,0,255);static Purple=new m(128,0,128,255);static Orange=new m(255,165,0,255);static Pink=new m(255,192,203,255);static Gray=new m(128,128,128,255);static Brown=new m(139,69,19,255);static Cyan=new m(0,255,255,255);static Magenta=new m(255,0,255,255);static Lime=new m(192,255,0,255);static White=new m(255,255,255,255);static Black=new m(0,0,0,255);static Transparent=new m(0,0,0,0);static TRANSPARENT=new m(0,0,0,0)}const E=6;class ft{matrixCount=0;buffer;data;temporary=-1;constructor(t=10){this.buffer=new C(M.Float32),this.buffer.resize(t*E),this.data=this.buffer.getArray(),this.reset()}alloc(){const t=this.allocDirty();return this.identity(t),t}allocDirty(){return this.buffer.resize(E)&&(this.data=this.buffer.getArray()),this.buffer.usedElemNum+=E,this.matrixCount++}reset(){this.matrixCount=0,this.buffer.usedElemNum=0,this.temporary=this.alloc()}identity(t){const e=t*E,r=this.data;r[e]=1,r[e+1]=0,r[e+2]=0,r[e+3]=1,r[e+4]=0,r[e+5]=0}translate(t,e,r){const i=t*E,s=this.data;s[i+4]=s[i]*e+s[i+2]*r+s[i+4],s[i+5]=s[i+1]*e+s[i+3]*r+s[i+5]}scale(t,e,r){const i=t*E,s=this.data;s[i]*=e,s[i+1]*=e,s[i+2]*=r,s[i+3]*=r}rotate(t,e){const r=t*E,i=this.data,s=Math.cos(e),a=Math.sin(e),o=i[r],h=i[r+1],c=i[r+2],u=i[r+3];i[r]=o*s+c*a,i[r+1]=h*s+u*a,i[r+2]=o*-a+c*s,i[r+3]=h*-a+u*s}rotateWithOffset(t,e,r,i){this.translate(t,r,i),this.rotate(t,e),this.translate(t,-r,-i)}copy(t,e){const r=t*E,i=e*E,s=this.data;s[r]=s[i],s[r+1]=s[i+1],s[r+2]=s[i+2],s[r+3]=s[i+3],s[r+4]=s[i+4],s[r+5]=s[i+5]}multiply(t,e){this.multiplyOut(t,t,e)}multiplyOut(t,e,r){const i=t*E,s=e*E,a=r*E,o=this.data,h=o[s],c=o[s+1],u=o[s+2],l=o[s+3],d=o[s+4],f=o[s+5],x=o[a],v=o[a+1],y=o[a+2],T=o[a+3],w=o[a+4],b=o[a+5];o[i]=h*x+u*v,o[i+1]=c*x+l*v,o[i+2]=h*y+u*T,o[i+3]=c*y+l*T,o[i+4]=h*w+u*b+d,o[i+5]=c*w+l*b+f}invert(t){const e=t*E,r=this.data,i=r[e],s=r[e+1],a=r[e+2],o=r[e+3],h=r[e+4],c=r[e+5];let u=i*o-s*a;if(!u){this.identity(t);return}u=1/u,r[e]=o*u,r[e+1]=-s*u,r[e+2]=-a*u,r[e+3]=i*u,r[e+4]=(a*c-o*h)*u,r[e+5]=(s*h-i*c)*u}transformPoint(t,e,r){const i=t*E,s=this.data;return{x:e*s[i]+r*s[i+2]+s[i+4],y:e*s[i+1]+r*s[i+3]+s[i+5]}}worldToLocal(t,e,r){const i=this.allocDirty();this.copy(i,t),this.invert(i);const s=this.transformPoint(i,e,r);return this.matrixCount--,this.buffer.usedElemNum-=E,s}localToWorld(t,e,r){return this.transformPoint(t,e,r)}getPosition(t){const e=t*E,r=this.data;return{x:r[e+4],y:r[e+5]}}getScale(t){const e=t*E,r=this.data,i=Math.sqrt(r[e]*r[e]+r[e+1]*r[e+1]),s=Math.sqrt(r[e+2]*r[e+2]+r[e+3]*r[e+3]);return{x:i,y:s}}getRotation(t){const e=t*E,r=this.data;return Math.atan2(r[e+1],r[e])}toCSSMatrix(t,e=1,r=1){const i=t*E,s=this.data;return`matrix(${s[i]*e}, ${s[i+1]*r}, ${s[i+2]*e}, ${s[i+3]*r}, ${s[i+4]*e}, ${s[i+5]*r})`}getMatrix(t){const e=t*E;return this.data.slice(e,e+E)}getMatrixRef(t){const e=t*E;return this.data.subarray(e,e+E)}setMatrix(t,e){const r=t*E,i=this.data;i[r]=e[0],i[r+1]=e[1],i[r+2]=e[2],i[r+3]=e[3],i[r+4]=e[4],i[r+5]=e[5]}multiplyAffineInPlace(t,e,r,i,s,a,o){const h=t*6,c=this.data,u=c[h],l=c[h+1],d=c[h+2],f=c[h+3],x=c[h+4],v=c[h+5];c[h]=u*e+d*r,c[h+1]=l*e+f*r,c[h+2]=u*i+d*s,c[h+3]=l*i+f*s,c[h+4]=u*a+d*o+x,c[h+5]=l*a+f*o+v}multiplyAffine(t,e,r,i,s,a,o,h){const c=t*6,u=e*6,l=this.data,d=l[c],f=l[c+1],x=l[c+2],v=l[c+3],y=l[c+4],T=l[c+5];l[u]=d*r+x*i,l[u+1]=f*r+v*i,l[u+2]=d*s+x*a,l[u+3]=f*s+v*a,l[u+4]=d*o+x*h+y,l[u+5]=f*o+v*h+T}}class mt{matrix=new ft;step=0;stack=new C(M.Uint32);stepWorldM=new C(M.Uint32);stepAction=new C(M.Uint32);stepParentM=new C(M.Uint32);parentStack=new C(M.Uint32);curLocalM=-1;curWorldM=-1;rapid;constructor(t){this.reset(),this.rapid=t}save(){this.stack.push(this.curWorldM);const t=this.curWorldM;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.matrix.copy(this.curWorldM,t),{world:this.curWorldM,local:this.curLocalM,step:this.step++}}restore(){this.stack.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.step++)}updateMatrix(t){const e=typeof t=="number"?t:t.step;let r=0,i=e;for(;i<this.step;){if(this.stepAction.get(i)===1){const a=this.stepWorldM.get(i),o=a-1,h=this.stepParentM.get(i);this.matrix.multiplyOut(a,h,o),r++}else if(r--,r===0)break;i++}}translate(t,e){this.matrix.translate(this.curLocalM,t,e),this.matrix.translate(this.curWorldM,t,e)}scale(t,e=t){this.matrix.scale(this.curLocalM,t,e),this.matrix.scale(this.curWorldM,t,e)}rotate(t){this.matrix.rotate(this.curLocalM,t),this.matrix.rotate(this.curWorldM,t)}rotateWithOffset(t,e,r){this.matrix.rotateWithOffset(this.curLocalM,t,e,r),this.matrix.rotateWithOffset(this.curWorldM,t,e,r)}identity(){this.matrix.identity(this.curLocalM),this.matrix.identity(this.curWorldM)}reset(){this.matrix.reset(),this.stack.reset(),this.stepAction.reset(),this.stepWorldM.reset(),this.stepParentM.reset(),this.step=0,this.curLocalM=this.matrix.alloc(),this.curWorldM=this.matrix.alloc()}localToWorld(t,e){return this.matrix.localToWorld(this.curWorldM,t,e)}worldToLocal(t,e){return this.matrix.worldToLocal(this.curWorldM,t,e)}getGlobalPosition(){return this.matrix.getPosition(this.curWorldM)}getGlobalScale(){return this.matrix.getScale(this.curWorldM)}getGlobalRotation(){return this.matrix.getRotation(this.curWorldM)}toCSSMatrix(){return this.matrix.toCSSMatrix(this.curWorldM)}getTransform(){const t=this.curWorldM*E;return this.matrix.data.slice(t,t+E)}setTransform(t){const e=this.curWorldM*E,r=this.matrix.data;r[e]=t[0],r[e+1]=t[1],r[e+2]=t[2],r[e+3]=t[3],r[e+4]=t[4],r[e+5]=t[5]}transformPoint(t,e){return this.matrix.transformPoint(this.curLocalM,t,e)}applyTransform(t,e=0,r=0,i=-1){let s=t.x??0,a=t.y??0;t.position&&(s+=t.position.x,a+=t.position.y);let o=1,h=1;const c=t.scale;c&&(typeof c=="number"?(o=c,h=c):(o=c.x,h=c.y));const u=t.rotation??0;let l=t.offsetX??0,d=t.offsetY??0;t.offset&&(l+=t.offset.x,d+=t.offset.y);const f=t.origin;f!==void 0&&(typeof f=="number"?(l-=f*e,d-=f*r):(l-=f.x*e,d-=f.y*r));const x=u?Math.cos(u):1,v=u?Math.sin(u):0,y=x*o,T=v*o,w=-v*h,b=x*h,N=s+y*l+w*d,_=a+T*l+b*d;if(i!==-1){this.matrix.multiplyAffine(this.curWorldM,i,y,T,w,b,N,_);return}this.matrix.multiplyAffineInPlace(this.curLocalM,y,T,w,b,N,_),this.matrix.multiplyAffineInPlace(this.curWorldM,y,T,w,b,N,_)}}var $=(n=>(n[n.REPEAT=0]="REPEAT",n[n.CLAMP=1]="CLAMP",n[n.MIRRORED_REPEAT=2]="MIRRORED_REPEAT",n))($||{});class xt{render;cache=new Map;constructor(t){this.render=t}async load(t,e){let r=this.cache.get(t);if(r)return new O(r);try{const i=await this._fetchImage(t);return r=G.fromSource(this.render,i,e),r.uid=t,this.cache.set(t,r),new O(r)}catch(i){throw console.error(`[TextureManager] Failed to load: ${t}`,i),i}}create(t,e){if(e?.key&&this.cache.has(e.key))return new O(this.cache.get(e.key));const r=G.fromSource(this.render,t,e);return e?.key&&(r.uid=e.key,this.cache.set(e.key,r)),new O(r)}createRenderTexture(t){return new pt(this.render,t)}createTextTexture(t){return new gt(this.render,t)}destroy(t,e=!1){let r,i;typeof t=="string"?(i=t,r=this.cache.get(i)):t instanceof O?(r=t.base,i=r?.uid,t.destroy()):(r=t,i=r.uid),r&&(r.refCount<=0||e)&&(r.destroy(this.render.gl),i&&this.cache.delete(i))}destroyAll(){for(const t of this.cache.values())t.destroy(this.render.gl);this.cache.clear()}_fetchImage(t){return new Promise((e,r)=>{const i=new Image;i.crossOrigin="anonymous",i.onload=()=>e(i),i.onerror=()=>r(new Error(`Failed to load image resource: ${t}`)),i.src=t})}}class G{glTexture=null;width;height;uid;refCount=0;constructor(t,e,r){this.glTexture=t,this.width=e,this.height=r}static fromSource(t,e,r={}){const i=tt(t,e,r);return new G(i,e.width,e.height)}updateSource(t,e,r={}){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))}setFilterMode(t,e){this.glTexture&&(t.bindTexture(t.TEXTURE_2D,this.glTexture),nt(t,e),t.bindTexture(t.TEXTURE_2D,null))}setWrapMode(t,e){this.glTexture&&(t.bindTexture(t.TEXTURE_2D,this.glTexture),st(t,e),t.bindTexture(t.TEXTURE_2D,null))}destroy(t){this.glTexture&&(t.deleteTexture(this.glTexture),this.glTexture=null)}}class O{base;uvX=0;uvY=0;uvW=1;uvH=1;rawWidth=0;rawHeight=0;offsetX=0;offsetY=0;scale=1;isAtlas=!1;flipY=!1;isRotated=!1;glTexture=null;get width(){return(this.isRotated?this.rawHeight:this.rawWidth)*this.scale}get height(){return(this.isRotated?this.rawWidth:this.rawHeight)*this.scale}_px=0;_py=0;_pw=0;_ph=0;constructor(t){t&&this.setBase(t)}setBase(t){return this.base===t?this:(this.base&&this.base.refCount--,this.base=t,this.base.refCount++,this.setRegion(0,0,t.width,t.height),this.glTexture=t.glTexture,this)}setRegion(t,e,r,i){return this.base?(this._px=t,this._py=e,this._pw=r,this._ph=i,this.isAtlas=t!==0||e!==0||r!==this.base.width||i!==this.base.height,this.uvX=t/this.base.width,this.uvY=e/this.base.height,this.uvW=this.uvX+r/this.base.width,this.uvH=this.uvY+i/this.base.height,this.rawWidth=r*this.scale,this.rawHeight=i*this.scale,this):this}getSubTexture(t,e,r,i){if(!this.base)return new O;const s=this.clone(),a=this._px,o=this._py;return s.setRegion(a+t,o+e,r,i),s}clone(t){const e=t??new O(this.base);return t&&this.base&&t.setBase(this.base),e.scale=this.scale,e.uvX=this.uvX,e.uvY=this.uvY,e.uvW=this.uvW,e.uvH=this.uvH,e.rawWidth=this.rawWidth,e.rawHeight=this.rawHeight,e.offsetX=this.offsetX,e.offsetY=this.offsetY,e.flipY=this.flipY,e.isRotated=this.isRotated,e.isAtlas=this.isAtlas,e._px=this._px,e._py=this._py,e._pw=this._pw,e._ph=this._ph,e}splitGrid(t,e,r,i,s=0){if(!this.base)return[];const a=[],o=this.rawWidth,h=this.rawHeight,c=Math.max(0,s),u=t+c,l=e+c,d=r??Math.floor((o+c)/u),f=i??Math.floor((h+c)/l);for(let x=0;x<f;x++)for(let v=0;v<d;v++)a.push(this.getSubTexture(v*u,x*l,t,e));return a}static fromImageSource(t,e,r={}){const i=tt(t,e,r);return new O(new G(i,e.width,e.height))}destroy(){this.base&&(this.base.refCount--,this.base=void 0),this.glTexture=null}}class pt extends O{framebuffer;renderbuffer;gl;flipY=!0;clearColor;_allocW=0;_allocH=0;constructor(t,e){super(),this.gl=t.gl,this.clearColor=e.clearColor??m.Black;const r=Math.max(Math.round(e.width),1),i=Math.max(Math.round(e.height),1),a=tt(t,{width:r,height:i},{...e,onlySize:!0});if(this.setBase(new G(a,r,i)),this.framebuffer=this.gl.createFramebuffer(),this.renderbuffer=this.gl.createRenderbuffer(),!this.framebuffer||!this.renderbuffer)throw new Error("Failed to create Framebuffer or Renderbuffer");this.resize(r,i,!0)}resize(t,e,r=!1){t=Math.max(Math.round(t),1),e=Math.max(Math.round(e),1);const i=this.rawWidth===t&&this.rawHeight===e;if(!r&&i)return;const s=this.gl;(r||t>this._allocW||e>this._allocH)&&this.base&&(this._allocW=Math.max(this._allocW,t),this._allocH=Math.max(this._allocH,e),this.base.width=this._allocW,this.base.height=this._allocH,s.bindTexture(s.TEXTURE_2D,this.base.glTexture),s.texImage2D(s.TEXTURE_2D,0,s.RGBA,this._allocW,this._allocH,0,s.RGBA,s.UNSIGNED_BYTE,null),s.bindRenderbuffer(s.RENDERBUFFER,this.renderbuffer),s.renderbufferStorage(s.RENDERBUFFER,s.STENCIL_INDEX8,this._allocW,this._allocH),s.bindFramebuffer(s.FRAMEBUFFER,this.framebuffer),s.framebufferTexture2D(s.FRAMEBUFFER,s.COLOR_ATTACHMENT0,s.TEXTURE_2D,this.base.glTexture,0),s.framebufferRenderbuffer(s.FRAMEBUFFER,s.STENCIL_ATTACHMENT,s.RENDERBUFFER,this.renderbuffer),s.bindFramebuffer(s.FRAMEBUFFER,null),s.bindRenderbuffer(s.RENDERBUFFER,null),s.bindTexture(s.TEXTURE_2D,null)),this.rawWidth=t,this.rawHeight=e,this.uvX=0,this.uvW=t/this._allocW,this.uvY=0,this.uvH=e/this._allocH}activate(t=!1){if(!this.framebuffer)return;const e=this.gl;e.bindFramebuffer(e.FRAMEBUFFER,this.framebuffer),e.viewport(0,0,this.rawWidth,this.rawHeight),t&&this.clear()}clear(t){if(!this.framebuffer)return;const e=this.gl,r=t??this.clearColor;r&&(r.setClearColor(e),e.clear(e.COLOR_BUFFER_BIT|e.STENCIL_BUFFER_BIT))}deactivate(){const t=this.gl;t.bindFramebuffer(t.FRAMEBUFFER,null)}GetPixels(t=0,e=0,r=this.rawWidth,i=this.rawHeight){if(!this.framebuffer)return new Uint8Array(0);const s=Math.floor(t),a=Math.floor(e),o=Math.floor(r),h=Math.floor(i);if(o<=0||h<=0)return new Uint8Array(0);if(s<0||a<0||s>=this.rawWidth||a>=this.rawHeight)return new Uint8Array(0);const c=Math.min(o,this.rawWidth-s),u=Math.min(h,this.rawHeight-a),l=this.rawHeight-a-u,d=this.gl,f=d.getParameter(d.FRAMEBUFFER_BINDING);d.bindFramebuffer(d.FRAMEBUFFER,this.framebuffer);const x=new Uint8Array(c*u*4);return d.readPixels(s,l,c,u,d.RGBA,d.UNSIGNED_BYTE,x),d.bindFramebuffer(d.FRAMEBUFFER,f),x}GetColorAt(t,e){const r=this.GetPixels(t,e,1,1);return r.length<4?new m(0,0,0,0):new m(r[0],r[1],r[2],r[3])}destroy(){super.destroy(),this.framebuffer&&this.gl.deleteFramebuffer(this.framebuffer),this.renderbuffer&&this.gl.deleteRenderbuffer(this.renderbuffer),this.base?.destroy(this.gl)}}const Xt={fontFamily:"Arial",fontSize:24,fontWeight:"normal",fill:"#000000",strokeThickness:0,align:"left",lineHeight:1};class gt extends O{canvas;ctx;render;_text;_style;options;flipY=!0;constructor(t,e){super(),this.render=t,this._style={...Xt,...e},this._text=e?.text??"",this.options={premultipliedAlpha:t.premultipliedAlpha,...e},this.scale=1/t.dpr,this.canvas=document.createElement("canvas");const r=this.canvas.getContext("2d",{willReadFrequently:!0});if(!r)throw new Error("Failed to get 2d context for TextTexture");this.ctx=r,this.canvas.width=1,this.canvas.height=1;const i=tt(t,this.canvas,this.options);this.setBase(new G(i,1,1)),this.update()}get text(){return this._text}set text(t){this._text!==t&&(this._text=t,this.update())}get style(){return this._style}set style(t){this._style={...this._style,...t},this.update()}updateOffset(t,e){switch(this._style.align){case"center":return t/2;case"right":return t;default:return e}}update(){const t=this.ctx,e=this.style,r=e.fontSize,i=e.fontWeight,s=e.fontFamily,a=e.lineHeight,o=`${i} ${r}px ${s}`,h=this.render.dpr;t.font=o,t.textBaseline="top";const c=this._text.split(`
|
|
2
|
+
`);let u=0,l=0;const d=t.measureText(c[0]||"M"),f=(d.fontBoundingBoxAscent||r)+(d.fontBoundingBoxDescent||r*.2);for(const k of c){const L=t.measureText(k);L.width>u&&(u=L.width),l+=f*a}l-=f*(a-1);const x=this._style.strokeThickness||0,v=Math.ceil(u+x*2)||1,y=Math.ceil(l+x*2)||1,T=Math.ceil(v*h),w=Math.ceil(y*h);(this.canvas.width!==T||this.canvas.height!==w)&&(this.canvas.width=T,this.canvas.height=w),t.setTransform(h,0,0,h,0,0),t.clearRect(0,0,v,y),t.font=o,t.textBaseline="top",t.textAlign=this._style.align;const b=this.updateOffset(v,x),N=e.strokeThickness,_=N>0;_&&(t.lineWidth=N,t.strokeStyle=e.stroke,t.lineJoin="round"),e.fill&&(t.fillStyle=e.fill);let D=x;for(const k of c)_&&t.strokeText(k,b,D),e.fill&&t.fillText(k,b,D),D+=f*a;this.base?.updateSource(this.render.gl,this.canvas,this.options),this.setRegion(0,0,T,w)}}const jt=(n,t=!1,e=!0)=>{const r=n.getContext("webgl2",{stencil:!0,antialias:t,premultipliedAlpha:e});if(!r)throw new Error("WebGL2 is not supported in this browser.");return r},vt=(n,t,e)=>{const r=n.createShader(e);if(!r)throw new Error("Unable to create webgl shader");if(n.shaderSource(r,t),n.compileShader(r),!n.getShaderParameter(r,n.COMPILE_STATUS)){const s=n.getShaderInfoLog(r);throw console.error("Shader compilation failed:",s),new Error("Unable to compile shader: "+s+t)}return r},$t=(n,t,e)=>{var r=n.createProgram(),i=vt(n,t,35633),s=vt(n,e,35632);if(!r)throw new Error("Unable to create program shader");if(n.attachShader(r,i),n.attachShader(r,s),n.linkProgram(r),!n.getProgramParameter(r,n.LINK_STATUS)){const o=n.getProgramInfoLog(r);throw new Error("Unable to link shader program: "+o)}return r};function st(n,t){const e={[$.CLAMP]:n.CLAMP_TO_EDGE,[$.REPEAT]:n.REPEAT,[$.MIRRORED_REPEAT]:n.MIRRORED_REPEAT}[t]||n.CLAMP_TO_EDGE;n.texParameteri(n.TEXTURE_2D,n.TEXTURE_WRAP_S,e),n.texParameteri(n.TEXTURE_2D,n.TEXTURE_WRAP_T,e)}function nt(n,t){const e={[it.LINEAR]:n.LINEAR,[it.NEAREST]:n.NEAREST}[t]??n.NEAREST;n.texParameteri(n.TEXTURE_2D,n.TEXTURE_MIN_FILTER,e),n.texParameteri(n.TEXTURE_2D,n.TEXTURE_MAG_FILTER,e)}function tt(n,t,e){const r=n.gl,i=r.createTexture();if(!i)throw new Error("Unable to create WebGL texture");if(r.bindTexture(r.TEXTURE_2D,i),r.pixelStorei(r.UNPACK_FLIP_Y_WEBGL,0),r.pixelStorei(r.UNPACK_ALIGNMENT,1),st(r,e.wrap??$.CLAMP),nt(r,e.textureFilter??n.textureFilter),e.onlySize){const s=t;r.texImage2D(r.TEXTURE_2D,0,r.RGBA,s.width,s.height,0,r.RGBA,r.UNSIGNED_BYTE,null)}else{const s=e.premultipliedAlpha??n.premultipliedAlpha;s&&r.pixelStorei(r.UNPACK_PREMULTIPLY_ALPHA_WEBGL,!0),r.texImage2D(r.TEXTURE_2D,0,r.RGBA,r.RGBA,r.UNSIGNED_BYTE,t),s&&r.pixelStorei(r.UNPACK_PREMULTIPLY_ALPHA_WEBGL,!1)}return r.bindTexture(r.TEXTURE_2D,null),i}function H(n,t){if(n.includes("%TEXTURE_NUM%")&&(n=n.replace("%TEXTURE_NUM%",t.toString())),n.includes("%GET_COLOR%")){let e="";for(let r=0;r<t;r++)r==0?e+=`if(vTextureId == ${r})`:r==t-1?e+="else":e+=`else if(vTextureId == ${r})`,e+=`{return texture(uTextures[${r}], uv);}`;n=n.replace("%GET_COLOR%",e)}return n}const Kt=5126,at=5121;function qt(n,t,e){n.vertexAttribDivisor(t,e)}function yt(n,t,e,r,i){n.drawArraysInstanced(t,e,r,i)}let Qt=0;class ht{program;attributeLoc={};uniformLoc={};isCustom=!1;padding=0;uniformType={};uniformIsArray={};gl;shaderId=Qt++;vao;constructor(t,e,r,i){this.gl=t,this.program=$t(t,e,r),this.parseShader(e),this.parseShader(r),this.vao=t.createVertexArray(),i&&this.setAttributes(i)}use(){this.gl.useProgram(this.program)}bindVAO(){this.gl.bindVertexArray(this.vao)}unbindVAO(){this.gl.bindVertexArray(null)}setUniform(t,e){const r=this.uniformLoc[t];if(r===void 0)return;const i=this.uniformType[t],s=this.uniformIsArray[t];Zt(this.gl,r,i,e,s)}setUniforms(t){for(const e in t)this.setUniform(e,t[e])}setAttribute(t){const e=this.attributeLoc[t.name];if(e===void 0)return;const r=this.gl;r.vertexAttribPointer(e,t.size,t.type??Kt,t.normalized??!1,t.stride,t.offset??0),r.enableVertexAttribArray(e),qt(r,e,t.divisor??0)}setAttributes(t){for(const e of t)this.setAttribute(e)}destroy(){this.gl.deleteProgram(this.program)}parseShader(t){const e=this.gl,r=t.match(/(?:in|attribute)\s+\w+\s+\w+/g);if(r)for(const s of r){const o=s.split(/\s+/)[2],h=e.getAttribLocation(this.program,o);h!==-1&&(this.attributeLoc[o]=h)}const i=t.match(/uniform\s+\w+\s+\w+(?:\[\d+\])?/g);if(i)for(const s of i){const a=s.split(/\s+/),o=a[1],h=a[2].replace(/\[\d+\]$/,""),c=e.getUniformLocation(this.program,h);c!==null&&(this.uniformLoc[h]=c,this.uniformType[h]=o,this.uniformIsArray[h]=/\[\d+\]$/.test(a[2]))}}}function Zt(n,t,e,r,i=!1){switch(e){case"float":return n.uniform1f(t,r);case"int":case"bool":case"sampler2D":case"samplerCube":return i?n.uniform1iv(t,r):n.uniform1i(t,r);case"vec2":return n.uniform2fv(t,r);case"vec3":return n.uniform3fv(t,r);case"vec4":return n.uniform4fv(t,r);case"ivec2":case"bvec2":return n.uniform2iv(t,r);case"ivec3":case"bvec3":return n.uniform3iv(t,r);case"ivec4":case"bvec4":return n.uniform4iv(t,r);case"mat2":return n.uniformMatrix2fv(t,!1,r);case"mat3":return n.uniformMatrix3fv(t,!1,r);case"mat4":return n.uniformMatrix4fv(t,!1,r);default:return Jt(n,t,r)}}function Jt(n,t,e){if(typeof e=="number")return n.uniform1f(t,e);const r=e;switch(r.length){case 2:return n.uniform2fv(t,r);case 3:return n.uniform3fv(t,r);case 4:return n.uniform4fv(t,r);case 9:return n.uniformMatrix3fv(t,!1,r);case 16:return n.uniformMatrix4fv(t,!1,r);default:return n.uniform1fv(t,r)}}class ot{vs;fs;glshader=new Map;uniforms={};uniformDirty=new Set;uniformTextures={};usedTextureUnitNum=0;padding=0;rapid;constructor(t,e,r,i=0,s){this.vs=e,this.fs=r,this.rapid=t,this.uniforms=s??{},this.usedTextureUnitNum=i}setUniforms(t){this.rapid.flush();const e={},r={};for(const[i,s]of Object.entries(t))s instanceof WebGLTexture?r[i]=s:e[i]=s;Object.assign(this.uniforms,e),Object.assign(this.uniformTextures,r);for(const i of this.glshader.keys())this.uniformDirty.add(i)}setPadding(t){this.padding=t;for(const e of this.glshader.values())e.padding=t;return this}applyUniform(t,e){const r=this.glshader.get(t);r&&(Object.keys(e).length>0&&r.setUniforms(e),this.uniformDirty.has(t)&&(r.setUniforms(this.uniforms),this.uniformDirty.delete(t)))}getGLShader(t,e,r,i){if(this.glshader.has(e))return this.glshader.get(e);if(i==null||r==null)return console.warn("CustomGlShader: missing base shader for "+e),null;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);const s=t.createShader(r,i);return s.isCustom=!0,s.padding=this.padding,this.glshader.set(e,s),this.uniformDirty.add(e),s}destroy(){this.glshader.forEach(t=>t.destroy())}}class ct{constructor(t){this.rapid=t,this.gl=t.gl,this.maxTextureUnits=t.maxTextureUnits,this.matrixStore=t.matrix}defaultShader;currentShader;customShader=null;customShaderUsedTextureNum=0;gl;maxTextureUnits;matrixStore;isDefaultShader=!0;KEY="default";vs="";fs="";usedTextures=[];usedTexturePadding=[];createBuffer(){}createDefaultShader(){}get freeTextureUnitNum(){return this.maxTextureUnits-this.usedTextures.length-this.customShaderUsedTextureNum}getTextureUnitList(){return Array.from({length:this.maxTextureUnits},(t,e)=>e)}findTextureUnit(t,e=0,r=0){for(let i=0;i<this.usedTextures.length;i++){const s=i*2;if(this.usedTextures[i]===t&&this.usedTexturePadding[s]===e&&this.usedTexturePadding[s+1]===r)return i}return-1}useTexture(t,e=0,r=0){const i=this.findTextureUnit(t,e,r);return i==-1?(this.freeTextureUnitNum===0&&this.flush(),this.usedTextures.push(t),this.usedTexturePadding.push(e),this.usedTexturePadding.push(r),this.usedTextures.length-1):i}createShader(t,e){return new ht(this.gl,t,e)}createCustomShader(t){return t.getGLShader(this,this.KEY,this.vs,this.fs)}getCustomShader(t){if(!t)return this.defaultShader;if(t instanceof ot){const e=this.createCustomShader(t);return e??this.defaultShader}return t}isSameShader(t){return this.isDefaultShader&&!t?!0:this.getCustomShader(t)==this.currentShader}enter(t){this.resetRender(),this.currentShader=this.getCustomShader(t),t instanceof ot?(this.customShader=t,this.customShaderUsedTextureNum=t.usedTextureUnitNum):(this.customShader=null,this.customShaderUsedTextureNum=0),this.isDefaultShader=this.currentShader==this.defaultShader,this.currentShader.use(),this.currentShader.setUniform("u_projection",this.rapid.projection)}exit(){this.hasPendingContent()&&this.flush(),this.gl.bindVertexArray(null)}flush(){this.render(),this.resetRender()}render(){this.prepareRender()}prepareRender(){const t=this.currentShader;if(t.use(),this.customShader){const r=this.customShader,i={};for(const s in r.uniformTextures)i[s]=this.useTexture(r.uniformTextures[s]);this.customShader.applyUniform(this.KEY,i)}const e=this.gl;for(let r=0;r<this.usedTextures.length;r++)e.activeTexture(e.TEXTURE0+r),e.bindTexture(e.TEXTURE_2D,this.usedTextures[r]);t.setUniform("uTextures",Int32Array.from({length:this.usedTextures.length},(r,i)=>i)),t.setUniform("uPadding",Float32Array.from({length:this.usedTexturePadding.length},(r,i)=>this.usedTexturePadding[i]))}resetRender(){this.usedTextures.length=0,this.usedTexturePadding.length=0}hasPendingContent(){return!1}}const et=`#version 300 es\r
|
|
3
3
|
precision highp float;\r
|
|
4
4
|
\r
|
|
5
5
|
// per-vertex\r
|
|
@@ -96,7 +96,7 @@ void main(void) {\r
|
|
|
96
96
|
\r
|
|
97
97
|
// CUSTOM_CODE_CALL\r
|
|
98
98
|
}\r
|
|
99
|
-
`,K=48,Rt=new Float32Array([0,0,1,0,0,1,1,1]);class rt extends ct{instanceBuffer;quadBuffer;instanceCount=0;localSpriteMatrix=new Float32Array(6);worldSpriteMatrix=new Float32Array(6);KEY="Sprite";constructor(t){super(t),this.createBuffer(),this.createDefaultShader()}createBuffer(){const t=this.gl;this.quadBuffer=new J(t,M.Float32,t.ARRAY_BUFFER,t.STATIC_DRAW),this.quadBuffer.bindBuffer();for(const e of Rt)this.quadBuffer.pushFloat32(e);this.quadBuffer.makeDirty(),this.quadBuffer.bufferData(),this.instanceBuffer=new J(t,M.Uint32,t.ARRAY_BUFFER,t.DYNAMIC_DRAW)}createDefaultShader(){const t=this.rapid,e=
|
|
99
|
+
`,K=48,Rt=new Float32Array([0,0,1,0,0,1,1,1]);class rt extends ct{instanceBuffer;quadBuffer;instanceCount=0;localSpriteMatrix=new Float32Array(6);worldSpriteMatrix=new Float32Array(6);KEY="Sprite";constructor(t){super(t),this.createBuffer(),this.createDefaultShader()}createBuffer(){const t=this.gl;this.quadBuffer=new J(t,M.Float32,t.ARRAY_BUFFER,t.STATIC_DRAW),this.quadBuffer.bindBuffer();for(const e of Rt)this.quadBuffer.pushFloat32(e);this.quadBuffer.makeDirty(),this.quadBuffer.bufferData(),this.instanceBuffer=new J(t,M.Uint32,t.ARRAY_BUFFER,t.DYNAMIC_DRAW)}createDefaultShader(){const t=this.rapid,e=H(Tt,t.maxTextureUnits),r=H(et,t.maxTextureUnits);return this.vs=r,this.fs=e,this.defaultShader=this.createShader(r,e),this.defaultShader}createShader(t,e){const r=super.createShader(t,e);return r.use(),r.bindVAO(),this.instanceBuffer.bindBuffer(),r.setAttributes([{name:"aMatrixRow0",size:3,stride:K,offset:0,divisor:1},{name:"aMatrixRow1",size:3,stride:K,offset:12,divisor:1},{name:"aUVRect",size:4,stride:K,offset:24,divisor:1},{name:"aColor",size:4,type:at,normalized:!0,stride:K,offset:40,divisor:1},{name:"aTextureId",size:1,type:at,stride:K,offset:44,divisor:1}]),this.quadBuffer.bindBuffer(),r.setAttributes([{name:"aVertex",size:2,stride:8,offset:0,divisor:0}]),r.unbindVAO(),this.currentShader&&this.currentShader.use(),r}createCustomShader(t){const e=H(Tt,this.rapid.maxTextureUnits-t.usedTextureUnitNum),r=H(et,this.rapid.maxTextureUnits-t.usedTextureUnitNum);return t.getGLShader(this,this.KEY,r,e)}drawSprite(t,e,r=0,i=0,s=1,a=1,o=4294967295,h=0,c=0,u=!1,l=!1,d=!1){const f=e*6,x=this.matrixStore.data;this.drawSpriteAffine(t,x[f],x[f+1],x[f+2],x[f+3],x[f+4],x[f+5],r,i,s,a,o,h,c,u,l,d)}drawSpriteAffine(t,e,r,i,s,a,o,h=0,c=0,u=1,l=1,d=4294967295,f=0,x=0,v=!1,y=!1,T=!1){const w=t.base,b=t.rawWidth,N=t.rawHeight,_=t.offsetX,D=t.offsetY,k=this.useTexture(t.glTexture,f/w.width,x/w.height),L=this.instanceBuffer,S=L.usedElemNum;L.resize(12);const P=L.float32,V=L.uint32,Y=T?N:b,z=T?b:N,W=Y+2*f,B=z+2*x,U=_-f,I=D-x,R=this.localSpriteMatrix;T?(R[0]=0,R[1]=y?B:-B,R[2]=v?-W:W,R[3]=0,R[4]=U+(v?W:0),R[5]=I+(y?0:B)):(R[0]=v?-W:W,R[1]=0,R[2]=0,R[3]=y?-B:B,R[4]=U+(v?W:0),R[5]=I+(y?B:0));const A=this.worldSpriteMatrix;A[0]=e*R[0]+i*R[1],A[1]=r*R[0]+s*R[1],A[2]=e*R[2]+i*R[3],A[3]=r*R[2]+s*R[3],A[4]=a+e*R[4]+i*R[5],A[5]=o+r*R[4]+s*R[5],P[S+6]=h,P[S+7]=c,P[S+8]=u,P[S+9]=l,V[S+10]=d,V[S+11]=k,this.rapid.roundPixels&&(A[4]=Math.round(A[4]),A[5]=Math.round(A[5])),P[S]=A[0],P[S+1]=A[2],P[S+2]=A[4],P[S+3]=A[1],P[S+4]=A[3],P[S+5]=A[5],L.usedElemNum+=12,L.makeDirty(),this.instanceCount++}render(){if(this.instanceCount===0)return;this.prepareRender();const t=this.gl,e=this.currentShader;this.instanceBuffer.bindBuffer(),this.instanceBuffer.bufferData(),e.bindVAO(),yt(t,t.TRIANGLE_STRIP,0,4,this.instanceCount),this.rapid.drawcallCount++}resetRender(){super.resetRender(),this.instanceBuffer.clear(),this.instanceCount=0}hasPendingContent(){return this.instanceCount>0}}const te=`#version 300 es\r
|
|
100
100
|
precision highp float;\r
|
|
101
101
|
\r
|
|
102
102
|
in vec2 aPosition;\r
|
|
@@ -216,7 +216,7 @@ void main(void) {\r
|
|
|
216
216
|
discard;\r
|
|
217
217
|
}\r
|
|
218
218
|
}\r
|
|
219
|
-
`,q=20;class Et extends ct{vertexBuffer;vertexCount=0;matrixIndex=-1;drawMode=this.gl.TRIANGLES;texture;maskShader;KEY="Graphic";constructor(t){super(t),this.createBuffer(),this.createDefaultShader()}createBuffer(){const t=this.gl;this.vertexBuffer=new J(t,M.Float32,t.ARRAY_BUFFER,t.DYNAMIC_DRAW),this.maskShader=this.createMaskShader(re,ie)}createDefaultShader(){return this.vs=te,this.fs=ee,this.defaultShader=this.createShader(this.vs,this.fs),this.defaultShader}createMaskShader(t,e){const r=this.gl,i=new ht(r,t,e);return i.use(),i.bindVAO(),this.vertexBuffer.bindBuffer(),i.setAttributes([{name:"aPosition",size:2,type:r.FLOAT,stride:q-4,offset:0,divisor:0},{name:"aUV",size:2,type:r.FLOAT,stride:q-4,offset:8,divisor:0}]),i.unbindVAO(),this.currentShader&&this.currentShader.use(),i}createShader(t,e){const r=this.gl,i=super.createShader(t,e);return i.use(),i.bindVAO(),this.vertexBuffer.bindBuffer(),i.setAttributes([{name:"aPosition",size:2,type:r.FLOAT,stride:q,offset:0,divisor:0},{name:"aColor",size:4,type:r.UNSIGNED_BYTE,normalized:!0,stride:q,offset:8,divisor:0},{name:"aUV",size:2,type:r.FLOAT,stride:q,offset:12,divisor:0}]),i.unbindVAO(),this.currentShader&&this.currentShader.use(),i}startGraphic(t,e=this.gl.TRIANGLES,r){this.vertexBuffer.clear(),this.vertexCount=0,this.matrixIndex=t,this.drawMode=e,this.texture=r,this.texture?.glTexture&&this.useTexture(this.texture.glTexture,0,0)}addVertex(t,e,r=0,i=0,s=4294967295){const a=this.vertexBuffer;a.pushFloat32(t),a.pushFloat32(e),this.currentShader!=this.maskShader&&a.pushUint32(s),a.pushFloat32(r),a.pushFloat32(i),this.vertexCount++,this.vertexBuffer.makeDirty()}endGraphic(){this.flush()}render(){if(this.vertexCount===0)return;this.prepareRender();const t=this.gl,e=this.currentShader,r=this.matrixIndex*6,i=this.matrixStore.data;let s=i[r+4],a=i[r+5];this.rapid.roundPixels&&(s=Math.round(s),a=Math.round(a)),e.setUniform("uMatrixRow0",[i[r],i[r+2],s]),e.setUniform("uMatrixRow1",[i[r+1],i[r+3],a]),e.setUniform("uUVRect",this.texture?[this.texture.uvX,this.texture.uvY,this.texture.uvW,this.texture.uvH]:[0,0,1,1]),this.texture?.glTexture?(t.activeTexture(t.TEXTURE0),t.bindTexture(t.TEXTURE_2D,this.texture.glTexture),e.setUniform("uTexture",0),e.setUniform("uUseTexture",1)):e.setUniform("uUseTexture",0),this.vertexBuffer.bindBuffer(),this.vertexBuffer.bufferData(),e.bindVAO(),t.drawArrays(this.drawMode,0,this.vertexCount),this.rapid.drawcallCount++,this.texture=void 0}hasPendingContent(){return!1}}class p{x;y;static ZERO=new p(0,0);static ONE=new p(1,1);static UP=new p(0,1);static DOWN=new p(0,-1);static LEFT=new p(-1,0);static RIGHT=new p(1,0);constructor(t,e){this.x=t!==void 0?t:0,this.y=e!==void 0?e:0}set(t,e){return e===void 0?(this.x=t,this.y=t):(this.x=t,this.y=e),this}add(t){return new p(this.x+t.x,this.y+t.y)}subtract(t){return new p(this.x-t.x,this.y-t.y)}sub(t){return new p(this.x-t.x,this.y-t.y)}multiply(t){return t instanceof p?new p(this.x*t.x,this.y*t.y):new p(this.x*t,this.y*t)}mul(t){return this.multiply(t)}divide(t){return t instanceof p?new p(this.x/t.x,this.y/t.y):new p(this.x/t,this.y/t)}dot(t){return this.x*t.x+this.y*t.y}cross(t){return this.x*t.y-this.y*t.x}distanceTo(t){const e=this.x-t.x,r=this.y-t.y;return Math.sqrt(e*e+r*r)}clone(){return new p(this.x,this.y)}to(t){this.x=t.x,this.y=t.y}copy(){return new p(this.x,this.y)}equals(t){return t.x==this.x&&t.y==this.y}perpendicular(){const t=this.x;return this.x=-this.y,this.y=t,this}invert(){return this.x=-this.x,this.y=-this.y,this}inverted(){return new p(this.x*-1,this.y*-1)}length(){return Math.sqrt(this.x*this.x+this.y*this.y)}squaredLength(){return this.x*this.x+this.y*this.y}normalize(){const t=this.length();return this.x=this.x/t||0,this.y=this.y/t||0,this}normalized(){const t=this.length();return new p(this.x/t||0,this.y/t||0)}fix(t=1e-13){this.x=Math.abs(this.x)<t?0:this.x,this.y=Math.abs(this.y)<t?0:this.y}fixed(t=1e-13){return new p(Math.abs(this.x)<t?0:this.x,Math.abs(this.y)<t?0:this.y)}angle(){return Math.atan2(this.y,this.x)}abs(){return new p(Math.abs(this.x),Math.abs(this.y))}floor(){return new p(Math.floor(this.x),Math.floor(this.y))}ceil(){return new p(Math.ceil(this.x),Math.ceil(this.y))}stringify(){return`Vec2(${this.x}, ${this.y})`}clear(){this.x=0,this.y=0}isPrettyMuchZero(){return Math.abs(this.x)<1e-4&&Math.abs(this.y)<1e-4}isZero(){return this.x==0&&this.y==0}isNotZero(){return this.x!==0||this.y!==0}setToPolar(t,e=1){return this.x=Math.cos(t)*e,this.y=Math.sin(t)*e,this}static FromArray(t){return t.map(e=>new p(e[0],e[1]))}static fromAngle(t){return new p(Math.cos(t),Math.sin(t))}}const Mt=(n,t)=>{const[e,r,i,s,a,o]=t,h=new Float32Array([e,i,0,0,r,s,0,0,0,0,1,0,a,o,0,1]);return wt(h,n)},wt=(n,t)=>{const e=new Float32Array(16);for(let r=0;r<4;r++)for(let i=0;i<4;i++){let s=0;for(let a=0;a<4;a++)s+=n[i*4+a]*t[a*4+r];e[i*4+r]=s}return e};var St=(n=>(n[n.STRETCH=0]="STRETCH",n[n.REPEAT=1]="REPEAT",n))(St||{});const ut=10,At=(n,t,e,r,i,s)=>{const a=[],o=[],h=r?Math.atan2(t.y,t.x):Math.atan2(-t.y,-t.x),c=Math.PI,u=new p(t.y,-t.x);for(let l=0;l<ut;l++){const d=l/ut,f=(l+1)/ut,x=h+d*c,v=h+f*c,y=new p(Math.cos(x)*e,Math.sin(x)*e),T=new p(Math.cos(v)*e,Math.sin(v)*e);a.push(n),o.push(new p(i,.5)),a.push(n.add(y)),o.push(new p(i+y.dot(u)*s,.5-.5*y.dot(t)/e)),a.push(n.add(T)),o.push(new p(i+T.dot(u)*s,.5-.5*T.dot(t)/e))}return{vertices:a,uv:o}},se=(n,t=!1)=>{const e=[];if(n.length<2||t&&n.length<3)return{normals:e,length:0};const r=4,i=.001,s=n.length;let a=0;if(t)for(let h=0;h<s;h++){const c=n[h],u=n[(h+1)%s];a+=c.distanceTo(u)}else for(let h=0;h<s-1;h++)a+=n[h].distanceTo(n[h+1]);const o=(h,c,u)=>{const l=c.subtract(h).normalize(),d=c.subtract(u).normalize(),f=d.dot(l);if(f<-1+i)return{normal:l.perpendicular(),miters:1};{let x=d.add(l).normalize();l.cross(d)<0&&(x=x.multiply(-1));let v=1/Math.sqrt((1-f)/2);return{normal:x,miters:Math.min(v,r)}}};if(t)for(let h=0;h<s;h++){const c=h===0?n[s-1]:n[h-1],u=n[h],l=h===s-1?n[0]:n[h+1];e.push(o(c,u,l))}else for(let h=0;h<s;h++)if(h===0){const c=n[1].subtract(n[0]).normalize();e.push({normal:c.perpendicular(),miters:1})}else if(h===s-1){const c=n[h].subtract(n[h-1]).normalize();e.push({normal:c.perpendicular(),miters:1})}else e.push(o(n[h-1],n[h],n[h+1]));return{normals:e,length:a}},ne=n=>{const t=n.points;if(t.length<2)return{vertices:[],uv:[]};const{normals:e,length:r}=se(t,n.closed),i=(n.width||1)/2,s=[],a=[],o=n.roundCap||!1,h=n.textureMode||0;let c=0;const u=n.texture?.rawWidth||1,l=n.closed?t.length:t.length-1;for(let d=0;d<l;d++){const f=t[d],x=e[d].normal,v=e[d].miters,y=(d+1)%t.length,T=t[y],w=e[y].normal,b=e[y].miters,
|
|
219
|
+
`,q=20;class Et extends ct{vertexBuffer;vertexCount=0;matrixIndex=-1;drawMode=this.gl.TRIANGLES;texture;maskShader;KEY="Graphic";constructor(t){super(t),this.createBuffer(),this.createDefaultShader()}createBuffer(){const t=this.gl;this.vertexBuffer=new J(t,M.Float32,t.ARRAY_BUFFER,t.DYNAMIC_DRAW),this.maskShader=this.createMaskShader(re,ie)}createDefaultShader(){return this.vs=te,this.fs=ee,this.defaultShader=this.createShader(this.vs,this.fs),this.defaultShader}createMaskShader(t,e){const r=this.gl,i=new ht(r,t,e);return i.use(),i.bindVAO(),this.vertexBuffer.bindBuffer(),i.setAttributes([{name:"aPosition",size:2,type:r.FLOAT,stride:q-4,offset:0,divisor:0},{name:"aUV",size:2,type:r.FLOAT,stride:q-4,offset:8,divisor:0}]),i.unbindVAO(),this.currentShader&&this.currentShader.use(),i}createShader(t,e){const r=this.gl,i=super.createShader(t,e);return i.use(),i.bindVAO(),this.vertexBuffer.bindBuffer(),i.setAttributes([{name:"aPosition",size:2,type:r.FLOAT,stride:q,offset:0,divisor:0},{name:"aColor",size:4,type:r.UNSIGNED_BYTE,normalized:!0,stride:q,offset:8,divisor:0},{name:"aUV",size:2,type:r.FLOAT,stride:q,offset:12,divisor:0}]),i.unbindVAO(),this.currentShader&&this.currentShader.use(),i}startGraphic(t,e=this.gl.TRIANGLES,r){this.vertexBuffer.clear(),this.vertexCount=0,this.matrixIndex=t,this.drawMode=e,this.texture=r,this.texture?.glTexture&&this.useTexture(this.texture.glTexture,0,0)}addVertex(t,e,r=0,i=0,s=4294967295){const a=this.vertexBuffer;a.pushFloat32(t),a.pushFloat32(e),this.currentShader!=this.maskShader&&a.pushUint32(s),a.pushFloat32(r),a.pushFloat32(i),this.vertexCount++,this.vertexBuffer.makeDirty()}endGraphic(){this.flush()}render(){if(this.vertexCount===0)return;this.prepareRender();const t=this.gl,e=this.currentShader,r=this.matrixIndex*6,i=this.matrixStore.data;let s=i[r+4],a=i[r+5];this.rapid.roundPixels&&(s=Math.round(s),a=Math.round(a)),e.setUniform("uMatrixRow0",[i[r],i[r+2],s]),e.setUniform("uMatrixRow1",[i[r+1],i[r+3],a]),e.setUniform("uUVRect",this.texture?[this.texture.uvX,this.texture.uvY,this.texture.uvW,this.texture.uvH]:[0,0,1,1]),this.texture?.glTexture?(t.activeTexture(t.TEXTURE0),t.bindTexture(t.TEXTURE_2D,this.texture.glTexture),e.setUniform("uTexture",0),e.setUniform("uUseTexture",1)):e.setUniform("uUseTexture",0),this.vertexBuffer.bindBuffer(),this.vertexBuffer.bufferData(),e.bindVAO(),t.drawArrays(this.drawMode,0,this.vertexCount),this.rapid.drawcallCount++,this.texture=void 0}hasPendingContent(){return!1}}class p{x;y;static ZERO=new p(0,0);static ONE=new p(1,1);static UP=new p(0,1);static DOWN=new p(0,-1);static LEFT=new p(-1,0);static RIGHT=new p(1,0);constructor(t,e){this.x=t!==void 0?t:0,this.y=e!==void 0?e:0}set(t,e){return e===void 0?(this.x=t,this.y=t):(this.x=t,this.y=e),this}add(t){return new p(this.x+t.x,this.y+t.y)}subtract(t){return new p(this.x-t.x,this.y-t.y)}sub(t){return new p(this.x-t.x,this.y-t.y)}multiply(t){return t instanceof p?new p(this.x*t.x,this.y*t.y):new p(this.x*t,this.y*t)}mul(t){return this.multiply(t)}divide(t){return t instanceof p?new p(this.x/t.x,this.y/t.y):new p(this.x/t,this.y/t)}dot(t){return this.x*t.x+this.y*t.y}cross(t){return this.x*t.y-this.y*t.x}distanceTo(t){const e=this.x-t.x,r=this.y-t.y;return Math.sqrt(e*e+r*r)}clone(){return new p(this.x,this.y)}to(t){this.x=t.x,this.y=t.y}copy(){return new p(this.x,this.y)}equals(t){return t.x==this.x&&t.y==this.y}perpendicular(){const t=this.x;return this.x=-this.y,this.y=t,this}invert(){return this.x=-this.x,this.y=-this.y,this}inverted(){return new p(this.x*-1,this.y*-1)}length(){return Math.sqrt(this.x*this.x+this.y*this.y)}squaredLength(){return this.x*this.x+this.y*this.y}normalize(){const t=this.length();return this.x=this.x/t||0,this.y=this.y/t||0,this}normalized(){const t=this.length();return new p(this.x/t||0,this.y/t||0)}fix(t=1e-13){this.x=Math.abs(this.x)<t?0:this.x,this.y=Math.abs(this.y)<t?0:this.y}fixed(t=1e-13){return new p(Math.abs(this.x)<t?0:this.x,Math.abs(this.y)<t?0:this.y)}angle(){return Math.atan2(this.y,this.x)}abs(){return new p(Math.abs(this.x),Math.abs(this.y))}floor(){return new p(Math.floor(this.x),Math.floor(this.y))}ceil(){return new p(Math.ceil(this.x),Math.ceil(this.y))}stringify(){return`Vec2(${this.x}, ${this.y})`}clear(){this.x=0,this.y=0}isPrettyMuchZero(){return Math.abs(this.x)<1e-4&&Math.abs(this.y)<1e-4}isZero(){return this.x==0&&this.y==0}isNotZero(){return this.x!==0||this.y!==0}setToPolar(t,e=1){return this.x=Math.cos(t)*e,this.y=Math.sin(t)*e,this}static FromArray(t){return t.map(e=>new p(e[0],e[1]))}static fromAngle(t){return new p(Math.cos(t),Math.sin(t))}}const Mt=(n,t)=>{const[e,r,i,s,a,o]=t,h=new Float32Array([e,i,0,0,r,s,0,0,0,0,1,0,a,o,0,1]);return wt(h,n)},wt=(n,t)=>{const e=new Float32Array(16);for(let r=0;r<4;r++)for(let i=0;i<4;i++){let s=0;for(let a=0;a<4;a++)s+=n[i*4+a]*t[a*4+r];e[i*4+r]=s}return e};var St=(n=>(n[n.STRETCH=0]="STRETCH",n[n.REPEAT=1]="REPEAT",n))(St||{});const ut=10,At=(n,t,e,r,i,s)=>{const a=[],o=[],h=r?Math.atan2(t.y,t.x):Math.atan2(-t.y,-t.x),c=Math.PI,u=new p(t.y,-t.x);for(let l=0;l<ut;l++){const d=l/ut,f=(l+1)/ut,x=h+d*c,v=h+f*c,y=new p(Math.cos(x)*e,Math.sin(x)*e),T=new p(Math.cos(v)*e,Math.sin(v)*e);a.push(n),o.push(new p(i,.5)),a.push(n.add(y)),o.push(new p(i+y.dot(u)*s,.5-.5*y.dot(t)/e)),a.push(n.add(T)),o.push(new p(i+T.dot(u)*s,.5-.5*T.dot(t)/e))}return{vertices:a,uv:o}},se=(n,t=!1)=>{const e=[];if(n.length<2||t&&n.length<3)return{normals:e,length:0};const r=4,i=.001,s=n.length;let a=0;if(t)for(let h=0;h<s;h++){const c=n[h],u=n[(h+1)%s];a+=c.distanceTo(u)}else for(let h=0;h<s-1;h++)a+=n[h].distanceTo(n[h+1]);const o=(h,c,u)=>{const l=c.subtract(h).normalize(),d=c.subtract(u).normalize(),f=d.dot(l);if(f<-1+i)return{normal:l.perpendicular(),miters:1};{let x=d.add(l).normalize();l.cross(d)<0&&(x=x.multiply(-1));let v=1/Math.sqrt((1-f)/2);return{normal:x,miters:Math.min(v,r)}}};if(t)for(let h=0;h<s;h++){const c=h===0?n[s-1]:n[h-1],u=n[h],l=h===s-1?n[0]:n[h+1];e.push(o(c,u,l))}else for(let h=0;h<s;h++)if(h===0){const c=n[1].subtract(n[0]).normalize();e.push({normal:c.perpendicular(),miters:1})}else if(h===s-1){const c=n[h].subtract(n[h-1]).normalize();e.push({normal:c.perpendicular(),miters:1})}else e.push(o(n[h-1],n[h],n[h+1]));return{normals:e,length:a}},ne=n=>{const t=n.points;if(t.length<2)return{vertices:[],uv:[]};const{normals:e,length:r}=se(t,n.closed),i=(n.width||1)/2,s=[],a=[],o=n.roundCap||!1,h=n.textureMode||0;let c=0;const u=n.texture?.rawWidth||1,l=n.closed?t.length:t.length-1;for(let d=0;d<l;d++){const f=t[d],x=e[d].normal,v=e[d].miters,y=(d+1)%t.length,T=t[y],w=e[y].normal,b=e[y].miters,N=f.add(x.multiply(v*i)),_=f.subtract(x.multiply(v*i)),D=T.add(w.multiply(b*i)),k=T.subtract(w.multiply(b*i)),L=f.distanceTo(T);let S=0,P=0;h===0?(S=c/r,P=(c+L)/r):(S=c/u,P=S+L/u);const V=new p(S,0),Y=new p(S,1),z=new p(P,0),W=new p(P,1);s.push(N),a.push(V),s.push(_),a.push(Y),s.push(D),a.push(z),s.push(D),a.push(z),s.push(k),a.push(W),s.push(_),a.push(Y),c+=L}if(o&&!n.closed){const d=t[0],f=e[0].normal,x=h===0?r>0?1/r:0:1/u,v=At(d,f,i,!0,0,x);s.push(...v.vertices),a.push(...v.uv);const y=t[t.length-1],T=e[t.length-1].normal,w=h===0?1:c/u,b=h===0?r>0?1/r:0:1/u,N=At(y,T,i,!1,w,b);s.push(...N.vertices),a.push(...N.uv)}return{vertices:s,uv:a}},Q=(n,t,e,r)=>{if(t.customMatrix!==void 0)return t.customMatrix;if(t.modifyStack){n.matrixStack.applyTransform(t,e,r);return}else{const i=n.matrix.temporary;return n.matrixStack.applyTransform(t,e,r,i),i}},bt=(n,t)=>t?n?t.premultipliedUint32:t.uint32:4294967295,ae=2,Ut=(n,t)=>{const e=Q(n,t,t.texture.rawWidth,t.texture.rawHeight),r=t.texture;if(!r?.base||n.inCreateMask)return;const i=r.isAtlas?n.atlasSpriteRegion:n.spriteRegion;n.enterRegion(i,t.shader);let s=r.uvX,a=r.uvY,o=r.uvW,h=r.uvH;const c=!!t.flipX,u=!!t.flipY!=!!r.flipY;let l=t.padding??i.currentShader.padding;r.isAtlas&&(l+=ae);const d=s<=o?l:-l,f=a<=h?l:-l;i.drawSprite(r,e??t.customMatrix??n.matrixStack.curWorldM,s,a,o,h,n.getColorUint32(t.color),d,f,c,u,r.isRotated)},Ct=(n,t)=>{const e=n.particleRegion;n.enterRegion(e,t.shader);const r=t.texture,i=t.count??t.x.length,s=t.color;e.drawParticles(r,t.x,t.y,t.rotation,s,i,t.scaleX,t.scaleY,r.uvX,r.uvY,r.uvW,r.uvH,t.flipX,t.flipY,r.isRotated,t.originX,t.originY,n.premultipliedAlpha,t.reverseOrder,t.customMatrix)},lt=(n,t)=>{if(t.points.length===0)return;const e=Q(n,t,0,0);n.startGraphic(t.drawMode??n.gl.TRIANGLES,t.texture,t.shader,e??t.customMatrix);for(let r=0;r<t.points.length;r++){const i=t.points[r],s=t.uv?.[r],a=Array.isArray(t.color)?t.color[r]:t.color;n.addGraphicVertex(i.x,i.y,s?.x??0,s?.y??0,n.getColorUint32(a))}n.endGraphic()},_t=(n,t)=>{if(n.inCreateMask)return;const{vertices:e,uv:r}=ne(t);lt(n,{...t,points:e,uv:r,drawMode:n.gl.TRIANGLES})},Pt=(n,t)=>{const e=Q(n,t,t.texture.rawWidth,t.texture.rawHeight);n.startMaskGraphic(n.gl.TRIANGLE_FAN,t.texture,e??t.customMatrix),n.addRectVertex(t.texture.rawWidth,t.texture.rawHeight),n.endGraphic()},Nt=(n,t)=>{const e=Q(n,t,t.width,t.height);n.startGraphic(n.gl.TRIANGLE_FAN,t.texture,t.shader,e??t.customMatrix),n.addRectVertex(t.width,t.height,t.color),n.endGraphic()},Lt=(n,t)=>{const e=t.segments??32,r=Q(n,t,0,0);n.startGraphic(n.gl.TRIANGLE_FAN,void 0,t.shader,r??t.customMatrix);const i=n.getColorUint32(t.color);n.addGraphicVertex(0,0,.5,.5,i),n.addCircleVertex(t.radius,t.color,e),n.addGraphicVertex(t.radius,0,1,.5,i),n.endGraphic()},It=`#version 300 es\r
|
|
220
220
|
precision mediump float;\r
|
|
221
221
|
\r
|
|
222
222
|
uniform sampler2D uTextures[%TEXTURE_NUM%];\r
|
|
@@ -265,7 +265,7 @@ void main(void) {\r
|
|
|
265
265
|
\r
|
|
266
266
|
// CUSTOM_CODE_CALL\r
|
|
267
267
|
}\r
|
|
268
|
-
`;class he extends rt{KEY="AtlasSprite";constructor(t){super(t)}createCustomShader(t){const e=
|
|
268
|
+
`;class he extends rt{KEY="AtlasSprite";constructor(t){super(t)}createCustomShader(t){const e=H(It,this.rapid.maxTextureUnits-t.usedTextureUnitNum),r=H(et,this.rapid.maxTextureUnits-t.usedTextureUnitNum);return t.getGLShader(this,this.KEY,r,e)}createDefaultShader(){const t=this.rapid,e=H(It,t.maxTextureUnits),r=H(et,t.maxTextureUnits);return this.vs=r,this.fs=e,this.defaultShader=this.createShader(r,e),this.defaultShader}}const Ot=`#version 300 es\r
|
|
269
269
|
precision highp float;\r
|
|
270
270
|
\r
|
|
271
271
|
// per-vertex\r
|
|
@@ -322,4 +322,4 @@ void main(void) {\r
|
|
|
322
322
|
\r
|
|
323
323
|
// CUSTOM_CODE_CALL\r
|
|
324
324
|
}\r
|
|
325
|
-
`,z=48,Bt=z/4;class oe extends rt{KEY="ParticleSprite";texture;customMatrix;constructor(t){super(t)}createCustomShader(t){return t.getGLShader(this,this.KEY,Ot,Ft)}createDefaultShader(){const t=Ft,e=Ot;return this.vs=e,this.fs=t,this.defaultShader=this.createShader(e,t),this.defaultShader}enter(t){super.enter(t),this.currentShader.setUniform("u_projection",this.rapid.projection)}createShader(t,e){const r=super.createShader(t,e);return r.use(),r.bindVAO(),this.instanceBuffer.bindBuffer(),r.setAttributes([{name:"aPosition",size:2,stride:z,offset:0,divisor:1},{name:"aScale",size:2,stride:z,offset:8,divisor:1},{name:"aRotation",size:1,stride:z,offset:16,divisor:1},{name:"aUVRect",size:4,stride:z,offset:20,divisor:1},{name:"aColor",size:4,type:at,normalized:!0,stride:z,offset:36,divisor:1},{name:"aOrigin",size:2,stride:z,offset:40,divisor:1}]),this.quadBuffer.bindBuffer(),r.setAttributes([{name:"aVertex",size:2,stride:8,offset:0,divisor:0}]),r.unbindVAO(),this.currentShader&&this.currentShader.use(),r}drawParticles(t,e,r,i=0,s=4294967295,a,o=1,h=1,c=0,u=0,l=1,d=1,f=!1,x=!1,v=!1,y=.5,T=.5,w,b=!1,P){if(!t.glTexture||a<=0)return;let U=0;const F=typeof o=="number",$=typeof h=="number",B=typeof i=="number",S=typeof s=="number",N=!S&&typeof s[0]=="number";let H=t.rawWidth*(f?-1:1);F&&(H*=o);let V=t.rawHeight*(x?-1:1);$&&(V*=h);let Y=v?Math.PI/2:0;for(B&&(Y+=i);U<a;){this.texture&&t!==this.texture&&this.flush(),this.texture||(this.texture=t,this.useTexture(t.glTexture,0,0));const W=a-U,D=this.instanceBuffer;let C=D.usedElemNum;D.resize(W*Bt);const L=D.float32,R=D.uint32;for(let A=0;A<W;A++){const X=b?a-1-U-A:U+A;if(L[C]=e[X],L[C+1]=r[X],L[C+2]=F?H:H*o[X],L[C+3]=$?V:V*h[X],L[C+4]=B?Y:i[X]+Y,L[C+5]=c,L[C+6]=u,L[C+7]=l,L[C+8]=d,S)R[C+9]=4294967295;else if(N)R[C+9]=s[X];else{const zt=s[X];R[C+9]=w?zt.premultipliedUint32:zt.uint32}L[C+10]=y,L[C+11]=T,C+=Bt}D.usedElemNum=C,D.makeDirty(),this.instanceCount+=W,U+=W}this.customMatrix=P,this.flush()}render(){if(this.instanceCount===0)return;this.prepareRender();const t=this.gl;this.texture&&(t.activeTexture(t.TEXTURE0),t.bindTexture(t.TEXTURE_2D,this.texture.glTexture));const e=this.currentShader;this.instanceBuffer.bindBuffer(),this.instanceBuffer.bufferData(),e.bindVAO();const r=this.rapid.matrixStack,s=this.rapid.matrix.getMatrix(this.customMatrix??r.curWorldM),a=Mt(this.rapid.projection,s);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}}var Wt=(n=>(n[n.EQUAL=0]="EQUAL",n[n.NOT_EQUAL=1]="NOT_EQUAL",n))(Wt||{}),Dt=(n=>(n[n.NORMAL=0]="NORMAL",n[n.ADD=1]="ADD",n[n.MULTIPLY=2]="MULTIPLY",n[n.SCREEN=3]="SCREEN",n[n.ERASE=4]="ERASE",n))(Dt||{}),it=(n=>(n[n.LINEAR=0]="LINEAR",n[n.NEAREST=1]="NEAREST",n))(it||{}),kt=(n=>(n[n.CanvasItem=0]="CanvasItem",n[n.Viewport=1]="Viewport",n))(kt||{});class ce{gl;canvas;projection=new Float32Array(16);projectionDirty=!0;dpr;backgroundColor=m.Black;logicWidth=0;logicHeight=0;physicsWidth=0;physicsHeight=0;currentRegion=null;maxTextureUnits=0;matrixStack=new mt(this);matrix;spriteRegion;graphicRegion;atlasSpriteRegion;particleRegion;drawcallCount=0;inCreateMask=!1;texture;premultipliedAlpha;antialias;roundPixels;textureFilter;scaleMode;_filterRT=[null,null];_filterInput=new I;get height(){return this.logicHeight}get width(){return this.logicWidth}constructor(t){this.canvas=t.canvas,this.dpr=window.devicePixelRatio||1,this.antialias=t.antialias??!1,this.textureFilter=t.textureFilter??1,this.premultipliedAlpha=t.premultipliedAlpha??!0,this.roundPixels=t.roundPixels??!1,this.scaleMode=t.scaleMode??0;const e=jt(this.canvas,this.antialias,this.premultipliedAlpha);this.gl=e,this.maxTextureUnits=e.getParameter(e.MAX_TEXTURE_IMAGE_UNITS),this.matrix=this.matrixStack.matrix,this.spriteRegion=new rt(this),this.graphicRegion=new Et(this),this.atlasSpriteRegion=new he(this),this.particleRegion=new oe(this);const r=this.canvas.clientWidth||this.canvas.width,i=this.canvas.clientHeight||this.canvas.height;this.physicsWidth=t.physicsWidth||Math.round(r*this.dpr),this.physicsHeight=t.physicsHeight||Math.round(i*this.dpr),this.logicWidth=t.logicWidth||t.width||this.physicsWidth/this.dpr,this.logicHeight=t.logicHeight||t.height||this.physicsHeight/this.dpr,this.resize(this.logicWidth,this.logicHeight,this.physicsWidth,this.physicsHeight),this.texture=new xt(this),t.backgroundColor&&(this.backgroundColor=t.backgroundColor),e.enable(e.BLEND),this.premultipliedAlpha?e.blendFunc(e.ONE,e.ONE_MINUS_SRC_ALPHA):e.blendFunc(e.SRC_ALPHA,e.ONE_MINUS_SRC_ALPHA),e.enable(e.STENCIL_TEST),e.stencilFunc(e.ALWAYS,1,255),e.stencilOp(e.KEEP,e.KEEP,e.KEEP)}setTextureFilter(t){this.textureFilter=t}setAntialias(t){this.antialias=t}getColorUint32(t){return bt(this.premultipliedAlpha,t)}enterRegion(t,e){this.currentRegion===t&&this.currentRegion.isSameShader(e)||(this.flush(),this.currentRegion=t,t.enter(e))}drawParticles(t){Ct(this,t)}drawSprite(t){Ut(this,t)}drawLine(t){_t(this,t)}drawGraphic(t){lt(this,t)}drawRect(t){Nt(this,t)}drawCircle(t){Lt(this,t)}startGraphic(t=this.gl.TRIANGLES,e,r,i){this.enterRegion(this.graphicRegion,r),this.graphicRegion.startGraphic(i??this.matrixStack.curWorldM,t,e)}startMaskGraphic(t=this.gl.TRIANGLES,e,r){this.startGraphic(t,e,this.graphicRegion.maskShader,r)}drawMaskImage(t){Pt(this,t)}addRectVertex(t,e,r){const i=this.getColorUint32(r);this.addGraphicVertex(0,0,0,0,i),this.addGraphicVertex(t,0,1,0,i),this.addGraphicVertex(t,e,1,1,i),this.addGraphicVertex(0,e,0,1,i)}addCircleVertex(t,e,r=32){const i=Math.PI*2/r,s=this.getColorUint32(e);for(let a=0;a<r;a++){const o=i*a,h=Math.cos(o)*t,c=Math.sin(o)*t,u=.5+Math.cos(o)*.5,l=.5+Math.sin(o)*.5;this.addGraphicVertex(h,c,u,l,s)}}addGraphicVertex(t,e,r=0,i=0,s){this.graphicRegion.addVertex(t,e,r,i,typeof s=="number"?s:this.getColorUint32(s))}endGraphic(){this.graphicRegion.endGraphic()}resize(t,e,r,i){this.flush();const s=r??this.canvas.clientWidth??this.canvas.width,a=i??this.canvas.clientHeight??this.canvas.height;switch(r!==void 0&&(this.canvas.style.width=s+"px"),i!==void 0&&(this.canvas.style.height=a+"px"),this.physicsWidth=s*this.dpr,this.physicsHeight=a*this.dpr,this.logicWidth=t,this.logicHeight=e,this.scaleMode){case 1:this.canvas.width=t,this.canvas.height=e,this.canvas.style.imageRendering="pixelated",this.gl.viewport(0,0,t,e);break;case 0:this.canvas.width=this.physicsWidth,this.canvas.height=this.physicsHeight,this.canvas.style.imageRendering="auto",this.gl.viewport(0,0,this.physicsWidth,this.physicsHeight);break;default:throw new Error("scaleMode can only be CanvasScaleMode.Viewport or CanvasScaleMode.CanvasItem")}this.updateProjection(0,this.logicWidth,this.logicHeight,0)}updateProjection(t,e,r,i){this.updateOrthMatrix(this.projection,t,e,r,i),this.projectionDirty=!0}clear(){const t=this.gl;this.backgroundColor.setClearColor(t),t.clear(t.COLOR_BUFFER_BIT|t.STENCIL_BUFFER_BIT),this.drawcallCount=0,this.matrixStack.reset()}updateOrthMatrix(t,e,r,i,s){t[0]=2/(r-e),t[1]=0,t[2]=0,t[3]=0,t[4]=0,t[5]=2/(s-i),t[6]=0,t[7]=0,t[8]=0,t[9]=0,t[10]=-1,t[11]=0,t[12]=-(r+e)/(r-e),t[13]=-(s+i)/(s-i),t[14]=0,t[15]=1}flush(){this.currentRegion&&(this.currentRegion.exit(),this.currentRegion=null)}applyFilters(t,e){if(e.length===0)throw new Error("applyFilters: shaders array must not be empty.");const r=Math.max(...e.map(c=>c.padding)),i=r,s=t.rawWidth+r*2,a=t.rawHeight+r*2;for(let c=0;c<2;c++)this._filterRT[c]?this._filterRT[c].resize(s,a):this._filterRT[c]=this.texture.createRenderTexture({width:s,height:a});let o=t.clone(this._filterInput),h=0;this.matrixStack.save(),this.matrixStack.identity();for(let c=0;c<e.length;c++){const u=this._filterRT[h%2];u.offsetX=0,u.offsetY=0,this.enterRenderTexture(u),this.clearRenderTexture(),this.drawSprite({texture:o,shader:e[c],offsetX:c==0?i:0,offsetY:c==0?i:0,padding:r}),this.leaveRenderTexture(),o=u,h++}return this.matrixStack.restore(),o.offsetX=-i,o.offsetY=-i,o}enterRenderTexture(t){this.flush(),t.activate(),this.gl.viewport(0,0,t.rawWidth,t.rawHeight),this.updateProjection(0,t.rawWidth,t.rawHeight,0)}clearRenderTexture(t=new m(0,0,0,0)){this.flush();const e=this.gl;t.setClearColor(e),e.clear(e.COLOR_BUFFER_BIT)}leaveRenderTexture(){this.flush(),this.gl.bindFramebuffer(this.gl.FRAMEBUFFER,null),this.gl.viewport(0,0,this.physicsWidth,this.physicsHeight),this.updateProjection(0,this.logicWidth,this.logicHeight,0)}drawToRenderTexture(t,e,r=new m(0,0,0,0)){this.enterRenderTexture(t);try{r!==null&&this.clearRenderTexture(r),e()}finally{this.leaveRenderTexture()}}withMask(t,e,r=0,i=1){this.clearMask(),this.startDrawMask(i);try{t()}finally{this.endDrawMask()}this.enterMask(r,i);try{e()}finally{this.exitMask()}}withTransform(t,e,r=0,i=0){this.matrixStack.save();try{e&&this.matrixStack.applyTransform(e,r,i),t()}finally{this.matrixStack.restore()}}withScissor(t,e,r,i,s){this.startScissor(t,e,r,i);try{s()}finally{this.endScissor()}}withBlendMode(t,e){this.setBlendMode(t);try{e()}finally{this.setBlendMode(0)}}startDrawMask(t=1,e=255){this.flush();const r=this.gl;r.stencilMask(e),r.colorMask(!1,!1,!1,!1),r.stencilFunc(r.ALWAYS,t,e),r.stencilOp(r.KEEP,r.KEEP,r.REPLACE),this.enterRegion(this.graphicRegion,this.graphicRegion.maskShader),this.inCreateMask=!0}endDrawMask(){this.flush();const t=this.gl;t.colorMask(!0,!0,!0,!0),t.stencilMask(0),this.inCreateMask=!1}clearMask(t=255){this.flush();const e=this.gl;e.stencilMask(t),e.clear(e.STENCIL_BUFFER_BIT),this.inCreateMask=!1}enterMask(t,e=1,r=255){this.flush();const i=this.gl;i.colorMask(!0,!0,!0,!0),i.stencilMask(0),i.stencilFunc(t===0?i.EQUAL:i.NOTEQUAL,e,r),i.stencilOp(i.KEEP,i.KEEP,i.KEEP),this.inCreateMask=!1}exitMask(){this.flush();const t=this.gl;t.colorMask(!0,!0,!0,!0),t.stencilMask(255),t.stencilFunc(t.ALWAYS,1,255),t.stencilOp(t.KEEP,t.KEEP,t.KEEP),this.inCreateMask=!1}setBlendMode(t){this.flush();const e=this.gl;switch(t){case 0:this.premultipliedAlpha?e.blendFuncSeparate(e.ONE,e.ONE_MINUS_SRC_ALPHA,e.ONE,e.ONE_MINUS_SRC_ALPHA):e.blendFuncSeparate(e.SRC_ALPHA,e.ONE_MINUS_SRC_ALPHA,e.ONE,e.ONE_MINUS_SRC_ALPHA);break;case 1:e.blendFuncSeparate(e.ONE,e.ONE,e.ONE,e.ONE);break;case 2:e.blendFuncSeparate(e.DST_COLOR,e.ONE_MINUS_SRC_ALPHA,e.ONE,e.ONE_MINUS_SRC_ALPHA);break;case 3:e.blendFuncSeparate(e.ONE,e.ONE_MINUS_SRC_COLOR,e.ONE,e.ONE_MINUS_SRC_ALPHA);break;case 4:e.blendFuncSeparate(e.ZERO,e.ONE_MINUS_SRC_ALPHA,e.ZERO,e.ONE_MINUS_SRC_ALPHA);break}}startScissor(t,e,r,i){this.flush();const s=this.gl,a=this.physicsWidth/this.logicWidth,o=this.physicsHeight/this.logicHeight,h=Math.round(t*a),c=Math.round(this.physicsHeight-(e+i)*o),u=Math.round(r*a),l=Math.round(i*o);s.enable(s.SCISSOR_TEST),s.scissor(h,c,u,l)}endScissor(){this.flush(),this.gl.disable(this.gl.SCISSOR_TEST)}renderCamera(t){this.matrixStack.applyTransform(t);const e=this.matrixStack.curWorldM;this.matrix.invert(e)}logicToPhysics(t){return t.multiply(new p(this.physicsWidth/this.logicWidth,this.physicsHeight/this.logicHeight))}physicsToLogic(t){return t.multiply(new p(this.logicWidth/this.physicsWidth,this.logicHeight/this.physicsHeight))}cssToDevicePixel(t){return t.mul(this.dpr)}cssToLogic(t){const e=this.cssToDevicePixel(t);return this.physicsToLogic(e)}devicePixelToCss(t){return t.divide(this.dpr)}toCSSMatrix(t){const e=this.physicsWidth/this.dpr/this.logicWidth,r=this.physicsHeight/this.dpr/this.logicHeight;return this.matrix.toCSSMatrix(t??this.matrixStack.curWorldM,e,r)}}function ue(n){return!(n===null||typeof n!="object"||Array.isArray(n)||n instanceof p||n instanceof m)}class O{static float(t,e){return Math.random()*(e-t)+t}static int(t,e){return Math.floor(Math.random()*(e-t+1))+t}static angle(){return Math.random()*Math.PI*2}static vector(t,e,r,i){return new p(O.float(t,e),O.float(r,i))}static randomColor(t,e){return new m(O.float(t.r,e.r),O.float(t.g,e.g),O.float(t.b,e.b),O.float(t.a,e.a))}static pick(t){return t[O.int(0,t.length-1)]}static pickWeight(t){if(!t||t.length===0)return null;let e=0;for(const[,i]of t)e+=i;let r=Math.random()*e;for(const[i,s]of t)if(r-=s,r<=0)return i;return t[t.length-1][0]}static scalarOrRange(t,e){if(t==null)return e;if(Array.isArray(t)){const[r,i]=t;if(typeof r=="number")return O.float(r,i);if(r instanceof p)return O.vector(r.x,i.x,r.y,i.y);if(r instanceof m)return O.randomColor(r,i)}return typeof t=="number"?t:t.clone()}}var Gt=(n=>(n.POINT="point",n.CIRCLE="circle",n.RECT="rect",n))(Gt||{});const Ht=10,Vt=0,Yt=!0;class Z{components;constructor(t=1){if(!Number.isInteger(t)||t<=0)throw new RangeError("ParticleAttributeStore size must be a positive integer");this.components=Array.from({length:t},()=>({value:new _(M.Float32),delta:new _(M.Float32),damping:new _(M.Float32)}))}getArrayBuffer(){return this.components.flatMap(({value:t,delta:e,damping:r})=>[t,e,r])}update(t){for(const{value:e,delta:r,damping:i}of this.components)for(let s=0;s<e.usedElemNum;s++)e.typedArray[s]*=Math.pow(i.get(s),t),e.typedArray[s]+=r.get(s)*t}get(t,e=0){return this.components[e].value.get(t)}getComponent(t,e){if(typeof t=="number")return t;if(t instanceof p)return e===0?t.x:t.y;switch(e){case 0:return t.r;case 1:return t.g;case 2:return t.b;case 3:return t.a;default:return 0}}resolveComponent(t,e,r){return t===void 0?r:Array.isArray(t)?O.float(this.getComponent(t[0],e),this.getComponent(t[1],e)):this.getComponent(t,e)}create(t,e,r=1){const i=ue(t)?t:void 0,s=i===void 0?t:void 0,a=r>0?r:1;let o=0;for(let h=0;h<this.components.length;h++){const{value:c,delta:u,damping:l}=this.components[h],d=this.getComponent(e,h),f=s===void 0?this.resolveComponent(i?.start,h,d):this.getComponent(s,h),x=i?.end===void 0?f:this.resolveComponent(i.end,h,d);c.push(f),u.push(i?.delta===void 0?(x-f)/a:this.getComponent(i.delta,h)),l.push(i?.damping??1),h===0&&(o=f)}return o}}class dt{options;emitting=!1;emitTimer=0;emitRate=Ht;emitTime=Vt;emitTimeCounter=0;localSpace=Yt;rapid;x=new _(M.Float32);y=new _(M.Float32);scaleX=new _(M.Float32);scaleY=new _(M.Float32);rotation=new _(M.Float32);color=new _(M.Uint32);animations={speed:new Z,rotation:new Z,scale:new Z,color:new Z(4),velocity:new Z(2)};count=0;constructor(t,e){this.rapid=t,this.options=e,this.emitRate=e.emitRate??Ht,this.emitTime=e.emitTime??Vt,this.localSpace=e.localSpace??Yt}getAllArrayBuffer(){return[this.x,this.y,this.scaleX,this.scaleY,this.rotation,this.color,...Object.values(this.animations).flatMap(t=>t.getArrayBuffer())]}setTexture(t){this.options.texture=t}clone(){return new dt(this.rapid,{...this.options})}setEmitRate(t){this.emitRate=t}setEmitTime(t){this.emitTime=t}start(){this.emitting=!0,this.emitTimeCounter=0}stop(){this.emitting=!1}clear(){for(const t of this.getAllArrayBuffer())t.clear();this.count=0,this.emitTimeCounter=0}emit(t){const e=this.options.maxParticles??1/0,r=Math.min(t,e-this.count);for(let i=0;i<r;i++)this.createParticle()}createParticle(){const t=O.scalarOrRange(this.options.life,1);let e=0,r=0;switch(this.options.emitShape){case"circle":{const c=Math.random()*Math.PI*2,u=(this.options.emitRadius??0)*Math.sqrt(Math.random());e=Math.cos(c)*u,r=Math.sin(c)*u;break}case"rect":{e=(Math.random()-.5)*(this.options.emitRect?.width??0),r=(Math.random()-.5)*(this.options.emitRect?.height??0);break}}const i=this.rapid.matrixStack;if(this.localSpace)this.x.push(e),this.y.push(r);else{const{x:c,y:u}=i.localToWorld(e,r);this.x.push(c),this.y.push(u)}const s=this.options.animation,a=this.animations,o=a.scale.create(s.scale,1,t),h=a.rotation.create(s.rotation,0,t);a.speed.create(s.speed,0,t),a.velocity.create(s.velocity,p.ZERO,t),a.color.create(s.color,m.White,t),this.rotation.push(h),this.scaleX.push(o),this.scaleY.push(o),this.color.pushUint32(m.packColor(a.color.get(this.count,0),a.color.get(this.count,1),a.color.get(this.count,2),a.color.get(this.count,3),this.rapid.premultipliedAlpha)),this.count+=1}update(t){if(this.emitting&&this.emitRate>0)if(this.emitTime>0){if(this.emitTimeCounter+=t,this.emitTimeCounter>=this.emitTime){const r=Math.floor(this.emitTimeCounter/this.emitTime);this.emit(this.emitRate*r),this.emitTimeCounter-=r*this.emitTime}}else{this.emitTimer+=t;const r=Math.floor(this.emitTimer*this.emitRate);r>0&&(this.emit(r),this.emitTimer-=r/this.emitRate)}const e=this.updateParticle(t);this.count-=e.length,_.removeAtIndices(e,this.getAllArrayBuffer())}updateParticle(t){const e=[],r=this.animations;Object.values(r).forEach(i=>i.update(t));for(let i=0;i<this.count;i++){const s=r.rotation.get(i),a=r.scale.get(i),o=r.speed.get(i);this.rotation.typedArray[i]=s,this.scaleX.typedArray[i]=a,this.scaleY.typedArray[i]=a,this.color.uint32[i]=m.packColor(r.color.get(i,0),r.color.get(i,1),r.color.get(i,2),r.color.get(i,3),this.rapid.premultipliedAlpha),this.x.typedArray[i]+=(o*Math.cos(s)+r.velocity.get(i,0))*t,this.y.typedArray[i]+=(o*Math.sin(s)+r.velocity.get(i,1))*t}return e}render(){const t=this.options,e=this.localSpace?void 0:this.rapid.matrix.alloc();this.rapid.drawParticles({texture:t.texture,shader:t.shader,x:this.x.typedArray,y:this.y.typedArray,scaleX:this.scaleX.typedArray,scaleY:this.scaleY.typedArray,rotation:this.rotation.typedArray,color:this.color.typedArray,count:this.count,reverseOrder:!0,originX:t.origin?.x??.5,originY:t.origin?.y??.5,customMatrix:e})}isActive(){return this.emitting||this.count>0}oneShot(){this.emit(this.emitRate)}}g.ArrayType=M,g.BaseTexture=k,g.BlendMode=Dt,g.CanvasScaleMode=kt,g.Color=m,g.CustomGlShader=ot,g.DynamicArrayBuffer=_,g.GLShader=ht,g.GraphicRegion=Et,g.LineTextureMode=St,g.MaskType=Wt,g.MatrixStack=mt,g.MatrixStore=ft,g.ParticleEmitter=dt,g.ParticleShape=Gt,g.QUAD_VERTICES=Rt,g.Rapid=ce,g.Region=ct,g.RenderTexture=pt,g.SpriteRegion=rt,g.TextTexture=gt,g.Texture=I,g.TextureFilterMode=it,g.TextureManager=xt,g.TextureWrapMode=j,g.Vec2=p,g.WebglBufferArray=J,g.composeProjectionWithAffine=Mt,g.drawCircle=Lt,g.drawGraphic=lt,g.drawLine=_t,g.drawMaskImage=Pt,g.drawParticles=Ct,g.drawRect=Nt,g.drawSprite=Ut,g.getColorUint32=bt,g.multiplyMat4=wt,Object.defineProperty(g,Symbol.toStringTag,{value:"Module"})}));
|
|
325
|
+
`,X=48,Wt=X/4;class oe extends rt{KEY="ParticleSprite";texture;customMatrix;constructor(t){super(t)}createCustomShader(t){return t.getGLShader(this,this.KEY,Ot,Ft)}createDefaultShader(){const t=Ft,e=Ot;return this.vs=e,this.fs=t,this.defaultShader=this.createShader(e,t),this.defaultShader}enter(t){super.enter(t),this.currentShader.setUniform("u_projection",this.rapid.projection)}createShader(t,e){const r=super.createShader(t,e);return r.use(),r.bindVAO(),this.instanceBuffer.bindBuffer(),r.setAttributes([{name:"aPosition",size:2,stride:X,offset:0,divisor:1},{name:"aScale",size:2,stride:X,offset:8,divisor:1},{name:"aRotation",size:1,stride:X,offset:16,divisor:1},{name:"aUVRect",size:4,stride:X,offset:20,divisor:1},{name:"aColor",size:4,type:at,normalized:!0,stride:X,offset:36,divisor:1},{name:"aOrigin",size:2,stride:X,offset:40,divisor:1}]),this.quadBuffer.bindBuffer(),r.setAttributes([{name:"aVertex",size:2,stride:8,offset:0,divisor:0}]),r.unbindVAO(),this.currentShader&&this.currentShader.use(),r}drawParticles(t,e,r,i=0,s=4294967295,a,o=1,h=1,c=0,u=0,l=1,d=1,f=!1,x=!1,v=!1,y=.5,T=.5,w,b=!1,N){if(!t.glTexture||a<=0)return;let _=0;const D=typeof o=="number",k=typeof h=="number",L=typeof i=="number",S=typeof s=="number",P=!S&&typeof s[0]=="number";let V=t.rawWidth*(f?-1:1);D&&(V*=o);let Y=t.rawHeight*(x?-1:1);k&&(Y*=h);let z=v?Math.PI/2:0;for(L&&(z+=i);_<a;){this.texture&&t!==this.texture&&this.flush(),this.texture||(this.texture=t,this.useTexture(t.glTexture,0,0));const W=a-_,B=this.instanceBuffer;let U=B.usedElemNum;B.resize(W*Wt);const I=B.float32,R=B.uint32;for(let A=0;A<W;A++){const j=b?a-1-_-A:_+A;if(I[U]=e[j],I[U+1]=r[j],I[U+2]=D?V:V*o[j],I[U+3]=k?Y:Y*h[j],I[U+4]=L?z:i[j]+z,I[U+5]=c,I[U+6]=u,I[U+7]=l,I[U+8]=d,S)R[U+9]=4294967295;else if(P)R[U+9]=s[j];else{const zt=s[j];R[U+9]=w?zt.premultipliedUint32:zt.uint32}I[U+10]=y,I[U+11]=T,U+=Wt}B.usedElemNum=U,B.makeDirty(),this.instanceCount+=W,_+=W}this.customMatrix=N,this.flush()}render(){if(this.instanceCount===0)return;this.prepareRender();const t=this.gl;this.texture&&(t.activeTexture(t.TEXTURE0),t.bindTexture(t.TEXTURE_2D,this.texture.glTexture));const e=this.currentShader;this.instanceBuffer.bindBuffer(),this.instanceBuffer.bufferData(),e.bindVAO();const r=this.rapid.matrixStack,s=this.rapid.matrix.getMatrix(this.customMatrix??r.curWorldM),a=Mt(this.rapid.projection,s);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}}var Bt=(n=>(n[n.EQUAL=0]="EQUAL",n[n.NOT_EQUAL=1]="NOT_EQUAL",n))(Bt||{}),Dt=(n=>(n[n.NORMAL=0]="NORMAL",n[n.ADD=1]="ADD",n[n.MULTIPLY=2]="MULTIPLY",n[n.SCREEN=3]="SCREEN",n[n.ERASE=4]="ERASE",n))(Dt||{}),it=(n=>(n[n.LINEAR=0]="LINEAR",n[n.NEAREST=1]="NEAREST",n))(it||{}),kt=(n=>(n[n.CanvasItem=0]="CanvasItem",n[n.Viewport=1]="Viewport",n))(kt||{});class ce{gl;canvas;projection=new Float32Array(16);projectionDirty=!0;dpr;backgroundColor=m.Black;logicWidth=0;logicHeight=0;physicsWidth=0;physicsHeight=0;currentRegion=null;maxTextureUnits=0;matrixStack=new mt(this);matrix;spriteRegion;graphicRegion;atlasSpriteRegion;particleRegion;drawcallCount=0;inCreateMask=!1;texture;premultipliedAlpha;antialias;roundPixels;textureFilter;scaleMode;_filterRT=[null,null];_filterInput=new O;get height(){return this.logicHeight}get width(){return this.logicWidth}constructor(t){this.canvas=t.canvas,this.dpr=window.devicePixelRatio||1,this.antialias=t.antialias??!1,this.textureFilter=t.textureFilter??1,this.premultipliedAlpha=t.premultipliedAlpha??!0,this.roundPixels=t.roundPixels??!1,this.scaleMode=t.scaleMode??0;const e=jt(this.canvas,this.antialias,this.premultipliedAlpha);this.gl=e,this.maxTextureUnits=e.getParameter(e.MAX_TEXTURE_IMAGE_UNITS),this.matrix=this.matrixStack.matrix,this.spriteRegion=new rt(this),this.graphicRegion=new Et(this),this.atlasSpriteRegion=new he(this),this.particleRegion=new oe(this);const r=this.canvas.clientWidth||this.canvas.width,i=this.canvas.clientHeight||this.canvas.height;this.physicsWidth=t.physicsWidth||Math.round(r*this.dpr),this.physicsHeight=t.physicsHeight||Math.round(i*this.dpr),this.logicWidth=t.logicWidth||t.width||this.physicsWidth/this.dpr,this.logicHeight=t.logicHeight||t.height||this.physicsHeight/this.dpr,this.resize(this.logicWidth,this.logicHeight,this.physicsWidth,this.physicsHeight),this.texture=new xt(this),t.backgroundColor&&(this.backgroundColor=t.backgroundColor),e.enable(e.BLEND),this.premultipliedAlpha?e.blendFunc(e.ONE,e.ONE_MINUS_SRC_ALPHA):e.blendFunc(e.SRC_ALPHA,e.ONE_MINUS_SRC_ALPHA),e.enable(e.STENCIL_TEST),e.stencilFunc(e.ALWAYS,1,255),e.stencilOp(e.KEEP,e.KEEP,e.KEEP)}setTextureFilter(t){this.textureFilter=t}setAntialias(t){this.antialias=t}getColorUint32(t){return bt(this.premultipliedAlpha,t)}enterRegion(t,e){this.currentRegion===t&&this.currentRegion.isSameShader(e)||(this.flush(),this.currentRegion=t,t.enter(e))}drawParticles(t){Ct(this,t)}drawSprite(t){Ut(this,t)}drawLine(t){_t(this,t)}drawGraphic(t){lt(this,t)}drawRect(t){Nt(this,t)}drawCircle(t){Lt(this,t)}startGraphic(t=this.gl.TRIANGLES,e,r,i){this.enterRegion(this.graphicRegion,r),this.graphicRegion.startGraphic(i??this.matrixStack.curWorldM,t,e)}startMaskGraphic(t=this.gl.TRIANGLES,e,r){this.startGraphic(t,e,this.graphicRegion.maskShader,r)}drawMaskImage(t){Pt(this,t)}addRectVertex(t,e,r){const i=this.getColorUint32(r);this.addGraphicVertex(0,0,0,0,i),this.addGraphicVertex(t,0,1,0,i),this.addGraphicVertex(t,e,1,1,i),this.addGraphicVertex(0,e,0,1,i)}addCircleVertex(t,e,r=32){const i=Math.PI*2/r,s=this.getColorUint32(e);for(let a=0;a<r;a++){const o=i*a,h=Math.cos(o)*t,c=Math.sin(o)*t,u=.5+Math.cos(o)*.5,l=.5+Math.sin(o)*.5;this.addGraphicVertex(h,c,u,l,s)}}addGraphicVertex(t,e,r=0,i=0,s){this.graphicRegion.addVertex(t,e,r,i,typeof s=="number"?s:this.getColorUint32(s))}endGraphic(){this.graphicRegion.endGraphic()}resize(t,e,r,i){this.flush();const s=r??this.canvas.clientWidth??this.canvas.width,a=i??this.canvas.clientHeight??this.canvas.height;switch(r!==void 0&&(this.canvas.style.width=s+"px"),i!==void 0&&(this.canvas.style.height=a+"px"),this.physicsWidth=s*this.dpr,this.physicsHeight=a*this.dpr,this.logicWidth=t,this.logicHeight=e,this.scaleMode){case 1:this.canvas.width=t,this.canvas.height=e,this.canvas.style.imageRendering="pixelated",this.gl.viewport(0,0,t,e);break;case 0:this.canvas.width=this.physicsWidth,this.canvas.height=this.physicsHeight,this.canvas.style.imageRendering="auto",this.gl.viewport(0,0,this.physicsWidth,this.physicsHeight);break;default:throw new Error("scaleMode can only be CanvasScaleMode.Viewport or CanvasScaleMode.CanvasItem")}this.updateProjection(0,this.logicWidth,this.logicHeight,0)}updateProjection(t,e,r,i){this.updateOrthMatrix(this.projection,t,e,r,i),this.projectionDirty=!0}clear(){const t=this.gl;this.backgroundColor.setClearColor(t),t.clear(t.COLOR_BUFFER_BIT|t.STENCIL_BUFFER_BIT),this.drawcallCount=0,this.matrixStack.reset()}updateOrthMatrix(t,e,r,i,s){t[0]=2/(r-e),t[1]=0,t[2]=0,t[3]=0,t[4]=0,t[5]=2/(s-i),t[6]=0,t[7]=0,t[8]=0,t[9]=0,t[10]=-1,t[11]=0,t[12]=-(r+e)/(r-e),t[13]=-(s+i)/(s-i),t[14]=0,t[15]=1}flush(){this.currentRegion&&(this.currentRegion.exit(),this.currentRegion=null)}applyFilters(t,e){if(e.length===0)throw new Error("applyFilters: shaders array must not be empty.");const r=Math.max(...e.map(c=>c.padding)),i=r,s=t.rawWidth+r*2,a=t.rawHeight+r*2;for(let c=0;c<2;c++)this._filterRT[c]?this._filterRT[c].resize(s,a):this._filterRT[c]=this.texture.createRenderTexture({width:s,height:a});let o=t.clone(this._filterInput),h=0;this.matrixStack.save(),this.matrixStack.identity();for(let c=0;c<e.length;c++){const u=this._filterRT[h%2];u.offsetX=0,u.offsetY=0,this.enterRenderTexture(u),this.clearRenderTexture(),this.drawSprite({texture:o,shader:e[c],offsetX:c==0?i:0,offsetY:c==0?i:0,padding:r}),this.leaveRenderTexture(),o=u,h++}return this.matrixStack.restore(),o.offsetX=-i,o.offsetY=-i,o}enterRenderTexture(t){this.flush(),t.activate(),this.gl.viewport(0,0,t.rawWidth,t.rawHeight),this.updateProjection(0,t.rawWidth,t.rawHeight,0)}clearRenderTexture(t=new m(0,0,0,0)){this.flush();const e=this.gl;t.setClearColor(e),e.clear(e.COLOR_BUFFER_BIT)}leaveRenderTexture(){this.flush(),this.gl.bindFramebuffer(this.gl.FRAMEBUFFER,null),this.gl.viewport(0,0,this.physicsWidth,this.physicsHeight),this.updateProjection(0,this.logicWidth,this.logicHeight,0)}drawToRenderTexture(t,e,r=new m(0,0,0,0)){this.enterRenderTexture(t);try{r!==null&&this.clearRenderTexture(r),e()}finally{this.leaveRenderTexture()}}withMask(t,e,r=0,i=1){this.clearMask(),this.startDrawMask(i);try{t()}finally{this.endDrawMask()}this.enterMask(r,i);try{e()}finally{this.exitMask()}}withTransform(t,e,r=0,i=0){this.matrixStack.save();try{e&&this.matrixStack.applyTransform(e,r,i),t()}finally{this.matrixStack.restore()}}withScissor(t,e,r,i,s){this.startScissor(t,e,r,i);try{s()}finally{this.endScissor()}}withBlendMode(t,e){this.setBlendMode(t);try{e()}finally{this.setBlendMode(0)}}startDrawMask(t=1,e=255){this.flush();const r=this.gl;r.stencilMask(e),r.colorMask(!1,!1,!1,!1),r.stencilFunc(r.ALWAYS,t,e),r.stencilOp(r.KEEP,r.KEEP,r.REPLACE),this.enterRegion(this.graphicRegion,this.graphicRegion.maskShader),this.inCreateMask=!0}endDrawMask(){this.flush();const t=this.gl;t.colorMask(!0,!0,!0,!0),t.stencilMask(0),this.inCreateMask=!1}clearMask(t=255){this.flush();const e=this.gl;e.stencilMask(t),e.clear(e.STENCIL_BUFFER_BIT),this.inCreateMask=!1}enterMask(t,e=1,r=255){this.flush();const i=this.gl;i.colorMask(!0,!0,!0,!0),i.stencilMask(0),i.stencilFunc(t===0?i.EQUAL:i.NOTEQUAL,e,r),i.stencilOp(i.KEEP,i.KEEP,i.KEEP),this.inCreateMask=!1}exitMask(){this.flush();const t=this.gl;t.colorMask(!0,!0,!0,!0),t.stencilMask(255),t.stencilFunc(t.ALWAYS,1,255),t.stencilOp(t.KEEP,t.KEEP,t.KEEP),this.inCreateMask=!1}setBlendMode(t){this.flush();const e=this.gl;switch(t){case 0:this.premultipliedAlpha?e.blendFuncSeparate(e.ONE,e.ONE_MINUS_SRC_ALPHA,e.ONE,e.ONE_MINUS_SRC_ALPHA):e.blendFuncSeparate(e.SRC_ALPHA,e.ONE_MINUS_SRC_ALPHA,e.ONE,e.ONE_MINUS_SRC_ALPHA);break;case 1:e.blendFuncSeparate(e.ONE,e.ONE,e.ONE,e.ONE);break;case 2:e.blendFuncSeparate(e.DST_COLOR,e.ONE_MINUS_SRC_ALPHA,e.ONE,e.ONE_MINUS_SRC_ALPHA);break;case 3:e.blendFuncSeparate(e.ONE,e.ONE_MINUS_SRC_COLOR,e.ONE,e.ONE_MINUS_SRC_ALPHA);break;case 4:e.blendFuncSeparate(e.ZERO,e.ONE_MINUS_SRC_ALPHA,e.ZERO,e.ONE_MINUS_SRC_ALPHA);break}}startScissor(t,e,r,i){this.flush();const s=this.gl,a=this.physicsWidth/this.logicWidth,o=this.physicsHeight/this.logicHeight,h=Math.round(t*a),c=Math.round(this.physicsHeight-(e+i)*o),u=Math.round(r*a),l=Math.round(i*o);s.enable(s.SCISSOR_TEST),s.scissor(h,c,u,l)}endScissor(){this.flush(),this.gl.disable(this.gl.SCISSOR_TEST)}renderCamera(t){this.matrixStack.applyTransform(t);const e=this.matrixStack.curWorldM;this.matrix.invert(e)}logicToPhysics(t){return t.multiply(new p(this.physicsWidth/this.logicWidth,this.physicsHeight/this.logicHeight))}physicsToLogic(t){return t.multiply(new p(this.logicWidth/this.physicsWidth,this.logicHeight/this.physicsHeight))}cssToDevicePixel(t){return t.mul(this.dpr)}cssToLogic(t){const e=this.cssToDevicePixel(t);return this.physicsToLogic(e)}devicePixelToCss(t){return t.divide(this.dpr)}toCSSMatrix(t){const e=this.physicsWidth/this.dpr/this.logicWidth,r=this.physicsHeight/this.dpr/this.logicHeight;return this.matrix.toCSSMatrix(t??this.matrixStack.curWorldM,e,r)}}function ue(n){return!(n===null||typeof n!="object"||Array.isArray(n)||n instanceof p||n instanceof m)}class F{static float(t,e){return Math.random()*(e-t)+t}static int(t,e){return Math.floor(Math.random()*(e-t+1))+t}static angle(){return Math.random()*Math.PI*2}static vector(t,e,r,i){return new p(F.float(t,e),F.float(r,i))}static randomColor(t,e){return new m(F.float(t.r,e.r),F.float(t.g,e.g),F.float(t.b,e.b),F.float(t.a,e.a))}static pick(t){return t[F.int(0,t.length-1)]}static pickWeight(t){if(!t||t.length===0)return null;let e=0;for(const[,i]of t)e+=i;let r=Math.random()*e;for(const[i,s]of t)if(r-=s,r<=0)return i;return t[t.length-1][0]}static scalarOrRange(t,e){if(t==null)return e;if(Array.isArray(t)){const[r,i]=t;if(typeof r=="number")return F.float(r,i);if(r instanceof p)return F.vector(r.x,i.x,r.y,i.y);if(r instanceof m)return F.randomColor(r,i)}return typeof t=="number"?t:t.clone()}}var Gt=(n=>(n.POINT="point",n.CIRCLE="circle",n.RECT="rect",n))(Gt||{});const Ht=10,Vt=0,Yt=!0;class Z{components;constructor(t=1){if(!Number.isInteger(t)||t<=0)throw new RangeError("ParticleAttributeStore size must be a positive integer");this.components=Array.from({length:t},()=>({value:new C(M.Float32),delta:new C(M.Float32),damping:new C(M.Float32)}))}getArrayBuffer(){return this.components.flatMap(({value:t,delta:e,damping:r})=>[t,e,r])}update(t){for(const{value:e,delta:r,damping:i}of this.components)for(let s=0;s<e.usedElemNum;s++)e.typedArray[s]*=Math.pow(i.get(s),t),e.typedArray[s]+=r.get(s)*t}get(t,e=0){return this.components[e].value.get(t)}getComponent(t,e){if(typeof t=="number")return t;if(t instanceof p)return e===0?t.x:t.y;switch(e){case 0:return t.r;case 1:return t.g;case 2:return t.b;case 3:return t.a;default:return 0}}resolveComponent(t,e,r){return t===void 0?r:Array.isArray(t)?F.float(this.getComponent(t[0],e),this.getComponent(t[1],e)):this.getComponent(t,e)}create(t,e,r=1){const i=ue(t)?t:void 0,s=i===void 0?t:void 0,a=r>0?r:1;let o=0;for(let h=0;h<this.components.length;h++){const{value:c,delta:u,damping:l}=this.components[h],d=this.getComponent(e,h),f=s===void 0?this.resolveComponent(i?.start,h,d):this.getComponent(s,h),x=i?.end===void 0?f:this.resolveComponent(i.end,h,d);c.push(f),u.push(i?.delta===void 0?(x-f)/a:this.getComponent(i.delta,h)),l.push(i?.damping??1),h===0&&(o=f)}return o}}class dt{options;emitting=!1;emitTimer=0;emitRate=Ht;emitTime=Vt;emitTimeCounter=0;localSpace=Yt;rapid;x=new C(M.Float32);y=new C(M.Float32);scaleX=new C(M.Float32);scaleY=new C(M.Float32);rotation=new C(M.Float32);color=new C(M.Uint32);animations={speed:new Z,rotation:new Z,scale:new Z,color:new Z(4),velocity:new Z(2)};count=0;constructor(t,e){this.rapid=t,this.options=e,this.emitRate=e.emitRate??Ht,this.emitTime=e.emitTime??Vt,this.localSpace=e.localSpace??Yt}getAllArrayBuffer(){return[this.x,this.y,this.scaleX,this.scaleY,this.rotation,this.color,...Object.values(this.animations).flatMap(t=>t.getArrayBuffer())]}setTexture(t){this.options.texture=t}clone(){return new dt(this.rapid,{...this.options})}setEmitRate(t){this.emitRate=t}setEmitTime(t){this.emitTime=t}start(){this.emitting=!0,this.emitTimeCounter=0}stop(){this.emitting=!1}clear(){for(const t of this.getAllArrayBuffer())t.clear();this.count=0,this.emitTimeCounter=0}emit(t){const e=this.options.maxParticles??1/0,r=Math.min(t,e-this.count);for(let i=0;i<r;i++)this.createParticle()}createParticle(){const t=F.scalarOrRange(this.options.life,1);let e=0,r=0;switch(this.options.emitShape){case"circle":{const c=Math.random()*Math.PI*2,u=(this.options.emitRadius??0)*Math.sqrt(Math.random());e=Math.cos(c)*u,r=Math.sin(c)*u;break}case"rect":{e=(Math.random()-.5)*(this.options.emitRect?.width??0),r=(Math.random()-.5)*(this.options.emitRect?.height??0);break}}const i=this.rapid.matrixStack;if(this.localSpace)this.x.push(e),this.y.push(r);else{const{x:c,y:u}=i.localToWorld(e,r);this.x.push(c),this.y.push(u)}const s=this.options.animation,a=this.animations,o=a.scale.create(s.scale,1,t),h=a.rotation.create(s.rotation,0,t);a.speed.create(s.speed,0,t),a.velocity.create(s.velocity,p.ZERO,t),a.color.create(s.color,m.White,t),this.rotation.push(h),this.scaleX.push(o),this.scaleY.push(o),this.color.pushUint32(m.packColor(a.color.get(this.count,0),a.color.get(this.count,1),a.color.get(this.count,2),a.color.get(this.count,3),this.rapid.premultipliedAlpha)),this.count+=1}update(t){if(this.emitting&&this.emitRate>0)if(this.emitTime>0){if(this.emitTimeCounter+=t,this.emitTimeCounter>=this.emitTime){const r=Math.floor(this.emitTimeCounter/this.emitTime);this.emit(this.emitRate*r),this.emitTimeCounter-=r*this.emitTime}}else{this.emitTimer+=t;const r=Math.floor(this.emitTimer*this.emitRate);r>0&&(this.emit(r),this.emitTimer-=r/this.emitRate)}const e=this.updateParticle(t);this.count-=e.length,C.removeAtIndices(e,this.getAllArrayBuffer())}updateParticle(t){const e=[],r=this.animations;Object.values(r).forEach(i=>i.update(t));for(let i=0;i<this.count;i++){const s=r.rotation.get(i),a=r.scale.get(i),o=r.speed.get(i);this.rotation.typedArray[i]=s,this.scaleX.typedArray[i]=a,this.scaleY.typedArray[i]=a,this.color.uint32[i]=m.packColor(r.color.get(i,0),r.color.get(i,1),r.color.get(i,2),r.color.get(i,3),this.rapid.premultipliedAlpha),this.x.typedArray[i]+=(o*Math.cos(s)+r.velocity.get(i,0))*t,this.y.typedArray[i]+=(o*Math.sin(s)+r.velocity.get(i,1))*t}return e}render(){const t=this.options,e=this.localSpace?void 0:this.rapid.matrix.alloc();this.rapid.drawParticles({texture:t.texture,shader:t.shader,x:this.x.typedArray,y:this.y.typedArray,scaleX:this.scaleX.typedArray,scaleY:this.scaleY.typedArray,rotation:this.rotation.typedArray,color:this.color.typedArray,count:this.count,reverseOrder:!0,originX:t.origin?.x??.5,originY:t.origin?.y??.5,customMatrix:e})}isActive(){return this.emitting||this.count>0}oneShot(){this.emit(this.emitRate)}}g.ArrayType=M,g.BaseTexture=G,g.BlendMode=Dt,g.CanvasScaleMode=kt,g.Color=m,g.CustomGlShader=ot,g.DynamicArrayBuffer=C,g.GLShader=ht,g.GraphicRegion=Et,g.LineTextureMode=St,g.MaskType=Bt,g.MatrixStack=mt,g.MatrixStore=ft,g.ParticleEmitter=dt,g.ParticleShape=Gt,g.QUAD_VERTICES=Rt,g.Rapid=ce,g.Region=ct,g.RenderTexture=pt,g.SpriteRegion=rt,g.TextTexture=gt,g.Texture=O,g.TextureFilterMode=it,g.TextureManager=xt,g.TextureWrapMode=$,g.Vec2=p,g.WebglBufferArray=J,g.composeProjectionWithAffine=Mt,g.drawCircle=Lt,g.drawGraphic=lt,g.drawLine=_t,g.drawMaskImage=Pt,g.drawParticles=Ct,g.drawRect=Nt,g.drawSprite=Ut,g.getColorUint32=bt,g.multiplyMat4=wt,Object.defineProperty(g,Symbol.toStringTag,{value:"Module"})}));
|
package/dist/webgl/glshader.d.ts
CHANGED