示例#1
0
    private BezierContour[] BuildCircleContourWithMask(Rect rect, Vector2 rad, Rect maskRect, Vector2 maskRad)
    {
        var contours = new BezierContour[2];

        contours[0] = VectorUtils.BuildRectangleContour(rect, rad, rad, rad, rad);
        contours[1] = VectorUtils.BuildRectangleContour(maskRect, maskRad, maskRad, maskRad, maskRad);

        return(contours);
    }
    private BezierContour[] BuildRectangleContourWithMask(Rect rect, Rect maskRect)
    {
        // ToDo: Clamp [maskRect] by [rect]

        var contours = new BezierContour[2];

        contours[0] = VectorUtils.BuildRectangleContour(rect, Vector2Zero, Vector2Zero, Vector2Zero, Vector2Zero);
        contours[1] = VectorUtils.BuildRectangleContour(maskRect, Vector2Zero, Vector2Zero, Vector2Zero, Vector2Zero);
        return(contours);
    }
    void Start()
    {
        var tessOptions = new VectorUtils.TessellationOptions()
        {
            StepDistance         = 100.0f,
            MaxCordDeviation     = 0.5f,
            MaxTanAngleDeviation = 0.1f,
            SamplingStepSize     = 0.01f
        };

        // Prepare the fill
        var fill = new GradientFill()
        {
            Type  = GradientFillType.Linear,
            Stops = new GradientStop[] {
                new GradientStop()
                {
                    Color = Color.blue, StopPercentage = 0.0f
                },
                new GradientStop()
                {
                    Color = Color.red, StopPercentage = 1.0f
                },
            }
        };

        // Build the scene
        var cornerRad = new Vector2(10, 10);
        var rect      = VectorUtils.BuildRectangleContour(new Rect(0, 0, 100, 100), cornerRad, cornerRad, cornerRad, cornerRad);
        var scene     = new Scene()
        {
            Root = new SceneNode()
            {
                Shapes = new List <Shape> {
                    new Shape()
                    {
                        Contours = new BezierContour[] { rect },
                        Fill     = fill
                    }
                }
            }
        };

        // Dynamically import the SVG data, and tessellate the resulting vector scene.
        var geoms = VectorUtils.TessellateScene(scene, tessOptions);

        // Build a sprite with the tessellated geometry.
        var sprite = VectorUtils.BuildSprite(geoms, 10.0f, VectorUtils.Alignment.Center, Vector2.zero, 16, true);

        GetComponent <SpriteRenderer>().sprite = sprite;
    }
示例#4
0
        static public Sprite CreateBorderSpriteVector(int tl, int tr, int bl, int br)
        {
            tl = Mathf.Max(tl, 0);
            tr = Mathf.Max(tr, 0);
            bl = Mathf.Max(bl, 0);
            br = Mathf.Max(br, 0);

            var key = GetKey(tl, tr, bl, br);

            if (SpriteCache.ContainsKey(key))
            {
                return(SpriteCache[key]);
            }
            if (tl == 0 && tr == 0 && bl == 0 && br == 0)
            {
                return(CreateFlatBorder());
            }
            var(width, height) = GetSize(tl, tr, bl, br);

            if (!FeatureGuards.VectorGraphics && Application.isPlaying)
            {
                return(null);
            }

#if !REACT_VECTOR_GRAPHICS || UNITY_WEBGL
            return(null);
#else
            var svg = new Scene()
            {
                Root = new SceneNode()
                {
                    Shapes = new List <Shape>()
                }
            };

            // For some reason, the svg is inverted so I am inverting the corners
            var contour = VectorUtils.BuildRectangleContour(new Rect(0, 0, width, height),
                                                            Vector2.one * bl, Vector2.one * br, Vector2.one * tr, Vector2.one * tl);

            var roundedRect = new Shape()
            {
                Contours = new BezierContour[] { contour },
                Fill     = new SolidFill()
                {
                    Color = Color.white, Opacity = 1
                },
                PathProps = new PathProperties()
                {
                    Corners = PathCorner.Round
                },
                IsConvex = true,
            };
            svg.Root.Shapes.Add(roundedRect);

            var geo = VectorUtils.TessellateScene(svg, new VectorUtils.TessellationOptions()
            {
                StepDistance = 1, SamplingStepSize = 1
            });
            var sprite = VectorUtils.BuildSprite(geo, 1, VectorUtils.Alignment.Center, Vector2.one / 2, 100);

            var mat       = new Material(Shader.Find("Unlit/Vector"));
            var texture   = VectorUtils.RenderSpriteToTexture2D(sprite, width, height, mat);
            var newSprite = Sprite.Create(texture, new Rect(0, 0, width, height), Vector2.one / 2, 1, 0, SpriteMeshType.FullRect, GetBorder(tl, tr, bl, br));


            Object.DestroyImmediate(sprite);
            SpriteCache[key] = newSprite;
            return(newSprite);
#endif
        }
示例#5
0
        static public Sprite CreateBorderSprite(int borderRadius)
        {
            borderRadius = Mathf.Max(borderRadius, 0);
            if (SpriteCache.ContainsKey(borderRadius))
            {
                return(SpriteCache[borderRadius]);
            }

            if (borderRadius == 0)
            {
                var smallTexture = new Texture2D(4, 4);
                var colors       = new Color[16];
                for (int i = 0; i < 16; i++)
                {
                    colors[i] = Color.white;
                }
                smallTexture.SetPixels(colors);
                smallTexture.Apply();
                return(SpriteCache[borderRadius] =
                           Sprite.Create(smallTexture, new Rect(0, 0, 4, 4), Vector2.one / 2, 1, 0, SpriteMeshType.FullRect, Vector4.one));
            }


#if !REACT_VECTOR_GRAPHICS
            if (ShowVectorGraphicsMessage)
            {
                Debug.LogError("To use the 'borderRadius' feeature, 'Unity.VectorGraphics' package must be installed.");
                ShowVectorGraphicsMessage = false;
            }
            return(null);
#else
            var svg = new Scene()
            {
                Root = new SceneNode()
                {
                    Shapes = new List <Shape>()
                }
            };

            var totalSize = borderRadius * 2 + 2;

            var rad = Vector2.one * borderRadius;

            var contour     = VectorUtils.BuildRectangleContour(new Rect(0, 0, totalSize, totalSize), rad, rad, rad, rad);
            var roundedRect = new Shape()
            {
                Contours = new BezierContour[] { contour },
                Fill     = new SolidFill()
                {
                    Color = Color.white, Opacity = 1
                },
                PathProps = new PathProperties()
                {
                    Corners = PathCorner.Round
                },
                IsConvex = true,
            };
            svg.Root.Shapes.Add(roundedRect);

            var geo = VectorUtils.TessellateScene(svg, new VectorUtils.TessellationOptions()
            {
                StepDistance = 1, SamplingStepSize = 1
            });
            var sprite = VectorUtils.BuildSprite(geo, 1, VectorUtils.Alignment.Center, Vector2.one / 2, 100);

            var size       = Mathf.CeilToInt(totalSize);
            var spriteRect = new Vector4(borderRadius, borderRadius, borderRadius, borderRadius);
            var mat        = new Material(Shader.Find("Unlit/Vector"));
            var texture    = VectorUtils.RenderSpriteToTexture2D(sprite, size, size, mat);
            var newSprite  = Sprite.Create(texture, new Rect(0, 0, size, size), Vector2.one / 2, 1, 0, SpriteMeshType.FullRect, spriteRect);


            Object.DestroyImmediate(sprite);
            SpriteCache[borderRadius] = newSprite;
            return(newSprite);
#endif
        }