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
    1) I've just tested, and the pivot is always preserved for me. I couldn't find a way to make SpriteSharp lose the pivot point information. Are you doing something specific?
    2) It might be possible, but I don't plan to do that. Most importantly, because it would almost never make sense to convert each and every sprite to alpha separated. That option should really be used carefully, on a per-sprite basis, since there is no good way to know how exactly those sprites will be used in the game.
    Sprite mesh editor sounds like a nice idea, but in reality that isn't worth the time for most sprites. Making the mesh editor would take a lot of my time, and I really can't justify doing that for 1% of sprites that might actually benefit from manually edited meshes. I am not planning to implement that in observable future.

    Sprites/Opaque-Diffuse shader is easy. Will do!

    It would definitely be a lot of nicer to have a single GameObject instead of two. Unfortunately, Unity only allows a single SpriteRenderer to be attached to a GameObject, and there's no easy way to get around that. It can be hacked around, but with the cost of a special custom component attached to a GameObject, that will have to run for each frame and synchronize all the stuff. But that would be quite really bad for performance, and let me remind you that the whole idea is to increase the performance, so... Really, that would make things even worse. I guess you'll have to bear with 2 GameObjects for Alpha Separated sprites.

    Sprite-Opaque supports Sorting Layer/ Order In Layer just fine, thing is - as you've already noticed - that opaque stuff in rendered in a different Render Queue. Basically, this means that all opaque objects will be rendered before all transparent objects, and Sorting Layer/ Order In Layer would only work between those opaque objects. Setting the Render Queue to Transparent will result in overdraw again, since objects will be drawn back-to-front, so don't do that :)

    Oh, and nice catch regarding c.rgb *= c.a;, I'll remove that for next SpriteSharp release as well.

    Well, as far as I understand, Unity just sees no problem at all in the current sprite mesh generation, so why would they do that? :p
     
  2. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    963
    Yep! I was busy with other stuff lately, but SpriteSharp update is coming soon :)
     
  3. Stef_Morojna

    Stef_Morojna

    Joined:
    Apr 15, 2015
    Posts:
    289
    Hmmmm ok, makes sense. Then I guess ill just try program it myself.

    So I got a script that takes the vertices and allows me to midify them, then I got a triangulator script that returns me the vertices and indices.

    Now the only thing I need to know is how to put the indices/verices (or the mesh idk...) into the sprites mesh.
     
    Last edited: Jul 24, 2016
  4. AmirNaid

    AmirNaid

    Joined:
    Jul 11, 2016
    Posts:
    3
    Hi, since the latest update (unity 5.4), Sprite sharp does not respond anymore, please release the update as soon as possible.
     
  5. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    963
    Could you please elaborate? What do you mean by "does not respond"? I've tested SpriteSharp with Unity 5.4 and it worked fine. I've just tried it again and found no issues.
     
  6. AmirNaid

    AmirNaid

    Joined:
    Jul 11, 2016
    Posts:
    3
    By not respond I mean...it's supposed to reduce vertices and triangles and overdraws, right? (like it was working well in previous Versions of Unity), now it's not doing anything, after hitting apply button in mesh settings, nothing changes, like you have never applied your configurations...(assuming you have tested it in unity 5.4, like you said you did) do you think something is wrong with my editor? or do I have to reinstall SpriteSharp?
    Let me give you an example:

    Unity 5.3:
    original Vertices = 258 and Triangles = 120.
    After Applying SpriteSharp Mesh Settings:
    Vertices = 53 and Triangles = 43.

    Unity 5.4: (SAME Image.png)
    original Vertices = 258 and Triangles = 120.
    After Applying SpriteSharp Mesh Settings:
    Vertices = 258 and Triangles = 120.

    Thank you in advance.
     
    Last edited: Aug 19, 2016
  7. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    963
    It's hard to say what's going on without seeing it personally. Could you please create a new Unity project, import SpriteSharp into it, and see if it works in that project?
     
  8. AmirNaid

    AmirNaid

    Joined:
    Jul 11, 2016
    Posts:
    3
    We Already Did and it's not working.
     
  9. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    963
    That's strange. Could you ZIP and PM me this newly created project? I'll test at my machine, so at least we'll know if this is an issue with your Editor.
     
  10. Helical

    Helical

    Joined:
    Mar 2, 2014
    Posts:
    50
    Hi, we have a problem with version control and sprite sharp integration. After applying the sprite changes (which are awesome) and committing them. Other stations received the changes in meta, but their sprite meshes were not updated. This could be manually fixed by reimporting those assets which will force the changes made by the plugin to take effect, but its not a very good solution because each time someone inserts a sprite everybody will have to re import that specific sprite.

    How do you get around this?
     
  11. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    963
    I assume you are using Sprite Packer?.. If so, then this is a known Unity bug. Basically, Unity doesn't thinks that a sprite mesh change is a good enough reason to repack the sprite atlases, so what you end up seeing are old atlases with old sprite meshes.
    Fortunately, SpriteSharp includes a workaround for this. Just click "Tools → Lost Polygon → SpriteSharp → Clear Sprite Packer Atlas Cache" to force Unity to repack atlases. After they are repacked, you should immediately see the new meshes.
    If the issue is not related to sprite atlases in your case, try "Tools → Lost Polygon → SpriteSharp → Reimport Overriden Sprites". This will automatically reimport all the sprites that are using SpriteSharp meshes.

    There was also an issue with sprite meshes sometimes not updating when Cache Server is server, but it is fixed in SpriteSharp 2.3.0, so try to update first.
     
  12. ZimM

    ZimM

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

    SpriteSharp has been updated to version 2.3.0, the update will be available on the Asset Store in a few hours.

    Changelog:
    • Added Rectangle Grid processing method. Useful for creating custom vertex shaders that modify vertex positions.
    • Fixed an issue with sprite meshes not applying when Cache Server is enabled.
    • Added "Sprites-OpaqueDiffuse" shader for opaque lighted sprites.
    • Minor fixes and improvements.
     
    Shadeless likes this.
  13. Shadeless

    Shadeless

    Joined:
    Jul 22, 2013
    Posts:
    136
    @ZimM

    Hey, so if I used Sprite.Create during runtime to make a sprite, is there any way to also use SpriteSharp during runtime to optimize the sprite?

    Cheers
     
  14. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    963
    No. This is a limitation of Unity, it doesn't allows changing the sprite mesh in runtime. Nothing I can do with that.
     
  15. Shadeless

    Shadeless

    Joined:
    Jul 22, 2013
    Posts:
    136
  16. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    963
    Last edited: Sep 11, 2016
    BTStone likes this.
  17. Erikoinen

    Erikoinen

    Joined:
    Nov 10, 2014
    Posts:
    68
    I bought this in hopes of doing similar optimization for Image -components (their sprites) within the UI system, but the wireframes keep showing just quads instead of tight optimized meshes.

    I guess it's more about Unity not supporting it with the UI -system, since their own tight -system doesn't work in it either.

    Any knowledge on this? Thanks.
     
  18. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    963
    Indeed, Unity's UI system doesn't supports tight meshes, no nice way to get around that. What you can do, however, is to use sprites in the UI directly, like @Marrt proposed here:
    https://forum.unity3d.com/threads/spritesharp-sprite-mesh-optimizer.327997/#post-2175457
     
  19. Marrt

    Marrt

    Joined:
    Feb 7, 2012
    Posts:
    613
  20. keox

    keox

    Joined:
    Dec 4, 2014
    Posts:
    1
    Hey there, I think I just found out what the problem is.
    While the main alignment is being preserved correctly, the generated alpha separation sprites don't get the alignment copied over (so they always default to center/ in the spritesheet part of the meta file, alignment is 0).

    Edit: Hm, I looked into it and it seems like there is no nice way to get the alignment int value, that is shown in the dropdown next to Pivot (worst case, I'd read the alignment value directly from the .meta yaml in TightMeshSettingsDrawer. EnableMultipleSpriteMode(), but that seems really hacky :/ ).
     
    Last edited: Dec 2, 2016
  21. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    963
    Again, can't confirm. The metadata for the alpha separation sprites is copied as a whole. For the test, I've picked some random sprite, set its pivot to Bottom Right, enabled alpha separation, and both opaque and alpha sprites have preserved the pivot (just FYI, the pivot is contained in SpriteMetaData.pivot). Could you perhaps record and PM me a video showing the problem?
     
  22. Shadeless

    Shadeless

    Joined:
    Jul 22, 2013
    Posts:
    136
    Hey is there something like a Rect Tight Mode that works like this:

    So I have a sprite sheet that is cut like this and the pivot has to be at the bottom left points.



    Its very simple and it saves a lot of overdraw for me. I don't want to use the Precise mode because I have a lot of sprites and it would add a lot of vertices. I want the meshes to be Rects.

    Cheers
     
  23. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    963
    You don't really need SpriteSharp for this, just re-slice the sprites so they are rects instead of squares, and then move the pivot point outside of sprite. Though I agree, it might be much easier with SpriteSharp, but unfortunately, there is no such mode.
    But I think you can actually use Precise mode here. For example, for the top two items on your screenshot, if you set the Edge Inflation to 0, you will get a simple rect. And for the crowns, Edge Inflation of 1 or 2 should also work fine without introducing a lot of vertices.
     
  24. Shadeless

    Shadeless

    Joined:
    Jul 22, 2013
    Posts:
    136
    I know I can do it manually but I have hundreds probably thousands of sprites... This was just a simple small sample.

    I think this would be a really easy to add mode and it would be a great addition. And I really don't wanna use Precise mode on a lot of the sprites I have. I'm using it for characters since I have few of them in comparison to decorative sprites.
     
  25. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    963
    Wait, I'm stupid... You can already use the Rect Grid for this. Just set the X/Y Subdivisions to 1, enable "Cull by Bounding Box", and set Edge Inflation to 0. Should work exactly how you need it.
     
  26. Shadeless

    Shadeless

    Joined:
    Jul 22, 2013
    Posts:
    136
    Yeah! That's why I was asking if there was a way to do it already.
    I can't set X/Y Subdivisions to less than 2, can this be added somehow? :)

    Cheers!

    Edit: I meant less than 2 yes
     
    Last edited: Dec 6, 2016
  27. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    963
    You mean "less than 2"? Yeap, I couldn't believe it myself... Will update today :)
     
    Shadeless likes this.
  28. GalaxyRainGames

    GalaxyRainGames

    Joined:
    Dec 22, 2016
    Posts:
    2
  29. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    963
    Hi, can you elaborate how this is better than the current system with single database file? It is a regular JSON file that is easily versionable and human-readable. Putting all the data in the AssetImporter.userData property would break any other scripts that use this field, and would make it harder to read and not really diffable. So in the end it would probably be worse than it is now.
     
  30. GalaxyRainGames

    GalaxyRainGames

    Joined:
    Dec 22, 2016
    Posts:
    2
    Thanks for the answer.
    For example now it is impossible simultaneously to multiple developers to customize texture settings and not get merge conflicting (for the artist and level disigners its difficult). When I change settings for more than 500 sprites in one file is difficult to find that I changed. If settings for each texture save in her .meta its more easier to see (provided that Asset Serialization Force Text is enable) what and where the developer changed. It is easier to undo changes to a single sprite. It will be possible to change the texture settings cooperatively without merge conflicts.
    Yes its break any other scripts that use this field, but it's can be optional.
    In any case, thanks for such useful plugin =)
     
  31. BAIZOR

    BAIZOR

    Joined:
    Jul 4, 2013
    Posts:
    112
    Hi, thanks for the great plugin. But after updating Unity, we have strange behavior.
    SpriteSharp doesn't work at all. The texture is Tight and has alpha (from gray scale). But SpriteSharp drop it.
    What is going on?
    Best regards
    upload_2017-1-7_21-40-21.png
     
  32. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    963
    I'll see if this could be done as an option, but I can't promise anything. Unity's asset import pipeline is weird when it comes to details...
    Which version of Unity are you seeing this issue with, exactly? Did the problem only started after the Unity update, or perhaps you've recently updated SpriteSharp as well?
     
    Last edited: Jan 10, 2017
  33. BAIZOR

    BAIZOR

    Joined:
    Jul 4, 2013
    Posts:
    112
    My Unity version is 5.5.0f3
    And yes, I updated SpriteSharp too, to the latest version on Asset Store
     
  34. BAIZOR

    BAIZOR

    Joined:
    Jul 4, 2013
    Posts:
    112
    There is how the texture looks in inspector window. SpriteSharp doesn't work at all right now. It works just with that textures, which before update were transparent. But if they had transparency from grayscale, they doesn't work at all. And I nothing can do. Tried to change format, add transparency, doesn't help.
    upload_2017-1-10_19-36-46.png
     
  35. zzajac

    zzajac

    Joined:
    Feb 20, 2015
    Posts:
    22
    There is problem with Unity 5.6.0b4:

    Code (CSharp):
    1. AmbiguousMatchException: Ambiguous matching in method resolution
    2. at System.Reflection.Binder.FindMostDerivedMatch (System.Reflection.MethodBase[] match) [0x0002a] in /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Reflection/Binder.cs:106
    3. at System.MonoType.GetMethodImpl (System.String name, BindingFlags bindingAttr, System.Reflection.Binder binder, CallingConventions callConvention, System.Type[] types, System.Reflection.ParameterModifier[] modifiers) [0x000f8] in /Users/builduser/buildslave/mono/build/mcs/class/corlib/System/MonoType.cs:245
    4. at System.Type.GetMethod (System.String name, BindingFlags bindingAttr) [0x00011] in /Users/builduser/buildslave/mono/build/mcs/class/corlib/System/Type.cs:787
    5. at LostPolygon.SpriteSharp.BuildPlatformsUtility..cctor () [0x00000] in <filename unknown>:0
    6. Rethrow as TypeInitializationException: An exception was thrown by the type initializer for LostPolygon.SpriteSharp.BuildPlatformsUtility
    7. at (wrapper managed-to-native) System.Runtime.CompilerServices.RuntimeHelpers:RunClassConstructor (intptr)
    8. at System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor (RuntimeTypeHandle type) [0x00026] in /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Runtime.CompilerServices/RuntimeHelpers.cs:101
    9. at UnityEditor.EditorAssemblies.ProcessEditorInitializeOnLoad (System.Type type) [0x00008] in C:\buildslave\unity\build\Editor\Mono\EditorAssemblies.cs:136
     
  36. BAIZOR

    BAIZOR

    Joined:
    Jul 4, 2013
    Posts:
    112
    In Unity 5.5.0. Sprite doesn't work at all. I told about it in my previous posts
    Code (CSharp):
    1. InvalidOperationException: Check IsExists first
    2. LostPolygon.SpriteSharp.Database.Internal.PersistentMarker.get_Path ()
    3. LostPolygon.SpriteSharp.Gui.Internal.SpriteSharpEditorResources.get_PersistentDataPath ()
    4. LostPolygon.SpriteSharp.Database.Internal.DatabaseAssetsManager.GetDatabasePath (Boolean cached)
    5. LostPolygon.SpriteSharp.Database.Internal.DatabaseAssetPostProcessor.OnPostprocessAllAssets (System.String[] importedAssets, System.String[] deletedAssets, System.String[] movedAssets, System.String[] movedFromAssetPaths)
    6. Rethrow as TargetInvocationException: Exception has been thrown by the target of an invocation.
    UPDATE:
    Thanks for fix in 2.3.3!
     
    Last edited: Jan 24, 2017
  37. timsoret

    timsoret

    Joined:
    Apr 9, 2015
    Posts:
    16
    Hey @zinm.

    I'm working on The Last Night, and your plugin is essential to our workflow (by cutting our pixel art sprites with polygons, we can use opaque shaders & deferred renderer: this means we got access to all the cool FX that comes with it, depth of field, motion blur, lighting, shadows, reflections, etc…).

    I have only one request.

    Would it be possible to have an option to generate double-sided geometry? front-facing triangles + back-facing triangles. This way, we would avoid having a double-sided shader (unoptimized), and we would have correct normals when we flip a sprite horizontally.
     
    DanielGilbert likes this.
  38. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    963
    Unity 5.6 is in beta stage, don't expect everything to work. SpriteSharp will be updated to function with 5.6 when it is released.
    This is now fixed internally, expect an update soon.
    Any way to reproduce this? Is this somehow connected to the first issue you've mentioned?

    To answer your question - yes, this can be added, provided that there is any actual use of this feature.
    But if anything, double-sided geometry is the unoptimized way of doing what you need, since the amount of vertices would be doubled, with zero benefit. Backface culling is only beneficial performance-wise for 3D shapes. Double-sided shader is exactly what you'd want here, as with 2D shapes you can never see both sides at the same time, so there is zero performance loss. Double-sided shader doesn't means your sprites will be drawn twice.
    As for the normals - SpriteRenderer provides no normals at all, so I'm not sure how this is related.
     
    Last edited: Jan 24, 2017
  39. mml

    mml

    Joined:
    Mar 29, 2015
    Posts:
    11
    Hi ZimM,
    we're using SpriteSharp in a project with a lot of sprites, which is why we use Unity's own Sprite Packer. We would also love to use the Alpha Separation feature, but once we turn it on we get every sprite twice in our atlases. Which makes Alpha Seperation unusable for us. Are we doing something wrong or is there any way to prevent Unity from packing every sprite twice?

    Thanks,
    Felix
     
  40. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    963
    You are not doing anything wrong. Unfortunately, there is no way to avoid doubling the amount of sprites in atlases, since Unity has no idea those two sprites are actually parts of a whole, and there is no control over Unity's Sprite Packer behavior.
     
    Last edited: Jan 27, 2017
  41. TimNedvyga

    TimNedvyga

    Joined:
    May 18, 2015
    Posts:
    96
    Hey @ZimM,

    Watch the video please

    I think I got flipped faces when exporting mesh.
     
    Last edited: Feb 12, 2017
  42. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    963
    Not sure what do you mean. The object looks correctly lit, and the shadows are drawn correctly. Are talking about the disappearing center of the shadow when the light is close to an object? That is likely an artifact of how Unity renders shadows, and can be caused by many reasons. What Unity version do you use?
     
  43. TimNedvyga

    TimNedvyga

    Joined:
    May 18, 2015
    Posts:
    96
    I'm using Unity 5.5.0f3

    This is works opposite to what I expect.
    I mean when the light in front of the mesh it's need to be viewable and drop shadow.
    But at this time object drop shadow, but mesh is totally black.
    When I move light behind mesh it became viewable but do not drop shadow.

    Edit:
    Увидел, что ты с Киева. Можешь написать мне на почту timnedvyga@gmail.com или в скайп jester200906? Доставать не буду ^_^
     
    Last edited: Feb 18, 2017
  44. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    963
    Ah, I see. By default, Unity does backface culling when rendering shadows even if the shader has it disabled. To change this behavior, you need to change the value of Renderer.shadowCastingMode. Just change the shadow casting mode in Inspector like this
    upload_2017-2-18_15-45-39.png
     
  45. brownboot67

    brownboot67

    Joined:
    Jan 5, 2013
    Posts:
    375
    Is there an option to save the resulting mesh as its own file?
     
  46. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    963
    There is, right inside the sprite inspector:
    upload_2017-4-10_12-10-26.png
     
    brownboot67 likes this.
  47. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    963
  48. mahdi_jeddi

    mahdi_jeddi

    Joined:
    Jul 18, 2016
    Posts:
    246
    Is there a way to cut a huge sprite into a grid and re-assemble it into a new sprite atlas like 2DToolkit is doing?
    We have huge sprite for our backgrounds in 8K and right now I'm manually cutting them up into 2Kx2K sprites and put them on the scene. then Unity's sprite-packer packs them into 4K atlases, and because there are a lot of empty spaces in these sprites the atlas turn out a lot smaller.
     
  49. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    963
    Not at the moment. I've just made a quick prototype to test the concept:
    upload_2017-4-20_7-31-53.png

    It works pretty well, but I'm not sure if I'll finalize this. This would be a very niche feature, and it would require a lot of time to make it user-friendly and working in all conditions.
    However, it might work fine for your case. If you want, please contact me privately, and I'll send you the script.
     
    mahdi_jeddi likes this.
  50. mahdi_jeddi

    mahdi_jeddi

    Joined:
    Jul 18, 2016
    Posts:
    246
    Yes this is exactly what I was talking about. I can see that there are a few minor bugs in that image, but I think it's easy to fix. I wanted to do this myself but lack the info on how people do this in Unity. This also could be extended to make some more optimizations like this:
    https://twitter.com/desplesda/status/752318314953662464