@esotericsoftware/spine-webgl 4.3.9 → 4.3.11

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.
@@ -172,6 +172,7 @@ var spine = (() => {
172
172
  SkeletonData: () => SkeletonData,
173
173
  SkeletonDebugRenderer: () => SkeletonDebugRenderer,
174
174
  SkeletonJson: () => SkeletonJson,
175
+ SkeletonPhysicsMovement: () => SkeletonPhysicsMovement,
175
176
  SkeletonRenderer: () => SkeletonRenderer,
176
177
  SkeletonRendererCore: () => SkeletonRendererCore,
177
178
  Skin: () => Skin,
@@ -11262,7 +11263,7 @@ ${error}` : ""}`);
11262
11263
  t = triangles[i + 2];
11263
11264
  const x3 = vertices[t * stride], y3 = vertices[t * stride + 1];
11264
11265
  const u3 = uvs[t << 1], v3 = uvs[(t << 1) + 1];
11265
- const d0 = y2 - y3, d1 = x3 - x2, d2 = x1 - x3, d4 = y3 - y1, d = 1 / (d0 * d2 + d1 * (y1 - y3));
11266
+ let d0 = 0, d1 = 0, d2 = 0, d4 = 0, d = 0;
11266
11267
  for (let p = 0; p < polygonsCount; p++) {
11267
11268
  let s = clippedVertices.length;
11268
11269
  if (this.clip(x1, y1, x2, y2, x3, y3, polygons[p])) {
@@ -11270,6 +11271,13 @@ ${error}` : ""}`);
11270
11271
  const clipOutputLength = clipOutput.length;
11271
11272
  if (clipOutputLength === 0) continue;
11272
11273
  let clipOutputCount = clipOutputLength >> 1;
11274
+ if (d === 0) {
11275
+ d0 = y2 - y3;
11276
+ d1 = x3 - x2;
11277
+ d2 = x1 - x3;
11278
+ d4 = y3 - y1;
11279
+ d = 1 / (d0 * d2 - d1 * d4);
11280
+ }
11273
11281
  const cv = Utils.setArraySize(clippedVertices, s + clipOutputCount * stride);
11274
11282
  for (let ii = 0; ii < clipOutputLength; ii += 2, s += stride) {
11275
11283
  const x = clipOutputItems[ii], y = clipOutputItems[ii + 1];
@@ -11469,7 +11477,7 @@ ${error}` : ""}`);
11469
11477
  const x3 = vertices[vertexStart + v], y3 = vertices[vertexStart + v + 1];
11470
11478
  uv = t << 1;
11471
11479
  const u3 = uvs[uv], v3 = uvs[uv + 1];
11472
- const d0 = y2 - y3, d1 = x3 - x2, d2 = x1 - x3, d4 = y3 - y1, d = 1 / (d0 * d2 + d1 * (y1 - y3));
11480
+ let d0 = 0, d1 = 0, d2 = 0, d4 = 0, d = 0;
11473
11481
  for (let p = 0; p < polygonsCount; p++) {
11474
11482
  let s = this.clippedVerticesLength;
11475
11483
  if (this.clip(x1, y1, x2, y2, x3, y3, polygons[p])) {
@@ -11477,6 +11485,13 @@ ${error}` : ""}`);
11477
11485
  const clipOutputLength = clipOutput.length;
11478
11486
  if (clipOutputLength === 0) continue;
11479
11487
  let clipOutputCount = clipOutputLength >> 1;
11488
+ if (d === 0) {
11489
+ d0 = y2 - y3;
11490
+ d1 = x3 - x2;
11491
+ d2 = x1 - x3;
11492
+ d4 = y3 - y1;
11493
+ d = 1 / (d0 * d2 - d1 * d4);
11494
+ }
11480
11495
  const newLength = s + clipOutputCount * stride;
11481
11496
  if (clippedVertices.length < newLength) {
11482
11497
  this._clippedVerticesTyped = new Float32Array(newLength * 2);
@@ -11593,16 +11608,15 @@ ${error}` : ""}`);
11593
11608
  const edgeX = v[i], edgeY = v[i + 1], ex = edgeX - v[i + 2], ey = edgeY - v[i + 3];
11594
11609
  const outputStart = output.length;
11595
11610
  const iv2 = input;
11596
- for (let ii = 0, nn = input.length - 2; ii < nn; ) {
11597
- x1 = iv2[ii];
11598
- y1 = iv2[ii + 1];
11599
- ii += 2;
11611
+ x1 = iv2[0];
11612
+ y1 = iv2[1];
11613
+ let s1 = ey * (edgeX - x1) - ex * (edgeY - y1);
11614
+ for (let ii = 2, nn = input.length - 2; ii <= nn; ii += 2) {
11600
11615
  x2 = iv2[ii];
11601
11616
  y2 = iv2[ii + 1];
11602
- const s2 = ey * (edgeX - x2) > ex * (edgeY - y2);
11603
- const s1 = ey * (edgeX - x1) - ex * (edgeY - y1);
11617
+ const s2 = ey * (edgeX - x2) - ex * (edgeY - y2);
11604
11618
  if (s1 > 0) {
11605
- if (s2)
11619
+ if (s2 > 0)
11606
11620
  output.push(x2, y2);
11607
11621
  else {
11608
11622
  const ix = x2 - x1, iy = y2 - y1, t = s1 / (ix * ey - iy * ex);
@@ -11612,7 +11626,7 @@ ${error}` : ""}`);
11612
11626
  } else
11613
11627
  output.push(x2, y2);
11614
11628
  }
11615
- } else if (s2) {
11629
+ } else if (s2 > 0) {
11616
11630
  const ix = x2 - x1, iy = y2 - y1, t = s1 / (ix * ey - iy * ex);
11617
11631
  if (t >= 0 && t <= 1) {
11618
11632
  output.push(x1 + ix * t, y1 + iy * t, x2, y2);
@@ -11621,6 +11635,9 @@ ${error}` : ""}`);
11621
11635
  output.push(x2, y2);
11622
11636
  } else
11623
11637
  clipped = true;
11638
+ x1 = x2;
11639
+ y1 = y2;
11640
+ s1 = s2;
11624
11641
  }
11625
11642
  if (outputStart === output.length) {
11626
11643
  originalOutput.length = 0;
@@ -11668,16 +11685,15 @@ ${error}` : ""}`);
11668
11685
  const outputStart = output.length, fragmentStart = this.inverseVertices.length;
11669
11686
  this.inverseVertices.push(0);
11670
11687
  iv = input;
11671
- for (let ii = 0, nn = input.length - 2; ii < nn; ) {
11672
- x1 = iv[ii];
11673
- y1 = iv[ii + 1];
11674
- ii += 2;
11688
+ x1 = iv[0];
11689
+ y1 = iv[1];
11690
+ let s1 = ey * (edgeX - x1) - ex * (edgeY - y1);
11691
+ for (let ii = 2, nn = input.length - 2; ii <= nn; ii += 2) {
11675
11692
  x2 = iv[ii];
11676
11693
  y2 = iv[ii + 1];
11677
- const s2 = ey * (edgeX - x2) > ex * (edgeY - y2);
11678
- const s1 = ey * (edgeX - x1) - ex * (edgeY - y1);
11694
+ const s2 = ey * (edgeX - x2) - ex * (edgeY - y2);
11679
11695
  if (s1 > 0) {
11680
- if (s2)
11696
+ if (s2 > 0)
11681
11697
  output.push(x2, y2);
11682
11698
  else {
11683
11699
  const ix = x2 - x1, iy = y2 - y1, t = s1 / (ix * ey - iy * ex);
@@ -11688,16 +11704,19 @@ ${error}` : ""}`);
11688
11704
  } else
11689
11705
  output.push(x2, y2);
11690
11706
  }
11691
- } else if (s2) {
11692
- const dx = x2 - x1, dy = y2 - y1, t = s1 / (dx * ey - dy * ex);
11707
+ } else if (s2 > 0) {
11708
+ const ix = x2 - x1, iy = y2 - y1, t = s1 / (ix * ey - iy * ex);
11693
11709
  if (t >= 0 && t <= 1) {
11694
- const cx = x1 + dx * t, cy = y1 + dy * t;
11710
+ const cx = x1 + ix * t, cy = y1 + iy * t;
11695
11711
  this.inverseVertices.push(cx, cy);
11696
11712
  output.push(cx, cy, x2, y2);
11697
11713
  } else
11698
11714
  output.push(x2, y2);
11699
11715
  } else
11700
11716
  this.inverseVertices.push(x2, y2);
11717
+ x1 = x2;
11718
+ y1 = y2;
11719
+ s1 = s2;
11701
11720
  }
11702
11721
  const fragmentSize = this.inverseVertices.length - fragmentStart - 1;
11703
11722
  if (fragmentSize >= 6)
@@ -13052,6 +13071,151 @@ ${error}` : ""}`);
13052
13071
  return map[property] !== void 0 ? map[property] : defaultValue;
