转 mesh 生成模型

立方体

https://www.cnblogs.com/JLZT1223/category/911460.html   不错的链接mesh

using UnityEngine;

[RequireComponent(typeof(MeshRenderer), typeof(MeshFilter))]
public class cube_mesh : MonoBehaviour
{
    public float Length = 5;              //长方体的长
    public float Width = 6;               //长方体的宽
    public float Heigth = 7;              //长方体的高
    private MeshFilter meshFilter;

    void Start()
    {
        meshFilter = GetComponent<MeshFilter>();
        meshFilter.mesh = CreateMesh(Length, Width, Heigth);
    }

    Mesh CreateMesh(float length, float width, float heigth)
    {

        //vertices(顶点、必须):
        int vertices_count = 4*6;                                 //顶点数(每个面4个点,六个面)
        Vector3[] vertices = new Vector3[vertices_count];
        vertices[0] = new Vector3(0, 0, 0);                     //前面的左下角的点
        vertices[1] = new Vector3(0, heigth, 0);                //前面的左上角的点
        vertices[2] = new Vector3(length, 0, 0);                //前面的右下角的点
        vertices[3] = new Vector3(length, heigth, 0);           //前面的右上角的点

        vertices[4] = new Vector3(length, 0, width);           //后面的右下角的点
        vertices[5] = new Vector3(length, heigth, width);      //后面的右上角的点
        vertices[6] = new Vector3(0, 0, width);                //后面的左下角的点
        vertices[7] = new Vector3(0, heigth, width);           //后面的左上角的点

        vertices[8] = vertices[6];                              //左
        vertices[9] = vertices[7];
        vertices[10] = vertices[0];
        vertices[11] = vertices[1];

        vertices[12] = vertices[2];                              //右
        vertices[13] = vertices[3];
        vertices[14] = vertices[4];
        vertices[15] = vertices[5];

        vertices[16] = vertices[1];                              //上
        vertices[17] = vertices[7];
        vertices[18] = vertices[3];
        vertices[19] = vertices[5];

        vertices[20] = vertices[2];                              //下
        vertices[21] = vertices[4];
        vertices[22] = vertices[0];
        vertices[23] = vertices[6];


        //triangles(索引三角形、必须):
        int 分割三角形数 = 6 * 2;
        int triangles_cout = 分割三角形数 * 3;                  //索引三角形的索引点个数
        int[] triangles = new int [triangles_cout];            //索引三角形数组
        for(int i=0,vi=0;i< triangles_cout;i+=6,vi+=4)
        {
            triangles[i] = vi;
            triangles[i+1] = vi+1;
            triangles[i+2] = vi+2;

            triangles[i+3] = vi+3;
            triangles[i+4] = vi+2;
            triangles[i+5] = vi+1;

        }

        //uv:
        //.........

        //负载属性与mesh
        Mesh mesh = new Mesh();
        mesh.vertices = vertices;
        mesh.triangles = triangles;
        return mesh;
    }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值