Search Unity

Official Vector Graphics Preview Package

Discussion in 'UI Toolkit' started by rustum, May 4, 2018.

  1. OrderOfOut

    OrderOfOut

    Joined:
    Sep 21, 2018
    Posts:
    37
    Oh, weird. Are there any more thorough tutorials on how to draw with the shape class from scratch, procedurally? I remember seeing a little bit of it, but many of the examples for 2.0.0 still seem to use IDrawable. Overall, I want to make something similar to the AS3 / javascript canvas graphics class.
     
  2. mcoted3d

    mcoted3d

    Unity Technologies

    Joined:
    Feb 3, 2016
    Posts:
    1,003
    Correct, it seems the documentation hasn't been updated, thanks for reporting.

    There aren't many examples available, but overall, you want to add Shape objects to the SceneNode's list of shapes. A Shape is defined by a BezierContour. There are some utilities to simplify the BezierContour creations, such as MakeCircleShape, etc.:

    https://docs.unity3d.com/Packages/c...y_VectorGraphics_Shape_Vector2_System_Single_
     
    OrderOfOut likes this.
  3. OrderOfOut

    OrderOfOut

    Joined:
    Sep 21, 2018
    Posts:
    37
    Cool, that helped. One thing I'm stuck on -- I see some circle and rectangle examples about how to add them to a scene, which works fine, but lines seem to work differently; MakeLine doesn't use a shape like the others. So I made Bezier contours out of what it returned, then added them to a new shape. But I see nothing, even when I add on:
    Code (CSharp):
    1. shape.PathProps = new PathProperties()
    2.         {
    3.             Stroke = new Stroke() { Color = m_LineColor }
    4.         };
    Do Bezier Contours not use the stroke property? m_LineColor is (0,1,0), so I should be seeing green....

    EDIT: Okay, so using a fill instead of a stroke seems to draw the lines, oddly. Is that the way? </Mando>
     
  4. mcoted3d

    mcoted3d

    Unity Technologies

    Joined:
    Feb 3, 2016
    Posts:
    1,003
    It should work if your stroke has a thickness. Look at the HalfThickness property.
     
  5. awsapps

    awsapps

    Joined:
    Jun 15, 2021
    Posts:
    74
    Hi @mcoted3d , I'm trying to import an UI SVG with a linear gradient made in Inkscape, but it isn't displayed...
    Is there some workaround? Thanks! (I'm using vector graphics 2.0.0-preview.18 )
     
  6. mcoted3d

    mcoted3d

    Unity Technologies

    Joined:
    Feb 3, 2016
    Posts:
    1,003
    For gradients to work in UI you have to configure the Canvas to have "TexCoord2" enabled for the "Additional Vertex Channels" setting. Let me know if this wasn't the problem.
     
    awsapps likes this.
  7. awsapps

    awsapps

    Joined:
    Jun 15, 2021
    Posts:
    74
    Works perfectly, Thank you so much!
     
  8. SamuelIH

    SamuelIH

    Joined:
    Jan 7, 2021
    Posts:
    5
    In my current project, I’m getting SVGs from the artists and importing them as textured sprites to provide more resolution flexibility inside unity. This works but the vector renderer has serious issues rendering semitransparent whites onto the sprites. For example, this SVG:
    Code (CSharp):
    1. <svg width="128" height="128" version="1.1" xmlns="http://www.w3.org/2000/svg">
    2.   <defs>
    3.       <linearGradient id="Gradient2" x1="0" x2="0" y1="0" y2="1">
    4.         <stop offset="0%" stop-color="white"/>
    5.         <stop offset="100%" stop-color="white" stop-opacity="0"/>
    6.       </linearGradient>
    7.   </defs>
    8.   <rect x="0" y="0" width="128" height="128" fill="url(#Gradient2)"/>
    9. </svg>
    Produces the right side of this comparison:
    uiComparison.png
    Notice the dark area? That’s because the vector graphics shader’s alpha is premultiplied.

    By no means do I really know what I’m doing, but I was able to 'fix' it by editing VectorSprite.cs:RenderSpriteToTexture2D(…) and correcting the alpha between
    copy.ReadPixels
    and
    copy.Apply()
    :
    Code (CSharp):
    1. var pixels = copy.GetPixels();
    2. for (int i = 0; i<pixels.Length; i++) {
    3.     var c = pixels[i];
    4.     if (c.a == 0) {
    5.         continue;
    6.     }
    7.     // if premultiplied colors (p) = c * a
    8.     // then c = p / a
    9.     c.r = c.r / c.a;
    10.     c.g = c.g / c.a;
    11.     c.b = c.b / c.a;
    12.     pixels[i] = c;
    13. }
    14. copy.SetPixels(pixels);
    After this, I needed to do a complete reimport of all my SVGs.

    Hopefully, this issue can get some attention from the dev team, because in the future I’d rather not commit the entire vector graphics package to git for one small fix.


    (Version 2.0.0-preview.18)
     
  9. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,491
    I would highly recommend that if you believe there's an issue, by far the best way to report it is via the bug reporter!

    There's no guarantee that a forum post will be seen by the relevant person and even if they do, it's not a reminder or record in of itself.

    I'm sure the dev(s) in question would value a bug report so thank you on their behalf! :)
     
  10. mcoted3d

    mcoted3d

    Unity Technologies

    Joined:
    Feb 3, 2016
    Posts:
    1,003
    That could be a problem in your particular project, but as far as I can tell, the default sprite shader uses additive blending which expects premultiplied alpha.

    Similar issues could be caused by the colorspace of the project as well (gamma/linear).

    It would be nice to submit a bug report with a repro project so that we make sure to fix the right issue. Thanks!
     
  11. SamuelIH

    SamuelIH

    Joined:
    Jan 7, 2021
    Posts:
    5
    Bug report with repro project submitted. I wasn't aware I could submit package bugs through the standard 'report bug' menu, thanks for the tip!

    It's a fresh project using the standard pipeline and I observed the same issues with gamma and linear color spaces.
    'expects premultiplied alpha' This example used the default Unity UI shader, which does use premultiplied blending.
    Code (CSharp):
    1. Blend One OneMinusSrcAlpha
    2. ...
    3. fixed4 frag(v2f IN) : SV_Target {
    4. ...
    5.   color.rgb *= color.a;
    6. }
    7. ...
    I suspect the issue is that when the SVG is converted into a Texture2D that
    color.rgb *= color.a
    step is also being done, causing it to be done a total of two times: Once in the VectorGradient shader (pre-build) and then once at runtime as part of the ui shader.
     
  12. Grelle

    Grelle

    Joined:
    Oct 14, 2015
    Posts:
    15
    Hello! I very need an consultation.
    Out team supports many android devices for out projects, and some of these are old and have problem: its doesn't display semipransparent vector sprites. I mean, when you change transparency in SpriteRenderer component.
    In Editor (and new devices) its look like on gif.
    On some old devices it look like static images with alpha=1.
    Device is Samsung Galaxy Tab A8. (Also it was some iOS device some time ago; but may be we doesnt support it anemore, I will receive full info about it later).
    Please, tell me why it works like this? Does shader can be fixed for such old devices? (we use Vector Unlit)
     

    Attached Files:

  13. leviathanslop3

    leviathanslop3

    Joined:
    Nov 17, 2019
    Posts:
    1
    Hi there!
    First of all, I'm really enjoying this package. It's been very useful for my workplace program as we use a lot of SVGs.
    I had a question about recoloring aspects of the SVG. I know it's possible through getting the IDs of the SVG code, but is it possible to change SVG colors through css classes. As in, in our SVGs have styles in the code and the colors are tied to the classes. So is there a way to change colors through classes or is it only able to be done through
     
  14. Neilsonnn

    Neilsonnn

    Joined:
    Mar 29, 2016
    Posts:
    7
    looking for a little help from the team - I am loading .svg files from the web, feeding those byte arrays at runtime through the importer to get a Sprite and then later convert the Sprite to a Texture2D.

    This works in Editor but not WebGL or Standalone.

    The culprit seems to be at the very end of the process. After the sprite is successfully generated (it has a texture and can be displayed on a SpriteRenderer), I try to use the RenderSpriteToTexture2D function but end up getting a null value. It is hard to trace the nature of the error in WebGL but maybe the team has an idea.

    tex = VectorUtils.RenderSpriteToTexture2D(sprite, textureWidth, textureHeight, mat, SampleCount, expandEdges);


    Any help is appreciated.

    Unity 2021.1.21f1
    VectorGraphics Version 2.0.0-preview.18
     
    Last edited: Jun 1, 2022
  15. BinaryZERO

    BinaryZERO

    Joined:
    Jul 16, 2012
    Posts:
    5
    Hi there! I am failing to access to the pixel height (or unit height calculated with Pixels Per Unit) of my SVG asset, so I could calculate a custom pivot point to vertically set 60px above the bottom in my AssetPostprocessor.OnPreprocessTexture. Did anyone tried and succeed to do something
    similar?
    Code (CSharp):
    1.  
    2.         void OnPreprocessAsset()
    3.         {
    4.             if (assetImporter.importSettingsMissing)
    5.             {
    6.                 if (assetPath.EndsWith(".svg"))
    7.                     OnPreprocessSprite_SVG();
    8.             }
    9.         }
    10.  
    11.         void OnPreprocessSprite_SVG()
    12.         {
    13.             SVGImporter importer = (SVGImporter)assetImporter;
    14.  
    15.             importer.SvgType = SVGType.VectorSprite;
    16.             //...
    17.             importer.Alignment = VectorUtils.Alignment.Custom;
    18.             var image = AssetDatabase.LoadAssetAtPath<Texture>(assetPath);
    19.             var height = image.height;
    20.             importer.CustomPivot = new Vector2(0.5f, 60f / height);
    21.             //...
    22.         }
    Any help is appreciated.

    My Unity Version is 2020.3.17f1
    Vector Graphics Version is 2.0.0-preview.18
     
    Last edited: Apr 21, 2022
  16. Grelle

    Grelle

    Joined:
    Oct 14, 2015
    Posts:
    15
    Hm, I finally finded answer.
    I through problem was with alpha, and searching gave me nothing.
    But problem is in whole Color property in SpriteRenderer - coloring didn't work at all on this devices.
    So in was answered before in this theme - it is GPU Instancing, which is not work on some old devices. It is not supported by hardware.

    I tried to do shader, which multiply Vertex Color with custom Color. It works on old devices. I'm sure that I didn't consider all minuses of this (I am nubie in shaders), but it works.

    May be someone find this info useful.
     
    EvOne likes this.
  17. SavedByZero

    SavedByZero

    Joined:
    May 23, 2013
    Posts:
    124
    Are there clear examples of how to use BezierSegment anywhere? The Unity documentation lacks any examples of any kind, and all I can find are examples for BezierPathSegment. My issue is that Contours don't seem to accept BezierSegments....
     
  18. mcoted3d

    mcoted3d

    Unity Technologies

    Joined:
    Feb 3, 2016
    Posts:
    1,003
    BezierPathSegments are usually preferred because they guarantee that the bezier elements are properly connected. This is why BezierPathSegments are used most often.

    There are utility methods to convert a BezierSegment, or an array of BezierSegments into a BezierPathSegment:

    VectorUtils.BezierSegmentToPath
    https://docs.unity3d.com/Packages/c...entToPath_Unity_VectorGraphics_BezierSegment_

    VectorUtils.BezierSegmentsToPath

    https://docs.unity3d.com/Packages/c...sToPath_Unity_VectorGraphics_BezierSegment___
     
    SavedByZero likes this.
  19. SavedByZero

    SavedByZero

    Joined:
    May 23, 2013
    Posts:
    124
    Is this a mistake? I'm seeing this in the 2.0.0 documentation ('new GradientFillStop() when it should be new GradientStop()):
    Code (CSharp):
    1. var fill = new GradientFill() {
    2.     Type = GradientFillType.Linear,
    3.     Stops = new GradientFillStop[] {
    4.         new GradientFillStop() { Color = Color.blue, StopPercentage = 0.0f },
    5.         new GradientFillStop() { Color = Color.red, StopPercentage = 0.5f },
    6.         new GradientFillStop() { Color = Color.yellow, StopPercentage = 1.0f }
    7.     }
    8. };
    Either way, my .4 unit sized stroke (roughly 40 pixels) is coming out solid white, no matter what fill gradient I apply to it (even if I change that to be all black) -- any reason? (the documentation *says* you can apply a fill to a stroke...)

    EDIT: Linking back to earlier possible solution:
    https://forum.unity.com/threads/vector-graphics-preview-package.529845/page-19#post-5738677
     
    Last edited: May 3, 2022
  20. SavedByZero

    SavedByZero

    Joined:
    May 23, 2013
    Posts:
    124
    This one I'm less likely to self-solve and don't see anything in this thread quite like it, so: why is it that when I try to load a predefined png / texture for the material I use for my stroke, the stroke only shows one color at a time from the texture instead of the full pattern? I've got a fairly big stroke, between .4 and 2 units, so it should be able to encompass a simple 40px*40px four-color pattern with three shades of blue and one black line at the end...I'd think?

    Is there a different way to do this? PatternFill has practically no documentation around it, and the properties don't seem to include a texture or material. The squares pattern in the example page with the "clipping" demonstration is just kind of there, with no specific code showing how it was made.
     
    Last edited: May 4, 2022
  21. wedgiebee

    wedgiebee

    Joined:
    Aug 9, 2016
    Posts:
    40
    Is there currently/will there soon be a way to use this package with ShaderGraph (not counting using it to generate a TexturedSprite)?

    There are 2 effects I'm trying to achieve:

    1) Replacing a specific solid color with an object-space texture that can be changed at runtime. (For my sprite shader graph I'm using the ReplaceColor node)

    2) A thick outline around the silhouette of the vector image. (For my sprite shader graph I'm doing the "make 8 duplicates and offset them" trick)
     
  22. BinaryZERO

    BinaryZERO

    Joined:
    Jul 16, 2012
    Posts:
    5
    If anyone would require something similar, I guess I solved how to get the height and update the custom pivot during the import process in AssetPostprocessor.OnPreprocessAsset... My solution is a bit hacky but it is definitely working:

    Code (CSharp):
    1.     void OnPreprocessAsset()
    2.     {
    3.         if (assetPath.EndsWith(".svg"))
    4.         {
    5.             SVGImporter importer = (SVGImporter)assetImporter;
    6.             if (importer != null && importer.importSettingsMissing)
    7.             {
    8.                 Debug.Log(string.Format("Importing SVG Asset '{0}'...", assetPath));
    9.                 importer.SvgType = SVGType.VectorSprite;
    10.                 importer.ViewportOptions = ViewportOptions.PreserveViewport;
    11.                 //...
    12.                 importer.Alignment = VectorUtils.Alignment.Custom;
    13.  
    14.                 var svgContext = System.IO.File.ReadAllText(assetPath);
    15.                 var match = new System.Text.RegularExpressions.Regex(@"(?<=height="")\d{1,4}(?=px"")").Match(svgContext);
    16.                 int height = 256;
    17.                 if (match.Success)
    18.                     int.TryParse(match.Value, out height);
    19.                 importer.CustomPivot = new Vector2(0.5f, 60f / height);
    20.                 Debug.Log(string.Format("...Height: {0}px...", height));
    21.             }
    22.         }
    23.     }
     
  23. enhawk

    enhawk

    Joined:
    Aug 22, 2013
    Posts:
    833
    Any chance of Vector Graphics being bumped up from "experimental"?

    I've not had any issues with Vector Graphics since Unity 2018 and it was a real head scratcher to enable in Unity 2020.3 ... wild goose chase to find the package.

    Thanks as always for the sterling work, I use SVGs in all kinds of projects now, from VR to Pixel art games and everything inbetween.
     
    Last edited: May 21, 2022
  24. jGate99

    jGate99

    Joined:
    Oct 22, 2013
    Posts:
    1,945
    Hi there,

    Lets suppose I have SVG assets and i want to change colors in those assets to white so they become tintable (currently they are in black)
    so I was thinking there must be some api that let me process those svg files and save them back to white

    please advise
     
  25. Neilsonnn

    Neilsonnn

    Joined:
    Mar 29, 2016
    Posts:
    7

    Following up on this, can anyone on the team help explain why VectorUtils.RenderSpriteToTexture2D() returns null in Standalone and WebGL builds?
     
  26. lGillot

    lGillot

    Joined:
    Apr 11, 2013
    Posts:
    16
    Hi every one.
    First of all, thank you for all the good infos i got from here.

    Is there a way to generate a "textured Sprite" via script? At runtime or in editor. I'd like to force the width of all the strokes to stay consistent regardless of the scale of the gameobject.
    I managed to do it by generating a mesh only for the strokes but I wonder if i could do it another way.
    Anyway, i'm still interested in generating a textured sprite by script...
     
  27. Elmstrom

    Elmstrom

    Joined:
    May 8, 2015
    Posts:
    10
    I want to raycast a UI SVG image, but only the non-transparent parts. If it was a normally UI image I would set GetComponent<Image>().alphaHitTestMinimumThreshold = 0.1f;
    How do I accomplish the same with UI SVG?
     
  28. brunoma

    brunoma

    Joined:
    Mar 10, 2015
    Posts:
    4
    I seem to be having an issue with svg's that have "sub" svg's inside. They are valid and work fine in a browser. Does the vector package not support this? I've attached an example
     

    Attached Files:

  29. cmatias

    cmatias

    Joined:
    Feb 7, 2014
    Posts:
    7
    Hi there.
    Complete newbee question here:
    How can I access the SceneInfo property of an instance of a SVG file that was drag-dropped to the scene ? (making it a prefab instance)

    I need to read the Scene Node "Children" list of this object from a script.

    I start with the usual GameObject variable definition :
    Code (CSharp):
    1. [SerializeField]
    2. public GameObject mySVGGameObject;
    I drag/drop the scene instance of my imported SVG file object in the inspector to set the mySVGGameObject ,
    And then what ???... I'm lost.

    I've been trying to define the object as an SVGImage or an SVGImporter, but can't drop my prefab on those fields.

    I could import the svg file at runtime, but it is much more confortable for me to see it in the editor viewport.

    Any help would be greatly appreciated.
    C. MATIAS
     

    Attached Files:

    Last edited: Jul 8, 2022
  30. brunoma

    brunoma

    Joined:
    Mar 10, 2015
    Posts:
    4
    What's the state of this anyways? Is there a team still working on this? Is anyone even looking at these forums? I heard there were a bunch of layoffs at Unity so it would be good to know if it has affected the development of some features
     
    EvOne likes this.
  31. benoitd_unity

    benoitd_unity

    Unity Technologies

    Joined:
    Jan 2, 2018
    Posts:
    331
    Hi folks, we wanted to share a long overdue update about the Vector Graphics package.

    First of all, huge thanks to everyone who took the time to provide feedback, report issues and share their excitement with us. It has been super valuable for understanding how you’d use vector graphics in your projects, and how we could improve the feature set. As always it’s a great source of inspiration and motivation for us.

    Unfortunately, we took the difficult decision to pause all development activities around the Vector Graphics package. Even though we see the value of providing scalable vector graphics, we decided to give priority to other projects, and hope to consider resuming development in the future.

    Meanwhile, the package will remain available in its current Experimental state, to be used as is, or modified to your liking by embedding it in your project. I also want to highlight @mcoted3d’s dedication for continuously taking some of his spare time to follow up on this thread, and improving the package when he had a moment.

    This thread will remain open for discussions and keeping in touch with you all, but I highly encourage you to use our official product roadmap to share feedback, vote on what is important to you, and stay informed on our development priorities.


    Cheers,
     
    enhawk and Thaina like this.
  32. Carpet_Head

    Carpet_Head

    Joined:
    Nov 27, 2014
    Posts:
    258
    Thank you very much for the update. I personally think that this package is very important to our workflow, so will be heading straight to the roadmap to vote on the issue - but I have no idea how much value it has for others
     
    benoitd_unity and Aterion like this.
  33. kierenj

    kierenj

    Joined:
    Jul 7, 2013
    Posts:
    13
    Hi - I'd like to suggest a feature/bug fix for the package. I see above it's in theory not a priority, however checking the "needle-mirror" git mirror, I can see some updates recently (as recent as 3 days ago).

    Basically, VectorSprite.RenderSprite looks to normalise all vertex coordinates - but if the destination aspect ratio is different than the scene - the output is stretched.

    Is there a good place to make this request/recommendation, or to discuss other options @benoitd_unity ?


    For reference, the fix for resizing and maintaining aspect ratio I have here -

    https://github.com/needle-mirror/co...ter...kierenj:com.unity.vectorgraphics:master
     
    Last edited: Aug 8, 2022
  34. kierenj

    kierenj

    Joined:
    Jul 7, 2013
    Posts:
    13
    (I also have an SVG with radial gradient which fails to render correctly - fine in browsers but not here)
     
  35. enhawk

    enhawk

    Joined:
    Aug 22, 2013
    Posts:
    833
    @mcoted3d @benoitd_unity Thanks for the vector graphics package. I actually published an App using this framework, yay! I hope you can keep it around for bit, but totally understand if you need to retire it.

    Facemakr, iOS

    https://apps.apple.com/app/facemakr/id365878157

    Features scaling, rotating etc, recoloring, and lots of other fancy rendering, I think I used near every part of the API :D Well, if you want to see your hard work in an app that is mostly about the Vector Graphics package, please take a look!

     
    Last edited: Aug 10, 2022
    tosiabunio, Thaina and mcoted3d like this.
  36. Neilsonnn

    Neilsonnn

    Joined:
    Mar 29, 2016
    Posts:
    7
    Found the fix for those that want to do SVG import at realtime in builds. Import the shader VectorExpandEdges.shader into your Resources folder and then load it into the Texture2D RenderSpriteToTexture2D() function.

    Else this function will return null.
     
    KamilCSPS and Thaina like this.
  37. QuoVadisBLR

    QuoVadisBLR

    Joined:
    Jan 26, 2015
    Posts:
    1
    Hello.
    Gradient meshes are not supported in the importer?
    Inkscape:

    Unity:
     
  38. mcoted3d

    mcoted3d

    Unity Technologies

    Joined:
    Feb 3, 2016
    Posts:
    1,003
    No, only linear and radial gradients.
     
    QuoVadisBLR likes this.
  39. mcoted3d

    mcoted3d

    Unity Technologies

    Joined:
    Feb 3, 2016
    Posts:
    1,003
    This should work. I was able to make it work with a MonoBehaviour attached to a GameObject which has a SpriteRenderer component.

    Code (CSharp):
    1. using System.IO;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using Unity.VectorGraphics;
    6.  
    7. [RequireComponent(typeof(SpriteRenderer))]
    8. public class Loader : MonoBehaviour
    9. {
    10.     void Start()
    11.     {
    12.         string svgText = File.ReadAllText("Assets/rect.svg");
    13.         Debug.Log($"SVG: {svgText}");
    14.         SVGParser.SceneInfo sceneInfo;
    15.         using (var reader = new StringReader(svgText))
    16.         {
    17.             sceneInfo = SVGParser.ImportSVG(reader);
    18.         }
    19.         var vto = new VectorUtils.TessellationOptions()
    20.         {
    21.             StepDistance = 10f,
    22.             MaxCordDeviation = 0.5f,
    23.             MaxTanAngleDeviation = 0.1f,
    24.             SamplingStepSize = 0.01f
    25.         };
    26.         var geoms = VectorUtils.TessellateScene(sceneInfo.Scene, vto);
    27.         var sprite = VectorUtils.BuildSprite(geoms, 10.0f, VectorUtils.Alignment.Center, Vector2.zero, 128, true);
    28.         GetComponent<SpriteRenderer>().sprite = sprite;
    29.     }
    30. }
    31.  
    In your example, make sure that every step works along the way: that you get a valid SVG string, that the sceneInfo isn't empty, etc.
     
  40. mcoted3d

    mcoted3d

    Unity Technologies

    Joined:
    Feb 3, 2016
    Posts:
    1,003
    Yes, but you'll have to use an SVG Image component instead of UI Image, which are designed for textured sprites.
     
    squigglebucket likes this.
  41. tjh201

    tjh201

    Joined:
    Apr 4, 2014
    Posts:
    2
    Hi,

    I have been using the vector graphics package to programmatically draw a 2D map view as an svg based on terrain and biome vector data. I am looking to add a border around it, but am struggling to get the dash stroke working. The effect I am looking for is this, equal sized dark and light dashes in a line: upload_2022-10-6_17-8-21.png

    There is not much documentation around the stroke properties, when I try set a similar black and white dash pattern to 4px or 4 4, it comes out with a dash that varies in length like this:

    upload_2022-10-6_17-16-42.png

    Code to produce:
    dashedLine.PathProps.Stroke.Pattern = new float[] { 4 };

    Can I get a bit more info on how to use the dash properties correctly?

    Cheers!
     
  42. geo_p

    geo_p

    Joined:
    Jul 24, 2012
    Posts:
    5
    Hello guys! I love working with this package and am very sad it's no longer in active development.

    I just thought I'd share a fix I made in the VectorUtils class in case anyone encounters the same problem as me.

    Basically in trying to obtain the bounding box of a shape using the Bounds function (and consequently SceneNodeBounds function), the bounds of curved beziers are not properly considered.

    This is because there is a typo in the SolveQuadratic function:
    Code (CSharp):
    1. static void SolveQuadratic(float a, float b, float c, out float s1, out float s2)
    2. {
    3.     float det = b * b - 4.0f * a * c;
    4.     if (det < 0.0f)
    5.     {
    6.         s1 = s2 = float.NaN;
    7.         return;
    8.     }
    9.  
    10.     float detSqrt = Mathf.Sqrt(det);
    11.     s1 = (-b + detSqrt) / 2.0f * a; // typo - denominator needs brackets
    12.     if (Mathf.Abs(a) > float.Epsilon)
    13.         s2 = (-b - detSqrt) / 2.0f * a; // typo - denominator needs brackets
    14.     else s2 = float.NaN;
    15. }
    If you update the final section to add brackets as below, the bounds function will include bezier curves correctly:
    Code (CSharp):
    1.     float detSqrt = Mathf.Sqrt(det);
    2.     s1 = (-b + detSqrt) / (2.0f * a);
    3.     if (Mathf.Abs(a) > float.Epsilon)
    4.         s2 = (-b - detSqrt) / (2.0f * a);
    5.     else s2 = float.NaN;
    Hope this helps someone :)
     
    squigglebucket and EvOne like this.
  43. mcoted3d

    mcoted3d

    Unity Technologies

    Joined:
    Feb 3, 2016
    Posts:
    1,003
    I think you got it right, but 4 might be too small and seems to cause a weird moire pattern. I would play around with larger values (20+) and see how it goes.
     
  44. mcoted3d

    mcoted3d

    Unity Technologies

    Joined:
    Feb 3, 2016
    Posts:
    1,003
    Thank you! I'll try to get that fixed in the next release.
     
    geo_p, squigglebucket and jGate99 like this.
  45. squigglebucket

    squigglebucket

    Joined:
    Feb 19, 2016
    Posts:
    14
    Anyone know why I'm having trouble changing some SVGImporter settings with an Editor script?

    Code (CSharp):
    1.         string filepath = OutputFolderPath + "/" + fileName + ".svg";
    2.  
    3.         System.IO.File.WriteAllText(filepath, text);
    4.  
    5.         AssetDatabase.Refresh();
    6.  
    7.         SVGImporter importer            = (SVGImporter)AssetImporter.GetAtPath(filepath);
    8.  
    9.         Debug.Log(importer);
    10.  
    11.         importer.ViewportOptions        = Unity.VectorGraphics.ViewportOptions.PreserveViewport;
    12.         importer.GeneratePhysicsShape   = true;
    13.         importer.AdvancedMode           = true;
    14.         importer.StepDistance           = 1f;
    15.  
    16.         importer.SaveAndReimport();
    It creates my SVG files and grabs the importer without issue, but when I set importer settings they aren't saved properly.

    If I call importer.SaveAndReimport(); like I'm doing in the code above, it just resets the importer to the default options. If I don't call SaveAndReimport it will actually save the changes to the importer's inspector view, but they aren't actually applied when I add the svg to scenes.

    Not sure if it's something up with the SVGImporter or if I'm just using it incorrectly. Can't seem to find much on changing AssetImporter settings in the general API Documentation, and definitely nothing in the Vector Graphics package documentation.
     
  46. mcoted3d

    mcoted3d

    Unity Technologies

    Joined:
    Feb 3, 2016
    Posts:
    1,003
    It seems the importer isn't marked as "dirty", so the SaveAndReimport doesn't do anything. It may be a bug on the SVGImporter, but you can fix it on your side by calling
    EditorUtility.SetDirty(importer);
    just before the
    SaveAndReimport()
    call.

    Hope this helps.

    EDIT: You may get issues with the editor asking you if you want to save the settings after clicking on the asset. If so, you may want to give a try with the OnPreprocessAsset callback:
    https://docs.unity3d.com/ScriptReference/AssetPostprocessor.OnPreprocessAsset.html
     
    Last edited: Nov 14, 2022
    EvOne likes this.
  47. squigglebucket

    squigglebucket

    Joined:
    Feb 19, 2016
    Posts:
    14
    The .SetDirty call did the trick. Thank you!
     
  48. squigglebucket

    squigglebucket

    Joined:
    Feb 19, 2016
    Posts:
    14
    Is there a setting on an SVGImage that mimics the normal Image's alphaHitTestMinimumThreshold property?

    I just need to register clicks on vector graphics, and I am currently using PolygonCollider2Ds. It's a pain though because the physics shapes generated are.... bad for vector graphics so I"m spending a lot of time manually creating the colliders

    I would love to just use UI elements and the "Maintain Viewport" option to accurately place elements on import, but I can't figure out a way to accurately get an "OnClick" on them as UI elements.
     
  49. Deibu

    Deibu

    Joined:
    Feb 24, 2013
    Posts:
    34
    Is there any ETA on when text elements will be supported?
     
    JamesArndt likes this.
  50. Bliggfang

    Bliggfang

    Joined:
    Sep 8, 2021
    Posts:
    9
    My whole project builds on the Vector Graphics Package. So far the package is doing a great job and I'm very glad it exists! :)
    But now I encounter a big problem! I am creating procedurally generated (celestial) objects for a space exploration game. I have already been able to optimize the creation process very well. But now there is a fatal bottleneck that I don't know how to avoid. The TessellateScene() function!
    My procedurally generated objects are just simple Shapes, each with a single triangle (BezierContour with 3 BezierPathSegments) and a Fill. From just a few hundred of these triangle shapes, the tessellation takes so long that significant frame drops (>100 ms) occur.
    One possibility would be to outsource the tessellation to a job or a parallel async/await task. This works quite well, but unfortunately cannot be used in a WebGL application (which the project is designed for).
    So my question: is there any other option to speed up the tessellation or some kind of workaround like maybe I can split the tessellation into multiple frames or something else? I would be VERY GRATEFUL for any help! :rolleyes:
     
    tosiabunio likes this.