Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Feature request for Sprite trimming.

Discussion in '2D' started by jesper42, Mar 24, 2014.

  1. jesper42

    jesper42

    Joined:
    Jan 11, 2011
    Posts:
    28
    When cutting out sprites from a sprite sheet it would be nice to be able to set how tight "auto slicing" and "trim" will cut.

    In my current game I have some rectangular sprites - if there is no buffer around the edges the edges will get very jagged (especially on low res devices) due to the straight edges of the sprites being on the outer edge of a piece of mesh.

    Se the difference in this example:

    $sprite-border.png

    Currently I have two manually tedious ways to deal with this issue.
    a) Move the sprite sizes out a bit manually in the sprite editor (get unbearable when having many sprites that also animates)
    b) Make some border around my sprites in GIMP that is (almost) invisible that will make Unity not cut so thighly.

    Hope this makes sense :D

    Cheers,
    Jesper Rasmussen
     
  2. SiegfriedCroes

    SiegfriedCroes

    Joined:
    Oct 19, 2013
    Posts:
    569
    You might be able to fix this with code by adding this to the Start function of a script on a sprite:

    Code (csharp):
    1.  
    2. int border = 8; // Border tickness
    3.  
    4. Rect spriteRect = GetComponent<SpriteRenderer>().sprite.rect;
    5. spriteRect = new Rect(spriteRect.x - border, spriteRect.y - border, spriteRect.width + (border * 2), spriteRect.height + (border * 2));
    6.  
    I didn't actually try this so I don't know for sure if it works but it should.
     
  3. jesper42

    jesper42

    Joined:
    Jan 11, 2011
    Posts:
    28
    Btw. I have tried the "Extrude Edges" option in the Importer settings ... but I cannot see any difference in results no matter the setting.
     
  4. jesper42

    jesper42

    Joined:
    Jan 11, 2011
    Posts:
    28
    Unfortunately I see an issue with your idea DSSiege ....

    I'm using Unity's sprite packer - thus the sprites are already packed very close together, and if we extend the borders as you suggest we'd end up with having pieces of the surrounding sprites shown in the edges.

    I think that the easiest solution is to do it at slice time :D
     
    Last edited: Mar 24, 2014
  5. SiegfriedCroes

    SiegfriedCroes

    Joined:
    Oct 19, 2013
    Posts:
    569
    You're right, I didn't think of that... I only have Unity Free so I didn't think of the Sprite packer...
     
  6. jesper42

    jesper42

    Joined:
    Jan 11, 2011
    Posts:
    28
    This issue surfaces when the sprite have one or more straight (axis aligned) edges (which is then cut too close).

    It is most visible when:

    1) the sprite is rotated at low angles (as in the example) ... or
    2) when moving the sprite (at sub pixel coordinates) the outer edge will flicker like hell
     
  7. jesper42

    jesper42

    Joined:
    Jan 11, 2011
    Posts:
    28
    Hurray .. found an easy solution to this issue :D

    Enable Anti Aliasing (i use 2x multi sample) in Edit->Project Settings->Quality

    And voila ! ... no more ugly edges ! :D
     
  8. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,741
    Well, that solution does work, but it will be at the cost of performance. Anti-aliasing after the fact is much more intensive than a little bit of border on the edges. If you end up needing more performance and have the time to spend, I'd recommend adding a couple of pixels around each of the sprites by hand - you'll get a pretty decent FPS bump from disabling anti-aliasing. And your initial feature request is still a good one.
     
  9. jesper42

    jesper42

    Joined:
    Jan 11, 2011
    Posts:
    28
    @StarManta I totally agree ...

    But for my current project where the sprite count is fairly low (max ~500) I don't think a little aa is that costly (I hope :)

    I think it is by far the easiest solution until Unity adds this feature :D
     
  10. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,741
    I don't believe the performance cost of AA has any relation to the number of sprites in a scene. All the same, if your project has performance to spare, then by all means.. :)
     
  11. jesper42

    jesper42

    Joined:
    Jan 11, 2011
    Posts:
    28
    I really should test it shouldn't I ??

    ... ok, I will test it ... and return with some real numbers ... soonish :D



    Just to be certain we're talking about the same thing.

    The AA part of the Quality is described thus in the documentation ...

    "Anti aliasing improves the appearance of polygon edges, so they are not “jagged”, but smoothed out on the screen. However, it incurs a performance cost for the graphics card and uses more video memory (there’s no cost on the CPU though). The level of anti-aliasing determines how smooth polygon edges are (and how much video memory does it consume)."

    I don't think it is mentioned anywhere in the 2D sections of Unity ... but for now it solves my issues.

    Also notice that is is not a pro feature :D