unity+Mesh創建


參考鏈接:

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;
    }
}

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM