Search Unity

Official Tell us about your experience with Sprite Shape

Discussion in '2D' started by rustum, May 29, 2019.

Thread Status:
Not open for further replies.
  1. RedHillbilly

    RedHillbilly

    Joined:
    Mar 24, 2014
    Posts:
    39
    @Venkify

    Thanks for the solution. The drawback is that this only works if the shape is visible from the start, and not if the camera moves towards the shape, or the shape towards the camera in time. In this case you need to uncheck
    DestroyOnCompletion
    . But this seems to generate a lot of overhead, as each time
    OnGUI
    is called the script looks for all shape controllers in the scene.

    One solution I found is to modified the script so that you can put it on the object with the shape controller:
    Code (CSharp):
    1. private SpriteShapeController _spriteShapeController;
    2. private SpriteShapeRenderer _spriteShapeRenderer;
    3.  
    4. private void Awake()
    5. {
    6.     _spriteShapeController = GetComponent<SpriteShapeController>();
    7.     _spriteShapeRenderer = GetComponent<SpriteShapeRenderer>();
    8. }
    9. void OnGUI()
    10. {
    11.     // Loop invisible SpriteShapeRenderers and generate geometry.
    12.     if (_spriteShapeRenderer != null && _spriteShapeController != null)
    13.     {
    14.         if (!_spriteShapeRenderer.isVisible)
    15.         {
    16.             CommandBuffer rc = new CommandBuffer();
    17.             rc.GetTemporaryRT(0, 256, 256, 0);
    18.             rc.SetRenderTarget(0);      
    19.          
    20.             _spriteShapeController.BakeMesh();
    21.             rc.DrawRenderer(_spriteShapeRenderer, _spriteShapeRenderer.sharedMaterial);
    22.  
    23.             rc.ReleaseTemporaryRT(0);
    24.             Graphics.ExecuteCommandBuffer(rc);
    25.             // Debug.Log("generating shape for " + spriteShapeRenderer.gameObject.name);
    26.         }
    27.     }
    28. }
    But this still seems very silly. I think there should be an option to force the shape, or to set a bounding box on the shape which checks if it overlaps with the camera.
     
  2. Venkify

    Venkify

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    644
    @RedHillbilly Please submit a bug report. There have been a few more instances where its been reported. Agree that this should be handled better. Will take a look asap. Thanks.
     
  3. MassiveMonsterGames

    MassiveMonsterGames

    Joined:
    Aug 4, 2016
    Posts:
    12
    Been having some trouble with Sprite Shapes

    1. how do I set a edge material through code?
    2. We are geting issues of spriteshapes not rendering when they are meant to (Sorted through RedHillBilly's problem)
    3. Performance seems very bad on Nintendo Switch
     
    Last edited: Nov 4, 2020
  4. RedHillbilly

    RedHillbilly

    Joined:
    Mar 24, 2014
    Posts:
    39
    Thanks a lot for the answer.

    I have to correct my previous statement, the solution is needed if the shape changes in time and slowly enters the camera field of view, not if the camera or the object moves, so I am guessing this is intended behavior, as most object shapes won't change in time?

    I can still report a bug if you want, but I am not sure it is one.
     
  5. PhantomFox128

    PhantomFox128

    Joined:
    May 22, 2019
    Posts:
    157
    Is there a sort "best practice" for setting up colliders properly or some feature planned that creates colliders automatically similar to Ferr2D? Right now, it seems like the best way to do things is to constantly adjust the colliders manually, which is really time consuming and kind of a pain.
     
    RemDust likes this.
  6. MagicianArtemka

    MagicianArtemka

    Joined:
    Jan 15, 2019
    Posts:
    46
    Hi. Do we have any way to use Sprite Shapes with the DOTS?

    I'm very interested in converting sprite shape into an entity - for now Unity cannot convert Sprite Shape Renderer automatically. For common sprites, I use Quad mesh. Can I use this trick to render a sprite shape with the HybridRenderer?
     
  7. nabergh

    nabergh

    Joined:
    Jun 22, 2019
    Posts:
    7
    First of all, thanks for creating this cool package!

    I'm using Sprite Shape and would like to know if it's possible to render the lower index spline points above the higher index spline points. Alternatively, I'm wondering if it's possible for the sprite to start tiling at the lower indices instead of the higher indices when the spline is extended.

    For some context, I'm making a game with a snake character where I want the head to render on top of the tail if they overlap. I also want the snake's skin to repeat starting at the tail when it grows or shrinks. If I want the former to be true, then the head needs to have a lower spline point index, but then this would mean that the snake's skin sprite would grow at the head if the spline is extended. Adaptive UV doesn't fit my use case because I want there to be a smooth transition if the spline length is changed during gameplay.

    Here's a pic of the snake where the head is given the highest spline point index.
     
    Venkify and MagicianArtemka like this.
  8. MagicianArtemka

    MagicianArtemka

    Joined:
    Jan 15, 2019
    Posts:
    46
    Cute snake ;)
     
    nabergh likes this.
  9. Venkify

    Venkify

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    644
    1. how do I set a edge material through code?

    SpriteShapeRenderer has 2 materials. Fill and Edge. Please use Renderer.materials to set/get materials.
    Unity - Scripting API: Renderer.materials (unity3d.com)

    // A quick demonstration code:
    var materials = GetComponent<SpriteShapeRenderer>().materials;
    materials[1] = _CustomEdgeMaterial;
    GetComponent<SpriteShapeRenderer>().materials = materials;


    2. We are geting issues of spriteshapes not rendering when they are meant to (Sorted through RedHillBilly's problem)

    We are looking into this. Could you kindly submit a simple repro project ?

    3. Performance seems very bad on Nintendo Switch[/QUOTE]

    Could you please provide more details regarding this ?

    Geometry generation:
    1. Please try installing Burst for better performance. Geometry is generated entirely in C# Jobs and will be more fasterr with Burst installed.
    2. For static mesh, please consider using the Bake Geometry feature. This basically stores the generated geometry in Editor time, and loads this data in Runtime instead of Regenerating in the Player.
    SpriteShape Bake Geometry - YouTube

    Thanks for the post. Please feel free to submit bug report and we will take a look asap.
     
  10. Venkify

    Venkify

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    644
    Please do submit a bug report. This would be helpful to analyze the issue deeper.

    @RedHillbilly @MassiveMonsterGames

    Also kindly please note that the workaround script suggested above (Unity - Tell us about your experience with Sprite Shape | Page 4 - Unity Forum) would result in calls to OnGUI each frame and potentially multiple times. Please take a look at GenerateSpriteShapes Script provided in SpriteShape Extras that prevents this.
     
  11. Venkify

    Venkify

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    644
    Perhaps you can try using the same sprites as Variants .i.e
    For the same Angle Range, add SpriteHead, SpriteTail, SpriteBody, SpriteHead, SpriteTail (in that order) and the edges will be rendered in that order of selection of Variant. Select which SpriteHead (1 or 4) for the end control point, depending on the required order.
     
  12. Venkify

    Venkify

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    644
    Apart from the default Collider generation, there are also other generators provided in the Extras (you can install them from SpriteShape page in PackageManager). For more info, please check :

    Unity - SpriteShape Preview Package | Page 6 - Unity Forum
     
    PhantomFox128 likes this.
  13. Aurell-lioo

    Aurell-lioo

    Joined:
    Oct 23, 2020
    Posts:
    2
    Hello,
    I'm currently using SpriteShape 4.1.4 to create walls and obstacles in my 2D game and I'm facing an issue.
    Everything works fine until I assign an hierarchy to the Sprite Shape or create a prefab. Then when I click on "Edit Spline", select an anchor point and try to move it, it resets the latter anchor position to x=0 and y=0 instead of being able to drag it to the desired location.

    I already posted this issue in the topic below, which seems to be dealing with the exact same problem.
    https://forum.unity.com/threads/sprite-shape-2d-not-working.986412/

    Thank you in advance for your help.
     
  14. RemDust

    RemDust

    Joined:
    Aug 28, 2015
    Posts:
    432
    Hello guys :)
    Still no way to create "holes" or "shortcut" in an existing spriteshape collider ?


    I need to be able to bring modifications to my level designs, I can't test and correct my maps and levels if I can't create holes in the existing levels. It really holds me back from using SpriteShape in a real project :(
     
  15. UncleAlias

    UncleAlias

    Joined:
    Aug 18, 2017
    Posts:
    27
    1. Hi, I'm using SpriteShape for a single-screen platformer called Bird Panic.
    2. Mostly just using it to give the platforms an organic feel.
    3. I wouldn't mind a keyboard shortcut for going into spline-editing mode
    4. Only a little, but I feel like I'm missing something :)
    5. 0-radius Edge Colliders are great for platforms being interacted with on only one side, but when you increase the radius it sticks out past the image at the end. Having a slider to change where the collider ends seems like a simple thing that would save a lot of work.
    I tried GeometryCollision.cs, but the collider it creates conforms to the borders of image I'm using, so the characters are floating over the grass. I'm pretty sure I'm always going to be working with irregular terrain when I work with SpriteShape, so this isn't very useful to me.
    Is there some way to get around either of these limitations that I'm not seeing?
    8. I was aware of them, but didn't realise they had scripts that added new functionality until I read this thread

    Aside from that, I really like SpriteShape and hope you continue to develop it, thanks!

     
    RemDust likes this.
  16. nabergh

    nabergh

    Joined:
    Jun 22, 2019
    Posts:
    7
    Thanks for the advice! Unfortunately this doesn't completely solve my use case. Although I didn't clarify it in my original post, I'd like it if all points with lower indices are rendered above those with higher ones. While the solution you mentioned will help with the head and tail, if there are an arbitrary number of body points then they will all be rendered in the wrong order still which will create a weird effect if the head renders on top but the neck is rendered below an overlapping body point elsewhere on the spline. I suppose I could have a very large number of sprites in my profile (one for every body piece) but then the sprite would repeat at every spline control point and I'd be limited by the number of body pieces, neither of which is desirable.

    Is it possible to make all lower sprite indices render above higher ones or change how the sprite is repeated so that it is repeated starting at lower indices instead of higher ones?
     
  17. shieldgenerator7

    shieldgenerator7

    Joined:
    Dec 20, 2015
    Posts:
    39
    3. That would be great!
    5. The collider can stick out of the sprite for many reasons. One thing you can do is to make sure the terrain edge's sprite has its splice set up properly:
    upload_2021-1-21_19-25-37.png

    If that doesn't work, you can try adjusting the sprite shape's offset:
    upload_2021-1-21_19-28-38.png
     
  18. UncleAlias

    UncleAlias

    Joined:
    Aug 18, 2017
    Posts:
    27
    Thanks, but no, the edge collider goes to the end of the sprite regardless (extending past it by the edge collider's radius) and the offset only moves the edge up or down.
     
  19. Npicouet

    Npicouet

    Joined:
    May 17, 2018
    Posts:
    30
    I'm not sure if this has been discussed, but is it possible for the spline texture to use World Space UV coordinates?

    I'm generating a spline shape procedurally, but the spline texture changes when the shape gets recreated. Its different each time my terrain recalculates and removes old terrain, because the "spline" has changed. I'd like it to use world space so it never changes, it just "generates".

    I'm sure this issue is more involved than it seems. Any ideas?

    I attached a before and after picture of the same stretch of terrain before and after the spline is recreated (destroy old and bring in new)
     

    Attached Files:

  20. Venkify

    Venkify

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    644
    You can potentially do it in Shader. But you may need to make sure its a single texture sprite without being Atlased.
     
  21. Npicouet

    Npicouet

    Joined:
    May 17, 2018
    Posts:
    30
    Okay. I'm not even really sure where to begin with shaders. I also am not sure how its mapping the UV on the current spline texture. Is it 0-1 per segment, then tiling?
     
  22. Npicouet

    Npicouet

    Joined:
    May 17, 2018
    Posts:
    30
    I made a little bit of progress on this, but my shader code is all arbitrary values, and the UV tiling shader that I made stops working once the spline is not flat. I'm not sure what the spline is doing, so its hard to get a read on what I need to work on.

    Here is a shared folder with a few video examples:
    https://drive.google.com/drive/folders/17iBFoqQMrNTuYz3wX3iCtWjuqHEde8qL?usp=sharing

    You can see in the videos:
    correct.mp4 - This is how I want the spline to behave. It behaves properly when most of the spline is flat. The UV of the top edge stays consistent when it refreshes the spline.
    wrong.mp4 - This is what I'm trying to fix. When the spline refreshes, the UV tiling of the top edge is inconsistent. I want it to be based off the world space or spline coordinates.

    I also uploaded a screenshot of my current shader.
    The idea was to just offset the UV tiling by the first X coordinate on the spline, but its using arbitrary values that have no meaning yet.
     

    Attached Files:

  23. CDF

    CDF

    Joined:
    Sep 14, 2013
    Posts:
    1,311
    I don't think this is possible, since you need the length a vertex is along the spline in order to tile the edge in world space coordinates. Unless I'm mistaken, we don't have access to the mesh Unity generates for sprite shapes. However, there is a class
    Code (CSharp):
    1. SpriteShapeUtility.Generate(mesh, shapeParams, points, meta, angles, sprites, corners);
    That you can call to generate a mesh, which you can then further modify.
    This will require you use a MeshRenderer and MeshFilter though, as again. I don't think Unity allows setting the sprite shape renderer mesh
     
  24. Npicouet

    Npicouet

    Joined:
    May 17, 2018
    Posts:
    30
    I actually can get that value, but still having a hard time knowing exactly where it fits in.
    I'm missing a piece in my calculations, or am just doing it wrong.

    I can get the exact length calculation of the top edge of my terrain, or the total length, or length to a certain point from a certain point.
    It's represented as the red line in my screenshot.
     

    Attached Files:

  25. CDF

    CDF

    Joined:
    Sep 14, 2013
    Posts:
    1,311
    yep. Now you need to tell each vertex on the sprite shape mesh what distance they are along that red line. And use that value for UV

    then in your shader, something like this:

    uv = IN.texcoord;
    uv.x = uv.x / _WorldSpaceUVSize;

    //sample texure at uv

    problem is, there's no way (that I know of) to assign the UV's of each vertex on the generated mesh, since we don't have access to that generated mesh. Unless I'm misunderstanding your intention?
     
  26. Npicouet

    Npicouet

    Joined:
    May 17, 2018
    Posts:
    30
    I think you understand my intentions, and you are correct you cannot set the UV's at each vertex.

    What I can do is set the UV offset of the top repeating texture.
    My thought was to get the difference in the current length from previous generated terrain and apply that as an "offset".

    Example:
    Initial Terrain Created : 89.65 Top Length
    Terrain Recreates after moving a set amount : 96.13 Top Length
    Take 96.13 - 89.65 = 6.48, and use that to offset the UV.

    I'm not sure what else I need to do to manipulate that number, but it seems like it should work that way?

    EDIT: Solved!!!! I'll post a solution in the coming days.
     
    Last edited: Feb 18, 2021
  27. Npicouet

    Npicouet

    Joined:
    May 17, 2018
    Posts:
    30
    Got it working.

    The basics of how it works: It takes the size of the hill sprite (sprite width / pixels per meter = sprite size) and offsets the x coordinate of the UV by the "missing" first segment of the hill (sprite size / missing offset) every time it regenerates a new one.

    I've attached the final shader code.

    Here is the link to a video showing it in action:
    https://drive.google.com/file/d/12MuLWXMYlf0vgnOXg1wLrF-mIp1osJHk/view?usp=sharing
     

    Attached Files:

  28. cpetzold

    cpetzold

    Joined:
    Feb 5, 2014
    Posts:
    1
    They're referring to inverting the fill mode so that when you specify a spline, it fills outside the spline (with some range) instead of inside:
     
  29. castor76

    castor76

    Joined:
    Dec 5, 2011
    Posts:
    2,517
    How can I get a point info along the spline ? Not the control points, but interpolated position info etc, along the curve.
     
  30. Venkify

    Venkify

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    644
    Please take a look at Sprinker.cs from the Extras available with the package. Here is a quick sample code :

    Code (CSharp):
    1. // The following code instantiates a prefab in a random location between control points 1 and 2.
    2. SpriteShapeController ssc = GetComponent<SpriteShapeController>();
    3. Spline spl = ssc.spline;
    4. var i = 2;
    5. var go = GameObject.Instantiate(m_Prefab);
    6.  
    7. float2 _p0 = new float2( spl.GetPosition(i - 1).x, spl.GetPosition(i - 1).y );
    8. float2 _p1 = new float2( spl.GetPosition(i).x, spl.GetPosition(i).y );
    9. float2 _rt = _p0 + new float2( spl.GetRightTangent(i - 1).x, spl.GetRightTangent(i - 1).y );
    10. float2 _lt = _p1 + new float2( spl.GetLeftTangent(i).x, spl.GetLeftTangent(i).y );
    11.  
    12. float r = Random.Range(0.5f, 0.95f);
    13. float2 bp = BezierPoint(_rt, _p0, _p1, _lt, r);
    14. go.transform.position = new Vector3( bp.x, bp.y, 0);
     
  31. RulioOW

    RulioOW

    Joined:
    May 27, 2018
    Posts:
    7
    Hi,

    Is there any way to randomize the sprite variants directly between the points instead of selecting only one sprite variant for one point?
    The only thing i could think of is making a lot of points and manually set different variants for each point to get a good random design but it's heavily time consuming and resulting in a point overdose.:(
     
  32. lorenzoteslermabe

    lorenzoteslermabe

    Joined:
    Dec 23, 2020
    Posts:
    24
    Hey there! Trying to use sprite shapes for super fast, yet still accurate prototyping, and so it would be nice to be able to snap to the grid by holding ctrl/cmd (or at least enable snapping through a shortcut).

    In the same vein, it seems like snapping gets disabled often when moving the shape around with transform tools. Conversely, when moving control points without moving the whole shape with transform tools, the pivot ends up far away from the actual shape.

    Could be user error on all accounts, but these are just some thoughts :)
     
    CC_YN and mokalux like this.
  33. castor76

    castor76

    Joined:
    Dec 5, 2011
    Posts:
    2,517
    Sprite Shape Renderer does not take its shape into account when rendering normal buffer for 2DLighting.

    Also, probably related to the same issue, where if you scale the sprite negatively, that too.. is not taken into account when rendering normal map buffer.

    Rotating the actual sprite does seems to work however.
     
  34. castor76

    castor76

    Joined:
    Dec 5, 2011
    Posts:
    2,517
    Thanks, but I think this should really be implemented to SpriteShapeController level, so we can just call it.
     
  35. indie6

    indie6

    Joined:
    Dec 15, 2012
    Posts:
    101
    Hi, I am adding PolygonCollider2D to the sprite shape at runtime. The issue is, it does not gets updated on the same frame.. is there a function to update on the same frame? BakeCollider or setting autoUpdateCollider before adding doesn't work
     
  36. jarbleswestlington_unity

    jarbleswestlington_unity

    Joined:
    Dec 6, 2017
    Posts:
    32
    Having a fundamental issue with sprite-shape, and wanted to see if there was maybe a work-around?

    2D lighting doesn't seem to effect the FILL texture at all, everything else it does fine with. Not exactly sure why this is, but I hope there's a way around it, otherwise I'll need to find an alternative to spriteShape.

    Here's a screenshot of my inspector, and a comparison of the same texture on a sprite shape and on a regular sprite.


    upload_2021-5-16_21-35-17.png
     
    RemDust and castor76 like this.
  37. castor76

    castor76

    Joined:
    Dec 5, 2011
    Posts:
    2,517
    Yeah, 2D Lighting does not seem to work nicely with SpriteShape at all at the moment. We need official respond from Unity to clarify this.
     
    RemDust likes this.
  38. Venkify

    Venkify

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    644
    SpriteShape Renderer is indeed supported by 2D URP. We will take a look asap. @jarbleswestlington_unity Could you kindly please submit a bug with this simple repro project that could help us validate this ?
     
    RemDust likes this.
  39. Venkify

    Venkify

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    644
    Since the entire generation happens in a Job, Collider gets updated in the next frame.
    However if you would like to force generation in the same frame, please make sure to call these functions in this order:
    spriteShapeController.BakeMesh();
    spriteShapeController.BakeCollider();
     
    indie6 likes this.
  40. castor76

    castor76

    Joined:
    Dec 5, 2011
    Posts:
    2,517
  41. Venkify

    Venkify

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    644
    We have added a new API to set custom Bounds in 2021.2 to support fixing the above.
    https://docs.unity3d.com/2021.2/Doc...nce/U2D.SpriteShapeRenderer.SetLocalAABB.html
    We are fixing the above issue in an upcoming package release. Until then, please use the Render Mesh workaround or set custom Bounds to reflect any changes made to SpriteShape object on Runtime.
     
  42. indie6

    indie6

    Joined:
    Dec 15, 2012
    Posts:
    101
    Thank you, it worked perfectly!
     
  43. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,459
    This thread was started to get feedback, not to serve as a magnet for asking how to use the feature.

    Please start you own threads rather than resurrecting/hijacking old threads; they're free to do! :)
     
    DREBOTgamestudio likes this.
  44. isbasher

    isbasher

    Joined:
    Mar 19, 2014
    Posts:
    8
    Using Sprite Shape 7.0.3 alpha is not applied on the sprite shape. Is this intended?
     
  45. benryhenson

    benryhenson

    Joined:
    Dec 21, 2021
    Posts:
    4
    I'm experiencing something with sprite shapes that I can't seem to find anyone else talking about and this one's a head scratcher.

    TLDR I'm trying to make an asteroids clone for practice. I'm using sprite shapes for the asteroid prefab and then moving the spline control points around on instantiation to give them all a randomized appearance. so far so good. however as the game progresses and I am instantiating more and more of these prefabs, frequently the game object is instantiated with all of its components disabled for apparently no reason and with no error thrown in the console. if I pause the game, and then manually check the boxes to turn the components back on, then unpause magically the prefabs manifest into existence and work like normal. seems to happen more frequently if I'm creating a lot of them at a time in the same frame.

    I'm going to have to assume this is a bug or it's by design but not throwing any warnings or errors (Is it a conflict with trying to create too many things in nearly the same place?)

    side note: I would like to second the notion that the sprite sheet used for the textures could use the traditional nine tile orientation where the bottoms and sides face downwards and sideways respectively, rather than having to be oriented facing upwards
     
  46. benryhenson

    benryhenson

    Joined:
    Dec 21, 2021
    Posts:
    4
    Here's a vid of it occurring:

    And I attached an image of the Inspector when this happens.


    Edit: I tried spawning them on different frames, and it still happens. o_O
     

    Attached Files:

    Last edited: Feb 8, 2022
  47. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,459
    Please, see my post above. Please create your own thread and/or report a bug if you believe it is one.

    Thanks.
     
  48. Venkify

    Venkify

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    644
    Hi, there is no reason why the Components are auto-disabled. Could you please post a repro/sample project or file a bug report with the project ? Will take a look asap.
    Also as mentioned by @MelvMay, kindly start a new thread for the case if possible as its easier to follow and respond. Thanks.
     
Thread Status:
Not open for further replies.