Search Unity

Sprite Dicing (free, open-sourced)

Discussion in 'Assets and Asset Store' started by Elringus, Jul 29, 2018.

  1. Elringus

    Elringus

    Joined:
    Oct 3, 2012
    Posts:
    483
    Project on GitHub: github.com/Elringus/SpriteDicing
    Any questions and/or suggestions are welcome in this thread or via the GitHub issues.






    Sprite Dicing is an editor extension which allows to split up a set of large sprite textures into small chunks, discard identical ones, bake them into atlas textures and then seamlessly reconstruct the original sprites at runtime for render.

    This technique allows to significantly reduce build size, in cases when multiple textures with identical areas are used. Consider a visual novel type of game, where you have multiple textures per character, each portraying a different emotion; most of the textures space will be occupied with the identical data, and only a small area will vary:



    These original five textures have total size of 17.5MB. After dicing, the resulting atlas texture will contain only the unique chunks, having the size of just 2.4MB. We can now discard the original five textures and use the atlas to render the original sprites, effectively compressing source textures data by 86.3%:


    How to use:
    1. Create a `DicedSpriteAtlas` asset using `Assets -> Create -> Diced Sprite Atlas` menu command, select it;
    2. Specify `Input Folder` — project directory, containing the source textures you wish to process. You can simply drag-drop a folder from the project hierarchy window into the field;
    3. Press Build Atlas button and wait for the generation procedure to finish;
    4. Sub-assets will appear inside the atlas asset, representing the original sprites; select any of them and drop to the scene.
    You can optionally configure the atlas generation settings via the inspector editor window. Consult the tooltips for information on available options.

     
    Last edited: Apr 17, 2022
  2. tng2903mm

    tng2903mm

    Joined:
    May 17, 2018
    Posts:
    16
    Wow this is no small feat. Thank you very much for open it up
     
    Elringus likes this.
  3. zackblack

    zackblack

    Joined:
    May 17, 2015
    Posts:
    76
    This looks like a fantastic package! Any particular reason this is setup so that it can't be executed at runtime? (all of the fields and methods are private) I'm looking at modifying it to run from a directory in Application.persistentDataPath...
     
  4. Elringus

    Elringus

    Joined:
    Oct 3, 2012
    Posts:
    483
    Well, the whole point is to optimize memory usage and size of the builds; can't see why the atlas generation may be needed at runtime. Not sure it's even possible, as some of the Unity APIs could be editor-only.
     
  5. zackblack

    zackblack

    Joined:
    May 17, 2015
    Posts:
    76
    I have a sprite heavy app that I am looking to shift towards dynamically loaded content from a web server. I need to be able to download and pack a few hundred sprites into atlases at runtime. I currently have a utility running that works well for this - except it doesn't do the dicing part!
     
    Last edited: Aug 1, 2019
  6. zackblack

    zackblack

    Joined:
    May 17, 2015
    Posts:
    76
    ...That being said, I realize now how slow this it going to be on mobile. Might have to just used un-diced sprites on the downloadable content. Cheers for this great utility tho! :-D
     
    Elringus likes this.
  7. zackblack

    zackblack

    Joined:
    May 17, 2015
    Posts:
    76
    I'm getting a weird dicing/rendering artifact, as you can see with the image on the left:
     

    Attached Files:

  8. Elringus

    Elringus

    Joined:
    Oct 3, 2012
    Posts:
    483
    Can you please open an issue on the GitHub with the reproduction steps? Not sure if I'll have time for this in the near feature, but someone else might look into this.
     
  9. zackblack

    zackblack

    Joined:
    May 17, 2015
    Posts:
    76
    Done! One other question: Is there a convenient way to set a sprite through code if I only have a name (not a reference), like SetDicedSprite( "SpriteName" )?
     
  10. Elringus

    Elringus

    Joined:
    Oct 3, 2012
    Posts:
    483
    You can get the reference to a sprite by its name from the atlas.
     
  11. zackblack

    zackblack

    Joined:
    May 17, 2015
    Posts:
    76
    Found another issue: The sprite dicing algorithm is removing any white space, making fixed-size sprites (32x32, 64x64, etc.) coming out smaller than the really are, which is messing up alignment. For example, if I have a small graphic that animates by moving around the corners of a 32x32 square, the animations just all get cropped to the actual sprite, so it no longer animates. Does that make sense how I described it? I feel like the dicing algorithm should take into account where it trims the white space and adjusts the pivot point to compensate.
     
  12. Elringus

    Elringus

    Joined:
    Oct 3, 2012
    Posts:
    483
    Thanks for filing the stuff on GitHub! It's hard for me to comment on the matter, as I've worked on the asset quite a time ago. I'll probably be able to give the asset some love in the next 2-3 month and will make sure to check all the issues.
     
  13. BQT

    BQT

    Joined:
    Dec 20, 2018
    Posts:
    6
    Hello there,
    I just import this asset to my project but lots of errors popup related to assembly reference.
    i wanted to upload img but getting error for that.
    dice.PNG
     
  14. Elringus

    Elringus

    Joined:
    Oct 3, 2012
    Posts:
    483
    Hi! Try cloning the repository project and opening it with a compatible Unity version.
     
  15. Nekonism

    Nekonism

    Joined:
    Jan 3, 2020
    Posts:
    1
    Any way to obtain the original sprites only from the generated sliced image? Like doing the inverse thing
     
  16. Elringus

    Elringus

    Joined:
    Oct 3, 2012
    Posts:
    483
    Not sure what you mean. There's no "original sprites" in the context of the solution, just the textures. If you mean obtaining the original textures, that's not possible and shouldn't be possible, as the whole point of the solution is to discard the original data and keep only the relevant dices.
     
  17. Sherwinnie

    Sherwinnie

    Joined:
    Jan 24, 2020
    Posts:
    19
    Is it possible to use the generated asset in UI? As an Image for example?
     
  18. Elringus

    Elringus

    Joined:
    Oct 3, 2012
    Posts:
    483
    It's possible, just enable "Use Sprite Mesh".

     
  19. heroeines

    heroeines

    Joined:
    Jul 23, 2019
    Posts:
    2
    Hello there,
    I just add this package to my Demo-master project, a sample project from getting started guide from naninovel website, but when i import package Naninovel (v1.14) then it show two erorr in my console, my Unity version is 2019.4.28f1 personal, here is the screenshot : upload_2021-6-27_2-47-42.png
     
  20. Elringus

    Elringus

    Joined:
    Oct 3, 2012
    Posts:
    483
    Hi, Please contact Naninovel support: https://naninovel.com/support/#naninovel-support
     
  21. heroeines

    heroeines

    Joined:
    Jul 23, 2019
    Posts:
    2
  22. vresu

    vresu

    Joined:
    Sep 26, 2011
    Posts:
    4
    @Elringus how to use spritedicong in naninovel characters? this is good tools.
    usually i use sprite mesh. but i cannot drag into character naninovel.
     
  23. Elringus

    Elringus

    Joined:
    Oct 3, 2012
    Posts:
    483