Ejemplo n.º 1
0
        public static void DrawRect(this UICanvas canvas, List <UIVertex> vertices, List <int> indices, UIRectVO rectVO)
        {
            Rect    rect = rectVO.rect;
            Vector2 TL   = rect.position;
            Vector2 TR   = new Vector2(rect.xMax, rect.yMin);
            Vector2 BR   = new Vector2(rect.xMax, rect.yMax);
            Vector2 BL   = new Vector2(rect.xMin, rect.yMax);

            if (rectVO.fill)
            {
                var polygon = new List <Vector2>()
                {
                    TL, TR, BR, BL
                };
                canvas.FillPolygon(vertices, indices, polygon, rectVO.fillColor, rectVO.name);
            }
            if (rectVO.stroke)
            {
                var polygon = new List <Vector2>()
                {
                    TL, TR, BR, BL, TL
                };
                canvas.StrokePolygon(vertices, indices, polygon, rectVO.thickness, rectVO.strokeColor);
            }
        }
Ejemplo n.º 2
0
 public static void DrawLine(this UICanvas canvas, List <UIVertex> vertices, List <int> indices, UILineVO line)
 {
     if (line.fill)
     {
         FillPolygon(canvas, vertices, indices, line);
     }
     if (line.stroke)
     {
         canvas.StrokePolygon(vertices, indices, line.points, line.thickness, line.color);
     }
 }
Ejemplo n.º 3
0
        public static UILineVO LineTo(this UICanvas canvas, Vector2 point)
        {
            UILineVO line = canvas.Strokes.Last();

            if (line == null)
            {
                return(canvas.MoveTo(point));
            }
            line.points.Add(point);
            return(line);
        }
Ejemplo n.º 4
0
        public static UIRectVO Rect(this UICanvas canvas, float x, float y, float width, float height)
        {
            var vo = new UIRectVO();

            vo.rect        = new Rect(x, y, width, height);
            vo.fill        = canvas.strokeStyle.fill;
            vo.fillColor   = canvas.strokeStyle.fillColor;
            vo.thickness   = canvas.strokeStyle.thickness;
            vo.strokeColor = canvas.strokeStyle.strokeColor;
            vo.stroke      = canvas.strokeStyle.stroke;
            canvas.DrawingGraphics.Add(vo);
            return(vo);
        }
Ejemplo n.º 5
0
        public static UILineVO MoveTo(this UICanvas canvas, Vector2 point)
        {
            UILineVO line = new UILineVO();

            line.points.Add(point);
            line.fill      = canvas.strokeStyle.fill;
            line.stroke    = canvas.strokeStyle.stroke;
            line.fillColor = canvas.strokeStyle.fillColor;
            line.color     = canvas.strokeStyle.strokeColor;
            line.thickness = canvas.strokeStyle.thickness;
            canvas.Strokes.Add(line);
            return(line);
        }
Ejemplo n.º 6
0
        public static void DrawCircle(this UICanvas canvas, List <UIVertex> vertices, List <int> indices, UICircleVO circle)
        {
            float f       = (circle.fillAmount / 100f);
            float fs      = (circle.fillStart / 100f);
            float degrees = 360f / circle.segments;
            float fa      = (circle.segments) * f;
            float start   = (circle.segments) * fs;

            List <Vector2> allV = new List <Vector2>();
            float          i    = start * degrees;
            float          end  = (fa + start) * degrees;

            while (i <= end)
            {
                float rad = Mathf.Deg2Rad * i;
                float c   = Mathf.Cos(rad);
                float s   = Mathf.Sin(rad);

                Vector2 p0 = new Vector2(circle.center.x + circle.radius * c, circle.center.y + circle.radius * s);     // 外边框 顶点0
                allV.Add(p0);
                if (i == end)
                {
                    break;
                }

                i = Mathf.Min(i + degrees, end);
            }

            List <Vector2> polygonVertices = allV.ToArray().ToList();

            if (circle.fillAmount < 100)
            {
                polygonVertices.Add(circle.center);
            }
            else
            {
                polygonVertices = polygonVertices.GetRange(0, polygonVertices.Count - 1);
            }

            if (circle.fill)
            {
                canvas.FillPolygon(vertices, indices, polygonVertices, circle.fillColor);
            }
            if (circle.stroke)
            {
                canvas.StrokePolygon(vertices, indices, allV, circle.thickness, circle.color);
            }
        }
Ejemplo n.º 7
0
        public static UICircleVO Arc(this UICanvas canvas, Vector2 center, float radius, bool stroke, Color32 strokeColor, float thickness, bool fill, Color32 fillColor, float fillStart = 0, float fillAmount = 100, int segments = 360)
        {
            var vo = new UICircleVO();

            vo.center     = center;
            vo.radius     = radius;
            vo.fillAmount = fillAmount;
            vo.fill       = fill;
            vo.fillColor  = fillColor;
            vo.stroke     = stroke;
            vo.color      = strokeColor;
            vo.thickness  = thickness;
            vo.segments   = segments;
            vo.fillStart  = fillStart;
            canvas.DrawingGraphics.Add(vo);
            return(vo);
        }
