Added bone inherit rotation and scale, and slot additive blending.

This commit is contained in:
NathanSweet 2013-05-22 06:44:46 +02:00
parent 27d6766289
commit 8c3a4e4761
7 changed files with 53 additions and 5 deletions

View File

@ -69,9 +69,14 @@ public class Bone {
if (parent != null) {
worldX = x * parent.m00 + y * parent.m01 + parent.worldX;
worldY = x * parent.m10 + y * parent.m11 + parent.worldY;
worldScaleX = parent.worldScaleX * scaleX;
worldScaleY = parent.worldScaleY * scaleY;
worldRotation = parent.worldRotation + rotation;
if (data.inheritScale) {
worldScaleX = parent.worldScaleX * scaleX;
worldScaleY = parent.worldScaleY * scaleY;
} else {
worldScaleX = scaleX;
worldScaleY = scaleY;
}
worldRotation = data.inheritRotation ? parent.worldRotation + rotation : rotation;
} else {
worldX = x;
worldY = y;

View File

@ -32,6 +32,7 @@ public class BoneData {
float x, y;
float rotation;
float scaleX = 1, scaleY = 1;
boolean inheritScale = true, inheritRotation = true;
/** @param parent May be null. */
public BoneData (String name, BoneData parent) {
@ -111,6 +112,22 @@ public class BoneData {
this.scaleY = scaleY;
}
public boolean getInheritScale () {
return inheritScale;
}
public void setInheritScale (boolean inheritScale) {
this.inheritScale = inheritScale;
}
public boolean getInheritRotation () {
return inheritRotation;
}
public void setInheritRotation (boolean inheritRotation) {
this.inheritRotation = inheritRotation;
}
public String toString () {
return name;
}

View File

@ -98,7 +98,7 @@ public class SkeletonBinary {
String parentName = input.readString();
if (parentName != null) {
parent = skeletonData.findBone(parentName);
if (parent == null) throw new SerializationException("Bone not found: " + parentName);
if (parent == null) throw new SerializationException("Parent bone not found: " + parentName);
}
BoneData boneData = new BoneData(name, parent);
boneData.x = input.readFloat() * scale;
@ -107,6 +107,8 @@ public class SkeletonBinary {
boneData.scaleY = input.readFloat();
boneData.rotation = input.readFloat();
boneData.length = input.readFloat() * scale;
boneData.inheritScale = input.readByte() == 1;
boneData.inheritRotation = input.readByte() == 1;
skeletonData.addBone(boneData);
}
@ -119,6 +121,7 @@ public class SkeletonBinary {
SlotData slotData = new SlotData(slotName, boneData);
Color.rgba8888ToColor(slotData.getColor(), input.readInt());
slotData.setAttachmentName(input.readString());
slotData.additiveBlending = input.readByte() == 1;
skeletonData.addSlot(slotData);
}

View File

@ -100,6 +100,8 @@ public class SkeletonJson {
boneData.rotation = boneMap.getFloat("rotation", 0);
boneData.scaleX = boneMap.getFloat("scaleX", 1);
boneData.scaleY = boneMap.getFloat("scaleY", 1);
boneData.inheritScale = boneMap.getBoolean("inheritScale", true);
boneData.inheritRotation = boneMap.getBoolean("inheritRotation", true);
skeletonData.addBone(boneData);
}
@ -116,6 +118,8 @@ public class SkeletonJson {
slotData.setAttachmentName(slotMap.getString("attachment", null));
slotData.additiveBlending = slotMap.getBoolean("additive", false);
skeletonData.addSlot(slotData);
}

View File

@ -4,12 +4,14 @@ package com.esotericsoftware.spine;
import com.esotericsoftware.spine.attachments.Attachment;
import com.esotericsoftware.spine.attachments.RegionAttachment;
import com.badlogic.gdx.graphics.GL11;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.utils.Array;
public class SkeletonRenderer {
public void draw (SpriteBatch batch, Skeleton skeleton) {
Array<Slot> drawOrder = skeleton.drawOrder;
boolean additive = false;
for (int i = 0, n = drawOrder.size; i < n; i++) {
Slot slot = drawOrder.get(i);
Attachment attachment = slot.attachment;
@ -17,8 +19,16 @@ public class SkeletonRenderer {
RegionAttachment regionAttachment = (RegionAttachment)attachment;
regionAttachment.updateVertices(slot);
float[] vertices = regionAttachment.getVertices();
if (slot.data.getAdditiveBlending() != additive) {
additive = !additive;
if (additive)
batch.setBlendFunction(GL11.GL_SRC_ALPHA, GL11.GL_ONE);
else
batch.setBlendFunction(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
}
batch.draw(regionAttachment.getRegion().getTexture(), vertices, 0, vertices.length);
}
}
if (additive) batch.setBlendFunction(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
}
}

View File

@ -32,6 +32,7 @@ public class SlotData {
final BoneData boneData;
final Color color = new Color(1, 1, 1, 1);
String attachmentName;
boolean additiveBlending;
SlotData () {
name = null;
@ -67,6 +68,14 @@ public class SlotData {
return attachmentName;
}
public boolean getAdditiveBlending () {
return additiveBlending;
}
public void setAdditiveBlending (boolean additiveBlending) {
this.additiveBlending = additiveBlending;
}
public String toString () {
return name;
}

View File

@ -40,7 +40,7 @@ import com.badlogic.gdx.utils.NumberUtils;
/** Attachment that displays a texture region. */
public class RegionAttachment extends Attachment {
private TextureRegion region;
private float x, y, scaleX, scaleY, rotation, width, height;
private float x, y, scaleX = 1, scaleY = 1, rotation, width, height;
private final float[] vertices = new float[20];
private final float[] offset = new float[8];