Search Unity

SpriteSharp - Sprite Mesh Optimizer

Discussion in 'Assets and Asset Store' started by ZimM, May 24, 2015.

  1. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    963
    Editing sprite meshes manually is neat, but there is not that much use to it, really. It'll probably come in handy in 1% of sprites, and for the rest of time SpriteSharp currently does a better job :)
     
  2. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    963
    Small announcement about some new things to come in update.
    Most importantly, there are now 3 different modes of how sprites can be processed.
    1. Normal
    This is the same thing that is available now in SpriteSharp 1.0.

    2. Alpha Separation
    This mode is designed for sprites that are mostly opaque, but have important parts with alpha. It is identical to Normal mode, except one thing: it separates a single sprite into two, one of which contains only 100% opaque pixels, and the second one contains only the rest of pixels, and two of them match up perfectly.

    So for example, we have a sprite with an image of a cloud:
    upload_2015-9-4_19-50-28.png

    After processing, you'll get two sprites:
    upload_2015-9-4_19-51-3.png
    upload_2015-9-4_19-51-7.png


    So, why'd anyone want that? Simple. Imagine you have a sky that is full of clouds that cover a lot of screen space and overlap each other. You'll get a ton of overdraw. But now, we can draw the opaque part of the sprite using an opaque shader that utilizes hardware Z-Buffer in order to reduce overdraw to zero for those parts. The non-opaque parts of the sprite will render as usually.

    3. Precise
    This mode is designed mostly for pixel-art(ish) sprites, which often have hard edges and are of small pixel dimensions.
    It allows for per-pixel accuracy of the generated mesh, independent of the image resolution. And of course, you can tweak it to get the best overdraw/complexity ratio:
    upload_2015-9-4_19-42-2.png upload_2015-9-4_19-42-37.png
    @Marrt, you'll probably like this ;)

    There are also few other additions, like the ability to set per-project default settings.

    Unfortunately, the database format had to be changed to accomodate for these changes, so you'll have to re-set the mesh settings again. But I hope it'll be worth it.

    What do you think? :)
     
    Last edited: Sep 5, 2015
  3. jay-jin

    jay-jin

    Joined:
    Jun 22, 2014
    Posts:
    12
    This is very helpful,good job!
     
  4. I am da bawss

    I am da bawss

    Joined:
    Jun 2, 2011
    Posts:
    2,574

    Those are AWESOME features ZimM!!!!!
    Love it! I think that Alpha Separation feature is the most significant feature to date. I couldn't be happier to have purchase this extension. I think I will make most use of that feature - it will also allow users to turn off alpha parts for performance on mobile (where overdraw is critical to frame rate).

    Great work! Keep those features and bugfixes coming! :)
     
  5. abar

    abar

    Joined:
    Jan 12, 2014
    Posts:
    72
    Hi, I'm itching to try out the alpha separation mode - that's going to super useful for reducing overdraw in our game. Any idea when the new version will hit the store?
     
  6. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    963
    The asset has gone through quite an overhaul last month, which was longer than I expected, but I guess I'll release the new version in around a week :)
     
    Last edited: Oct 9, 2015
    theANMATOR2b likes this.
  7. Slawo

    Slawo

    Joined:
    Jan 29, 2014
    Posts:
    11
    Any news on the release date for this feature? I just bought the plugin and I don't see the alpha separation in the version I downloaded today...
     
  8. EmeralLotus

    EmeralLotus

    Joined:
    Aug 10, 2012
    Posts:
    1,462
    Really amazing plugin.
    I would like to use it during runtime. Can you explain in a bit more detail why it cannot be used during runtime ? Is there a work around ?
    Cheers.
     
  9. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    963
    Sorry for the delay everyone. I've decided to give the update a bit more work, so I've improved and optimized some stuff, fixed some edge-case issues, and added a new feature - per-platform settings, which would, obviously, be useful for cross-platform development:
    upload_2015-11-8_5-25-43.png

    I can now consider the update finished, so it'll be live in Asset Store in a few days :)
     
  10. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    963
    There are various reasons, but the most important one is: Unity provides no runtime API for changing the sprite mesh. Sprite.OverrideGeometry() throws an error when called in Play mode, and Sprite.triangles, Sprite.vertices, Sprite.uv are all read-only, so even if the new sprite mesh would be generated in runtime, there is no way to actually apply it. Therefore, SpriteSharp is limited to Editor usage only for the time being.
     
    Last edited: Nov 8, 2015
  11. hakonber

    hakonber

    Joined:
    Apr 17, 2013
    Posts:
    1
    Sorry to bother you, ZimM, but I'm very eager to try out your latest update. I think it could really help with reducing overdraw in our mobile game. Any idea when it will be released?
     
    Last edited: Nov 16, 2015
  12. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    963
    Hi everyone!

    This was a very long and bumpy road to release the update, and I'm sorry for all the delays, but the day has finally come. I proudly present to you — SpriteSharp 2.0. I've worked hard to make this version as polished as possible. As a result, the asset was pretty much redone from scratch, and is now better than ever before.
    The grand 2.0 update is already live on Asset Store, go grab it! :)

    Changelog:
    • Added Precise and Alpha Separation processing methods.
    • Per-project default sprite mesh settings (available in Unity Preferences window).
    • Per-platform sprite mesh settings support.
    • Unity Cloud Build support.
    • Added “Sprites-Opaque” shader for opaque sprites.
    • Database is now reliably stored in a JSON file, no more losing data when scripts mismatch.
    • Correct behavior during initial texture import phase (i.e. after deleting Library).
    • GUI improvements.
    • Fix sprites meshes being reset after renaming the texture.
    • Significant mesh simplification improvements.
    • Performance improvements.
    • Fixed errors when importing the package.
    • Fixed namespace conflict in third-party libraries that prevented SpriteSharp from being importing correctly for some people.
    • Stability and reliability improvements.
    Let me know what you think about the update, and rate SpriteSharp on the Asset Store if you like it :)
     
    Last edited: Nov 29, 2015
    theANMATOR2b likes this.
  13. Juaprebo

    Juaprebo

    Joined:
    Nov 15, 2014
    Posts:
    19
    Hi, I found that Sprites/Default shader always is rendered in front of Sprites/Opaque shader ignoring sorting layer. Can you help me?

    *Solved: it was a incorrect scene configuration
     
    Last edited: Dec 3, 2015
  14. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    963
    Hi everyone,

    an update to version 2.1 has been finished and should be live on Asset Store in a few hours. This one improves the quality of Alpha Separation mode and exposes more properties to it, making it more usable with complex sprites.

    Changelog:
    • Improvements of Alpha Separation method. Exposed a separate "Vertex merge distance" property for opaque sprite. Added an option to reduce alpha bleeding.
    • Minor GUI improvements.
    • Minor performance optimizations.
     
    Marrt likes this.
  15. mh114

    mh114

    Joined:
    Nov 17, 2013
    Posts:
    295
    Very useful looking asset, bought! I'll let you know how it goes, hoping to get some decent vertex savings with this, as I'm using mostly very downscaled sprites. :)
     
  16. rrlDev

    rrlDev

    Joined:
    Feb 26, 2015
    Posts:
    7
    Just bought this asset a few hours ago and it's working great!

    I was having some issues when I first imported it into my project. Namely, Unity would override my SpriteSharp settings at runtime, which kind of made this asset completely unhelpful. I messed with some settings to no avail, and then finally decided to try to reproduce the issue in a new project... and I couldn't. So, that lead me to the idea of reimporting all my assets in my actual project. That did it. Worked perfectly after that. Maybe I just needed to reimport the art assets, but whatever, it was a small project. So, I guess if anyone else has the same issue I did... reimport.

    Though, there is one thing that's bugging me. The Alpha Separation feature seems awesome, but it requires me to switch the sprite to 'multiple' mode, which I suppose makes sense. However, in doing that, I lose my pivot point settings. Much of my art is animated with Mecanim so I kind of need those pivot points. Is there some workaround to this, or is there some setting I'm completely overlooking? I haven't worked too much with Unity's sprites, and never with sprites in 'multiple' mode, so I wouldn't be surprised if I'm just missing something simple.
     
  17. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    963
    Thanks for the workaround. I couldn't personally reproduce the issue you've described, but at least there's some solution if someone else will face this problem.

    As for the sprite pivots - in Multiple sprite mode, pivots are edited per-sprite in Sprite Editor:
    upload_2016-2-11_11-57-58.png
     
  18. rrlDev

    rrlDev

    Joined:
    Feb 26, 2015
    Posts:
    7
    Ah, thanks so much! I only ever used the sprite editor for setting borders for UI images so I didn't even think to look there. Silly me. Keep up the good work!
     
  19. zwickarr

    zwickarr

    Joined:
    Nov 20, 2012
    Posts:
    28
    ZimM, does Sprite Sharp have a mesh exporter? thanks!
     
  20. MaxFr77

    MaxFr77

    Joined:
    Nov 9, 2015
    Posts:
    7
    Cool stuff, I love the integration in the editor. Although it only seems to work with sprites and not UI images (but I guess that's Unity limitation).

    Question: I coded a shader that adds border to sprites. Is there any way to increase border size (beyond alpha tolerance = 0) ?

    Answer: just found this is exactly what precise mode/inflation was supposed to do. Unfortunately this mode was giving crappy result on my highres sprite (created holes and such). After some search I tweaked the CollectPixelPaths method so that the inflation is based on pixels rather than merging paths, also I extended inflation input range.
    ZimM contact me if you want the code.
     
    Last edited: Feb 22, 2016
  21. zwickarr

    zwickarr

    Joined:
    Nov 20, 2012
    Posts:
    28
  22. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    963
    SpriteSharp doesn't has that option built-in, but it's really easy to make it if you look at SpriteProcessor.ProcessSprite() method - the vertex and index data is all generated there, you can just grab it. Or... yes, you might just use that exporter :) I'll add a built-in exporter in the next SpriteSharp version.
     
    zwickarr likes this.
  23. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    963
    The easiest way to do that is switch your texture to Advanced mode and increase the Extrude Edges value, as it is accounted for by SpriteSharp. This is per-texture, though, not per-sprite.
    upload_2016-2-24_3-53-37.png
     
  24. Ologon

    Ologon

    Joined:
    Nov 16, 2014
    Posts:
    16
    Thanks for this great asset!
    I have a doubt though. You wrote it doesn't work at runtime, but does it mean that if I prepare the sprite in the editor, build a prefab asset bundle from it and then dowload and display it at runtime it will be using unity algorithm?
     
  25. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    963
    By "doesn't work at runtime" I mean that you are not able to generate a new mesh for a sprite in runtime. Asset bundles are built from the Editor, and sprite meshes are baked into it as well. So no, asset bundles shouldn't be a problem.
     
  26. Ologon

    Ologon

    Joined:
    Nov 16, 2014
    Posts:
    16
    I see, thank you!
     
  27. PedroSchabib

    PedroSchabib

    Joined:
    Oct 2, 2015
    Posts:
    10
    does It work on multiplesprite?
     
  28. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    963
    Do you mean "Sprite Mode → Multiple"? Yes, it does.
     
  29. PedroSchabib

    PedroSchabib

    Joined:
    Oct 2, 2015
    Posts:
    10
    Yes, i mean it!
    sorry my english, and thank you!
     
  30. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    I think this may well be quite useful for 3D actually. We'd like to render billboards (or sprites) with central part in opaque queue and the transparent parts in transparent queue, which your tool supports, so I'm keeping tabs on this. This isn't just for mobile, and it isn't just for 2D. It's a grand optimisation for anything.
     
  31. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    963
    It's definitely not only for mobile, desktop and console projects benefit from reduced overdraw as well. Usage in 3D pretty limited, though - I can't really think of anything other than billboards where it could be used... Keep in mind that it currently only works with Sprites, so you can't just generate a mesh for Texture itself. Though you can export a sprite mesh and then do whatever you want.
     
    Last edited: Mar 21, 2016
  32. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Yeah sure! It's just for a lot of games you want a ton of interesting billboarding going on in the background and overdraw is a killer for any platform.
     
  33. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    963
    Hi everyone!

    SpriteSharp has been updated to version 2.2.0, the update is already available on the Asset Store.

    Changelog:
    • Improved Unity 5.4 compatibility.
    • Added a menu item to remove sprites without any overrides from the database.
    • Added sprite mesh export feature.
    • Massively reduced memory consumption. Previously, all sprites were loaded into memory constantly, now they are only loaded when needed.
    • Fixed SpriteSharp database not saved when Editor is closed.
    • Fixed broken Alpha Separation method when Extrude Edges is set to a high value in texture settings.
    • Minor UI fixes and improvements.
     
  34. Zehru

    Zehru

    Joined:
    Jun 19, 2015
    Posts:
    84
    Hello @ZimM, I want to buy this assets, but I want to be sure of how to use it.
    can you or someone else here, make a video showing how to setup this asset?
    I really would like to see this asset working in a video, so I'll be sure to buy it.
    thank you
     
  35. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    963
    Well, that video could just be one frame with this screenshot :)


    There is literally nothing more, it's very straightforward to use. You just import SpriteSharp and edit your sprite meshes settings, that's it.
    Here's a really short video on how to use SpriteSharp:
     
    Zehru and theANMATOR2b like this.
  36. Zehru

    Zehru

    Joined:
    Jun 19, 2015
    Posts:
    84

    Whaaaat!? this is amazing!!!
    when I receive my money, I'll surelly buy it!
    thank you :D
     
  37. Shadeless

    Shadeless

    Joined:
    Jul 22, 2013
    Posts:
    136
    @ZimM
    Hey so is it possible for you to add a mode where Full Rect is used but we can define how many vertices the Rect should be split in? For example:

    http://prntscr.com/ba937o

    The reason this would be useful is for the vert data in shaders. Can it be added easily?

    Cheers
     
  38. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    963
    It should be pretty easy to add, but I'm not exactly sure could it that be useful, since it is actually not possible to put custom vertex data into meshes. Do you have an example of when this could be useful?
     
  39. Shadeless

    Shadeless

    Joined:
    Jul 22, 2013
    Posts:
    136
    @ZimM
    Well basically when writing vertex shaders I think the vertices on the mesh matter, so increasing the amount of vertices in the sprites mesh is like increasing the resolution on which the vertex shader can work on.

    For example skewing a simple rect vs. detailed rect.
    http://prntscr.com/ba9ccm
    At least that's how I'm thinking of using it.


    You can see an example of how the mesh geometry matters in the first 5 minutes.

    Cheers
     
  40. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    963
    I see, makes sense. I'll try do add that in a next update.
     
  41. Shadeless

    Shadeless

    Joined:
    Jul 22, 2013
    Posts:
    136
    @ZimM
    Thanks man, that sounds great! :D
     
  42. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    963
    And... done! Coming soon :)
    Here's a few trippy vertex shader effects I've just made with sprite in Rect Grid mode:

     
    Last edited: May 31, 2016
    Marrt, Shadeless, mh114 and 1 other person like this.
  43. Shadeless

    Shadeless

    Joined:
    Jul 22, 2013
    Posts:
    136
    @ZimM
    Wow man, that's exactly what I meant! :D
    That was fast!
     
  44. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    963
    Added an optimization to not include fully transparent cells to the mesh. Allows to save some fill rate even in Rect Grid mode :)
     
    docsavage, colin299, raccoon- and 3 others like this.
  45. abuki

    abuki

    Joined:
    Nov 14, 2013
    Posts:
    40
    Hello, two questions about using alpha separation.
    1) is it possible to preserve pivot? I have a bottom pivot, but after alpha separation, it is now center.
    2) is it possible to add alpha separation to global config, so it is applied automatically?
     
  46. Shadeless

    Shadeless

    Joined:
    Jul 22, 2013
    Posts:
    136
    @ZimM
    Hey is the asset getting an update on the asset store any time soon?
     
  47. Stef_Morojna

    Stef_Morojna

    Joined:
    Apr 15, 2015
    Posts:
    289
    I just bought the tool and I must say the tool is really great!

    Now some stuff that I think could get some improvments:

    It would be nice to be able to edit the mesh manually ( The same way you would edit a 2d Polygon Collider) Its so annoying to see you could change 1 triangle and pretty much cut the overdraw % in half, and not being able to do it is really infuriating :p

    I would really like to have a Sprite-Opaque-Diffuse Material / Shader.

    It would be also a lot more practical if you would not need to have 2 gameObjects/SpriteRenderes for the Alpha Separation sprites, Something along the lines of a costum Component with 2 sprite slots and 2 material slots would probably work great!

    Maybe something like this:
    Actually, now that I think about it you wouldn't even need 2 textures, only 1, and have the transparent shader take alpha and color from it, and the opaque one only color.

    Edit: Another problem that I found was that the Sprite-Opaque shader dosn't support SortingLayer/OrderInlayer, something essential in any sprite shader. ( I'm working on fixing it, and I will post the code once its finish)
     
    Last edited: Jul 18, 2016
    theANMATOR2b likes this.
  48. Stef_Morojna

    Stef_Morojna

    Joined:
    Apr 15, 2015
    Posts:
    289
    So after messing around for some time I figured out what was causing the problem with the SortingLayer/OrderInLayer,
    it was the "Queue" = "Opaque".

    Basically all Sprites are rendered in the transparent queue ( That means they are rendered on top of all the 3d objects), so by changing the "Queue" = "Opaque" to "Queue" = "Transparent", now the sprite is rendered in the same layer as other Sprites. ( I hope I actually explained something and not just said something stupid/incorect :p)

    Now the only thing that i'm worried about is that by setting it into the Transparent queue, it wont avoid the Overdraw problem that we were trying to fix in the first place by creating a opaque mesh/shader.

    Here is the edited shader:
    Code (CSharp):
    1. Shader "Sprites/Opaque" {
    2.     Properties {
    3.         [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
    4.         _Color ("Tint", Color) = (1,1,1,1)
    5.         [MaterialToggle] PixelSnap ("Pixel snap", Float) = 0
    6.     }
    7.  
    8.     SubShader {
    9.         Tags
    10.         {
    11.             "Queue" = "Transparent"
    12.             "IgnoreProjector"="True"
    13.             "RenderType"="Opaque"
    14.             "PreviewType"="Plane"
    15.             "CanUseSpriteAtlas"="True"
    16.         }
    17.  
    18.         Cull Off
    19.  
    20.         Pass {
    21.  
    22.         CGPROGRAM
    23.             #pragma vertex vert
    24.             #pragma fragment frag
    25.             #pragma multi_compile DUMMY PIXELSNAP_ON
    26.             #include "UnityCG.cginc"
    27.  
    28.             struct appdata_t
    29.             {
    30.                 float4 vertex   : POSITION;
    31.                 float4 color    : COLOR;
    32.                 float2 texcoord : TEXCOORD0;
    33.             };
    34.  
    35.             struct v2f
    36.             {
    37.                 float4 vertex   : SV_POSITION;
    38.                 fixed4 color    : COLOR;
    39.                 half2 texcoord  : TEXCOORD0;
    40.             };
    41.  
    42.             fixed4 _Color;
    43.  
    44.             v2f vert(appdata_t IN)
    45.             {
    46.                 v2f OUT;
    47.                 OUT.vertex = mul(UNITY_MATRIX_MVP, IN.vertex);
    48.                 OUT.texcoord = IN.texcoord;
    49.                 OUT.color = IN.color * _Color;
    50.                 #ifdef PIXELSNAP_ON
    51.                 OUT.vertex = UnityPixelSnap (OUT.vertex);
    52.                 #endif
    53.  
    54.                 return OUT;
    55.             }
    56.  
    57.             sampler2D _MainTex;
    58.  
    59.             fixed4 frag(v2f IN) : SV_Target
    60.             {
    61.                 fixed4 c = tex2D(_MainTex, IN.texcoord) * IN.color;
    62.                 return c;
    63.             }
    64.         ENDCG
    65.         }
    66.     }
    67. }
    I also removed the c.rgb *= c.a;

    This line was unnecessary because parts of the texture with alpha lower than 1 wont render anyway (And if they did, this line would just make them a black color anyway).
     
    Last edited: Jul 19, 2016
  49. zenGarden

    zenGarden

    Joined:
    Mar 30, 2013
    Posts:
    4,538
    Almost a year later 2D tools are still coming.
    Sometimes you must pick up the tool for your project , you can't wait some undetermined time.
     
  50. Stef_Morojna

    Stef_Morojna

    Joined:
    Apr 15, 2015
    Posts:
    289
    I'm actually kinda surprised that unity didn't buy the asset and implemented it into unity itself.