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

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

 public enum MeshPivot
    {
        /// 
        /// 中心
        /// 
        CENTER,
        /// 
        /// 上
        /// 
        TOP,
        /// 
        /// 下
        /// 
        BOTTOM,
        /// 
        /// 左
        /// 
        LEFT,
        /// 
        /// 右
        /// 
        RIGHT,
        /// 
        /// 前
        /// 
        FRONT,
        /// 
        /// 后
        /// 
        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
        /// 
        /// 轴心的位置
        /// 
        public MeshPivot MeshPivot
        {
            get { return _meshPivot; }
        }

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

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

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

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

        /// 
        /// 生成的mesh数据
        /// 
        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
        /// 
        /// 生成mesh
        /// 
        /// 
        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
        /// 
        /// 根据mesh中心点的位置,获得顶点位置的偏移量
        /// 
        /// 
        protected abstract Vector3 GetVertexOffset();

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

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

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

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

        #endregion

        #region protected static functions

        /// 
        /// 获得圆心切割的边数(按照固定长度)
        /// 
        /// 半径
        /// 弧度的长度
        /// 
        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)