ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

unity+Mesh创建

2019-08-01 19:04:30  阅读:351  来源: 互联网

标签:int 创建 Vector3 vertices unity Mesh normals new triangles


参考链接:

Unity3D之Mesh(一)绘制三角形 - 乐学习 - 博客园  https://www.cnblogs.com/JLZT1223/p/6080164.html

Unity3D之Mesh(七)绘制长方体 - 乐学习 - 博客园  https://www.cnblogs.com/JLZT1223/p/6089996.html

 实现效果如图所示:(其实这就是一个gameobject展示出来的效果,如果想完成两万个小立方体的效果,可以拼接这个K02 )

 

unity中新建一个空物体,加上组件MeshRenderer和MeshFilter

新建一个材质球createMesh拖入meshrenderer中

代码如下所示:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof(MeshRenderer), typeof(MeshFilter))]
public class CreateMesh : MonoBehaviour {
    public float Length = 1;              //长方体的长
    public float Width = 1;               //长方体的宽
    public float Heigth = 1;              //长方体的高
    private MeshFilter meshFilter;
    List<Vector3> lstVertices = new List<Vector3>();
    List<int> lstIndices = new List<int>();
    List<Vector3> lstNormals = new List<Vector3>();

    void Start()
    {
        meshFilter = GetComponent<MeshFilter>();
        //旧版本,一个立方体
        //meshFilter.mesh = OnCreateMesh(Length, Width, Heigth);

        ////一个正方体不包含法线
        //meshFilter.mesh = OnCreateMeshSmipleNONormal(Length, Width, Heigth);

        //最完整的方法,10*10*10个立方体mesh新方法包含法线
        OnCreateNewMeshNormal();

    }
    //最完整的方法,10*10*10个立方体mesh新方法包含法线
    private void OnCreateNewMeshNormal()
    {
        
        for (int i = 0; i < 10; i++)
        {
            for (int j = 0; j < 10; j++)
            {
                for (int k = 0; k < 10; k++)
                {
                    OnCreateTriangles(Length, Width, Heigth, new Vector3(i * 2, j * 2, k * 2));

                }
            }
        }
        //负载属性与mesh
        Mesh mesh = new Mesh();
        mesh.vertices = lstVertices.ToArray();
        mesh.triangles = lstIndices.ToArray();
        mesh.normals = lstNormals.ToArray();
        meshFilter.mesh = mesh;
    }

    void  OnCreateTriangles(float length, float width, float heigth, Vector3 trans)
    {                              
        Vector3[] vertices = new Vector3[4 * 6];               //顶点数(每个面4个点,六个面)
        Vector3[] normals = new Vector3[4 * 6];                 //法线 (每个面4个点,六个面)
        vertices[0] = new Vector3(0, 0, 0);                     //前面的左下角的点
        normals[0] = new Vector3(0, 0, -1);
        vertices[1] = new Vector3(0, heigth, 0);                //前面的左上角的点
        normals[1] = new Vector3(0, 0, -1); ;
        vertices[2] = new Vector3(length, 0, 0);                //前面的右下角的点
        normals[2] = new Vector3(0, 0, -1); ;
        vertices[3] = new Vector3(length, heigth, 0);           //前面的右上角的点
        normals[3] = new Vector3(0, 0, -1); ;

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

        vertices[8] = vertices[6];                              //左
        normals[8] = new Vector3(-1, 0, 0);
        vertices[9] = vertices[7];
        normals[9] = new Vector3(-1, 0, 0);
        vertices[10] = vertices[0];
        normals[10] = new Vector3(-1, 0, 0);
        vertices[11] = vertices[1];
        normals[11] = new Vector3(-1, 0, 0);

        vertices[12] = vertices[2];                              //右
        normals[12] = new Vector3(1, 0, 0);
        vertices[13] = vertices[3];
        normals[13] = new Vector3(1, 0, 0);
        vertices[14] = vertices[4];
        normals[14] = new Vector3(1, 0, 0);
        vertices[15] = vertices[5];
        normals[15] = new Vector3(1, 0, 0);

        vertices[16] = vertices[1];                              //上
        normals[16] = new Vector3(0, 1, 0);
        vertices[17] = vertices[7];
        normals[17] = new Vector3(0, 1, 0);
        vertices[18] = vertices[3];
        normals[18] = new Vector3(0, 1, 0);
        vertices[19] = vertices[5];
        normals[19] = new Vector3(0, 1, 0);

        vertices[20] = vertices[2];                              //下
        normals[20] = new Vector3(0, -1, 0);
        vertices[21] = vertices[4];
        normals[21] = new Vector3(0, -1, 0);
        vertices[22] = vertices[0];
        normals[22] = new Vector3(0, -1, 0);
        vertices[23] = vertices[6];
        normals[23] = new Vector3(0, -1, 0);

        int nStartIndex = lstVertices.Count;
        for(int i = 0; i < vertices.Length; i++)
        {
            lstVertices.Add(vertices[i] + trans);
        }
        lstNormals.AddRange(normals);

        //triangles(索引三角形、必须):
        int triangles_cout = 6 * 2 * 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;
        }
       
        //int[] triangles2 = new int[6 * 2 * 3] {1,0,2, 3,1,2,
        //                            3,2,4, 4,5,3,
        //                            6,7,4, 7,5,4,
        //                            7,6,1, 6,0,1,
        //                            7,1,3, 3,5,7,
        //                            0,6,2, 6,4,2};            //索引三角形数组
        for (int i = 0; i < triangles.Length; i++)
        {
            lstIndices.Add(triangles[i] + nStartIndex);
        }
    }
    //没有法线,一个立方体,没有重复的点
    Mesh OnCreateMeshSmipleNONormal(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);           //后面的左上角的点
        int[] triangles2 = new int[6 * 2 * 3] {1,0,2, 3,1,2,
                                     3,2,4, 4,5,3,
                                     6,7,4, 7,5,4,
                                     7,6,1, 6,0,1,
                                     7,1,3, 3,5,7,
                                     0,6,2, 6,4,2};            //索引三角形数组

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

        //负载属性与mesh
        Mesh mesh = new Mesh();
        mesh.vertices = vertices;
        mesh.triangles = triangles2;
        return mesh;
    }
    //最原始,重复点,无法线
    Mesh OnCreateMesh(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;
    }
}

  

 

标签:int,创建,Vector3,vertices,unity,Mesh,normals,new,triangles
来源: https://www.cnblogs.com/WalkingSnail/p/11284576.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有