Example #1
0
        public void OnPopulateMesh(VertexBuffer vb)
        {
            Rect    rect  = drawRect != null ? (Rect)drawRect : vb.contentRect;
            Color32 color = fillColor != null ? (Color32)fillColor : vb.vertexColor;

            if (lineWidth == 0)
            {
                if (color.a != 0)//optimized
                {
                    vb.AddQuad(rect, color);
                }
            }
            else
            {
                Rect part;

                //left,right
                part = new Rect(rect.x, rect.y, lineWidth, rect.height);
                vb.AddQuad(part, lineColor);
                part = new Rect(rect.xMax - lineWidth, rect.y, lineWidth, rect.height);
                vb.AddQuad(part, lineColor);

                //top, bottom
                part = new Rect(rect.x + lineWidth, rect.y, rect.width - lineWidth * 2, lineWidth);
                vb.AddQuad(part, lineColor);
                part = new Rect(rect.x + lineWidth, rect.yMax - lineWidth, rect.width - lineWidth * 2, lineWidth);
                vb.AddQuad(part, lineColor);

                //middle
                if (color.a != 0)//optimized
                {
                    part = Rect.MinMaxRect(rect.x + lineWidth, rect.y + lineWidth, rect.xMax - lineWidth, rect.yMax - lineWidth);
                    if (part.width > 0 && part.height > 0)
                    {
                        vb.AddQuad(part, color);
                    }
                }
            }

            if (colors != null)
            {
                vb.RepeatColors(colors, 0, vb.currentVertCount);
            }

            vb.AddTriangles();
        }
