[ts][canvas] Minor fix to triangle renderer, improve examples

This commit is contained in:
Mario Zechner 2024-07-05 17:53:48 +02:00
parent 4f1cdb3c88
commit 1df497e1e7
5 changed files with 28 additions and 23 deletions

View File

@ -448,6 +448,7 @@ cp -f ../sack/export/sack-pma.png "$ROOT/spine-ts/spine-webgl/example/assets/"
cp -f ../celestial-circus/export/* "$ROOT/spine-ts/spine-webgl/example/assets/" cp -f ../celestial-circus/export/* "$ROOT/spine-ts/spine-webgl/example/assets/"
rm "$ROOT/spine-ts/spine-canvas/example/assets/"* rm "$ROOT/spine-ts/spine-canvas/example/assets/"*
cp -f ../spineboy/export/spineboy-pro.skel "$ROOT/spine-ts/spine-canvas/example/assets/"
cp -f ../spineboy/export/spineboy-ess.json "$ROOT/spine-ts/spine-canvas/example/assets/" cp -f ../spineboy/export/spineboy-ess.json "$ROOT/spine-ts/spine-canvas/example/assets/"
cp -f ../spineboy/export/spineboy.atlas "$ROOT/spine-ts/spine-canvas/example/assets/" cp -f ../spineboy/export/spineboy.atlas "$ROOT/spine-ts/spine-canvas/example/assets/"
cp -f ../spineboy/export/spineboy.png "$ROOT/spine-ts/spine-canvas/example/assets/" cp -f ../spineboy/export/spineboy.png "$ROOT/spine-ts/spine-canvas/example/assets/"

Binary file not shown.

View File

@ -21,19 +21,19 @@
canvas = document.getElementById("canvas"); canvas = document.getElementById("canvas");
context = canvas.getContext("2d"); context = canvas.getContext("2d");
skeletonRenderer = new spine.SkeletonRenderer(context); skeletonRenderer = new spine.SkeletonRenderer(context);
// skeletonRenderer.triangleRendering = true; skeletonRenderer.triangleRendering = true;
// Load the assets. // Load the assets.
assetManager = new spine.AssetManager("https://esotericsoftware.com/files/examples/4.0/spineboy/export/"); assetManager = new spine.AssetManager("assets/");
assetManager.loadText("spineboy-ess.json"); assetManager.loadBinary("spineboy-pro.skel");
assetManager.loadTextureAtlas("spineboy.atlas"); assetManager.loadTextureAtlas("spineboy.atlas");
await assetManager.loadAll(); await assetManager.loadAll();
// Create the texture atlas and skeleton data. // Create the texture atlas and skeleton data.
let atlas = assetManager.require("spineboy.atlas"); let atlas = assetManager.require("spineboy.atlas");
let atlasLoader = new spine.AtlasAttachmentLoader(atlas); let atlasLoader = new spine.AtlasAttachmentLoader(atlas);
let skeletonJson = new spine.SkeletonJson(atlasLoader); let skeletonBinary = new spine.SkeletonBinary(atlasLoader);
let skeletonData = skeletonJson.readSkeletonData(assetManager.require("spineboy-ess.json")); let skeletonData = skeletonBinary.readSkeletonData(assetManager.require("spineboy-pro.skel"));
// Instantiate a new skeleton based on the atlas and skeleton data. // Instantiate a new skeleton based on the atlas and skeleton data.
skeleton = new spine.Skeleton(skeletonData); skeleton = new spine.Skeleton(skeletonData);

View File

@ -24,8 +24,8 @@
skeletonRenderer.triangleRendering = true; skeletonRenderer.triangleRendering = true;
// Load the assets. // Load the assets.
assetManager = new spine.AssetManager("https://esotericsoftware.com/files/examples/4.0/spineboy/export/"); assetManager = new spine.AssetManager("assets/");
assetManager.loadText("spineboy-pro.json"); assetManager.loadText("spineboy-ess.json");
assetManager.loadTextureAtlas("spineboy.atlas"); assetManager.loadTextureAtlas("spineboy.atlas");
await assetManager.loadAll(); await assetManager.loadAll();
@ -33,7 +33,7 @@
let atlas = assetManager.require("spineboy.atlas"); let atlas = assetManager.require("spineboy.atlas");
let atlasLoader = new spine.AtlasAttachmentLoader(atlas); let atlasLoader = new spine.AtlasAttachmentLoader(atlas);
let skeletonJson = new spine.SkeletonJson(atlasLoader); let skeletonJson = new spine.SkeletonJson(atlasLoader);
let skeletonData = skeletonJson.readSkeletonData(assetManager.require("spineboy-pro.json")); let skeletonData = skeletonJson.readSkeletonData(assetManager.require("spineboy-ess.json"));
// Instantiate a new skeleton based on the atlas and skeleton data. // Instantiate a new skeleton based on the atlas and skeleton data.
skeleton = new spine.Skeleton(skeletonData); skeleton = new spine.Skeleton(skeletonData);

View File

@ -178,12 +178,14 @@ export class SkeletonRenderer {
x2: number, y2: number, u2: number, v2: number) { x2: number, y2: number, u2: number, v2: number) {
let ctx = this.ctx; let ctx = this.ctx;
u0 *= img.width; const width = img.width - 1;
v0 *= img.height; const height = img.height - 1;
u1 *= img.width; u0 *= width;
v1 *= img.height; v0 *= height;
u2 *= img.width; u1 *= width;
v2 *= img.height; v1 *= height;
u2 *= width;
v2 *= height;
ctx.beginPath(); ctx.beginPath();
ctx.moveTo(x0, y0); ctx.moveTo(x0, y0);
@ -201,17 +203,19 @@ export class SkeletonRenderer {
u2 -= u0; u2 -= u0;
v2 -= v0; v2 -= v0;
var det = 1 / (u1 * v2 - u2 * v1), let det = u1 * v2 - u2 * v1;
if (det == 0) return;
det = 1 / det;
// linear transformation // linear transformation
a = (v2 * x1 - v1 * x2) * det, const a = (v2 * x1 - v1 * x2) * det;
b = (v2 * y1 - v1 * y2) * det, const b = (v2 * y1 - v1 * y2) * det;
c = (u1 * x2 - u2 * x1) * det, const c = (u1 * x2 - u2 * x1) * det;
d = (u1 * y2 - u2 * y1) * det, const d = (u1 * y2 - u2 * y1) * det;
// translation // translation
e = x0 - a * u0 - c * v0, const e = x0 - a * u0 - c * v0;
f = y0 - b * u0 - d * v0; const f = y0 - b * u0 - d * v0;
ctx.save(); ctx.save();
ctx.transform(a, b, c, d, e, f); ctx.transform(a, b, c, d, e, f);