@daaoling
2017-01-13T20:45:22.000000Z
字数 2161
阅读 2551
Unity
NGUI
为什么要自定义Mesh,有以下一些好处:
1.很多时候在游戏开发中我们可以通过2分图,4分图来实现对包大小的进一步压缩.
2.满足一些游戏功能,比如地图,五维属性图之类。
Mesh 构成
create trangle //创建一个最基本的三角形
public Mesh mMesh; // First generated mesh
public MeshFilter mFilter; // Mesh filter for this draw call
void CreateTrangle()
{
mMesh = new Mesh();
//mMesh.hideFlags = HideFlags.DontSave;
mMesh.MarkDynamic();
//1 2
//0 3
Vector3[] verts = new Vector3[3];
Vector2[] uvs = new Vector2[verts.Length];
int vert_index = 0;
int uv_index = 0;
for (int i = 0; i < verts.Length; i += 4)
{
verts[vert_index++] = new Vector3(0, 0, 0); //top-left
verts[vert_index++] = new Vector3(0, 1, 0); //bottom-left
verts[vert_index++] = new Vector3(1, 1, 0); //top-right
uvs[uv_index++] = new Vector2(0, 0);
uvs[uv_index++] = new Vector2(0, 1);
uvs[uv_index++] = new Vector2(1, 1);
}
int[] triangles = new int[(verts.Length >> 1) * 3];
int triangle_index = 0;
for (int i = 0; i < verts.Length; i += 4)
{
triangles[triangle_index++] = i;
triangles[triangle_index++] = i + 1;
triangles[triangle_index++] = i + 2;
}
mMesh.vertices = verts;
mMesh.uv = uvs;
mMesh.triangles = triangles;
////Shader wants normals, but the mesh doesn't have them
mMesh.RecalculateNormals();
mFilter.mesh = mMesh;
}
void UpdateGeometryV2()
{
mMesh = new Mesh();
//mMesh.hideFlags = HideFlags.DontSave;
mMesh.MarkDynamic();
//1 2
//0 3
BetterList<Vector3> verts = new BetterList<Vector3>();
BetterList<Vector2> uvs = new BetterList<Vector2>();
BetterList<int> triangles = new BetterList<int>();
int panel_count = 5;
for (int i = 0; i < panel_count; i++)
{
verts.Add(Vector3.zero);
verts.Add(Quaternion.AngleAxis(-72.0f * i, Vector3.forward) * Vector3.up);
verts.Add(Quaternion.AngleAxis(-72.0f * (i+1), Vector3.forward) * Vector3.up);
//verts.Add(Vector3.up * Quaternion.);
//verts.Add(new Vector3(0, 0, 0)); //top-left
//verts.Add(new Vector3(0, 1, 0)); //bottom-left
//verts.Add(new Vector3(1, 1, 0)); //top-right
uvs.Add(new Vector2(0, 0));
uvs.Add(new Vector2(0, 1));
uvs.Add(new Vector2(1, 1));
triangles.Add(3 * i);
triangles.Add(3 * i + 1);
triangles.Add(3 * i + 2);
}
mMesh.vertices = verts.ToArray();
mMesh.uv = uvs.ToArray();
mMesh.triangles = triangles.ToArray();
//GenerateCachedIndexBuffer(verts.size, (verts.size >> 1) * 3);
mMesh.RecalculateNormals();
mFilter.mesh = mMesh;
}
UIDrawCall.cs
Debug.LogError("UIWidgets must fill the buffer with 4 vertices per quad. Found " + count);
考虑重构UIDrwaCall 实现三角形