1 using System;
2 using
UnityEngine;
3 using
Object = UnityEngine.Object;
4
5 namespace
UnityStandardAssets.ImageEffects
6 {
7     
class Triangles
8     {
9         
private static Mesh[] meshes;
10         
private static int currentTris = 0;
11
12         
static bool HasMeshes()
13         {
14             
if (meshes == null)
15                 
return false;
16             
for (int i = 0; i < meshes.Length; i++)
17                 
if (null == meshes[i])
18                     
return false;
19
20             
return true;
21         }
22
23         
static void Cleanup()
24         {
25             
if (meshes == null)
26                 
return;
27
28             
for (int i = 0; i < meshes.Length; i++)
29             {
30                 
if (null != meshes[i])
31                 {
32                     Object.DestroyImmediate(meshes[i]);
33                     meshes[i] =
null;
34                 }
35             }
36             meshes =
null;
37         }
38
39         
static Mesh[] GetMeshes(int totalWidth, int totalHeight)
40         {
41             
if (HasMeshes() && (currentTris == (totalWidth * totalHeight)))
42             {
43                 
return meshes;
44             }
45
46             
int maxTris = 65000 / 3;
47             
int totalTris = totalWidth * totalHeight;
48             currentTris = totalTris;
49
50             
int meshCount = Mathf.CeilToInt((1.0f * totalTris) / (1.0f * maxTris));
51
52             meshes =
new Mesh[meshCount];
53
54             
int i = 0;
55             
int index = 0;
56             
for (i = 0; i < totalTris; i += maxTris)
57             {
58                 
int tris = Mathf.FloorToInt(Mathf.Clamp((totalTris - i), 0, maxTris));
59
60                 meshes[index] = GetMesh(tris, i, totalWidth, totalHeight);
61                 index++;
62             }
63
64             
return meshes;
65         }
66
67         
static Mesh GetMesh(int triCount, int triOffset, int totalWidth, int totalHeight)
68         {
69             
var mesh = new Mesh();
70             mesh.hideFlags = HideFlags.DontSave;
71
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];
76
77             
for (int i = 0; i < triCount; i++)
78             {
79                 
int i3 = i * 3;
80                 
int vertexWithOffset = triOffset + i;
81
82                 
float x = Mathf.Floor(vertexWithOffset % totalWidth) / totalWidth;
83                 
float y = Mathf.Floor(vertexWithOffset / totalWidth) / totalHeight;
84
85                 Vector3 position =
new Vector3(x * 2 - 1, y * 2 - 1, 1.0f);
86
87                 verts[i3 +
0] = position;
88                 verts[i3 +
1] = position;
89                 verts[i3 +
2] = position;
90
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);
94
95                 uvs2[i3 +
0] = new Vector2(x, y);
96                 uvs2[i3 +
1] = new Vector2(x, y);
97                 uvs2[i3 +
2] = new Vector2(x, y);
98
99                 tris[i3 +
0] = i3 + 0;
100                 tris[i3 +
1] = i3 + 1;
101                 tris[i3 +
2] = i3 + 2;
102             }
103
104             mesh.vertices = verts;
105             mesh.triangles = tris;
106             mesh.uv = uvs;
107             mesh.uv2 = uvs2;
108
109             
return mesh;
110         }
111     }
112 }


Gõ tìm kiếm nhanh...