Looking for Vector Graphics API examples

Hello !

I want to create some svg in code with the api I found here : About Vector Graphics | Vector Graphics | 2.0.0-preview.24
But I have no full example of the use, do you know where I can found something like a script that update a sprite renderer with a simple sprite (like, a line that look like to ‘S’) fully generated via the API ?

Thanks !

PS : Another problem, I don’t find the class “Path” that is in documentation (I use Unity 2021.1.22f1 and the Vector Graphics package at 2.0.0-preview.17)

Ok, I found some sample in the git repository, like this one
If you look for a sample with sprite :

public class test : MonoBehaviour
{
    public Transform[] controlPoints;

    private Scene m_Scene;
    private Shape m_Path;
    private VectorUtils.TessellationOptions m_Options;
    private SpriteRenderer m_SpriteRenderer;

    // Start is called before the first frame update
    void Start()
    {
        m_SpriteRenderer = GetComponent<SpriteRenderer>();
        // Prepare the vector path, add it to the vector scene.
        m_Path = new Shape()
        {
            Contours = new BezierContour[] { new BezierContour() { Segments = new BezierPathSegment[2] } },
            PathProps = new PathProperties()
            {
                Stroke = new Stroke() { Color = Color.white, HalfThickness = 0.1f }
            }
        };

        m_Scene = new Scene()
        {
            Root = new SceneNode() { Shapes = new List<Shape> { m_Path } }
        };

        m_Options = new VectorUtils.TessellationOptions()
        {
            StepDistance = 1000.0f,
            MaxCordDeviation = 0.05f,
            MaxTanAngleDeviation = 0.05f,
            SamplingStepSize = 0.01f
        };
    }


    // Update is called once per frame
    void Update()
    {
        if (m_Scene == null)
            Start();
        // Update the control points of the spline.
        m_Path.Contours[0].Segments[0].P0 = (Vector2)controlPoints[0].localPosition;
        m_Path.Contours[0].Segments[0].P1 = (Vector2)controlPoints[1].localPosition;
        m_Path.Contours[0].Segments[0].P2 = (Vector2)controlPoints[2].localPosition;
        m_Path.Contours[0].Segments[1].P0 = (Vector2)controlPoints[3].localPosition;

        // Tessellate the vector scene, and fill the mesh with the resulting geometry.
        var geoms = VectorUtils.TessellateScene(m_Scene, m_Options);
        m_SpriteRenderer.sprite = VectorUtils.BuildSprite(geoms, 1.0f, Alignment.Center, Vector2.zero, 1);
    }
}

Of course, in my code, you need a SpriteRenderer component and 4 transforms in conntrolPoints.

1 Like