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) {
// 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;
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;
skins[i] = name;
if (name == initialSkinName.stringValue)
@ -76,7 +76,7 @@ public class SkeletonAnimationInspector : Editor {
EditorGUIUtility.LookLikeInspector();
EditorGUILayout.EndHorizontal();
initialSkinName.stringValue = skinIndex == 0 ? null : skins[skinIndex];
initialSkinName.stringValue = skins[skinIndex];
// Animation name.
String[] animations = new String[component.skeleton.Data.Animations.Count + 2];

View File

@ -56,9 +56,9 @@ public class SkeletonComponentInspector : Editor {
if (component.skeleton != null) {
// 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;
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;
skins[i] = name;
if (name == initialSkinName.stringValue)
@ -72,7 +72,7 @@ public class SkeletonComponentInspector : Editor {
EditorGUIUtility.LookLikeInspector();
EditorGUILayout.EndHorizontal();
initialSkinName.stringValue = skinIndex == 0 ? null : skins[skinIndex];
initialSkinName.stringValue = skins[skinIndex];
}
EditorGUILayout.PropertyField(timeScale);

View File

@ -54,8 +54,7 @@ public class SkeletonComponent : MonoBehaviour {
private Vector2[] uvs;
private Material[] sharedMaterials = new Material[0];
private List<Material> submeshMaterials = new List<Material>();
private List<int[]> submeshIndexes = new List<int[]>();
private List<int> submeshFirstVertex = new List<int>();
private List<Submesh> submeshes = new List<Submesh>();
private Vector4[] tangents = new Vector4[0];
public virtual void Clear () {
@ -77,7 +76,7 @@ public class SkeletonComponent : MonoBehaviour {
skeleton = new Skeleton(skeletonDataAsset.GetSkeletonData(false));
if (initialSkinName != null && initialSkinName.Length > 0) {
if (initialSkinName != null && initialSkinName.Length > 0 && initialSkinName != "default") {
skeleton.SetSkin(initialSkinName);
skeleton.SetSlotsToSetupPose();
}
@ -150,7 +149,7 @@ public class SkeletonComponent : MonoBehaviour {
mesh.Clear();
} else {
// 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++)
vertices[i] = zero;
}
@ -162,6 +161,7 @@ public class SkeletonComponent : MonoBehaviour {
Color32[] colors = this.colors;
int vertexIndex = 0;
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++) {
Slot slot = drawOrder[i];
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 + 3] = new Vector3(vertexPositions[RegionAttachment.X3], vertexPositions[RegionAttachment.Y3], 0);
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);
color.a = (byte)(a * slot.A);
color.r = (byte)(r * slot.R * color.a);
color.g = (byte)(g * slot.G * color.a);
color.b = (byte)(b * slot.B * color.a);
colors[vertexIndex] = color;
colors[vertexIndex + 1] = color;
colors[vertexIndex + 2] = color;
@ -195,10 +195,11 @@ public class SkeletonComponent : MonoBehaviour {
mesh.vertices = vertices;
mesh.colors32 = colors;
mesh.uv = uvs;
mesh.subMeshCount = submeshMaterials.Count;
for (int i = 0, n = mesh.subMeshCount; i < n; ++i)
mesh.SetTriangles(submeshIndexes[i], i);
int submeshCount = submeshes.Count;
mesh.subMeshCount = submeshCount;
for (int i = 0; i < submeshCount; ++i)
mesh.SetTriangles(submeshes[i].indexes, i);
if (calculateNormals) {
mesh.RecalculateNormals();
@ -222,41 +223,32 @@ public class SkeletonComponent : MonoBehaviour {
int indexCount = submeshQuadCount * 6;
int vertexIndex = (endQuadCount - submeshQuadCount) * 4;
int[] indexes;
if (submeshIndexes.Count > submeshIndex) {
indexes = submeshIndexes[submeshIndex];
// Don't reallocate if existing indexes are right size. Skip setting vertices if already set correctly.
if (!lastSubmesh) {
if (indexes.Length == indexCount) {
if (submeshFirstVertex[submeshIndex] == vertexIndex) return;
} else
submeshIndexes[submeshIndex] = indexes = new int[indexCount];
} else {
if (indexes.Length >= indexCount) { // Allow last submesh to have more indices than required.
if (submeshFirstVertex[submeshIndex] == vertexIndex) return;
} else
submeshIndexes[submeshIndex] = indexes = new int[indexCount];
if (submeshes.Count <= submeshIndex) submeshes.Add(new Submesh());
Submesh submesh = submeshes[submeshIndex];
// Allocate indexes if not the right size, allowing last submesh to have more than required.
int[] indexes = submesh.indexes;
if (lastSubmesh ? (indexes.Length < indexCount) : (indexes.Length != indexCount))
submesh.indexes = indexes = new int[indexCount];
// Set indexes if not already set.
if (submesh.firstVertex != vertexIndex || submesh.indexCount < indexCount) {
submesh.indexCount = indexCount;
submesh.firstVertex = vertexIndex;
for (int i = 0; i < indexCount; i += 6, vertexIndex += 4) {
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) {
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;
}
if (lastSubmesh) {
// Update vertices to the end.
// Last submesh may have more indices than required, so zero indexes to the end.
if (lastSubmesh && submesh.indexCount != indexCount) {
submesh.indexCount = indexCount;
for (int i = indexCount, n = indexes.Length; i < n; i++)
indexes[i] = 0;
}
@ -291,3 +283,9 @@ public class SkeletonComponent : MonoBehaviour {
}
#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) {
// 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;
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;
skins[i] = name;
if (name == initialSkinName.stringValue)
@ -76,7 +76,7 @@ public class SkeletonAnimationInspector : Editor {
EditorGUIUtility.LookLikeInspector();
EditorGUILayout.EndHorizontal();
initialSkinName.stringValue = skinIndex == 0 ? null : skins[skinIndex];
initialSkinName.stringValue = skins[skinIndex];
// Animation name.
String[] animations = new String[component.skeleton.Data.Animations.Count + 2];

View File

@ -56,9 +56,9 @@ public class SkeletonComponentInspector : Editor {
if (component.skeleton != null) {
// 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;
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;
skins[i] = name;
if (name == initialSkinName.stringValue)
@ -72,7 +72,7 @@ public class SkeletonComponentInspector : Editor {
EditorGUIUtility.LookLikeInspector();
EditorGUILayout.EndHorizontal();
initialSkinName.stringValue = skinIndex == 0 ? null : skins[skinIndex];
initialSkinName.stringValue = skins[skinIndex];
}
EditorGUILayout.PropertyField(timeScale);

View File

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