mirror of
https://github.com/EsotericSoftware/spine-runtimes.git
synced 2026-02-06 23:34:53 +08:00
Double buffer meshes.
Number 13: http://docs.unity3d.com/Documentation/Manual/MobileOptimisation.html
This commit is contained in:
parent
0a74525261
commit
271d0b630a
@ -46,7 +46,9 @@ public class SkeletonComponent : MonoBehaviour {
|
||||
public float timeScale = 1;
|
||||
public bool calculateNormals;
|
||||
public bool calculateTangents;
|
||||
private Mesh mesh;
|
||||
private MeshFilter meshFilter;
|
||||
private Mesh mesh, mesh1, mesh2;
|
||||
private bool useMesh1;
|
||||
private float[] vertexPositions = new float[8];
|
||||
private int lastVertexCount;
|
||||
private Vector3[] vertices;
|
||||
@ -57,7 +59,7 @@ public class SkeletonComponent : MonoBehaviour {
|
||||
private List<Submesh> submeshes = new List<Submesh>();
|
||||
|
||||
public virtual void Clear () {
|
||||
GetComponent<MeshFilter>().mesh = null;
|
||||
meshFilter.sharedMesh = null;
|
||||
DestroyImmediate(mesh);
|
||||
mesh = null;
|
||||
renderer.sharedMaterial = null;
|
||||
@ -65,11 +67,9 @@ public class SkeletonComponent : MonoBehaviour {
|
||||
}
|
||||
|
||||
public virtual void Initialize () {
|
||||
mesh = new Mesh();
|
||||
GetComponent<MeshFilter>().mesh = mesh;
|
||||
mesh.name = "Skeleton Mesh";
|
||||
mesh.hideFlags = HideFlags.HideAndDontSave;
|
||||
mesh.MarkDynamic();
|
||||
meshFilter = GetComponent<MeshFilter>();
|
||||
mesh1 = newMesh();
|
||||
mesh2 = newMesh();
|
||||
|
||||
vertices = new Vector3[0];
|
||||
|
||||
@ -81,6 +81,14 @@ public class SkeletonComponent : MonoBehaviour {
|
||||
}
|
||||
}
|
||||
|
||||
private Mesh newMesh () {
|
||||
Mesh mesh = new Mesh();
|
||||
mesh.name = "Skeleton Mesh";
|
||||
mesh.hideFlags = HideFlags.HideAndDontSave;
|
||||
mesh.MarkDynamic();
|
||||
return mesh;
|
||||
}
|
||||
|
||||
public virtual void UpdateSkeleton () {
|
||||
skeleton.Update(Time.deltaTime * timeScale);
|
||||
skeleton.UpdateWorldTransform();
|
||||
@ -134,9 +142,13 @@ public class SkeletonComponent : MonoBehaviour {
|
||||
else
|
||||
sharedMaterials = submeshMaterials.ToArray();
|
||||
renderer.sharedMaterials = sharedMaterials;
|
||||
|
||||
// Double buffer mesh.
|
||||
Mesh mesh = useMesh1 ? mesh1 : mesh2;
|
||||
useMesh1 = !useMesh1;
|
||||
meshFilter.sharedMesh = mesh;
|
||||
|
||||
// Ensure mesh data is the right size.
|
||||
Mesh mesh = this.mesh;
|
||||
Vector3[] vertices = this.vertices;
|
||||
int vertexCount = quadCount * 4;
|
||||
bool newTriangles = vertexCount > vertices.Length;
|
||||
@ -145,7 +157,8 @@ public class SkeletonComponent : MonoBehaviour {
|
||||
this.vertices = vertices = new Vector3[vertexCount];
|
||||
this.colors = new Color32[vertexCount];
|
||||
this.uvs = new Vector2[vertexCount];
|
||||
mesh.Clear();
|
||||
mesh1.Clear();
|
||||
mesh2.Clear();
|
||||
} else {
|
||||
// Too many vertices, zero the extra.
|
||||
Vector3 zero = Vector3.zero;
|
||||
@ -206,14 +219,17 @@ public class SkeletonComponent : MonoBehaviour {
|
||||
Vector3 normal = new Vector3(0, 0, -1);
|
||||
for (int i = 0; i < vertexCount; i++)
|
||||
normals[i] = normal;
|
||||
mesh.normals = normals;
|
||||
(useMesh1 ? mesh1 : mesh2).vertices = vertices;
|
||||
mesh1.normals = normals;
|
||||
mesh2.normals = normals;
|
||||
|
||||
if (calculateTangents) {
|
||||
Vector4[] tangents = new Vector4[vertexCount];
|
||||
Vector3 tangent = new Vector3(0, 0, 1);
|
||||
for (int i = 0; i < vertexCount; i++)
|
||||
tangents[i] = tangent;
|
||||
mesh.tangents = tangents;
|
||||
mesh1.tangents = tangents;
|
||||
mesh2.tangents = tangents;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -46,7 +46,9 @@ public class SkeletonComponent : MonoBehaviour {
|
||||
public float timeScale = 1;
|
||||
public bool calculateNormals;
|
||||
public bool calculateTangents;
|
||||
private Mesh mesh;
|
||||
private MeshFilter meshFilter;
|
||||
private Mesh mesh, mesh1, mesh2;
|
||||
private bool useMesh1;
|
||||
private float[] vertexPositions = new float[8];
|
||||
private int lastVertexCount;
|
||||
private Vector3[] vertices;
|
||||
@ -57,7 +59,7 @@ public class SkeletonComponent : MonoBehaviour {
|
||||
private List<Submesh> submeshes = new List<Submesh>();
|
||||
|
||||
public virtual void Clear () {
|
||||
GetComponent<MeshFilter>().mesh = null;
|
||||
meshFilter.sharedMesh = null;
|
||||
DestroyImmediate(mesh);
|
||||
mesh = null;
|
||||
renderer.sharedMaterial = null;
|
||||
@ -65,11 +67,9 @@ public class SkeletonComponent : MonoBehaviour {
|
||||
}
|
||||
|
||||
public virtual void Initialize () {
|
||||
mesh = new Mesh();
|
||||
GetComponent<MeshFilter>().mesh = mesh;
|
||||
mesh.name = "Skeleton Mesh";
|
||||
mesh.hideFlags = HideFlags.HideAndDontSave;
|
||||
mesh.MarkDynamic();
|
||||
meshFilter = GetComponent<MeshFilter>();
|
||||
mesh1 = newMesh();
|
||||
mesh2 = newMesh();
|
||||
|
||||
vertices = new Vector3[0];
|
||||
|
||||
@ -81,6 +81,14 @@ public class SkeletonComponent : MonoBehaviour {
|
||||
}
|
||||
}
|
||||
|
||||
private Mesh newMesh () {
|
||||
Mesh mesh = new Mesh();
|
||||
mesh.name = "Skeleton Mesh";
|
||||
mesh.hideFlags = HideFlags.HideAndDontSave;
|
||||
mesh.MarkDynamic();
|
||||
return mesh;
|
||||
}
|
||||
|
||||
public virtual void UpdateSkeleton () {
|
||||
skeleton.Update(Time.deltaTime * timeScale);
|
||||
skeleton.UpdateWorldTransform();
|
||||
@ -134,9 +142,13 @@ public class SkeletonComponent : MonoBehaviour {
|
||||
else
|
||||
sharedMaterials = submeshMaterials.ToArray();
|
||||
renderer.sharedMaterials = sharedMaterials;
|
||||
|
||||
// Double buffer mesh.
|
||||
Mesh mesh = useMesh1 ? mesh1 : mesh2;
|
||||
useMesh1 = !useMesh1;
|
||||
meshFilter.sharedMesh = mesh;
|
||||
|
||||
// Ensure mesh data is the right size.
|
||||
Mesh mesh = this.mesh;
|
||||
Vector3[] vertices = this.vertices;
|
||||
int vertexCount = quadCount * 4;
|
||||
bool newTriangles = vertexCount > vertices.Length;
|
||||
@ -145,7 +157,8 @@ public class SkeletonComponent : MonoBehaviour {
|
||||
this.vertices = vertices = new Vector3[vertexCount];
|
||||
this.colors = new Color32[vertexCount];
|
||||
this.uvs = new Vector2[vertexCount];
|
||||
mesh.Clear();
|
||||
mesh1.Clear();
|
||||
mesh2.Clear();
|
||||
} else {
|
||||
// Too many vertices, zero the extra.
|
||||
Vector3 zero = Vector3.zero;
|
||||
@ -206,14 +219,17 @@ public class SkeletonComponent : MonoBehaviour {
|
||||
Vector3 normal = new Vector3(0, 0, -1);
|
||||
for (int i = 0; i < vertexCount; i++)
|
||||
normals[i] = normal;
|
||||
mesh.normals = normals;
|
||||
(useMesh1 ? mesh1 : mesh2).vertices = vertices;
|
||||
mesh1.normals = normals;
|
||||
mesh2.normals = normals;
|
||||
|
||||
if (calculateTangents) {
|
||||
Vector4[] tangents = new Vector4[vertexCount];
|
||||
Vector3 tangent = new Vector3(0, 0, 1);
|
||||
for (int i = 0; i < vertexCount; i++)
|
||||
tangents[i] = tangent;
|
||||
mesh.tangents = tangents;
|
||||
mesh1.tangents = tangents;
|
||||
mesh2.tangents = tangents;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user