Beispiel #1
0
        public override MeshData GetData()
        {
            if (roundings.Length != resolution)
            {
                int[] arr = new int[resolution];
                for (int i = 0; i < resolution && i < roundings.Length; i++)
                {
                    arr[i] = roundings[i];
                }
                roundings = arr;
            }

            if (roundingsDistances.Length != resolution)
            {
                float[] arr = new float[resolution];
                for (int i = 0; i < resolution && i < roundingsDistances.Length; i++)
                {
                    arr[i] = roundingsDistances[i];
                }
                roundingsDistances = arr;
            }
            bool rounded = false;

            uniform = true;
            // This is why using rounding for at least one corner adds redundant vertices.
            int segments = resolution * 2;

            for (int i = 0; i < roundings.Length; i++)
            {
                roundings[i] = Mathf.RoundToInt(1 + (roundingsDistances[i] * 20));
                if (roundings[i] > 1)
                {
                    uniform   = false;
                    rounded   = true;
                    segments += 2;
                }
            }

            Vector3[] vertices          = new Vector3[segments + 1];
            int[]     indices           = new int[segments * 3];
            int[]     borderVertices    = new int[segments];
            int[]     roundableVertices = new int[resolution];

            vertices[0] = Vector3.zero;

            int index         = 0;
            int a             = 0;
            int furtherCorner = 1;
            int closerCorner  = furtherCorner + 1;

            if (roundings[0] > 1)
            {
                if (roundType)
                {
                    furtherCorner = 1;
                }
                else
                {
                    furtherCorner = 2;
                }
                closerCorner = furtherCorner + 2;
            }
            for (int i = 1; i < vertices.Length; i++)
            {
                if (i == closerCorner)
                {
                    vertices[i] = MakeVertex(2 * a - 1);
                }
                else if (i == furtherCorner && a < roundings.Length)
                {
                    vertices[i] = MakeVertex(2 * a);

                    closerCorner = i + (roundings[a] > 1 ? 2 : 1);
                    if (roundType)
                    {
                        roundableVertices[a] = closerCorner;
                    }
                    else
                    {
                        roundableVertices[a] = i;
                    }

                    a++;
                    int nex = a < roundings.Length ? a : 0;

                    if (roundings[a - 1] > 1 && roundings[nex] > 1)
                    {
                        furtherCorner += 4;
                    }
                    else if (roundings[a - 1] == 1 && roundings[nex] == 1)
                    {
                        furtherCorner += 2;
                    }
                    else
                    {
                        if (roundType)
                        {
                            furtherCorner += roundings[a - 1] > 1 ? 4 : 2;
                        }
                        else
                        {
                            furtherCorner += 3;
                        }
                    }
                }
                else
                {
                    vertices[i] = Vector3.zero; // will be overwiten below, based on previous and next vertex position
                }
                borderVertices[i - 1] = i;

                indices[index++] = 0;
                indices[index++] = i;
                indices[index++] = i == vertices.Length - 1 ? 1 : i + 1;
            }
            if (rounded)
            {
                for (int i = 0; i < roundableVertices.Length; i++)
                {
                    if (roundings[i] > 1)
                    {
                        int tipID = roundableVertices[i];
                        int prevI = tipID == 1 ? vertices.Length - 1 : tipID - 1;
                        int nextI = tipID + 1;

                        int prevR = tipID == 1 ? vertices.Length - 2 : tipID == 2 ? vertices.Length - 1 : tipID - 2;
                        int nextR = tipID == vertices.Length - 2 ? 1 : tipID + 2;

                        vertices[prevI] = Vector3.Lerp(vertices[prevR], vertices[tipID], 1 - roundingsDistances[i]);
                        vertices[nextI] = Vector3.Lerp(vertices[nextR], vertices[tipID], 1 - roundingsDistances[i]);
                    }
                }
            }

            var data = new MeshData(vertices, indices, borderVertices, roundableVertices, 0);

            if (rounded)
            {
                for (int i = 0; i < resolution; i++)
                {
                    if (roundings[i] > 1)
                    {
                        data.Bevel(i, roundings[i], 1);
                    }
                }
            }
            return(data);
        }
