@holtjs/stage 0.5.2 → 0.7.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 = [];
@@ -329,16 +637,42 @@ var ImageObject = class extends DisplayObject {
329
637
  cropW = 0;
330
638
  cropH = 0;
331
639
  customRenderNodes = {};
332
- constructor(scene, x, y, key, frame) {
333
- super(scene, "Image");
640
+ constructor(scene, x, y, key, frame, nodeType = "Image") {
641
+ super(scene, nodeType);
334
642
  this.x = x;
335
643
  this.y = y;
336
644
  this.texture = scene.textures.get(key);
337
645
  this.frame = this.texture.get(frame);
338
646
  this.width = this.frame.width;
339
647
  this.height = this.frame.height;
340
- this._displayOriginX = this.width * this.originX;
341
- this._displayOriginY = this.height * this.originY;
648
+ this._syncFrameOrigin();
649
+ }
650
+ get displayOriginX() {
651
+ if (!this.customOrigin && this.frame?.customPivot) return this.frame.pixelPivotX;
652
+ return super.displayOriginX;
653
+ }
654
+ get displayOriginY() {
655
+ if (!this.customOrigin && this.frame?.customPivot) return this.frame.pixelPivotY;
656
+ return super.displayOriginY;
657
+ }
658
+ _syncFrameOrigin() {
659
+ if (!this.customOrigin) {
660
+ if (this.frame.customPivot) {
661
+ this._displayOriginX = this.frame.pixelPivotX;
662
+ this._displayOriginY = this.frame.pixelPivotY;
663
+ this.originX = this.width > 0 ? this._displayOriginX / this.width : .5;
664
+ this.originY = this.height > 0 ? this._displayOriginY / this.height : .5;
665
+ } else {
666
+ this._displayOriginX = this.width * this.originX;
667
+ this._displayOriginY = this.height * this.originY;
668
+ }
669
+ }
670
+ }
671
+ /** Clear any manual `setOrigin()` override so per-frame baked pivots (`ax/ay` or `pivotX/pivotY`) drive alignment automatically. */
672
+ useFramePivot() {
673
+ this.customOrigin = false;
674
+ this._syncFrameOrigin();
675
+ return this;
342
676
  }
343
677
  setTexture(key, frame) {
344
678
  this.texture = this.scene.textures.get(key);
@@ -348,12 +682,66 @@ var ImageObject = class extends DisplayObject {
348
682
  this.frame = this.texture.get(frame);
349
683
  this.width = this.frame.width;
350
684
  this.height = this.frame.height;
351
- if (!this.customOrigin) {
352
- this._displayOriginX = this.width * this.originX;
353
- this._displayOriginY = this.height * this.originY;
354
- }
685
+ this._syncFrameOrigin();
355
686
  return this;
356
687
  }
688
+ /**
689
+ * Compute the exact transformed coordinates of a named per-frame pixel socket (e.g. `'hand'`, `'head'`, `'emissive'`)
690
+ * accounting for trimmed `displayOrigin`, `flipX`/`flipY`, `scaleX`/`scaleY`, `rotation`, and ancestor `Container`s.
691
+ */
692
+ getAnchor(name, space = "parent", out = {
693
+ x: 0,
694
+ y: 0
695
+ }) {
696
+ const raw = this.frame?.getAnchor(name);
697
+ if (!raw) return null;
698
+ const ox = this._displayOriginX;
699
+ const oy = this._displayOriginY;
700
+ const pivotMirrored = this.frame.customPivot && !this.customOrigin && !this.isCropped;
701
+ const lx = this.flipX ? pivotMirrored ? ox - raw[0] : this.width - ox - raw[0] : raw[0] - ox;
702
+ const ly = this.flipY ? pivotMirrored ? oy - raw[1] : this.height - oy - raw[1] : raw[1] - oy;
703
+ if (space === "local") {
704
+ out.x = lx;
705
+ out.y = ly;
706
+ return out;
707
+ }
708
+ const sx = lx * this.scaleX;
709
+ const sy = ly * this.scaleY;
710
+ let px;
711
+ let py;
712
+ if (this.rotation === 0) {
713
+ px = this.x + sx;
714
+ py = this.y + sy;
715
+ } else {
716
+ const c = Math.cos(this.rotation);
717
+ const s = Math.sin(this.rotation);
718
+ px = this.x + sx * c - sy * s;
719
+ py = this.y + sx * s + sy * c;
720
+ }
721
+ if (space === "parent" || !this.parentContainer) {
722
+ out.x = px;
723
+ out.y = py;
724
+ return out;
725
+ }
726
+ let cur = this.parentContainer;
727
+ while (cur) {
728
+ const csx = px * cur.scaleX;
729
+ const csy = py * cur.scaleY;
730
+ if (cur.rotation === 0) {
731
+ px = cur.x + csx;
732
+ py = cur.y + csy;
733
+ } else {
734
+ const cc = Math.cos(cur.rotation);
735
+ const cs = Math.sin(cur.rotation);
736
+ px = cur.x + csx * cc - csy * cs;
737
+ py = cur.y + csx * cs + csy * cc;
738
+ }
739
+ cur = cur.parentContainer;
740
+ }
741
+ out.x = px;
742
+ out.y = py;
743
+ return out;
744
+ }
357
745
  setTint(color) {
358
746
  this.tint = color & HEX_MASK;
359
747
  this.isTinted = true;
@@ -387,6 +775,226 @@ var ImageObject = class extends DisplayObject {
387
775
  return this;
388
776
  }
389
777
  };
778
+ /**
779
+ * First-class animated 2D / 2.5D sprite (`scene.add.sprite(x, y, key, frame)`).
780
+ * - Automatically aligns per-frame trimmed pivots (`ax/ay`) & transforms named sockets (`getAnchor('hand')`)
781
+ * - Deterministic simulation-stepped clip playback (`play`, `playDirectional`, `stop`, `pause`, `resume`)
782
+ * - Phase-preserving 4/8-directional locomotion (`playDirectional`) aligned with `Camera25D` & `@holtjs/craft`
783
+ * - Exact frame-crossing event callbacks (`onAnimEvent` for `footstep`, `hit`, etc.)
784
+ */
785
+ var SpriteObject = class extends ImageObject {
786
+ _anim = null;
787
+ _frameIdx = 0;
788
+ _frameElapsedMs = 0;
789
+ _forward = true;
790
+ _repeatsLeft = 0;
791
+ _playing = false;
792
+ _paused = false;
793
+ _timeScale = 1;
794
+ _activeDirBase = null;
795
+ _activeDirIndex = -1;
796
+ _animListeners = /* @__PURE__ */ new Set();
797
+ constructor(scene, x, y, key, frame) {
798
+ super(scene, x, y, key, frame, "Sprite");
799
+ }
800
+ get currentAnim() {
801
+ return this._anim;
802
+ }
803
+ get currentAnimKey() {
804
+ return this._anim?.key ?? null;
805
+ }
806
+ get currentFrameIndex() {
807
+ return this._frameIdx;
808
+ }
809
+ get currentDirection() {
810
+ return this._activeDirIndex;
811
+ }
812
+ get isPlaying() {
813
+ return this._playing && !this._paused;
814
+ }
815
+ get timeScale() {
816
+ return this._timeScale;
817
+ }
818
+ setTimeScale(scale) {
819
+ this._timeScale = Number.isFinite(scale) ? Math.max(0, scale) : 1;
820
+ return this;
821
+ }
822
+ onAnimEvent(fn) {
823
+ this._animListeners.add(fn);
824
+ return () => {
825
+ this._animListeners.delete(fn);
826
+ };
827
+ }
828
+ /** Normalized progress `[0..1)` through the current forward pass of the active animation. */
829
+ get cycleProgress() {
830
+ const anim = this._anim;
831
+ if (!anim || anim.totalDurationMs <= 0 || anim.frames.length === 0) return 0;
832
+ const cf = anim.frames[this._frameIdx];
833
+ if (!cf) return 0;
834
+ return Math.max(0, Math.min(.999999, (cf.offsetMs + this._frameElapsedMs) / anim.totalDurationMs));
835
+ }
836
+ /** Play a registered animation clip by key (or `StageAnimation` object). Fails fast if `key` does not exist! */
837
+ play(keyOrAnim, opts) {
838
+ const options = typeof opts === "boolean" ? { ignoreIfPlaying: opts } : opts ?? {};
839
+ const nextAnim = this.scene.anims.resolveOrThrow(keyOrAnim);
840
+ if (options.timeScale !== void 0) this.setTimeScale(options.timeScale);
841
+ if (options.ignoreIfPlaying && this._playing && this._anim === nextAnim) return this;
842
+ const prevPhase = options.preservePhase && this._anim ? this.cycleProgress : null;
843
+ this._activeDirBase = null;
844
+ this._anim = nextAnim;
845
+ this._playing = true;
846
+ this._paused = false;
847
+ this._forward = true;
848
+ this._repeatsLeft = options.repeat !== void 0 ? options.repeat : nextAnim.repeat;
849
+ if (prevPhase !== null && nextAnim.frames.length > 0) {
850
+ const targetMs = prevPhase * nextAnim.totalDurationMs;
851
+ let idx = 0;
852
+ while (idx + 1 < nextAnim.frames.length && nextAnim.frames[idx + 1].offsetMs <= targetMs) idx++;
853
+ this._frameIdx = idx;
854
+ this._frameElapsedMs = Math.max(0, targetMs - nextAnim.frames[idx].offsetMs);
855
+ this._applyAnimFrame(idx, false);
856
+ } else {
857
+ const startIdx = Math.max(0, Math.min(nextAnim.frames.length - 1, Math.floor(options.startFrame ?? 0)));
858
+ this._frameIdx = startIdx;
859
+ this._frameElapsedMs = 0;
860
+ this._applyAnimFrame(startIdx, true);
861
+ }
862
+ this.scene._registerAnimatedSprite(this);
863
+ return this;
864
+ }
865
+ /**
866
+ * Play a 4- or 8-directional animation group (e.g. `'survivor/walk'` or `'zombie/attack'`) quantized by
867
+ * world movement angle `headingRad = Math.atan2(vy, vx)` and optional `Camera25D.yaw`!
868
+ *
869
+ * Automatically preserves stride phase when turning between directions of the same clip (`walk/d0 -> walk/d1`)
870
+ * so characters never freeze on frame 0 or stutter mid-step!
871
+ */
872
+ playDirectional(baseKey, headingRad, opts = {}) {
873
+ const grp = this.scene.anims.resolveDirectionalOrThrow(baseKey);
874
+ const dirs = Math.max(1, grp.directions);
875
+ const step = Math.PI * 2 / dirs;
876
+ const camYaw = opts.cameraYaw ?? 0;
877
+ const atlasYaw = opts.atlasYaw ?? grp.atlasYaw;
878
+ const effective = (Number.isFinite(headingRad) ? headingRad : 0) - camYaw + atlasYaw;
879
+ const dirIdx = (Math.round(effective / step) % dirs + dirs) % dirs;
880
+ const targetAnim = grp.dirClips[dirIdx] ?? grp.dirClips[0];
881
+ const sameBase = this._playing && this._activeDirBase === grp.baseKey;
882
+ this._activeDirIndex = dirIdx;
883
+ if (sameBase && this._anim === targetAnim) {
884
+ if (opts.timeScale !== void 0) this.setTimeScale(opts.timeScale);
885
+ this._activeDirBase = grp.baseKey;
886
+ return this;
887
+ }
888
+ this.play(targetAnim, {
889
+ ignoreIfPlaying: false,
890
+ preservePhase: sameBase ? opts.preservePhase !== false : Boolean(opts.preservePhase),
891
+ timeScale: opts.timeScale,
892
+ repeat: opts.repeat
893
+ });
894
+ this._activeDirBase = grp.baseKey;
895
+ return this;
896
+ }
897
+ pause() {
898
+ this._paused = true;
899
+ return this;
900
+ }
901
+ resume() {
902
+ this._paused = false;
903
+ return this;
904
+ }
905
+ stop() {
906
+ this._playing = false;
907
+ this._paused = false;
908
+ this._activeDirBase = null;
909
+ this.scene._unregisterAnimatedSprite(this);
910
+ return this;
911
+ }
912
+ /** Internal deterministic simulation step invoked by `Scene._stepAnimations(dt)`. */
913
+ _stepAnim(deltaMs) {
914
+ if (!this._playing || this._paused || !this._anim) return;
915
+ const scaled = deltaMs * this._timeScale;
916
+ if (scaled <= 0) return;
917
+ const anim = this._anim;
918
+ const frames = anim.frames;
919
+ const n = frames.length;
920
+ if (n === 0) return;
921
+ this._frameElapsedMs += scaled;
922
+ let safety = 0;
923
+ while (this._playing && this._anim === anim && safety++ < 64) {
924
+ const cur = frames[this._frameIdx];
925
+ const dur = Math.max(.001, cur.durationMs);
926
+ if (this._frameElapsedMs < dur) break;
927
+ this._frameElapsedMs -= dur;
928
+ if (n === 1) {
929
+ if (this._repeatsLeft === 0) {
930
+ this._finishAnim(anim);
931
+ return;
932
+ }
933
+ if (this._repeatsLeft > 0) this._repeatsLeft--;
934
+ this._applyAnimFrame(0, true);
935
+ this.emit("animrepeat", anim.key, this);
936
+ continue;
937
+ }
938
+ if (this._forward) {
939
+ if (this._frameIdx + 1 < n) {
940
+ this._frameIdx++;
941
+ this._applyAnimFrame(this._frameIdx, true);
942
+ } else if (anim.yoyo) {
943
+ this._forward = false;
944
+ this._frameIdx = n - 2;
945
+ this._applyAnimFrame(this._frameIdx, true);
946
+ } else if (this._repeatsLeft === 0) {
947
+ this._finishAnim(anim);
948
+ return;
949
+ } else {
950
+ if (this._repeatsLeft > 0) this._repeatsLeft--;
951
+ this._frameIdx = 0;
952
+ this._applyAnimFrame(0, true);
953
+ this.emit("animrepeat", anim.key, this);
954
+ }
955
+ } else if (this._frameIdx - 1 >= 0) {
956
+ this._frameIdx--;
957
+ this._applyAnimFrame(this._frameIdx, true);
958
+ } else if (this._repeatsLeft === 0) {
959
+ this._finishAnim(anim);
960
+ return;
961
+ } else {
962
+ if (this._repeatsLeft > 0) this._repeatsLeft--;
963
+ this._forward = true;
964
+ this._frameIdx = Math.min(1, n - 1);
965
+ this._applyAnimFrame(this._frameIdx, true);
966
+ this.emit("animrepeat", anim.key, this);
967
+ }
968
+ }
969
+ }
970
+ _applyAnimFrame(index, fireEvents) {
971
+ const anim = this._anim;
972
+ if (!anim) return;
973
+ const cf = anim.frames[index];
974
+ if (!cf) return;
975
+ if (this.texture.key !== cf.textureKey) this.setTexture(cf.textureKey, cf.frameName);
976
+ else this.setFrame(cf.frameName);
977
+ if (fireEvents && cf.events.length > 0) for (let i = 0; i < cf.events.length; i++) {
978
+ const ev = cf.events[i];
979
+ for (const fn of this._animListeners) fn(ev, anim.key, index, cf.frameName);
980
+ this.emit("animevent", ev, anim.key, index, cf.frameName);
981
+ }
982
+ }
983
+ _finishAnim(anim) {
984
+ this._frameElapsedMs = 0;
985
+ this._playing = false;
986
+ this._activeDirBase = null;
987
+ this.scene._unregisterAnimatedSprite(this);
988
+ this.emit("animcomplete", anim.key, this);
989
+ }
990
+ destroy(fromScene) {
991
+ this.scene._unregisterAnimatedSprite(this);
992
+ this._animListeners.clear();
993
+ this._anim = null;
994
+ this._playing = false;
995
+ super.destroy(fromScene);
996
+ }
997
+ };
390
998
  var NineSliceObject = class extends DisplayObject {
391
999
  texture;
392
1000
  frame;
@@ -956,8 +1564,14 @@ var Graphics = class extends DisplayObject {
956
1564
  numLen = 0;
957
1565
  /** Monotonic geometry revision bumped on every clear/draw op so WebGL can cache static tessellation meshes. */
958
1566
  version = 0;
1567
+ /** True when curved SDF-capable primitives (`fillCircle`, `strokeCircle`, `fillRoundedRect`, `strokeRoundedRect`) are recorded. */
1568
+ _hasSdfShapes = false;
1569
+ /** True when arbitrary polygons, lines, arcs, or eccentric ellipses requiring polygon tessellation are recorded. */
1570
+ _hasNonSdfOps = false;
959
1571
  /** Renderer-owned tessellated local-space mesh cache (opaque to user code). */
960
1572
  _glCache = null;
1573
+ /** Canvas2D retained `Path2D` cache keyed by `version` (opaque to user code). */
1574
+ _c2dCache = null;
961
1575
  /** Last filled rectangle bounds (used O(1) by `Pane` and `ScrollList` rect clip masks). */
962
1576
  maskRect = null;
963
1577
  constructor(scene) {
@@ -988,6 +1602,8 @@ var Graphics = class extends DisplayObject {
988
1602
  if (this.opLen !== 0 || this.numLen !== 0 || this.maskRect !== null) {
989
1603
  this.opLen = 0;
990
1604
  this.numLen = 0;
1605
+ this._hasSdfShapes = false;
1606
+ this._hasNonSdfOps = false;
991
1607
  this.maskRect = null;
992
1608
  this.version++;
993
1609
  }
@@ -1044,6 +1660,7 @@ var Graphics = class extends DisplayObject {
1044
1660
  }
1045
1661
  fillRoundedRect(x, y, w, h, radius = 8) {
1046
1662
  const r = typeof radius === "number" ? radius : radius.tl ?? radius.tr ?? 8;
1663
+ this._hasSdfShapes = true;
1047
1664
  const i = this.pushOp(5, 5);
1048
1665
  this.nums[i] = x;
1049
1666
  this.nums[i + 1] = y;
@@ -1054,6 +1671,7 @@ var Graphics = class extends DisplayObject {
1054
1671
  }
1055
1672
  strokeRoundedRect(x, y, w, h, radius = 8) {
1056
1673
  const r = typeof radius === "number" ? radius : radius.tl ?? radius.tr ?? 8;
1674
+ this._hasSdfShapes = true;
1057
1675
  const i = this.pushOp(6, 5);
1058
1676
  this.nums[i] = x;
1059
1677
  this.nums[i + 1] = y;
@@ -1063,6 +1681,7 @@ var Graphics = class extends DisplayObject {
1063
1681
  return this;
1064
1682
  }
1065
1683
  fillCircle(x, y, r) {
1684
+ this._hasSdfShapes = true;
1066
1685
  const i = this.pushOp(7, 3);
1067
1686
  this.nums[i] = x;
1068
1687
  this.nums[i + 1] = y;
@@ -1070,6 +1689,7 @@ var Graphics = class extends DisplayObject {
1070
1689
  return this;
1071
1690
  }
1072
1691
  strokeCircle(x, y, r) {
1692
+ this._hasSdfShapes = true;
1073
1693
  const i = this.pushOp(8, 3);
1074
1694
  this.nums[i] = x;
1075
1695
  this.nums[i + 1] = y;
@@ -1077,6 +1697,8 @@ var Graphics = class extends DisplayObject {
1077
1697
  return this;
1078
1698
  }
1079
1699
  fillEllipse(x, y, width, height) {
1700
+ if (Math.abs(width - height) <= 1e-4) this._hasSdfShapes = true;
1701
+ else this._hasNonSdfOps = true;
1080
1702
  const i = this.pushOp(9, 4);
1081
1703
  this.nums[i] = x;
1082
1704
  this.nums[i + 1] = y;
@@ -1085,6 +1707,8 @@ var Graphics = class extends DisplayObject {
1085
1707
  return this;
1086
1708
  }
1087
1709
  strokeEllipse(x, y, width, height) {
1710
+ if (Math.abs(width - height) <= 1e-4) this._hasSdfShapes = true;
1711
+ else this._hasNonSdfOps = true;
1088
1712
  const i = this.pushOp(10, 4);
1089
1713
  this.nums[i] = x;
1090
1714
  this.nums[i + 1] = y;
@@ -1093,6 +1717,7 @@ var Graphics = class extends DisplayObject {
1093
1717
  return this;
1094
1718
  }
1095
1719
  fillTriangle(x0, y0, x1, y1, x2, y2) {
1720
+ this._hasNonSdfOps = true;
1096
1721
  const i = this.pushOp(11, 6);
1097
1722
  this.nums[i] = x0;
1098
1723
  this.nums[i + 1] = y0;
@@ -1103,6 +1728,7 @@ var Graphics = class extends DisplayObject {
1103
1728
  return this;
1104
1729
  }
1105
1730
  strokeTriangle(x0, y0, x1, y1, x2, y2) {
1731
+ this._hasNonSdfOps = true;
1106
1732
  const i = this.pushOp(12, 6);
1107
1733
  this.nums[i] = x0;
1108
1734
  this.nums[i + 1] = y0;
@@ -1113,6 +1739,7 @@ var Graphics = class extends DisplayObject {
1113
1739
  return this;
1114
1740
  }
1115
1741
  lineBetween(x1, y1, x2, y2) {
1742
+ this._hasNonSdfOps = true;
1116
1743
  const i = this.pushOp(13, 4);
1117
1744
  this.nums[i] = x1;
1118
1745
  this.nums[i + 1] = y1;
@@ -1121,22 +1748,26 @@ var Graphics = class extends DisplayObject {
1121
1748
  return this;
1122
1749
  }
1123
1750
  beginPath() {
1751
+ this._hasNonSdfOps = true;
1124
1752
  this.pushOp(14, 0);
1125
1753
  return this;
1126
1754
  }
1127
1755
  moveTo(x, y) {
1756
+ this._hasNonSdfOps = true;
1128
1757
  const i = this.pushOp(15, 2);
1129
1758
  this.nums[i] = x;
1130
1759
  this.nums[i + 1] = y;
1131
1760
  return this;
1132
1761
  }
1133
1762
  lineTo(x, y) {
1763
+ this._hasNonSdfOps = true;
1134
1764
  const i = this.pushOp(16, 2);
1135
1765
  this.nums[i] = x;
1136
1766
  this.nums[i + 1] = y;
1137
1767
  return this;
1138
1768
  }
1139
1769
  arc(x, y, r, startAngle, endAngle, anticlockwise = false) {
1770
+ this._hasNonSdfOps = true;
1140
1771
  const i = this.pushOp(17, 6);
1141
1772
  this.nums[i] = x;
1142
1773
  this.nums[i + 1] = y;
@@ -1147,6 +1778,7 @@ var Graphics = class extends DisplayObject {
1147
1778
  return this;
1148
1779
  }
1149
1780
  slice(x, y, r, startAngle, endAngle, anticlockwise = false) {
1781
+ this._hasNonSdfOps = true;
1150
1782
  const i = this.pushOp(18, 6);
1151
1783
  this.nums[i] = x;
1152
1784
  this.nums[i + 1] = y;
@@ -1198,6 +1830,7 @@ var Graphics = class extends DisplayObject {
1198
1830
  }
1199
1831
  destroy(fromScene) {
1200
1832
  this._glCache = null;
1833
+ this._c2dCache = null;
1201
1834
  super.destroy(fromScene);
1202
1835
  }
1203
1836
  };
@@ -1430,6 +2063,51 @@ var InstancedBatchObject = class extends DisplayObject {
1430
2063
  super.destroy(fromScene);
1431
2064
  }
1432
2065
  };
2066
+ /**
2067
+ * Zero-texture GPU analytical SDF shape batch (`scene.add.shapeBatch(maxCount, defaults?)`).
2068
+ * Renders thousands of dynamic circles, rings, pills, capsules, shadows, health bars, or rounded rects
2069
+ * directly from `SoaWorld` `Float32Array` / `Uint32Array` columns — 1 analytical SDF quad (2 tris) per shape
2070
+ * on WebGL2 & WebGPU with zero sprite atlas baking and zero CPU polygon triangulation.
2071
+ */
2072
+ var ShapeBatchObject = class extends DisplayObject {
2073
+ maxCount;
2074
+ defaultWidth;
2075
+ defaultHeight;
2076
+ defaultRadius;
2077
+ defaultFillColor;
2078
+ defaultFillAlpha;
2079
+ defaultBorderColor;
2080
+ defaultBorderAlpha;
2081
+ defaultBorderWidth;
2082
+ pixelStep;
2083
+ columns = null;
2084
+ constructor(scene, maxCount = 1024, defaults = {}) {
2085
+ super(scene, "ShapeBatch");
2086
+ this.maxCount = Math.max(1, Math.floor(maxCount));
2087
+ this.originX = .5;
2088
+ this.originY = .5;
2089
+ const w = defaults.width ?? 12;
2090
+ const h = defaults.height ?? w;
2091
+ this.defaultWidth = w;
2092
+ this.defaultHeight = h;
2093
+ this.defaultRadius = defaults.radius ?? Math.min(w, h) * .5;
2094
+ this.defaultFillColor = (defaults.fillColor ?? 0xffffff) & HEX_MASK;
2095
+ this.defaultFillAlpha = defaults.fillAlpha ?? 1;
2096
+ this.defaultBorderColor = (defaults.borderColor ?? 0) & HEX_MASK;
2097
+ this.defaultBorderAlpha = defaults.borderAlpha ?? 1;
2098
+ this.defaultBorderWidth = defaults.borderWidth ?? 0;
2099
+ this.pixelStep = defaults.pixelStep ?? 0;
2100
+ }
2101
+ /** Bind or update the SoA column views read on each render pass (zero copying on caller side). */
2102
+ setColumns(columns) {
2103
+ this.columns = columns;
2104
+ return this;
2105
+ }
2106
+ destroy(fromScene) {
2107
+ this.columns = null;
2108
+ super.destroy(fromScene);
2109
+ }
2110
+ };
1433
2111
  // src/renderer-2d.ts
1434
2112
  function addPerCornerRoundedRectSubpath(ctx, x, y, w, h, tl, tr, br, bl) {
1435
2113
  const maxR = Math.min(w, h) * .5;
@@ -1486,12 +2164,16 @@ var Renderer2D = class Renderer2D extends EventEmitter {
1486
2164
  height;
1487
2165
  tintCanvas = null;
1488
2166
  tintCtx = null;
2167
+ tintCache = /* @__PURE__ */ new Map();
1489
2168
  maskWorldMat = createMatrix2D();
2169
+ rootScrollMat = createMatrix2D();
2170
+ matPool = Array.from({ length: 16 }, () => createMatrix2D());
1490
2171
  pathCache = /* @__PURE__ */ new Map();
1491
2172
  curDrawCalls = 0;
1492
2173
  curFlushes = 0;
1493
2174
  curQuads = 0;
1494
2175
  curTris = 0;
2176
+ curCulled = 0;
1495
2177
  curSdfPanels = 0;
1496
2178
  curMsdfIcons = 0;
1497
2179
  lastStats = {
@@ -1530,12 +2212,42 @@ var Renderer2D = class Renderer2D extends EventEmitter {
1530
2212
  this.canvas.width = w;
1531
2213
  this.canvas.height = h;
1532
2214
  }
1533
- getTintedSource(img, sx, sy, sw, sh, rgb, fill) {
2215
+ getTintedSource(img, sx, sy, sw, sh, rgb, fill, texSource) {
1534
2216
  const iw = Math.max(1, Math.ceil(sw));
1535
2217
  const ih = Math.max(1, Math.ceil(sh));
2218
+ const doc = this.canvas.ownerDocument ?? (typeof document !== "undefined" ? document : null);
2219
+ if (!doc) return null;
2220
+ if (texSource && !texSource.isRenderTarget && iw <= 512 && ih <= 512) {
2221
+ const cacheKey = `${texSource.id}:${texSource.version}:${sx},${sy},${sw},${sh}:${rgb}:${fill ? 1 : 0}`;
2222
+ const hit = this.tintCache.get(cacheKey);
2223
+ if (hit) {
2224
+ this.tintCache.delete(cacheKey);
2225
+ this.tintCache.set(cacheKey, hit);
2226
+ return hit;
2227
+ }
2228
+ const entry = doc.createElement("canvas");
2229
+ entry.width = iw;
2230
+ entry.height = ih;
2231
+ const eg = entry.getContext("2d");
2232
+ if (eg) {
2233
+ eg.globalCompositeOperation = "source-over";
2234
+ eg.drawImage(img, sx, sy, sw, sh, 0, 0, iw, ih);
2235
+ eg.globalCompositeOperation = fill ? "source-in" : "multiply";
2236
+ eg.fillStyle = rgbToCss(rgb, 1);
2237
+ eg.fillRect(0, 0, iw, ih);
2238
+ if (!fill) {
2239
+ eg.globalCompositeOperation = "destination-in";
2240
+ eg.drawImage(img, sx, sy, sw, sh, 0, 0, iw, ih);
2241
+ }
2242
+ if (this.tintCache.size >= 256) {
2243
+ const oldest = this.tintCache.keys().next().value;
2244
+ if (oldest !== void 0) this.tintCache.delete(oldest);
2245
+ }
2246
+ this.tintCache.set(cacheKey, entry);
2247
+ return entry;
2248
+ }
2249
+ }
1536
2250
  if (!this.tintCanvas) {
1537
- const doc = this.canvas.ownerDocument ?? (typeof document !== "undefined" ? document : null);
1538
- if (!doc) return null;
1539
2251
  this.tintCanvas = doc.createElement("canvas");
1540
2252
  this.tintCtx = this.tintCanvas.getContext("2d");
1541
2253
  }
@@ -1560,7 +2272,39 @@ var Renderer2D = class Renderer2D extends EventEmitter {
1560
2272
  tg.restore();
1561
2273
  return tc;
1562
2274
  }
1563
- renderImage(img, ctx) {
2275
+ isOffscreen(m, x0, y0, x1, y1) {
2276
+ const a = m.a, b = m.b, c = m.c, d = m.d, tx = m.tx, ty = m.ty;
2277
+ const px0 = a * x0 + c * y0 + tx, py0 = b * x0 + d * y0 + ty;
2278
+ const px1 = a * x1 + c * y0 + tx, py1 = b * x1 + d * y0 + ty;
2279
+ const px2 = a * x1 + c * y1 + tx, py2 = b * x1 + d * y1 + ty;
2280
+ const px3 = a * x0 + c * y1 + tx, py3 = b * x0 + d * y1 + ty;
2281
+ if (px0 < 0 && px1 < 0 && px2 < 0 && px3 < 0) return true;
2282
+ if (py0 < 0 && py1 < 0 && py2 < 0 && py3 < 0) return true;
2283
+ const w = this.width, h = this.height;
2284
+ if (px0 > w && px1 > w && px2 > w && px3 > w) return true;
2285
+ if (py0 > h && py1 > h && py2 > h && py3 > h) return true;
2286
+ return false;
2287
+ }
2288
+ mulMat(parent, x, y, sx, sy, rot, out) {
2289
+ const pa = parent.a, pb = parent.b, pc = parent.c, pd = parent.d;
2290
+ out.tx = pa * x + pc * y + parent.tx;
2291
+ out.ty = pb * x + pd * y + parent.ty;
2292
+ if (rot === 0) {
2293
+ out.a = pa * sx;
2294
+ out.b = pb * sx;
2295
+ out.c = pc * sy;
2296
+ out.d = pd * sy;
2297
+ } else {
2298
+ const cos = Math.cos(rot), sin = Math.sin(rot);
2299
+ const la = cos * sx, lb = sin * sx, lc = -sin * sy, ld = cos * sy;
2300
+ out.a = pa * la + pc * lb;
2301
+ out.b = pb * la + pd * lb;
2302
+ out.c = pa * lc + pc * ld;
2303
+ out.d = pb * lc + pd * ld;
2304
+ }
2305
+ return out;
2306
+ }
2307
+ renderImage(img, ctx, m) {
1564
2308
  const frame = img.frame;
1565
2309
  if (!frame || frame.width <= 0 || frame.height <= 0) return;
1566
2310
  const srcImg = frame.source.image;
@@ -1583,7 +2327,6 @@ var Renderer2D = class Renderer2D extends EventEmitter {
1583
2327
  dh = rh;
1584
2328
  }
1585
2329
  if (img.pixelAA) {
1586
- const m = ctx.getTransform();
1587
2330
  const dispW = Math.hypot(m.a * dw, m.b * dw);
1588
2331
  const dispH = Math.hypot(m.c * dh, m.d * dh);
1589
2332
  const sxRatio = dispW / Math.max(1, sw);
@@ -1591,11 +2334,11 @@ var Renderer2D = class Renderer2D extends EventEmitter {
1591
2334
  ctx.imageSmoothingEnabled = !(sxRatio >= .98 && syRatio >= .98 && Math.abs(sxRatio - Math.round(sxRatio)) <= .02 && Math.abs(syRatio - Math.round(syRatio)) <= .02);
1592
2335
  } else ctx.imageSmoothingEnabled = frame.source.filterMode !== FilterMode.NEAREST;
1593
2336
  if (img.flipX || img.flipY) {
1594
- ctx.translate(img.flipX ? dx * 2 + dw : 0, img.flipY ? dy * 2 + dh : 0);
2337
+ if (!(frame.customPivot && !img.customOrigin && !img.isCropped)) ctx.translate(img.flipX ? dx * 2 + dw : 0, img.flipY ? dy * 2 + dh : 0);
1595
2338
  ctx.scale(img.flipX ? -1 : 1, img.flipY ? -1 : 1);
1596
2339
  }
1597
2340
  if (img.isTinted && (img.tint !== 0xffffff || img.tintMode === TintModes.FILL)) {
1598
- const tinted = this.getTintedSource(srcImg, sx, sy, sw, sh, img.tint, img.tintMode === TintModes.FILL);
2341
+ const tinted = this.getTintedSource(srcImg, sx, sy, sw, sh, img.tint, img.tintMode === TintModes.FILL, frame.source);
1599
2342
  if (tinted) {
1600
2343
  ctx.drawImage(tinted, 0, 0, Math.ceil(sw), Math.ceil(sh), dx, dy, dw, dh);
1601
2344
  this.curDrawCalls++;
@@ -1680,7 +2423,7 @@ var Renderer2D = class Renderer2D extends EventEmitter {
1680
2423
  let drawSrc = srcImg;
1681
2424
  let baseSx = frame.cutX, baseSy = frame.cutY;
1682
2425
  if (ts.isTinted && ts.tint !== 0xffffff) {
1683
- const tinted = this.getTintedSource(srcImg, frame.cutX, frame.cutY, frame.cutWidth, frame.cutHeight, ts.tint, false);
2426
+ const tinted = this.getTintedSource(srcImg, frame.cutX, frame.cutY, frame.cutWidth, frame.cutHeight, ts.tint, false, frame.source);
1684
2427
  if (tinted) {
1685
2428
  drawSrc = tinted;
1686
2429
  baseSx = 0;
@@ -1825,23 +2568,201 @@ var Renderer2D = class Renderer2D extends EventEmitter {
1825
2568
  renderGraphics(g, ctx) {
1826
2569
  if (g.opLen === 0) return;
1827
2570
  this.curDrawCalls++;
2571
+ const cached = g._c2dCache;
2572
+ if (cached && cached.version === g.version) {
2573
+ for (let i = 0; i < cached.runs.length; i++) {
2574
+ const run = cached.runs[i];
2575
+ if (run.kind === "fill") {
2576
+ ctx.fillStyle = run.style;
2577
+ ctx.fill(run.path);
2578
+ } else {
2579
+ ctx.lineWidth = run.lw;
2580
+ ctx.strokeStyle = run.style;
2581
+ ctx.stroke(run.path);
2582
+ }
2583
+ }
2584
+ return;
2585
+ }
2586
+ if (g._c2dSeenVer === g.version && typeof Path2D === "function" && !g._hasNonSdfOps) {
2587
+ const runs = [];
2588
+ let curFillStyle = "";
2589
+ let curFillPath = null;
2590
+ let fillRgb = WHITE_RGB, fillA = 1;
2591
+ let lineW = 1, lineRgb = WHITE_RGB, lineA = 1;
2592
+ const stack = [];
2593
+ const ops = g.ops;
2594
+ const nums = g.nums;
2595
+ let ni = 0;
2596
+ const ensureFillRun = (style, canBatch) => {
2597
+ if (canBatch && curFillPath && curFillStyle === style) return curFillPath;
2598
+ const p = new Path2D();
2599
+ runs.push({
2600
+ kind: "fill",
2601
+ style,
2602
+ lw: 0,
2603
+ path: p
2604
+ });
2605
+ curFillPath = canBatch ? p : null;
2606
+ curFillStyle = canBatch ? style : "";
2607
+ return p;
2608
+ };
2609
+ const pushStrokeRun = (style, lw) => {
2610
+ curFillPath = null;
2611
+ curFillStyle = "";
2612
+ const p = new Path2D();
2613
+ runs.push({
2614
+ kind: "stroke",
2615
+ style,
2616
+ lw,
2617
+ path: p
2618
+ });
2619
+ return p;
2620
+ };
2621
+ const addRRectToPath = (p, x, y, w, h, radius) => {
2622
+ const r = Math.max(0, Math.min(radius, w * .5, h * .5));
2623
+ p.moveTo(x + r, y);
2624
+ p.lineTo(x + w - r, y);
2625
+ if (r > 0) p.quadraticCurveTo(x + w, y, x + w, y + r);
2626
+ p.lineTo(x + w, y + h - r);
2627
+ if (r > 0) p.quadraticCurveTo(x + w, y + h, x + w - r, y + h);
2628
+ p.lineTo(x + r, y + h);
2629
+ if (r > 0) p.quadraticCurveTo(x, y + h, x, y + h - r);
2630
+ p.lineTo(x, y + r);
2631
+ if (r > 0) p.quadraticCurveTo(x, y, x + r, y);
2632
+ p.closePath();
2633
+ };
2634
+ for (let oi = 0; oi < g.opLen; oi++) switch (ops[oi]) {
2635
+ case 22:
2636
+ stack.push(fillRgb, fillA, lineW, lineRgb, lineA);
2637
+ break;
2638
+ case 23:
2639
+ if (stack.length >= 5) {
2640
+ lineA = stack.pop();
2641
+ lineRgb = stack.pop();
2642
+ lineW = stack.pop();
2643
+ fillA = stack.pop();
2644
+ fillRgb = stack.pop();
2645
+ }
2646
+ break;
2647
+ case 1:
2648
+ fillRgb = nums[ni++];
2649
+ fillA = nums[ni++];
2650
+ break;
2651
+ case 2:
2652
+ lineW = nums[ni++];
2653
+ lineRgb = nums[ni++];
2654
+ lineA = nums[ni++];
2655
+ break;
2656
+ case 3: {
2657
+ const x = nums[ni++], y = nums[ni++], w = nums[ni++], h = nums[ni++];
2658
+ if (fillA > 0) ensureFillRun(rgbToCss(fillRgb, fillA), fillA >= 1).rect(x, y, w, h);
2659
+ break;
2660
+ }
2661
+ case 4: {
2662
+ const x = nums[ni++], y = nums[ni++], w = nums[ni++], h = nums[ni++];
2663
+ if (lineA > 0 && lineW > 0) pushStrokeRun(rgbToCss(lineRgb, lineA), lineW).rect(x, y, w, h);
2664
+ break;
2665
+ }
2666
+ case 5: {
2667
+ const x = nums[ni++], y = nums[ni++], w = nums[ni++], h = nums[ni++], r = nums[ni++];
2668
+ if (fillA > 0 && w > 0 && h > 0) addRRectToPath(ensureFillRun(rgbToCss(fillRgb, fillA), fillA >= 1), x, y, w, h, r);
2669
+ break;
2670
+ }
2671
+ case 6: {
2672
+ const x = nums[ni++], y = nums[ni++], w = nums[ni++], h = nums[ni++], r = nums[ni++];
2673
+ if (lineA > 0 && lineW > 0 && w > 0 && h > 0) addRRectToPath(pushStrokeRun(rgbToCss(lineRgb, lineA), lineW), x, y, w, h, r);
2674
+ break;
2675
+ }
2676
+ case 7: {
2677
+ const cx = nums[ni++], cy = nums[ni++], r = nums[ni++];
2678
+ if (fillA > 0 && r > 0) {
2679
+ const p = ensureFillRun(rgbToCss(fillRgb, fillA), fillA >= 1);
2680
+ p.moveTo(cx + r, cy);
2681
+ p.arc(cx, cy, r, 0, Math.PI * 2);
2682
+ }
2683
+ break;
2684
+ }
2685
+ case 8: {
2686
+ const cx = nums[ni++], cy = nums[ni++], r = nums[ni++];
2687
+ if (lineA > 0 && lineW > 0 && r > 0) {
2688
+ const p = pushStrokeRun(rgbToCss(lineRgb, lineA), lineW);
2689
+ p.moveTo(cx + r, cy);
2690
+ p.arc(cx, cy, r, 0, Math.PI * 2);
2691
+ }
2692
+ break;
2693
+ }
2694
+ case 9: {
2695
+ const cx = nums[ni++], cy = nums[ni++], w = nums[ni++], h = nums[ni++];
2696
+ if (fillA > 0 && w > 0 && h > 0) {
2697
+ const p = ensureFillRun(rgbToCss(fillRgb, fillA), fillA >= 1);
2698
+ p.moveTo(cx + w * .5, cy);
2699
+ p.ellipse(cx, cy, w * .5, h * .5, 0, 0, Math.PI * 2);
2700
+ }
2701
+ break;
2702
+ }
2703
+ case 10: {
2704
+ const cx = nums[ni++], cy = nums[ni++], w = nums[ni++], h = nums[ni++];
2705
+ if (lineA > 0 && lineW > 0 && w > 0 && h > 0) {
2706
+ const p = pushStrokeRun(rgbToCss(lineRgb, lineA), lineW);
2707
+ p.moveTo(cx + w * .5, cy);
2708
+ p.ellipse(cx, cy, w * .5, h * .5, 0, 0, Math.PI * 2);
2709
+ }
2710
+ break;
2711
+ }
2712
+ }
2713
+ g._c2dCache = {
2714
+ version: g.version,
2715
+ runs
2716
+ };
2717
+ for (let i = 0; i < runs.length; i++) {
2718
+ const run = runs[i];
2719
+ if (run.kind === "fill") {
2720
+ ctx.fillStyle = run.style;
2721
+ ctx.fill(run.path);
2722
+ } else {
2723
+ ctx.lineWidth = run.lw;
2724
+ ctx.strokeStyle = run.style;
2725
+ ctx.stroke(run.path);
2726
+ }
2727
+ }
2728
+ return;
2729
+ }
2730
+ g._c2dSeenVer = g.version;
1828
2731
  let fillRgb = WHITE_RGB, fillA = 1;
1829
2732
  let lineW = 1, lineRgb = WHITE_RGB, lineA = 1;
2733
+ ctx.fillStyle = rgbToCss(fillRgb, fillA);
2734
+ ctx.lineWidth = lineW;
2735
+ ctx.strokeStyle = rgbToCss(lineRgb, lineA);
2736
+ let batchFillOpen = false;
2737
+ const flushBatchFill = () => {
2738
+ if (batchFillOpen) {
2739
+ ctx.fill();
2740
+ batchFillOpen = false;
2741
+ }
2742
+ };
1830
2743
  const ops = g.ops;
1831
2744
  const nums = g.nums;
1832
2745
  let ni = 0;
1833
2746
  for (let oi = 0; oi < g.opLen; oi++) switch (ops[oi]) {
1834
2747
  case 22:
2748
+ flushBatchFill();
1835
2749
  ctx.save();
1836
2750
  break;
1837
2751
  case 23:
2752
+ flushBatchFill();
1838
2753
  ctx.restore();
1839
2754
  break;
1840
- case 1:
1841
- fillRgb = nums[ni++];
1842
- fillA = nums[ni++];
1843
- ctx.fillStyle = rgbToCss(fillRgb, fillA);
2755
+ case 1: {
2756
+ const nextRgb = nums[ni++];
2757
+ const nextA = nums[ni++];
2758
+ if (nextRgb !== fillRgb || nextA !== fillA) {
2759
+ flushBatchFill();
2760
+ fillRgb = nextRgb;
2761
+ fillA = nextA;
2762
+ ctx.fillStyle = rgbToCss(fillRgb, fillA);
2763
+ }
1844
2764
  break;
2765
+ }
1845
2766
  case 2:
1846
2767
  lineW = nums[ni++];
1847
2768
  lineRgb = nums[ni++];
@@ -1851,17 +2772,30 @@ var Renderer2D = class Renderer2D extends EventEmitter {
1851
2772
  break;
1852
2773
  case 3: {
1853
2774
  const x = nums[ni++], y = nums[ni++], w = nums[ni++], h = nums[ni++];
1854
- if (fillA > 0) ctx.fillRect(x, y, w, h);
2775
+ if (fillA >= 1) {
2776
+ if (!batchFillOpen) {
2777
+ ctx.beginPath();
2778
+ batchFillOpen = true;
2779
+ }
2780
+ ctx.rect(x, y, w, h);
2781
+ } else if (fillA > 0) {
2782
+ flushBatchFill();
2783
+ ctx.fillRect(x, y, w, h);
2784
+ }
1855
2785
  break;
1856
2786
  }
1857
2787
  case 4: {
1858
2788
  const x = nums[ni++], y = nums[ni++], w = nums[ni++], h = nums[ni++];
1859
- if (lineA > 0 && lineW > 0) ctx.strokeRect(x, y, w, h);
2789
+ if (lineA > 0 && lineW > 0) {
2790
+ flushBatchFill();
2791
+ ctx.strokeRect(x, y, w, h);
2792
+ }
1860
2793
  break;
1861
2794
  }
1862
2795
  case 5: {
1863
2796
  const x = nums[ni++], y = nums[ni++], w = nums[ni++], h = nums[ni++], r = nums[ni++];
1864
2797
  if (fillA > 0 && w > 0 && h > 0) {
2798
+ flushBatchFill();
1865
2799
  roundedRectPath(ctx, x, y, w, h, r);
1866
2800
  ctx.fill();
1867
2801
  }
@@ -1870,6 +2804,7 @@ var Renderer2D = class Renderer2D extends EventEmitter {
1870
2804
  case 6: {
1871
2805
  const x = nums[ni++], y = nums[ni++], w = nums[ni++], h = nums[ni++], r = nums[ni++];
1872
2806
  if (lineA > 0 && lineW > 0 && w > 0 && h > 0) {
2807
+ flushBatchFill();
1873
2808
  roundedRectPath(ctx, x, y, w, h, r);
1874
2809
  ctx.stroke();
1875
2810
  }
@@ -1877,7 +2812,15 @@ var Renderer2D = class Renderer2D extends EventEmitter {
1877
2812
  }
1878
2813
  case 7: {
1879
2814
  const cx = nums[ni++], cy = nums[ni++], r = nums[ni++];
1880
- if (fillA > 0 && r > 0) {
2815
+ if (fillA >= 1 && r > 0) {
2816
+ if (!batchFillOpen) {
2817
+ ctx.beginPath();
2818
+ batchFillOpen = true;
2819
+ }
2820
+ ctx.moveTo(cx + r, cy);
2821
+ ctx.arc(cx, cy, r, 0, Math.PI * 2);
2822
+ } else if (fillA > 0 && r > 0) {
2823
+ flushBatchFill();
1881
2824
  ctx.beginPath();
1882
2825
  ctx.arc(cx, cy, r, 0, Math.PI * 2);
1883
2826
  ctx.fill();
@@ -1887,6 +2830,7 @@ var Renderer2D = class Renderer2D extends EventEmitter {
1887
2830
  case 8: {
1888
2831
  const cx = nums[ni++], cy = nums[ni++], r = nums[ni++];
1889
2832
  if (lineA > 0 && lineW > 0 && r > 0) {
2833
+ flushBatchFill();
1890
2834
  ctx.beginPath();
1891
2835
  ctx.arc(cx, cy, r, 0, Math.PI * 2);
1892
2836
  ctx.stroke();
@@ -1895,7 +2839,15 @@ var Renderer2D = class Renderer2D extends EventEmitter {
1895
2839
  }
1896
2840
  case 9: {
1897
2841
  const cx = nums[ni++], cy = nums[ni++], w = nums[ni++], h = nums[ni++];
1898
- if (fillA > 0 && w > 0 && h > 0) {
2842
+ if (fillA >= 1 && w > 0 && h > 0) {
2843
+ if (!batchFillOpen) {
2844
+ ctx.beginPath();
2845
+ batchFillOpen = true;
2846
+ }
2847
+ ctx.moveTo(cx + w * .5, cy);
2848
+ ctx.ellipse(cx, cy, w * .5, h * .5, 0, 0, Math.PI * 2);
2849
+ } else if (fillA > 0 && w > 0 && h > 0) {
2850
+ flushBatchFill();
1899
2851
  ctx.beginPath();
1900
2852
  ctx.ellipse(cx, cy, w * .5, h * .5, 0, 0, Math.PI * 2);
1901
2853
  ctx.fill();
@@ -1905,6 +2857,7 @@ var Renderer2D = class Renderer2D extends EventEmitter {
1905
2857
  case 10: {
1906
2858
  const cx = nums[ni++], cy = nums[ni++], w = nums[ni++], h = nums[ni++];
1907
2859
  if (lineA > 0 && lineW > 0 && w > 0 && h > 0) {
2860
+ flushBatchFill();
1908
2861
  ctx.beginPath();
1909
2862
  ctx.ellipse(cx, cy, w * .5, h * .5, 0, 0, Math.PI * 2);
1910
2863
  ctx.stroke();
@@ -1914,6 +2867,7 @@ var Renderer2D = class Renderer2D extends EventEmitter {
1914
2867
  case 11: {
1915
2868
  const x0 = nums[ni++], y0 = nums[ni++], x1 = nums[ni++], y1 = nums[ni++], x2 = nums[ni++], y2 = nums[ni++];
1916
2869
  if (fillA > 0) {
2870
+ flushBatchFill();
1917
2871
  ctx.beginPath();
1918
2872
  ctx.moveTo(x0, y0);
1919
2873
  ctx.lineTo(x1, y1);
@@ -1926,6 +2880,7 @@ var Renderer2D = class Renderer2D extends EventEmitter {
1926
2880
  case 12: {
1927
2881
  const x0 = nums[ni++], y0 = nums[ni++], x1 = nums[ni++], y1 = nums[ni++], x2 = nums[ni++], y2 = nums[ni++];
1928
2882
  if (lineA > 0 && lineW > 0) {
2883
+ flushBatchFill();
1929
2884
  ctx.beginPath();
1930
2885
  ctx.moveTo(x0, y0);
1931
2886
  ctx.lineTo(x1, y1);
@@ -1938,6 +2893,7 @@ var Renderer2D = class Renderer2D extends EventEmitter {
1938
2893
  case 13: {
1939
2894
  const x0 = nums[ni++], y0 = nums[ni++], x1 = nums[ni++], y1 = nums[ni++];
1940
2895
  if (lineA > 0 && lineW > 0) {
2896
+ flushBatchFill();
1941
2897
  ctx.beginPath();
1942
2898
  ctx.moveTo(x0, y0);
1943
2899
  ctx.lineTo(x1, y1);
@@ -1946,6 +2902,7 @@ var Renderer2D = class Renderer2D extends EventEmitter {
1946
2902
  break;
1947
2903
  }
1948
2904
  case 14:
2905
+ flushBatchFill();
1949
2906
  ctx.beginPath();
1950
2907
  break;
1951
2908
  case 15:
@@ -1974,6 +2931,7 @@ var Renderer2D = class Renderer2D extends EventEmitter {
1974
2931
  break;
1975
2932
  case 21: if (lineA > 0 && lineW > 0) ctx.stroke();
1976
2933
  }
2934
+ flushBatchFill();
1977
2935
  }
1978
2936
  renderInstancedBatch(batch, ctx) {
1979
2937
  const cols = batch.columns;
@@ -2007,7 +2965,7 @@ var Renderer2D = class Renderer2D extends EventEmitter {
2007
2965
  if (sx !== 1 || sy !== 1) ctx.scale(sx, sy);
2008
2966
  if (rgb !== 0xffffff && fr.cutWidth > 0 && fr.cutHeight > 0) {
2009
2967
  if (srcImg !== lastTintImg || fr.cutX !== lastCutX || fr.cutY !== lastCutY || fr.cutWidth !== lastCutW || fr.cutHeight !== lastCutH || rgb !== lastRgb) {
2010
- cachedTinted = this.getTintedSource(srcImg, fr.cutX, fr.cutY, fr.cutWidth, fr.cutHeight, rgb, false);
2968
+ cachedTinted = this.getTintedSource(srcImg, fr.cutX, fr.cutY, fr.cutWidth, fr.cutHeight, rgb, false, fr.source);
2011
2969
  lastTintImg = srcImg;
2012
2970
  lastCutX = fr.cutX;
2013
2971
  lastCutY = fr.cutY;
@@ -2034,6 +2992,109 @@ var Renderer2D = class Renderer2D extends EventEmitter {
2034
2992
  this.curTris += vis * 2;
2035
2993
  }
2036
2994
  }
2995
+ renderShapeBatch(batch, ctx) {
2996
+ const cols = batch.columns;
2997
+ if (!cols || cols.count <= 0) return;
2998
+ const baseAlpha = ctx.globalAlpha;
2999
+ const n = Math.min(cols.count, batch.maxCount, cols.x.length, cols.y.length);
3000
+ const oxRatio = batch.originX;
3001
+ const oyRatio = batch.originY;
3002
+ const wCol = typeof cols.width === "object" && cols.width ? cols.width : null;
3003
+ const wDef = typeof cols.width === "number" ? cols.width : batch.defaultWidth;
3004
+ const hCol = typeof cols.height === "object" && cols.height ? cols.height : null;
3005
+ const hDef = typeof cols.height === "number" ? cols.height : typeof cols.width === "number" ? cols.width : batch.defaultHeight;
3006
+ const rCol = typeof cols.radius === "object" && cols.radius ? cols.radius : null;
3007
+ const rDef = typeof cols.radius === "number" ? cols.radius : batch.defaultRadius;
3008
+ const fcCol = typeof cols.fillColor === "object" && cols.fillColor ? cols.fillColor : null;
3009
+ const fcDef = typeof cols.fillColor === "number" ? cols.fillColor : batch.defaultFillColor;
3010
+ const faCol = typeof cols.fillAlpha === "object" && cols.fillAlpha ? cols.fillAlpha : null;
3011
+ const faDef = typeof cols.fillAlpha === "number" ? cols.fillAlpha : batch.defaultFillAlpha;
3012
+ const bcCol = typeof cols.borderColor === "object" && cols.borderColor ? cols.borderColor : null;
3013
+ const bcDef = typeof cols.borderColor === "number" ? cols.borderColor : batch.defaultBorderColor;
3014
+ const baCol = typeof cols.borderAlpha === "object" && cols.borderAlpha ? cols.borderAlpha : null;
3015
+ const baDef = typeof cols.borderAlpha === "number" ? cols.borderAlpha : batch.defaultBorderAlpha;
3016
+ const bwCol = typeof cols.borderWidth === "object" && cols.borderWidth ? cols.borderWidth : null;
3017
+ const bwDef = typeof cols.borderWidth === "number" ? cols.borderWidth : batch.defaultBorderWidth;
3018
+ const rotCol = cols.rotation ?? null;
3019
+ const ordCol = cols.order ?? null;
3020
+ let vis = 0;
3021
+ let lastFc = -1, lastFa = -1, fillCss = "";
3022
+ let lastBc = -1, lastBa = -1, borderCss = "";
3023
+ for (let i = 0; i < n; i++) {
3024
+ const s = ordCol ? ordCol[i] : i;
3025
+ const w = wCol ? wCol[s] : wDef;
3026
+ const h = hCol ? hCol[s] : wCol ? w : hDef;
3027
+ if (w <= 0 || h <= 0) continue;
3028
+ const fa = faCol ? faCol[s] : faDef;
3029
+ const bw = bwCol ? bwCol[s] : bwDef;
3030
+ const ba = bw > 0 ? baCol ? baCol[s] : baDef : 0;
3031
+ if (fa <= 0 && ba <= 0) continue;
3032
+ const rad = Math.max(0, Math.min(rCol ? rCol[s] : rDef, Math.min(w, h) * .5));
3033
+ const px = cols.x[s];
3034
+ const py = cols.y[s];
3035
+ const rot = rotCol ? rotCol[s] : 0;
3036
+ if (fa > 0) {
3037
+ const fc = fcCol ? fcCol[s] : fcDef;
3038
+ if (fc !== lastFc || fa !== lastFa) {
3039
+ lastFc = fc;
3040
+ lastFa = fa;
3041
+ fillCss = rgbToCss(fc, fa);
3042
+ }
3043
+ }
3044
+ if (ba > 0) {
3045
+ const bc = bcCol ? bcCol[s] : bcDef;
3046
+ if (bc !== lastBc || ba !== lastBa) {
3047
+ lastBc = bc;
3048
+ lastBa = ba;
3049
+ borderCss = rgbToCss(bc, ba);
3050
+ }
3051
+ }
3052
+ const rx = rot === 0 ? px - w * oxRatio : -w * oxRatio;
3053
+ const ry = rot === 0 ? py - h * oyRatio : -h * oyRatio;
3054
+ if (rot !== 0) {
3055
+ ctx.save();
3056
+ ctx.translate(px, py);
3057
+ ctx.rotate(rot);
3058
+ }
3059
+ if (rad <= 0 && bw <= 0) {
3060
+ ctx.fillStyle = fillCss;
3061
+ ctx.fillRect(rx, ry, w, h);
3062
+ } else if (Math.abs(w - h) <= 1e-4 && rad >= w * .5 - 1e-4) {
3063
+ ctx.beginPath();
3064
+ ctx.arc(rx + w * .5, ry + h * .5, Math.max(0, w * .5 - bw * .5), 0, Math.PI * 2);
3065
+ if (fa > 0) {
3066
+ ctx.fillStyle = fillCss;
3067
+ ctx.fill();
3068
+ }
3069
+ if (ba > 0 && bw > 0) {
3070
+ ctx.lineWidth = bw;
3071
+ ctx.strokeStyle = borderCss;
3072
+ ctx.stroke();
3073
+ }
3074
+ } else {
3075
+ roundedRectPath(ctx, rx + bw * .5, ry + bw * .5, Math.max(0, w - bw), Math.max(0, h - bw), Math.max(0, rad - bw * .5));
3076
+ if (fa > 0) {
3077
+ ctx.fillStyle = fillCss;
3078
+ ctx.fill();
3079
+ }
3080
+ if (ba > 0 && bw > 0) {
3081
+ ctx.lineWidth = bw;
3082
+ ctx.strokeStyle = borderCss;
3083
+ ctx.stroke();
3084
+ }
3085
+ }
3086
+ if (rot !== 0) ctx.restore();
3087
+ vis++;
3088
+ }
3089
+ ctx.globalAlpha = baseAlpha;
3090
+ if (vis > 0) {
3091
+ this.curDrawCalls++;
3092
+ this.curFlushes++;
3093
+ this.curQuads += vis;
3094
+ this.curTris += vis * 2;
3095
+ this.curSdfPanels += vis;
3096
+ }
3097
+ }
2037
3098
  /** Create an offscreen Canvas2D render target backed by a registered Stage `Texture` (`key`). */
2038
3099
  createRenderTarget(key, width, height, textures, filterMode = FilterMode.LINEAR) {
2039
3100
  const w = Math.max(1, Math.round(width));
@@ -2046,21 +3107,30 @@ var Renderer2D = class Renderer2D extends EventEmitter {
2046
3107
  const tex = textures.addCanvas(key, rtCanvas);
2047
3108
  if (!tex) throw new Error("Stage: failed to register Canvas2D RenderTarget texture");
2048
3109
  tex.source[0]?.setFilter(filterMode);
3110
+ const rtCamera = new Camera(w, h, true);
2049
3111
  const rtRenderer = new Renderer2D(rtCanvas, w, h);
2050
3112
  return {
2051
3113
  key,
2052
3114
  width: w,
2053
3115
  height: h,
2054
3116
  texture: tex,
3117
+ camera: rtCamera,
2055
3118
  _rtRenderer: rtRenderer,
3119
+ readPixels: (out) => {
3120
+ const byteLen = w * h * 4;
3121
+ const dst = out && out.length >= byteLen ? out : new Uint8Array(byteLen);
3122
+ const img = rtRenderer.ctx.getImageData(0, 0, w, h);
3123
+ dst.set(img.data.subarray(0, byteLen));
3124
+ return dst;
3125
+ },
2056
3126
  destroy: () => {
2057
3127
  rtRenderer.destroy();
2058
3128
  textures.remove(key);
2059
3129
  }
2060
3130
  };
2061
3131
  }
2062
- /** Render a `DisplayList` (or callback) into an offscreen `StageRenderTarget` in Canvas2D / Headless Node. */
2063
- renderToTarget(target, content, camera) {
3132
+ /** Render a `DisplayList` (or callback) into an offscreen `StageRenderTarget` in Canvas2D / Headless Node (`camera` defaults to `target.camera`). */
3133
+ renderToTarget(target, content, camera = target.camera) {
2064
3134
  const sub = target._rtRenderer;
2065
3135
  if (!sub) return;
2066
3136
  if (typeof content === "function") {
@@ -2068,9 +3138,12 @@ var Renderer2D = class Renderer2D extends EventEmitter {
2068
3138
  content(sub, camera);
2069
3139
  } else sub.render(content, camera);
2070
3140
  const src = target.texture.source[0];
2071
- if (src) src.dirty = true;
3141
+ if (src) {
3142
+ src.version++;
3143
+ src.dirty = true;
3144
+ }
2072
3145
  }
2073
- renderBlitter(blitter, ctx) {
3146
+ renderBlitter(blitter, ctx, m) {
2074
3147
  const list = blitter.children.list;
2075
3148
  const baseAlpha = ctx.globalAlpha;
2076
3149
  let vis = 0;
@@ -2078,6 +3151,10 @@ var Renderer2D = class Renderer2D extends EventEmitter {
2078
3151
  const bob = list[i];
2079
3152
  if (!bob.visible || bob.alpha <= 0) continue;
2080
3153
  const frame = bob.frame;
3154
+ if (this.isOffscreen(m, bob.x, bob.y, bob.x + frame.width, bob.y + frame.height)) {
3155
+ this.curCulled++;
3156
+ continue;
3157
+ }
2081
3158
  ctx.imageSmoothingEnabled = frame.source.filterMode !== FilterMode.NEAREST;
2082
3159
  ctx.globalAlpha = baseAlpha * bob.alpha;
2083
3160
  ctx.drawImage(frame.source.image, frame.cutX, frame.cutY, frame.cutWidth, frame.cutHeight, bob.x, bob.y, frame.width, frame.height);
@@ -2090,23 +3167,57 @@ var Renderer2D = class Renderer2D extends EventEmitter {
2090
3167
  this.curTris += vis * 2;
2091
3168
  }
2092
3169
  }
2093
- renderNode(obj, camera, ctx, isRoot) {
3170
+ renderNode(obj, camera, ctx, parentMat, depth) {
2094
3171
  if (!obj.visible || obj.alpha <= 0) return;
2095
- ctx.save();
2096
- if (isRoot) {
2097
- if (obj.scrollFactorX === 1 && obj.scrollFactorY === 1) {
2098
- const cm = camera.matrixCombined;
2099
- ctx.setTransform(cm.a, cm.b, cm.c, cm.d, cm.tx, cm.ty);
2100
- } else {
3172
+ let baseMat;
3173
+ if (parentMat === null) {
3174
+ if (obj.scrollFactorX === 1 && obj.scrollFactorY === 1) baseMat = camera.matrixCombined;
3175
+ else {
2101
3176
  const z = camera.zoom || 1;
2102
3177
  const ox = camera.width * camera.originX;
2103
3178
  const oy = camera.height * camera.originY;
2104
3179
  const sx = camera.roundPixels ? Math.round(camera.scrollX) : camera.scrollX;
2105
3180
  const sy = camera.roundPixels ? Math.round(camera.scrollY) : camera.scrollY;
2106
- ctx.setTransform(z, 0, 0, z, (-sx * obj.scrollFactorX - ox) * z + ox + camera.x, (-sy * obj.scrollFactorY - oy) * z + oy + camera.y);
3181
+ const rm = this.rootScrollMat;
3182
+ rm.a = z;
3183
+ rm.b = 0;
3184
+ rm.c = 0;
3185
+ rm.d = z;
3186
+ rm.tx = (-sx * obj.scrollFactorX - ox) * z + ox + camera.x;
3187
+ rm.ty = (-sy * obj.scrollFactorY - oy) * z + oy + camera.y;
3188
+ baseMat = rm;
3189
+ }
3190
+ } else baseMat = parentMat;
3191
+ while (depth >= this.matPool.length) this.matPool.push(createMatrix2D());
3192
+ const m = this.mulMat(baseMat, obj.x, obj.y, obj.scaleX, obj.scaleY, obj.rotation, this.matPool[depth]);
3193
+ const t = obj.type;
3194
+ if (t === "Image" || t === "Sprite" || t === "NineSlice" || t === "TileSprite" || t === "Rectangle" || t === "Panel") {
3195
+ const ox = obj.displayOriginX;
3196
+ const oy = obj.displayOriginY;
3197
+ const w = obj.width;
3198
+ const h = obj.height;
3199
+ let x0 = -ox, y0 = -oy, x1 = w - ox, y1 = h - oy;
3200
+ if (t === "Image" || t === "Sprite") {
3201
+ const img = obj;
3202
+ if (img.frame?.customPivot && !img.customOrigin && !img.isCropped) {
3203
+ if (img.flipX) {
3204
+ x0 = ox - w;
3205
+ x1 = ox;
3206
+ }
3207
+ if (img.flipY) {
3208
+ y0 = oy - h;
3209
+ y1 = oy;
3210
+ }
3211
+ }
3212
+ }
3213
+ if (this.isOffscreen(m, x0, y0, x1, y1)) {
3214
+ this.curCulled++;
3215
+ return;
2107
3216
  }
2108
3217
  }
2109
- if (obj.type === "Container") {
3218
+ ctx.save();
3219
+ if (parentMat === null) ctx.setTransform(baseMat.a, baseMat.b, baseMat.c, baseMat.d, baseMat.tx, baseMat.ty);
3220
+ if (t === "Container") {
2110
3221
  const mask = obj.clipMask;
2111
3222
  if (mask && mask.active && mask.graphics?.maskRect) {
2112
3223
  const mr = mask.graphics.maskRect;
@@ -2135,9 +3246,10 @@ var Renderer2D = class Renderer2D extends EventEmitter {
2135
3246
  if (obj.x !== 0 || obj.y !== 0) ctx.translate(obj.x, obj.y);
2136
3247
  if (obj.rotation !== 0) ctx.rotate(obj.rotation);
2137
3248
  if (obj.scaleX !== 1 || obj.scaleY !== 1) ctx.scale(obj.scaleX, obj.scaleY);
2138
- switch (obj.type) {
3249
+ switch (t) {
2139
3250
  case "Image":
2140
- this.renderImage(obj, ctx);
3251
+ case "Sprite":
3252
+ this.renderImage(obj, ctx, m);
2141
3253
  break;
2142
3254
  case "NineSlice":
2143
3255
  this.renderNineSlice(obj, ctx);
@@ -2161,14 +3273,17 @@ var Renderer2D = class Renderer2D extends EventEmitter {
2161
3273
  this.renderGraphics(obj, ctx);
2162
3274
  break;
2163
3275
  case "Blitter":
2164
- this.renderBlitter(obj, ctx);
3276
+ this.renderBlitter(obj, ctx, m);
2165
3277
  break;
2166
3278
  case "InstancedBatch":
2167
3279
  this.renderInstancedBatch(obj, ctx);
2168
3280
  break;
3281
+ case "ShapeBatch":
3282
+ this.renderShapeBatch(obj, ctx);
3283
+ break;
2169
3284
  case "Container": {
2170
3285
  const list = obj.sortedChildren();
2171
- for (let i = 0; i < list.length; i++) this.renderNode(list[i], camera, ctx, false);
3286
+ for (let i = 0; i < list.length; i++) this.renderNode(list[i], camera, ctx, m, depth + 1);
2172
3287
  break;
2173
3288
  }
2174
3289
  }
@@ -2179,6 +3294,7 @@ var Renderer2D = class Renderer2D extends EventEmitter {
2179
3294
  this.curFlushes = 0;
2180
3295
  this.curQuads = 0;
2181
3296
  this.curTris = 0;
3297
+ this.curCulled = 0;
2182
3298
  this.curSdfPanels = 0;
2183
3299
  this.curMsdfIcons = 0;
2184
3300
  camera.syncMetrics();
@@ -2194,11 +3310,12 @@ var Renderer2D = class Renderer2D extends EventEmitter {
2194
3310
  }
2195
3311
  ctx.globalAlpha = camera.alpha;
2196
3312
  const list = displayList.sorted();
2197
- for (let i = 0; i < list.length; i++) this.renderNode(list[i], camera, ctx, true);
3313
+ for (let i = 0; i < list.length; i++) this.renderNode(list[i], camera, ctx, null, 0);
2198
3314
  this.lastStats.drawCalls = this.curDrawCalls;
2199
3315
  this.lastStats.flushes = this.curFlushes;
2200
3316
  this.lastStats.quads = this.curQuads;
2201
3317
  this.lastStats.tris = this.curTris;
3318
+ this.lastStats.culled = this.curCulled;
2202
3319
  this.lastStats.sdfPanels = this.curSdfPanels;
2203
3320
  this.lastStats.sdfDraws = this.curSdfPanels > 0 ? 1 : 0;
2204
3321
  this.lastStats.msdfIcons = this.curMsdfIcons;
@@ -2206,6 +3323,7 @@ var Renderer2D = class Renderer2D extends EventEmitter {
2206
3323
  }
2207
3324
  destroy() {
2208
3325
  this.removeAllListeners();
3326
+ this.tintCache.clear();
2209
3327
  this.tintCanvas = null;
2210
3328
  this.tintCtx = null;
2211
3329
  }
@@ -2246,7 +3364,7 @@ const MAX_VERTS = 16384;
2246
3364
  const MAX_INDICES = 32768;
2247
3365
  const VERT_FLOATS = 6;
2248
3366
  const VERT_STRIDE = 24;
2249
- const MAX_SDF_PANELS = 1024;
3367
+ const MAX_SDF_PANELS = 16384;
2250
3368
  const SDF_FLOATS = 16;
2251
3369
  const SDF_STRIDE = 64;
2252
3370
  const WGSL_BATCH_SRC = `
@@ -2945,18 +4063,13 @@ var RendererWebGpu = class extends EventEmitter {
2945
4063
  this.vertCount = bv + 3;
2946
4064
  this.indexCount = ii + 3;
2947
4065
  }
2948
- renderPanel(panel, m, alpha) {
2949
- if (this.sdfCount >= MAX_SDF_PANELS) return;
4066
+ pushSdfInstance(m, rx, ry, w, h, rTL, rTR, rBR, rBL, borderW, pixelStep, fillAbgr, borderAbgr, blend) {
4067
+ if (this.sdfCount >= MAX_SDF_PANELS || w <= 0 || h <= 0) return;
2950
4068
  if (this.indexCount > this.cmdStartIndex) this.flushPendingBatch();
2951
- if (this.curBlend !== panel.blendMode && this.sdfCount > this.sdfCmdStart) this.flushPendingSdf();
2952
- this.curBlend = panel.blendMode;
2953
- const w = panel.width;
2954
- const h = panel.height;
2955
- if (w <= 0 || h <= 0) return;
2956
- const ox = panel.displayOriginX;
2957
- const oy = panel.displayOriginY;
2958
- const tx = m.a * -ox + m.c * -oy + m.tx;
2959
- const ty = m.b * -ox + m.d * -oy + m.ty;
4069
+ if (this.curBlend !== blend && this.sdfCount > this.sdfCmdStart) this.flushPendingSdf();
4070
+ this.curBlend = blend;
4071
+ const tx = m.a * rx + m.c * ry + m.tx;
4072
+ const ty = m.b * rx + m.d * ry + m.ty;
2960
4073
  const maxR = Math.min(w, h) * .5;
2961
4074
  const base = this.sdfCount * SDF_FLOATS;
2962
4075
  const f = this.sdfF32;
@@ -2969,16 +4082,22 @@ var RendererWebGpu = class extends EventEmitter {
2969
4082
  f[base + 5] = ty;
2970
4083
  f[base + 6] = w;
2971
4084
  f[base + 7] = h;
2972
- f[base + 8] = Math.min(maxR, panel.radiusTL);
2973
- f[base + 9] = Math.min(maxR, panel.radiusTR);
2974
- f[base + 10] = Math.min(maxR, panel.radiusBR);
2975
- f[base + 11] = Math.min(maxR, panel.radiusBL);
2976
- f[base + 12] = panel.borderWidth;
2977
- f[base + 13] = panel.pixelStep;
2978
- u[base + 14] = packColorAbgr(panel.fillColor, panel.fillAlpha * alpha);
2979
- u[base + 15] = packColorAbgr(panel.borderColor, panel.borderAlpha * alpha);
4085
+ f[base + 8] = Math.max(0, Math.min(maxR, rTL));
4086
+ f[base + 9] = Math.max(0, Math.min(maxR, rTR));
4087
+ f[base + 10] = Math.max(0, Math.min(maxR, rBR));
4088
+ f[base + 11] = Math.max(0, Math.min(maxR, rBL));
4089
+ f[base + 12] = Math.max(0, Math.min(maxR, borderW));
4090
+ f[base + 13] = pixelStep;
4091
+ u[base + 14] = fillAbgr;
4092
+ u[base + 15] = borderAbgr;
2980
4093
  this.sdfCount++;
2981
4094
  }
4095
+ renderPanel(panel, m, alpha) {
4096
+ const w = panel.width;
4097
+ const h = panel.height;
4098
+ if (w <= 0 || h <= 0) return;
4099
+ 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);
4100
+ }
2982
4101
  renderGraphics(g, m, alpha) {
2983
4102
  const ops = g.ops;
2984
4103
  const nums = g.nums;
@@ -3021,17 +4140,18 @@ var RendererWebGpu = class extends EventEmitter {
3021
4140
  lineRgb = nums[ni++];
3022
4141
  lineA = nums[ni++];
3023
4142
  break;
3024
- case 3:
3025
- case 5: {
4143
+ case 3: {
3026
4144
  const x = nums[ni++], y = nums[ni++], w = nums[ni++], h = nums[ni++];
3027
- if (op === 5) ni++;
3028
4145
  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);
3029
4146
  break;
3030
4147
  }
3031
- case 4:
3032
- case 6: {
4148
+ case 5: {
4149
+ const x = nums[ni++], y = nums[ni++], w = nums[ni++], h = nums[ni++], r = nums[ni++];
4150
+ 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);
4151
+ break;
4152
+ }
4153
+ case 4: {
3033
4154
  const x = nums[ni++], y = nums[ni++], w = nums[ni++], h = nums[ni++];
3034
- if (op === 6) ni++;
3035
4155
  if (lineA * alpha > 0 && lineW > 0) {
3036
4156
  const abgr = packColorAbgr(lineRgb, lineA * alpha);
3037
4157
  lineSeg(x, y, x + w, y, abgr);
@@ -3041,34 +4161,59 @@ var RendererWebGpu = class extends EventEmitter {
3041
4161
  }
3042
4162
  break;
3043
4163
  }
3044
- case 7:
4164
+ case 6: {
4165
+ const x = nums[ni++], y = nums[ni++], w = nums[ni++], h = nums[ni++], r = nums[ni++];
4166
+ if (lineA * alpha > 0 && lineW > 0 && w > 0 && h > 0) {
4167
+ const hw = lineW * .5, rad = Math.max(0, r) + hw;
4168
+ this.pushSdfInstance(m, x - hw, y - hw, w + lineW, h + lineW, rad, rad, rad, rad, lineW, 0, 0, packColorAbgr(lineRgb, lineA * alpha), g.blendMode);
4169
+ }
4170
+ break;
4171
+ }
4172
+ case 7: {
4173
+ const cx = nums[ni++], cy = nums[ni++], r = nums[ni++];
4174
+ 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);
4175
+ break;
4176
+ }
3045
4177
  case 9: {
3046
- const cx = nums[ni++], cy = nums[ni++];
3047
- const rx = op === 7 ? nums[ni++] : nums[ni++] * .5;
3048
- const ry = op === 7 ? rx : nums[ni++] * .5;
4178
+ const cx = nums[ni++], cy = nums[ni++], w = nums[ni++], h = nums[ni++];
4179
+ const rx = w * .5, ry = h * .5;
3049
4180
  if (fillA * alpha > 0 && rx > 0 && ry > 0) {
3050
- const segs = Math.max(12, Math.min(48, Math.ceil(Math.max(rx, ry) * 1.4)));
3051
- const abgr = packColorAbgr(fillRgb, fillA * alpha);
3052
- for (let s = 0; s < segs; s++) {
3053
- const a0 = s / segs * Math.PI * 2;
3054
- const a1 = (s + 1) / segs * Math.PI * 2;
3055
- 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);
4181
+ 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);
4182
+ else {
4183
+ const segs = Math.max(12, Math.min(48, Math.ceil(Math.max(rx, ry) * 1.4)));
4184
+ const abgr = packColorAbgr(fillRgb, fillA * alpha);
4185
+ for (let s = 0; s < segs; s++) {
4186
+ const a0 = s / segs * Math.PI * 2;
4187
+ const a1 = (s + 1) / segs * Math.PI * 2;
4188
+ 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);
4189
+ }
3056
4190
  }
3057
4191
  }
3058
4192
  break;
3059
4193
  }
3060
- case 8:
4194
+ case 8: {
4195
+ const cx = nums[ni++], cy = nums[ni++], r = nums[ni++];
4196
+ if (lineA * alpha > 0 && lineW > 0 && r > 0) {
4197
+ const ro = r + lineW * .5;
4198
+ this.pushSdfInstance(m, cx - ro, cy - ro, ro * 2, ro * 2, ro, ro, ro, ro, lineW, 0, 0, packColorAbgr(lineRgb, lineA * alpha), g.blendMode);
4199
+ }
4200
+ break;
4201
+ }
3061
4202
  case 10: {
3062
- const cx = nums[ni++], cy = nums[ni++];
3063
- const rx = op === 8 ? nums[ni++] : nums[ni++] * .5;
3064
- const ry = op === 8 ? rx : nums[ni++] * .5;
4203
+ const cx = nums[ni++], cy = nums[ni++], w = nums[ni++], h = nums[ni++];
4204
+ const rx = w * .5, ry = h * .5;
3065
4205
  if (lineA * alpha > 0 && lineW > 0 && rx > 0 && ry > 0) {
3066
- const segs = Math.max(12, Math.min(48, Math.ceil(Math.max(rx, ry) * 1.4)));
3067
- const abgr = packColorAbgr(lineRgb, lineA * alpha);
3068
- for (let s = 0; s < segs; s++) {
3069
- const a0 = s / segs * Math.PI * 2;
3070
- const a1 = (s + 1) / segs * Math.PI * 2;
3071
- lineSeg(cx + Math.cos(a0) * rx, cy + Math.sin(a0) * ry, cx + Math.cos(a1) * rx, cy + Math.sin(a1) * ry, abgr);
4206
+ if (Math.abs(w - h) <= 1e-4) {
4207
+ const ro = rx + lineW * .5;
4208
+ this.pushSdfInstance(m, cx - ro, cy - ro, ro * 2, ro * 2, ro, ro, ro, ro, lineW, 0, 0, packColorAbgr(lineRgb, lineA * alpha), g.blendMode);
4209
+ } else {
4210
+ const segs = Math.max(12, Math.min(48, Math.ceil(Math.max(rx, ry) * 1.4)));
4211
+ const abgr = packColorAbgr(lineRgb, lineA * alpha);
4212
+ for (let s = 0; s < segs; s++) {
4213
+ const a0 = s / segs * Math.PI * 2;
4214
+ const a1 = (s + 1) / segs * Math.PI * 2;
4215
+ lineSeg(cx + Math.cos(a0) * rx, cy + Math.sin(a0) * ry, cx + Math.cos(a1) * rx, cy + Math.sin(a1) * ry, abgr);
4216
+ }
3072
4217
  }
3073
4218
  }
3074
4219
  break;
@@ -3167,23 +4312,29 @@ var RendererWebGpu = class extends EventEmitter {
3167
4312
  m = multiplyMat(baseMat, obj.x, obj.y, obj.scaleX, obj.scaleY, obj.rotation, this.matPool[depth]);
3168
4313
  }
3169
4314
  switch (obj.type) {
3170
- case "Image": {
4315
+ case "Image":
4316
+ case "Sprite": {
3171
4317
  const img = obj;
3172
4318
  const fr = img.frame;
3173
4319
  if (!fr || fr.width <= 0 || fr.height <= 0) break;
3174
4320
  const bg = this.ensureSourceBindGroup(fr.source);
3175
4321
  if (!bg) break;
3176
4322
  const ox = img.displayOriginX, oy = img.displayOriginY;
4323
+ const pivotMirrored = fr.customPivot && !img.customOrigin && !img.isCropped;
4324
+ const x0 = pivotMirrored && img.flipX ? ox - img.width : -ox;
4325
+ const y0 = pivotMirrored && img.flipY ? oy - img.height : -oy;
4326
+ const x1 = pivotMirrored && img.flipX ? ox : img.width - ox;
4327
+ const y1 = pivotMirrored && img.flipY ? oy : img.height - oy;
3177
4328
  const u0 = img.flipX ? fr.u1 : fr.u0, u1 = img.flipX ? fr.u0 : fr.u1;
3178
4329
  const v0 = img.flipY ? fr.v1 : fr.v0, v1 = img.flipY ? fr.v0 : fr.v1;
3179
4330
  const rgb = img.isTinted ? img.tint : WHITE_RGB;
3180
4331
  const mode = img.isTinted && img.tintMode === TintModes.FILL ? 1 : 0;
3181
- this.pushQuad(m, -ox, -oy, img.width - ox, img.height - oy, u0, v0, u1, v1, packColorAbgr(rgb, effAlpha), mode, img.blendMode, bg);
4332
+ this.pushQuad(m, x0, y0, x1, y1, u0, v0, u1, v1, packColorAbgr(rgb, effAlpha), mode, img.blendMode, bg);
3182
4333
  break;
3183
4334
  }
3184
4335
  case "Rectangle": {
3185
4336
  const r = obj;
3186
- if (r.isFilled && r.fillAlpha * effAlpha > 0) {
4337
+ if (r.width > 0 && r.height > 0 && r.fillAlpha * effAlpha > 0) {
3187
4338
  const ox = r.displayOriginX, oy = r.displayOriginY;
3188
4339
  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);
3189
4340
  }
@@ -3260,6 +4411,48 @@ var RendererWebGpu = class extends EventEmitter {
3260
4411
  }
3261
4412
  break;
3262
4413
  }
4414
+ case "ShapeBatch": {
4415
+ const batch = obj;
4416
+ const cols = batch.columns;
4417
+ if (!cols || cols.count <= 0) break;
4418
+ const n = Math.min(cols.count, batch.maxCount, cols.x.length, cols.y.length);
4419
+ const wCol = typeof cols.width === "object" && cols.width ? cols.width : null;
4420
+ const wDef = typeof cols.width === "number" ? cols.width : batch.defaultWidth;
4421
+ const hCol = typeof cols.height === "object" && cols.height ? cols.height : null;
4422
+ const hDef = typeof cols.height === "number" ? cols.height : typeof cols.width === "number" ? cols.width : batch.defaultHeight;
4423
+ const rCol = typeof cols.radius === "object" && cols.radius ? cols.radius : null;
4424
+ const rDef = typeof cols.radius === "number" ? cols.radius : batch.defaultRadius;
4425
+ const fcCol = typeof cols.fillColor === "object" && cols.fillColor ? cols.fillColor : null;
4426
+ const fcDef = typeof cols.fillColor === "number" ? cols.fillColor : batch.defaultFillColor;
4427
+ const faCol = typeof cols.fillAlpha === "object" && cols.fillAlpha ? cols.fillAlpha : null;
4428
+ const faDef = typeof cols.fillAlpha === "number" ? cols.fillAlpha : batch.defaultFillAlpha;
4429
+ const bcCol = typeof cols.borderColor === "object" && cols.borderColor ? cols.borderColor : null;
4430
+ const bcDef = typeof cols.borderColor === "number" ? cols.borderColor : batch.defaultBorderColor;
4431
+ const baCol = typeof cols.borderAlpha === "object" && cols.borderAlpha ? cols.borderAlpha : null;
4432
+ const baDef = typeof cols.borderAlpha === "number" ? cols.borderAlpha : batch.defaultBorderAlpha;
4433
+ const bwCol = typeof cols.borderWidth === "object" && cols.borderWidth ? cols.borderWidth : null;
4434
+ const bwDef = typeof cols.borderWidth === "number" ? cols.borderWidth : batch.defaultBorderWidth;
4435
+ const rotCol = cols.rotation ?? null;
4436
+ const ordCol = cols.order ?? null;
4437
+ for (let i = 0; i < n; i++) {
4438
+ const s = ordCol ? ordCol[i] : i;
4439
+ const w = wCol ? wCol[s] : wDef;
4440
+ const h = hCol ? hCol[s] : wCol ? w : hDef;
4441
+ if (w <= 0 || h <= 0) continue;
4442
+ const fa = (faCol ? faCol[s] : faDef) * effAlpha;
4443
+ const bw = bwCol ? bwCol[s] : bwDef;
4444
+ const ba = bw > 0 ? (baCol ? baCol[s] : baDef) * effAlpha : 0;
4445
+ if (fa <= 0 && ba <= 0) continue;
4446
+ const rad = rCol ? rCol[s] : rDef;
4447
+ const fc = fcCol ? fcCol[s] : fcDef;
4448
+ const bc = bcCol ? bcCol[s] : bcDef;
4449
+ const rot = rotCol ? rotCol[s] : 0;
4450
+ const im = depth + 1 >= this.matPool.length ? createMatrix2D() : this.matPool[depth + 1];
4451
+ multiplyMat(m, cols.x[s], cols.y[s], 1, 1, rot, im);
4452
+ 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);
4453
+ }
4454
+ break;
4455
+ }
3263
4456
  case "Container": {
3264
4457
  const cont = obj;
3265
4458
  const mask = cont.clipMask;
@@ -3395,21 +4588,30 @@ var RendererWebGpu = class extends EventEmitter {
3395
4588
  const tex = textures.addCanvas(key, rtCanvas);
3396
4589
  if (!tex) throw new Error("Stage: failed to register WebGPU RenderTarget texture");
3397
4590
  tex.source[0]?.setFilter(filterMode);
4591
+ const rtCamera = new Camera(w, h, true);
3398
4592
  const rtRenderer = new Renderer2D(rtCanvas, w, h);
3399
4593
  return {
3400
4594
  key,
3401
4595
  width: w,
3402
4596
  height: h,
3403
4597
  texture: tex,
4598
+ camera: rtCamera,
3404
4599
  _rtRenderer: rtRenderer,
4600
+ readPixels: (out) => {
4601
+ const byteLen = w * h * 4;
4602
+ const dst = out && out.length >= byteLen ? out : new Uint8Array(byteLen);
4603
+ const img = rtRenderer.ctx.getImageData(0, 0, w, h);
4604
+ dst.set(img.data.subarray(0, byteLen));
4605
+ return dst;
4606
+ },
3405
4607
  destroy: () => {
3406
4608
  rtRenderer.destroy();
3407
4609
  textures.remove(key);
3408
4610
  }
3409
4611
  };
3410
4612
  }
3411
- /** Render a `DisplayList` (or callback) into an offscreen `StageRenderTarget` and mark its texture dirty for WebGPU upload. */
3412
- renderToTarget(target, content, camera) {
4613
+ /** Render a `DisplayList` (or callback) into an offscreen `StageRenderTarget` and mark its texture dirty for WebGPU upload (`camera` defaults to `target.camera`). */
4614
+ renderToTarget(target, content, camera = target.camera) {
3413
4615
  const sub = target._rtRenderer;
3414
4616
  if (!sub) return;
3415
4617
  if (typeof content === "function") {
@@ -3432,4 +4634,4 @@ var RendererWebGpu = class extends EventEmitter {
3432
4634
  } catch {}
3433
4635
  }
3434
4636
  };
3435
- 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 };
4637
+ export { SpriteObject as _, Container as a, Camera as b, 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, TextObject as v, TileSpriteObject as y };