From 133625559291f71b65918d711e0b92adfd3ae7e6 Mon Sep 17 00:00:00 2001 From: Davide Tantillo Date: Fri, 24 May 2024 16:37:48 +0200 Subject: [PATCH] [ts][pixi] Add methods to transform from/to pixi/spine coordinates. Add control bone example to show usage. --- .../example/control-bones-example.html | 112 ++++++++++++++++++ spine-ts/spine-pixi/src/Spine.ts | 41 +++++++ 2 files changed, 153 insertions(+) create mode 100644 spine-ts/spine-pixi/example/control-bones-example.html diff --git a/spine-ts/spine-pixi/example/control-bones-example.html b/spine-ts/spine-pixi/example/control-bones-example.html new file mode 100644 index 000000000..c28201677 --- /dev/null +++ b/spine-ts/spine-pixi/example/control-bones-example.html @@ -0,0 +1,112 @@ + + + + spine-pixi + + + + + + + + + + \ No newline at end of file diff --git a/spine-ts/spine-pixi/src/Spine.ts b/spine-ts/spine-pixi/src/Spine.ts index 8408f115b..46a18e544 100644 --- a/spine-ts/spine-pixi/src/Spine.ts +++ b/spine-ts/spine-pixi/src/Spine.ts @@ -398,6 +398,47 @@ export class Spine extends Container { return outPos; } + /** Converts a point from the skeleton coordinate system to the Pixi world coordinate system. */ + skeletonToPixiWorldCoordinates (point: { x: number; y: number }) { + let transform = this.worldTransform; + let a = transform.a, + b = transform.b, + c = transform.c, + d = transform.d, + tx = transform.tx, + ty = transform.ty; + let x = point.x; + let y = point.y; + point.x = x * a + y * c + tx; + point.y = x * b + y * d + ty; + } + + /** Converts a point from the Pixi world coordinate system to the skeleton coordinate system. */ + pixiWorldCoordinatesToSkeleton (point: { x: number; y: number }) { + let transform = this.worldTransform.clone(); + transform = transform.invert(); + let a = transform.a, + b = transform.b, + c = transform.c, + d = transform.d, + tx = transform.tx, + ty = transform.ty; + let x = point.x; + let y = point.y; + point.x = x * a + y * c + tx; + point.y = x * b + y * d + ty; + } + + /** Converts a point from the Pixi world coordinate system to the bone's local coordinate system. */ + pixiWorldCoordinatesToBone (point: { x: number; y: number }, bone: Bone) { + this.pixiWorldCoordinatesToSkeleton(point); + if (bone.parent) { + bone.parent.worldToLocal(point as Vector2); + } else { + bone.worldToLocal(point as Vector2); + } + } + /** A cache containing skeleton data and atlases already loaded by {@link Spine.from}. */ public static readonly skeletonCache: Record = Object.create(null);