private void GenerateRing(Quaternion[] rotations)
    {
        var positions       = new Vector3[rotations.Length * (meshSegments * 2 + 2)];
        var indices         = new int[rotations.Length * meshSegments * 6];
        var uv0             = new Vector2[rotations.Length * (meshSegments * 2 + 2)];
        var vStep           = 1.0f / meshSegments;
        var angleStep       = (Mathf.PI * 2.0f) / meshSegments;
        var meshRadiusOuter = (meshRadius + meshHeight) * SGT_Helper.PolygonOuterBoundScale(angleStep);

        // Write index data
        {
            var indicesIndex = 0;
            var vertexIndex  = 0;

            for (var r = 0; r < rotations.Length; r++)
            {
                for (var i = 0; i < meshSegments; i++)
                {
                    var vertexIndexNext = vertexIndex + 2;

                    // Index data
                    indices[indicesIndex + 0] = vertexIndex + 0;
                    indices[indicesIndex + 1] = vertexIndex + 1;
                    indices[indicesIndex + 2] = vertexIndexNext + 1;
                    indices[indicesIndex + 3] = vertexIndex + 0;
                    indices[indicesIndex + 4] = vertexIndexNext + 1;
                    indices[indicesIndex + 5] = vertexIndexNext + 0;

                    indicesIndex += 6;
                    vertexIndex  += 2;
                }

                vertexIndex += 2;
            }
        }

        // Write vertex data
        {
            var vertexIndex = 0;

            for (var r = 0; r < rotations.Length; r++)
            {
                var rot = rotations[r];

                for (var i = 0; i < meshSegments + 1; i++)
                {
                    // Calculate stuff
                    var angle     = angleStep * i;
                    var direction = new Vector3(Mathf.Sin(angle), Mathf.Cos(angle), 0.0f);

                    // Write vertex data
                    positions[vertexIndex + 0] = rot * (direction * meshRadius);
                    positions[vertexIndex + 1] = rot * (direction * meshRadiusOuter);

                    uv0[vertexIndex + 0] = new Vector2(vStep * i, 0.0f);
                    uv0[vertexIndex + 1] = new Vector2(vStep * i, 1.0f);

                    vertexIndex += 2;
                }
            }
        }

        generatedMesh           = new Mesh();
        generatedMesh.vertices  = positions;
        generatedMesh.triangles = indices;
        generatedMesh.uv        = uv0;
        generatedMesh.RecalculateBounds();
        generatedMesh.RecalculateNormals();
    }
Exemple #2
0
    private void GenerateSliced()
    {
        var positions       = new Vector3[ringSegments * 2 + 2];
        var indices         = new int[ringSegments * 6];
        var uv0             = new Vector2[ringSegments * 2 + 2];
        var normals         = new Vector3[ringSegments * 2 + 2];
        var vStep           = (float)ringTextureRepeat / ringSegments;
        var angleStep       = ((Mathf.PI * 2.0f) / ringSlices) / ringSegments;
        var ringRadiusInner = RingRadiusInner;
        var ringRadiusOuter = RingRadiusOuter * SGT_Helper.PolygonOuterBoundScale(angleStep);

        for (var i = 0; i < ringSegments; i++)
        {
            var indicesIndex    = i * 6;
            var vertexIndex     = i * 2;
            var vertexIndexNext = i * 2 + 2;

            // Index data
            indices[indicesIndex + 0] = vertexIndex + 0;
            indices[indicesIndex + 1] = vertexIndex + 1;
            indices[indicesIndex + 2] = vertexIndexNext + 1;
            indices[indicesIndex + 3] = vertexIndex + 0;
            indices[indicesIndex + 4] = vertexIndexNext + 1;
            indices[indicesIndex + 5] = vertexIndexNext + 0;
        }

        for (var i = 0; i < ringSegments + 1; i++)
        {
            var vertexIndex = i * 2;

            // Calculate stuff
            var angle     = angleStep * (float)i;
            var direction = new Vector3(Mathf.Sin(angle), 0.0f, Mathf.Cos(angle));

            // Write vertex data
            positions[vertexIndex + 0] = direction * ringRadiusInner;
            positions[vertexIndex + 1] = direction * ringRadiusOuter;

            normals[vertexIndex + 0] = Vector3.up;
            normals[vertexIndex + 1] = Vector3.up;

            uv0[vertexIndex + 0] = new Vector2(0.0f, vStep * i);
            uv0[vertexIndex + 1] = new Vector2(1.0f, vStep * i);
        }

        generatedMesh           = new Mesh();
        generatedMesh.name      = "Sliced Ring Mesh";
        generatedMesh.vertices  = positions;
        generatedMesh.triangles = indices;
        generatedMesh.normals   = normals;
        generatedMesh.uv        = uv0;
        generatedMesh.RecalculateBounds();

        if (ringSlices > 1)
        {
            generatedRotations = new Quaternion[ringSlices];

            var sliceAngleStep = 360.0f / (float)ringSlices;

            for (var i = 0; i < ringSlices; i++)
            {
                var angle = sliceAngleStep * (float)i;

                generatedRotations[i] = Quaternion.Euler(0.0f, angle, 0.0f);
            }
        }
    }