该文件只限于画2D的图形,在模仿Paper.io这个游戏时,遇到的问题,记录下,以供下次参考.
关于Unity中的网格过滤器,和网格渲染器,可以参考Unity的手册文档(请看文章最后).
这里简单说下我在模仿paper这个游戏的思路.
玩家在移动的过程中,会不断的更新位置,所以需要记录下玩家的点.并画在屏幕上,可以用Mesh Renderer,和Mesh Filter组件来渲染,然后初始化一个mesh对象,将其设置给Mesh Filter的相关属性.这个mesh就是负责画图的内容,他需要图形的顶点坐标,和三角形的顺序,而如果顶点数据复杂,计算三角形的顺序,就会变得繁琐,这个Triangulator刚好将这个过程封装了.只要给他传入一个顶点坐标数组,就能直接生成mesh需要的三角形数组出来,然后将顶点坐标,和三角形数组设置给mesh,即可显示我们需要的图形出来.
这里有个简单示例
void Start () {
List<Vector2> vectors = new List<Vector2> ();
vectors.Add (new Vector2 (0, 0));
vectors.Add (new Vector2 (10, 0));
vectors.Add (new Vector2 (10, 20));
vectors.Add (new Vector2 (20, 20));
vectors.Add (new Vector2 (20, 30));
vectors.Add (new Vector2 (10, 30));
vectors.Add (new Vector2 (0, 20));
// Use the triangulator to get indices for creating triangles
Triangulator tr = new Triangulator(vectors.ToArray());
int[] indices = tr.Triangulate();
// Create the Vector3 vertices
Vector3[] vertices = new Vector3[vectors.Count];
for (int i=0; i<vertices.Length; i++) {
vertices[i] = new Vector3(vectors[i].x, vectors[i].y, 0);
}
// Create the mesh
Mesh msh = new Mesh();
msh.vertices = vertices;
msh.triangles = indices;
msh.RecalculateNormals();
msh.RecalculateBounds();
// Set up game object with mesh;
gameObject.AddComponent(typeof(MeshRenderer));
MeshFilter filter = gameObject.GetComponent<MeshFilter> ();
if (filter == null) {
filter = gameObject.AddComponent<MeshFilter>();
}
filter.mesh = msh;
}
生成的内容就是这个样子
以下为Triangulator的完整代码
using UnityEngine;
using System.Collections.Generic;
public class Triangulator
{
private List<Vector2> m_points = new List<Vector2>();
public Triangulator (Vector2[] points) {
m_points = new List<Vector2>(points);
}
public int[] Triangulate() {
List<int> indices = new List<int>();
int n = m_points.Count;
if (n < 3)
return indices.ToArray();
int[] V = new int[n];
if (Area() > 0) {
for (int v = 0; v < n; v++)
V[v] = v;
}
else {
for (int v = 0; v < n; v++)
V[v] = (n - 1) - v;
}
int nv = n;
int count = 2 * nv;
for (int m = 0, v = nv - 1; nv > 2; ) {
if ((count--) <= 0)
return indices.ToArray();
int u = v;
if (nv <= u)
u = 0;
v = u + 1;
if (nv <= v)
v = 0;
int w = v + 1;
if (nv <= w)
w = 0;
if (Snip(u, v, w, nv, V)) {
int a, b, c, s, t;
a = V[u];
b = V[v];
c = V[w];
indices.Add(a);
indices.Add(b);
indices.Add(c);
m++;
for (s = v, t = v + 1; t < nv; s++, t++)
V[s] = V[t];
nv--;
count = 2 * nv;
}
}
indices.Reverse();
return indices.ToArray();
}
private float Area () {
int n = m_points.Count;
float A = 0.0f;
for (int p = n - 1, q = 0; q < n; p = q++) {
Vector2 pval = m_points[p];
Vector2 qval = m_points[q];
A += pval.x * qval.y - qval.x * pval.y;
}
return (A * 0.5f);
}
private bool Snip (int u, int v, int w, int n, int[] V) {
int p;
Vector2 A = m_points[V[u]];
Vector2 B = m_points[V[v]];
Vector2 C = m_points[V[w]];
if (Mathf.Epsilon > (((B.x - A.x) * (C.y - A.y)) - ((B.y - A.y) * (C.x - A.x))))
return false;
for (p = 0; p < n; p++) {
if ((p == u) || (p == v) || (p == w))
continue;
Vector2 P = m_points[V[p]];
if (InsideTriangle(A, B, C, P))
return false;
}
return true;
}
private bool InsideTriangle (Vector2 A, Vector2 B, Vector2 C, Vector2 P) {
float ax, ay, bx, by, cx, cy, apx, apy, bpx, bpy, cpx, cpy;
float cCROSSap, bCROSScp, aCROSSbp;
ax = C.x - B.x; ay = C.y - B.y;
bx = A.x - C.x; by = A.y - C.y;
cx = B.x - A.x; cy = B.y - A.y;
apx = P.x - A.x; apy = P.y - A.y;
bpx = P.x - B.x; bpy = P.y - B.y;
cpx = P.x - C.x; cpy = P.y - C.y;
aCROSSbp = ax * bpy - ay * bpx;
cCROSSap = cx * apy - cy * apx;
bCROSScp = bx * cpy - by * cpx;
return ((aCROSSbp >= 0.0f) && (bCROSScp >= 0.0f) && (cCROSSap >= 0.0f));
}
}
参考资料:
http://wiki.unity3d.com/index.php?title=Triangulator
https://docs.unity3d.com/ScriptReference/Mesh.html