Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Resolved Looking for Vector Graphics API examples

Discussion in 'General Graphics' started by LoicLeGrosFrere, Oct 11, 2021.

  1. LoicLeGrosFrere

    LoicLeGrosFrere

    Joined:
    Jan 5, 2020
    Posts:
    4
    Hello !

    I want to create some svg in code with the api I found here : https://docs.unity3d.com/Packages/com.unity.vectorgraphics@2.0/manual/index.html#vector-graphics-api
    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)
     
  2. LoicLeGrosFrere

    LoicLeGrosFrere

    Joined:
    Jan 5, 2020
    Posts:
    4
    Ok, I found some sample in the git repository, like this one
    If you look for a sample with sprite :

    Code (CSharp):
    1. public class test : MonoBehaviour
    2. {
    3.     public Transform[] controlPoints;
    4.  
    5.     private Scene m_Scene;
    6.     private Shape m_Path;
    7.     private VectorUtils.TessellationOptions m_Options;
    8.     private SpriteRenderer m_SpriteRenderer;
    9.  
    10.     // Start is called before the first frame update
    11.     void Start()
    12.     {
    13.         m_SpriteRenderer = GetComponent<SpriteRenderer>();
    14.         // Prepare the vector path, add it to the vector scene.
    15.         m_Path = new Shape()
    16.         {
    17.             Contours = new BezierContour[] { new BezierContour() { Segments = new BezierPathSegment[2] } },
    18.             PathProps = new PathProperties()
    19.             {
    20.                 Stroke = new Stroke() { Color = Color.white, HalfThickness = 0.1f }
    21.             }
    22.         };
    23.  
    24.         m_Scene = new Scene()
    25.         {
    26.             Root = new SceneNode() { Shapes = new List<Shape> { m_Path } }
    27.         };
    28.  
    29.         m_Options = new VectorUtils.TessellationOptions()
    30.         {
    31.             StepDistance = 1000.0f,
    32.             MaxCordDeviation = 0.05f,
    33.             MaxTanAngleDeviation = 0.05f,
    34.             SamplingStepSize = 0.01f
    35.         };
    36.     }
    37.  
    38.  
    39.     // Update is called once per frame
    40.     void Update()
    41.     {
    42.         if (m_Scene == null)
    43.             Start();
    44.         // Update the control points of the spline.
    45.         m_Path.Contours[0].Segments[0].P0 = (Vector2)controlPoints[0].localPosition;
    46.         m_Path.Contours[0].Segments[0].P1 = (Vector2)controlPoints[1].localPosition;
    47.         m_Path.Contours[0].Segments[0].P2 = (Vector2)controlPoints[2].localPosition;
    48.         m_Path.Contours[0].Segments[1].P0 = (Vector2)controlPoints[3].localPosition;
    49.  
    50.         // Tessellate the vector scene, and fill the mesh with the resulting geometry.
    51.         var geoms = VectorUtils.TessellateScene(m_Scene, m_Options);
    52.         m_SpriteRenderer.sprite = VectorUtils.BuildSprite(geoms, 1.0f, Alignment.Center, Vector2.zero, 1);
    53.     }
    54. }
    Of course, in my code, you need a SpriteRenderer component and 4 transforms in conntrolPoints.
     
    eidetic likes this.