Fixed error setting triangles.

Submesh index stuff should be solid now.
closes #130, merged manually
This commit is contained in:
NathanSweet 2013-10-01 19:36:29 +02:00
parent 47ce2a40c1
commit 6f1fbb004d
8 changed files with 96 additions and 100 deletions

View File

@ -60,9 +60,9 @@ public class SkeletonAnimationInspector : Editor {
if (component.skeleton != null) { if (component.skeleton != null) {
// Initial skin name. // Initial skin name.
String[] skins = new String[component.skeleton.Data.Skins.Count + 1]; String[] skins = new String[component.skeleton.Data.Skins.Count];
int skinIndex = 0; int skinIndex = 0;
for (int i = 0; i < skins.Length - 1; i++) { for (int i = 0; i < skins.Length; i++) {
String name = component.skeleton.Data.Skins[i].Name; String name = component.skeleton.Data.Skins[i].Name;
skins[i] = name; skins[i] = name;
if (name == initialSkinName.stringValue) if (name == initialSkinName.stringValue)
@ -76,7 +76,7 @@ public class SkeletonAnimationInspector : Editor {
EditorGUIUtility.LookLikeInspector(); EditorGUIUtility.LookLikeInspector();
EditorGUILayout.EndHorizontal(); EditorGUILayout.EndHorizontal();
initialSkinName.stringValue = skinIndex == 0 ? null : skins[skinIndex]; initialSkinName.stringValue = skins[skinIndex];
// Animation name. // Animation name.
String[] animations = new String[component.skeleton.Data.Animations.Count + 2]; String[] animations = new String[component.skeleton.Data.Animations.Count + 2];

View File

@ -56,9 +56,9 @@ public class SkeletonComponentInspector : Editor {
if (component.skeleton != null) { if (component.skeleton != null) {
// Initial skin name. // Initial skin name.
String[] skins = new String[component.skeleton.Data.Skins.Count + 1]; String[] skins = new String[component.skeleton.Data.Skins.Count];
int skinIndex = 0; int skinIndex = 0;
for (int i = 0; i < skins.Length - 1; i++) { for (int i = 0; i < skins.Length; i++) {
String name = component.skeleton.Data.Skins[i].Name; String name = component.skeleton.Data.Skins[i].Name;
skins[i] = name; skins[i] = name;
if (name == initialSkinName.stringValue) if (name == initialSkinName.stringValue)
@ -72,7 +72,7 @@ public class SkeletonComponentInspector : Editor {
EditorGUIUtility.LookLikeInspector(); EditorGUIUtility.LookLikeInspector();
EditorGUILayout.EndHorizontal(); EditorGUILayout.EndHorizontal();
initialSkinName.stringValue = skinIndex == 0 ? null : skins[skinIndex]; initialSkinName.stringValue = skins[skinIndex];
} }
EditorGUILayout.PropertyField(timeScale); EditorGUILayout.PropertyField(timeScale);

View File

@ -54,8 +54,7 @@ public class SkeletonComponent : MonoBehaviour {
private Vector2[] uvs; private Vector2[] uvs;
private Material[] sharedMaterials = new Material[0]; private Material[] sharedMaterials = new Material[0];
private List<Material> submeshMaterials = new List<Material>(); private List<Material> submeshMaterials = new List<Material>();
private List<int[]> submeshIndexes = new List<int[]>(); private List<Submesh> submeshes = new List<Submesh>();
private List<int> submeshFirstVertex = new List<int>();
private Vector4[] tangents = new Vector4[0]; private Vector4[] tangents = new Vector4[0];
public virtual void Clear () { public virtual void Clear () {
@ -77,7 +76,7 @@ public class SkeletonComponent : MonoBehaviour {
skeleton = new Skeleton(skeletonDataAsset.GetSkeletonData(false)); skeleton = new Skeleton(skeletonDataAsset.GetSkeletonData(false));
if (initialSkinName != null && initialSkinName.Length > 0) { if (initialSkinName != null && initialSkinName.Length > 0 && initialSkinName != "default") {
skeleton.SetSkin(initialSkinName); skeleton.SetSkin(initialSkinName);
skeleton.SetSlotsToSetupPose(); skeleton.SetSlotsToSetupPose();
} }
@ -150,7 +149,7 @@ public class SkeletonComponent : MonoBehaviour {
mesh.Clear(); mesh.Clear();
} else { } else {
// Too many vertices, zero the extra. // Too many vertices, zero the extra.
Vector3 zero = new Vector3(0, 0, 0); Vector3 zero = Vector3.zero;
for (int i = vertexCount, n = lastVertexCount; i < n; i++) for (int i = vertexCount, n = lastVertexCount; i < n; i++)
vertices[i] = zero; vertices[i] = zero;
} }
@ -162,6 +161,7 @@ public class SkeletonComponent : MonoBehaviour {
Color32[] colors = this.colors; Color32[] colors = this.colors;
int vertexIndex = 0; int vertexIndex = 0;
Color32 color = new Color32(); Color32 color = new Color32();
float a = skeleton.A * 255, r = skeleton.R, g = skeleton.G, b = skeleton.B;
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];
RegionAttachment regionAttachment = slot.Attachment as RegionAttachment; RegionAttachment regionAttachment = slot.Attachment as RegionAttachment;
@ -175,10 +175,10 @@ public class SkeletonComponent : MonoBehaviour {
vertices[vertexIndex + 2] = new Vector3(vertexPositions[RegionAttachment.X2], vertexPositions[RegionAttachment.Y2], 0); vertices[vertexIndex + 2] = new Vector3(vertexPositions[RegionAttachment.X2], vertexPositions[RegionAttachment.Y2], 0);
vertices[vertexIndex + 3] = new Vector3(vertexPositions[RegionAttachment.X3], vertexPositions[RegionAttachment.Y3], 0); vertices[vertexIndex + 3] = new Vector3(vertexPositions[RegionAttachment.X3], vertexPositions[RegionAttachment.Y3], 0);
color.a = (byte)(skeleton.A * slot.A * 255); color.a = (byte)(a * slot.A);
color.r = (byte)(skeleton.R * slot.R * color.a); color.r = (byte)(r * slot.R * color.a);
color.g = (byte)(skeleton.G * slot.G * color.a); color.g = (byte)(g * slot.G * color.a);
color.b = (byte)(skeleton.B * slot.B * color.a); color.b = (byte)(b * slot.B * color.a);
colors[vertexIndex] = color; colors[vertexIndex] = color;
colors[vertexIndex + 1] = color; colors[vertexIndex + 1] = color;
colors[vertexIndex + 2] = color; colors[vertexIndex + 2] = color;
@ -196,9 +196,10 @@ public class SkeletonComponent : MonoBehaviour {
mesh.colors32 = colors; mesh.colors32 = colors;
mesh.uv = uvs; mesh.uv = uvs;
mesh.subMeshCount = submeshMaterials.Count; int submeshCount = submeshes.Count;
for (int i = 0, n = mesh.subMeshCount; i < n; ++i) mesh.subMeshCount = submeshCount;
mesh.SetTriangles(submeshIndexes[i], i); for (int i = 0; i < submeshCount; ++i)
mesh.SetTriangles(submeshes[i].indexes, i);
if (calculateNormals) { if (calculateNormals) {
mesh.RecalculateNormals(); mesh.RecalculateNormals();
@ -223,40 +224,31 @@ public class SkeletonComponent : MonoBehaviour {
int indexCount = submeshQuadCount * 6; int indexCount = submeshQuadCount * 6;
int vertexIndex = (endQuadCount - submeshQuadCount) * 4; int vertexIndex = (endQuadCount - submeshQuadCount) * 4;
int[] indexes; if (submeshes.Count <= submeshIndex) submeshes.Add(new Submesh());
if (submeshIndexes.Count > submeshIndex) { Submesh submesh = submeshes[submeshIndex];
indexes = submeshIndexes[submeshIndex];
// Don't reallocate if existing indexes are right size. Skip setting vertices if already set correctly. // Allocate indexes if not the right size, allowing last submesh to have more than required.
if (!lastSubmesh) { int[] indexes = submesh.indexes;
if (indexes.Length == indexCount) { if (lastSubmesh ? (indexes.Length < indexCount) : (indexes.Length != indexCount))
if (submeshFirstVertex[submeshIndex] == vertexIndex) return; submesh.indexes = indexes = new int[indexCount];
} else
submeshIndexes[submeshIndex] = indexes = new int[indexCount]; // Set indexes if not already set.
} else { if (submesh.firstVertex != vertexIndex || submesh.indexCount < indexCount) {
if (indexes.Length >= indexCount) { // Allow last submesh to have more indices than required. submesh.indexCount = indexCount;
if (submeshFirstVertex[submeshIndex] == vertexIndex) return; submesh.firstVertex = vertexIndex;
} else for (int i = 0; i < indexCount; i += 6, vertexIndex += 4) {
submeshIndexes[submeshIndex] = indexes = new int[indexCount]; indexes[i] = vertexIndex;
indexes[i + 1] = vertexIndex + 2;
indexes[i + 2] = vertexIndex + 1;
indexes[i + 3] = vertexIndex + 2;
indexes[i + 4] = vertexIndex + 3;
indexes[i + 5] = vertexIndex + 1;
} }
submeshFirstVertex[submeshIndex] = vertexIndex;
} else {
// Need new indexes.
indexes = new int[indexCount];
submeshIndexes.Add(indexes);
submeshFirstVertex.Add(vertexIndex);
} }
for (int i = 0; i < indexCount; i += 6, vertexIndex += 4) { // Last submesh may have more indices than required, so zero indexes to the end.
indexes[i] = vertexIndex; if (lastSubmesh && submesh.indexCount != indexCount) {
indexes[i + 1] = vertexIndex + 2; submesh.indexCount = indexCount;
indexes[i + 2] = vertexIndex + 1;
indexes[i + 3] = vertexIndex + 2;
indexes[i + 4] = vertexIndex + 3;
indexes[i + 5] = vertexIndex + 1;
}
if (lastSubmesh) {
// Update vertices to the end.
for (int i = indexCount, n = indexes.Length; i < n; i++) for (int i = indexCount, n = indexes.Length; i < n; i++)
indexes[i] = 0; indexes[i] = 0;
} }
@ -291,3 +283,9 @@ public class SkeletonComponent : MonoBehaviour {
} }
#endif #endif
} }
class Submesh {
public int[] indexes = new int[0];
public int firstVertex = -1;
public int indexCount;
}

View File

@ -60,9 +60,9 @@ public class SkeletonAnimationInspector : Editor {
if (component.skeleton != null) { if (component.skeleton != null) {
// Initial skin name. // Initial skin name.
String[] skins = new String[component.skeleton.Data.Skins.Count + 1]; String[] skins = new String[component.skeleton.Data.Skins.Count];
int skinIndex = 0; int skinIndex = 0;
for (int i = 0; i < skins.Length - 1; i++) { for (int i = 0; i < skins.Length; i++) {
String name = component.skeleton.Data.Skins[i].Name; String name = component.skeleton.Data.Skins[i].Name;
skins[i] = name; skins[i] = name;
if (name == initialSkinName.stringValue) if (name == initialSkinName.stringValue)
@ -76,7 +76,7 @@ public class SkeletonAnimationInspector : Editor {
EditorGUIUtility.LookLikeInspector(); EditorGUIUtility.LookLikeInspector();
EditorGUILayout.EndHorizontal(); EditorGUILayout.EndHorizontal();
initialSkinName.stringValue = skinIndex == 0 ? null : skins[skinIndex]; initialSkinName.stringValue = skins[skinIndex];
// Animation name. // Animation name.
String[] animations = new String[component.skeleton.Data.Animations.Count + 2]; String[] animations = new String[component.skeleton.Data.Animations.Count + 2];

View File

@ -56,9 +56,9 @@ public class SkeletonComponentInspector : Editor {
if (component.skeleton != null) { if (component.skeleton != null) {
// Initial skin name. // Initial skin name.
String[] skins = new String[component.skeleton.Data.Skins.Count + 1]; String[] skins = new String[component.skeleton.Data.Skins.Count];
int skinIndex = 0; int skinIndex = 0;
for (int i = 0; i < skins.Length - 1; i++) { for (int i = 0; i < skins.Length; i++) {
String name = component.skeleton.Data.Skins[i].Name; String name = component.skeleton.Data.Skins[i].Name;
skins[i] = name; skins[i] = name;
if (name == initialSkinName.stringValue) if (name == initialSkinName.stringValue)
@ -72,7 +72,7 @@ public class SkeletonComponentInspector : Editor {
EditorGUIUtility.LookLikeInspector(); EditorGUIUtility.LookLikeInspector();
EditorGUILayout.EndHorizontal(); EditorGUILayout.EndHorizontal();
initialSkinName.stringValue = skinIndex == 0 ? null : skins[skinIndex]; initialSkinName.stringValue = skins[skinIndex];
} }
EditorGUILayout.PropertyField(timeScale); EditorGUILayout.PropertyField(timeScale);

View File

@ -54,8 +54,7 @@ public class SkeletonComponent : MonoBehaviour {
private Vector2[] uvs; private Vector2[] uvs;
private Material[] sharedMaterials = new Material[0]; private Material[] sharedMaterials = new Material[0];
private List<Material> submeshMaterials = new List<Material>(); private List<Material> submeshMaterials = new List<Material>();
private List<int[]> submeshIndexes = new List<int[]>(); private List<Submesh> submeshes = new List<Submesh>();
private List<int> submeshFirstVertex = new List<int>();
private Vector4[] tangents = new Vector4[0]; private Vector4[] tangents = new Vector4[0];
public virtual void Clear () { public virtual void Clear () {
@ -77,7 +76,7 @@ public class SkeletonComponent : MonoBehaviour {
skeleton = new Skeleton(skeletonDataAsset.GetSkeletonData(false)); skeleton = new Skeleton(skeletonDataAsset.GetSkeletonData(false));
if (initialSkinName != null && initialSkinName.Length > 0) { if (initialSkinName != null && initialSkinName.Length > 0 && initialSkinName != "default") {
skeleton.SetSkin(initialSkinName); skeleton.SetSkin(initialSkinName);
skeleton.SetSlotsToSetupPose(); skeleton.SetSlotsToSetupPose();
} }
@ -150,7 +149,7 @@ public class SkeletonComponent : MonoBehaviour {
mesh.Clear(); mesh.Clear();
} else { } else {
// Too many vertices, zero the extra. // Too many vertices, zero the extra.
Vector3 zero = new Vector3(0, 0, 0); Vector3 zero = Vector3.zero;
for (int i = vertexCount, n = lastVertexCount; i < n; i++) for (int i = vertexCount, n = lastVertexCount; i < n; i++)
vertices[i] = zero; vertices[i] = zero;
} }
@ -162,6 +161,7 @@ public class SkeletonComponent : MonoBehaviour {
Color32[] colors = this.colors; Color32[] colors = this.colors;
int vertexIndex = 0; int vertexIndex = 0;
Color32 color = new Color32(); Color32 color = new Color32();
float a = skeleton.A * 255, r = skeleton.R, g = skeleton.G, b = skeleton.B;
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];
RegionAttachment regionAttachment = slot.Attachment as RegionAttachment; RegionAttachment regionAttachment = slot.Attachment as RegionAttachment;
@ -175,10 +175,10 @@ public class SkeletonComponent : MonoBehaviour {
vertices[vertexIndex + 2] = new Vector3(vertexPositions[RegionAttachment.X2], vertexPositions[RegionAttachment.Y2], 0); vertices[vertexIndex + 2] = new Vector3(vertexPositions[RegionAttachment.X2], vertexPositions[RegionAttachment.Y2], 0);
vertices[vertexIndex + 3] = new Vector3(vertexPositions[RegionAttachment.X3], vertexPositions[RegionAttachment.Y3], 0); vertices[vertexIndex + 3] = new Vector3(vertexPositions[RegionAttachment.X3], vertexPositions[RegionAttachment.Y3], 0);
color.a = (byte)(skeleton.A * slot.A * 255); color.a = (byte)(a * slot.A);
color.r = (byte)(skeleton.R * slot.R * color.a); color.r = (byte)(r * slot.R * color.a);
color.g = (byte)(skeleton.G * slot.G * color.a); color.g = (byte)(g * slot.G * color.a);
color.b = (byte)(skeleton.B * slot.B * color.a); color.b = (byte)(b * slot.B * color.a);
colors[vertexIndex] = color; colors[vertexIndex] = color;
colors[vertexIndex + 1] = color; colors[vertexIndex + 1] = color;
colors[vertexIndex + 2] = color; colors[vertexIndex + 2] = color;
@ -196,9 +196,10 @@ public class SkeletonComponent : MonoBehaviour {
mesh.colors32 = colors; mesh.colors32 = colors;
mesh.uv = uvs; mesh.uv = uvs;
mesh.subMeshCount = submeshMaterials.Count; int submeshCount = submeshes.Count;
for (int i = 0, n = mesh.subMeshCount; i < n; ++i) mesh.subMeshCount = submeshCount;
mesh.SetTriangles(submeshIndexes[i], i); for (int i = 0; i < submeshCount; ++i)
mesh.SetTriangles(submeshes[i].indexes, i);
if (calculateNormals) { if (calculateNormals) {
mesh.RecalculateNormals(); mesh.RecalculateNormals();
@ -223,40 +224,31 @@ public class SkeletonComponent : MonoBehaviour {
int indexCount = submeshQuadCount * 6; int indexCount = submeshQuadCount * 6;
int vertexIndex = (endQuadCount - submeshQuadCount) * 4; int vertexIndex = (endQuadCount - submeshQuadCount) * 4;
int[] indexes; if (submeshes.Count <= submeshIndex) submeshes.Add(new Submesh());
if (submeshIndexes.Count > submeshIndex) { Submesh submesh = submeshes[submeshIndex];
indexes = submeshIndexes[submeshIndex];
// Don't reallocate if existing indexes are right size. Skip setting vertices if already set correctly. // Allocate indexes if not the right size, allowing last submesh to have more than required.
if (!lastSubmesh) { int[] indexes = submesh.indexes;
if (indexes.Length == indexCount) { if (lastSubmesh ? (indexes.Length < indexCount) : (indexes.Length != indexCount))
if (submeshFirstVertex[submeshIndex] == vertexIndex) return; submesh.indexes = indexes = new int[indexCount];
} else
submeshIndexes[submeshIndex] = indexes = new int[indexCount]; // Set indexes if not already set.
} else { if (submesh.firstVertex != vertexIndex || submesh.indexCount < indexCount) {
if (indexes.Length >= indexCount) { // Allow last submesh to have more indices than required. submesh.indexCount = indexCount;
if (submeshFirstVertex[submeshIndex] == vertexIndex) return; submesh.firstVertex = vertexIndex;
} else for (int i = 0; i < indexCount; i += 6, vertexIndex += 4) {
submeshIndexes[submeshIndex] = indexes = new int[indexCount]; indexes[i] = vertexIndex;
indexes[i + 1] = vertexIndex + 2;
indexes[i + 2] = vertexIndex + 1;
indexes[i + 3] = vertexIndex + 2;
indexes[i + 4] = vertexIndex + 3;
indexes[i + 5] = vertexIndex + 1;
} }
submeshFirstVertex[submeshIndex] = vertexIndex;
} else {
// Need new indexes.
indexes = new int[indexCount];
submeshIndexes.Add(indexes);
submeshFirstVertex.Add(vertexIndex);
} }
for (int i = 0; i < indexCount; i += 6, vertexIndex += 4) { // Last submesh may have more indices than required, so zero indexes to the end.
indexes[i] = vertexIndex; if (lastSubmesh && submesh.indexCount != indexCount) {
indexes[i + 1] = vertexIndex + 2; submesh.indexCount = indexCount;
indexes[i + 2] = vertexIndex + 1;
indexes[i + 3] = vertexIndex + 2;
indexes[i + 4] = vertexIndex + 3;
indexes[i + 5] = vertexIndex + 1;
}
if (lastSubmesh) {
// Update vertices to the end.
for (int i = indexCount, n = indexes.Length; i < n; i++) for (int i = indexCount, n = indexes.Length; i < n; i++)
indexes[i] = 0; indexes[i] = 0;
} }
@ -291,3 +283,9 @@ public class SkeletonComponent : MonoBehaviour {
} }
#endif #endif
} }
class Submesh {
public int[] indexes = new int[0];
public int firstVertex = -1;
public int indexCount;
}