乐趣区

关于unity:二Unity-生成几种常用模型meshPlaneShape

面板 Mesh 次要由四个点组成,这里生成的是双面 Plane,所以须要四个点的坐标。

顶面顶点的程序如图,以 p0 点和 p2 点为对角线切割成两个三角面
代码如下

 public class PlaneShape : Shape
    {
        private static readonly string _planeMeshName = "Plane Mesh";//mesh 的名称

        #region private members
        private float _xSize, _zSize;
        #endregion

        #region ctor
        public PlaneShape(float xSize,float zSize,MeshPivot meshPivot = MeshPivot.CENTER) : base(meshPivot, _planeMeshName)
        {
            _xSize = xSize;
            _zSize = zSize;

            _verticeOffset = GetVertexOffset();}
        #endregion

        #region override functions
        /// <summary>
        /// 依据 mesh 中心点的地位,取得顶点地位的偏移量
        /// </summary>
        /// <returns></returns>
        protected override Vector3 GetVertexOffset()
        {
            Vector3 offset;
            switch (_meshPivot)
            {
                case MeshPivot.CENTER:
                case MeshPivot.TOP:
                case MeshPivot.BOTTOM:
                    offset = Vector3.zero;
                    break;
                case MeshPivot.LEFT:
                    offset = new Vector3(-_xSize, 0, 0) * 0.5f;
                    break;
                case MeshPivot.RIGHT:
                    offset = new Vector3(_xSize, 0, 0) * 0.5f;
                    break;
                case MeshPivot.FRONT:
                    offset = new Vector3(0, 0, _zSize) * 0.5f;
                    break;
                case MeshPivot.BACK:
                    offset = new Vector3(0, 0, -_zSize) * 0.5f;
                    break;
                default:
                    offset = Vector3.zero;
                    break;
            }
            return offset;
        }

        /// <summary>
        /// 取得顶点的数据汇合
        /// </summary>
        /// <returns></returns>
        protected override Vector3[] GetVertices()
        {var p0 = new Vector3(-_xSize, 0, -_zSize) * 0.5f - _verticeOffset;// 左下
            var p1 = new Vector3(-_xSize, 0, _zSize) * 0.5f - _verticeOffset;// 左上
            var p2 = new Vector3(_xSize, 0, _zSize) * 0.5f - _verticeOffset;// 右上
            var p3 = new Vector3(_xSize, 0, -_zSize) * 0.5f - _verticeOffset;// 右下

            return new Vector3[]
            {
                p0,p1,p2,p3,
                p3,p2,p1,p0
            };
        }

        /// <summary>
        /// 取得法线方向的数据汇合
        /// </summary>
        /// <returns></returns>
        protected override Vector3[] GetNormals()
        {return new Vector3[]
            {
                Vector3.up,Vector3.up,Vector3.up,Vector3.up,
                Vector3.down,Vector3.down,Vector3.down,Vector3.down
            };
        }

        /// <summary>
        ///  取得三角面顶点的索引
        /// </summary>
        /// <returns></returns>
        protected override int[] GetTriangles()
        {return new int[]
            {
                0,1,2,2,3,0,
                4,5,6,6,7,4
            };
        }

        /// <summary>
        /// 取得 UV 坐标的数据汇合
        /// </summary>
        /// <returns></returns>
        protected override Vector2[] GetUVs()
        {return new Vector2[]
            {new Vector2(0,0),new Vector2(0,1),new Vector2(1,1),new Vector2(1,0),
                new Vector2(0,0),new Vector2(0,1),new Vector2(1,1),new Vector2(1,0),
            };
        }
        #endregion
    }
退出移动版