Moved Skeleton refernce from Slot to Bone.

This commit is contained in:
NathanSweet 2014-08-31 12:46:45 +02:00
parent e18de6cbe6
commit 2cfb6ad980
12 changed files with 79 additions and 99 deletions

View File

@ -90,10 +90,9 @@ namespace Spine {
}
}
public void ComputeWorldVertices (float x, float y, Slot slot, float[] worldVertices) {
public void ComputeWorldVertices (Slot slot, float[] worldVertices) {
Bone bone = slot.bone;
x += bone.worldX;
y += bone.worldY;
float x = bone.skeleton.x + bone.worldX, y = bone.skeleton.y + bone.worldY;
float m00 = bone.m00, m01 = bone.m01, m10 = bone.m10, m11 = bone.m11;
float[] vertices = this.vertices;
int verticesCount = vertices.Length;

View File

@ -134,9 +134,8 @@ namespace Spine {
offset[Y4] = localYCos + localX2Sin;
}
public void ComputeWorldVertices (float x, float y, Bone bone, float[] worldVertices) {
x += bone.worldX;
y += bone.worldY;
public void ComputeWorldVertices (Bone bone, float[] worldVertices) {
float x = bone.skeleton.x + bone.worldX, y = bone.skeleton.y + bone.worldY;
float m00 = bone.m00, m01 = bone.m01, m10 = bone.m10, m11 = bone.m11;
float[] offset = this.offset;
worldVertices[X1] = offset[X1] * m00 + offset[Y1] * m01 + x;

View File

@ -93,8 +93,10 @@ namespace Spine {
}
}
public void ComputeWorldVertices (float x, float y, Slot slot, float[] worldVertices) {
List<Bone> skeletonBones = slot.skeleton.bones;
public void ComputeWorldVertices (Slot slot, float[] worldVertices) {
Skeleton skeleton = slot.bone.skeleton;
List<Bone> skeletonBones = skeleton.bones;
float x = skeleton.x, y = skeleton.y;
float[] weights = this.weights;
int[] bones = this.bones;
if (slot.attachmentVerticesCount == 0) {

View File

@ -35,13 +35,14 @@ namespace Spine {
static public bool yDown;
internal BoneData data;
internal Skeleton skeleton;
internal Bone parent;
internal float x, y, rotation, rotationIK, scaleX, scaleY;
internal float m00, m01, m10, m11;
internal float worldX, worldY, worldRotation, worldScaleX, worldScaleY;
internal bool flipX, flipY;
public BoneData Data { get { return data; } }
public Skeleton Skeleton { get { return skeleton; } }
public Bone Parent { get { return parent; } }
public float X { get { return x; } set { x = value; } }
public float Y { get { return y; } set { y = value; } }
@ -63,9 +64,11 @@ namespace Spine {
public float WorldScaleY { get { return worldScaleY; } }
/// <param name="parent">May be null.</param>
public Bone (BoneData data, Bone parent) {
public Bone (BoneData data, Skeleton skeleton, Bone parent) {
if (data == null) throw new ArgumentNullException("data cannot be null.");
if (skeleton == null) throw new ArgumentNullException("skeleton cannot be null.");
this.data = data;
this.skeleton = skeleton;
this.parent = parent;
SetToSetupPose();
}
@ -73,6 +76,7 @@ namespace Spine {
/// <summary>Computes the world SRT using the parent bone and the local SRT.</summary>
public void UpdateWorldTransform () {
Bone parent = this.parent;
Skeleton skeleton = this.skeleton;
float x = this.x, y = this.y;
if (parent != null) {
worldX = x * parent.m00 + y * parent.m01 + parent.worldX;
@ -86,8 +90,8 @@ namespace Spine {
}
worldRotation = data.inheritRotation ? parent.worldRotation + rotationIK : rotationIK;
} else {
worldX = flipX ? -x : x;
worldY = flipY != yDown ? -y : y;
worldX = skeleton.flipX ? -x : x;
worldY = skeleton.flipY != yDown ? -y : y;
worldScaleX = scaleX;
worldScaleY = scaleY;
worldRotation = rotationIK;
@ -95,14 +99,14 @@ namespace Spine {
float radians = worldRotation * (float)Math.PI / 180;
float cos = (float)Math.Cos(radians);
float sin = (float)Math.Sin(radians);
if (flipX) {
if (skeleton.flipX) {
m00 = -cos * worldScaleX;
m01 = sin * worldScaleY;
} else {
m00 = cos * worldScaleX;
m01 = -sin * worldScaleY;
}
if (flipY != yDown) {
if (skeleton.flipY != yDown) {
m10 = -sin * worldScaleX;
m11 = -cos * worldScaleY;
} else {
@ -124,7 +128,8 @@ namespace Spine {
public void worldToLocal (float worldX, float worldY, out float localX, out float localY) {
float dx = worldX - this.worldX, dy = worldY - this.worldY;
float m00 = this.m00, m10 = this.m10, m01 = this.m01, m11 = this.m11;
if (flipX != (flipY != yDown)) {
Skeleton skeleton = this.skeleton;
if (skeleton.flipX != (skeleton.flipY != yDown)) {
m00 *= -1;
m11 *= -1;
}

View File

@ -53,11 +53,9 @@ namespace Spine {
bendDirection = data.bendDirection;
bones = new List<Bone>(data.bones.Count);
if (skeleton != null) {
foreach (BoneData boneData in data.bones)
bones.Add(skeleton.FindBone(boneData.name));
target = skeleton.FindBone(data.target.name);
}
foreach (BoneData boneData in data.bones)
bones.Add(skeleton.FindBone(boneData.name));
target = skeleton.FindBone(data.target.name);
}
public void apply () {

View File

@ -38,7 +38,7 @@ namespace Spine {
internal List<Slot> slots;
internal List<Slot> drawOrder;
internal List<IkConstraint> ikConstraints = new List<IkConstraint>();
private List<List<Bone>> updateBonesCache = new List<List<Bone>>();
private List<List<Bone>> bonesCache = new List<List<Bone>>();
internal Skin skin;
internal float r = 1, g = 1, b = 1, a = 1;
internal float time;
@ -58,28 +58,8 @@ namespace Spine {
public float Time { get { return time; } set { time = value; } }
public float X { get { return x; } set { x = value; } }
public float Y { get { return y; } set { y = value; } }
public bool FlipX {
get { return flipX; }
set {
if (flipX == value) return;
flipX = value;
List<Bone> bones = this.bones;
for (int i = 0, n = bones.Count; i < n; i++)
bones[i].flipX = value;
}
}
public bool FlipY {
get { return flipY; }
set {
if (flipY == value) return;
flipY = value;
List<Bone> bones = this.bones;
for (int i = 0, n = bones.Count; i < n; i++)
bones[i].flipY = value;
}
}
public bool FlipX { get { return flipX; } set { flipX = value; } }
public bool FlipY { get { return flipY; } set { flipY = value; } }
public Bone RootBone {
get {
@ -94,14 +74,14 @@ namespace Spine {
bones = new List<Bone>(data.bones.Count);
foreach (BoneData boneData in data.bones) {
Bone parent = boneData.parent == null ? null : bones[data.bones.IndexOf(boneData.parent)];
bones.Add(new Bone(boneData, parent));
bones.Add(new Bone(boneData, this, parent));
}
slots = new List<Slot>(data.slots.Count);
drawOrder = new List<Slot>(data.slots.Count);
foreach (SlotData slotData in data.slots) {
Bone bone = bones[data.bones.IndexOf(slotData.boneData)];
Slot slot = new Slot(slotData, this, bone);
Slot slot = new Slot(slotData, bone);
slots.Add(slot);
drawOrder.Add(slot);
}
@ -116,18 +96,18 @@ namespace Spine {
/// <summary>Caches information about bones and IK constraints. Must be called if bones or IK constraints are added or
/// removed.</summary>
public void UpdateCache () {
List<List<Bone>> updateBonesCache = this.updateBonesCache;
List<List<Bone>> bonesCache = this.bonesCache;
List<IkConstraint> ikConstraints = this.ikConstraints;
int ikConstraintsCount = ikConstraints.Count;
int arrayCount = ikConstraintsCount + 1;
if (updateBonesCache.Count > arrayCount) updateBonesCache.RemoveRange(arrayCount, updateBonesCache.Count - arrayCount);
for (int i = 0, n = updateBonesCache.Count; i < n; i++)
updateBonesCache[i].Clear();
while (updateBonesCache.Count < arrayCount)
updateBonesCache.Add(new List<Bone>());
if (bonesCache.Count > arrayCount) bonesCache.RemoveRange(arrayCount, bonesCache.Count - arrayCount);
for (int i = 0, n = bonesCache.Count; i < n; i++)
bonesCache[i].Clear();
while (bonesCache.Count < arrayCount)
bonesCache.Add(new List<Bone>());
List<Bone> nonIkBones = updateBonesCache[0];
List<Bone> nonIkBones = bonesCache[0];
for (int i = 0, n = bones.Count; i < n; i++) {
Bone bone = bones[i];
@ -139,8 +119,8 @@ namespace Spine {
Bone child = ikConstraint.bones[ikConstraint.bones.Count - 1];
while (true) {
if (current == child) {
updateBonesCache[ii].Add(bone);
updateBonesCache[ii + 1].Add(bone);
bonesCache[ii].Add(bone);
bonesCache[ii + 1].Add(bone);
goto outer;
}
if (child == parent) break;
@ -161,11 +141,11 @@ namespace Spine {
Bone bone = bones[ii];
bone.rotationIK = bone.rotation;
}
List<List<Bone>> updateBonesCache = this.updateBonesCache;
List<List<Bone>> bonesCache = this.bonesCache;
List<IkConstraint> ikConstraints = this.ikConstraints;
int i = 0, last = updateBonesCache.Count - 1;
int i = 0, last = bonesCache.Count - 1;
while (true) {
List<Bone> updateBones = updateBonesCache[i];
List<Bone> updateBones = bonesCache[i];
for (int ii = 0, nn = updateBones.Count; ii < nn; ii++)
updateBones[ii].UpdateWorldTransform();
if (i == last) break;

View File

@ -91,14 +91,14 @@ namespace Spine {
if (root == null) throw new Exception("Invalid JSON.");
// Skeleton.
var skeletonMap = (Dictionary<String, Object>)root["skeleton"];
if (skeletonMap != null) {
if (root.ContainsKey("skeleton")) {
var skeletonMap = (Dictionary<String, Object>)root["skeleton"];
skeletonData.version = (String)skeletonMap["spine"];
skeletonData.hash = (String)skeletonMap["hash"];
skeletonData.width = GetFloat(skeletonMap, "width", 0);
skeletonData.height = GetFloat(skeletonMap, "width", 1);
}
// Bones.
foreach (Dictionary<String, Object> boneMap in (List<Object>)root["bones"]) {
BoneData parent = null;
@ -120,23 +120,25 @@ namespace Spine {
}
// IK constraints.
foreach (Dictionary<String, Object> ikMap in (List<Object>)root["ik"]) {
IkConstraintData ikConstraintData = new IkConstraintData((String)ikMap["name"]);
if (root.ContainsKey("ik")) {
foreach (Dictionary<String, Object> ikMap in (List<Object>)root["ik"]) {
IkConstraintData ikConstraintData = new IkConstraintData((String)ikMap["name"]);
foreach (String boneName in (List<Object>)ikMap["bones"]) {
BoneData bone = skeletonData.FindBone(boneName);
if (bone == null) throw new Exception("IK bone not found: " + boneName);
ikConstraintData.bones.Add(bone);
foreach (String boneName in (List<Object>)ikMap["bones"]) {
BoneData bone = skeletonData.FindBone(boneName);
if (bone == null) throw new Exception("IK bone not found: " + boneName);
ikConstraintData.bones.Add(bone);
}
String targetName = (String)ikMap["target"];
ikConstraintData.target = skeletonData.FindBone(targetName);
if (ikConstraintData.target == null) throw new Exception("Target bone not found: " + targetName);
ikConstraintData.bendDirection = GetBoolean(ikMap, "bendPositive", true) ? 1 : -1;
ikConstraintData.mix = GetFloat(ikMap, "mix", 1);
skeletonData.ikConstraints.Add(ikConstraintData);
}
String targetName = (String)ikMap["target"];
ikConstraintData.target = skeletonData.FindBone(targetName);
if (ikConstraintData.target == null) throw new Exception("Target bone not found: " + targetName);
ikConstraintData.bendDirection = GetBoolean(ikMap, "bendPositive", true) ? 1 : -1;
ikConstraintData.mix = GetFloat(ikMap, "mix", 1);
skeletonData.ikConstraints.Add(ikConstraintData);
}
// Slots.

View File

@ -34,7 +34,6 @@ namespace Spine {
public class Slot {
internal SlotData data;
internal Bone bone;
internal Skeleton skeleton;
internal float r, g, b, a;
internal Attachment attachment;
internal float attachmentTime;
@ -43,7 +42,7 @@ namespace Spine {
public SlotData Data { get { return data; } }
public Bone Bone { get { return bone; } }
public Skeleton Skeleton { get { return skeleton; } }
public Skeleton Skeleton { get { return bone.skeleton; } }
public float R { get { return r; } set { r = value; } }
public float G { get { return g; } set { g = value; } }
public float B { get { return b; } set { b = value; } }
@ -56,29 +55,27 @@ namespace Spine {
}
set {
attachment = value;
attachmentTime = skeleton.time;
attachmentTime = bone.skeleton.time;
attachmentVerticesCount = 0;
}
}
public float AttachmentTime {
get {
return skeleton.time - attachmentTime;
return bone.skeleton.time - attachmentTime;
}
set {
attachmentTime = skeleton.time - value;
attachmentTime = bone.skeleton.time - value;
}
}
public float[] AttachmentVertices { get { return attachmentVertices; } set { attachmentVertices = value; } }
public int AttachmentVerticesCount { get { return attachmentVerticesCount; } set { attachmentVerticesCount = value; } }
public Slot (SlotData data, Skeleton skeleton, Bone bone) {
public Slot (SlotData data, Bone bone) {
if (data == null) throw new ArgumentNullException("data cannot be null.");
if (skeleton == null) throw new ArgumentNullException("skeleton cannot be null.");
if (bone == null) throw new ArgumentNullException("bone cannot be null.");
this.data = data;
this.skeleton = skeleton;
this.bone = bone;
SetToSetupPose();
}
@ -88,11 +85,11 @@ namespace Spine {
g = data.g;
b = data.b;
a = data.a;
Attachment = data.attachmentName == null ? null : skeleton.GetAttachment(slotIndex, data.attachmentName);
Attachment = data.attachmentName == null ? null : bone.skeleton.GetAttachment(slotIndex, data.attachmentName);
}
public void SetToSetupPose () {
SetToSetupPose(skeleton.data.slots.IndexOf(data));
SetToSetupPose(bone.skeleton.data.slots.IndexOf(data));
}
override public String ToString () {

View File

@ -190,14 +190,14 @@ public class SkeletonRenderer : MonoBehaviour {
Color32[] colors = this.colors;
int vertexIndex = 0;
Color32 color = new Color32();
float x = skeleton.x, y = skeleton.y, zSpacing = this.zSpacing;
float zSpacing = this.zSpacing;
float a = skeleton.a * 255, r = skeleton.r, g = skeleton.g, b = skeleton.b;
for (int i = 0; i < drawOrderCount; i++) {
Slot slot = drawOrder[i];
Attachment attachment = slot.attachment;
if (attachment is RegionAttachment) {
RegionAttachment regionAttachment = (RegionAttachment)attachment;
regionAttachment.ComputeWorldVertices(x, y, slot.bone, tempVertices);
regionAttachment.ComputeWorldVertices(slot.bone, tempVertices);
float z = i * zSpacing;
vertices[vertexIndex] = new Vector3(tempVertices[RegionAttachment.X1], tempVertices[RegionAttachment.Y1], z);
@ -228,7 +228,7 @@ public class SkeletonRenderer : MonoBehaviour {
MeshAttachment meshAttachment = (MeshAttachment)attachment;
int meshVertexCount = meshAttachment.vertices.Length;
if (tempVertices.Length < meshVertexCount) tempVertices = new float[meshVertexCount];
meshAttachment.ComputeWorldVertices(x, y, slot, tempVertices);
meshAttachment.ComputeWorldVertices(slot, tempVertices);
color.a = (byte)(a * slot.a * meshAttachment.a);
color.r = (byte)(r * slot.r * meshAttachment.r * color.a);
@ -247,7 +247,7 @@ public class SkeletonRenderer : MonoBehaviour {
SkinnedMeshAttachment meshAttachment = (SkinnedMeshAttachment)attachment;
int meshVertexCount = meshAttachment.uvs.Length;
if (tempVertices.Length < meshVertexCount) tempVertices = new float[meshVertexCount];
meshAttachment.ComputeWorldVertices(x, y, slot, tempVertices);
meshAttachment.ComputeWorldVertices(slot, tempVertices);
color.a = (byte)(a * slot.a * meshAttachment.a);
color.r = (byte)(r * slot.r * meshAttachment.r * color.a);

View File

@ -193,14 +193,14 @@ public class SkeletonRenderer : MonoBehaviour {
Color32[] colors = this.colors;
int vertexIndex = 0;
Color32 color = new Color32();
float x = skeleton.x, y = skeleton.y, zSpacing = this.zSpacing;
float zSpacing = this.zSpacing;
float a = skeleton.a * 255, r = skeleton.r, g = skeleton.g, b = skeleton.b;
for (int i = 0; i < drawOrderCount; i++) {
Slot slot = drawOrder[i];
Attachment attachment = slot.attachment;
if (attachment is RegionAttachment) {
RegionAttachment regionAttachment = (RegionAttachment)attachment;
regionAttachment.ComputeWorldVertices(x, y, slot.bone, tempVertices);
regionAttachment.ComputeWorldVertices(slot.bone, tempVertices);
float z = i * zSpacing;
vertices[vertexIndex] = new Vector3(tempVertices[RegionAttachment.X1], tempVertices[RegionAttachment.Y1], z);
@ -231,7 +231,7 @@ public class SkeletonRenderer : MonoBehaviour {
MeshAttachment meshAttachment = (MeshAttachment)attachment;
int meshVertexCount = meshAttachment.vertices.Length;
if (tempVertices.Length < meshVertexCount) tempVertices = new float[meshVertexCount];
meshAttachment.ComputeWorldVertices(x, y, slot, tempVertices);
meshAttachment.ComputeWorldVertices(slot, tempVertices);
color.a = (byte)(a * slot.a * meshAttachment.a);
color.r = (byte)(r * slot.r * meshAttachment.r * color.a);
@ -250,7 +250,7 @@ public class SkeletonRenderer : MonoBehaviour {
SkinnedMeshAttachment meshAttachment = (SkinnedMeshAttachment)attachment;
int meshVertexCount = meshAttachment.uvs.Length;
if (tempVertices.Length < meshVertexCount) tempVertices = new float[meshVertexCount];
meshAttachment.ComputeWorldVertices(x, y, slot, tempVertices);
meshAttachment.ComputeWorldVertices(slot, tempVertices);
color.a = (byte)(a * slot.a * meshAttachment.a);
color.r = (byte)(r * slot.r * meshAttachment.r * color.a);

View File

@ -90,7 +90,6 @@ namespace Spine {
public void Draw (Skeleton skeleton) {
float[] vertices = this.vertices;
List<Slot> drawOrder = skeleton.DrawOrder;
float x = skeleton.X, y = skeleton.Y;
float skeletonR = skeleton.R, skeletonG = skeleton.G, skeletonB = skeleton.B, skeletonA = skeleton.A;
for (int i = 0, n = drawOrder.Count; i < n; i++) {
Slot slot = drawOrder[i];
@ -128,7 +127,7 @@ namespace Spine {
itemVertices[BR].Color = color;
itemVertices[TR].Color = color;
regionAttachment.ComputeWorldVertices(x, y, slot.Bone, vertices);
regionAttachment.ComputeWorldVertices(slot.Bone, vertices);
itemVertices[TL].Position.X = vertices[RegionAttachment.X1];
itemVertices[TL].Position.Y = vertices[RegionAttachment.Y1];
itemVertices[TL].Position.Z = 0;
@ -155,7 +154,7 @@ namespace Spine {
MeshAttachment mesh = (MeshAttachment)attachment;
int vertexCount = mesh.Vertices.Length;
if (vertices.Length < vertexCount) vertices = new float[vertexCount];
mesh.ComputeWorldVertices(x, y, slot, vertices);
mesh.ComputeWorldVertices(slot, vertices);
int[] triangles = mesh.Triangles;
MeshItem item = batcher.NextItem(vertexCount, triangles.Length);
@ -192,7 +191,7 @@ namespace Spine {
SkinnedMeshAttachment mesh = (SkinnedMeshAttachment)attachment;
int vertexCount = mesh.UVs.Length;
if (vertices.Length < vertexCount) vertices = new float[vertexCount];
mesh.ComputeWorldVertices(x, y, slot, vertices);
mesh.ComputeWorldVertices(slot, vertices);
int[] triangles = mesh.Triangles;
MeshItem item = batcher.NextItem(vertexCount, triangles.Length);

View File

@ -83,7 +83,6 @@ namespace Spine {
public void Draw (Skeleton skeleton) {
List<Slot> drawOrder = skeleton.DrawOrder;
float x = skeleton.X, y = skeleton.Y;
float skeletonR = skeleton.R, skeletonG = skeleton.G, skeletonB = skeleton.B, skeletonA = skeleton.A;
for (int i = 0, n = drawOrder.Count; i < n; i++) {
Slot slot = drawOrder[i];
@ -112,7 +111,7 @@ namespace Spine {
item.vertexTR.Color = color;
float[] vertices = this.vertices;
regionAttachment.ComputeWorldVertices(x, y, slot.Bone, vertices);
regionAttachment.ComputeWorldVertices(slot.Bone, vertices);
item.vertexTL.Position.X = vertices[RegionAttachment.X1];
item.vertexTL.Position.Y = vertices[RegionAttachment.Y1];
item.vertexTL.Position.Z = 0;