3 using Object = UnityEngine.Object;
5 namespace UnityStandardAssets.ImageEffects
9 private static Mesh[] meshes;
10 private static int currentTris = 0;
12 static bool HasMeshes()
16 for (
int i = 0; i < meshes.Length; i++)
17 if (null == meshes[i])
28 for (
int i = 0; i < meshes.Length; i++)
30 if (null != meshes[i])
32 Object.DestroyImmediate(meshes[i]);
39 static Mesh[] GetMeshes(
int totalWidth,
int totalHeight)
41 if (HasMeshes() && (currentTris == (totalWidth * totalHeight)))
46 int maxTris = 65000 / 3;
47 int totalTris = totalWidth * totalHeight;
48 currentTris = totalTris;
50 int meshCount = Mathf.CeilToInt((1.0f * totalTris) / (1.0f * maxTris));
52 meshes =
new Mesh[meshCount];
56 for (i = 0; i < totalTris; i += maxTris)
58 int tris = Mathf.FloorToInt(Mathf.Clamp((totalTris - i), 0, maxTris));
60 meshes[index] = GetMesh(tris, i, totalWidth, totalHeight);
67 static Mesh GetMesh(
int triCount,
int triOffset,
int totalWidth,
int totalHeight)
69 var mesh =
new Mesh();
70 mesh.hideFlags = HideFlags.DontSave;
72 var verts =
new Vector3[triCount * 3];
73 var uvs =
new Vector2[triCount * 3];
74 var uvs2 =
new Vector2[triCount * 3];
75 var tris =
new int[triCount * 3];
77 for (
int i = 0; i < triCount; i++)
80 int vertexWithOffset = triOffset + i;
82 float x = Mathf.Floor(vertexWithOffset % totalWidth) / totalWidth;
83 float y = Mathf.Floor(vertexWithOffset / totalWidth) / totalHeight;
85 Vector3 position =
new Vector3(x * 2 - 1, y * 2 - 1, 1.0f);
87 verts[i3 + 0] = position;
88 verts[i3 + 1] = position;
89 verts[i3 + 2] = position;
91 uvs[i3 + 0] =
new Vector2(0.0f, 0.0f);
92 uvs[i3 + 1] =
new Vector2(1.0f, 0.0f);
93 uvs[i3 + 2] =
new Vector2(0.0f, 1.0f);
95 uvs2[i3 + 0] =
new Vector2(x, y);
96 uvs2[i3 + 1] =
new Vector2(x, y);
97 uvs2[i3 + 2] =
new Vector2(x, y);
99 tris[i3 + 0] = i3 + 0;
100 tris[i3 + 1] = i3 + 1;
101 tris[i3 + 2] = i3 + 2;
104 mesh.vertices = verts;
105 mesh.triangles = tris;