Beispiel #2
0
        public override MeshData GetData()
        {
            int  seg     = resolution;
            bool rounded = false;

            uniform = true;
            for (int i = 0; i < roundings.Length; i++)
            {
                if (i >= roundingsDistances.Length)
                {
                    float[] nRoundings = new float[roundings.Length];
                    for (int j = 0; j < nRoundings.Length; j++)
                    {
                        if (j < roundingsDistances.Length)
                        {
                            nRoundings[j] = roundingsDistances[j];
                        }
                        else
                        {
                            nRoundings[j] = roundingsDistances[0];
                        }
                    }
                    roundingsDistances = nRoundings;
                }
                if (i >= 0 && i < roundingsDistances.Length)
                {
                    roundings[i] = Mathf.RoundToInt(1 + (roundingsDistances[i] * 40));
                }

                if (roundings[i] > 1)
                {
                    uniform     = false;
                    rounded     = true;
                    resolution += 2;
                    // if a corner have rounding we add 2 vertexes that will be bounds for rounding function
                }
            }

            if (roundings.Length != seg)
            {
                roundings = new int[seg];
            }
            Vector3[] vertices       = new Vector3[resolution + 1];
            int[]     indices        = new int[resolution * 3];
            int[]     borderVertices = new int[resolution];
            resolution = seg;
            int[] roundableVertices = new int[resolution];

            vertices[0] = Vector3.zero; //Center vertex

            int index     = 0;
            int a         = 0;
            int curCorner = 1;

            if (roundings[0] > 1)
            {
                curCorner = 2;
            }
            for (int i = 1; i < vertices.Length; i++)
            {
                if (i == curCorner)
                {
                    vertices[i]          = MakeVertex(a);
                    roundableVertices[a] = i;
                    a++;
                    if (a < roundings.Length)
                    {
                        if (roundings[a - 1] > 1 && roundings[a] > 1)
                        {
                            curCorner += 3;
                        }
                        else if ((roundings[a - 1] == 1 && roundings[a] == 1) || !rounded)
                        {
                            curCorner += 1;
                        }
                        else
                        {
                            curCorner += 2;
                        }
                    }
                }
                else
                {
                    vertices[i] = MakeVertex(a);
                }
                borderVertices[i - 1] = i;
                indices[index++]      = 0;
                indices[index++]      = i;
                indices[index++]      = i == vertices.Length - 1 ? 1 : i + 1;
            }
            if (rounded)
            {
                //Fixing position of rounding vertexes
                for (int i = 0; i < roundableVertices.Length; i++)
                {
                    if (roundings[i] > 1)
                    {
                        int prevI = (roundableVertices[i] - 1 < 0)? roundableVertices.Length - 1 : (roundableVertices[i] - 1);
                        int nextI = (roundableVertices[i] + 1 >= vertices.Length)? 0 :(roundableVertices[i] + 1);
                        int prevR = roundableVertices[i - 1 >= 0 ? i - 1 : roundableVertices.Length - 1];
                        int nextR = roundableVertices[i + 1 < roundableVertices.Length ? i + 1 : 0];

                        vertices[prevI] = Vector3.Lerp(vertices[roundableVertices[i]], vertices[prevR], roundingsDistances[i]);
                        vertices[nextI] = Vector3.Lerp(vertices[roundableVertices[i]], vertices[nextR], roundingsDistances[i]);
                    }
                }
            }
            MeshData data = new MeshData(vertices, indices, borderVertices, roundableVertices, 0);

            for (int i = 0; i < roundableVertices.Length; i++)
            {
                if (roundings[i] > 1)
                {
                    data.Bevel(i, roundings[i], 1f);
                }
            }

            return(data);
        }
Beispiel #3
0
        public override MeshData GetData()
        {
            if (roundings.Length != 5)
            {
                roundings = new int[5];
            }
            if (roundingsDistances.Length != 5)
            {
                roundingsDistances = new float[5];
            }
            uniform = true;
            bool  rounded      = false;
            float directionMlt = facingDirection == 0 ? 1f : -1f;
            float row          = 0.5f * (2 * middleHeight - 1f);
            float hatX         = topWidth * 0.5f * directionMlt;
            float shoulderX    = 0.5f * directionMlt;

            Vector3[] vertices = new[]
            {
                new Vector3(0, 0, 0),
                new Vector3(hatX, 0.5f, 0),
                new Vector3(-hatX, 0.5f, 0),
                new Vector3(-shoulderX, row, 0),
                new Vector3(0, -0.5f, 0),
                new Vector3(shoulderX, row, 0),
            };

            int[] indices = new[]
            {
                5, 4, 0,
                4, 3, 0,
                3, 2, 0,
                2, 1, 0,
                1, 5, 0,
            };
            int[] borderVertices    = new[] { 5, 4, 3, 2, 1 };
            int[] roundableVertices = new int[] { 1, 2, 3, 4, 5 };
            for (int i = 0; i < roundableVertices.Length; i++)
            {
                float m1, m2;
                if (i == 0)
                {
                    m1 = Vector3.Distance(vertices[roundableVertices[i]], vertices[roundableVertices[roundableVertices.Length - 1]]);
                    m2 = Vector3.Distance(vertices[roundableVertices[i]], vertices[roundableVertices[i + 1]]);
                }
                else if (i == roundableVertices.Length - 1)
                {
                    m1 = Vector3.Distance(vertices[roundableVertices[i]], vertices[roundableVertices[0]]);
                    m2 = Vector3.Distance(vertices[roundableVertices[i]], vertices[roundableVertices[i - 1]]);
                }
                else
                {
                    m1 = Vector3.Distance(vertices[roundableVertices[i]], vertices[roundableVertices[i - 1]]);
                    m2 = Vector3.Distance(vertices[roundableVertices[i]], vertices[roundableVertices[i + 1]]);
                }
                roundingsDistances[i] = Mathf.Min(roundingsDistances[i], Mathf.Min(m1, m2) / 2.2f);

                roundings[i] = Mathf.RoundToInt(1 + (roundingsDistances[i] * 40));
            }
            MeshData data = new MeshData(vertices, indices, borderVertices, roundableVertices, 0);

            for (int i = 0; i < roundings.Length; i++)
            {
                if (roundings[i] > 1)
                {
                    rounded = true;
                    uniform = false;
                }
            }
            if (rounded)
            {
                for (int i = 0; i < roundableVertices.Length; i++)
                {
                    if (roundings[i] > 1)
                    {
                        data.PrepareBevel(i, roundingsDistances[i]);
                    }
                }
                for (int i = 0; i < roundableVertices.Length; i++)
                {
                    if (roundings[i] > 1)
                    {
                        data.Bevel(i, roundings[i], 1);
                    }
                }
            }
            return(data);
        }