Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Pixel Perfect with Orthographic Camera

Discussion in 'Scripting' started by Frieza, Jun 8, 2013.

  1. Frieza

    Frieza

    Joined:
    Aug 29, 2008
    Posts:
    68
    Hi there, I'm suffering a bit of an issue with rendering elements pixel perfect in an orthographic camera.

    I'm mostly there, I just have an annoying artifact I want to overcome.
    I have two cameras in scene, one is perspective and one is orthographic. The orthographic camera is rendering my user interface.

    I use WorldToScreenPoint to translate a position into screen space from the perspective camera, and then ScreenToWorldPoint on my orthographic camera (note: both cameras occupy the same screen space).

    This works but I am suffering an issue where I have elements 'in-between' pixel space. They are also pixel perfect, except for this one flaw. See attached images:

    $correct.jpg
    This is what I'm shooting for. Note: it is 128x16

    But I can sometimes get this:
    $incorrect.jpg
    which, again is 128x16 so things are coming out pixel perfect in terms of size, but it seems that my object is positioned over a 'pixel border' in screen space and so the graphics engine is forced to drop a pixel and gain a pixel in places. I was wondering if there's any way I can correct this issue?

    Many Thanks!

    (Also: please don't suggest that I use GUI. classes or buy something off the asset store to solve this - it isn't the solution I'm looking for, I've made specific decisions to render my UI with an orthographic camera - and I want my UI to batch!).
     
  2. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Pixel perfect is only perfect up to the point of your screen resolution, if it won't fit it can't be pixel perfect, if you're scaling anything. There is nothing you can do about it except raise your resolution, or don't use any form of scaling ie keep 1:1 pixel ratios. But that will mean you need a different set of artwork for every resolution, which imho isn't remotely feasible.

    However if you generate artwork for your SMALLEST resolution that you support, going up should minimise this effect. In the other brothers, we blatantly ignore the issue - it does it all the time and not one customer has seen fit to notice it. I guess it's because for most people playing games, it's a non issue. It's a huge problem in the minds of developers, not the game playing public.

    The alternative workaround is to use trilinear filtering, and anisotropic, with high res pixel art work (!) which generally gets you to a slightly fuzzy but crisp look. Ultimately, you're a victim of the GPU and point filtering, and there is nothing that can be done except ensure the resolution of your assets are designed for the lowest possible resolution display.

    If you are certain you are matching the screen resolution then the next step is to make your orth camera size half the screen height, then all pixels become 1:1 in size.
     
    Last edited: Jun 8, 2013
  3. Frieza

    Frieza

    Joined:
    Aug 29, 2008
    Posts:
    68
    That doesn't make much sense; my texture is 128x16, it's being rendered as 128x16 no matter what resolution I'm using. If I pause the game and literally just poke the object up by a few fractions it'll render without the unsightly pixel bleed.
     
  4. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    That is related to a whole different aspect: Handling of subpixel rendering.
    Depending on hardware, it can be favorable or horrible if stuff is rendered at subpixel positions (means with X,Y that are not ints), on other cards and OS it is actually better to be offset by .5
     
  5. Frieza

    Frieza

    Joined:
    Aug 29, 2008
    Posts:
    68
    Yep, this is exactly my problem Dreamora, not sure what I can do to handle it effectively. If I take my game window, put it to free aspect and resize, it'll sometimes be spot on and sometimes sit on a subpixel position that doesn't work out for me.