@holtjs/stage 0.5.1 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,312 @@
1
- import { _ as rgbToCss, a as EventEmitter, h as packColorAbgr, m as createMatrix2D, o as FilterMode, p as WHITE_RGB, r as BlendModes, s as HEX_MASK, u as TintModes } from "./types-DRFB0Klc.js";
1
+ import { _ as rgbToCss, a as EventEmitter, g as parseColor, h as packColorAbgr, m as createMatrix2D, o as FilterMode, p as WHITE_RGB, r as BlendModes, s as HEX_MASK, u as TintModes } from "./types-CXsCvp1p.js";
2
+ // src/camera.ts
3
+ var Camera = class {
4
+ x = 0;
5
+ y = 0;
6
+ width;
7
+ height;
8
+ scrollX = 0;
9
+ scrollY = 0;
10
+ zoom = 1;
11
+ /** Active device-pixel ratio applied via `configureHiDpi()` (`1` when unconfigured). */
12
+ dpr = 1;
13
+ alpha = 1;
14
+ originX = .5;
15
+ originY = .5;
16
+ roundPixels = false;
17
+ useBounds = false;
18
+ boundsX = 0;
19
+ boundsY = 0;
20
+ boundsWidth = 0;
21
+ boundsHeight = 0;
22
+ backgroundColor;
23
+ transparent;
24
+ worldView = {
25
+ x: 0,
26
+ y: 0,
27
+ width: 0,
28
+ height: 0,
29
+ right: 0,
30
+ bottom: 0
31
+ };
32
+ matrixCombined = createMatrix2D();
33
+ followTarget = null;
34
+ followLerpX = 1;
35
+ followLerpY = 1;
36
+ followOffsetX = 0;
37
+ followOffsetY = 0;
38
+ deadzoneW = 0;
39
+ deadzoneH = 0;
40
+ constructor(width, height, transparent = false, backgroundColor) {
41
+ this.width = Math.max(1, width);
42
+ this.height = Math.max(1, height);
43
+ this.transparent = transparent;
44
+ this.backgroundColor = backgroundColor !== void 0 ? parseColor(backgroundColor, transparent ? 0 : 1) : {
45
+ r: 0,
46
+ g: 0,
47
+ b: 0,
48
+ a: transparent ? 0 : 1
49
+ };
50
+ this.syncMetrics();
51
+ }
52
+ get displayWidth() {
53
+ return this.width / (this.zoom || 1);
54
+ }
55
+ get displayHeight() {
56
+ return this.height / (this.zoom || 1);
57
+ }
58
+ setBackgroundColor(color) {
59
+ this.backgroundColor = parseColor(color, 1);
60
+ this.transparent = this.backgroundColor.a <= 0;
61
+ return this;
62
+ }
63
+ setRoundPixels(v) {
64
+ this.roundPixels = !!v;
65
+ return this;
66
+ }
67
+ setOrigin(x = .5, y = x) {
68
+ this.originX = x;
69
+ this.originY = y;
70
+ this.syncMetrics();
71
+ return this;
72
+ }
73
+ setZoom(z) {
74
+ this.zoom = z > 0 ? z : 1;
75
+ if (this.useBounds) {
76
+ this.scrollX = this.clampX(this.scrollX);
77
+ this.scrollY = this.clampY(this.scrollY);
78
+ }
79
+ this.syncMetrics();
80
+ return this;
81
+ }
82
+ setScroll(x, y) {
83
+ this.scrollX = this.useBounds ? this.clampX(x) : x;
84
+ this.scrollY = this.useBounds ? this.clampY(y) : y;
85
+ this.syncMetrics();
86
+ return this;
87
+ }
88
+ setViewport(x, y, width, height) {
89
+ this.x = x;
90
+ this.y = y;
91
+ this.width = Math.max(1, width);
92
+ this.height = Math.max(1, height);
93
+ if (this.useBounds) {
94
+ this.scrollX = this.clampX(this.scrollX);
95
+ this.scrollY = this.clampY(this.scrollY);
96
+ }
97
+ this.syncMetrics();
98
+ return this;
99
+ }
100
+ setSize(width, height) {
101
+ this.width = Math.max(1, width);
102
+ this.height = Math.max(1, height);
103
+ if (this.useBounds) {
104
+ this.scrollX = this.clampX(this.scrollX);
105
+ this.scrollY = this.clampY(this.scrollY);
106
+ }
107
+ this.syncMetrics();
108
+ return this;
109
+ }
110
+ setAlpha(a) {
111
+ this.alpha = Math.max(0, Math.min(1, a));
112
+ return this;
113
+ }
114
+ setBounds(x, y, width, height) {
115
+ this.useBounds = true;
116
+ this.boundsX = x;
117
+ this.boundsY = y;
118
+ this.boundsWidth = width;
119
+ this.boundsHeight = height;
120
+ this.scrollX = this.clampX(this.scrollX);
121
+ this.scrollY = this.clampY(this.scrollY);
122
+ this.syncMetrics();
123
+ return this;
124
+ }
125
+ /** Disable world-bounds scroll clamping (`removeBounds()` alias included for Phaser familiarity). */
126
+ clearBounds() {
127
+ this.useBounds = false;
128
+ return this;
129
+ }
130
+ removeBounds() {
131
+ return this.clearBounds();
132
+ }
133
+ clampX(sx) {
134
+ if (!this.useBounds || this.boundsWidth <= 0) return sx;
135
+ const z = this.zoom || 1;
136
+ const ox = this.width * this.originX;
137
+ const dw = this.width / z;
138
+ if (dw >= this.boundsWidth) return this.boundsX + this.boundsWidth * .5 - dw * .5 - ox + ox / z;
139
+ const minSx = this.boundsX - ox + ox / z;
140
+ const maxSx = this.boundsX + this.boundsWidth - dw - ox + ox / z;
141
+ return sx < minSx ? minSx : sx > maxSx ? maxSx : sx;
142
+ }
143
+ clampY(sy) {
144
+ if (!this.useBounds || this.boundsHeight <= 0) return sy;
145
+ const z = this.zoom || 1;
146
+ const oy = this.height * this.originY;
147
+ const dh = this.height / z;
148
+ if (dh >= this.boundsHeight) return this.boundsY + this.boundsHeight * .5 - dh * .5 - oy + oy / z;
149
+ const minSy = this.boundsY - oy + oy / z;
150
+ const maxSy = this.boundsY + this.boundsHeight - dh - oy + oy / z;
151
+ return sy < minSy ? minSy : sy > maxSy ? maxSy : sy;
152
+ }
153
+ centerOn(x, y) {
154
+ const z = this.zoom || 1;
155
+ const ox = this.width * this.originX;
156
+ const oy = this.height * this.originY;
157
+ const dw = this.width / z;
158
+ const dh = this.height / z;
159
+ return this.setScroll(x - dw * .5 - ox + ox / z, y - dh * .5 - oy + oy / z);
160
+ }
161
+ /**
162
+ * One-line HiDPI camera + backing-buffer configurator: maps logical `(0, 0)..(viewportW, viewportH)`
163
+ * 1:1 onto crisp physical `viewportW * dpr` pixels without copy-pasting origin/scroll math.
164
+ * Pass `{ fit: true }` (or configure `scale: { mode: Scale.FIT }`) for automatic aspect-preserving CSS letterboxing.
165
+ * Returns the clamped `dpr` applied.
166
+ */
167
+ configureHiDpi(scale, viewportWidth, viewportHeight, dpr = typeof window !== "undefined" ? window.devicePixelRatio || 1 : 1, maxDpr = 2, options) {
168
+ const safeDpr = Math.min(Math.max(1, maxDpr), Math.max(1, Number.isFinite(dpr) ? dpr : 1));
169
+ const vw = Math.max(1, Math.round(viewportWidth));
170
+ const vh = Math.max(1, Math.round(viewportHeight));
171
+ this.dpr = safeDpr;
172
+ scale.logicalWidth = vw;
173
+ scale.logicalHeight = vh;
174
+ scale.zoom = 1 / safeDpr;
175
+ const hadBounds = this.useBounds;
176
+ this.useBounds = false;
177
+ scale.resize(Math.round(vw * safeDpr), Math.round(vh * safeDpr));
178
+ if ((options?.fit || scale.mode === 1) && typeof scale.fitToContainer === "function") scale.fitToContainer(options?.containerWidth, options?.containerHeight);
179
+ else if (scale.canvas?.style) {
180
+ scale.canvas.style.width = `${vw}px`;
181
+ scale.canvas.style.height = `${vh}px`;
182
+ }
183
+ const ox = this.width * this.originX;
184
+ const oy = this.height * this.originY;
185
+ this.zoom = safeDpr;
186
+ this.useBounds = hadBounds;
187
+ this.setScroll(ox / safeDpr - ox, oy / safeDpr - oy);
188
+ return safeDpr;
189
+ }
190
+ /** Convert canvas/backing-buffer coordinates `(screenX, screenY)` into world coordinates. */
191
+ screenToWorld(screenX, screenY, out = {
192
+ x: 0,
193
+ y: 0
194
+ }) {
195
+ const m = this.matrixCombined;
196
+ out.x = (screenX - m.tx) / (m.a || 1);
197
+ out.y = (screenY - m.ty) / (m.d || 1);
198
+ return out;
199
+ }
200
+ /** Convert world coordinates `(worldX, worldY)` into canvas/backing-buffer coordinates. */
201
+ worldToScreen(worldX, worldY, out = {
202
+ x: 0,
203
+ y: 0
204
+ }) {
205
+ const m = this.matrixCombined;
206
+ out.x = worldX * m.a + m.tx;
207
+ out.y = worldY * m.d + m.ty;
208
+ return out;
209
+ }
210
+ /** Convert logical/CSS viewport coordinates `(0..viewportW, 0..viewportH)` into world coordinates (HiDPI-aware). */
211
+ logicalToWorld(logicalX, logicalY, out = {
212
+ x: 0,
213
+ y: 0
214
+ }) {
215
+ return this.screenToWorld(logicalX * this.dpr, logicalY * this.dpr, out);
216
+ }
217
+ /** Convert world coordinates into logical/CSS viewport coordinates `(0..viewportW, 0..viewportH)` (HiDPI-aware). */
218
+ worldToLogical(worldX, worldY, out = {
219
+ x: 0,
220
+ y: 0
221
+ }) {
222
+ this.worldToScreen(worldX, worldY, out);
223
+ const inv = 1 / (this.dpr || 1);
224
+ out.x *= inv;
225
+ out.y *= inv;
226
+ return out;
227
+ }
228
+ /** Convert raw DOM `MouseEvent`/`PointerEvent` `(clientX, clientY)` directly into world coordinates across HiDPI & `Scale.FIT`. */
229
+ clientToWorld(clientX, clientY, canvas, out = {
230
+ x: 0,
231
+ y: 0
232
+ }) {
233
+ const rect = canvas.getBoundingClientRect();
234
+ const sx = (clientX - rect.left) * canvas.width / (rect.width || 1);
235
+ const sy = (clientY - rect.top) * canvas.height / (rect.height || 1);
236
+ return this.screenToWorld(sx, sy, out);
237
+ }
238
+ /** Lock or smoothly follow any `{ x, y }` actor/point (frame-rate independent lerp + optional deadzone & bounds). */
239
+ startFollow(target, options = {}) {
240
+ this.followTarget = target;
241
+ this.followLerpX = Math.max(.001, Math.min(1, options.lerpX ?? 1));
242
+ this.followLerpY = Math.max(.001, Math.min(1, options.lerpY ?? this.followLerpX));
243
+ this.followOffsetX = options.offsetX ?? 0;
244
+ this.followOffsetY = options.offsetY ?? 0;
245
+ this.deadzoneW = Math.max(0, options.deadzoneWidth ?? 0);
246
+ this.deadzoneH = Math.max(0, options.deadzoneHeight ?? 0);
247
+ if (options.bounds) this.setBounds(options.bounds.x, options.bounds.y, options.bounds.width, options.bounds.height);
248
+ return this;
249
+ }
250
+ stopFollow() {
251
+ this.followTarget = null;
252
+ return this;
253
+ }
254
+ /** Internal per-step follow update invoked automatically by `Game.step()` before rendering. */
255
+ stepFollow(deltaMs) {
256
+ const target = this.followTarget;
257
+ if (!target) return;
258
+ const tx = target.x + this.followOffsetX;
259
+ const ty = target.y + this.followOffsetY;
260
+ const curCx = this.worldView.x + this.worldView.width * .5;
261
+ const curCy = this.worldView.y + this.worldView.height * .5;
262
+ let wantCx = curCx;
263
+ let wantCy = curCy;
264
+ const halfDzW = this.deadzoneW * .5;
265
+ const halfDzH = this.deadzoneH * .5;
266
+ if (tx < curCx - halfDzW) wantCx = tx + halfDzW;
267
+ else if (tx > curCx + halfDzW) wantCx = tx - halfDzW;
268
+ if (ty < curCy - halfDzH) wantCy = ty + halfDzH;
269
+ else if (ty > curCy + halfDzH) wantCy = ty - halfDzH;
270
+ const frames = Math.max(.1, deltaMs / (1e3 / 60));
271
+ const ax = this.followLerpX >= 1 ? 1 : 1 - Math.pow(1 - this.followLerpX, frames);
272
+ const ay = this.followLerpY >= 1 ? 1 : 1 - Math.pow(1 - this.followLerpY, frames);
273
+ const nextCx = curCx + (wantCx - curCx) * ax;
274
+ const nextCy = curCy + (wantCy - curCy) * ay;
275
+ const ox = this.width * this.originX;
276
+ const oy = this.height * this.originY;
277
+ const z = this.zoom || 1;
278
+ const nextSx = nextCx - this.worldView.width * .5 - ox + ox / z;
279
+ const nextSy = nextCy - this.worldView.height * .5 - oy + oy / z;
280
+ this.setScroll(nextSx, nextSy);
281
+ }
282
+ /** Recompute `worldView` and world->canvas `matrixCombined`. */
283
+ syncMetrics() {
284
+ const z = this.zoom || 1;
285
+ const ox = this.width * this.originX;
286
+ const oy = this.height * this.originY;
287
+ const sx = this.roundPixels ? Math.round(this.scrollX) : this.scrollX;
288
+ const sy = this.roundPixels ? Math.round(this.scrollY) : this.scrollY;
289
+ const dw = this.width / z;
290
+ const dh = this.height / z;
291
+ const vx = sx + ox - ox / z;
292
+ const vy = sy + oy - oy / z;
293
+ this.worldView.x = vx;
294
+ this.worldView.y = vy;
295
+ this.worldView.width = dw;
296
+ this.worldView.height = dh;
297
+ this.worldView.right = vx + dw;
298
+ this.worldView.bottom = vy + dh;
299
+ const m = this.matrixCombined;
300
+ m.a = z;
301
+ m.b = 0;
302
+ m.c = 0;
303
+ m.d = z;
304
+ m.tx = (-sx - ox) * z + ox + this.x;
305
+ m.ty = (-sy - oy) * z + oy + this.y;
306
+ m.scaleX = z;
307
+ m.scaleY = z;
308
+ }
309
+ };
2
310
  // src/display.ts
