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’re making changes to the Unity Runtime Fee pricing policy that we announced on September 12th. Access our latest thread for more information!
    Dismiss Notice
  3. Dismiss Notice

Official 2D Sprite Shape is out of preview for 2019.3

Discussion in '2D' started by rustum, Jan 30, 2020.

  1. Venkify

    Venkify

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    615
    A closed shape can only be defined by a single path. What you are trying to achieve is creating a closed shape with multiple (2) paths which is not possible. However the desired end-result can definitely be achieved using NULL variant. This allows a Edge to not use any Sprite (thereby edges are not rendered) at all.
    Please check the enclosed demo. Thanks. NullVariant.gif
     
  2. BACALL

    BACALL

    Joined:
    Feb 21, 2017
    Posts:
    79
    Thanks for the quick reply! Doesn't work with this setup (see attached pictures). The variant doesn't show up in the inspector? Only when I click some points (the ones in the upper left corner)?

    inspector.png spriteshape.png

    Edit: I found why, I'd have to add a null/none sprite to every angle. But now my inner corner looks weird where the seem is? See attached picture "inner corner".

    inner-corner.png

    Do you think the "inverse" option could work by creating an enclosing box around the drawn shape like in this picture below. (And maybe it would be a lot more user-friendly for level designers than the steps above) I've seen this in assets before, but they are all obsolete now.

    Screenshot 2021-03-01 at 21.55.48.png
     
  3. jrmgx

    jrmgx

    Joined:
    Oct 21, 2016
    Posts:
    41
    Thank you I understand now, but what about the missing fill texture under the corner?
    Still using the same configuration as above (https://forum.unity.com/threads/2d-...preview-for-2019-3.818799/page-3#post-6870950)
     
  4. n-shalimov

    n-shalimov

    Joined:
    Dec 20, 2017
    Posts:
    1
    Hi! First of all thank you for this helpful package. It saved me from having to do a bunch of things myself.

    I have a case where I need to rebuild a shape at runtime, sometimes every frame. Moreover, the number of points in the spline can be different.
    1. I looked at the package code and couldn't find a reason why SplineControlPoint has reference type. Changing it to value type would help reduce the number of memory allocations for high dynamic use cases.
    2. The situation when two adjacent control points have a close position is considered an error. But why not weld them together with degeneration of inner tangents?
     
    Last edited: Mar 12, 2021
  5. Npicouet

    Npicouet

    Joined:
    May 17, 2018
    Posts:
    27
    Is there a way to force render the sprite shape? Kinda like a SkinnedMesh "Always Render".

    I'm getting issues where the sprite shape stops rendering after it leaves camera. The user can "fly" up above the terrain and it causes the sprite shape to leave the camera for a bit. When the user falls back down, the sprite shape stops rendering.
     
  6. enhawk

    enhawk

    Joined:
    Aug 22, 2013
    Posts:
    829
    @Venkify is it possible to create the spline for sprite shape from a composite collider?

    I have a bunch of polygon colliders, merging them with a composite collider, and wanted to outline the result with a sprite shape.
     
  7. nicmarxp

    nicmarxp

    Joined:
    Dec 3, 2017
    Posts:
    403
    Can’t you loop through each point of the composite collider and add that to a sprite shape?
     
  8. enhawk

    enhawk

    Joined:
    Aug 22, 2013
    Posts:
    829
    is it a case of simply copying over a List<Vertex2> or is there a lot to it? Just wondering if theres already a script for this, making a shape from an existing shape seems like a common task
     
  9. nicmarxp

    nicmarxp

    Joined:
    Dec 3, 2017
    Posts:
    403
    enhawk likes this.
  10. enhawk

    enhawk

    Joined:
    Aug 22, 2013
    Posts:
    829
  11. enhawk

    enhawk

    Joined:
    Aug 22, 2013
    Posts:
    829
    Last edited: May 25, 2021
  12. enhawk

    enhawk

    Joined:
    Aug 22, 2013
    Posts:
    829
    two more feature ideas...

    1. please make it possible to use the Sprite Shape Renderer as a Sprite Mask or,
    2. please make it possible to have convex sprite shapes. (holes)

    I'm running into these two road blocks on a top down game, it's awkward to use sprite shape as organic shaped walls... it seems to be only designed with platformers in mind... I want the fill on the outside and the path sprites on the inside (as mentioned a few times in this thread, the fix at the top of the page is no good for me as the walls are randomly generated)

    Being able to use a Sprite Shape Renderer as a mask would instantly solve this problem as I could just punch a hole in a sprite and use the same shape on another object to draw the profile
     
    Last edited: May 25, 2021
    RemDust likes this.
  13. enhawk

    enhawk

    Joined:
    Aug 22, 2013
    Posts:
    829
    @Venkify I get an error when trying to dynamically make a shape, sometimes I get this:

    ArgumentException: Internal error: Point too close to neighbor
    UnityEngine.U2D.Spline.InsertPointAt (System.Int32 index, UnityEngine.Vector3 point) (at Library/PackageCache/com.unity.2d.spriteshape@3.0.15/Runtime/Spline.cs:60)

    This would be a major problem in a game, as it crashes the playloop. Instead of crashing because points are too close, could they just be skipped or merged instead?

    I can check my points that I am feeding it, however how close is "too close"?
     
  14. enhawk

    enhawk

    Joined:
    Aug 22, 2013
    Posts:
    829
    So I figured out a pipeline for doing inverse sections, it's not an extension for Sprite Shape but rather a work around:

    1. fill your "inverse" sprite shape with some color
    2. set it to a unique layer
    3. add a new camera and set it to only view that new layer
    4. render the cameras output to a render texture
    5. in code, use the rendertexture and generate a new sprite from it using tex2d
    6. apply that sprite in the same position as your sprite shape on a new sprite mask
    7. make another tiling sprite using the fill sprite you want as your wall, set it to draw only outside the mask, or set where you want the "hole" in another sprite shape and set that to only draw outside the spritemask.
    8. make a duplicate of your sprite shape and invert the path tiles
    9. run scene :)
    I'm doing this at runtime and works perfectly, you could probably get it to run in edit mode too.
     
  15. Gamingbir

    Gamingbir

    Joined:
    Apr 1, 2014
    Posts:
    191
    BACALL, NotaNaN, indie6 and 1 other person like this.
  16. indie6

    indie6

    Joined:
    Dec 15, 2012
    Posts:
    100
    Hi,

    I am using 2D Sprite Shape 5.1.2 in my game, Unity version 202.3.7f1

    The sprite sprites are dynamic, which are composed of 2D physics rigidbodies.. and are modified at runtime

    I have fill tessellation enabled, since it optimizes the CPU greatly and gives me a good 60FPS on low-end devices. My SpriteShapes fulfil the criteria for "Fill Tessellation" which is
    1 - Dynamic sprite Shape
    2 - No overlapping edges or duplicate points

    Yet, the game gets stuck randomly on all the platforms, be it Unity editor, Android or iOS device. And there are no crash logs. To me it seems like the Fill Tessellation Jobs run into some "race conditions"

    Could it be that I am modifying the shape in "Update" method, which causes this issue? Should they be done in "FixedUpdate" because of physics or LateUpdate?
    Or do I need to call some method after modifying the shape? This is how I can updating the shape

    Code (CSharp):
    1. private void Update()
    2. {
    3.         int totalPoints = ShapeSpline.GetPointCount();
    4.         for (int i = 0; i < totalPoints; i++)
    5.         {
    6.             ShapeSpline.SetPosition(i, edges[i].transform.localPosition);
    7.         }
    8. }
    Thank you and looking forward to your reply
     
  17. Venkify

    Venkify

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    615
    I would suggest adding it on LateUpdate. Could you also please submit a bug report with a simple repro project please ? That will be very helpful in analyzing the issue. Will take a look asap. Thanks.
     
    indie6 likes this.
  18. indie6

    indie6

    Joined:
    Dec 15, 2012
    Posts:
    100
    Hey thank you for the quick reply, I will try this and see how it works. I will try to file the bug sometime next week since I need to strip out this part from the project
     
  19. indie6

    indie6

    Joined:
    Dec 15, 2012
    Posts:
    100
    Hi, first of all sorry I haven't yet made the sample demo the issue, but I opted on not using the Fill Tessellation option.
    While the game works fine, I am running into another issue. I get the follow exception

    InvalidOperationException: The previously scheduled job SpriteShapeGenerator writes to the Unity.Collections.NativeArray

    Maybe this is the problem here?
    https://forum.unity.com/threads/the...-call-jobhandle-complete.660160/#post-4419259

    Thank you and looking forward to your reply.
     
  20. indie6

    indie6

    Joined:
    Dec 15, 2012
    Posts:
    100
    Hey were you able to find a solution for the exterior & interior image?
     
    RemDust likes this.
  21. enhawk

    enhawk

    Joined:
    Aug 22, 2013
    Posts:
    829
    see my solution above.
     
  22. midsummer

    midsummer

    Joined:
    Jan 12, 2017
    Posts:
    33
    Why doesn't Sprite Shape respect the editor's grid snap toggle?

    In my project I found that I need to snap practically every point in every shape to the grid. There's a lot of shapes in a level, as you can imagine. This is currently a very, very painful process:

    Every time I select a shape for editing I have to click the Snapping checkbox (which always resets back to unchecked whenever a shape is selected). I have a 5K display. The checkbox is tiny. I have to do this to every shape every time I want to change something.

    Sprite Shape goes through the trouble of disabling the global grid snapping option when the shape editing button is clicked. The setting is then very politely turned back on when the user exits shape edit mode.

    In my opinion the snapping toggle should not be in the inspector in the first place. It's not a persistent per-object setting, the checkbox is only visible while the shape is being edited in the scene. Moving points and snapping them to the grid should be considered tool-level functionality that should respect the settings of the editor's grid snapping settings, as one would expect.

    Sprite Shape does use the unit increments specified in the editor's grid options for snapping, it just overrides the current state of snapping and decides that it should be off, always. Is this just a bug?

    I tried to hack a custom keyboard shortcut for checking the snapping option in the inspector, but the logic is buried so deep in the code that I gave up on it... I did discover that there's a few handy undocumented(?) keyboard shortcuts for Sprite Shape, though (search for spriteshape in Shortcuts to see these). Alas, no shortcut for snapping.
     
    indie6 and RemDust like this.
  23. Venkify

    Venkify

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    615
    Agree especially when Snapping is heavily used.. We are working on this and will be posting an update soon. Will update this thread too. Sorry for the inconvenience caused.
     
    MousePods and tosiabunio like this.
  24. midsummer

    midsummer

    Joined:
    Jan 12, 2017
    Posts:
    33
    This is great news, much appreciated!
     
  25. indie6

    indie6

    Joined:
    Dec 15, 2012
    Posts:
    100
    @Venkify any plans on adding this feature? Other terrain tools like Ferr2D and Spline Terrain 2D have this

    Possible to do with the workaround mentioned, but that is just extra work and processing
     
    MousePods likes this.
  26. BACALL

    BACALL

    Joined:
    Feb 21, 2017
    Posts:
    79
    Sorry for the late response, me and my team are still struggling with interiors/inverted shapes (since start of this year). We are creating rooms with venkifys workaround below (but hoping every month there comes a fix/better solution. I still don't get why Unity won't add a checkbox to invert shapes):

     
    RemDust and indie6 like this.
  27. indie6

    indie6

    Joined:
    Dec 15, 2012
    Posts:
    100
    Hi! Another feature request..

    Can you add an option to re-align pivot to at least center? While creating a shape, almost all the time the pivot goes way off
     
    Last edited: Sep 11, 2021
    RemDust likes this.
  28. MousePods

    MousePods

    Joined:
    Jul 19, 2012
    Posts:
    737
    This would be great! In the meantime, I do have this script that works pretty well.... It doesn't keep the terrain in the same place but it does center the pivot.

    Just add this script to your Editor folder and use it like this:



    Hope it works!
     

    Attached Files:

    indie6 and RemDust like this.
  29. RemDust

    RemDust

    Joined:
    Aug 28, 2015
    Posts:
    431
    Thank you so much for this !
    The pivot points were driving me insane for days xD
     
    MousePods likes this.
  30. indie6

    indie6

    Joined:
    Dec 15, 2012
    Posts:
    100
    Thanks, works well!
     
    MousePods likes this.
  31. KarlKarl2000

    KarlKarl2000

    Joined:
    Jan 25, 2016
    Posts:
    588
    [ Update ]
    I was curious and did a performance test to see how far I could take sprite shapes before the fps took a hit.

    In no way is this definitive, only an extreme test on my end to see what kind of performance is affected. Thought I'd share the results. In the end, despite the deceptively simple ease of use and results, sprite shape can be a bit taxing on the fps, if abused. :D

    The original goal was to see if I could make the entire level with sprite shapes, because of sprite shapes nice curved profiles. (Foreground, middle ground, background and sky\clouds) In the end perhaps it'll be a combination of planes and limited sprite shapes.

    Obviously the editor loop will affect the performance too.

    Also I'm not sure if dynamically disabling\ enabling out-of-camera sprite shapes would have improved performance. Didn't see that type of option in the sprite shape settings.

    [ Test scenario ]
    Empty scene with camera and closed sprite shapes or planes.

    About 800 closed sprite shapes
    - 15 fps average
    - max time ms 18.50
    Illustration2.jpg

    About 1000 planes
    - 200 fps average
    - max time ms 2.69

    sdfds.jpg

    [ Update End ]



    Is there a way to dynamically occlude the sprite shapes when out of camera view? Kind of like how mesh renderers have the checkbox to dynamically occlude the mesh.
     
    Last edited: Nov 15, 2021
  32. OlderFox

    OlderFox

    Joined:
    Sep 18, 2022
    Posts:
    3
    Last edited: Oct 9, 2022
  33. samanabo

    samanabo

    Joined:
    Mar 10, 2015
    Posts:
    48
    Is it possible to offset only the collider for a spriteshape, on a single edge?
    For example if I want my collider offset to be zero, except for the bottom edge which should be 0.5f?
    I'd also like this offset to be applied only to the collider, and not to the sprite for the bottom edge.

    I know you can set an edge sprite's pivot point which will adjust both the position of the sprite, and the position of the collider edge. But in this instance I would like to just modify the collider, and keep the sprite's pivot at zero.

    The attached image shows a spriteshape where I'd like the bottom edge to extend all the way to the bottom of the sprite, but keep the sprite itself for the bottom edge positioned where it is now.
     

    Attached Files:

  34. pastaluego

    pastaluego

    Joined:
    Mar 30, 2017
    Posts:
    192
    It would be amazing if the SpriteShape mesh could be used with SpriteMask to define the shape of the mask rather than needing a custom sprite for every mask you want to have.
     
  35. Venkify

    Venkify

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    615
    This feature is available in 2023.1
    Thanks.
     
    pastaluego likes this.