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:
    945

    SpriteSharp is an efficient solution for generating better meshes for sprites, helping reduce your 2D scene total polygon count, draw call amount, overdraw and CPU load.

    While Unity itself is capable of doing this, the possibilities are limited. There are no options to tweak to get the best possible performance, and sometimes meshes generated by Unity are so suboptimal you have to revert back to using Full Rect quads, losing the overdraw improvement.

    This is why SpriteSharp was invented. It extends built-in Unity sprite mesh generation with various tweakable options, allowing you to get the best performance out of your 2D game. It is especially useful when working on complex scenes and developing for mobile platforms, where overdraw is often a problem.

    Free evaluation version is available upon request.

    Just a few more points:
    • If you are drawing a lot of sprites, the difference between 1000 and 100 triangles per sprite is going to end up a big deal.
    • Objects with SpriteRenderer are only batched up to a certain amount of triangles, and the limit of triangles per batch is around 700. This is because sprites are batched on the CPU, and doing that has a slight overhead. Therefore, decreasing the polygon count also decreases the amount of draw calls and offloads the CPU for more interesting work, like running your game scripts.
    • Unity has no prior knowledge about how sprites will be scaled in the scene. There is no point of making highly-tesselated mesh for an object that takes a small area on the screen. That's why manual control is needed.

    Unity 5.1 and newer is supported, both Personal and Pro.





















     
    Last edited: Jun 28, 2017
    Furry, boysenberry and theANMATOR2b like this.
  2. zenGarden

    zenGarden

    Joined:
    Mar 30, 2013
    Posts:
    4,538
    Within Unity it is not possible to control the amount of vertices generated ?
    You posted a dragon picture and that's strange but in this case Unity uses only 9 vertices even if it is some overdraw.
    Or does Unity randomly changes the amount of vertices it generates ?
     
  3. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    945
    Exactly, there are no options to control the mesh generation in Unity. It isn't random, but it isn't based on some vertex limit. The amount of vertices generated depends on the actual sprite.
    Yes, sometimes Unity overshoots the mesh details, and sometimes it undershoots it. In this example, Unity generates too few vertices, which results in increased overdraw, and there is nothing you can do about it. SpriteSharp gives you ability to fine-tune the mesh to get lower overdraw while still maintaining pretty low triangle count.
     
    zenGarden likes this.
  4. zenGarden

    zenGarden

    Joined:
    Mar 30, 2013
    Posts:
    4,538
    It is a very interesting tool, did you decided about the price already ? (i never spend above 10$ for small but usefull utilities)
     
  5. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    945
    @zenGarden
    Yes, the price tag for the initial version would be $10 :)
     
    boysenberry likes this.
  6. Furry

    Furry

    Joined:
    Feb 28, 2013
    Posts:
    5
    Hi, I have a question: have you solved problem with that case(?):
    -setting up new geometry for sprite
    -reimporting sprite(because we wanted to change something in settings)
    -in that case we lose mesh which we have created before

    If yes: do you hold settings of sprites somewhere? If yes, are they in Assets folder(so I'll be able to commit them to repository)?
     
  7. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    945
    Hi,
    yes, of course, the mesh is regenerated after the reimport. Yes, sprite mesh settings are kept in a single file that you can easily put under version control.
     
    Furry and theANMATOR2b like this.
  8. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    945
    Sorry for the delay everyone, it seems like Asset Store guys are overloaded and haven't even looked at SpriteSharp yet :( It might take some more time until it is approved.
     
    Last edited: Jun 8, 2015
    theANMATOR2b likes this.
  9. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    945
    I am happy to announce that SpriteSharp in now live on Asset Store! The introductory price is $4.95 for just one week, so go grab it until the price is back to normal :)
    Asset Store Link
    I'd be grateful if you'll share your experience about how good (or not) SpriteSharp works for you, so I can think of directions to improve the product.
    Thanks for your support!
     
  10. I am da bawss

    I am da bawss

    Joined:
    Jun 2, 2011
    Posts:
    2,574
    I am getting kinda confuse - what's difference between you and
    PolygonCollider2D Optimizer


    ???
    Does this only do polygonCollider2D only?

    The interface looks very similar as well:
     
  11. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    945
    It may look similar at the first glance, but it really isn't. PolygonCollider2D only works PolygonCollider2D that are used for physics, and it only optimizes the existing paths. SpriteSharp, on the other hand, generates new sprite meshes, and those sprite meshes are used for rendering, not physics. So the purpose is completely different.
    That said, the algorithm indeed can have many practical usages, so maybe I'll implement some physics-related functionality into SpriteSharp as well in some future update.
     
  12. I am da bawss

    I am da bawss

    Joined:
    Jun 2, 2011
    Posts:
    2,574

    Thanks for the clarification!
    Looks like I was right to buy both! :)
     
    theANMATOR2b likes this.
  13. ikemen_blueD

    ikemen_blueD

    Joined:
    Jan 19, 2013
    Posts:
    308
    hi there, quick questions will it work for animated sprite? will it be able to generate new sprite-mesh in runtime? I suppose the new mesh is no longer Sprite-Renderer right? Because I have some shaders only work for Sprite-Renderer.
     
  14. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    945
    It is still a SpriteRenderer in all senses, so yes, you can use specific shaders and animate the sprites. However, SpriteSharp only works in the Editor, it can't be used in runtime. This is a limitation of Unity API. In runtime, the default Unity algorithm will be used, so it's better to prepare your sprites in Editor, if possible.
     
    ikemen_blueD likes this.
  15. GCat

    GCat

    Joined:
    Jul 31, 2012
    Posts:
    174
    very useful, will buy for sure
     
  16. Marrt

    Marrt

    Joined:
    Feb 7, 2012
    Posts:
    527
  17. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    945
    @Marrt
    Hi, I'm on vacation for now, far away from my PC, so I can't verify that. I think it may work if you crank the Quality high enough, but I can't guarantee that at the moment.
     
    Last edited: Jun 23, 2015
  18. Marrt

    Marrt

    Joined:
    Feb 7, 2012
    Posts:
    527
    Bought it anyway, - Awsome, it works, Animations are also a breeze since you can multi-edit all sprites in a spritsheet:

    Detail:1, MergeDistance 1:
    upload_2015-6-23_20-14-18.png
    (don't judge my awful template sprites, i just made them as a guide for my artist :) )



    The Detail could be better though - i only have hard Edges (only 0 & 100% alpha), but this Tool is intended for bilinear Sprites with an alpha Falloff on the Edges i guess.

    Thanks ZimM!
    ___________

    Something is strange but it is nothing that is bothering me now after everything is set up, so enjoy your vacation:

    - I get below errors after applying and setting up any bunch of Sprites for the first time in Playmode
    - Then, after restarting Playmode, everthing works seamlessly until i select a Gameobject containing a modified Sprite
    - This throws below errors again and Unity crashes - seems like Unity doesn't like meshing on those small 16x16-sprites initially since on bigger sprites i get no such error
    - After restarting Unity everthing works, i re-edit the modified sprites with your extension sometimes brings that error back

    Errors:
    "IndexOutOfRangeException: Array index is out of range.
    UnityEditor.SpriteInspector.BuildPreviewTexture (Int32 width, Int32 height, UnityEngine.Sprite sprite, UnityEngine.Material spriteRendererMaterial) (at C:/buildslave/unity/build/Editor/Mono/Inspector/SpriteFrameInspector.cs:224)
    UnityEditor.SpriteInspector.DrawPreview (Rect r, UnityEngine.Sprite frame, UnityEngine.Material spriteRendererMaterial) (at C:/buildslave/unity/build/Editor/Mono/Inspector/SpriteFrameInspector.cs:289)
    UnityEditor.SpriteInspector.OnPreviewGUI (Rect r, UnityEngine.GUIStyle background) (at C:/buildslave/unity/build/Editor/Mono/Inspector/SpriteFrameInspector.cs:277)
    LostPolygon.SpriteSharp.SpriteMeshInspector.OnPreviewGUI (Rect rect, UnityEngine.GUIStyle background)
    UnityEditor.Editor.OnHeaderIconGUI (Rect iconRect) (at C:/buildslave/unity/build/Editor/Mono/Inspector/Editor.cs:420)
    UnityEditor.Editor.DrawHeaderGUI (UnityEditor.Editor editor, System.String header, Single leftMargin) (at C:/buildslave/unity/build/Editor/Mono/Inspector/Editor.cs:493)
    UnityEditor.Editor.DrawHeaderGUI (UnityEditor.Editor editor, System.String header) (at C:/buildslave/unity/build/Editor/Mono/Inspector/Editor.cs:462)
    UnityEditor.Editor.OnHeaderGUI () (at C:/buildslave/unity/build/Editor/Mono/Inspector/Editor.cs:359)
    UnityEditor.Editor.DrawHeader () (at C:/buildslave/unity/build/Editor/Mono/Inspector/Editor.cs:352)
    UnityEditor.InspectorWindow.DrawEditor (UnityEditor.Editor editor, Int32 editorIndex, Boolean forceDirty, System.Boolean& showImportedObjectBarNext, UnityEngine.Rect& importedObjectBarRect, Boolean eyeDropperDirty) (at C:/buildslave/unity/build/Editor/Mono/Inspector/InspectorWindow.cs:1044)
    UnityEditor.InspectorWindow.DrawEditors (UnityEditor.Editor[] editors) (at C:/buildslave/unity/build/Editor/Mono/Inspector/InspectorWindow.cs:969)
    UnityEditor.InspectorWindow.OnGUI () (at C:/buildslave/unity/build/Editor/Mono/Inspector/InspectorWindow.cs:350)
    System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:222)"
    And after that
    "!m_MappedChunk" multiple times
     
    theANMATOR2b likes this.
  19. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    945
    @Marrt
    Thank you for reporting this, I'll look into this issue when I'm back.
    You can also get better results if you set the Texture Mode to Advanced, and then set Extrude Edges to 0 (default is 1). Edge extrusion is pointless for hard-edges sprites.
     
    Last edited: Jun 26, 2015
  20. Furry

    Furry

    Joined:
    Feb 28, 2013
    Posts:
    5
    The tool is great, very nice low poly meshes and good customization! But I have a question: I need to generate some sprites in runtime(you know generating texture and than making it sprite texture) and put them on scene, is that possible to use your code somehow to generate proper mesh for those sprites?
     
  21. Mr.Stein

    Mr.Stein

    Joined:
    Dec 4, 2013
    Posts:
    169
    Hi @ZimM, I bought your asset, why this is not working in Canvas image?

    Regards,
     
  22. Marrt

    Marrt

    Joined:
    Feb 7, 2012
    Posts:
    527
    Last edited: Jun 24, 2015
    ZimM likes this.
  23. Mr.Stein

    Mr.Stein

    Joined:
    Dec 4, 2013
    Posts:
    169
    @ZimM, So then there is no way that SpriteSharp is used in canvas?
     
  24. Marrt

    Marrt

    Joined:
    Feb 7, 2012
    Posts:
    527
    There is, just attach a SpriteRenderer to the UI-Gameobject, and everything works - except scaling by Anchors

    It is pixelperfect and it seems that you need to have PixelPerUnit in your texture at 1 for a sprite to be 1TexPx:1ScreenPixel or correct the Scale in its rectTransform (e.g.import setting: 100PxPU -> Scale: 100/100/100)

    So if you use Anchors to stretch your Sprite into unproportional aspect ratios (like when you do if you target multiple Resolutions), the Sprite will just center on the midPoint without scaling or stretching - you need to use Scale and move it with a custom function reading anchorpoints of the rectTransform.
    upload_2015-6-25_13-35-18.png
     
    Mr.Stein and ZimM like this.
  25. Mr.Stein

    Mr.Stein

    Joined:
    Dec 4, 2013
    Posts:
    169
    Oh! thanks you very much @Marrt
     
  26. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    945
    @Marrt
    I've tried to reproduce the issue with errors on applying the mesh, but weren't able to trigger it in any way. Even tried using small 16x16 images. Could you please send me some of the images that triggered the error? What Unity version do you use?
     
  27. Marrt

    Marrt

    Joined:
    Feb 7, 2012
    Posts:
    527
    I am at office right now, but i once uploaded a sample project that uses L-shapes which are configured exactly like those i used when the error came up, I am using the latest version of Unity:
    http://forum.unity3d.com/threads/sorting-order-breaks-batching.334048/#post-2164006

    Maybe, if you find the time, you can identify a possibility of adding a better hard-edge support for small textures so i can get those meshes even tighter :D
     
  28. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    945
    Tried multiple sprites from that project in various combinations of settings, still couldn't trigger the error :/
    Yes, I thought about adding a pixel-art low-res specific mode. Can't give any promises or ETA, but it is on my backlog.
     
  29. Marrt

    Marrt

    Joined:
    Feb 7, 2012
    Posts:
    527
    Hmm, can't reproduce it either now... sry, to bother you about that
     
  30. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    945
    No problem. This is a typical Heisen-bug that collapses when observed :) Probably it'll pop up again eventually, but it's probably not a big deal anyway, since it's so hard to reproduce.
     
    Marrt likes this.
  31. I am da bawss

    I am da bawss

    Joined:
    Jun 2, 2011
    Posts:
    2,574
    I just upgraded to Unity 5.1.1 today and upon upgrading the project to 5.1.1 (this is automatic by Unity) it gave me an error:

    Mono.CompilerServices.SymbolWriter.MonoSymbolFileException: Symbol file `/Users/234rferverd/Documents/Unity 5 Projects/Unity Project/Assets/SpriteSharp/Editor/LostPolygon.SpriteSharp.dll.mdb' does not match assembly `/Users/234rferverd/Documents/Unity 5 Projects/Unity Project/Assets/SpriteSharp/Editor/LostPolygon.SpriteSharp.dll'
     
  32. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    945
    Not sure why Unity sometimes throws that error and sometimes not, but just ignore it. This error is absolutely harmless and only occurs sometimes when importing the dll.
     
  33. Reizla

    Reizla

    Joined:
    Nov 5, 2013
    Posts:
    136
    Does this one work with 2Dtoolkit? I'm still using tk2D as my 2D help and one of my games also runs pretty slow on my old Lg and was looking for a reason. Guess that the meshes might be the problem..?
     
  34. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    945
    AFAIK, 2DTk doesn't uses meshes at all, therefore, SpriteSharp can not be used with it.
     
  35. Reizla

    Reizla

    Joined:
    Nov 5, 2013
    Posts:
    136
    Ah, that's good news I guess. On the other hand it would mean I really need to update my code for better efficiency (already made a start on it).
     
  36. arkhament

    arkhament

    Joined:
    Jan 24, 2015
    Posts:
    114
    Hi @ZimM, if I have a big Atlass with all my sprite this asset SpriteSharp will work? or this only work with separate image?
     
  37. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    945
    Yes, atlases work as they usually work, you can use them just fine, no changes needed.
     
  38. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    945
    SpriteSharp is now 50% off for just 24 hours! Go grab it while you still can ;)
    Asset Store link
     
  39. arkhament

    arkhament

    Joined:
    Jan 24, 2015
    Posts:
    114
    @ZimM I mean Atlas with Sprite Mode Multiple..? no problem with that?
     
  40. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    945
    Yep, that works with no problems.
     
  41. arkhament

    arkhament

    Joined:
    Jan 24, 2015
    Posts:
    114
    great!, thanks man
     
  42. rafaellop

    rafaellop

    Joined:
    Feb 4, 2015
    Posts:
    25
    Is the project only for Unity 5 or can be also utilized in the prior version (e.g. latest 4.x) ?
     
  43. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    945
    The API that allows sprite mesh modification has only became available in Unity 5.0, so it is Unity 5.0 and newer only.
     
  44. EmeralLotus

    EmeralLotus

    Joined:
    Aug 10, 2012
    Posts:
    1,317
    Nice asset. Can this be used during run time or is it only in-editor?
     
  45. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    945
    @rocki
     
  46. Reizla

    Reizla

    Joined:
    Nov 5, 2013
    Posts:
    136
    Me again... Gonna drop 2Dtoolkit in favor of Unity's 2D engine, so I might need Sprite Sharp sooner or later. Now I'm wondering how Sprite Sharp works with Spine and Sprite Lamp? I'll most certainly will be using them for my next project(s) and I'm not sure how well they'll work along with Sprite Sharp.
     
  47. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    945
    The rule is simple here. If it works with SpriteRenderer - then it'll work with SpriteSharp. I'm not sure how Sprite Lamp works with Unity, but if it works SpriteRenderer - then it'll be fine. Spine, however, generates it's own meshes, so SpriteSharp is just not applicable to it. This kind of makes sense, since Spine models are not sprites at all, they are much more complex.
     
    Reizla likes this.
  48. Ortath

    Ortath

    Joined:
    May 22, 2014
    Posts:
    7
    I just imported this asset into a 5.1.2 pro project for the first time with other sprite assets: "2d sprites: letters", "PixelPerfect", "Meshinator" and "PixelPacker". I use WIndows 8.1 and have K: substituted to E:\K for my projects. I doubt this is a drive letter problem as all your paths should be relative only but note that C: is not available as its a tiny fast SSD for the OS only. I use K: to keep the drive letter the same no matter which computer I move the project to. I am getting the following error from your asset:

    Mono.CompilerServices.SymbolWriter.MonoSymbolFileException: Symbol file `K:\PU5\props test\Assets\SpriteSharp\Editor\LostPolygon.SpriteSharp.dll.mdb' does not match assembly `K:\PU5\props test\Assets\SpriteSharp\Editor\LostPolygon.SpriteSharp.dll'
    at Mono.CompilerServices.SymbolWriter.MonoSymbolFile.CheckGuidMatch (Guid other, System.String filename, System.String assembly) [0x00000] in <filename unknown>:0
    at Mono.CompilerServices.SymbolWriter.MonoSymbolFile..ctor (System.String filename, Mono.Cecil.ModuleDefinition module) [0x00000] in <filename unknown>:0
    at Mono.CompilerServices.SymbolWriter.MonoSymbolFile.ReadSymbolFile (Mono.Cecil.ModuleDefinition module, System.String filename) [0x00000] in <filename unknown>:0
    at Mono.Cecil.Mdb.MdbReaderProvider.GetSymbolReader (Mono.Cecil.ModuleDefinition module, System.String fileName) [0x00000] in <filename unknown>:0
    at Mono.Cecil.ModuleReader.ReadSymbols (Mono.Cecil.ModuleDefinition module, Mono.Cecil.ReaderParameters parameters) [0x00000] in <filename unknown>:0
    at Mono.Cecil.ModuleReader.CreateModuleFrom (Mono.Cecil.PE.Image image, Mono.Cecil.ReaderParameters parameters) [0x00000] in <filename unknown>:0
    at Mono.Cecil.ModuleDefinition.ReadModule (System.IO.Stream stream, Mono.Cecil.ReaderParameters parameters) [0x00000] in <filename unknown>:0
    at Mono.Cecil.AssemblyDefinition.ReadAssembly (System.IO.Stream stream, Mono.Cecil.ReaderParameters parameters) [0x00000] in <filename unknown>:0
    at AssemblyUpdater.Core.AssemblyUpdaterContext.ReadAssembly (System.String assemblyPath, IAPIUpdaterListener listener, FileAccess mode, System.String[] searchPaths) [0x00000] in <filename unknown>:0
    at AssemblyUpdater.C
     
  49. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    945
    @Ortath
    That error is actually harmless and should only happen on import, you can safely ignore it as long as SpriteSharp actually works.
    That said, I'm discovering the source of this, this should be gone in next update.
     
  50. jay-jin

    jay-jin

    Joined:
    Jun 22, 2014
    Posts:
    12