Some small optimizations.

This commit is contained in:
NathanSweet 2013-06-10 17:02:49 +02:00
parent c03860383c
commit 4a03850829

View File

@ -56,6 +56,8 @@ public class SkeletonComponent : MonoBehaviour {
mesh.name = "Skeleton Mesh"; mesh.name = "Skeleton Mesh";
mesh.hideFlags = HideFlags.HideAndDontSave; mesh.hideFlags = HideFlags.HideAndDontSave;
renderer.sharedMaterial = skeletonDataAsset.atlasAsset.material;
vertices = new Vector3[0]; vertices = new Vector3[0];
skeleton = new Skeleton(skeletonDataAsset.GetSkeletonData(false)); skeleton = new Skeleton(skeletonDataAsset.GetSkeletonData(false));
@ -95,8 +97,10 @@ public class SkeletonComponent : MonoBehaviour {
} }
// Ensure mesh data is the right size. // Ensure mesh data is the right size.
if (quadCount > vertices.Length / 4) { Vector3[] vertices = this.vertices;
vertices = new Vector3[quadCount * 4]; bool newTriangles = quadCount > vertices.Length / 4;
if (newTriangles) {
this.vertices = vertices = new Vector3[quadCount * 4];
colors = new Color[quadCount * 4]; colors = new Color[quadCount * 4];
uvs = new Vector2[quadCount * 4]; uvs = new Vector2[quadCount * 4];
triangles = new int[quadCount * 6]; triangles = new int[quadCount * 6];
@ -104,13 +108,13 @@ public class SkeletonComponent : MonoBehaviour {
for (int i = 0, n = quadCount; i < n; i++) { for (int i = 0, n = quadCount; i < n; i++) {
int index = i * 6; int index = i * 6;
int vertexIndex = i * 4; int vertex = i * 4;
triangles[index] = vertexIndex; triangles[index] = vertex;
triangles[index + 1] = vertexIndex + 2; triangles[index + 1] = vertex + 2;
triangles[index + 2] = vertexIndex + 1; triangles[index + 2] = vertex + 1;
triangles[index + 3] = vertexIndex + 2; triangles[index + 3] = vertex + 2;
triangles[index + 4] = vertexIndex + 3; triangles[index + 4] = vertex + 3;
triangles[index + 5] = vertexIndex + 1; triangles[index + 5] = vertex + 1;
} }
} else { } else {
Vector3 zero = new Vector3(0, 0, 0); Vector3 zero = new Vector3(0, 0, 0);
@ -120,16 +124,15 @@ public class SkeletonComponent : MonoBehaviour {
// Setup mesh. // Setup mesh.
float[] vertexPositions = this.vertexPositions; float[] vertexPositions = this.vertexPositions;
int quadIndex = 0; int vertexIndex = 0;
Color color = new Color(); Color color = new Color();
for (int i = 0, n = drawOrder.Count; i < n; i++) { for (int i = 0, n = drawOrder.Count; i < n; i++) {
Slot slot = drawOrder[i]; Slot slot = drawOrder[i];
Attachment attachment = slot.Attachment; RegionAttachment regionAttachment = slot.Attachment as RegionAttachment;
if (attachment is RegionAttachment) { if (regionAttachment == null) continue;
RegionAttachment regionAttachment = (RegionAttachment)attachment;
regionAttachment.ComputeVertices(skeleton.X, skeleton.Y, slot.Bone, vertexPositions); regionAttachment.ComputeVertices(skeleton.X, skeleton.Y, slot.Bone, vertexPositions);
int vertexIndex = quadIndex * 4;
vertices[vertexIndex] = new Vector3(vertexPositions[RegionAttachment.X1], vertexPositions[RegionAttachment.Y1], 0); vertices[vertexIndex] = new Vector3(vertexPositions[RegionAttachment.X1], vertexPositions[RegionAttachment.Y1], 0);
vertices[vertexIndex + 1] = new Vector3(vertexPositions[RegionAttachment.X4], vertexPositions[RegionAttachment.Y4], 0); vertices[vertexIndex + 1] = new Vector3(vertexPositions[RegionAttachment.X4], vertexPositions[RegionAttachment.Y4], 0);
vertices[vertexIndex + 2] = new Vector3(vertexPositions[RegionAttachment.X2], vertexPositions[RegionAttachment.Y2], 0); vertices[vertexIndex + 2] = new Vector3(vertexPositions[RegionAttachment.X2], vertexPositions[RegionAttachment.Y2], 0);
@ -150,15 +153,12 @@ public class SkeletonComponent : MonoBehaviour {
uvs[vertexIndex + 2] = new Vector2(regionUVs[RegionAttachment.X2], 1 - regionUVs[RegionAttachment.Y2]); uvs[vertexIndex + 2] = new Vector2(regionUVs[RegionAttachment.X2], 1 - regionUVs[RegionAttachment.Y2]);
uvs[vertexIndex + 3] = new Vector2(regionUVs[RegionAttachment.X3], 1 - regionUVs[RegionAttachment.Y3]); uvs[vertexIndex + 3] = new Vector2(regionUVs[RegionAttachment.X3], 1 - regionUVs[RegionAttachment.Y3]);
quadIndex++; vertexIndex += 4;
}
} }
mesh.vertices = vertices; mesh.vertices = vertices;
mesh.colors = colors; mesh.colors = colors;
mesh.uv = uvs; mesh.uv = uvs;
mesh.triangles = triangles; if (newTriangles) mesh.triangles = triangles;
renderer.sharedMaterial = skeletonDataAsset.atlasAsset.material;
} }
public virtual void OnEnable () { public virtual void OnEnable () {