[ts][canvas] Canvas renderer now supports shearing and non-uniform scale. Closes #851

This commit is contained in:
badlogic 2017-03-02 11:04:11 +01:00
parent 30d52282af
commit e8df948d9d
10 changed files with 118 additions and 2566 deletions

View File

@ -20,7 +20,11 @@ The Spine Runtimes are developed with the intent to be used with data exported f
spine-ts works with data exported from Spine 3.5.xx. spine-ts works with data exported from Spine 3.5.xx.
spine-ts WebGL & Widget backends supports all Spine features. The spine-ts Canvas backend does not support color tinting, mesh attachments or shearing. Mesh attachments are supported by setting `spine.canvas.SkeletonRenderer.useTriangleRendering` to true. Note that this method is slow and may lead to artifacts on some browsers. The spine-ts THREE.JS backend does not support color tinting and blend modes. The THREE.JS backend provides `SkeletonMesh.zOffset` to avoid z-fighting. Adjust to your near/far plane settings. spine-ts WebGL & Widget backends supports all Spine features.
spine-ts Canvas does not support color tinting and mesh attachments. Experimental support for mesh attachments can be enabled by setting `spine.canvas.SkeletonRenderer.useTriangleRendering` to true. Note that this method is slow and may lead to artifacts on some browsers.
spine-ts THREE.JS does not support color tinting and blend modes. The THREE.JS backend provides `SkeletonMesh.zOffset` to avoid z-fighting. Adjust to your near/far plane settings.
spine-ts does not yet support loading the binary format. spine-ts does not yet support loading the binary format.

View File

