一Unity 生成几种常用模型mesh-----基类

Posted 聪明的墨菲特

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了一Unity 生成几种常用模型mesh-----基类相关的知识,希望对你有一定的参考价值。

Unity中mesh主要通过Vertexs(顶点)、Normals(法线)、Triangles(三角面)、UVs(uv坐标)这四个部分数据生成。
一、Mesh的轴心位置的类型
通过这个类型判断生成坐标的偏移量

 public enum MeshPivot
    {
        /// <summary>
        /// 中心
        /// </summary>
        CENTER,
        /// <summary>
        /// 上
        /// </summary>
        TOP,
        /// <summary>
        /// 下
        /// </summary>
        BOTTOM,
        /// <summary>
        /// 左
        /// </summary>
        LEFT,
        /// <summary>
        /// 右
        /// </summary>
        RIGHT,
        /// <summary>
        /// 前
        /// </summary>
        FRONT,
        /// <summary>
        /// 后
        /// </summary>
        BACK,
    }

二、形状父类
设置成抽象类,需要必须要继承这个类并实现相关抽象方法

 public abstract class Shape
    {
        #region protected members
        protected MeshPivot _meshPivot = MeshPivot.CENTER;//轴心的位置
        protected string _meshName;

        protected Vector3 _verticeOffset;//顶点的偏移量
        protected Vector3[] _vertices;//顶点数据
        protected Vector3[] _normals;//法线数据
        protected int[] _triangles;//三角面顶点的索引数据
        protected Vector2[] _uvs;//uv坐标数据

        protected Mesh _shapeMesh;//保存生成的mesh
        #endregion

        #region public properties
        /// <summary>
        /// 轴心的位置
        /// </summary>
        public MeshPivot MeshPivot
        {
            get { return _meshPivot; }
        }

        /// <summary>
        /// 顶点数据
        /// </summary>
        public Vector3[] Vertices
        {
            get
            {
                if (_vertices == null) _vertices = GetVertices();
                return _vertices;
            }
        }

        /// <summary>
        /// 法线数据
        /// </summary>
        public Vector3[] Normals
        {
            get
            {
                if (_normals == null) _normals = GetNormals();
                return _normals;
            }
        }

        /// <summary>
        /// 三角面顶点的索引数据
        /// </summary>
        public int[] Triangles
        {
            get
            {
                if (_triangles == null) _triangles = GetTriangles();
                return _triangles;
            }
        }

        /// <summary>
        /// uv坐标数据
        /// </summary>
        public Vector2[] UVs
        {
            get
            {
                if (_uvs == null) _uvs = GetUVs();
                return _uvs;
            }
        }

        /// <summary>
        /// 生成的mesh数据
        /// </summary>
        public Mesh ShapeMesh
        {
            get
            {
                if (_shapeMesh == null) _shapeMesh = GenerateMesh();
                return _shapeMesh;
            }
        }
        #endregion

        #region ctor
        public Shape(MeshPivot meshPivot, string meshName)
        {
            _meshPivot = meshPivot;
            _meshName = meshName;
        }
        #endregion

        #region protected functions
        /// <summary>
        /// 生成mesh
        /// </summary>
        /// <returns></returns>
        protected Mesh GenerateMesh()
        {
            var mesh = new Mesh();
            mesh.name = _meshName;

            _vertices = GetVertices();
            _normals = GetNormals();
            _triangles = GetTriangles();
            _uvs = GetUVs();

            mesh.vertices = _vertices;
            mesh.normals = _normals;
            mesh.triangles = _triangles;
            mesh.uv = _uvs;
            mesh.RecalculateTangents();
            mesh.RecalculateBounds();
            mesh.Optimize();
            return mesh;
        }
        #endregion

        #region abstract
        /// <summary>
        /// 根据mesh中心点的位置,获得顶点位置的偏移量
        /// </summary>
        /// <returns></returns>
        protected abstract Vector3 GetVertexOffset();

        /// <summary>
        /// 获得顶点的数据集合
        /// </summary>
        /// <returns></returns>
        protected abstract Vector3[] GetVertices();

        /// <summary>
        /// 获得法线方向的数据集合
        /// </summary>
        /// <returns></returns>
        protected abstract Vector3[] GetNormals();

        /// <summary>
        /// 获得三角面顶点的索引
        /// </summary>
        /// <returns></returns>
        protected abstract int[] GetTriangles();

        /// <summary>
        /// 获得UV坐标的数据集合
        /// </summary>
        /// <returns></returns>
        protected abstract Vector2[] GetUVs();

        #endregion

        #region protected static functions

        /// <summary>
        /// 获得圆心切割的边数(按照固定长度)
        /// </summary>
        /// <param name="radius">半径</param>
        /// <param name="arcLen">弧度的长度</param>
        /// <returns></returns>
        protected static int GetCircularSideCount(float radius, float arcLen = 0.1f)
        {
            const int minSideCount = 3;
            const int maxSideCount = 3000;

            int sideCount = Mathf.RoundToInt(Mathf.PI * 2 * radius / arcLen);
            sideCount = Mathf.Min(Mathf.Max(minSideCount, sideCount), maxSideCount);
            Debug.Log("sideCount:" + sideCount);
            return sideCount;
        }

        #endregion

    }

以上是关于一Unity 生成几种常用模型mesh-----基类的主要内容,如果未能解决你的问题,请参考以下文章

Unity核心对象模型

Unity场景优化工具:Mesh Baker 基础教程(贴图篇)

一个模型(Mesh)导入unity后变成两个部分,变成XX__MeshPart0和XX__MeshPart1

Unity---------Mesh理解

Unity自动生成阻挡Collider的GameObject工具

Unity3D网格 Mesh ( 网格概念 | 网格示例 | Unity 中 3D 物体渲染模式 | 着色模式 | 线框模式 | 线框着色模式 )