13053
13072
  }
13054
13073
 
13074
+ // spine-core/src/SkeletonPhysicsMovement.ts
13075
+ var SkeletonPhysicsMovement = class {
13076
+ /**
13077
+ * Creates a movement tracker for a skeleton displayed by a host runtime object.
13078
+ * @param skeleton The skeleton whose physics constraints receive inherited movement.
13079
+ * @param adapter Runtime-specific hooks used to read and convert the host object's transform.
13080
+ * @param options Initial movement inheritance values.
13081
+ */
13082
+ constructor(skeleton, adapter, options = {}) {
13083
+ this.skeleton = skeleton;
13084
+ this.adapter = adapter;
13085
+ this.positionInheritanceFactorX = options.positionInheritanceX ?? 0;
13086
+ this.positionInheritanceFactorY = options.positionInheritanceY ?? 0;
13087
+ this.rotationInheritanceFactor = options.rotationInheritance ?? 0;
13088
+ }
13089
+ positionInheritanceFactorX = 0;
13090
+ positionInheritanceFactorY = 0;
13091
+ rotationInheritanceFactor = 0;
13092
+ hasLastTransform = false;
13093
+ lastX = 0;
13094
+ lastY = 0;
13095
+ lastZ = 0;
13096
+ lastRotation = 0;
13097
+ currentTransform = { x: 0, y: 0, z: 0, rotation: 0 };
13098
+ currentPosition = { x: 0, y: 0, z: 0 };
13099
+ lastPosition = { x: 0, y: 0, z: 0 };
13100
+ /** Horizontal position inheritance factor. `0` disables horizontal position inheritance. */
13101
+ get positionInheritanceX() {
13102
+ return this.positionInheritanceFactorX;
13103
+ }
13104
+ /** Vertical position inheritance factor. `0` disables vertical position inheritance. */
13105
+ get positionInheritanceY() {
13106
+ return this.positionInheritanceFactorY;
13107
+ }
13108
+ /**
13109
+ * Sets how much host object translation is inherited by skeleton physics constraints.
13110
+ * Use `(1, 1)` for normal inheritance, or `(0, 0)` to disable position inheritance.
13111
+ * @param x The horizontal position inheritance factor.
13112
+ * @param y The vertical position inheritance factor.
13113
+ */
13114
+ setPositionInheritance(x, y) {
13115
+ const wasDisabled = this.positionInheritanceFactorX === 0 && this.positionInheritanceFactorY === 0;
13116
+ const isEnabled = x !== 0 || y !== 0;
13117
+ this.positionInheritanceFactorX = x;
13118
+ this.positionInheritanceFactorY = y;
13119
+ if (wasDisabled && isEnabled) this.resetPosition();
13120
+ }
13121
+ /** Rotation inheritance factor. `0` disables rotation inheritance. */
13122
+ get rotationInheritance() {
13123
+ return this.rotationInheritanceFactor;
13124
+ }
13125
+ /**
13126
+ * Sets how much host object rotation is inherited by skeleton physics constraints.
13127
+ * @param value The rotation inheritance factor.
13128
+ */
13129
+ set rotationInheritance(value) {
13130
+ const wasDisabled = this.rotationInheritanceFactor === 0;
13131
+ this.rotationInheritanceFactor = value;
13132
+ if (wasDisabled && value !== 0) this.resetRotation();
13133
+ }
13134
+ /** Resets the previous position used to calculate inherited translation. */
13135
+ resetPosition() {
13136
+ const transform = this.currentTransform;
13137
+ const readRotation = !this.hasLastTransform && this.rotationInheritanceFactor !== 0;
13138
+ this.adapter.readTransform(transform, readRotation);
13139
+ this.lastX = transform.x;
13140
+ this.lastY = transform.y;
13141
+ this.lastZ = transform.z;
13142
+ if (readRotation) this.lastRotation = transform.rotation;
13143
+ this.hasLastTransform = true;
13144
+ }
13145
+ /** Resets the previous rotation used to calculate inherited rotation. */
13146
+ resetRotation() {
13147
+ const transform = this.currentTransform;
13148
+ this.adapter.readTransform(transform, true);
13149
+ this.lastRotation = transform.rotation;
13150
+ if (!this.hasLastTransform) {
13151
+ this.lastX = transform.x;
13152
+ this.lastY = transform.y;
13153
+ this.lastZ = transform.z;
13154
+ }
13155
+ this.hasLastTransform = true;
13156
+ }
13157
+ /** Resets both previous position and previous rotation. */
13158
+ resetTransform() {
13159
+ const transform = this.currentTransform;
13160
+ this.adapter.readTransform(transform, true);
13161
+ this.setLastTransform(transform.x, transform.y, transform.z, transform.rotation);
13162
+ }
13163
+ /**
13164
+ * Applies host object transform movement since the previous call to the skeleton's physics constraints.
13165
+ *
13166
+ * The first call records the current transform as the baseline and does not apply movement.
13167
+ */
13168
+ applyTransformMovement() {
13169
+ const inheritPosition = this.positionInheritanceFactorX !== 0 || this.positionInheritanceFactorY !== 0;
13170
+ const inheritRotation = this.rotationInheritanceFactor !== 0;
13171
+ if (!inheritPosition && !inheritRotation) return;
13172
+ const transform = this.currentTransform;
13173
+ this.adapter.readTransform(transform, inheritRotation);
13174
+ const { x, y, z } = transform;
13175
+ const currentRotation = inheritRotation ? transform.rotation : this.lastRotation;
13176
+ const positionChanged = x !== this.lastX || y !== this.lastY || z !== this.lastZ;
13177
+ if (this.hasLastTransform) {
13178
+ if (!positionChanged && currentRotation === this.lastRotation) return;
13179
+ if (inheritPosition && positionChanged) this.applyPositionMovement(x, y, z);
13180
+ if (inheritRotation && currentRotation !== this.lastRotation) this.applyRotationMovement(currentRotation);
13181
+ }
13182
+ this.setLastTransform(x, y, z, currentRotation);
13183
+ }
13184
+ applyPositionMovement(currentX, currentY, currentZ) {
13185
+ const currentPosition = this.currentPosition;
13186
+ currentPosition.x = currentX;
13187
+ currentPosition.y = currentY;
13188
+ currentPosition.z = currentZ;
13189
+ this.adapter.worldToSkeleton(currentPosition);
13190
+ const lastPosition = this.lastPosition;
13191
+ lastPosition.x = this.lastX;
13192
+ lastPosition.y = this.lastY;
13193
+ lastPosition.z = this.lastZ;
13194
+ this.adapter.worldToSkeleton(lastPosition);
13195
+ this.skeleton.physicsTranslate(
13196
+ (currentPosition.x - lastPosition.x) * this.positionInheritanceFactorX,
13197
+ (currentPosition.y - lastPosition.y) * this.positionInheritanceFactorY
13198
+ );
13199
+ }
13200
+ applyRotationMovement(currentRotation) {
13201
+ const rotationFactor = this.rotationInheritanceFactor;
13202
+ if (rotationFactor === 0) return;
13203
+ this.skeleton.physicsRotate(0, 0, this.getRotationDelta(currentRotation, this.lastRotation) * rotationFactor);
13204
+ }
13205
+ setLastTransform(x, y, z, rotation) {
13206
+ this.lastX = x;
13207
+ this.lastY = y;
13208
+ this.lastZ = z;
13209
+ this.lastRotation = rotation;
13210
+ this.hasLastTransform = true;
13211
+ }
13212
+ getRotationDelta(current, previous) {
13213
+ let delta = current - previous;
13214
+ delta = (delta + 180) % 360 - 180;
13215
+ return delta < -180 ? delta + 360 : delta;
13216
+ }
13217
+ };
13218
+
13055
13219
  // spine-core/src/SkeletonRendererCore.ts
