unity---Mesh网格编程

Posted 格拉格拉

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了unity---Mesh网格编程相关的知识,希望对你有一定的参考价值。

目录

1.创建一个正方体

2.创建一个立方体



unity---Mesh网格编程(二)说了用mesh 创建一个3X3的面

1.创建一个正方体

using UnityEngine;

//挂载到物体时自动添加 MeshFilter 与 MeshRenderer 组件
[RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))]
public class Mesh3 : MonoBehaviour

    private Vector3[] _vertices;
    private int[] _triangles;

    void Start()
    
        Mesh mesh = new Mesh();

        MeshFilter filter = GetComponent<MeshFilter>();
        filter.mesh = mesh;
        mesh.name = "Mesh3";
        GenerateVextex();
        GenerateTriangle();

        mesh.vertices = _vertices;
        mesh.triangles = _triangles;
    

    //正方体顶点数8个
    private void GenerateVextex()
    
        _vertices = new Vector3[8];
        //上面
        _vertices[0] = new Vector3(-0.5f, -0.5f, -0.5f);
        _vertices[1] = new Vector3(0.5f, -0.5f, -0.5f);
        _vertices[2] = new Vector3(-0.5f, -0.5f, 0.5f);
        _vertices[3] = new Vector3(0.5f, -0.5f, 0.5f);
        //下面
        _vertices[4] = new Vector3(-0.5f, 0.5f, -0.5f);
        _vertices[5] = new Vector3(0.5f, 0.5f, -0.5f);
        _vertices[6] = new Vector3(-0.5f, 0.5f, 0.5f);
        _vertices[7] = new Vector3(0.5f, 0.5f, 0.5f);
    

    //正方体6个面的顶点下标,注意法线朝向
    private void GenerateTriangle()
    
        _triangles = new int[36];
        //底面三角1
        _triangles[0] = 0;
        _triangles[1] = 1;
        _triangles[2] = 2;
        //底面三角2
        _triangles[3] = 1;
        _triangles[4] = 3;
        _triangles[5] = 2;
        //前面三角1
        _triangles[6] = 0;
        _triangles[7] = 4;
        _triangles[8] = 5;
        //前面三角2
        _triangles[9] = 5;
        _triangles[10] = 1;
        _triangles[11] = 0;
        //后面三角1
        _triangles[12] = 3;
        _triangles[13] = 7;
        _triangles[14] = 6;
        //后面三角2
        _triangles[15] = 6;
        _triangles[16] = 3;
        _triangles[17] = 2;
        //左面三角1
        _triangles[18] = 6;
        _triangles[19] = 4;
        _triangles[20] = 0;
        //左面三角2
        _triangles[21] = 4;
        _triangles[22] = 0;
        _triangles[23] = 2;
        //右面三角1
        _triangles[24] = 1;
        _triangles[25] = 5;
        _triangles[26] = 7;
        //右面三角2
        _triangles[27] = 7;
        _triangles[28] = 3;
        _triangles[29] = 1;
        //上面三角1
        _triangles[30] = 6;
        _triangles[31] = 7;
        _triangles[32] = 4;
        //上面三角2
        _triangles[33] = 7;
        _triangles[34] = 5;
        _triangles[35] = 4;
    


2.创建一个立方体

using System.Collections;
using UnityEngine;

[RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))]
public class Mesh3 : MonoBehaviour

    public int xGridCount, yGridCount, zGridCount;
    private Vector3[] _vertices;
    private int[] _triangles;
    private int _t = 0, _v = 0;

    void Start()
    
        Mesh mesh = new Mesh();
        MeshFilter filter = GetComponent<MeshFilter>();
        filter.mesh = mesh;
        mesh.name = "Mesh3";

        StartCoroutine(GenerateMesh(mesh));
    

    private IEnumerator GenerateMesh(Mesh mesh)
    
        yield return StartCoroutine(GenerateVextex());
        mesh.vertices = _vertices;
        yield return StartCoroutine(GenerateTriange(mesh));
    

    //计算顶点数
    private int GetVextexCount()
    
        //公共-顶点数
        int connerVertexCount = 8;
        //边--顶点数
        int edgeVertexCount = (xGridCount + yGridCount + zGridCount - 3) * 4;
        //内部顶点数
        int faceVertexCount = (xGridCount - 1) * (yGridCount - 1) * 2
                              + (zGridCount - 1) * (yGridCount - 1) * 2
                              + (xGridCount - 1) * (zGridCount - 1) * 2;

        return connerVertexCount + edgeVertexCount + faceVertexCount;
    

    //生成顶点
    private IEnumerator GenerateVextex()
    
        _vertices = new Vector3[GetVextexCount()];
        int index = 0;

        //循环生成左右前后4个面的顶点
        for (int y = 0; y < yGridCount + 1; y++)
        
            //底部正方形-第一条边的顶点
            for (int x = 0; x < xGridCount + 1; x++, index++)
            
                _vertices[index] = new Vector3(x, y, 0);
                yield return null;
            
            //底部正方形-第二条边的顶点
            for (int z = 1; z < zGridCount + 1; z++, index++)
            
                _vertices[index] = new Vector3(xGridCount, y, z);
                yield return null;
            
            //底部正方形-第三条边的顶点
            for (int x = xGridCount - 1; x >= 0; x--, index++)
            
                _vertices[index] = new Vector3(x, y, zGridCount);
                yield return null;
            
            //底部正方形-第四条边的顶点
            for (int z = zGridCount - 1; z > 0; z--, index++)
            
                _vertices[index] = new Vector3(0, y, z);
                yield return null;
            
        
        //生成上面的顶点
        for (int z = 1; z < zGridCount; z++)
        
            for (int x = 1; x < xGridCount; x++, index++)
            
                _vertices[index] = new Vector3(x, yGridCount, z);
                yield return null;
            
        
        //生成下面的顶点
        for (int z = 1; z < zGridCount; z++)
        
            for (int x = 1; x < xGridCount; x++, index++)
            
                _vertices[index] = new Vector3(x, 0, z);
                yield return null;
            
        
    

    //计算顶点序列下标数
    private int GetTriangeCount()
    
        return xGridCount * yGridCount * 4 + xGridCount * zGridCount * 4 + yGridCount * zGridCount * 4;
    

    //顶点绘制三角面序列
    private IEnumerator GenerateTriange(Mesh mesh)
    
        _triangles = new int[GetTriangeCount() * 3];
        int circleVextexCount = 2 * xGridCount + 2 * zGridCount;

        yield return StartCoroutine(GenerateSide(mesh, circleVextexCount));

        yield return StartCoroutine(GenerateTop(mesh, circleVextexCount));

        yield return StartCoroutine(GenerateBottom(mesh, circleVextexCount));
    

    private IEnumerator GenerateSide(Mesh mesh, int circleVextexCount)
    
        for (int y = 0; y < yGridCount; y++)
        
            for (int i = 0; i < circleVextexCount; i++, _v++, _t += 6)
            
                SetQuad(_triangles, _t, _v, _v + 1, _v + circleVextexCount, _v + 1 + circleVextexCount, circleVextexCount);
                mesh.triangles = _triangles;
                yield return null;
            
        
    

    private IEnumerator GenerateTop(Mesh mesh, int circleVextexCount)
    
        //第一行前三个面
        for (int x = 0; x < xGridCount - 1; x++, _v++, _t += 6)
        
            SetQuad(_triangles, _t, _v, _v + 1, _v + circleVextexCount - 1, _v + circleVextexCount);
            mesh.triangles = _triangles;
            yield return null;
        

        //第一行第四个面
        SetQuad(_triangles, _t, _v, _v + 1, _v + circleVextexCount - 1, _v + 2);
        mesh.triangles = _triangles;
        _t += 6;
        yield return null;

        int vMin = circleVextexCount * (yGridCount + 1) - 1;
        int vMid = vMin + 1;
        int vMax = _v + 2;
        //中间行所有面
        for (int z = 0; z < zGridCount - 2; z++, vMin--, vMid++, vMax++)
        
            //第一个面
            SetQuad(_triangles, _t, vMin, vMid, vMin - 1, vMid + xGridCount);
            mesh.triangles = _triangles;
            _t += 6;
            yield return null;

            //中间面片
            for (int i = 0; i < xGridCount - 2; i++, _t += 6, vMid++)
            
                SetQuad(_triangles, _t, vMid, vMid + 1, vMid + xGridCount - 1, vMid + xGridCount);
                mesh.triangles = _triangles;
                yield return null;
            

            //最后一个面
            SetQuad(_triangles, _t, vMid, vMax, vMid + xGridCount - 1, vMax + 1);
            mesh.triangles = _triangles;
            _t += 6;
            yield return null;
        

        int vTop = vMin - 2;
        //第一个面
        SetQuad(_triangles, _t, vMin, vMid, vMin - 1, vTop);
        mesh.triangles = _triangles;
        _t += 6;
        yield return null;


        for (int i = 0; i < xGridCount - 2; i++, vMid++, vTop--)
        
            //中间面
            SetQuad(_triangles, _t, vMid, vMid + 1, vTop, vTop - 1);
            mesh.triangles = _triangles;
            _t += 6;
            yield return null;
        

        //最后一行最后一个面
        SetQuad(_triangles, _t, vMid, vTop - 2, vTop, vTop - 1);
        mesh.triangles = _triangles;
        _t += 6;
        yield return null;
    

    private IEnumerator GenerateBottom(Mesh mesh, int circleVextexCount)
    
        int vMin = circleVextexCount - 1;
        int vMid = _vertices.Length - (xGridCount - 1) * (zGridCount - 1);

        //第一行,第一面
        SetQuad(_triangles, _t, vMin, vMid, 0, 1);
        _t += 6;
        mesh.triangles = _triangles;
        yield return null;

        int vMax = 1;
        //第一行,中间面
        for (int i = 0; i < xGridCount - 2; i++, _t += 6, vMax++, vMid++)
        
            SetQuad(_triangles, _t, vMid, vMid + 1, vMax, vMax + 1);
            mesh.triangles = _triangles;
            yield return null;
        

        //第一行,最后面
        SetQuad(_triangles, _t, vMid, vMax + 2, vMax, vMax + 1);
        _t += 6;
        mesh.triangles = _triangles;
        yield return null;

        vMid++;
        vMax += 2;

        for (int z = 0; z < zGridCount - 2; z++, vMin--, vMid++, vMax++)
        
            //第一面
            SetQuad(_triangles, _t, vMin - 1, vMid, vMin, vMid - xGridCount + 1);
            _t += 6;
            mesh.triangles = _triangles;
            yield return null;

            //中间面
            for (int i = 0; i < xGridCount - 2; i++, _t += 6, vMid++)
            
                SetQuad(_triangles, _t, vMid, vMid + 1, vMid - xGridCount + 1, vMid - xGridCount + 2);
                mesh.triangles = _triangles;
                yield return null;
            

            //最后面
            SetQuad(_triangles, _t, vMid, vMax + 1, vMid - xGridCount + 1, vMax);
            _t += 6;
            mesh.triangles = _triangles;
            yield return null;
        

        vMid = vMid - xGridCount + 1;
        //最后行,第一面
        SetQuad(_triangles, _t, vMin - 1, vMin - 2, vMin, vMid);
        _t += 6;
        mesh.triangles = _triangles;
        yield return null;

        int vBottom = vMin - 2;
        //最后行,中间面
        for (int i = 0; i < xGridCount - 2; i++, _t += 6, vBottom--, vMid++)
        
            SetQuad(_triangles, _t, vBottom, vBottom - 1, vMid, vMid + 1);
            mesh.triangles = _triangles;
            yield return null;
        

        //最后行,最后面
        SetQuad(_triangles, _t, vBottom, vBottom - 1, vMid, vBottom - 2);
        _t += 6;
        mesh.triangles = _triangles;
        yield return null;
    

    private void SetQuad(int[] triangles, int i, int v00, int v10, int v01, int v11, int circleVextexCount)
    
        v10 = (v00 / circleVextexCount) * circleVextexCount + v10 % circleVextexCount;
        v11 = (v01 / circleVextexCount) * circleVextexCount + v11 % circleVextexCount;

        SetQuad(triangles, i, v00, v10, v01, v11);
    

    //每个面片 6个顶点下标序列
    private void SetQuad(int[] triangles, int i, int v00, int v10, int v01, int v11)
    
        //第1个三角面
        triangles[i] = v00;
        triangles[i + 1] = v01;
        triangles[i + 2] = v10;
        //第2个三角面
        triangles[i + 3] = v01;
        triangles[i + 4] = v11;
        triangles[i + 5] = v10;
    

以上是关于unity---Mesh网格编程的主要内容,如果未能解决你的问题,请参考以下文章

unity---Mesh网格编程

unity---Mesh网格编程

unity---Mesh网格编程

unity---Mesh网格编程

unity---Mesh网格编程

unity---Mesh网格编程