Ejemplo n.º 8
0
        private static int[] DoFillPolygon(UICanvas canvas, List <UIVertex> vertices, List <int> indices, List <Vector2> points, Color32 fillColor, float depth, string polygonName = "")
        {
            if (points == null)
            {
                return(null);
            }
            List <UIVertex> vs = new List <UIVertex>();

            for (int i = 0; i < points.Count; i++)
            {
                UIVertex v = UIVertex.simpleVert;
                v.position = new Vector3(points[i].x, points[i].y, depth);
                v.color    = fillColor;
                v.uv0      = UICanvas.uvVertices[0];
                vs.Add(v);
            }
            if (canvas.OnPopulatePolygon != null)
            {
                UIVertex[] arr        = vs.ToArray();
                var        polygons   = canvas.OnPopulatePolygon(arr, polygonName);
                List <int> newIndices = new List <int>();
                polygons.ForEach(
                    p => {
                    vs = new List <UIVertex>(p);
                    newIndices.AddRange(AddPolygon(vs, vertices));
                }
                    );
                return(newIndices.ToArray());
            }
            else
            {
                return(AddPolygon(vs, vertices));
            }
            // var tri = new Triangulator(points.ToArray());
            // int[] newIndices = tri.Triangulate();
            // int start = vertices.Count;
            // AddOn(start, newIndices);
            // vertices.AddRange(vs);
        }
Ejemplo n.º 9
0
 public static UILineVO MoveTo(this UICanvas canvas, float x, float y)
 {
     return(MoveTo(canvas, new Vector2(x, y)));
 }
Ejemplo n.º 10
0
 public static void Stroke(this UICanvas canvas)
 {
     canvas.DrawingGraphics.AddRange(canvas.Strokes.ToArray());
     canvas.Strokes.Clear();
 }
Ejemplo n.º 11
0
        private static void FillPolygon(UICanvas canvas, List <UIVertex> vertices, List <int> indices, UILineVO line)
        {
            var points = line.points.GetRange(0, line.points.Count - 1);

            canvas.FillPolygon(vertices, indices, points, line.fillColor, line.name);
        }
Ejemplo n.º 12
0
        public static void StrokePolygon(this UICanvas canvas, List <UIVertex> vertices, List <int> indices, List <Vector2> points, float thickness, Color32 strokeColor)
        {
            if (points == null || points.Count < 2)
            {
                return;
            }
            int countStart = vertices.Count;

            for (int i = 0; i < points.Count - 1; i++)
            {
                Vector2 p0   = points[i];
                Vector2 p1   = points[i + 1];
                float   r    = Vector2.Distance(p0, p1);
                float   cosA = (p1.x - p0.x) / r;
                float   radA = Mathf.Acos(cosA);
                if (p1.y < p0.y)
                {
                    radA += (Mathf.PI - radA) * 2;
                }
                float rad90 = Mathf.PI / 2;
                // float degA = Mathf.Rad2Deg * radA;
                Vector3 v0   = Vector3.zero;
                Vector3 v1   = Vector3.zero;
                Vector3 v2   = Vector3.zero;
                Vector3 v3   = Vector3.zero;
                float   half = thickness / 2;
                v0.x = p0.x + half * Mathf.Cos(radA - rad90);
                v0.y = p0.y + half * Mathf.Sin(radA - rad90);
                v1.x = p1.x + half * Mathf.Cos(radA - rad90);
                v1.y = p1.y + half * Mathf.Sin(radA - rad90);
                v2.x = p1.x + half * Mathf.Cos(radA + rad90);
                v2.y = p1.y + half * Mathf.Sin(radA + rad90);
                v3.x = p0.x + half * Mathf.Cos(radA + rad90);
                v3.y = p0.y + half * Mathf.Sin(radA + rad90);

                UIVertex[] vertex;
                if (canvas.LineSprite)
                {
                    Vector4   outer = UnityEngine.Sprites.DataUtility.GetOuterUV(canvas.LineSprite);
                    Vector2[] uvs   = new Vector2[4] {
                        new Vector2(outer.x, outer.y),
                        new Vector2(outer.z, outer.y),
                        new Vector2(outer.z, outer.w),
                        new Vector2(outer.x, outer.w)
                    };
                    vertex = canvas.SetVbo(new[] { v0, v1, v2, v3 }, uvs, strokeColor);
                }
                else
                {
                    vertex = canvas.SetVbo(new[] { v0, v1, v2, v3 }, UICanvas.uvVertices, strokeColor);
                }

                int   start      = vertices.Count;
                int[] newIndices = new [] { start, start + 1, start + 2, start + 2, start + 3, start };
                vertices.AddRange(vertex);
                indices.AddRange(newIndices);
            }

            int vCount = vertices.Count;

            // 画锚点
            for (int i = countStart; i < vCount - 4; i += 4)
            {
                indices.AddRange(new [] { i + 1, i + 2, i + 4 });
                indices.AddRange(new [] { i + 1, i + 2, i + 7 });
            }
            // 如果起始点和结束点坐标一样,则需要锚接这两个点
            Vector2 first = points.First();
            Vector2 last  = points.Last();

            if (first.Equals(last))
            {
                indices.AddRange(new [] { countStart, countStart + 3, vCount - 3 });
            }
        }
Ejemplo n.º 13
0
        public static void FillPolygon(this UICanvas canvas, List <UIVertex> vertices, List <int> indices, List <Vector2> points, Color32 fillColor, string polygonName = "")
        {
            int[] frontQuad = DoFillPolygon(canvas, vertices, indices, points, fillColor, 0, polygonName);

            indices.AddRange(frontQuad);
        }