[ts] Small refactor alignment with reference runtime.

This commit is contained in:
Davide Tantillo 2025-04-01 14:40:11 +02:00
parent d459b040d0
commit 8711e55672
9 changed files with 45 additions and 54 deletions

View File

@ -1421,10 +1421,9 @@ export class DeformTimeline extends CurveTimeline implements SlotTimeline {
apply (skeleton: Skeleton, lastTime: number, time: number, firedEvents: Array<Event>, alpha: number, blend: MixBlend, direction: MixDirection) {
let slot: Slot = skeleton.slots[this.slotIndex];
if (!slot.bone.active) return;
let slotAttachment: Attachment | null = slot.getAttachment();
if (!slotAttachment) return;
if (!(slotAttachment instanceof VertexAttachment) || (<VertexAttachment>slotAttachment).timelineAttachment != this.attachment) return;
if (!slot.bone.active || !(slot.attachment instanceof VertexAttachment)) return;
let vertexAttachment = slot.attachment;
if (vertexAttachment.timelineAttachment != this.attachment) return;
let deform: Array<number> = slot.deform;
if (deform.length == 0) blend = MixBlend.setup;
@ -1444,7 +1443,6 @@ export class DeformTimeline extends CurveTimeline implements SlotTimeline {
return;
}
deform.length = vertexCount;
let vertexAttachment = <VertexAttachment>slotAttachment;
if (!vertexAttachment.bones) {
// Unweighted vertex positions.
let setupVertices = vertexAttachment.vertices;
@ -1465,7 +1463,6 @@ export class DeformTimeline extends CurveTimeline implements SlotTimeline {
let lastVertices = vertices[frames.length - 1];
if (alpha == 1) {
if (blend == MixBlend.add) {
let vertexAttachment = slotAttachment as VertexAttachment;
if (!vertexAttachment.bones) {
// Unweighted vertex positions, with alpha.
let setupVertices = vertexAttachment.vertices;
@ -1481,7 +1478,6 @@ export class DeformTimeline extends CurveTimeline implements SlotTimeline {
} else {
switch (blend) {
case MixBlend.setup: {
let vertexAttachment = slotAttachment as VertexAttachment;
if (!vertexAttachment.bones) {
// Unweighted vertex positions, with alpha.
let setupVertices = vertexAttachment.vertices;
@ -1502,7 +1498,6 @@ export class DeformTimeline extends CurveTimeline implements SlotTimeline {
deform[i] += (lastVertices[i] - deform[i]) * alpha;
break;
case MixBlend.add:
let vertexAttachment = slotAttachment as VertexAttachment;
if (!vertexAttachment.bones) {
// Unweighted vertex positions, with alpha.
let setupVertices = vertexAttachment.vertices;
@ -1526,7 +1521,6 @@ export class DeformTimeline extends CurveTimeline implements SlotTimeline {
if (alpha == 1) {
if (blend == MixBlend.add) {
let vertexAttachment = slotAttachment as VertexAttachment;
if (!vertexAttachment.bones) {
// Unweighted vertex positions, with alpha.
let setupVertices = vertexAttachment.vertices;
@ -1550,7 +1544,6 @@ export class DeformTimeline extends CurveTimeline implements SlotTimeline {
} else {
switch (blend) {
case MixBlend.setup: {
let vertexAttachment = slotAttachment as VertexAttachment;
if (!vertexAttachment.bones) {
// Unweighted vertex positions, with alpha.
let setupVertices = vertexAttachment.vertices;
@ -1575,7 +1568,6 @@ export class DeformTimeline extends CurveTimeline implements SlotTimeline {
}
break;
case MixBlend.add:
let vertexAttachment = slotAttachment as VertexAttachment;
if (!vertexAttachment.bones) {
// Unweighted vertex positions, with alpha.
let setupVertices = vertexAttachment.vertices;
@ -2327,7 +2319,7 @@ export class SequenceTimeline extends Timeline implements SlotTimeline {
let attachment = this.attachment as unknown as Attachment;
if (slotAttachment != attachment) {
if (!(slotAttachment instanceof VertexAttachment)
|| (slotAttachment as VertexAttachment).timelineAttachment != attachment) return;
|| slotAttachment.timelineAttachment != attachment) return;
}
if (direction == MixDirection.mixOut) {

View File

@ -938,7 +938,7 @@ export class TrackEntry {
*
* The `mixDuration` can be set manually rather than use the value from
* {@link AnimationStateData#getMix()}. In that case, the `mixDuration` can be set for a new
* track entry only before {@link AnimationState#update(float)} is first called.
* track entry only before {@link AnimationState#update(float)} is next called.
*
* When using {@link AnimationState#addAnimation()} with a `delay` <= 0, note the
* {@link #delay} is set using the mix duration from the {@link AnimationStateData}, not a mix duration set
@ -963,7 +963,7 @@ export class TrackEntry {
* replaces the values from the lower tracks with the animation values. {@link MixBlend#add} adds the animation values to
* the values from the lower tracks.
*
* The `mixBlend` can be set for a new track entry only before {@link AnimationState#apply()} is first
* The `mixBlend` can be set for a new track entry only before {@link AnimationState#apply()} is next
* called. */
mixBlend = MixBlend.replace;
timelineMode = new Array<number>();
@ -1155,11 +1155,18 @@ export enum EventType {
/** The interface to implement for receiving TrackEntry events. It is always safe to call AnimationState methods when receiving
* events.
*
* <p>
* TrackEntry events are collected during {@link AnimationState#update} and {@link AnimationState#apply} and
* fired only after those methods are finished.
* <p>
* See TrackEntry {@link TrackEntry#listener} and AnimationState
* {@link AnimationState#addListener()}. */
* {@link AnimationState#addListener}. */
export interface AnimationStateListener {
/** Invoked when this entry has been set as the current entry. */
/** Invoked when this entry has been set as the current entry. {@link #end(TrackEntry)} will occur when this entry will no
* longer be applied.
* <p>
* When this event is triggered by calling {@link AnimationState#setAnimation}, take care not to
* call {@link AnimationState#update} until after the TrackEntry has been configured. */
start?: (entry: TrackEntry) => void;
/** Invoked when another entry has replaced this entry as the current entry. This entry may continue being applied for

View File

@ -132,7 +132,7 @@ export class Bone implements Updatable {
}
/** Computes the world transform using the parent bone and this bone's local applied transform. */
update (physics: Physics) {
update (physics: Physics | null) {
this.updateWorldTransformWith(this.ax, this.ay, this.arotation, this.ascaleX, this.ascaleY, this.ashearX, this.ashearY);
}

View File

@ -172,7 +172,7 @@ export class PathConstraint implements Updatable {
}
}
let positions = this.computeWorldPositions(<PathAttachment>attachment, spacesCount, tangents);
let positions = this.computeWorldPositions(attachment, spacesCount, tangents);
let boneX = positions[0], boneY = positions[1], offsetRotation = data.offsetRotation;
let tip = false;
if (offsetRotation == 0)

View File

@ -39,13 +39,8 @@ import { MathUtils } from "./Utils.js";
* See <a href="http://esotericsoftware.com/spine-physics-constraints">Physics constraints</a> in the Spine User Guide. */
export class PhysicsConstraint implements Updatable {
readonly data: PhysicsConstraintData;
private _bone: Bone | null = null;
/** The bone constrained by this physics constraint. */
public set bone (bone: Bone) { this._bone = bone; }
public get bone () {
if (!this._bone) throw new Error("Bone not set.")
else return this._bone;
}
bone: Bone;
inertia = 0;
strength = 0;
damping = 0;
@ -80,6 +75,8 @@ export class PhysicsConstraint implements Updatable {
this.data = data;
this.skeleton = skeleton;
let bone = skeleton.findBone(data.bone.name);
if (!bone) throw new Error(`Couldn't find bone ${data.bone.name}.`);
this.bone = skeleton.bones[data.bone.index];
this.inertia = data.inertia;

View File

@ -263,7 +263,8 @@ export class Skeleton {
}
sortPathConstraint (constraint: PathConstraint) {
constraint.active = constraint.target.bone.isActive() && (!constraint.data.skinRequired || (this.skin && Utils.contains(this.skin.constraints, constraint.data, true)))!;
constraint.active = constraint.target.bone.isActive()
&& (!constraint.data.skinRequired || (this.skin && Utils.contains(this.skin.constraints, constraint.data, true)))!;
if (!constraint.active) return;
let slot = constraint.target;
@ -275,8 +276,7 @@ export class Skeleton {
for (let i = 0, n = this.data.skins.length; i < n; i++)
this.sortPathConstraintAttachment(this.data.skins[i], slotIndex, slotBone);
let attachment = slot.getAttachment();
if (attachment instanceof PathAttachment) this.sortPathConstraintAttachmentWith(attachment, slotBone);
this.sortPathConstraintAttachmentWith(slot.attachment, slotBone);
let constrained = constraint.bones;
let boneCount = constrained.length;
@ -327,9 +327,9 @@ export class Skeleton {
}
}
sortPathConstraintAttachmentWith (attachment: Attachment, slotBone: Bone) {
sortPathConstraintAttachmentWith (attachment: Attachment | null, slotBone: Bone) {
if (!(attachment instanceof PathAttachment)) return;
let pathBones = (<PathAttachment>attachment).bones;
let pathBones = attachment.bones;
if (!pathBones)
this.sortBone(slotBone);
else {
@ -643,11 +643,10 @@ export class Skeleton {
attachment.computeWorldVertices(slot, vertices, 0, 2);
triangles = Skeleton.quadTriangles;
} else if (attachment instanceof MeshAttachment) {
let mesh = (<MeshAttachment>attachment);
verticesLength = mesh.worldVerticesLength;
verticesLength = attachment.worldVerticesLength;
vertices = Utils.setArraySize(temp, verticesLength, 0);
mesh.computeWorldVertices(slot, 0, verticesLength, vertices, 0, 2);
triangles = mesh.triangles;
attachment.computeWorldVertices(slot, 0, verticesLength, vertices, 0, 2);
triangles = attachment.triangles;
} else if (attachment instanceof ClippingAttachment && clipper != null) {
clipper.clipStart(slot, attachment);
continue;

View File

@ -78,15 +78,14 @@ export class SkeletonBounds {
if (!slot.bone.active) continue;
let attachment = slot.getAttachment();
if (attachment instanceof BoundingBoxAttachment) {
let boundingBox = attachment as BoundingBoxAttachment;
boundingBoxes.push(boundingBox);
boundingBoxes.push(attachment);
let polygon = polygonPool.obtain();
if (polygon.length != boundingBox.worldVerticesLength) {
polygon = Utils.newFloatArray(boundingBox.worldVerticesLength);
if (polygon.length != attachment.worldVerticesLength) {
polygon = Utils.newFloatArray(attachment.worldVerticesLength);
}
polygons.push(polygon);
boundingBox.computeWorldVertices(slot, 0, boundingBox.worldVerticesLength, polygon, 0, 2);
attachment.computeWorldVertices(slot, 0, attachment.worldVerticesLength, polygon, 0, 2);
}
}

View File

@ -91,7 +91,7 @@ export class Slot {
setAttachment (attachment: Attachment | null) {
if (this.attachment == attachment) return;
if (!(attachment instanceof VertexAttachment) || !(this.attachment instanceof VertexAttachment)
|| (<VertexAttachment>attachment).timelineAttachment != (<VertexAttachment>this.attachment).timelineAttachment) {
|| attachment.timelineAttachment != this.attachment.timelineAttachment) {
this.deform.length = 0;
}
this.attachment = attachment;

View File

@ -88,11 +88,11 @@ export class SkeletonDebugRenderer implements Disposable {
let slots = skeleton.slots;
for (let i = 0, n = slots.length; i < n; i++) {
let slot = slots[i];
if (!slot.bone.active) continue;
let attachment = slot.getAttachment();
if (attachment instanceof RegionAttachment) {
let regionAttachment = <RegionAttachment>attachment;
let vertices = this.vertices;
regionAttachment.computeWorldVertices(slot, vertices, 0, 2);
attachment.computeWorldVertices(slot, vertices, 0, 2);
shapes.line(vertices[0], vertices[1], vertices[2], vertices[3]);
shapes.line(vertices[2], vertices[3], vertices[4], vertices[5]);
shapes.line(vertices[4], vertices[5], vertices[6], vertices[7]);
@ -108,11 +108,10 @@ export class SkeletonDebugRenderer implements Disposable {
if (!slot.bone.active) continue;
let attachment = slot.getAttachment();
if (!(attachment instanceof MeshAttachment)) continue;
let mesh = <MeshAttachment>attachment;
let vertices = this.vertices;
mesh.computeWorldVertices(slot, 0, mesh.worldVerticesLength, vertices, 0, 2);
let triangles = mesh.triangles;
let hullLength = mesh.hullLength;
attachment.computeWorldVertices(slot, 0, attachment.worldVerticesLength, vertices, 0, 2);
let triangles = attachment.triangles;
let hullLength = attachment.hullLength;
if (this.drawMeshTriangles) {
shapes.setColor(this.triangleLineColor);
for (let ii = 0, nn = triangles.length; ii < nn; ii += 3) {
@ -158,13 +157,12 @@ export class SkeletonDebugRenderer implements Disposable {
if (!slot.bone.active) continue;
let attachment = slot.getAttachment();
if (!(attachment instanceof PathAttachment)) continue;
let path = <PathAttachment>attachment;
let nn = path.worldVerticesLength;
let nn = attachment.worldVerticesLength;
let world = this.temp = Utils.setArraySize(this.temp, nn, 0);
path.computeWorldVertices(slot, 0, nn, world, 0, 2);
attachment.computeWorldVertices(slot, 0, nn, world, 0, 2);
let color = this.pathColor;
let x1 = world[2], y1 = world[3], x2 = 0, y2 = 0;
if (path.closed) {
if (attachment.closed) {
shapes.setColor(color);
let cx1 = world[0], cy1 = world[1], cx2 = world[nn - 2], cy2 = world[nn - 1];
x2 = world[nn - 4];
@ -207,10 +205,9 @@ export class SkeletonDebugRenderer implements Disposable {
if (!slot.bone.active) continue;
let attachment = slot.getAttachment();
if (!(attachment instanceof ClippingAttachment)) continue;
let clip = <ClippingAttachment>attachment;
let nn = clip.worldVerticesLength;
let nn = attachment.worldVerticesLength;
let world = this.temp = Utils.setArraySize(this.temp, nn, 0);
clip.computeWorldVertices(slot, 0, nn, world, 0, 2);
attachment.computeWorldVertices(slot, 0, nn, world, 0, 2);
for (let i = 0, n = world.length; i < n; i += 2) {
let x = world[i];
let y = world[i + 1];