Unity3D之Mesh(五)绘制圆
发表于2018-11-16
本篇给大家介绍下Mesh绘制圆,Unity3D中Mesh的基本单位是三角形,而圆形就是由许许多多的三角形组成的。那么我们就知道了绘制圆形的Mesh需要两个变量:圆的半径以及分割数;
一、实现过程
基本过程与之前的类似,最基本的依然是顶点以及三角形索引数组,即:我们需要根据圆的半径以及预备的分割数,通过算法得到:顶点的Vector3数组 以及对应的三角形索引数组;
1、基本的程序实现架构如下:
using UnityEngine;
[RequireComponent(typeof(MeshRenderer), typeof(MeshFilter))]
public class yuan : MonoBehaviour
{
public float Radius = 6; //半径
public int Segments = 60; //分割数
private MeshFilter meshFilter;
void Start()
{
meshFilter = GetComponent<MeshFilter>();
meshFilter.mesh = CreateMesh(Radius, Segments);
}
Mesh CreateMesh(float radius, int segments)
{
Mesh mesh = new Mesh();
return mesh;
}
}
2、接下来就需要实现CreateMesh()函数的基本功能(下述的程序包含该函数功能的实现);整个程序的代码如下:
using UnityEngine;
[RequireComponent(typeof(MeshRenderer), typeof(MeshFilter))]
public class yuan : MonoBehaviour
{
public float Radius = 6; //半径
public int Segments = 60; //分割数
private MeshFilter meshFilter;
void Start()
{
meshFilter = GetComponent<MeshFilter>();
meshFilter.mesh = CreateMesh(Radius, Segments);
}
Mesh CreateMesh(float radius, int segments)
{
//vertices:
int vertices_count = Segments + 1;
Vector3[] vertices = new Vector3[vertices_count];
vertices[0] = Vector3.zero;
float angledegree = 360.0f;
float angleRad = Mathf.Deg2Rad * angledegree;
float angleCur = angleRad;
float angledelta = angleRad / Segments;
for(int i=1;i< vertices_count; i++)
{
float cosA = Mathf.Cos(angleCur);
float sinA = Mathf.Sin(angleCur);
vertices[i] = new Vector3(Radius * cosA, 0, Radius * sinA);
angleCur -= angledelta;
}
//triangles
int triangle_count = segments * 3;
int[] triangles = new int[triangle_count];
for(int i=0,vi=1;i<= triangle_count-1;i+=3,vi++) //因为该案例分割了60个三角形,故最后一个索引顺序应该是:0 60 1;所以需要单独处理
{
triangles[i] = 0;
triangles[i + 1] = vi;
triangles[i + 2] = vi + 1;
}
triangles[triangle_count - 3] = 0;
triangles[triangle_count - 2] = vertices_count - 1;
triangles[triangle_count - 1] = 1; //为了完成闭环,将最后一个三角形单独拎出来
//uv:
Vector2[] uvs = new Vector2[vertices_count];
for (int i = 0; i < vertices_count; i++)
{
uvs[i] = new Vector2(vertices[i].x / radius / 2 + 0.5f, vertices[i].z / radius / 2 + 0.5f);
}
//负载属性与mesh
Mesh mesh = new Mesh();
mesh.vertices = vertices;
mesh.triangles = triangles;
mesh.uv = uvs;
return mesh;
}
}
3、效果图:


小知识:

