美文网首页
015. mesh 圆柱体

015. mesh 圆柱体

作者: cmd_ts | 来源:发表于2020-03-01 08:16 被阅读0次

圆柱的生成跟圆差不错。
它是由多个三角体构成的。
这次这个我取巧了,因为涉及算法比较耗时间。
我先做一个10°的三角体,然后克隆35个,把他们拼成一个圆柱


51.png 52.png
GameObject tGO = new GameObject("asd");

MeshFilter mesh_filter = tGO.AddComponent<MeshFilter>();
MeshRenderer mesh_renderer =  tGO.AddComponent<MeshRenderer>();

Mesh mesh = mesh_filter.mesh;
mesh.Clear();
//设置顶点
float radius = 1;   //圆的半径
int segments = 36;  //设置有64个三角形
Vector3[] vertices = new Vector3[6];
Vector3 centerCircle = new Vector3(0,0,0);
vertices[0] = centerCircle;

Vector3 centerCircle1 = new Vector3(0,0,-2);
vertices[3] = centerCircle1;
//Mathf.Deg2Rad 度转弧度:Mathf.Deg2Rad = (PI * 2) / 360
float deltaAngle = Mathf.Deg2Rad * 360f / segments;
float currentAngle = 0;
//设置三角形顶点顺序,顺时针设置
for (int i = 1; i <= 2; i++)
{
    float cosA = Mathf.Cos(currentAngle);
    float sinA = Mathf.Sin(currentAngle);
    vertices[i] = new Vector3(cosA * radius + centerCircle.x, sinA * radius + centerCircle.y, 0);
    currentAngle += deltaAngle;    
    Debug.Log(i);   
    Debug.Log(vertices[i]);    
}

currentAngle = 0;
for (int i = 1; i <= 2; i++)
{
    float cosA = Mathf.Cos(currentAngle);
    float sinA = Mathf.Sin(currentAngle);
    vertices[i+3] = new Vector3(cosA * radius + centerCircle1.x, sinA * radius + centerCircle1.y, -2);
    currentAngle += deltaAngle; 
    Debug.Log(i+3);   
    Debug.Log(vertices[i+3]);              
}

mesh.vertices = vertices;
//设置三角形顶点顺序,顺时针设置
mesh.triangles = new int[]{
    0,2,1,  //上
    4,5,3,  //下

    0,1,3,
    1,4,3,  //左

    1,2,4,
    2,5,4,  //右

    2,0,5,
    0,3,5,  //后
};

//生成一个圆
for(int i =0;i<36;i++)
{
    GameObject asd1 = Instantiate(tGO) as GameObject;
    asd1.transform.rotation = Quaternion.Euler(new Vector3(0, 0, i*10));
}

相关文章

网友评论

      本文标题:015. mesh 圆柱体

      本文链接:https://www.haomeiwen.com/subject/xvbtkhtx.html