13056
13220
  var SkeletonRendererCore = class {
13057
13221
  commandPool = new CommandPool();
@@ -13064,17 +13228,21 @@ ${error}` : ""}`);
13064
13228
  this.renderCommands.length = 0;
13065
13229
  const clipper = this.clipping;
13066
13230
  const drawOrder = skeleton.drawOrder.appliedPose;
13067
- for (let i = 0; i < skeleton.slots.length; i++) {
13231
+ for (let i = 0, n = drawOrder.length; i < n; i++) {
13068
13232
  const slot = drawOrder[i];
13069
- const attachment = slot.appliedPose.attachment;
13070
- if (!attachment) {
13233
+ if (!slot.bone.active) {
13071
13234
  clipper.clipEnd(slot);
13072
13235
  continue;
13073
13236
  }
13074
13237
  const pose = slot.appliedPose;
13238
+ const attachment = pose.attachment;
13239
+ if (!attachment) {
13240
+ clipper.clipEnd(slot);
13241
+ continue;
13242
+ }
13075
13243
  const slotColor = pose.color;
13076
13244
  const alpha = slotColor.a;
13077
- if ((alpha === 0 || !slot.bone.active) && !(attachment instanceof ClippingAttachment)) {
13245
+ if (alpha === 0 && !(attachment instanceof ClippingAttachment)) {
13078
13246
  clipper.clipEnd(slot);
13079
13247
  continue;
13080
13248
  }
@@ -13118,9 +13286,15 @@ ${error}` : ""}`);
13118
13286
  indicesCount = indices.length;
13119
13287
  texture = sequence.regions[sequenceIndex]?.texture;
13120
13288
  } else if (attachment instanceof ClippingAttachment) {
13289
+ clipper.clipEnd(slot);
13121
13290
  clipper.clipStart(skeleton, slot, attachment);
13122
13291
  continue;
13123
13292
  } else {
13293
+ clipper.clipEnd(slot);
13294
+ continue;
13295
+ }
13296
+ if (!texture) {
13297
+ clipper.clipEnd(slot);
13124
13298
  continue;
13125
13299
  }
13126
13300
  const skelColor = skeleton.color;