Example #2
0
        public void OnPopulateMesh(VertexBuffer vb)
        {
            Rectangle rect  = drawRect != null ? (Rectangle)drawRect : vb.contentRect;
            Color     color = fillColor != null ? (Color)fillColor : vb.vertexColor;

            if (lineWidth == 0)
            {
                if (color.A != 0)                //optimized
                {
                    vb.AddQuad(rect, color);
                }
            }
            else
            {
                Rectangle part;

                //left,right
                part = Rectangle.FromLTRB(rect.X, rect.Y, lineWidth, rect.Height);
                vb.AddQuad(part, lineColor);
                part = Rectangle.FromLTRB(rect.Right - lineWidth, 0, rect.Right, rect.Bottom);
                vb.AddQuad(part, lineColor);

                //top, bottom
                part = Rectangle.FromLTRB(lineWidth, rect.X, rect.Right - lineWidth, lineWidth);
                vb.AddQuad(part, lineColor);
                part = Rectangle.FromLTRB(lineWidth, rect.Bottom - lineWidth, rect.Right - lineWidth, rect.Bottom);
                vb.AddQuad(part, lineColor);

                //middle
                if (color.A != 0)                //optimized
                {
                    part = Rectangle.FromLTRB(lineWidth, lineWidth, rect.Right - lineWidth, rect.Bottom - lineWidth);
                    vb.AddQuad(part, color);
                }
            }

            if (colors != null)
            {
                vb.RepeatColors(colors, 0, vb.currentVertCount);
            }

            vb.AddTriangles();
        }
        public void OnPopulateMesh(VertexBuffer vb)
        {
            int numVertices = points.Count;

            if (numVertices < 3)
            {
                return;
            }

            int restIndexPos, numRestIndices;

            Color color = fillColor != null ? (Color)fillColor : vb.vertexColor;

            for (int i = 0; i < numVertices; i++)
            {
                Vector3 vec = new Vector3(points[i].X, points[i].Y, 0);
                if (usePercentPositions)
                {
                    vec.X *= vb.contentRect.Width;
                    vec.Y *= vb.contentRect.Height;
                }
                vb.AddVert(vec, color);
            }

            // Algorithm "Ear clipping method" described here:
            // -> https://en.wikipedia.org/wiki/Polygon_triangulation
            //
            // Implementation inspired by:
            // -> http://polyk.ivank.net
            // -> Starling

            sRestIndices.Clear();
            for (int i = 0; i < numVertices; ++i)
            {
                sRestIndices.Add(i);
            }

            restIndexPos   = 0;
            numRestIndices = numVertices;

            Vector2 a, b, c, p;
            int     otherIndex;
            bool    earFound;
            int     i0, i1, i2;

            while (numRestIndices > 3)
            {
                earFound = false;
                i0       = sRestIndices[restIndexPos % numRestIndices];
                i1       = sRestIndices[(restIndexPos + 1) % numRestIndices];
                i2       = sRestIndices[(restIndexPos + 2) % numRestIndices];

                a = points[i0];
                b = points[i1];
                c = points[i2];

                if ((a.Y - b.Y) * (c.X - b.X) + (b.X - a.X) * (c.Y - b.Y) >= 0)
                {
                    earFound = true;
                    for (int i = 3; i < numRestIndices; ++i)
                    {
                        otherIndex = sRestIndices[(restIndexPos + i) % numRestIndices];
                        p          = points[otherIndex];

                        if (IsPointInTriangle(ref p, ref a, ref b, ref c))
                        {
                            earFound = false;
                            break;
                        }
                    }
                }

                if (earFound)
                {
                    vb.AddTriangle(i0, i1, i2);
                    sRestIndices.RemoveAt((restIndexPos + 1) % numRestIndices);

                    numRestIndices--;
                    restIndexPos = 0;
                }
                else
                {
                    restIndexPos++;
                    if (restIndexPos == numRestIndices)
                    {
                        break;                                                     // no more ears
                    }
                }
            }
            vb.AddTriangle(sRestIndices[0], sRestIndices[1], sRestIndices[2]);

            if (colors != null)
            {
                vb.RepeatColors(colors, 0, vb.currentVertCount);
            }
        }
        public void OnPopulateMesh(VertexBuffer vb)
        {
            int numVertices = points.Count;

            if (numVertices < 3)
            {
                return;
            }

            int     restIndexPos, numRestIndices;
            Color32 color = fillColor != null ? (Color32)fillColor : vb.vertexColor;

            float w            = vb.contentRect.width;
            float h            = vb.contentRect.height;
            bool  useTexcoords = texcoords.Count >= numVertices;
            bool  fullUV       = true;

            for (int i = 0; i < numVertices; i++)
            {
                Vector3 vec = new Vector3(points[i].x, points[i].y, 0);
                if (usePercentPositions)
                {
                    vec.x *= w;
                    vec.y *= h;
                }
                if (useTexcoords)
                {
                    Vector2 uv = texcoords[i];
                    if (uv.x != 0 && uv.x != 1 || uv.y != 0 && uv.y != 1)
                    {
                        fullUV = false;
                    }
                    uv.x = Mathf.Lerp(vb.uvRect.x, vb.uvRect.xMax, uv.x);
                    uv.y = Mathf.Lerp(vb.uvRect.y, vb.uvRect.yMax, uv.y);
                    vb.AddVert(vec, color, uv);
                }
                else
                {
                    vb.AddVert(vec, color);
                }
            }

            if (useTexcoords && fullUV && numVertices == 4)
            {
                vb._isArbitraryQuad = true;
            }

            // Algorithm "Ear clipping method" described here:
            // -> https://en.wikipedia.org/wiki/Polygon_triangulation
            //
            // Implementation inspired by:
            // -> http://polyk.ivank.net
            // -> Starling

            sRestIndices.Clear();
            for (int i = 0; i < numVertices; ++i)
            {
                sRestIndices.Add(i);
            }

            restIndexPos   = 0;
            numRestIndices = numVertices;

            Vector2 a, b, c, p;
            int     otherIndex;
            bool    earFound;
            int     i0, i1, i2;

            while (numRestIndices > 3)
            {
                earFound = false;
                i0       = sRestIndices[restIndexPos % numRestIndices];
                i1       = sRestIndices[(restIndexPos + 1) % numRestIndices];
                i2       = sRestIndices[(restIndexPos + 2) % numRestIndices];

                a = points[i0];
                b = points[i1];
                c = points[i2];

                if ((a.y - b.y) * (c.x - b.x) + (b.x - a.x) * (c.y - b.y) >= 0)
                {
                    earFound = true;
                    for (int i = 3; i < numRestIndices; ++i)
                    {
                        otherIndex = sRestIndices[(restIndexPos + i) % numRestIndices];
                        p          = points[otherIndex];

                        if (IsPointInTriangle(ref p, ref a, ref b, ref c))
                        {
                            earFound = false;
                            break;
                        }
                    }
                }

                if (earFound)
                {
                    vb.AddTriangle(i0, i1, i2);
                    sRestIndices.RemoveAt((restIndexPos + 1) % numRestIndices);

                    numRestIndices--;
                    restIndexPos = 0;
                }
                else
                {
                    restIndexPos++;
                    if (restIndexPos == numRestIndices)
                    {
                        break;                                                     // no more ears
                    }
                }
            }
            vb.AddTriangle(sRestIndices[0], sRestIndices[1], sRestIndices[2]);

            if (colors != null)
            {
                vb.RepeatColors(colors, 0, vb.currentVertCount);
            }
        }