Skip to content

Commit 329ff5b

Browse files
committed
Changed scaleX and scaleY props meaning, they are now offset ratio with the bounds. Removed usage of skeleton scale, in favor of C3Matrix scale.
Add getter/setter to editor instance for numeric properties. Width and height changes scale the skeleton, not only bbox.
1 parent adfb71b commit 329ff5b

File tree

8 files changed

+177
-108
lines changed

8 files changed

+177
-108
lines changed

spine-ts/spine-construct3/spine-construct3-lib/src/C3Matrix.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,24 @@ export class C3Matrix {
4141
public prevX = Infinity;
4242
public prevY = Infinity;
4343
public prevAngle = Infinity;
44+
public prevScaleX = Infinity;
45+
public prevScaleY = Infinity;
4446

4547
private tempPoint = new Vector2();
4648

47-
public update (x: number, y: number, angle: number) {
48-
if (this.prevX === x && this.prevY === y && this.prevAngle === angle) return false;
49+
public update (x: number, y: number, angle: number, scaleX = 1, scaleY = 1) {
50+
if (this.prevX === x && this.prevY === y && this.prevAngle === angle && this.prevScaleX === scaleX && this.prevScaleY === scaleY) return false;
4951
this.prevX = x;
5052
this.prevY = y;
5153
this.prevAngle = angle;
54+
this.prevScaleX = scaleX;
55+
this.prevScaleY = scaleY;
5256
const cos = Math.cos(angle);
5357
const sin = Math.sin(angle);
54-
this.a = cos;
55-
this.b = sin;
56-
this.c = -sin;
57-
this.d = cos;
58+
this.a = scaleX * cos;
59+
this.b = scaleX * sin;
60+
this.c = -scaleY * sin;
61+
this.d = scaleY * cos;
5862
this.tx = x;
5963
this.ty = y;
6064
return true;

spine-ts/spine-construct3/spine-construct3-lib/src/C3SkeletonRenderer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
* THE SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2828
*****************************************************************************/
2929

30-
import { type BlendMode, type Bone, ClippingAttachment, MathUtils, MeshAttachment, PathAttachment, RegionAttachment, RenderCommand, type Skeleton, SkeletonRendererCore, Utils, Vector2 } from "@esotericsoftware/spine-core";
30+
import { type BlendMode, type Bone, ClippingAttachment, MathUtils, MeshAttachment, PathAttachment, RegionAttachment, type RenderCommand, type Skeleton, SkeletonRendererCore, Utils, Vector2 } from "@esotericsoftware/spine-core";
3131
import type { C3Matrix } from "./C3Matrix";
3232
import { BlendingModeSpineToC3, type C3TextureEditor, type C3TextureRuntime } from "./C3Texture";
3333

spine-ts/spine-construct3/spine-construct3-lib/src/SpineBoundsProvider.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ interface GameObject {
4141
state?: AnimationState,
4242
}
4343

44+
export type SpineBoundsProviderType = "setup" | "animation-skin" | "AABB";
45+
4446
export interface SpineBoundsProvider {
4547
/** Returns the bounding box for the skeleton, in skeleton space. */
4648
calculateBounds (gameObject: GameObject): Rectangle;

spine-ts/spine-construct3/src/c3runtime/instance.ts

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { AnimationState, AnimationStateListener, AssetLoader, Bone, C3Matrix, C3RendererRuntime, Event, NumberArrayLike, RegionAttachment, Skeleton, Skin, Slot, TextureAtlas, } from "@esotericsoftware/spine-construct3-lib";
1+
import type { AnimationState, AnimationStateListener, AssetLoader, Bone, C3Matrix, C3RendererRuntime, Event, NumberArrayLike, RegionAttachment, Skeleton, Skin, Slot, SpineBoundsProvider, SpineBoundsProviderType, TextureAtlas, } from "@esotericsoftware/spine-construct3-lib";
22

33
const C3 = globalThis.C3;
44
const spine = globalThis.spine;
@@ -17,9 +17,10 @@ class SpineC3Instance extends globalThis.ISDKWorldInstanceBase {
1717
propScaleX = 1;
1818
propScaleY = 1;
1919
propDebugSkeleton = false;
20+
propBoundsProvider: SpineBoundsProviderType = "setup";
2021

2122
isFlippedX = false;
22-
isPlaying = false;
23+
isPlaying = true;
2324
animationSpeed = 1.0;
2425
physicsMode = spine.Physics.update;
2526
customSkins: Record<string, Skin> = {};
@@ -43,6 +44,13 @@ class SpineC3Instance extends globalThis.ISDKWorldInstanceBase {
4344
private matrix: C3Matrix;
4445
private requestRedraw = false;
4546

47+
private spineBounds = {
48+
x: 0,
49+
y: 0,
50+
width: 200,
51+
height: 200,
52+
};
53+
4654
private verticesTemp = spine.Utils.newFloatArray(2 * 1024);
4755

4856
private boneFollowers = new Map<string, { uid: number, offsetX: number, offsetY: number, offsetAngle: number }>();
@@ -71,7 +79,9 @@ class SpineC3Instance extends globalThis.ISDKWorldInstanceBase {
7179
this.propSkin = skinProp === "" ? [] : skinProp.split(",");
7280
this.propAnimation = properties[4] as string;
7381
this.propDebugSkeleton = properties[5] as boolean;
74-
82+
const boundsProviderIndex = properties[6] as number;
83+
this.propBoundsProvider = boundsProviderIndex === 0 ? "setup" : "animation-skin";
84+
// properties[7] is PROP_BOUNDS_PROVIDER_MOVE
7585
this.propOffsetX = properties[8] as number;
7686
this.propOffsetY = properties[9] as number;
7787
this.propOffsetAngle = properties[10] as number;
@@ -115,7 +125,9 @@ class SpineC3Instance extends globalThis.ISDKWorldInstanceBase {
115125
this.matrix.update(
116126
this.x + this.propOffsetX,
117127
this.y + this.propOffsetY,
118-
this.angle + this.propOffsetAngle);
128+
this.angle + this.propOffsetAngle,
129+
this.width / this.spineBounds.width * this.propScaleX * (this.isFlippedX ? -1 : 1),
130+
this.height / this.spineBounds.height * this.propScaleY);
119131

120132
if (this.isPlaying) this.update(this.dt);
121133
}
@@ -266,8 +278,8 @@ class SpineC3Instance extends globalThis.ISDKWorldInstanceBase {
266278
pose.y = y;
267279
} else {
268280
const { x, y } = matrix.gameToSkeleton(touchX - handleObject.offsetX, touchY - handleObject.offsetY);
269-
pose.x = x / skeleton.scaleX;
270-
pose.y = -y / skeleton.scaleY * spine.Skeleton.yDir;
281+
pose.x = x;
282+
pose.y = -y * spine.Skeleton.yDir;
271283
}
272284
} else if (!this.prevLeftClickDown) {
273285
const applied = bone.applied;
@@ -422,15 +434,36 @@ class SpineC3Instance extends globalThis.ISDKWorldInstanceBase {
422434

423435
this._setSkin();
424436

425-
this.skeleton.scaleX = this.isFlippedX ? -this.propScaleX : this.propScaleX;
426-
this.skeleton.scaleY = this.propScaleY;
437+
this.calculateBounds();
427438

428439
this.update(0);
429440

430441
this.skeletonLoaded = true;
431442
this._trigger(C3.Plugins.EsotericSoftware_SpineConstruct3.Cnds.OnSkeletonLoaded);
432443
}
433444
}
445+
446+
private calculateBounds () {
447+
const { skeleton } = this;
448+
if (!skeleton) return;
449+
450+
let boundsProvider: SpineBoundsProvider;
451+
console.log(this.propBoundsProvider);
452+
if (this.propBoundsProvider === "animation-skin") {
453+
const { propSkin, propAnimation } = this;
454+
if ((propSkin && propSkin.length > 0) || propAnimation) {
455+
boundsProvider = new spine.SkinsAndAnimationBoundsProvider(propAnimation, propSkin);
456+
} else {
457+
boundsProvider = new spine.SetupPoseBoundsProvider();
458+
}
459+
} else if (this.propBoundsProvider === "setup") {
460+
boundsProvider = new spine.SetupPoseBoundsProvider();
461+
} else {
462+
boundsProvider = new spine.AABBRectangleBoundsProvider(0, 0, 100, 100);
463+
}
464+
465+
this.spineBounds = boundsProvider.calculateBounds(this);
466+
}
434467
/**********/
435468

436469
/*
@@ -742,7 +775,6 @@ class SpineC3Instance extends globalThis.ISDKWorldInstanceBase {
742775
const { x, y } = matrix.boneToGame(bone);
743776
const boneRotation = bone.applied.getWorldRotationX();
744777

745-
// Apply rotation to offset
746778
const rotationRadians = boneRotation * Math.PI / 180;
747779
const cos = Math.cos(rotationRadians);
748780
const sin = Math.sin(rotationRadians);
@@ -912,11 +944,6 @@ class SpineC3Instance extends globalThis.ISDKWorldInstanceBase {
912944

913945
public flipX (isFlippedX: boolean) {
914946
this.isFlippedX = isFlippedX;
915-
916-
const { skeleton } = this;
917-
if (skeleton) {
918-
skeleton.scaleX = isFlippedX ? -this.propScaleX : this.propScaleX;
919-
}
920947
}
921948

922949
public setPhysicsMode (mode: 0 | 1 | 2 | 3) {

0 commit comments

Comments
 (0)