Ejemplo n.º 1
0
    public void Regenerate()
    {
        // Create or clear mesh
        if (mesh == null)
        {
            mesh = new Mesh();

            mesh.name      = "Spiral";
            mesh.hideFlags = HideFlags.DontSave;
        }
        else
        {
            mesh.Clear();
        }

        // Apply mesh to filter
        if (cachedMeshFilter == null)
        {
            cachedMeshFilter = GetComponent <MeshFilter>();
        }

        cachedMeshFilter.sharedMesh = mesh;

        // Invalid segment count?
        if (InvalidSegmentCount == true)
        {
            return;
        }

        // Allocate arrays?
        var vertexCount = SegmentCount * 2 + 2;

        if (positions == null || positions.Length != vertexCount)
        {
            positions = new Vector3[vertexCount];
        }

        if (uvs == null || uvs.Length != vertexCount)
        {
            uvs = new Vector2[vertexCount];
        }

        // Generate indices?
        if (indices == null || indices.Length != SegmentCount * 6)
        {
            indices = new int[SegmentCount * 6];

            for (var i = 0; i < SegmentCount; i++)
            {
                indices[i * 6 + 0] = i * 2 + 0;
                indices[i * 6 + 1] = i * 2 + 1;
                indices[i * 6 + 2] = i * 2 + 2;
                indices[i * 6 + 3] = i * 2 + 3;
                indices[i * 6 + 4] = i * 2 + 2;
                indices[i * 6 + 5] = i * 2 + 1;
            }
        }

        var angle    = InitialAngle;
        var distance = InitialDistance;

        for (var i = 0; i <= SegmentCount; i++)
        {
            var vertex = i * 2;

            positions[vertex + 0] = VA_Helper.SinCos(angle * Mathf.Deg2Rad) * distance;
            positions[vertex + 1] = VA_Helper.SinCos(angle * Mathf.Deg2Rad) * (distance + SegmentThickness);

            uvs[vertex + 0] = Vector2.zero;
            uvs[vertex + 1] = Vector2.one;

            angle    += AngleStep;
            distance += DistanceStep;
        }

        // Update mesh
        mesh.vertices  = positions;
        mesh.triangles = indices;
        mesh.uv        = uvs;

        mesh.RecalculateBounds();
        mesh.RecalculateNormals();
    }