Search Unity

  1. Click here to see what's on sale for the "Best of Super Sale" on the Asset Store
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Pixel perfect background resize possible?

Discussion in '2D' started by Avalin, Oct 6, 2020.

  1. Avalin

    Avalin

    Joined:
    Oct 12, 2018
    Posts:
    65
    Hi!

    I am creating a game that should be playable from multiple resolutions.
    I made an image 1920x1080 pixels, it looks like this:
    upload_2020-10-6_18-7-57.png
    (2x upscaled for preview)

    What I get in my (otherwise pixel perfect game) is this:

    upload_2020-10-6_18-7-22.png
    (Also 2x upscaled for preview)

    RIP dither.

    I resize the background with this method:

    Code (CSharp):
    1.     void ResizeSpriteToScreen()
    2.     {
    3.         var sr = GetComponent<SpriteRenderer>();
    4.         if (sr == null) return;
    5.  
    6.         transform.localPosition = new Vector3(0, 0, 0);
    7.         Vector2 offset = new Vector3(0.2F, 0.2F, 0.2F);
    8.  
    9.         var width = sr.sprite.bounds.size.x;
    10.         var height = sr.sprite.bounds.size.y;
    11.  
    12.         var worldScreenHeight = Camera.main.orthographicSize * 2.0;
    13.         var worldScreenWidth = worldScreenHeight / Screen.height * Screen.width;
    14.  
    15.         Vector2 newLocalScale = new Vector2((float) (worldScreenWidth / width), (float) (worldScreenHeight / height));
    16.         transform.localScale = newLocalScale + offset;
    17.     }
    I can fix it to be pixel perfect by adjusting the offset to 1 or 0.5f, but the background will be far too upscaled.
    I have set the background to repeat, point (no filter) and no compression.
    In the sprite editor I adjusted the green line to be just below my dither. As such:
    upload_2020-10-6_18-12-15.png

    I have a zoom feature in my game, and I need to keep my background the same size but also fill out the viewport which changes when I zoom.

    Anyone with ideas to crispify the background?
     

    Attached Files:

unityunity