@ -231,10 +231,8 @@ var spine;
var attachment = slot.getAttachment(); var attachment = slot.getAttachment();
var region = null; var region = null;
var image = null; var image = null;
var vertices = null;
if (attachment instanceof spine.RegionAttachment) { if (attachment instanceof spine.RegionAttachment) {
var regionAttachment = attachment; var regionAttachment = attachment;
vertices = regionAttachment.updateWorldVertices(slot, false);
region = regionAttachment.region; region = regionAttachment.region;
image = (region).texture.getImage(); image = (region).texture.getImage();
} }
@ -242,28 +240,22 @@ var spine;
continue; continue;
var att = attachment; var att = attachment;
var bone = slot.bone; var bone = slot.bone;
var x = vertices[0]; var w = region.width;
var y = vertices[1]; var h = region.height;
var rotation = (bone.getWorldRotationX() - att.rotation) * Math.PI / 180; var offsetX = attachment.offset[0];
var xx = vertices[24] - vertices[0]; var offsetY = attachment.offset[1];
var xy = vertices[25] - vertices[1]; ctx.save();
var yx = vertices[8] - vertices[0]; ctx.transform(bone.a, bone.c, bone.b, bone.d, bone.worldX, bone.worldY);
var yy = vertices[9] - vertices[1]; ctx.translate(offsetX, offsetY);
var w = Math.sqrt(xx * xx + xy * xy), h = -Math.sqrt(yx * yx + yy * yy); ctx.rotate(attachment.rotation * Math.PI / 180);
ctx.translate(x, y); ctx.scale(attachment.scaleX, attachment.scaleY);
ctx.rotate(rotation); ctx.translate(region.width / 2, region.height / 2);
if (region.rotate) { ctx.scale(1, -1);
ctx.rotate(Math.PI / 2); ctx.translate(-region.width / 2, -region.height / 2);
ctx.drawImage(image, region.x, region.y, region.height, region.width, 0, 0, h, -w); ctx.drawImage(image, region.x, region.y, region.width, region.height, 0, 0, w, h);
ctx.rotate(-Math.PI / 2);
}
else {
ctx.drawImage(image, region.x, region.y, region.width, region.height, 0, 0, w, h);
}
if (this.debugRendering) if (this.debugRendering)
ctx.strokeRect(0, 0, w, h); ctx.strokeRect(0, 0, w, h);
ctx.rotate(-rotation); ctx.restore();
ctx.translate(-x, -y);
} }
}; };
SkeletonRenderer.prototype.drawTriangles = function (skeleton) { SkeletonRenderer.prototype.drawTriangles = function (skeleton) {

File diff suppressed because one or more lines are too long

View File

@ -231,10 +231,8 @@ var spine;
var attachment = slot.getAttachment(); var attachment = slot.getAttachment();
var region = null; var region = null;
var image = null; var image = null;
var vertices = null;
if (attachment instanceof spine.RegionAttachment) { if (attachment instanceof spine.RegionAttachment) {
var regionAttachment = attachment; var regionAttachment = attachment;
vertices = regionAttachment.updateWorldVertices(slot, false);
region = regionAttachment.region; region = regionAttachment.region;
image = (region).texture.getImage(); image = (region).texture.getImage();
} }
@ -242,28 +240,22 @@ var spine;
continue; continue;
var att = attachment; var att = attachment;
var bone = slot.bone; var bone = slot.bone;
var x = vertices[0]; var w = region.width;
var y = vertices[1]; var h = region.height;
var rotation = (bone.getWorldRotationX() - att.rotation) * Math.PI / 180; var offsetX = attachment.offset[0];
var xx = vertices[24] - vertices[0]; var offsetY = attachment.offset[1];
var xy = vertices[25] - vertices[1]; ctx.save();
var yx = vertices[8] - vertices[0]; ctx.transform(bone.a, bone.c, bone.b, bone.d, bone.worldX, bone.worldY);
var yy = vertices[9] - vertices[1]; ctx.translate(offsetX, offsetY);
var w = Math.sqrt(xx * xx + xy * xy), h = -Math.sqrt(yx * yx + yy * yy); ctx.rotate(attachment.rotation * Math.PI / 180);
ctx.translate(x, y); ctx.scale(attachment.scaleX, attachment.scaleY);
ctx.rotate(rotation); ctx.translate(region.width / 2, region.height / 2);
if (region.rotate) { ctx.scale(1, -1);
ctx.rotate(Math.PI / 2); ctx.translate(-region.width / 2, -region.height / 2);
ctx.drawImage(image, region.x, region.y, region.height, region.width, 0, 0, h, -w); ctx.drawImage(image, region.x, region.y, region.width, region.height, 0, 0, w, h);
ctx.rotate(-Math.PI / 2);
}
else {
ctx.drawImage(image, region.x, region.y, region.width, region.height, 0, 0, w, h);
}
if (this.debugRendering) if (this.debugRendering)
ctx.strokeRect(0, 0, w, h); ctx.strokeRect(0, 0, w, h);
ctx.rotate(-rotation); ctx.restore();
ctx.translate(-x, -y);
} }
}; };
SkeletonRenderer.prototype.drawTriangles = function (skeleton) { SkeletonRenderer.prototype.drawTriangles = function (skeleton) {

File diff suppressed because one or more lines are too long

View File

@ -5,141 +5,120 @@ format: RGBA8888
filter: Linear,Linear filter: Linear,Linear
repeat: none repeat: none
eye_indifferent eye_indifferent
rotate: true rotate: false
xy: 648, 629 xy: 275, 349
size: 93, 89 size: 93, 89
orig: 93, 89 orig: 93, 89
offset: 0, 0 offset: 0, 0
index: -1 index: -1
eye_surprised eye_surprised
rotate: true rotate: false
xy: 233, 179 xy: 214, 125
size: 93, 89 size: 93, 89
orig: 93, 89 orig: 93, 89
offset: 0, 0 offset: 0, 0
index: -1 index: -1
front_bracer front_bracer
rotate: false rotate: false
xy: 245, 2 xy: 678, 774
size: 58, 80 size: 58, 80
orig: 58, 80 orig: 58, 80
offset: 0, 0 offset: 0, 0
index: -1 index: -1
front_fist_closed front_fist_closed
rotate: false rotate: false
xy: 168, 45 xy: 944, 940
size: 75, 82 size: 75, 82
orig: 75, 82 orig: 75, 82
offset: 0, 0 offset: 0, 0
index: -1 index: -1
front_fist_open front_fist_open
rotate: false rotate: false
xy: 844, 646 xy: 132, 28
size: 86, 87 size: 86, 87
orig: 86, 87 orig: 86, 87
offset: 0, 0 offset: 0, 0
index: -1 index: -1
front_foot front_foot
rotate: true rotate: false
xy: 310, 326 xy: 550, 785
size: 126, 69 size: 126, 69
orig: 126, 69 orig: 126, 69
offset: 0, 0 offset: 0, 0
index: -1 index: -1
front_foot_bend1 front_foot_bend1
rotate: true rotate: false
xy: 951, 894 xy: 2, 45
size: 128, 70 size: 128, 70
orig: 128, 70 orig: 128, 70
offset: 0, 0 offset: 0, 0
index: -1 index: -1
front_foot_bend2 front_foot_bend2
rotate: false rotate: false
xy: 2, 33 xy: 729, 929
size: 108, 93 size: 108, 93
orig: 108, 93 orig: 108, 93
offset: 0, 0 offset: 0, 0
index: -1 index: -1
front_shin front_shin
rotate: true rotate: false
xy: 739, 735 xy: 466, 670
size: 82, 184 size: 82, 184
orig: 82, 184 orig: 82, 184
offset: 0, 0 offset: 0, 0
index: -1 index: -1
front_thigh front_thigh
rotate: false rotate: false
xy: 381, 340 xy: 281, 235
size: 48, 112 size: 48, 112
orig: 48, 112 orig: 48, 112
offset: 0, 0 offset: 0, 0
index: -1 index: -1
front_upper_arm front_upper_arm
rotate: false rotate: false
xy: 112, 29 xy: 220, 26
size: 54, 97 size: 54, 97
orig: 54, 97 orig: 54, 97
offset: 0, 0 offset: 0, 0
index: -1 index: -1
goggles goggles
rotate: false rotate: false
xy: 156, 454 xy: 466, 856
size: 261, 166 size: 261, 166
orig: 261, 166 orig: 261, 166
offset: 0, 0 offset: 0, 0
index: -1 index: -1
gun gun
rotate: false rotate: false
xy: 739, 819 xy: 2, 117
size: 210, 203 size: 210, 203
orig: 210, 203 orig: 210, 203
offset: 0, 0 offset: 0, 0
index: -1 index: -1
head head
rotate: false rotate: false
xy: 466, 724 xy: 2, 322
size: 271, 298 size: 271, 298
orig: 271, 298 orig: 271, 298
offset: 0, 0 offset: 0, 0
index: -1 index: -1
hoverboard_board
rotate: true
xy: 2, 128
size: 492, 152
orig: 492, 152
offset: 0, 0
index: -1
hoverboard_thruster
rotate: false
xy: 602, 558
size: 60, 64
orig: 60, 64
offset: 0, 0
index: -1
hoverglow_small
rotate: true
xy: 156, 178
size: 274, 75
orig: 274, 75
offset: 0, 0
index: -1
mouth_grind mouth_grind
rotate: true rotate: false
xy: 951, 799 xy: 844, 878
size: 93, 59 size: 93, 59
orig: 93, 59 orig: 93, 59
offset: 0, 0 offset: 0, 0
index: -1 index: -1
mouth_oooo mouth_oooo
rotate: true rotate: false
xy: 245, 84 xy: 550, 656
size: 93, 59 size: 93, 59
orig: 93, 59 orig: 93, 59
offset: 0, 0 offset: 0, 0
index: -1 index: -1
mouth_smile mouth_smile
rotate: false rotate: false
xy: 925, 738 xy: 738, 806
size: 93, 59 size: 93, 59
orig: 93, 59 orig: 93, 59
offset: 0, 0 offset: 0, 0
@ -153,63 +132,63 @@ muzzle
index: -1 index: -1
neck neck
rotate: false rotate: false
xy: 168, 2 xy: 2, 2
size: 36, 41 size: 36, 41
orig: 36, 41 orig: 36, 41
offset: 0, 0 offset: 0, 0
index: -1 index: -1
rear_bracer rear_bracer
rotate: false rotate: false
xy: 932, 664 xy: 276, 51
size: 56, 72 size: 56, 72
orig: 56, 72 orig: 56, 72
offset: 0, 0 offset: 0, 0
index: -1 index: -1
rear_foot rear_foot
rotate: false rotate: false
xy: 487, 562 xy: 729, 867
size: 113, 60 size: 113, 60
orig: 113, 60 orig: 113, 60
offset: 0, 0 offset: 0, 0
index: -1 index: -1
rear_foot_bend1 rear_foot_bend1
rotate: true rotate: false
xy: 419, 503 xy: 550, 717
size: 117, 66 size: 117, 66
orig: 117, 66 orig: 117, 66
offset: 0, 0 offset: 0, 0
index: -1 index: -1
rear_foot_bend2 rear_foot_bend2
rotate: false rotate: false
xy: 739, 650 xy: 839, 939
size: 103, 83 size: 103, 83
orig: 103, 83 orig: 103, 83
offset: 0, 0 offset: 0, 0
index: -1 index: -1
rear_shin rear_shin
rotate: false rotate: false
xy: 233, 274 xy: 375, 442
size: 75, 178 size: 75, 178
orig: 75, 178 orig: 75, 178
offset: 0, 0 offset: 0, 0
index: -1 index: -1
rear_thigh rear_thigh
rotate: true rotate: false
xy: 487, 495 xy: 214, 216
size: 65, 104 size: 65, 104
orig: 65, 104 orig: 65, 104
offset: 0, 0 offset: 0, 0
index: -1 index: -1
rear_upper_arm rear_upper_arm
rotate: true rotate: false
xy: 156, 129 xy: 331, 260
size: 47, 87 size: 47, 87
orig: 47, 87 orig: 47, 87
offset: 0, 0 offset: 0, 0
index: -1 index: -1
torso torso
rotate: true rotate: false
xy: 466, 624 xy: 275, 440
size: 98, 180 size: 98, 180
orig: 98, 180 orig: 98, 180
offset: 0, 0 offset: 0, 0

File diff suppressed because one or more lines are too long

Binary file not shown.

Before

Width:  |  Height:  |  Size: 678 KiB

After

Width:  |  Height:  |  Size: 595 KiB

View File

@ -17,6 +17,9 @@ var assetManager;
var skeleton, state, bounds; var skeleton, state, bounds;
var skeletonRenderer; var skeletonRenderer;
var skelName = "spineboy";
var animName = "idle";
function init () { function init () {
canvas = document.getElementById("canvas"); canvas = document.getElementById("canvas");
canvas.width = window.innerWidth; canvas.width = window.innerWidth;
@ -31,16 +34,16 @@ function init () {
assetManager = new spine.canvas.AssetManager(); assetManager = new spine.canvas.AssetManager();
assetManager.loadText("assets/spineboy.json"); assetManager.loadText("assets/" + skelName + ".json");
assetManager.loadText("assets/spineboy.atlas"); assetManager.loadText("assets/" + skelName + ".atlas");
assetManager.loadTexture("assets/spineboy.png"); assetManager.loadTexture("assets/" + skelName + ".png");
requestAnimationFrame(load); requestAnimationFrame(load);
} }
function load () { function load () {
if (assetManager.isLoadingComplete()) { if (assetManager.isLoadingComplete()) {
var data = loadSkeleton("spineboy", "walk", "default"); var data = loadSkeleton(skelName, animName, "default");
skeleton = data.skeleton; skeleton = data.skeleton;
state = data.state; state = data.state;
bounds = data.bounds; bounds = data.bounds;
@ -122,6 +125,14 @@ function render () {
skeleton.updateWorldTransform(); skeleton.updateWorldTransform();
skeletonRenderer.draw(skeleton); skeletonRenderer.draw(skeleton);
context.strokeStyle = "green";
context.beginPath();
context.moveTo(-1000, 0);
context.lineTo(1000, 0);
context.moveTo(0, -1000);
context.lineTo(0, 1000);
context.stroke();
requestAnimationFrame(render); requestAnimationFrame(render);
} }
@ -140,8 +151,8 @@ function resize () {
var scaleY = bounds.size.y / canvas.height; var scaleY = bounds.size.y / canvas.height;
var scale = Math.max(scaleX, scaleY) * 1.2; var scale = Math.max(scaleX, scaleY) * 1.2;
if (scale < 1) scale = 1; if (scale < 1) scale = 1;
var width = canvas.width * scale; var width = canvas.width;
var height = canvas.height * scale; var height = canvas.height;
context.resetTransform(); context.resetTransform();
context.scale(1 / scale, 1 / scale); context.scale(1 / scale, 1 / scale);

View File

@ -57,37 +57,29 @@ module spine.canvas {
let attachment = slot.getAttachment(); let attachment = slot.getAttachment();
let region: TextureAtlasRegion = null; let region: TextureAtlasRegion = null;
let image: HTMLImageElement = null; let image: HTMLImageElement = null;
let vertices: ArrayLike<number> = null;
if (attachment instanceof RegionAttachment) { if (attachment instanceof RegionAttachment) {
let regionAttachment = <RegionAttachment>attachment; let regionAttachment = <RegionAttachment>attachment;
vertices = regionAttachment.updateWorldVertices(slot, false);
region = <TextureAtlasRegion>regionAttachment.region; region = <TextureAtlasRegion>regionAttachment.region;
image = (<CanvasTexture>(region).texture).getImage(); image = (<CanvasTexture>(region).texture).getImage();
} else continue; } else continue;
let att = <RegionAttachment>attachment; let att = <RegionAttachment>attachment;
let bone = slot.bone; let bone = slot.bone;
let x = vertices[0]; let w = region.width;
let y = vertices[1]; let h = region.height;
let rotation = (bone.getWorldRotationX() - att.rotation) * Math.PI / 180; let offsetX = attachment.offset[0];
let xx = vertices[24] - vertices[0]; let offsetY = attachment.offset[1];
let xy = vertices[25] - vertices[1]; ctx.save();
let yx = vertices[8] - vertices[0]; ctx.transform(bone.a, bone.c, bone.b, bone.d, bone.worldX, bone.worldY);
let yy = vertices[9] - vertices[1]; ctx.translate(offsetX, offsetY);
let w = Math.sqrt(xx * xx + xy * xy), h = -Math.sqrt(yx * yx + yy * yy); ctx.rotate(attachment.rotation * Math.PI / 180);
ctx.translate(x, y); ctx.scale(attachment.scaleX, attachment.scaleY);
ctx.rotate(rotation); ctx.translate(region.width / 2, region.height / 2);
if (region.rotate) { ctx.scale(1, -1);
ctx.rotate(Math.PI / 2); ctx.translate(-region.width / 2, -region.height / 2);
ctx.drawImage(image, region.x, region.y, region.height, region.width, 0, 0, h, -w); ctx.drawImage(image, region.x, region.y, region.width, region.height, 0, 0, w, h);
ctx.rotate(-Math.PI / 2);
} else {
ctx.drawImage(image, region.x, region.y, region.width, region.height, 0, 0, w, h);
}
if (this.debugRendering) ctx.strokeRect(0, 0, w, h); if (this.debugRendering) ctx.strokeRect(0, 0, w, h);
ctx.rotate(-rotation); ctx.restore();
ctx.translate(-x, -y);
} }
} }