mirror of
https://github.com/EsotericSoftware/spine-runtimes.git
synced 2026-03-26 22:49:01 +08:00
replaced Mesh.RecalculateBounds() in SkeletonRenderer with manual calculation, since we're going through all the vertices anyway
optimized SkeletonRenderer.OnDrawGizmos
This commit is contained in:
parent
990d8d3016
commit
b07e24f910
@ -69,8 +69,8 @@ public class SkeletonRenderer : MonoBehaviour {
|
|||||||
private Color32[] colors;
|
private Color32[] colors;
|
||||||
private Vector2[] uvs;
|
private Vector2[] uvs;
|
||||||
private Material[] sharedMaterials = new Material[0];
|
private Material[] sharedMaterials = new Material[0];
|
||||||
private readonly List<Material> submeshMaterials = new List<Material>();
|
private readonly ExposedList<Material> submeshMaterials = new ExposedList<Material>();
|
||||||
private readonly List<Submesh> submeshes = new List<Submesh>();
|
private readonly ExposedList<Submesh> submeshes = new ExposedList<Submesh>();
|
||||||
|
|
||||||
|
|
||||||
public virtual void Reset () {
|
public virtual void Reset () {
|
||||||
@ -250,6 +250,14 @@ public class SkeletonRenderer : MonoBehaviour {
|
|||||||
lastVertexCount = vertexCount;
|
lastVertexCount = vertexCount;
|
||||||
|
|
||||||
// Setup mesh.
|
// Setup mesh.
|
||||||
|
Vector3 meshBoundsMin;
|
||||||
|
meshBoundsMin.x = float.MaxValue;
|
||||||
|
meshBoundsMin.y = float.MaxValue;
|
||||||
|
meshBoundsMin.z = float.MaxValue;
|
||||||
|
Vector3 meshBoundsMax;
|
||||||
|
meshBoundsMax.x = float.MinValue;
|
||||||
|
meshBoundsMax.y = float.MinValue;
|
||||||
|
meshBoundsMax.z = float.MinValue;
|
||||||
float[] tempVertices = this.tempVertices;
|
float[] tempVertices = this.tempVertices;
|
||||||
Vector2[] uvs = this.uvs;
|
Vector2[] uvs = this.uvs;
|
||||||
Color32[] colors = this.colors;
|
Color32[] colors = this.colors;
|
||||||
@ -299,6 +307,48 @@ public class SkeletonRenderer : MonoBehaviour {
|
|||||||
uvs[vertexIndex + 3].x = regionUVs[RegionAttachment.X3];
|
uvs[vertexIndex + 3].x = regionUVs[RegionAttachment.X3];
|
||||||
uvs[vertexIndex + 3].y = regionUVs[RegionAttachment.Y3];
|
uvs[vertexIndex + 3].y = regionUVs[RegionAttachment.Y3];
|
||||||
|
|
||||||
|
// Calculate min/max X
|
||||||
|
if (tempVertices[RegionAttachment.X1] < meshBoundsMin.x)
|
||||||
|
meshBoundsMin.x = tempVertices[RegionAttachment.X1];
|
||||||
|
else if (tempVertices[RegionAttachment.X1] > meshBoundsMax.x)
|
||||||
|
meshBoundsMax.x = tempVertices[RegionAttachment.X1];
|
||||||
|
if (tempVertices[RegionAttachment.X2] < meshBoundsMin.x)
|
||||||
|
meshBoundsMin.x = tempVertices[RegionAttachment.X2];
|
||||||
|
else if (tempVertices[RegionAttachment.X2] > meshBoundsMax.x)
|
||||||
|
meshBoundsMax.x = tempVertices[RegionAttachment.X2];
|
||||||
|
if (tempVertices[RegionAttachment.X3] < meshBoundsMin.x)
|
||||||
|
meshBoundsMin.x = tempVertices[RegionAttachment.X3];
|
||||||
|
else if (tempVertices[RegionAttachment.X3] > meshBoundsMax.x)
|
||||||
|
meshBoundsMax.x = tempVertices[RegionAttachment.X3];
|
||||||
|
if (tempVertices[RegionAttachment.X4] < meshBoundsMin.x)
|
||||||
|
meshBoundsMin.x = tempVertices[RegionAttachment.X4];
|
||||||
|
else if (tempVertices[RegionAttachment.X4] > meshBoundsMax.x)
|
||||||
|
meshBoundsMax.x = tempVertices[RegionAttachment.X4];
|
||||||
|
|
||||||
|
// Calculate min/max Y
|
||||||
|
if (tempVertices[RegionAttachment.Y1] < meshBoundsMin.y)
|
||||||
|
meshBoundsMin.y = tempVertices[RegionAttachment.Y1];
|
||||||
|
else if (tempVertices[RegionAttachment.Y1] > meshBoundsMax.y)
|
||||||
|
meshBoundsMax.y = tempVertices[RegionAttachment.Y1];
|
||||||
|
if (tempVertices[RegionAttachment.Y2] < meshBoundsMin.y)
|
||||||
|
meshBoundsMin.y = tempVertices[RegionAttachment.Y2];
|
||||||
|
else if (tempVertices[RegionAttachment.Y2] > meshBoundsMax.y)
|
||||||
|
meshBoundsMax.y = tempVertices[RegionAttachment.Y2];
|
||||||
|
if (tempVertices[RegionAttachment.Y3] < meshBoundsMin.y)
|
||||||
|
meshBoundsMin.y = tempVertices[RegionAttachment.Y3];
|
||||||
|
else if (tempVertices[RegionAttachment.Y3] > meshBoundsMax.y)
|
||||||
|
meshBoundsMax.y = tempVertices[RegionAttachment.Y3];
|
||||||
|
if (tempVertices[RegionAttachment.Y4] < meshBoundsMin.y)
|
||||||
|
meshBoundsMin.y = tempVertices[RegionAttachment.Y4];
|
||||||
|
else if (tempVertices[RegionAttachment.Y4] > meshBoundsMax.y)
|
||||||
|
meshBoundsMax.y = tempVertices[RegionAttachment.Y4];
|
||||||
|
|
||||||
|
// Calculate min/max Z
|
||||||
|
if (z < meshBoundsMin.z)
|
||||||
|
meshBoundsMin.z = z;
|
||||||
|
else if (z > meshBoundsMax.z)
|
||||||
|
meshBoundsMax.z = z;
|
||||||
|
|
||||||
vertexIndex += 4;
|
vertexIndex += 4;
|
||||||
} else {
|
} else {
|
||||||
if (!renderMeshes)
|
if (!renderMeshes)
|
||||||
@ -326,6 +376,19 @@ public class SkeletonRenderer : MonoBehaviour {
|
|||||||
colors[vertexIndex] = color;
|
colors[vertexIndex] = color;
|
||||||
uvs[vertexIndex].x = meshUVs[ii];
|
uvs[vertexIndex].x = meshUVs[ii];
|
||||||
uvs[vertexIndex].y = meshUVs[ii + 1];
|
uvs[vertexIndex].y = meshUVs[ii + 1];
|
||||||
|
|
||||||
|
if (tempVertices[ii] < meshBoundsMin.x)
|
||||||
|
meshBoundsMin.x = tempVertices[ii];
|
||||||
|
else if (tempVertices[ii] > meshBoundsMax.x)
|
||||||
|
meshBoundsMax.x = tempVertices[ii];
|
||||||
|
if (tempVertices[ii + 1]< meshBoundsMin.y)
|
||||||
|
meshBoundsMin.y = tempVertices[ii + 1];
|
||||||
|
else if (tempVertices[ii + 1] > meshBoundsMax.y)
|
||||||
|
meshBoundsMax.y = tempVertices[ii + 1];
|
||||||
|
if (z < meshBoundsMin.z)
|
||||||
|
meshBoundsMin.z = z;
|
||||||
|
else if (z > meshBoundsMax.z)
|
||||||
|
meshBoundsMax.z = z;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
SkinnedMeshAttachment skinnedMeshAttachment = attachment as SkinnedMeshAttachment;
|
SkinnedMeshAttachment skinnedMeshAttachment = attachment as SkinnedMeshAttachment;
|
||||||
@ -351,6 +414,19 @@ public class SkeletonRenderer : MonoBehaviour {
|
|||||||
colors[vertexIndex] = color;
|
colors[vertexIndex] = color;
|
||||||
uvs[vertexIndex].x = meshUVs[ii];
|
uvs[vertexIndex].x = meshUVs[ii];
|
||||||
uvs[vertexIndex].y = meshUVs[ii + 1];
|
uvs[vertexIndex].y = meshUVs[ii + 1];
|
||||||
|
|
||||||
|
if (tempVertices[ii] < meshBoundsMin.x)
|
||||||
|
meshBoundsMin.x = tempVertices[ii];
|
||||||
|
else if (tempVertices[ii] > meshBoundsMax.x)
|
||||||
|
meshBoundsMax.x = tempVertices[ii];
|
||||||
|
if (tempVertices[ii + 1]< meshBoundsMin.y)
|
||||||
|
meshBoundsMin.y = tempVertices[ii + 1];
|
||||||
|
else if (tempVertices[ii + 1] > meshBoundsMax.y)
|
||||||
|
meshBoundsMax.y = tempVertices[ii + 1];
|
||||||
|
if (z < meshBoundsMin.z)
|
||||||
|
meshBoundsMin.z = z;
|
||||||
|
else if (z > meshBoundsMax.z)
|
||||||
|
meshBoundsMax.z = z;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -368,8 +444,10 @@ public class SkeletonRenderer : MonoBehaviour {
|
|||||||
int submeshCount = submeshMaterials.Count;
|
int submeshCount = submeshMaterials.Count;
|
||||||
mesh.subMeshCount = submeshCount;
|
mesh.subMeshCount = submeshCount;
|
||||||
for (int i = 0; i < submeshCount; ++i)
|
for (int i = 0; i < submeshCount; ++i)
|
||||||
mesh.SetTriangles(submeshes[i].triangles, i);
|
mesh.SetTriangles(submeshes.Items[i].triangles, i);
|
||||||
mesh.RecalculateBounds();
|
|
||||||
|
Vector3 meshBoundsExtents = meshBoundsMax - meshBoundsMin;
|
||||||
|
mesh.bounds = new Bounds(meshBoundsMin + meshBoundsExtents * 0.5f, meshBoundsExtents);
|
||||||
|
|
||||||
if (newTriangles && calculateNormals) {
|
if (newTriangles && calculateNormals) {
|
||||||
Vector3[] normals = new Vector3[vertexCount];
|
Vector3[] normals = new Vector3[vertexCount];
|
||||||
@ -403,7 +481,7 @@ public class SkeletonRenderer : MonoBehaviour {
|
|||||||
else if (immutableTriangles)
|
else if (immutableTriangles)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
Submesh submesh = submeshes[submeshIndex];
|
Submesh submesh = submeshes.Items[submeshIndex];
|
||||||
|
|
||||||
int[] triangles = submesh.triangles;
|
int[] triangles = submesh.triangles;
|
||||||
int trianglesCapacity = triangles.Length;
|
int trianglesCapacity = triangles.Length;
|
||||||
@ -504,22 +582,16 @@ public class SkeletonRenderer : MonoBehaviour {
|
|||||||
#if UNITY_EDITOR
|
#if UNITY_EDITOR
|
||||||
void OnDrawGizmos () {
|
void OnDrawGizmos () {
|
||||||
// Make selection easier by drawing a clear gizmo over the skeleton.
|
// Make selection easier by drawing a clear gizmo over the skeleton.
|
||||||
if (vertices == null) return;
|
meshFilter = GetComponent<MeshFilter>();
|
||||||
Vector3 gizmosCenter = new Vector3();
|
if (meshFilter == null) return;
|
||||||
Vector3 gizmosSize = new Vector3();
|
|
||||||
Vector3 min = new Vector3(float.MaxValue, float.MaxValue, 0f);
|
Mesh mesh = meshFilter.sharedMesh;
|
||||||
Vector3 max = new Vector3(float.MinValue, float.MinValue, 0f);
|
if (mesh == null) return;
|
||||||
foreach (Vector3 vert in vertices) {
|
|
||||||
min = Vector3.Min(min, vert);
|
Bounds meshBounds = mesh.bounds;
|
||||||
max = Vector3.Max(max, vert);
|
Gizmos.color = Color.red;
|
||||||
}
|
|
||||||
float width = max.x - min.x;
|
|
||||||
float height = max.y - min.y;
|
|
||||||
gizmosCenter = new Vector3(min.x + (width / 2f), min.y + (height / 2f), 0f);
|
|
||||||
gizmosSize = new Vector3(width, height, 1f);
|
|
||||||
Gizmos.color = Color.clear;
|
|
||||||
Gizmos.matrix = transform.localToWorldMatrix;
|
Gizmos.matrix = transform.localToWorldMatrix;
|
||||||
Gizmos.DrawCube(gizmosCenter, gizmosSize);
|
Gizmos.DrawCube(meshBounds.center, meshBounds.size);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user