利用Mesh可以绘制任意样式的图形,创建一个新的Mesh并设置vertices和triangles,因为任何图形都是由若干个三角形组成的
注意:三角形的顶点顺序必须是顺时针,顺时针表示正面,逆时针表示背面,而Unity在渲染时默认只渲染正面,背面是看不见的
using UnityEngine;
public class Test : MonoBehaviour
{
private void Awake()
{
DrawTriangle();
}
///
/// 绘制三角形
///
void DrawTriangle()
{
GameObject go = new GameObject("Triangle");
MeshRenderer mr = go.AddComponent();
MeshFilter mf = go.AddComponent();
Mesh mesh = new Mesh();
mesh.vertices = new Vector3[3]
{
new Vector3(0, 0, 0),
new Vector3(0, 0, 1),
new Vector3(1, 0, 0)
};
mesh.triangles = new int[3]
{
0, 1, 2
};
mf.mesh = mesh;
}
}
using UnityEngine;
public class Test : MonoBehaviour
{
private void Awake()
{
DrawSquare();
}
///
/// 绘制正方形
///
void DrawSquare()
{
GameObject go = new GameObject("Square");
MeshRenderer mr = go.AddComponent();
MeshFilter mf = go.AddComponent();
Mesh mesh = new Mesh();
mesh.vertices = new Vector3[]
{
new Vector3(0, 0, 0),
new Vector3(0, 0, 1),
new Vector3(1, 0, 1),
new Vector3(1, 0, 0)
};
mesh.triangles = new int[]
{
0, 1, 2,
0, 2, 3
};
mf.mesh = mesh;
}
}
——圆形
由三角函数求得A点坐标:(cosθ*r,sinθ*r)
绘制圆需要的参数:原点位置,半径,分成多少段(分成的段越多圆形越平滑)
using UnityEngine;
public class Test : MonoBehaviour
{
private void Awake()
{
DrawCircle();
}
///
/// 绘制圆形
///
void DrawCircle()
{
int radius = 1;
int segments = 100;
float deltaAngle = Mathf.Deg2Rad * 360f / segments;
float curAngle = 0;
Vector3[] vertices = new Vector3[segments + 1];
vertices[0] = Vector3.zero;
for (int i = 1; i < vertices.Length; i++)
{
float x = Mathf.Cos(curAngle) * radius;
float z = Mathf.Sin(curAngle) * radius;
vertices[i] = new Vector3(x, 0, z);
curAngle += deltaAngle;
}
int[] triangles = new int[segments * 3];
for (int i = 0, j = 1; i < triangles.Length - 3; i += 3, j++)
{
triangles[i] = 0;
triangles[i + 1] = j + 1;
triangles[i + 2] = j;
}
triangles[triangles.Length - 3] = 0;
triangles[triangles.Length - 2] = 1;
triangles[triangles.Length - 1] = segments;
GameObject go = new GameObject("Circle");
MeshRenderer mr = go.AddComponent();
MeshFilter mf = go.AddComponent();
Mesh mesh = new Mesh();
mesh.vertices = vertices;
mesh.triangles = triangles;
mf.mesh = mesh;
}
}
using UnityEngine;
public class Test : MonoBehaviour
{
private void Awake()
{
DrawAnnulus();
}
///
/// 绘制圆环
///
void DrawAnnulus()
{
int innerRadius = 1;
int outerRadius = 2;
int segments = 100;
float deltaAngle = Mathf.Deg2Rad * 360f / segments;
float curAngle = 0;
Vector3[] vertices = new Vector3[segments * 2];
for (int i = 0; i < vertices.Length; i += 2)
{
float innerX = Mathf.Cos(curAngle) * innerRadius;
float innerZ = Mathf.Sin(curAngle) * innerRadius;
float outerX = Mathf.Cos(curAngle) * outerRadius;
float outerZ = Mathf.Sin(curAngle) * outerRadius;
vertices[i] = new Vector3(innerX, 0, innerZ);
vertices[i + 1] = new Vector3(outerX, 0, outerZ);
curAngle += deltaAngle;
Debug.Log(vertices[i] + "==" + vertices[i + 1]);
}
int[] triangles = new int[segments * 6];
for (int i = 0, j = 0; i < triangles.Length - 6; i += 6, j += 2)
{
triangles[i] = j;
triangles[i + 1] = j + 3;
triangles[i + 2] = j + 1;
triangles[i + 3] = j;
triangles[i + 4] = j + 2;
triangles[i + 5] = j + 3;
}
triangles[triangles.Length - 6] = 2 * segments - 2;
triangles[triangles.Length - 5] = 1;
triangles[triangles.Length - 4] = 2 * segments - 1;
triangles[triangles.Length - 3] = 2 * segments - 2;
triangles[triangles.Length - 2] = 0;
triangles[triangles.Length - 1] = 1;
GameObject go = new GameObject("Annulus");
MeshRenderer mr = go.AddComponent();
MeshFilter mf = go.AddComponent();
Mesh mesh = new Mesh();
mesh.vertices = vertices;
mesh.triangles = triangles;
mf.mesh = mesh;
}
}
using UnityEngine;
public class Test : MonoBehaviour
{
private void Awake()
{
DrawSector();
}
///
/// 绘制扇形
///
void DrawSector()
{
int angle = 120;
int radius = 1;
int segments = 100;
float deltaAngle = Mathf.Deg2Rad * angle / segments;
float curAngle = Mathf.Deg2Rad * (90 - angle / 2);
Vector3[] vertices = new Vector3[segments + 1];
vertices[0] = Vector3.zero;
for (int i = 1; i < vertices.Length; i++)
{
float x = Mathf.Cos(curAngle) * radius;
float z = Mathf.Sin(curAngle) * radius;
vertices[i] = new Vector3(x, 0, z);
curAngle += deltaAngle;
}
int[] triangles = new int[segments * 3];
for (int i = 0, j = 1; i < triangles.Length - 3; i += 3, j++)
{
triangles[i] = 0;
triangles[i + 1] = j + 1;
triangles[i + 2] = j;
}
triangles[triangles.Length - 3] = 0;
triangles[triangles.Length - 2] = 1;
triangles[triangles.Length - 1] = segments;
GameObject go = new GameObject("Sector");
MeshRenderer mr = go.AddComponent();
MeshFilter mf = go.AddComponent();
Mesh mesh = new Mesh();
mesh.vertices = vertices;
mesh.triangles = triangles;
mf.mesh = mesh;
}
}