@holtjs/stage 0.6.0 → 0.8.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.
@@ -349,7 +349,7 @@ var DisplayObject = class extends EventEmitter {
349
349
  rotation = 0;
350
350
  alpha = 1;
351
351
  depth = 0;
352
- zIndex = 0;
352
+ _zIndex = 0;
353
353
  trackId = null;
354
354
  alt = null;
355
355
  role = null;
@@ -441,10 +441,19 @@ var DisplayObject = class extends EventEmitter {
441
441
  }
442
442
  return this;
443
443
  }
444
+ get zIndex() {
445
+ return this._zIndex;
446
+ }
447
+ set zIndex(z) {
448
+ if (this._zIndex !== z) {
449
+ this._zIndex = z;
450
+ if (this.parentContainer) this.parentContainer._markZDirty(z !== 0);
451
+ }
452
+ }
444
453
  /** Native container-local stacking order (0 by default; sorted stably when any sibling has non-zero zIndex). */
445
454
  setZIndex(z) {
446
- if (this.zIndex !== z) {
447
- this.zIndex = z;
455
+ if (this._zIndex !== z) {
456
+ this._zIndex = z;
448
457
  if (this.parentContainer) this.parentContainer._markZDirty(z !== 0);
449
458
  else this.setDepth(z);
450
459
  }
@@ -637,16 +646,42 @@ var ImageObject = class extends DisplayObject {
637
646
  cropW = 0;
638
647
  cropH = 0;
639
648
  customRenderNodes = {};
640
- constructor(scene, x, y, key, frame) {
641
- super(scene, "Image");
649
+ constructor(scene, x, y, key, frame, nodeType = "Image") {
650
+ super(scene, nodeType);
642
651
  this.x = x;
643
652
  this.y = y;
644
653
  this.texture = scene.textures.get(key);
645
654
  this.frame = this.texture.get(frame);
646
655
  this.width = this.frame.width;
647
656
  this.height = this.frame.height;
648
- this._displayOriginX = this.width * this.originX;
649
- this._displayOriginY = this.height * this.originY;
657
+ this._syncFrameOrigin();
658
+ }
659
+ get displayOriginX() {
660
+ if (!this.customOrigin && this.frame?.customPivot) return this.frame.pixelPivotX;
661
+ return super.displayOriginX;
662
+ }
663
+ get displayOriginY() {
664
+ if (!this.customOrigin && this.frame?.customPivot) return this.frame.pixelPivotY;
665
+ return super.displayOriginY;
666
+ }
667
+ _syncFrameOrigin() {
668
+ if (!this.customOrigin) {
669
+ if (this.frame.customPivot) {
670
+ this._displayOriginX = this.frame.pixelPivotX;
671
+ this._displayOriginY = this.frame.pixelPivotY;
672
+ this.originX = this.width > 0 ? this._displayOriginX / this.width : .5;
673
+ this.originY = this.height > 0 ? this._displayOriginY / this.height : .5;
674
+ } else {
675
+ this._displayOriginX = this.width * this.originX;
676
+ this._displayOriginY = this.height * this.originY;
677
+ }
678
+ }
679
+ }
680
+ /** Clear any manual `setOrigin()` override so per-frame baked pivots (`ax/ay` or `pivotX/pivotY`) drive alignment automatically. */
681
+ useFramePivot() {
682
+ this.customOrigin = false;
683
+ this._syncFrameOrigin();
684
+ return this;
650
685
  }
651
686
  setTexture(key, frame) {
652
687
  this.texture = this.scene.textures.get(key);
@@ -656,12 +691,151 @@ var ImageObject = class extends DisplayObject {
656
691
  this.frame = this.texture.get(frame);
657
692
  this.width = this.frame.width;
658
693
  this.height = this.frame.height;
659
- if (!this.customOrigin) {
660
- this._displayOriginX = this.width * this.originX;
661
- this._displayOriginY = this.height * this.originY;
662
- }
694
+ this._syncFrameOrigin();
663
695
  return this;
664
696
  }
697
+ /**
698
+ * Compute the exact transformed coordinates of a named per-frame pixel socket (e.g. `'hand'`, `'head'`, `'emissive'`)
699
+ * accounting for trimmed `displayOrigin`, `flipX`/`flipY`, `scaleX`/`scaleY`, `rotation`, and ancestor `Container`s.
700
+ */
701
+ getAnchor(name, space = "parent", out = {
702
+ x: 0,
703
+ y: 0
704
+ }) {
705
+ const raw = this.frame?.getAnchor(name);
706
+ if (!raw) return null;
707
+ const ox = this._displayOriginX;
708
+ const oy = this._displayOriginY;
709
+ const pivotMirrored = this.frame.customPivot && !this.customOrigin && !this.isCropped;
710
+ const lx = this.flipX ? pivotMirrored ? ox - raw[0] : this.width - ox - raw[0] : raw[0] - ox;
711
+ const ly = this.flipY ? pivotMirrored ? oy - raw[1] : this.height - oy - raw[1] : raw[1] - oy;
712
+ if (space === "local") {
713
+ out.x = lx;
714
+ out.y = ly;
715
+ return out;
716
+ }
717
+ const sx = lx * this.scaleX;
718
+ const sy = ly * this.scaleY;
719
+ let px;
720
+ let py;
721
+ if (this.rotation === 0) {
722
+ px = this.x + sx;
723
+ py = this.y + sy;
724
+ } else {
725
+ const c = Math.cos(this.rotation);
726
+ const s = Math.sin(this.rotation);
727
+ px = this.x + sx * c - sy * s;
728
+ py = this.y + sx * s + sy * c;
729
+ }
730
+ if (space === "parent" || !this.parentContainer) {
731
+ out.x = px;
732
+ out.y = py;
733
+ return out;
734
+ }
735
+ let cur = this.parentContainer;
736
+ while (cur) {
737
+ const csx = px * cur.scaleX;
738
+ const csy = py * cur.scaleY;
739
+ if (cur.rotation === 0) {
740
+ px = cur.x + csx;
741
+ py = cur.y + csy;
742
+ } else {
743
+ const cc = Math.cos(cur.rotation);
744
+ const cs = Math.sin(cur.rotation);
745
+ px = cur.x + csx * cc - csy * cs;
746
+ py = cur.y + csx * cs + csy * cc;
747
+ }
748
+ cur = cur.parentContainer;
749
+ }
750
+ out.x = px;
751
+ out.y = py;
752
+ return out;
753
+ }
754
+ /**
755
+ * Read the full transformed 2D pose (`x`, `y`, `rotation`, `zBias`, `hasAngle`) of an oriented socket
756
+ * (`[x, y, angleRad, zBias]` from `@holtjs/craft/three` `charKit.handSocket`), transforming the 2D grip
757
+ * tangent vector through the exact `flip -> scale -> rotate -> parentContainer` Jacobian!
758
+ */
759
+ getAnchorPose(name, space = "parent", out = {
760
+ x: 0,
761
+ y: 0,
762
+ rotation: 0,
763
+ zBias: 1,
764
+ hasAngle: false
765
+ }) {
766
+ const raw = this.frame?.getAnchor(name);
767
+ if (!raw) return null;
768
+ this.getAnchor(name, space, out);
769
+ const hasAngle = raw.length >= 3 && typeof raw[2] === "number" && Number.isFinite(raw[2]);
770
+ const rawAngle = hasAngle ? raw[2] : 0;
771
+ const zBias = raw.length >= 4 && typeof raw[3] === "number" ? raw[3] >= 0 ? 1 : -1 : 1;
772
+ let dx = Math.cos(rawAngle);
773
+ let dy = Math.sin(rawAngle);
774
+ if (this.flipX) dx = -dx;
775
+ if (this.flipY) dy = -dy;
776
+ if (space !== "local") {
777
+ dx *= this.scaleX;
778
+ dy *= this.scaleY;
779
+ if (this.rotation !== 0) {
780
+ const c = Math.cos(this.rotation);
781
+ const s = Math.sin(this.rotation);
782
+ const rx = dx * c - dy * s;
783
+ const ry = dx * s + dy * c;
784
+ dx = rx;
785
+ dy = ry;
786
+ }
787
+ if (space === "world") {
788
+ let cur = this.parentContainer;
789
+ while (cur) {
790
+ dx *= cur.scaleX;
791
+ dy *= cur.scaleY;
792
+ if (cur.rotation !== 0) {
793
+ const cc = Math.cos(cur.rotation);
794
+ const cs = Math.sin(cur.rotation);
795
+ const rx = dx * cc - dy * cs;
796
+ const ry = dx * cs + dy * cc;
797
+ dx = rx;
798
+ dy = ry;
799
+ }
800
+ cur = cur.parentContainer;
801
+ }
802
+ }
803
+ }
804
+ out.rotation = Math.atan2(dy, dx);
805
+ out.zBias = zBias;
806
+ out.hasAngle = hasAngle;
807
+ return out;
808
+ }
809
+ /**
810
+ * Align an equipped weapon/tool `DisplayObject` (`axe`, `spear`, `torch`, `shield`) to a named socket (`'hand'`) in one call:
811
+ * - Sets `child.x` & `child.y` to the transformed socket position
812
+ * - Rotates `child.rotation` so `restAngle` (default `-Math.PI / 2` = upright sprite) aligns with the socket's projected 2D shaft angle
813
+ * - Mirrors asymmetric blades (`mirrorBlade: true`) across the shaft axis via `O(2)` reflection when aiming left (`cos < 0`) so single-bladed axes never strike backwards!
814
+ * - Applies `zBias` (`+1` in front of body, `-1` behind body) via `child.setZIndex(baseZIndex + zBias)`
815
+ */
816
+ syncAttachment(child, anchorName, opts = {}) {
817
+ const space = opts.space ?? "parent";
818
+ const pose = this.getAnchorPose(anchorName, space);
819
+ if (!pose) return false;
820
+ child.x = pose.x;
821
+ child.y = pose.y;
822
+ if (pose.hasAngle || opts.restAngle !== void 0 || opts.angleOffset !== void 0) {
823
+ const rest = opts.restAngle ?? -Math.PI * .5;
824
+ const mirror = opts.mirrorBlade !== false;
825
+ const aimLeft = Math.cos(pose.rotation) < -1e-4;
826
+ if (mirror) {
827
+ if (Math.abs(Math.sin(rest)) >= Math.abs(Math.cos(rest))) child.flipX = aimLeft;
828
+ else child.flipY = aimLeft;
829
+ }
830
+ const offset = opts.angleOffset ?? 0;
831
+ child.rotation = pose.rotation - rest + (mirror && aimLeft ? -offset : offset);
832
+ }
833
+ if (opts.baseZIndex !== false) {
834
+ const baseZ = opts.baseZIndex ?? this.zIndex;
835
+ child.setZIndex(baseZ + pose.zBias);
836
+ }
837
+ return true;
838
+ }
665
839
  setTint(color) {
666
840
  this.tint = color & HEX_MASK;
667
841
  this.isTinted = true;
@@ -695,6 +869,226 @@ var ImageObject = class extends DisplayObject {
695
869
  return this;
696
870
  }
697
871
  };
872
+ /**
873
+ * First-class animated 2D / 2.5D sprite (`scene.add.sprite(x, y, key, frame)`).
874
+ * - Automatically aligns per-frame trimmed pivots (`ax/ay`) & transforms named sockets (`getAnchor('hand')`)
875
+ * - Deterministic simulation-stepped clip playback (`play`, `playDirectional`, `stop`, `pause`, `resume`)
876
+ * - Phase-preserving 4/8-directional locomotion (`playDirectional`) aligned with `Camera25D` & `@holtjs/craft`
877
+ * - Exact frame-crossing event callbacks (`onAnimEvent` for `footstep`, `hit`, etc.)
878
+ */
879
+ var SpriteObject = class extends ImageObject {
880
+ _anim = null;
881
+ _frameIdx = 0;
882
+ _frameElapsedMs = 0;
883
+ _forward = true;
884
+ _repeatsLeft = 0;
885
+ _playing = false;
886
+ _paused = false;
887
+ _timeScale = 1;
888
+ _activeDirBase = null;
889
+ _activeDirIndex = -1;
890
+ _animListeners = /* @__PURE__ */ new Set();
891
+ constructor(scene, x, y, key, frame) {
892
+ super(scene, x, y, key, frame, "Sprite");
893
+ }
894
+ get currentAnim() {
895
+ return this._anim;
896
+ }
897
+ get currentAnimKey() {
898
+ return this._anim?.key ?? null;
899
+ }
900
+ get currentFrameIndex() {
901
+ return this._frameIdx;
902
+ }
903
+ get currentDirection() {
904
+ return this._activeDirIndex;
905
+ }
906
+ get isPlaying() {
907
+ return this._playing && !this._paused;
908
+ }
909
+ get timeScale() {
910
+ return this._timeScale;
911
+ }
912
+ setTimeScale(scale) {
913
+ this._timeScale = Number.isFinite(scale) ? Math.max(0, scale) : 1;
914
+ return this;
915
+ }
916
+ onAnimEvent(fn) {
917
+ this._animListeners.add(fn);
918
+ return () => {
919
+ this._animListeners.delete(fn);
920
+ };
921
+ }
922
+ /** Normalized progress `[0..1)` through the current forward pass of the active animation. */
923
+ get cycleProgress() {
924
+ const anim = this._anim;
925
+ if (!anim || anim.totalDurationMs <= 0 || anim.frames.length === 0) return 0;
926
+ const cf = anim.frames[this._frameIdx];
927
+ if (!cf) return 0;
928
+ return Math.max(0, Math.min(.999999, (cf.offsetMs + this._frameElapsedMs) / anim.totalDurationMs));
929
+ }
930
+ /** Play a registered animation clip by key (or `StageAnimation` object). Fails fast if `key` does not exist! */
931
+ play(keyOrAnim, opts) {
932
+ const options = typeof opts === "boolean" ? { ignoreIfPlaying: opts } : opts ?? {};
933
+ const nextAnim = this.scene.anims.resolveOrThrow(keyOrAnim);
934
+ if (options.timeScale !== void 0) this.setTimeScale(options.timeScale);
935
+ if (options.ignoreIfPlaying && this._playing && this._anim === nextAnim) return this;
936
+ const prevPhase = options.preservePhase && this._anim ? this.cycleProgress : null;
937
+ this._activeDirBase = null;
938
+ this._anim = nextAnim;
939
+ this._playing = true;
940
+ this._paused = false;
941
+ this._forward = true;
942
+ this._repeatsLeft = options.repeat !== void 0 ? options.repeat : nextAnim.repeat;
943
+ if (prevPhase !== null && nextAnim.frames.length > 0) {
944
+ const targetMs = prevPhase * nextAnim.totalDurationMs;
945
+ let idx = 0;
946
+ while (idx + 1 < nextAnim.frames.length && nextAnim.frames[idx + 1].offsetMs <= targetMs) idx++;
947
+ this._frameIdx = idx;
948
+ this._frameElapsedMs = Math.max(0, targetMs - nextAnim.frames[idx].offsetMs);
949
+ this._applyAnimFrame(idx, false);
950
+ } else {
951
+ const startIdx = Math.max(0, Math.min(nextAnim.frames.length - 1, Math.floor(options.startFrame ?? 0)));
952
+ this._frameIdx = startIdx;
953
+ this._frameElapsedMs = 0;
954
+ this._applyAnimFrame(startIdx, true);
955
+ }
956
+ this.scene._registerAnimatedSprite(this);
957
+ return this;
958
+ }
959
+ /**
960
+ * Play a 4- or 8-directional animation group (e.g. `'survivor/walk'` or `'zombie/attack'`) quantized by
961
+ * world movement angle `headingRad = Math.atan2(vy, vx)` and optional `Camera25D.yaw`!
962
+ *
963
+ * Automatically preserves stride phase when turning between directions of the same clip (`walk/d0 -> walk/d1`)
964
+ * so characters never freeze on frame 0 or stutter mid-step!
965
+ */
966
+ playDirectional(baseKey, headingRad, opts = {}) {
967
+ const grp = this.scene.anims.resolveDirectionalOrThrow(baseKey);
968
+ const dirs = Math.max(1, grp.directions);
969
+ const step = Math.PI * 2 / dirs;
970
+ const camYaw = opts.cameraYaw ?? 0;
971
+ const atlasYaw = opts.atlasYaw ?? grp.atlasYaw;
972
+ const effective = (Number.isFinite(headingRad) ? headingRad : 0) - camYaw + atlasYaw;
973
+ const dirIdx = (Math.round(effective / step) % dirs + dirs) % dirs;
974
+ const targetAnim = grp.dirClips[dirIdx] ?? grp.dirClips[0];
975
+ const sameBase = this._playing && this._activeDirBase === grp.baseKey;
976
+ this._activeDirIndex = dirIdx;
977
+ if (sameBase && this._anim === targetAnim) {
978
+ if (opts.timeScale !== void 0) this.setTimeScale(opts.timeScale);
979
+ this._activeDirBase = grp.baseKey;
980
+ return this;
981
+ }
982
+ this.play(targetAnim, {
983
+ ignoreIfPlaying: false,
984
+ preservePhase: sameBase ? opts.preservePhase !== false : Boolean(opts.preservePhase),
985
+ timeScale: opts.timeScale,
986
+ repeat: opts.repeat
987
+ });
988
+ this._activeDirBase = grp.baseKey;
989
+ return this;
990
+ }
991
+ pause() {
992
+ this._paused = true;
993
+ return this;
994
+ }
995
+ resume() {
996
+ this._paused = false;
997
+ return this;
998
+ }
999
+ stop() {
1000
+ this._playing = false;
1001
+ this._paused = false;
1002
+ this._activeDirBase = null;
1003
+ this.scene._unregisterAnimatedSprite(this);
1004
+ return this;
1005
+ }
1006
+ /** Internal deterministic simulation step invoked by `Scene._stepAnimations(dt)`. */
1007
+ _stepAnim(deltaMs) {
1008
+ if (!this._playing || this._paused || !this._anim) return;
1009
+ const scaled = deltaMs * this._timeScale;
1010
+ if (scaled <= 0) return;
1011
+ const anim = this._anim;
1012
+ const frames = anim.frames;
1013
+ const n = frames.length;
1014
+ if (n === 0) return;
1015
+ this._frameElapsedMs += scaled;
1016
+ let safety = 0;
1017
+ while (this._playing && this._anim === anim && safety++ < 64) {
1018
+ const cur = frames[this._frameIdx];
1019
+ const dur = Math.max(.001, cur.durationMs);
1020
+ if (this._frameElapsedMs < dur) break;
1021
+ this._frameElapsedMs -= dur;
1022
+ if (n === 1) {
1023
+ if (this._repeatsLeft === 0) {
1024
+ this._finishAnim(anim);
1025
+ return;
1026
+ }
1027
+ if (this._repeatsLeft > 0) this._repeatsLeft--;
1028
+ this._applyAnimFrame(0, true);
1029
+ this.emit("animrepeat", anim.key, this);
1030
+ continue;
1031
+ }
1032
+ if (this._forward) {
1033
+ if (this._frameIdx + 1 < n) {
1034
+ this._frameIdx++;
1035
+ this._applyAnimFrame(this._frameIdx, true);
1036
+ } else if (anim.yoyo) {
1037
+ this._forward = false;
1038
+ this._frameIdx = n - 2;
1039
+ this._applyAnimFrame(this._frameIdx, true);
1040
+ } else if (this._repeatsLeft === 0) {
1041
+ this._finishAnim(anim);
1042
+ return;
1043
+ } else {
1044
+ if (this._repeatsLeft > 0) this._repeatsLeft--;
1045
+ this._frameIdx = 0;
1046
+ this._applyAnimFrame(0, true);
1047
+ this.emit("animrepeat", anim.key, this);
1048
+ }
1049
+ } else if (this._frameIdx - 1 >= 0) {
1050
+ this._frameIdx--;
1051
+ this._applyAnimFrame(this._frameIdx, true);
1052
+ } else if (this._repeatsLeft === 0) {
1053
+ this._finishAnim(anim);
1054
+ return;
1055
+ } else {
1056
+ if (this._repeatsLeft > 0) this._repeatsLeft--;
1057
+ this._forward = true;
1058
+ this._frameIdx = Math.min(1, n - 1);
1059
+ this._applyAnimFrame(this._frameIdx, true);
1060
+ this.emit("animrepeat", anim.key, this);
1061
+ }
1062
+ }
1063
+ }
1064
+ _applyAnimFrame(index, fireEvents) {
1065
+ const anim = this._anim;
1066
+ if (!anim) return;
1067
+ const cf = anim.frames[index];
1068
+ if (!cf) return;
1069
+ if (this.texture.key !== cf.textureKey) this.setTexture(cf.textureKey, cf.frameName);
1070
+ else this.setFrame(cf.frameName);
1071
+ if (fireEvents && cf.events.length > 0) for (let i = 0; i < cf.events.length; i++) {
1072
+ const ev = cf.events[i];
1073
+ for (const fn of this._animListeners) fn(ev, anim.key, index, cf.frameName);
1074
+ this.emit("animevent", ev, anim.key, index, cf.frameName);
1075
+ }
1076
+ }
1077
+ _finishAnim(anim) {
1078
+ this._frameElapsedMs = 0;
1079
+ this._playing = false;
1080
+ this._activeDirBase = null;
1081
+ this.scene._unregisterAnimatedSprite(this);
1082
+ this.emit("animcomplete", anim.key, this);
1083
+ }
1084
+ destroy(fromScene) {
1085
+ this.scene._unregisterAnimatedSprite(this);
1086
+ this._animListeners.clear();
1087
+ this._anim = null;
1088
+ this._playing = false;
1089
+ super.destroy(fromScene);
1090
+ }
1091
+ };
698
1092
  var NineSliceObject = class extends DisplayObject {
699
1093
  texture;
700
1094
  frame;
@@ -1866,11 +2260,14 @@ var Renderer2D = class Renderer2D extends EventEmitter {
1866
2260
  tintCtx = null;
1867
2261
  tintCache = /* @__PURE__ */ new Map();
1868
2262
  maskWorldMat = createMatrix2D();
2263
+ rootScrollMat = createMatrix2D();
2264
+ matPool = Array.from({ length: 16 }, () => createMatrix2D());
1869
2265
  pathCache = /* @__PURE__ */ new Map();
1870
2266
  curDrawCalls = 0;
1871
2267
  curFlushes = 0;
1872
2268
  curQuads = 0;
1873
2269
  curTris = 0;
2270
+ curCulled = 0;
1874
2271
  curSdfPanels = 0;
1875
2272
  curMsdfIcons = 0;
1876
2273
  lastStats = {
@@ -1969,7 +2366,39 @@ var Renderer2D = class Renderer2D extends EventEmitter {
1969
2366
  tg.restore();
1970
2367
  return tc;
1971
2368
  }
1972
- renderImage(img, ctx) {
2369
+ isOffscreen(m, x0, y0, x1, y1) {
2370
+ const a = m.a, b = m.b, c = m.c, d = m.d, tx = m.tx, ty = m.ty;
2371
+ const px0 = a * x0 + c * y0 + tx, py0 = b * x0 + d * y0 + ty;
2372
+ const px1 = a * x1 + c * y0 + tx, py1 = b * x1 + d * y0 + ty;
2373
+ const px2 = a * x1 + c * y1 + tx, py2 = b * x1 + d * y1 + ty;
2374
+ const px3 = a * x0 + c * y1 + tx, py3 = b * x0 + d * y1 + ty;
2375
+ if (px0 < 0 && px1 < 0 && px2 < 0 && px3 < 0) return true;
2376
+ if (py0 < 0 && py1 < 0 && py2 < 0 && py3 < 0) return true;
2377
+ const w = this.width, h = this.height;
2378
+ if (px0 > w && px1 > w && px2 > w && px3 > w) return true;
2379
+ if (py0 > h && py1 > h && py2 > h && py3 > h) return true;
2380
+ return false;
2381
+ }
2382
+ mulMat(parent, x, y, sx, sy, rot, out) {
2383
+ const pa = parent.a, pb = parent.b, pc = parent.c, pd = parent.d;
2384
+ out.tx = pa * x + pc * y + parent.tx;
2385
+ out.ty = pb * x + pd * y + parent.ty;
2386
+ if (rot === 0) {
2387
+ out.a = pa * sx;
2388
+ out.b = pb * sx;
2389
+ out.c = pc * sy;
2390
+ out.d = pd * sy;
2391
+ } else {
2392
+ const cos = Math.cos(rot), sin = Math.sin(rot);
2393
+ const la = cos * sx, lb = sin * sx, lc = -sin * sy, ld = cos * sy;
2394
+ out.a = pa * la + pc * lb;
2395
+ out.b = pb * la + pd * lb;
2396
+ out.c = pa * lc + pc * ld;
2397
+ out.d = pb * lc + pd * ld;
2398
+ }
2399
+ return out;
2400
+ }
2401
+ renderImage(img, ctx, m) {
1973
2402
  const frame = img.frame;
1974
2403
  if (!frame || frame.width <= 0 || frame.height <= 0) return;
1975
2404
  const srcImg = frame.source.image;
@@ -1992,7 +2421,6 @@ var Renderer2D = class Renderer2D extends EventEmitter {
1992
2421
  dh = rh;
1993
2422
  }
1994
2423
  if (img.pixelAA) {
1995
- const m = ctx.getTransform();
1996
2424
  const dispW = Math.hypot(m.a * dw, m.b * dw);
1997
2425
  const dispH = Math.hypot(m.c * dh, m.d * dh);
1998
2426
  const sxRatio = dispW / Math.max(1, sw);
@@ -2000,7 +2428,7 @@ var Renderer2D = class Renderer2D extends EventEmitter {
2000
2428
  ctx.imageSmoothingEnabled = !(sxRatio >= .98 && syRatio >= .98 && Math.abs(sxRatio - Math.round(sxRatio)) <= .02 && Math.abs(syRatio - Math.round(syRatio)) <= .02);
2001
2429
  } else ctx.imageSmoothingEnabled = frame.source.filterMode !== FilterMode.NEAREST;
2002
2430
  if (img.flipX || img.flipY) {
2003
- ctx.translate(img.flipX ? dx * 2 + dw : 0, img.flipY ? dy * 2 + dh : 0);
2431
+ if (!(frame.customPivot && !img.customOrigin && !img.isCropped)) ctx.translate(img.flipX ? dx * 2 + dw : 0, img.flipY ? dy * 2 + dh : 0);
2004
2432
  ctx.scale(img.flipX ? -1 : 1, img.flipY ? -1 : 1);
2005
2433
  }
2006
2434
  if (img.isTinted && (img.tint !== 0xffffff || img.tintMode === TintModes.FILL)) {
@@ -2809,7 +3237,7 @@ var Renderer2D = class Renderer2D extends EventEmitter {
2809
3237
  src.dirty = true;
2810
3238
  }
2811
3239
  }
2812
- renderBlitter(blitter, ctx) {
3240
+ renderBlitter(blitter, ctx, m) {
2813
3241
  const list = blitter.children.list;
2814
3242
  const baseAlpha = ctx.globalAlpha;
2815
3243
  let vis = 0;
@@ -2817,6 +3245,10 @@ var Renderer2D = class Renderer2D extends EventEmitter {
2817
3245
  const bob = list[i];
2818
3246
  if (!bob.visible || bob.alpha <= 0) continue;
2819
3247
  const frame = bob.frame;
3248
+ if (this.isOffscreen(m, bob.x, bob.y, bob.x + frame.width, bob.y + frame.height)) {
3249
+ this.curCulled++;
3250
+ continue;
3251
+ }
2820
3252
  ctx.imageSmoothingEnabled = frame.source.filterMode !== FilterMode.NEAREST;
2821
3253
  ctx.globalAlpha = baseAlpha * bob.alpha;
2822
3254
  ctx.drawImage(frame.source.image, frame.cutX, frame.cutY, frame.cutWidth, frame.cutHeight, bob.x, bob.y, frame.width, frame.height);
@@ -2829,23 +3261,57 @@ var Renderer2D = class Renderer2D extends EventEmitter {
2829
3261
  this.curTris += vis * 2;
2830
3262
  }
2831
3263
  }
2832
- renderNode(obj, camera, ctx, isRoot) {
3264
+ renderNode(obj, camera, ctx, parentMat, depth) {
2833
3265
  if (!obj.visible || obj.alpha <= 0) return;
2834
- ctx.save();
2835
- if (isRoot) {
2836
- if (obj.scrollFactorX === 1 && obj.scrollFactorY === 1) {
2837
- const cm = camera.matrixCombined;
2838
- ctx.setTransform(cm.a, cm.b, cm.c, cm.d, cm.tx, cm.ty);
2839
- } else {
3266
+ let baseMat;
3267
+ if (parentMat === null) {
3268
+ if (obj.scrollFactorX === 1 && obj.scrollFactorY === 1) baseMat = camera.matrixCombined;
3269
+ else {
2840
3270
  const z = camera.zoom || 1;
2841
3271
  const ox = camera.width * camera.originX;
2842
3272
  const oy = camera.height * camera.originY;
2843
3273
  const sx = camera.roundPixels ? Math.round(camera.scrollX) : camera.scrollX;
2844
3274
  const sy = camera.roundPixels ? Math.round(camera.scrollY) : camera.scrollY;
2845
- ctx.setTransform(z, 0, 0, z, (-sx * obj.scrollFactorX - ox) * z + ox + camera.x, (-sy * obj.scrollFactorY - oy) * z + oy + camera.y);
3275
+ const rm = this.rootScrollMat;
3276
+ rm.a = z;
3277
+ rm.b = 0;
3278
+ rm.c = 0;
3279
+ rm.d = z;
3280
+ rm.tx = (-sx * obj.scrollFactorX - ox) * z + ox + camera.x;
3281
+ rm.ty = (-sy * obj.scrollFactorY - oy) * z + oy + camera.y;
3282
+ baseMat = rm;
3283
+ }
3284
+ } else baseMat = parentMat;
3285
+ while (depth >= this.matPool.length) this.matPool.push(createMatrix2D());
3286
+ const m = this.mulMat(baseMat, obj.x, obj.y, obj.scaleX, obj.scaleY, obj.rotation, this.matPool[depth]);
3287
+ const t = obj.type;
3288
+ if (t === "Image" || t === "Sprite" || t === "NineSlice" || t === "TileSprite" || t === "Rectangle" || t === "Panel") {
3289
+ const ox = obj.displayOriginX;
3290
+ const oy = obj.displayOriginY;
3291
+ const w = obj.width;
3292
+ const h = obj.height;
3293
+ let x0 = -ox, y0 = -oy, x1 = w - ox, y1 = h - oy;
3294
+ if (t === "Image" || t === "Sprite") {
3295
+ const img = obj;
3296
+ if (img.frame?.customPivot && !img.customOrigin && !img.isCropped) {
3297
+ if (img.flipX) {
3298
+ x0 = ox - w;
3299
+ x1 = ox;
3300
+ }
3301
+ if (img.flipY) {
3302
+ y0 = oy - h;
3303
+ y1 = oy;
3304
+ }
3305
+ }
3306
+ }
3307
+ if (this.isOffscreen(m, x0, y0, x1, y1)) {
3308
+ this.curCulled++;
3309
+ return;
2846
3310
  }
2847
3311
  }
2848
- if (obj.type === "Container") {
3312
+ ctx.save();
3313
+ if (parentMat === null) ctx.setTransform(baseMat.a, baseMat.b, baseMat.c, baseMat.d, baseMat.tx, baseMat.ty);
3314
+ if (t === "Container") {
2849
3315
  const mask = obj.clipMask;
2850
3316
  if (mask && mask.active && mask.graphics?.maskRect) {
2851
3317
  const mr = mask.graphics.maskRect;
@@ -2874,9 +3340,10 @@ var Renderer2D = class Renderer2D extends EventEmitter {
2874
3340
  if (obj.x !== 0 || obj.y !== 0) ctx.translate(obj.x, obj.y);
2875
3341
  if (obj.rotation !== 0) ctx.rotate(obj.rotation);
2876
3342
  if (obj.scaleX !== 1 || obj.scaleY !== 1) ctx.scale(obj.scaleX, obj.scaleY);
2877
- switch (obj.type) {
3343
+ switch (t) {
2878
3344
  case "Image":
2879
- this.renderImage(obj, ctx);
3345
+ case "Sprite":
3346
+ this.renderImage(obj, ctx, m);
2880
3347
  break;
2881
3348
  case "NineSlice":
2882
3349
  this.renderNineSlice(obj, ctx);
@@ -2900,7 +3367,7 @@ var Renderer2D = class Renderer2D extends EventEmitter {
2900
3367
  this.renderGraphics(obj, ctx);
2901
3368
  break;
2902
3369
  case "Blitter":
2903
- this.renderBlitter(obj, ctx);
3370
+ this.renderBlitter(obj, ctx, m);
2904
3371
  break;
2905
3372
  case "InstancedBatch":
2906
3373
  this.renderInstancedBatch(obj, ctx);
@@ -2910,7 +3377,7 @@ var Renderer2D = class Renderer2D extends EventEmitter {
2910
3377
  break;
2911
3378
  case "Container": {
2912
3379
  const list = obj.sortedChildren();
2913
- for (let i = 0; i < list.length; i++) this.renderNode(list[i], camera, ctx, false);
3380
+ for (let i = 0; i < list.length; i++) this.renderNode(list[i], camera, ctx, m, depth + 1);
2914
3381
  break;
2915
3382
  }
2916
3383
  }
@@ -2921,6 +3388,7 @@ var Renderer2D = class Renderer2D extends EventEmitter {
2921
3388
  this.curFlushes = 0;
2922
3389
  this.curQuads = 0;
2923
3390
  this.curTris = 0;
3391
+ this.curCulled = 0;
2924
3392
  this.curSdfPanels = 0;
2925
3393
  this.curMsdfIcons = 0;
2926
3394
  camera.syncMetrics();
@@ -2936,11 +3404,12 @@ var Renderer2D = class Renderer2D extends EventEmitter {
2936
3404
  }
2937
3405
  ctx.globalAlpha = camera.alpha;
2938
3406
  const list = displayList.sorted();
2939
- for (let i = 0; i < list.length; i++) this.renderNode(list[i], camera, ctx, true);
3407
+ for (let i = 0; i < list.length; i++) this.renderNode(list[i], camera, ctx, null, 0);
2940
3408
  this.lastStats.drawCalls = this.curDrawCalls;
2941
3409
  this.lastStats.flushes = this.curFlushes;
2942
3410
  this.lastStats.quads = this.curQuads;
2943
3411
  this.lastStats.tris = this.curTris;
3412
+ this.lastStats.culled = this.curCulled;
2944
3413
  this.lastStats.sdfPanels = this.curSdfPanels;
2945
3414
  this.lastStats.sdfDraws = this.curSdfPanels > 0 ? 1 : 0;
2946
3415
  this.lastStats.msdfIcons = this.curMsdfIcons;
@@ -3937,18 +4406,24 @@ var RendererWebGpu = class extends EventEmitter {
3937
4406
  m = multiplyMat(baseMat, obj.x, obj.y, obj.scaleX, obj.scaleY, obj.rotation, this.matPool[depth]);
3938
4407
  }
3939
4408
  switch (obj.type) {
3940
- case "Image": {
4409
+ case "Image":
4410
+ case "Sprite": {
3941
4411
  const img = obj;
3942
4412
  const fr = img.frame;
3943
4413
  if (!fr || fr.width <= 0 || fr.height <= 0) break;
3944
4414
  const bg = this.ensureSourceBindGroup(fr.source);
3945
4415
  if (!bg) break;
3946
4416
  const ox = img.displayOriginX, oy = img.displayOriginY;
4417
+ const pivotMirrored = fr.customPivot && !img.customOrigin && !img.isCropped;
4418
+ const x0 = pivotMirrored && img.flipX ? ox - img.width : -ox;
4419
+ const y0 = pivotMirrored && img.flipY ? oy - img.height : -oy;
4420
+ const x1 = pivotMirrored && img.flipX ? ox : img.width - ox;
4421
+ const y1 = pivotMirrored && img.flipY ? oy : img.height - oy;
3947
4422
  const u0 = img.flipX ? fr.u1 : fr.u0, u1 = img.flipX ? fr.u0 : fr.u1;
3948
4423
  const v0 = img.flipY ? fr.v1 : fr.v0, v1 = img.flipY ? fr.v0 : fr.v1;
3949
4424
  const rgb = img.isTinted ? img.tint : WHITE_RGB;
3950
4425
  const mode = img.isTinted && img.tintMode === TintModes.FILL ? 1 : 0;
3951
- this.pushQuad(m, -ox, -oy, img.width - ox, img.height - oy, u0, v0, u1, v1, packColorAbgr(rgb, effAlpha), mode, img.blendMode, bg);
4426
+ this.pushQuad(m, x0, y0, x1, y1, u0, v0, u1, v1, packColorAbgr(rgb, effAlpha), mode, img.blendMode, bg);
3952
4427
  break;
3953
4428
  }
3954
4429
  case "Rectangle": {
@@ -4253,4 +4728,4 @@ var RendererWebGpu = class extends EventEmitter {
4253
4728
  } catch {}
4254
4729
  }
4255
4730
  };
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 };
4731
+ 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 };