using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Creat_Yuan : MonoBehaviour
{
MeshFilter meshFilter;
MeshRenderer meshRenderer;
MeshCollider meshCollider;
public int n;//个数
public float r;//半径
// Start is called before the first frame update
void Start()
{
meshFilter = gameObject.AddComponent<MeshFilter>();
meshRenderer = gameObject.AddComponent<MeshRenderer>();
meshCollider = gameObject.AddComponent<MeshCollider>();
Mesh mesh = new Mesh();
VertexHelper vh = new VertexHelper();
//计算每个角的弧度
float ang = (2 * Mathf.PI) / n;
//添加圆心
vh.AddVert(Vector3.zero, Color.white, new Vector2(0.5f, 0.5f));
//添加顶点(顶点坐标,顶点颜色,uv坐标)
for (int i = 0; i < n; i++)
{
float x = Mathf.Sin(ang * i) * r;
float y = Mathf.Sin(ang * i) * r;
float uvx = (r + x) / (r + y);
float uvy = (r + y) / (r + r);
vh.AddVert(new Vector3(x, y, 0), Color.white, new Vector2(uvx, uvy)); ;
}
//添加绘制顺序
for (int i = 0; i < n; i++)
{
if (i == 0)
{
vh.AddTriangle(0, n, 1);
}
else
{
vh.AddTriangle(0, i, i + 1);
}
}
//把顶点助手的信息赋值给mesh网格
vh.FillMesh(mesh);
//重新计算法线
mesh.RecalculateNormals();
//把mesh网格赋值给网格过滤器
meshFilter.mesh = mesh;
//给网格碰撞器赋值
}
// Update is called once per frame
void Update()
{
}
}