3
311
  var DisplayList = class {
4
312
  list = [];
@@ -956,8 +1264,14 @@ var Graphics = class extends DisplayObject {
956
1264
  numLen = 0;
957
1265
  /** Monotonic geometry revision bumped on every clear/draw op so WebGL can cache static tessellation meshes. */
958
1266
  version = 0;
1267
+ /** True when curved SDF-capable primitives (`fillCircle`, `strokeCircle`, `fillRoundedRect`, `strokeRoundedRect`) are recorded. */
1268
+ _hasSdfShapes = false;
1269
+ /** True when arbitrary polygons, lines, arcs, or eccentric ellipses requiring polygon tessellation are recorded. */
1270
+ _hasNonSdfOps = false;
959
1271
  /** Renderer-owned tessellated local-space mesh cache (opaque to user code). */
960
1272
  _glCache = null;
1273
+ /** Canvas2D retained `Path2D` cache keyed by `version` (opaque to user code). */
1274
+ _c2dCache = null;
961
1275
  /** Last filled rectangle bounds (used O(1) by `Pane` and `ScrollList` rect clip masks). */
962
1276
  maskRect = null;
963
1277
  constructor(scene) {
@@ -988,6 +1302,8 @@ var Graphics = class extends DisplayObject {
988
1302
  if (this.opLen !== 0 || this.numLen !== 0 || this.maskRect !== null) {
989
1303
  this.opLen = 0;
990
1304
  this.numLen = 0;
1305
+ this._hasSdfShapes = false;
1306
+ this._hasNonSdfOps = false;
991
1307
  this.maskRect = null;
992
1308
  this.version++;
993
1309
  }
@@ -1044,6 +1360,7 @@ var Graphics = class extends DisplayObject {
1044
1360
  }
1045
1361
  fillRoundedRect(x, y, w, h, radius = 8) {
1046
1362
  const r = typeof radius === "number" ? radius : radius.tl ?? radius.tr ?? 8;
1363
+ this._hasSdfShapes = true;
1047
1364
  const i = this.pushOp(5, 5);
1048
1365
  this.nums[i] = x;
1049
1366
  this.nums[i + 1] = y;
@@ -1054,6 +1371,7 @@ var Graphics = class extends DisplayObject {
1054
1371
  }
1055
1372
  strokeRoundedRect(x, y, w, h, radius = 8) {
1056
1373
  const r = typeof radius === "number" ? radius : radius.tl ?? radius.tr ?? 8;
1374
+ this._hasSdfShapes = true;
1057
1375
  const i = this.pushOp(6, 5);
1058
1376
  this.nums[i] = x;
1059
1377
  this.nums[i + 1] = y;
@@ -1063,6 +1381,7 @@ var Graphics = class extends DisplayObject {
1063
1381
  return this;
1064
1382
  }
1065
1383
  fillCircle(x, y, r) {
1384
+ this._hasSdfShapes = true;
1066
1385
  const i = this.pushOp(7, 3);
1067
1386
  this.nums[i] = x;
1068
1387
  this.nums[i + 1] = y;
@@ -1070,6 +1389,7 @@ var Graphics = class extends DisplayObject {
1070
1389
  return this;
1071
1390
  }
1072
1391
  strokeCircle(x, y, r) {
1392
+ this._hasSdfShapes = true;
1073
1393
  const i = this.pushOp(8, 3);
1074
1394
  this.nums[i] = x;
1075
1395
  this.nums[i + 1] = y;
@@ -1077,6 +1397,8 @@ var Graphics = class extends DisplayObject {
1077
1397
  return this;
1078
1398
  }
1079
1399
  fillEllipse(x, y, width, height) {
1400
+ if (Math.abs(width - height) <= 1e-4) this._hasSdfShapes = true;
1401
+ else this._hasNonSdfOps = true;
1080
1402
  const i = this.pushOp(9, 4);
1081
1403
  this.nums[i] = x;
1082
1404
  this.nums[i + 1] = y;
@@ -1085,6 +1407,8 @@ var Graphics = class extends DisplayObject {
1085
1407
  return this;
1086
1408
  }
1087
1409
  strokeEllipse(x, y, width, height) {
1410
+ if (Math.abs(width - height) <= 1e-4) this._hasSdfShapes = true;
1411
+ else this._hasNonSdfOps = true;
1088
1412
  const i = this.pushOp(10, 4);
1089
1413
  this.nums[i] = x;
1090
1414
  this.nums[i + 1] = y;
@@ -1093,6 +1417,7 @@ var Graphics = class extends DisplayObject {
1093
1417
  return this;
1094
1418
  }
1095
1419
  fillTriangle(x0, y0, x1, y1, x2, y2) {
1420
+ this._hasNonSdfOps = true;
1096
1421
  const i = this.pushOp(11, 6);
1097
1422
  this.nums[i] = x0;
1098
1423
  this.nums[i + 1] = y0;
@@ -1103,6 +1428,7 @@ var Graphics = class extends DisplayObject {
1103
1428
  return this;
1104
1429
  }
1105
1430
  strokeTriangle(x0, y0, x1, y1, x2, y2) {
1431
+ this._hasNonSdfOps = true;
1106
1432
  const i = this.pushOp(12, 6);
1107
1433
  this.nums[i] = x0;
1108
1434
  this.nums[i + 1] = y0;
@@ -1113,6 +1439,7 @@ var Graphics = class extends DisplayObject {
1113
1439
  return this;
1114
1440
  }
1115
1441
  lineBetween(x1, y1, x2, y2) {
1442
+ this._hasNonSdfOps = true;
1116
1443
  const i = this.pushOp(13, 4);
1117
1444
  this.nums[i] = x1;
1118
1445
  this.nums[i + 1] = y1;
@@ -1121,22 +1448,26 @@ var Graphics = class extends DisplayObject {
1121
1448
  return this;
1122
1449
  }
1123
1450
  beginPath() {
1451
+ this._hasNonSdfOps = true;
1124
1452
  this.pushOp(14, 0);
1125
1453
  return this;
1126
1454
  }
1127
1455
  moveTo(x, y) {
1456
+ this._hasNonSdfOps = true;
1128
1457
  const i = this.pushOp(15, 2);
1129
1458
  this.nums[i] = x;
1130
1459
  this.nums[i + 1] = y;
1131
1460
  return this;
1132
1461
  }
1133
1462
  lineTo(x, y) {
1463
+ this._hasNonSdfOps = true;
1134
1464
  const i = this.pushOp(16, 2);
1135
1465
  this.nums[i] = x;
1136
1466
  this.nums[i + 1] = y;
1137
1467
  return this;
1138
1468
  }
1139
1469
  arc(x, y, r, startAngle, endAngle, anticlockwise = false) {
1470
+ this._hasNonSdfOps = true;
1140
1471
  const i = this.pushOp(17, 6);
1141
1472
  this.nums[i] = x;
1142
1473
  this.nums[i + 1] = y;
@@ -1147,6 +1478,7 @@ var Graphics = class extends DisplayObject {
1147
1478
  return this;
1148
1479
  }
1149
1480
  slice(x, y, r, startAngle, endAngle, anticlockwise = false) {
1481
+ this._hasNonSdfOps = true;
1150
1482
  const i = this.pushOp(18, 6);
1151
1483
  this.nums[i] = x;
1152
1484
  this.nums[i + 1] = y;
@@ -1198,6 +1530,7 @@ var Graphics = class extends DisplayObject {
1198
1530
  }
1199
1531
  destroy(fromScene) {
1200
1532
  this._glCache = null;
1533
+ this._c2dCache = null;
1201
1534
  super.destroy(fromScene);
1202
1535
  }
1203
1536
  };
@@ -1430,6 +1763,51 @@ var InstancedBatchObject = class extends DisplayObject {
1430
1763
  super.destroy(fromScene);
1431
1764
  }
1432
1765
  };
1766
+ /**
1767
+ * Zero-texture GPU analytical SDF shape batch (`scene.add.shapeBatch(maxCount, defaults?)`).
1768
+ * Renders thousands of dynamic circles, rings, pills, capsules, shadows, health bars, or rounded rects
1769
+ * directly from `SoaWorld` `Float32Array` / `Uint32Array` columns — 1 analytical SDF quad (2 tris) per shape
1770
+ * on WebGL2 & WebGPU with zero sprite atlas baking and zero CPU polygon triangulation.
1771
+ */
1772
+ var ShapeBatchObject = class extends DisplayObject {
1773
+ maxCount;
1774
+ defaultWidth;
1775
+ defaultHeight;
1776
+ defaultRadius;
1777
+ defaultFillColor;
1778
+ defaultFillAlpha;
1779
+ defaultBorderColor;
1780
+ defaultBorderAlpha;
1781
+ defaultBorderWidth;
1782
+ pixelStep;
1783
+ columns = null;
1784
+ constructor(scene, maxCount = 1024, defaults = {}) {
1785
+ super(scene, "ShapeBatch");
1786
+ this.maxCount = Math.max(1, Math.floor(maxCount));
1787
+ this.originX = .5;
1788
+ this.originY = .5;
1789
+ const w = defaults.width ?? 12;
1790
+ const h = defaults.height ?? w;
1791
+ this.defaultWidth = w;
1792
+ this.defaultHeight = h;
1793
+ this.defaultRadius = defaults.radius ?? Math.min(w, h) * .5;
1794
+ this.defaultFillColor = (defaults.fillColor ?? 0xffffff) & HEX_MASK;
1795
+ this.defaultFillAlpha = defaults.fillAlpha ?? 1;
1796
+ this.defaultBorderColor = (defaults.borderColor ?? 0) & HEX_MASK;
1797
+ this.defaultBorderAlpha = defaults.borderAlpha ?? 1;
1798
+ this.defaultBorderWidth = defaults.borderWidth ?? 0;
1799
+ this.pixelStep = defaults.pixelStep ?? 0;
1800
+ }
1801
+ /** Bind or update the SoA column views read on each render pass (zero copying on caller side). */
1802
+ setColumns(columns) {
1803
+ this.columns = columns;
1804
+ return this;
1805
+ }
1806
+ destroy(fromScene) {
1807
+ this.columns = null;
1808
+ super.destroy(fromScene);
1809
+ }
1810
+ };
1433
1811
  // src/renderer-2d.ts
1434
1812
  function addPerCornerRoundedRectSubpath(ctx, x, y, w, h, tl, tr, br, bl) {
1435
1813
  const maxR = Math.min(w, h) * .5;
@@ -1486,8 +1864,30 @@ var Renderer2D = class Renderer2D extends EventEmitter {
1486
1864
  height;
1487
1865
  tintCanvas = null;
1488
1866
  tintCtx = null;
1867
+ tintCache = /* @__PURE__ */ new Map();
1489
1868
  maskWorldMat = createMatrix2D();
1490
1869
  pathCache = /* @__PURE__ */ new Map();
1870
+ curDrawCalls = 0;
1871
+ curFlushes = 0;
1872
+ curQuads = 0;
1873
+ curTris = 0;
1874
+ curSdfPanels = 0;
1875
+ curMsdfIcons = 0;
1876
+ lastStats = {
1877
+ drawCalls: 0,
1878
+ flushes: 0,
1879
+ quads: 0,
1880
+ tris: 0,
1881
+ culled: 0,
1882
+ texBinds: 0,
1883
+ batchMaxUnits: 0,
1884
+ sdfPanels: 0,
1885
+ sdfDraws: 0,
1886
+ msdfIcons: 0,
1887
+ msdfDraws: 0,
1888
+ pixelAaIcons: 0,
1889
+ pixelAaDraws: 0
1890
+ };
1491
1891
  constructor(canvas, width, height, existingCtx) {
1492
1892
  super();
1493
1893
  this.canvas = canvas;
@@ -1498,21 +1898,7 @@ var Renderer2D = class Renderer2D extends EventEmitter {
1498
1898
  this.ctx = ctx;
1499
1899
  }
1500
1900
  stats() {
1501
- return {
1502
- drawCalls: 0,
1503
- flushes: 0,
1504
- quads: 0,
1505
- tris: 0,
1506
- culled: 0,
1507
- texBinds: 0,
1508
- batchMaxUnits: 0,
1509
- sdfPanels: 0,
1510
- sdfDraws: 0,
1511
- msdfIcons: 0,
1512
- msdfDraws: 0,
1513
- pixelAaIcons: 0,
1514
- pixelAaDraws: 0
1515
- };
1901
+ return this.lastStats;
1516
1902
  }
1517
1903
  resize(width, height) {
1518
1904
  const w = Math.max(1, Math.round(width));
@@ -1523,12 +1909,42 @@ var Renderer2D = class Renderer2D extends EventEmitter {
1523
1909
  this.canvas.width = w;
1524
1910
  this.canvas.height = h;
1525
1911
  }
1526
- getTintedSource(img, sx, sy, sw, sh, rgb, fill) {
1912
+ getTintedSource(img, sx, sy, sw, sh, rgb, fill, texSource) {
1527
1913
  const iw = Math.max(1, Math.ceil(sw));
1528
1914
  const ih = Math.max(1, Math.ceil(sh));
1915
+ const doc = this.canvas.ownerDocument ?? (typeof document !== "undefined" ? document : null);
1916
+ if (!doc) return null;
1917
+ if (texSource && !texSource.isRenderTarget && iw <= 512 && ih <= 512) {
1918
+ const cacheKey = `${texSource.id}:${texSource.version}:${sx},${sy},${sw},${sh}:${rgb}:${fill ? 1 : 0}`;
1919
+ const hit = this.tintCache.get(cacheKey);
1920
+ if (hit) {
1921
+ this.tintCache.delete(cacheKey);
1922
+ this.tintCache.set(cacheKey, hit);
1923
+ return hit;
1924
+ }
1925
+ const entry = doc.createElement("canvas");
1926
+ entry.width = iw;
1927
+ entry.height = ih;
1928
+ const eg = entry.getContext("2d");
1929
+ if (eg) {
1930
+ eg.globalCompositeOperation = "source-over";
1931
+ eg.drawImage(img, sx, sy, sw, sh, 0, 0, iw, ih);
1932
+ eg.globalCompositeOperation = fill ? "source-in" : "multiply";
1933
+ eg.fillStyle = rgbToCss(rgb, 1);
1934
+ eg.fillRect(0, 0, iw, ih);
1935
+ if (!fill) {
1936
+ eg.globalCompositeOperation = "destination-in";
1937
+ eg.drawImage(img, sx, sy, sw, sh, 0, 0, iw, ih);
1938
+ }
1939
+ if (this.tintCache.size >= 256) {
1940
+ const oldest = this.tintCache.keys().next().value;
1941
+ if (oldest !== void 0) this.tintCache.delete(oldest);
1942
+ }
1943
+ this.tintCache.set(cacheKey, entry);
1944
+ return entry;
1945
+ }
1946
+ }
1529
1947
  if (!this.tintCanvas) {
1530
- const doc = this.canvas.ownerDocument ?? (typeof document !== "undefined" ? document : null);
1531
- if (!doc) return null;
1532
1948
  this.tintCanvas = doc.createElement("canvas");
1533
1949
  this.tintCtx = this.tintCanvas.getContext("2d");
1534
1950
  }
@@ -1588,13 +2004,19 @@ var Renderer2D = class Renderer2D extends EventEmitter {
1588
2004
  ctx.scale(img.flipX ? -1 : 1, img.flipY ? -1 : 1);
1589
2005
  }
1590
2006
  if (img.isTinted && (img.tint !== 0xffffff || img.tintMode === TintModes.FILL)) {
1591
- const tinted = this.getTintedSource(srcImg, sx, sy, sw, sh, img.tint, img.tintMode === TintModes.FILL);
2007
+ const tinted = this.getTintedSource(srcImg, sx, sy, sw, sh, img.tint, img.tintMode === TintModes.FILL, frame.source);
1592
2008
  if (tinted) {
1593
2009
  ctx.drawImage(tinted, 0, 0, Math.ceil(sw), Math.ceil(sh), dx, dy, dw, dh);
2010
+ this.curDrawCalls++;
2011
+ this.curQuads++;
2012
+ this.curTris += 2;
1594
2013
  return;
1595
2014
  }
1596
2015
  }
1597
2016
  ctx.drawImage(srcImg, sx, sy, sw, sh, dx, dy, dw, dh);
2017
+ this.curDrawCalls++;
2018
+ this.curQuads++;
2019
+ this.curTris += 2;
1598
2020
  }
1599
2021
  renderNineSlice(ns, ctx) {
1600
2022
  const frame = ns.frame;
@@ -1638,6 +2060,7 @@ var Renderer2D = class Renderer2D extends EventEmitter {
1638
2060
  -oy + h - B,
1639
2061
  -oy + h
1640
2062
  ];
2063
+ let patches = 0;
1641
2064
  for (let r = 0; r < 3; r++) {
1642
2065
  const sh = sys[r + 1] - sys[r], dh = dys[r + 1] - dys[r];
1643
2066
  if (sh <= 0 || dh <= 0) continue;
@@ -1645,8 +2068,14 @@ var Renderer2D = class Renderer2D extends EventEmitter {
1645
2068
  const sw = sxs[c + 1] - sxs[c], dw = dxs[c + 1] - dxs[c];
1646
2069
  if (sw <= 0 || dw <= 0) continue;
1647
2070
  ctx.drawImage(srcImg, sxs[c], sys[r], sw, sh, dxs[c], dys[r], dw, dh);
2071
+ patches++;
1648
2072
  }
1649
2073
  }
2074
+ if (patches > 0) {
2075
+ this.curDrawCalls++;
2076
+ this.curQuads += patches;
2077
+ this.curTris += patches * 2;
2078
+ }
1650
2079
  }
1651
2080
  renderTileSprite(ts, ctx) {
1652
2081
  const frame = ts.frame;
@@ -1660,13 +2089,14 @@ var Renderer2D = class Renderer2D extends EventEmitter {
1660
2089
  let drawSrc = srcImg;
1661
2090
  let baseSx = frame.cutX, baseSy = frame.cutY;
1662
2091
  if (ts.isTinted && ts.tint !== 0xffffff) {
1663
- const tinted = this.getTintedSource(srcImg, frame.cutX, frame.cutY, frame.cutWidth, frame.cutHeight, ts.tint, false);
2092
+ const tinted = this.getTintedSource(srcImg, frame.cutX, frame.cutY, frame.cutWidth, frame.cutHeight, ts.tint, false, frame.source);
1664
2093
  if (tinted) {
1665
2094
  drawSrc = tinted;
1666
2095
  baseSx = 0;
1667
2096
  baseSy = 0;
1668
2097
  }
1669
2098
  }
2099
+ let tiles = 0;
1670
2100
  for (let y = 0; y < ts.height;) {
1671
2101
  const ty = (y + offY) % fh;
1672
2102
  const dh = Math.min(fh - ty, ts.height - y);
@@ -1674,19 +2104,32 @@ var Renderer2D = class Renderer2D extends EventEmitter {
1674
2104
  const tx = (x + offX) % fw;
1675
2105
  const dw = Math.min(fw - tx, ts.width - x);
1676
2106
  ctx.drawImage(drawSrc, baseSx + tx, baseSy + ty, dw, dh, -ox + x, -oy + y, dw, dh);
2107
+ tiles++;
1677
2108
  x += dw;
1678
2109
  }
1679
2110
  y += dh;
1680
2111
  }
2112
+ if (tiles > 0) {
2113
+ this.curDrawCalls++;
2114
+ this.curQuads += tiles;
2115
+ this.curTris += tiles * 2;
2116
+ }
1681
2117
  }
1682
2118
  renderRectangle(r, ctx) {
1683
2119
  if (r.fillAlpha <= 0 || r.width <= 0 || r.height <= 0) return;
1684
2120
  ctx.fillStyle = rgbToCss(r.fillColor, r.fillAlpha);
1685
2121
  ctx.fillRect(-r.displayOriginX, -r.displayOriginY, r.width, r.height);
2122
+ this.curDrawCalls++;
2123
+ this.curQuads++;
2124
+ this.curTris += 2;
1686
2125
  }
1687
2126
  renderPanel(p, ctx) {
1688
2127
  const w = p.width, h = p.height;
1689
2128
  if (w <= 0 || h <= 0) return;
2129
+ this.curDrawCalls++;
2130
+ this.curQuads++;
2131
+ this.curTris += 2;
2132
+ this.curSdfPanels++;
1690
2133
  const x = -p.displayOriginX, y = -p.displayOriginY;
1691
2134
  const bw = Math.max(0, Math.min(p.borderWidth, Math.min(w, h) * .5));
1692
2135
  if (bw > 0 && p.openSide !== 0) {
@@ -1757,6 +2200,10 @@ var Renderer2D = class Renderer2D extends EventEmitter {
1757
2200
  ctx.fill(p2d);
1758
2201
  }
1759
2202
  ctx.restore();
2203
+ this.curDrawCalls++;
2204
+ this.curQuads++;
2205
+ this.curTris += 2;
2206
+ this.curMsdfIcons++;
1760
2207
  }
1761
2208
  renderText(txt, ctx) {
1762
2209
  const cv = txt.ensureCanvas();
@@ -1765,6 +2212,9 @@ var Renderer2D = class Renderer2D extends EventEmitter {
1765
2212
  const ox = txt.displayOriginX, oy = txt.displayOriginY;
1766
2213
  if (!txt.isCropped) {
1767
2214
  ctx.drawImage(cv, 0, 0, cv.width, cv.height, -ox, -oy, txt.width, txt.height);
2215
+ this.curDrawCalls++;
2216
+ this.curQuads++;
2217
+ this.curTris += 2;
1768
2218
  return;
1769
2219
  }
1770
2220
  const fw = txt.frame.cutWidth || cv.width || 1;
@@ -1777,26 +2227,208 @@ var Renderer2D = class Renderer2D extends EventEmitter {
1777
2227
  const resX = fw / txt.width;
1778
2228
  const resY = fh / txt.height;
1779
2229
  ctx.drawImage(cv, rx, ry, rw, rh, -ox + rx / resX, -oy + ry / resY, rw / resX, rh / resY);
2230
+ this.curDrawCalls++;
2231
+ this.curQuads++;
2232
+ this.curTris += 2;
1780
2233
  }
1781
2234
  renderGraphics(g, ctx) {
1782
2235
  if (g.opLen === 0) return;
2236
+ this.curDrawCalls++;
2237
+ const cached = g._c2dCache;
2238
+ if (cached && cached.version === g.version) {
2239
+ for (let i = 0; i < cached.runs.length; i++) {
2240
+ const run = cached.runs[i];
2241
+ if (run.kind === "fill") {
2242
+ ctx.fillStyle = run.style;
2243
+ ctx.fill(run.path);
2244
+ } else {
2245
+ ctx.lineWidth = run.lw;
2246
+ ctx.strokeStyle = run.style;
2247
+ ctx.stroke(run.path);
2248
+ }
2249
+ }
2250
+ return;
2251
+ }
2252
+ if (g._c2dSeenVer === g.version && typeof Path2D === "function" && !g._hasNonSdfOps) {
2253
+ const runs = [];
2254
+ let curFillStyle = "";
2255
+ let curFillPath = null;
2256
+ let fillRgb = WHITE_RGB, fillA = 1;
2257
+ let lineW = 1, lineRgb = WHITE_RGB, lineA = 1;
2258
+ const stack = [];
2259
+ const ops = g.ops;
2260
+ const nums = g.nums;
2261
+ let ni = 0;
2262
+ const ensureFillRun = (style, canBatch) => {
2263
+ if (canBatch && curFillPath && curFillStyle === style) return curFillPath;
2264
+ const p = new Path2D();
2265
+ runs.push({
2266
+ kind: "fill",
2267
+ style,
2268
+ lw: 0,
2269
+ path: p
2270
+ });
2271
+ curFillPath = canBatch ? p : null;
2272
+ curFillStyle = canBatch ? style : "";
2273
+ return p;
2274
+ };
2275
+ const pushStrokeRun = (style, lw) => {
2276
+ curFillPath = null;
2277
+ curFillStyle = "";
2278
+ const p = new Path2D();
2279
+ runs.push({
2280
+ kind: "stroke",
2281
+ style,
2282
+ lw,
2283
+ path: p
2284
+ });
2285
+ return p;
2286
+ };
2287
+ const addRRectToPath = (p, x, y, w, h, radius) => {
2288
+ const r = Math.max(0, Math.min(radius, w * .5, h * .5));
2289
+ p.moveTo(x + r, y);
2290
+ p.lineTo(x + w - r, y);
2291
+ if (r > 0) p.quadraticCurveTo(x + w, y, x + w, y + r);
2292
+ p.lineTo(x + w, y + h - r);
2293
+ if (r > 0) p.quadraticCurveTo(x + w, y + h, x + w - r, y + h);
2294
+ p.lineTo(x + r, y + h);
2295
+ if (r > 0) p.quadraticCurveTo(x, y + h, x, y + h - r);
2296
+ p.lineTo(x, y + r);
2297
+ if (r > 0) p.quadraticCurveTo(x, y, x + r, y);
2298
+ p.closePath();
2299
+ };
2300
+ for (let oi = 0; oi < g.opLen; oi++) switch (ops[oi]) {
2301
+ case 22:
2302
+ stack.push(fillRgb, fillA, lineW, lineRgb, lineA);
2303
+ break;
2304
+ case 23:
2305
+ if (stack.length >= 5) {
2306
+ lineA = stack.pop();
2307
+ lineRgb = stack.pop();
2308
+ lineW = stack.pop();
2309
+ fillA = stack.pop();
2310
+ fillRgb = stack.pop();
2311
+ }
2312
+ break;
2313
+ case 1:
2314
+ fillRgb = nums[ni++];
2315
+ fillA = nums[ni++];
2316
+ break;
2317
+ case 2:
2318
+ lineW = nums[ni++];
2319
+ lineRgb = nums[ni++];
2320
+ lineA = nums[ni++];
2321
+ break;
2322
+ case 3: {
2323
+ const x = nums[ni++], y = nums[ni++], w = nums[ni++], h = nums[ni++];
2324
+ if (fillA > 0) ensureFillRun(rgbToCss(fillRgb, fillA), fillA >= 1).rect(x, y, w, h);
2325
+ break;
2326
+ }
2327
+ case 4: {
2328
+ const x = nums[ni++], y = nums[ni++], w = nums[ni++], h = nums[ni++];
2329
+ if (lineA > 0 && lineW > 0) pushStrokeRun(rgbToCss(lineRgb, lineA), lineW).rect(x, y, w, h);
2330
+ break;
2331
+ }
2332
+ case 5: {
2333
+ const x = nums[ni++], y = nums[ni++], w = nums[ni++], h = nums[ni++], r = nums[ni++];
2334
+ if (fillA > 0 && w > 0 && h > 0) addRRectToPath(ensureFillRun(rgbToCss(fillRgb, fillA), fillA >= 1), x, y, w, h, r);
2335
+ break;
2336
+ }
2337
+ case 6: {
2338
+ const x = nums[ni++], y = nums[ni++], w = nums[ni++], h = nums[ni++], r = nums[ni++];
2339
+ if (lineA > 0 && lineW > 0 && w > 0 && h > 0) addRRectToPath(pushStrokeRun(rgbToCss(lineRgb, lineA), lineW), x, y, w, h, r);
2340
+ break;
2341
+ }
2342
+ case 7: {
2343
+ const cx = nums[ni++], cy = nums[ni++], r = nums[ni++];
2344
+ if (fillA > 0 && r > 0) {
2345
+ const p = ensureFillRun(rgbToCss(fillRgb, fillA), fillA >= 1);
2346
+ p.moveTo(cx + r, cy);
2347
+ p.arc(cx, cy, r, 0, Math.PI * 2);
2348
+ }
2349
+ break;
2350
+ }
2351
+ case 8: {
2352
+ const cx = nums[ni++], cy = nums[ni++], r = nums[ni++];
2353
+ if (lineA > 0 && lineW > 0 && r > 0) {
2354
+ const p = pushStrokeRun(rgbToCss(lineRgb, lineA), lineW);
2355
+ p.moveTo(cx + r, cy);
2356
+ p.arc(cx, cy, r, 0, Math.PI * 2);
2357
+ }
2358
+ break;
2359
+ }
2360
+ case 9: {
2361
+ const cx = nums[ni++], cy = nums[ni++], w = nums[ni++], h = nums[ni++];
2362
+ if (fillA > 0 && w > 0 && h > 0) {
2363
+ const p = ensureFillRun(rgbToCss(fillRgb, fillA), fillA >= 1);
2364
+ p.moveTo(cx + w * .5, cy);
2365
+ p.ellipse(cx, cy, w * .5, h * .5, 0, 0, Math.PI * 2);
2366
+ }
2367
+ break;
2368
+ }
2369
+ case 10: {
2370
+ const cx = nums[ni++], cy = nums[ni++], w = nums[ni++], h = nums[ni++];
2371
+ if (lineA > 0 && lineW > 0 && w > 0 && h > 0) {
2372
+ const p = pushStrokeRun(rgbToCss(lineRgb, lineA), lineW);
2373
+ p.moveTo(cx + w * .5, cy);
2374
+ p.ellipse(cx, cy, w * .5, h * .5, 0, 0, Math.PI * 2);
2375
+ }
2376
+ break;
2377
+ }
2378
+ }
2379
+ g._c2dCache = {
2380
+ version: g.version,
2381
+ runs
2382
+ };
2383
+ for (let i = 0; i < runs.length; i++) {
2384
+ const run = runs[i];
2385
+ if (run.kind === "fill") {
2386
+ ctx.fillStyle = run.style;
2387
+ ctx.fill(run.path);
2388
+ } else {
2389
+ ctx.lineWidth = run.lw;
2390
+ ctx.strokeStyle = run.style;
2391
+ ctx.stroke(run.path);
2392
+ }
2393
+ }
2394
+ return;
2395
+ }
2396
+ g._c2dSeenVer = g.version;
1783
2397
  let fillRgb = WHITE_RGB, fillA = 1;
1784
2398
  let lineW = 1, lineRgb = WHITE_RGB, lineA = 1;
2399
+ ctx.fillStyle = rgbToCss(fillRgb, fillA);
2400
+ ctx.lineWidth = lineW;
2401
+ ctx.strokeStyle = rgbToCss(lineRgb, lineA);
2402
+ let batchFillOpen = false;
2403
+ const flushBatchFill = () => {
2404
+ if (batchFillOpen) {
2405
+ ctx.fill();
2406
+ batchFillOpen = false;
2407
+ }
2408
+ };
1785
2409
  const ops = g.ops;
1786
2410
  const nums = g.nums;
1787
2411
  let ni = 0;
1788
2412
  for (let oi = 0; oi < g.opLen; oi++) switch (ops[oi]) {
1789
2413
  case 22:
2414
+ flushBatchFill();
1790
2415
  ctx.save();
1791
2416
  break;
1792
2417
  case 23:
2418
+ flushBatchFill();
1793
2419
  ctx.restore();
1794
2420
  break;
1795
- case 1:
1796
- fillRgb = nums[ni++];
1797
- fillA = nums[ni++];
1798
- ctx.fillStyle = rgbToCss(fillRgb, fillA);
2421
+ case 1: {
2422
+ const nextRgb = nums[ni++];
2423
+ const nextA = nums[ni++];
2424
+ if (nextRgb !== fillRgb || nextA !== fillA) {
2425
+ flushBatchFill();
2426
+ fillRgb = nextRgb;
2427
+ fillA = nextA;
2428
+ ctx.fillStyle = rgbToCss(fillRgb, fillA);
2429
+ }
1799
2430
  break;
2431
+ }
1800
2432
  case 2:
1801
2433
  lineW = nums[ni++];
1802
2434
  lineRgb = nums[ni++];
@@ -1806,17 +2438,30 @@ var Renderer2D = class Renderer2D extends EventEmitter {
1806
2438
  break;
1807
2439
  case 3: {
1808
2440
  const x = nums[ni++], y = nums[ni++], w = nums[ni++], h = nums[ni++];
1809
- if (fillA > 0) ctx.fillRect(x, y, w, h);
2441
+ if (fillA >= 1) {
2442
+ if (!batchFillOpen) {
2443
+ ctx.beginPath();
2444
+ batchFillOpen = true;
2445
+ }
2446
+ ctx.rect(x, y, w, h);
2447
+ } else if (fillA > 0) {
2448
+ flushBatchFill();
2449
+ ctx.fillRect(x, y, w, h);
2450
+ }
1810
2451
  break;
1811
2452
  }
1812
2453
  case 4: {
1813
2454
  const x = nums[ni++], y = nums[ni++], w = nums[ni++], h = nums[ni++];
1814
- if (lineA > 0 && lineW > 0) ctx.strokeRect(x, y, w, h);
2455
+ if (lineA > 0 && lineW > 0) {
2456
+ flushBatchFill();
2457
+ ctx.strokeRect(x, y, w, h);
2458
+ }
1815
2459
  break;
1816
2460
  }
1817
2461
  case 5: {
1818
2462
  const x = nums[ni++], y = nums[ni++], w = nums[ni++], h = nums[ni++], r = nums[ni++];
1819
2463
  if (fillA > 0 && w > 0 && h > 0) {
2464
+ flushBatchFill();
1820
2465
  roundedRectPath(ctx, x, y, w, h, r);
1821
2466
  ctx.fill();
1822
2467
  }
@@ -1825,6 +2470,7 @@ var Renderer2D = class Renderer2D extends EventEmitter {
1825
2470
  case 6: {
1826
2471
  const x = nums[ni++], y = nums[ni++], w = nums[ni++], h = nums[ni++], r = nums[ni++];
1827
2472
  if (lineA > 0 && lineW > 0 && w > 0 && h > 0) {
2473
+ flushBatchFill();
1828
2474
  roundedRectPath(ctx, x, y, w, h, r);
1829
2475
  ctx.stroke();
1830
2476
  }
@@ -1832,7 +2478,15 @@ var Renderer2D = class Renderer2D extends EventEmitter {
1832
2478
  }
1833
2479
  case 7: {
1834
2480
  const cx = nums[ni++], cy = nums[ni++], r = nums[ni++];
1835
- if (fillA > 0 && r > 0) {
2481
+ if (fillA >= 1 && r > 0) {
2482
+ if (!batchFillOpen) {
2483
+ ctx.beginPath();
2484
+ batchFillOpen = true;
2485
+ }
2486
+ ctx.moveTo(cx + r, cy);
2487
+ ctx.arc(cx, cy, r, 0, Math.PI * 2);
2488
+ } else if (fillA > 0 && r > 0) {
2489
+ flushBatchFill();
1836
2490
  ctx.beginPath();
1837
2491
  ctx.arc(cx, cy, r, 0, Math.PI * 2);
1838
2492
  ctx.fill();
@@ -1842,6 +2496,7 @@ var Renderer2D = class Renderer2D extends EventEmitter {
1842
2496
  case 8: {
1843
2497
  const cx = nums[ni++], cy = nums[ni++], r = nums[ni++];
1844
2498
  if (lineA > 0 && lineW > 0 && r > 0) {
2499
+ flushBatchFill();
1845
2500
  ctx.beginPath();
1846
2501
  ctx.arc(cx, cy, r, 0, Math.PI * 2);
1847
2502
  ctx.stroke();
@@ -1850,7 +2505,15 @@ var Renderer2D = class Renderer2D extends EventEmitter {
1850
2505
  }
1851
2506
  case 9: {
1852
2507
  const cx = nums[ni++], cy = nums[ni++], w = nums[ni++], h = nums[ni++];
1853
- if (fillA > 0 && w > 0 && h > 0) {
2508
+ if (fillA >= 1 && w > 0 && h > 0) {
2509
+ if (!batchFillOpen) {
2510
+ ctx.beginPath();
2511
+ batchFillOpen = true;
2512
+ }
2513
+ ctx.moveTo(cx + w * .5, cy);
2514
+ ctx.ellipse(cx, cy, w * .5, h * .5, 0, 0, Math.PI * 2);
2515
+ } else if (fillA > 0 && w > 0 && h > 0) {
2516
+ flushBatchFill();
1854
2517
  ctx.beginPath();
1855
2518
  ctx.ellipse(cx, cy, w * .5, h * .5, 0, 0, Math.PI * 2);
1856
2519
  ctx.fill();
@@ -1860,6 +2523,7 @@ var Renderer2D = class Renderer2D extends EventEmitter {
1860
2523
  case 10: {
1861
2524
  const cx = nums[ni++], cy = nums[ni++], w = nums[ni++], h = nums[ni++];
1862
2525
  if (lineA > 0 && lineW > 0 && w > 0 && h > 0) {
2526
+ flushBatchFill();
1863
2527
  ctx.beginPath();
1864
2528
  ctx.ellipse(cx, cy, w * .5, h * .5, 0, 0, Math.PI * 2);
1865
2529
  ctx.stroke();
@@ -1869,6 +2533,7 @@ var Renderer2D = class Renderer2D extends EventEmitter {
1869
2533
  case 11: {
1870
2534
  const x0 = nums[ni++], y0 = nums[ni++], x1 = nums[ni++], y1 = nums[ni++], x2 = nums[ni++], y2 = nums[ni++];
1871
2535
  if (fillA > 0) {
2536
+ flushBatchFill();
1872
2537
  ctx.beginPath();
1873
2538
  ctx.moveTo(x0, y0);
1874
2539
  ctx.lineTo(x1, y1);
@@ -1881,6 +2546,7 @@ var Renderer2D = class Renderer2D extends EventEmitter {
1881
2546
  case 12: {
1882
2547
  const x0 = nums[ni++], y0 = nums[ni++], x1 = nums[ni++], y1 = nums[ni++], x2 = nums[ni++], y2 = nums[ni++];
1883
2548
  if (lineA > 0 && lineW > 0) {
2549
+ flushBatchFill();
1884
2550
  ctx.beginPath();
1885
2551
  ctx.moveTo(x0, y0);
1886
2552
  ctx.lineTo(x1, y1);
@@ -1893,6 +2559,7 @@ var Renderer2D = class Renderer2D extends EventEmitter {
1893
2559
  case 13: {
1894
2560
  const x0 = nums[ni++], y0 = nums[ni++], x1 = nums[ni++], y1 = nums[ni++];
1895
2561
  if (lineA > 0 && lineW > 0) {
2562
+ flushBatchFill();
1896
2563
  ctx.beginPath();
1897
2564
  ctx.moveTo(x0, y0);
1898
2565
  ctx.lineTo(x1, y1);
@@ -1901,6 +2568,7 @@ var Renderer2D = class Renderer2D extends EventEmitter {
1901
2568
  break;
1902
2569
  }
1903
2570
  case 14:
2571
+ flushBatchFill();
1904
2572
  ctx.beginPath();
1905
2573
  break;
1906
2574
  case 15:
@@ -1929,32 +2597,169 @@ var Renderer2D = class Renderer2D extends EventEmitter {
1929
2597
  break;
1930
2598
  case 21: if (lineA > 0 && lineW > 0) ctx.stroke();
1931
2599
  }
2600
+ flushBatchFill();
1932
2601
  }
1933
2602
  renderInstancedBatch(batch, ctx) {
1934
2603
  const cols = batch.columns;
1935
2604
  if (!cols || cols.count <= 0) return;
1936
2605
  const baseAlpha = ctx.globalAlpha;
1937
2606
  const n = Math.min(cols.count, batch.maxCount, cols.x.length, cols.y.length);
2607
+ let vis = 0;
2608
+ let lastTintImg = null;
2609
+ let lastCutX = -1, lastCutY = -1, lastCutW = -1, lastCutH = -1, lastRgb = -1;
2610
+ let cachedTinted = null;
1938
2611
  for (let i = 0; i < n; i++) {
1939
2612
  const s = cols.order ? cols.order[i] : i;
1940
2613
  const a = baseAlpha * (cols.alpha ? cols.alpha[s] : 1);
1941
2614
  if (a <= 0) continue;
1942
2615
  const fr = cols.frameIndex ? batch.texture.get(cols.frameIndex[s]) : batch.defaultFrame;
2616
+ if (!fr || fr.width <= 0 || fr.height <= 0) continue;
1943
2617
  const sx = cols.scaleX ? cols.scaleX[s] : 1;
1944
2618
  const sy = cols.scaleY ? cols.scaleY[s] : sx;
1945
2619
  const rot = cols.rotation ? cols.rotation[s] : 0;
2620
+ const rgb = cols.tint ? cols.tint[s] ?? 0xffffff : WHITE_RGB;
1946
2621
  const px = cols.x[s];
1947
2622
  const py = cols.y[s];
2623
+ const dx = -fr.width * batch.originX;
2624
+ const dy = -fr.height * batch.originY;
2625
+ const srcImg = fr.source.image;
1948
2626
  ctx.save();
1949
2627
  ctx.globalAlpha = a;
1950
2628
  ctx.imageSmoothingEnabled = fr.source.filterMode !== FilterMode.NEAREST;
1951
2629
  ctx.translate(px, py);
1952
2630
  if (rot !== 0) ctx.rotate(rot);
1953
2631
  if (sx !== 1 || sy !== 1) ctx.scale(sx, sy);
1954
- ctx.drawImage(fr.source.image, fr.cutX, fr.cutY, fr.cutWidth, fr.cutHeight, -fr.width * batch.originX, -fr.height * batch.originY, fr.width, fr.height);
2632
+ if (rgb !== 0xffffff && fr.cutWidth > 0 && fr.cutHeight > 0) {
2633
+ if (srcImg !== lastTintImg || fr.cutX !== lastCutX || fr.cutY !== lastCutY || fr.cutWidth !== lastCutW || fr.cutHeight !== lastCutH || rgb !== lastRgb) {
2634
+ cachedTinted = this.getTintedSource(srcImg, fr.cutX, fr.cutY, fr.cutWidth, fr.cutHeight, rgb, false, fr.source);
2635
+ lastTintImg = srcImg;
2636
+ lastCutX = fr.cutX;
2637
+ lastCutY = fr.cutY;
2638
+ lastCutW = fr.cutWidth;
2639
+ lastCutH = fr.cutHeight;
2640
+ lastRgb = rgb;
2641
+ }
2642
+ if (cachedTinted) {
2643
+ ctx.drawImage(cachedTinted, 0, 0, Math.ceil(fr.cutWidth), Math.ceil(fr.cutHeight), dx, dy, fr.width, fr.height);
2644
+ ctx.restore();
2645
+ vis++;
2646
+ continue;
2647
+ }
2648
+ }
2649
+ ctx.drawImage(srcImg, fr.cutX, fr.cutY, fr.cutWidth, fr.cutHeight, dx, dy, fr.width, fr.height);
1955
2650
  ctx.restore();
2651
+ vis++;
1956
2652
  }
1957
2653
  ctx.globalAlpha = baseAlpha;
2654
+ if (vis > 0) {
2655
+ this.curDrawCalls++;
2656
+ this.curFlushes++;
2657
+ this.curQuads += vis;
2658
+ this.curTris += vis * 2;
2659
+ }
2660
+ }
2661
+ renderShapeBatch(batch, ctx) {
2662
+ const cols = batch.columns;
2663
+ if (!cols || cols.count <= 0) return;
2664
+ const baseAlpha = ctx.globalAlpha;
2665
+ const n = Math.min(cols.count, batch.maxCount, cols.x.length, cols.y.length);
2666
+ const oxRatio = batch.originX;
2667
+ const oyRatio = batch.originY;
2668
+ const wCol = typeof cols.width === "object" && cols.width ? cols.width : null;
2669
+ const wDef = typeof cols.width === "number" ? cols.width : batch.defaultWidth;
2670
+ const hCol = typeof cols.height === "object" && cols.height ? cols.height : null;
2671
+ const hDef = typeof cols.height === "number" ? cols.height : typeof cols.width === "number" ? cols.width : batch.defaultHeight;
2672
+ const rCol = typeof cols.radius === "object" && cols.radius ? cols.radius : null;
2673
+ const rDef = typeof cols.radius === "number" ? cols.radius : batch.defaultRadius;
2674
+ const fcCol = typeof cols.fillColor === "object" && cols.fillColor ? cols.fillColor : null;
2675
+ const fcDef = typeof cols.fillColor === "number" ? cols.fillColor : batch.defaultFillColor;
2676
+ const faCol = typeof cols.fillAlpha === "object" && cols.fillAlpha ? cols.fillAlpha : null;
2677
+ const faDef = typeof cols.fillAlpha === "number" ? cols.fillAlpha : batch.defaultFillAlpha;
2678
+ const bcCol = typeof cols.borderColor === "object" && cols.borderColor ? cols.borderColor : null;
2679
+ const bcDef = typeof cols.borderColor === "number" ? cols.borderColor : batch.defaultBorderColor;
2680
+ const baCol = typeof cols.borderAlpha === "object" && cols.borderAlpha ? cols.borderAlpha : null;
2681
+ const baDef = typeof cols.borderAlpha === "number" ? cols.borderAlpha : batch.defaultBorderAlpha;
2682
+ const bwCol = typeof cols.borderWidth === "object" && cols.borderWidth ? cols.borderWidth : null;
2683
+ const bwDef = typeof cols.borderWidth === "number" ? cols.borderWidth : batch.defaultBorderWidth;
2684
+ const rotCol = cols.rotation ?? null;
2685
+ const ordCol = cols.order ?? null;
2686
+ let vis = 0;
2687
+ let lastFc = -1, lastFa = -1, fillCss = "";
2688
+ let lastBc = -1, lastBa = -1, borderCss = "";
2689
+ for (let i = 0; i < n; i++) {
2690
+ const s = ordCol ? ordCol[i] : i;
2691
+ const w = wCol ? wCol[s] : wDef;
2692
+ const h = hCol ? hCol[s] : wCol ? w : hDef;
2693
+ if (w <= 0 || h <= 0) continue;
2694
+ const fa = faCol ? faCol[s] : faDef;
2695
+ const bw = bwCol ? bwCol[s] : bwDef;
2696
+ const ba = bw > 0 ? baCol ? baCol[s] : baDef : 0;
2697
+ if (fa <= 0 && ba <= 0) continue;
2698
+ const rad = Math.max(0, Math.min(rCol ? rCol[s] : rDef, Math.min(w, h) * .5));
2699
+ const px = cols.x[s];
2700
+ const py = cols.y[s];
2701
+ const rot = rotCol ? rotCol[s] : 0;
2702
+ if (fa > 0) {
2703
+ const fc = fcCol ? fcCol[s] : fcDef;
2704
+ if (fc !== lastFc || fa !== lastFa) {
2705
+ lastFc = fc;
2706
+ lastFa = fa;
2707
+ fillCss = rgbToCss(fc, fa);
2708
+ }
2709
+ }
2710
+ if (ba > 0) {
2711
+ const bc = bcCol ? bcCol[s] : bcDef;
2712
+ if (bc !== lastBc || ba !== lastBa) {
2713
+ lastBc = bc;
2714
+ lastBa = ba;
2715
+ borderCss = rgbToCss(bc, ba);
2716
+ }
2717
+ }
2718
+ const rx = rot === 0 ? px - w * oxRatio : -w * oxRatio;
2719
+ const ry = rot === 0 ? py - h * oyRatio : -h * oyRatio;
2720
+ if (rot !== 0) {
2721
+ ctx.save();
2722
+ ctx.translate(px, py);
2723
+ ctx.rotate(rot);
2724
+ }
2725
+ if (rad <= 0 && bw <= 0) {
2726
+ ctx.fillStyle = fillCss;
2727
+ ctx.fillRect(rx, ry, w, h);
2728
+ } else if (Math.abs(w - h) <= 1e-4 && rad >= w * .5 - 1e-4) {
2729
+ ctx.beginPath();
2730
+ ctx.arc(rx + w * .5, ry + h * .5, Math.max(0, w * .5 - bw * .5), 0, Math.PI * 2);
2731
+ if (fa > 0) {
2732
+ ctx.fillStyle = fillCss;
2733
+ ctx.fill();
2734
+ }
2735
+ if (ba > 0 && bw > 0) {
2736
+ ctx.lineWidth = bw;
2737
+ ctx.strokeStyle = borderCss;
2738
+ ctx.stroke();
2739
+ }
2740
+ } else {
2741
+ roundedRectPath(ctx, rx + bw * .5, ry + bw * .5, Math.max(0, w - bw), Math.max(0, h - bw), Math.max(0, rad - bw * .5));
2742
+ if (fa > 0) {
2743
+ ctx.fillStyle = fillCss;
2744
+ ctx.fill();
2745
+ }
2746
+ if (ba > 0 && bw > 0) {
2747
+ ctx.lineWidth = bw;
2748
+ ctx.strokeStyle = borderCss;
2749
+ ctx.stroke();
2750
+ }
2751
+ }
2752
+ if (rot !== 0) ctx.restore();
2753
+ vis++;
2754
+ }
2755
+ ctx.globalAlpha = baseAlpha;
2756
+ if (vis > 0) {
2757
+ this.curDrawCalls++;
2758
+ this.curFlushes++;
2759
+ this.curQuads += vis;
2760
+ this.curTris += vis * 2;
2761
+ this.curSdfPanels += vis;
2762
+ }
1958
2763
  }
1959
2764
  /** Create an offscreen Canvas2D render target backed by a registered Stage `Texture` (`key`). */
1960
2765
  createRenderTarget(key, width, height, textures, filterMode = FilterMode.LINEAR) {
@@ -1968,21 +2773,30 @@ var Renderer2D = class Renderer2D extends EventEmitter {
1968
2773
  const tex = textures.addCanvas(key, rtCanvas);
1969
2774
  if (!tex) throw new Error("Stage: failed to register Canvas2D RenderTarget texture");
1970
2775
  tex.source[0]?.setFilter(filterMode);
2776
+ const rtCamera = new Camera(w, h, true);
1971
2777
  const rtRenderer = new Renderer2D(rtCanvas, w, h);
1972
2778
  return {
1973
2779
  key,
1974
2780
  width: w,
1975
2781
  height: h,
1976
2782
  texture: tex,
2783
+ camera: rtCamera,
1977
2784
  _rtRenderer: rtRenderer,
2785
+ readPixels: (out) => {
2786
+ const byteLen = w * h * 4;
2787
+ const dst = out && out.length >= byteLen ? out : new Uint8Array(byteLen);
2788
+ const img = rtRenderer.ctx.getImageData(0, 0, w, h);
2789
+ dst.set(img.data.subarray(0, byteLen));
2790
+ return dst;
2791
+ },
1978
2792
  destroy: () => {
1979
2793
  rtRenderer.destroy();
1980
2794
  textures.remove(key);
1981
2795
  }
1982
2796
  };
1983
2797
  }
1984
- /** Render a `DisplayList` (or callback) into an offscreen `StageRenderTarget` in Canvas2D / Headless Node. */
1985
- renderToTarget(target, content, camera) {
2798
+ /** Render a `DisplayList` (or callback) into an offscreen `StageRenderTarget` in Canvas2D / Headless Node (`camera` defaults to `target.camera`). */
2799
+ renderToTarget(target, content, camera = target.camera) {
1986
2800
  const sub = target._rtRenderer;
1987
2801
  if (!sub) return;
1988
2802
  if (typeof content === "function") {
@@ -1990,11 +2804,15 @@ var Renderer2D = class Renderer2D extends EventEmitter {
1990
2804
  content(sub, camera);
1991
2805
  } else sub.render(content, camera);
1992
2806
  const src = target.texture.source[0];
1993
- if (src) src.dirty = true;
2807
+ if (src) {
2808
+ src.version++;
2809
+ src.dirty = true;
2810
+ }
1994
2811
  }
1995
2812
  renderBlitter(blitter, ctx) {
1996
2813
  const list = blitter.children.list;
1997
2814
  const baseAlpha = ctx.globalAlpha;
2815
+ let vis = 0;
1998
2816
  for (let i = 0; i < list.length; i++) {
1999
2817
  const bob = list[i];
2000
2818
  if (!bob.visible || bob.alpha <= 0) continue;
@@ -2002,8 +2820,14 @@ var Renderer2D = class Renderer2D extends EventEmitter {
2002
2820
  ctx.imageSmoothingEnabled = frame.source.filterMode !== FilterMode.NEAREST;
2003
2821
  ctx.globalAlpha = baseAlpha * bob.alpha;
2004
2822
  ctx.drawImage(frame.source.image, frame.cutX, frame.cutY, frame.cutWidth, frame.cutHeight, bob.x, bob.y, frame.width, frame.height);
2823
+ vis++;
2005
2824
  }
2006
2825
  ctx.globalAlpha = baseAlpha;
2826
+ if (vis > 0) {
2827
+ this.curDrawCalls++;
2828
+ this.curQuads += vis;
2829
+ this.curTris += vis * 2;
2830
+ }
2007
2831
  }
2008
2832
  renderNode(obj, camera, ctx, isRoot) {
2009
2833
  if (!obj.visible || obj.alpha <= 0) return;
@@ -2081,6 +2905,9 @@ var Renderer2D = class Renderer2D extends EventEmitter {
2081
2905
  case "InstancedBatch":
2082
2906
  this.renderInstancedBatch(obj, ctx);
2083
2907
  break;
2908
+ case "ShapeBatch":
2909
+ this.renderShapeBatch(obj, ctx);
2910
+ break;
2084
2911
  case "Container": {
2085
2912
  const list = obj.sortedChildren();
2086
2913
  for (let i = 0; i < list.length; i++) this.renderNode(list[i], camera, ctx, false);
@@ -2090,6 +2917,12 @@ var Renderer2D = class Renderer2D extends EventEmitter {
2090
2917
  ctx.restore();
2091
2918
  }
2092
2919
  render(displayList, camera) {
2920
+ this.curDrawCalls = 0;
2921
+ this.curFlushes = 0;
2922
+ this.curQuads = 0;
2923
+ this.curTris = 0;
2924
+ this.curSdfPanels = 0;
2925
+ this.curMsdfIcons = 0;
2093
2926
  camera.syncMetrics();
2094
2927
  const ctx = this.ctx;
2095
2928
  ctx.setTransform(1, 0, 0, 1, 0, 0);
@@ -2104,9 +2937,18 @@ var Renderer2D = class Renderer2D extends EventEmitter {
2104
2937
  ctx.globalAlpha = camera.alpha;
2105
2938
  const list = displayList.sorted();
2106
2939
  for (let i = 0; i < list.length; i++) this.renderNode(list[i], camera, ctx, true);
2940
+ this.lastStats.drawCalls = this.curDrawCalls;
2941
+ this.lastStats.flushes = this.curFlushes;
2942
+ this.lastStats.quads = this.curQuads;
2943
+ this.lastStats.tris = this.curTris;
2944
+ this.lastStats.sdfPanels = this.curSdfPanels;
2945
+ this.lastStats.sdfDraws = this.curSdfPanels > 0 ? 1 : 0;
2946
+ this.lastStats.msdfIcons = this.curMsdfIcons;
2947
+ this.lastStats.msdfDraws = this.curMsdfIcons > 0 ? 1 : 0;
2107
2948
  }
2108
2949
  destroy() {
2109
2950
  this.removeAllListeners();
2951
+ this.tintCache.clear();
2110
2952
  this.tintCanvas = null;
2111
2953
  this.tintCtx = null;
2112
2954
  }
@@ -2147,7 +2989,7 @@ const MAX_VERTS = 16384;
2147
2989
  const MAX_INDICES = 32768;
2148
2990
  const VERT_FLOATS = 6;
2149
2991
  const VERT_STRIDE = 24;
2150
- const MAX_SDF_PANELS = 1024;
2992
+ const MAX_SDF_PANELS = 16384;
2151
2993
  const SDF_FLOATS = 16;
2152
2994
  const SDF_STRIDE = 64;
2153
2995
  const WGSL_BATCH_SRC = `
@@ -2846,18 +3688,13 @@ var RendererWebGpu = class extends EventEmitter {
2846
3688
  this.vertCount = bv + 3;
2847
3689
  this.indexCount = ii + 3;
2848
3690
  }
2849
- renderPanel(panel, m, alpha) {
2850
- if (this.sdfCount >= MAX_SDF_PANELS) return;
3691
+ pushSdfInstance(m, rx, ry, w, h, rTL, rTR, rBR, rBL, borderW, pixelStep, fillAbgr, borderAbgr, blend) {
3692
+ if (this.sdfCount >= MAX_SDF_PANELS || w <= 0 || h <= 0) return;
2851
3693
  if (this.indexCount > this.cmdStartIndex) this.flushPendingBatch();
2852
- if (this.curBlend !== panel.blendMode && this.sdfCount > this.sdfCmdStart) this.flushPendingSdf();
2853
- this.curBlend = panel.blendMode;
2854
- const w = panel.width;
2855
- const h = panel.height;
2856
- if (w <= 0 || h <= 0) return;
2857
- const ox = panel.displayOriginX;
2858
- const oy = panel.displayOriginY;
2859
- const tx = m.a * -ox + m.c * -oy + m.tx;
2860
- const ty = m.b * -ox + m.d * -oy + m.ty;
3694
+ if (this.curBlend !== blend && this.sdfCount > this.sdfCmdStart) this.flushPendingSdf();
3695
+ this.curBlend = blend;
3696
+ const tx = m.a * rx + m.c * ry + m.tx;
3697
+ const ty = m.b * rx + m.d * ry + m.ty;
2861
3698
  const maxR = Math.min(w, h) * .5;
2862
3699
  const base = this.sdfCount * SDF_FLOATS;
2863
3700
  const f = this.sdfF32;
@@ -2870,16 +3707,22 @@ var RendererWebGpu = class extends EventEmitter {
2870
3707
  f[base + 5] = ty;
2871
3708
  f[base + 6] = w;
2872
3709
  f[base + 7] = h;
2873
- f[base + 8] = Math.min(maxR, panel.radiusTL);
2874
- f[base + 9] = Math.min(maxR, panel.radiusTR);
2875
- f[base + 10] = Math.min(maxR, panel.radiusBR);
2876
- f[base + 11] = Math.min(maxR, panel.radiusBL);
2877
- f[base + 12] = panel.borderWidth;
2878
- f[base + 13] = panel.pixelStep;
2879
- u[base + 14] = packColorAbgr(panel.fillColor, panel.fillAlpha * alpha);
2880
- u[base + 15] = packColorAbgr(panel.borderColor, panel.borderAlpha * alpha);
3710
+ f[base + 8] = Math.max(0, Math.min(maxR, rTL));
3711
+ f[base + 9] = Math.max(0, Math.min(maxR, rTR));
3712
+ f[base + 10] = Math.max(0, Math.min(maxR, rBR));
3713
+ f[base + 11] = Math.max(0, Math.min(maxR, rBL));
3714
+ f[base + 12] = Math.max(0, Math.min(maxR, borderW));
3715
+ f[base + 13] = pixelStep;
3716
+ u[base + 14] = fillAbgr;
3717
+ u[base + 15] = borderAbgr;
2881
3718
  this.sdfCount++;
2882
3719
  }
3720
+ renderPanel(panel, m, alpha) {
3721
+ const w = panel.width;
3722
+ const h = panel.height;
3723
+ if (w <= 0 || h <= 0) return;
3724
+ this.pushSdfInstance(m, -panel.displayOriginX, -panel.displayOriginY, w, h, panel.radiusTL, panel.radiusTR, panel.radiusBR, panel.radiusBL, panel.borderWidth, panel.pixelStep, packColorAbgr(panel.fillColor, panel.fillAlpha * alpha), packColorAbgr(panel.borderColor, panel.borderAlpha * alpha), panel.blendMode);
3725
+ }
2883
3726
  renderGraphics(g, m, alpha) {
2884
3727
  const ops = g.ops;
2885
3728
  const nums = g.nums;
@@ -2922,17 +3765,18 @@ var RendererWebGpu = class extends EventEmitter {
2922
3765
  lineRgb = nums[ni++];
2923
3766
  lineA = nums[ni++];
2924
3767
  break;
2925
- case 3:
2926
- case 5: {
3768
+ case 3: {
2927
3769
  const x = nums[ni++], y = nums[ni++], w = nums[ni++], h = nums[ni++];
2928
- if (op === 5) ni++;
2929
3770
  if (fillA * alpha > 0) this.pushQuad(m, x, y, x + w, y + h, 0, 0, 1, 1, packColorAbgr(fillRgb, fillA * alpha), 2, g.blendMode, this.dummyEntry.bindGroupLinear);
2930
3771
  break;
2931
3772
  }
2932
- case 4:
2933
- case 6: {
3773
+ case 5: {
3774
+ const x = nums[ni++], y = nums[ni++], w = nums[ni++], h = nums[ni++], r = nums[ni++];
3775
+ if (fillA * alpha > 0 && w > 0 && h > 0) this.pushSdfInstance(m, x, y, w, h, r, r, r, r, 0, 0, packColorAbgr(fillRgb, fillA * alpha), 0, g.blendMode);
3776
+ break;
3777
+ }
3778
+ case 4: {
2934
3779
  const x = nums[ni++], y = nums[ni++], w = nums[ni++], h = nums[ni++];
2935
- if (op === 6) ni++;
2936
3780
  if (lineA * alpha > 0 && lineW > 0) {
2937
3781
  const abgr = packColorAbgr(lineRgb, lineA * alpha);
2938
3782
  lineSeg(x, y, x + w, y, abgr);
@@ -2942,34 +3786,59 @@ var RendererWebGpu = class extends EventEmitter {
2942
3786
  }
2943
3787
  break;
2944
3788
  }
2945
- case 7:
3789
+ case 6: {
3790
+ const x = nums[ni++], y = nums[ni++], w = nums[ni++], h = nums[ni++], r = nums[ni++];
3791
+ if (lineA * alpha > 0 && lineW > 0 && w > 0 && h > 0) {
3792
+ const hw = lineW * .5, rad = Math.max(0, r) + hw;
3793
+ this.pushSdfInstance(m, x - hw, y - hw, w + lineW, h + lineW, rad, rad, rad, rad, lineW, 0, 0, packColorAbgr(lineRgb, lineA * alpha), g.blendMode);
3794
+ }
3795
+ break;
3796
+ }
3797
+ case 7: {
3798
+ const cx = nums[ni++], cy = nums[ni++], r = nums[ni++];
3799
+ if (fillA * alpha > 0 && r > 0) this.pushSdfInstance(m, cx - r, cy - r, r * 2, r * 2, r, r, r, r, 0, 0, packColorAbgr(fillRgb, fillA * alpha), 0, g.blendMode);
3800
+ break;
3801
+ }
2946
3802
  case 9: {
2947
- const cx = nums[ni++], cy = nums[ni++];
2948
- const rx = op === 7 ? nums[ni++] : nums[ni++] * .5;
2949
- const ry = op === 7 ? rx : nums[ni++] * .5;
3803
+ const cx = nums[ni++], cy = nums[ni++], w = nums[ni++], h = nums[ni++];
3804
+ const rx = w * .5, ry = h * .5;
2950
3805
  if (fillA * alpha > 0 && rx > 0 && ry > 0) {
2951
- const segs = Math.max(12, Math.min(48, Math.ceil(Math.max(rx, ry) * 1.4)));
2952
- const abgr = packColorAbgr(fillRgb, fillA * alpha);
2953
- for (let s = 0; s < segs; s++) {
2954
- const a0 = s / segs * Math.PI * 2;
2955
- const a1 = (s + 1) / segs * Math.PI * 2;
2956
- this.pushTri(m, cx, cy, cx + Math.cos(a0) * rx, cy + Math.sin(a0) * ry, cx + Math.cos(a1) * rx, cy + Math.sin(a1) * ry, abgr, g.blendMode);
3806
+ if (Math.abs(w - h) <= 1e-4) this.pushSdfInstance(m, cx - rx, cy - ry, w, h, rx, rx, rx, rx, 0, 0, packColorAbgr(fillRgb, fillA * alpha), 0, g.blendMode);
3807
+ else {
3808
+ const segs = Math.max(12, Math.min(48, Math.ceil(Math.max(rx, ry) * 1.4)));
3809
+ const abgr = packColorAbgr(fillRgb, fillA * alpha);
3810
+ for (let s = 0; s < segs; s++) {
3811
+ const a0 = s / segs * Math.PI * 2;
3812
+ const a1 = (s + 1) / segs * Math.PI * 2;
3813
+ this.pushTri(m, cx, cy, cx + Math.cos(a0) * rx, cy + Math.sin(a0) * ry, cx + Math.cos(a1) * rx, cy + Math.sin(a1) * ry, abgr, g.blendMode);
3814
+ }
2957
3815
  }
2958
3816
  }
2959
3817
  break;
2960
3818
  }
2961
- case 8:
3819
+ case 8: {
3820
+ const cx = nums[ni++], cy = nums[ni++], r = nums[ni++];
3821
+ if (lineA * alpha > 0 && lineW > 0 && r > 0) {
3822
+ const ro = r + lineW * .5;
3823
+ this.pushSdfInstance(m, cx - ro, cy - ro, ro * 2, ro * 2, ro, ro, ro, ro, lineW, 0, 0, packColorAbgr(lineRgb, lineA * alpha), g.blendMode);
3824
+ }
3825
+ break;
3826
+ }
2962
3827
  case 10: {
2963
- const cx = nums[ni++], cy = nums[ni++];
2964
- const rx = op === 8 ? nums[ni++] : nums[ni++] * .5;
2965
- const ry = op === 8 ? rx : nums[ni++] * .5;
3828
+ const cx = nums[ni++], cy = nums[ni++], w = nums[ni++], h = nums[ni++];
3829
+ const rx = w * .5, ry = h * .5;
2966
3830
  if (lineA * alpha > 0 && lineW > 0 && rx > 0 && ry > 0) {
2967
- const segs = Math.max(12, Math.min(48, Math.ceil(Math.max(rx, ry) * 1.4)));
2968
- const abgr = packColorAbgr(lineRgb, lineA * alpha);
2969
- for (let s = 0; s < segs; s++) {
2970
- const a0 = s / segs * Math.PI * 2;
2971
- const a1 = (s + 1) / segs * Math.PI * 2;
2972
- lineSeg(cx + Math.cos(a0) * rx, cy + Math.sin(a0) * ry, cx + Math.cos(a1) * rx, cy + Math.sin(a1) * ry, abgr);
3831
+ if (Math.abs(w - h) <= 1e-4) {
3832
+ const ro = rx + lineW * .5;
3833
+ this.pushSdfInstance(m, cx - ro, cy - ro, ro * 2, ro * 2, ro, ro, ro, ro, lineW, 0, 0, packColorAbgr(lineRgb, lineA * alpha), g.blendMode);
3834
+ } else {
3835
+ const segs = Math.max(12, Math.min(48, Math.ceil(Math.max(rx, ry) * 1.4)));
3836
+ const abgr = packColorAbgr(lineRgb, lineA * alpha);
3837
+ for (let s = 0; s < segs; s++) {
3838
+ const a0 = s / segs * Math.PI * 2;
3839
+ const a1 = (s + 1) / segs * Math.PI * 2;
3840
+ lineSeg(cx + Math.cos(a0) * rx, cy + Math.sin(a0) * ry, cx + Math.cos(a1) * rx, cy + Math.sin(a1) * ry, abgr);
3841
+ }
2973
3842
  }
2974
3843
  }
2975
3844
  break;
@@ -3084,7 +3953,7 @@ var RendererWebGpu = class extends EventEmitter {
3084
3953
  }
3085
3954
  case "Rectangle": {
3086
3955
  const r = obj;
3087
- if (r.isFilled && r.fillAlpha * effAlpha > 0) {
3956
+ if (r.width > 0 && r.height > 0 && r.fillAlpha * effAlpha > 0) {
3088
3957
  const ox = r.displayOriginX, oy = r.displayOriginY;
3089
3958
  this.pushQuad(m, -ox, -oy, r.width - ox, r.height - oy, 0, 0, 1, 1, packColorAbgr(r.fillColor, r.fillAlpha * effAlpha), 2, r.blendMode, this.dummyEntry.bindGroupLinear);
3090
3959
  }
@@ -3161,6 +4030,48 @@ var RendererWebGpu = class extends EventEmitter {
3161
4030
  }
3162
4031
  break;
3163
4032
  }
4033
+ case "ShapeBatch": {
4034
+ const batch = obj;
4035
+ const cols = batch.columns;
4036
+ if (!cols || cols.count <= 0) break;
4037
+ const n = Math.min(cols.count, batch.maxCount, cols.x.length, cols.y.length);
4038
+ const wCol = typeof cols.width === "object" && cols.width ? cols.width : null;
4039
+ const wDef = typeof cols.width === "number" ? cols.width : batch.defaultWidth;
4040
+ const hCol = typeof cols.height === "object" && cols.height ? cols.height : null;
4041
+ const hDef = typeof cols.height === "number" ? cols.height : typeof cols.width === "number" ? cols.width : batch.defaultHeight;
4042
+ const rCol = typeof cols.radius === "object" && cols.radius ? cols.radius : null;
4043
+ const rDef = typeof cols.radius === "number" ? cols.radius : batch.defaultRadius;
4044
+ const fcCol = typeof cols.fillColor === "object" && cols.fillColor ? cols.fillColor : null;
4045
+ const fcDef = typeof cols.fillColor === "number" ? cols.fillColor : batch.defaultFillColor;
4046
+ const faCol = typeof cols.fillAlpha === "object" && cols.fillAlpha ? cols.fillAlpha : null;
4047
+ const faDef = typeof cols.fillAlpha === "number" ? cols.fillAlpha : batch.defaultFillAlpha;
4048
+ const bcCol = typeof cols.borderColor === "object" && cols.borderColor ? cols.borderColor : null;
4049
+ const bcDef = typeof cols.borderColor === "number" ? cols.borderColor : batch.defaultBorderColor;
4050
+ const baCol = typeof cols.borderAlpha === "object" && cols.borderAlpha ? cols.borderAlpha : null;
4051
+ const baDef = typeof cols.borderAlpha === "number" ? cols.borderAlpha : batch.defaultBorderAlpha;
4052
+ const bwCol = typeof cols.borderWidth === "object" && cols.borderWidth ? cols.borderWidth : null;
4053
+ const bwDef = typeof cols.borderWidth === "number" ? cols.borderWidth : batch.defaultBorderWidth;
4054
+ const rotCol = cols.rotation ?? null;
4055
+ const ordCol = cols.order ?? null;
4056
+ for (let i = 0; i < n; i++) {
4057
+ const s = ordCol ? ordCol[i] : i;
4058
+ const w = wCol ? wCol[s] : wDef;
4059
+ const h = hCol ? hCol[s] : wCol ? w : hDef;
4060
+ if (w <= 0 || h <= 0) continue;
4061
+ const fa = (faCol ? faCol[s] : faDef) * effAlpha;
4062
+ const bw = bwCol ? bwCol[s] : bwDef;
4063
+ const ba = bw > 0 ? (baCol ? baCol[s] : baDef) * effAlpha : 0;
4064
+ if (fa <= 0 && ba <= 0) continue;
4065
+ const rad = rCol ? rCol[s] : rDef;
4066
+ const fc = fcCol ? fcCol[s] : fcDef;
4067
+ const bc = bcCol ? bcCol[s] : bcDef;
4068
+ const rot = rotCol ? rotCol[s] : 0;
4069
+ const im = depth + 1 >= this.matPool.length ? createMatrix2D() : this.matPool[depth + 1];
4070
+ multiplyMat(m, cols.x[s], cols.y[s], 1, 1, rot, im);
4071
+ this.pushSdfInstance(im, -w * batch.originX, -h * batch.originY, w, h, rad, rad, rad, rad, bw, batch.pixelStep, packColorAbgr(fc, fa), bw > 0 ? packColorAbgr(bc, ba) : 0, batch.blendMode);
4072
+ }
4073
+ break;
4074
+ }
3164
4075
  case "Container": {
3165
4076
  const cont = obj;
3166
4077
  const mask = cont.clipMask;
@@ -3296,21 +4207,30 @@ var RendererWebGpu = class extends EventEmitter {
3296
4207
  const tex = textures.addCanvas(key, rtCanvas);
3297
4208
  if (!tex) throw new Error("Stage: failed to register WebGPU RenderTarget texture");
3298
4209
  tex.source[0]?.setFilter(filterMode);
4210
+ const rtCamera = new Camera(w, h, true);
3299
4211
  const rtRenderer = new Renderer2D(rtCanvas, w, h);
3300
4212
  return {
3301
4213
  key,
3302
4214
  width: w,
3303
4215
  height: h,
3304
4216
  texture: tex,
4217
+ camera: rtCamera,
3305
4218
  _rtRenderer: rtRenderer,
4219
+ readPixels: (out) => {
4220
+ const byteLen = w * h * 4;
4221
+ const dst = out && out.length >= byteLen ? out : new Uint8Array(byteLen);
4222
+ const img = rtRenderer.ctx.getImageData(0, 0, w, h);
4223
+ dst.set(img.data.subarray(0, byteLen));
4224
+ return dst;
4225
+ },
3306
4226
  destroy: () => {
3307
4227
  rtRenderer.destroy();
3308
4228
  textures.remove(key);
3309
4229
  }
3310
4230
  };
3311
4231
  }
3312
- /** Render a `DisplayList` (or callback) into an offscreen `StageRenderTarget` and mark its texture dirty for WebGPU upload. */
3313
- renderToTarget(target, content, camera) {
4232
+ /** Render a `DisplayList` (or callback) into an offscreen `StageRenderTarget` and mark its texture dirty for WebGPU upload (`camera` defaults to `target.camera`). */
4233
+ renderToTarget(target, content, camera = target.camera) {
3314
4234
  const sub = target._rtRenderer;
3315
4235
  if (!sub) return;
3316
4236
  if (typeof content === "function") {
@@ -3333,4 +4253,4 @@ var RendererWebGpu = class extends EventEmitter {
3333
4253
  } catch {}
3334
4254
  }
3335
4255
  };
3336
- export { TileSpriteObject as _, Container as a, DisplayObject as c, InstancedBatchObject as d, MsdfIconObject as f, TextObject as g, RectangleObject as h, BlitterObject as i, Graphics as l, PanelObject as m, requestWebGpuDevice as n, CustomPassObject as o, NineSliceObject as p, Renderer2D as r, DisplayList as s, RendererWebGpu as t, ImageObject as u };
4256
+ export { TextObject as _, Container as a, DisplayObject as c, InstancedBatchObject as d, MsdfIconObject as f, ShapeBatchObject as g, RectangleObject as h, BlitterObject as i, Graphics as l, PanelObject as m, requestWebGpuDevice as n, CustomPassObject as o, NineSliceObject as p, Renderer2D as r, DisplayList as s, RendererWebGpu as t, ImageObject as u, TileSpriteObject as v, Camera as y };