Search Unity

[WIP] Tight Mesh Sprites - Configurable and Optimized

Discussion in 'Works In Progress - Archive' started by ZimM, Apr 18, 2015.

  1. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    963
    UPDATE: SpriteSharp has since been released and is available on Asset Store:
    https://www.assetstore.unity3d.com/en/#!/content/37599
    Go check it now :)

    ------------------------
    Hi everyone,

    as you probably know, sprites in Unity have two different options for sprite meshes - Full Rect and Tight.
    Full Rect is nothing more than a single quad, which is fine, but results into huge overdraw, as seemingly empty regions of the sprites are actually still rendered, consuming the GPU fill rate, which is especially in short supply on mobile platforms. Tight meshes avoids this issue by constructing a mesh that follows the contours of the sprite based on the texture alpha. This is a really awesome feature of 2D in Unity, the only problem is... there is no way to control the generation of the sprite mesh, and quite often Unity generates absurdly high-poly meshes, pretty much defeating the whole performance gain from this optimization This is long-known issue, but apparently, there is no fix to this day:
    http://forum.unity3d.com/threads/tight-sprite-mesh-generating-terrible-meshes.247102/
    http://issuetracker.unity3d.com/issues/triangle-and-quad-sprites-can-generate-far-too-many-vertices

    But when Unity 5 was released, I've noticed a new API that allows overriding the sprite meshes. And so the work began to give better control over sprite meshes. Here are a few current results (Unity mesh on the left, mesh generated by my tool on the right):
    comparison1.png comparison2.png comparison3.png
    The generated mesh is generally much cleaner, with less triangles to render.

    You can change the mesh settings per-sprite, even if sprites are coming from the same texture. It also integrates nicely into Inspector, allowing to preview the new sprite mesh:
    inspector.png

    It won't work with Unity 4, as the API only became available in Unity 5, but I believe that's not a problem for most people.

    I am thinking about putting this on the Asset Store for maybe $10. Does this seems like a fair price to you? It's pretty much finished and just needs a lot of polish, but probably you guys have some ideas on what can be added.

    All things considered, what do you think about this?
     
    Last edited: Jul 27, 2016
  2. kylerk

    kylerk

    Joined:
    Jul 4, 2014
    Posts:
    4
    Great work! Keep it up.
     
  3. LightSource

    LightSource

    Joined:
    Sep 29, 2012
    Posts:
    249
    Wow, I'm impressed. Nicely done.
     
  4. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    963
    Added multi-sprite editing, apply/revert buttons and undo/redo functions, so now it pretty much looks and feels like it has always been there :)
     
    theANMATOR2b likes this.
  5. kylerk

    kylerk

    Joined:
    Jul 4, 2014
    Posts:
    4
    What is your timeline for getting this in the Asset store?
     
  6. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    963
    Somewhere at the middle of May.
     
    Last edited: Apr 28, 2015
  7. zenGarden

    zenGarden

    Joined:
    Mar 30, 2013
    Posts:
    4,538
    I'm very surprised Unity engineeres not proposing some option really o_O
     
    Stef_Morojna likes this.
  8. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    963
    Well, AFAIK, they are not doing that deliberately, with the justification that the amount of vertices generated by Unity is never to cause problems even on mobile platforms. And while vertex processing generally takes much less time draw pixel processing, that is obviously not entirely true:
    1) If you are drawing a lot of sprites, the difference between 1000 triangles and 100 triangles per sprite is increasingly big.
    2) Also, SpriteRenderers are only batched up to a certain amount of polygons (somewhere like 5k), because they are batched on the CPU, and there is an overhead of doing that. So decreasing the polygon count could in fact decrease the amount of draw calls and CPU load.
    3) Unity doesn't takes into account that sprites can actually be scaled, because, well, it has no knowledge about how sprites will actually look in game. There is no point of making highly-tesselated mesh for an object that typically takes a small amount of screen space anyway. That's why a manual control is required.
     
  9. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    Have you considered an option for maximizing opaque / minimizing semitransparent areas?
     
  10. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    963
    There is an "alpha tolerance" parameter that controls just that. Basically, all pixels with alpha lower than the alpha tolerance value are considered opaque and are included to the mesh.
     
  11. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    I'm talking about using two submeshes/sprite renderers. It's worth doing if the opaque areas cover a lot of screen area. I don't know if it's even feasible in Unity yet.
     
  12. theANMATOR2b

    theANMATOR2b

    Joined:
    Jul 12, 2014
    Posts:
    7,790
    Tomorrow is the middle of May

    :)

    Looking forward to release
     
  13. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    963
    Ah, makes sense. I believe this can be done, but at the very least this'd mean doubling the amount of sprites, since mesh is generated per-sprite, so that'd be a bit of inconvenience. But otherwise this is a great idea, I'll probably try to implement this in some future update.
    Well, I said "somewhere at the middle of May" :) I've just came back home from a trip and hope to release it next week.
     
    theANMATOR2b and Jessy like this.
  14. Haagndaaz

    Haagndaaz

    Joined:
    Feb 20, 2013
    Posts:
    232
    This is such a neat tool!
     
  15. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    963
    OK everyone, I've sent the first version to the Asset Store for approval :) It usually takes around a week for them to accept a new asset, so hopefully it all goes smoothly.
     
    theANMATOR2b likes this.
  16. ZimM

    ZimM

    Joined:
    Dec 24, 2012
    Posts:
    963