关于unity:四Unity-生成几种常用模型meshCylinderShape圆柱体

46次阅读

共计 3542 个字符,预计需要花费 9 分钟才能阅读完成。

圆柱体的顶点有顶面、底面和侧面三局部。
1、取得顶点的数据汇合

protected override Vector3[] GetVertices()
        {
            int curIndex = 0;// 以后索引
            int arrayLen = (_circularSideCount + 1) * 2 + _circularSideCount * 2 + 2;// 数组的长度
            var vertices = new Vector3[arrayLen];
            // 底面
            vertices[curIndex++] = new Vector3(0, -_height * 0.5f, 0) - _verticeOffset; ;// 圆心
            for (int i = 0; i < _circularSideCount; i++)
            {
                var rad = i * 1.0f / _circularSideCount * Mathf.PI * 2;// 弧度
                var cos = Mathf.Cos(rad) * _radius;
                var sin = Mathf.Sin(rad) * _radius;
                vertices[curIndex++] = new Vector3(cos, -_height * 0.5f, sin) - _verticeOffset;
            }
            // 顶面
            vertices[curIndex++] = new Vector3(0, _height * 0.5f, 0) - _verticeOffset; ;// 圆心
            for (int i = 0; i < _circularSideCount; i++)
            {
                var rad = i * 1.0f / _circularSideCount * Mathf.PI * 2;// 弧度
                var cos = Mathf.Cos(rad) * _radius;
                var sin = Mathf.Sin(rad) * _radius;
                vertices[curIndex++] = new Vector3(cos, _height * 0.5f, sin) - _verticeOffset;
            }
            // 侧面
            int sideStartIndex = curIndex;
            for (int i = 0; i < _circularSideCount; i++)
            {
                var rad = i * 1.0f / _circularSideCount * Mathf.PI * 2;// 弧度
                var cos = Mathf.Cos(rad) * _radius;
                var sin = Mathf.Sin(rad) * _radius;
                vertices[curIndex++] = new Vector3(cos, -_height * 0.5f, sin) - _verticeOffset;
                vertices[curIndex++] = new Vector3(cos, _height * 0.5f, sin) - _verticeOffset;
            }
            vertices[curIndex++] = vertices[sideStartIndex];
            vertices[curIndex] = vertices[sideStartIndex + 1];
            return vertices;
        }

2、取得法线方向的数据汇合

protected override Vector3[] GetNormals()
        {
            int curIndex = 0;
            int arrayLen = (_circularSideCount + 1) * 2 + _circularSideCount * 2 + 2;
            var normals = new Vector3[arrayLen];
            // 底面
            for (int i = 0; i <= _circularSideCount; i++)
            {normals[curIndex++] = Vector3.down;
            }
            // 顶面
            for (int i = 0; i <= _circularSideCount; i++)
            {normals[curIndex++] = Vector3.up;
            }
            // 侧面
            for (int i = 0; i <= _circularSideCount; i++)
            {
                float rad = i * 1.0f / _circularSideCount * Mathf.PI * 2;
                var cos = Mathf.Cos(rad);
                var sin = Mathf.Sin(rad);
                normals[curIndex++] = new Vector3(cos, 0, sin);
                normals[curIndex++] = new Vector3(cos, 0, sin);
            }
            return normals;
        }

3、取得三角面顶点的索引

 protected override int[] GetTriangles()
        {
            int curIndex = 0;
            int arrayLen = _circularSideCount * 3 + _circularSideCount * 3 + _circularSideCount * 3 * 2;
            var triangles = new int[arrayLen];
            // 底面
            for (int i = 1; i < _circularSideCount; i++)
            {triangles[curIndex++] = 0;
                triangles[curIndex++] = i;
                triangles[curIndex++] = i + 1;
            }
            triangles[curIndex++] = 0;
            triangles[curIndex++] = _circularSideCount;
            triangles[curIndex++] = 1;
            // 顶面
            for (int i = _circularSideCount + 2; i < _circularSideCount * 2 + 1; i++)
            {triangles[curIndex++] = _circularSideCount + 1;
                triangles[curIndex++] = i + 1;
                triangles[curIndex++] = i;
            }
            triangles[curIndex++] = _circularSideCount + 1;
            triangles[curIndex++] = _circularSideCount + 2;
            triangles[curIndex++] = _circularSideCount * 2 + 1;
            // 侧面
            int startIndex = _circularSideCount * 2 + 2;
            for (int i = 0; i < _circularSideCount; i++)
            {triangles[curIndex++] = startIndex;
                triangles[curIndex++] = startIndex + 1;
                triangles[curIndex++] = startIndex + 3;

                triangles[curIndex++] = startIndex + 3;
                triangles[curIndex++] = startIndex + 2;
                triangles[curIndex++] = startIndex;

                startIndex += 2;
            }
            return triangles;
        }

4、取得 UV 坐标的数据汇合

 protected override Vector2[] GetUVs()
        {
            int curIndex = 0;
            int arrayLen = (_circularSideCount + 1) * 2 + (_circularSideCount + 1) * 2;
            var uvs = new Vector2[arrayLen];
            // 底面
            uvs[curIndex++] = new Vector2(0.5f, 0.5f);
            for (int i = 0; i < _circularSideCount; i++)
            {
                var rad = i * 1.0f / _circularSideCount * Mathf.PI * 2;
                var cos = Mathf.Cos(rad);
                var sin = Mathf.Sin(rad);
                uvs[curIndex++] = new Vector2(cos, sin) * 0.5f + new Vector2(0.5f, 0.5f);
            }
            // 顶面
            uvs[curIndex++] = new Vector2(0.5f, 0.5f);
            for (int i = 0; i < _circularSideCount; i++)
            {
                var rad = i * 1.0f / _circularSideCount * Mathf.PI * 2;
                var cos = Mathf.Cos(rad);
                var sin = Mathf.Sin(rad);
                uvs[curIndex++] = new Vector2(cos, sin) * 0.5f + new Vector2(0.5f, 0.5f);
            }
            // 侧面
            float value = 1.0f / _circularSideCount;
            for (int i = 0; i <= _circularSideCount; i++)
            {uvs[curIndex++] = new Vector2(i * value, 0);
                uvs[curIndex++] = new Vector2(i * value, 1);
            }
            return uvs;
        }

整体成果如下:

gitee 仓库地址:https://gitee.com/pangdudu010…

正文完
 0