Skip to content

Commit

Permalink
Changed to Color32. Reduced zeroing mesh verts.
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanSweet committed Jun 13, 2013
1 parent 00803c4 commit 692f24c
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions spine-unity/Assets/Plugins/Spine/SkeletonComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ public class SkeletonComponent : MonoBehaviour {
public String initialSkinName;
public float timeScale = 1;
private Mesh mesh;
private int lastVertexCount;
private Vector3[] vertices;
private Color[] colors;
private Color32[] colors;
private Vector2[] uvs;
private int[] triangles;
private float[] vertexPositions = new float[8];
Expand Down Expand Up @@ -99,11 +100,13 @@ public virtual void Update () {

// Ensure mesh data is the right size.
Vector3[] vertices = this.vertices;
bool newTriangles = quadCount > vertices.Length / 4;
int vertexCount = quadCount * 4;
bool newTriangles = vertexCount > vertices.Length;
if (newTriangles) {
this.vertices = vertices = new Vector3[quadCount * 4];
colors = new Color[quadCount * 4];
uvs = new Vector2[quadCount * 4];
// Not enough vertices, increase size.
this.vertices = vertices = new Vector3[vertexCount];
colors = new Color32[vertexCount];
uvs = new Vector2[vertexCount];
triangles = new int[quadCount * 6];
mesh.Clear();

Expand All @@ -118,15 +121,17 @@ public virtual void Update () {
triangles[index + 5] = vertex + 1;
}
} else {
// Too many vertices, zero the extra.
Vector3 zero = new Vector3(0, 0, 0);
for (int i = quadCount * 4, n = vertices.Length; i < n; i++)
for (int i = vertexCount, n = lastVertexCount; i < n; i++)
vertices[i] = zero;
}
lastVertexCount = vertexCount;

// Setup mesh.
float[] vertexPositions = this.vertexPositions;
int vertexIndex = 0;
Color color = new Color();
Color32 color = new Color32();
for (int i = 0, n = drawOrder.Count; i < n; i++) {
Slot slot = drawOrder[i];
RegionAttachment regionAttachment = slot.Attachment as RegionAttachment;
Expand All @@ -139,10 +144,10 @@ public virtual void Update () {
vertices[vertexIndex + 2] = new Vector3(vertexPositions[RegionAttachment.X2], vertexPositions[RegionAttachment.Y2], 0);
vertices[vertexIndex + 3] = new Vector3(vertexPositions[RegionAttachment.X3], vertexPositions[RegionAttachment.Y3], 0);

color.a = skeleton.A * slot.A;
color.r = skeleton.R * slot.R * color.a;
color.g = skeleton.G * slot.G * color.a;
color.b = skeleton.B * slot.B * color.a;
color.a = (byte)(skeleton.A * slot.A * 255);
color.r = (byte)(skeleton.R * slot.R * color.a);
color.g = (byte)(skeleton.G * slot.G * color.a);
color.b = (byte)(skeleton.B * slot.B * color.a);
colors[vertexIndex] = color;
colors[vertexIndex + 1] = color;
colors[vertexIndex + 2] = color;
Expand All @@ -157,7 +162,7 @@ public virtual void Update () {
vertexIndex += 4;
}
mesh.vertices = vertices;
mesh.colors = colors;
mesh.colors32 = colors;
mesh.uv = uvs;
if (newTriangles) mesh.triangles = triangles;
}
Expand Down

0 comments on commit 692f24c

Please sign in to comment.