Search Unity

Resolved Target Texture Pixel Resolution

Discussion in 'Shader Graph' started by JG-Denver, Jan 24, 2021.

  1. JG-Denver

    JG-Denver

    Joined:
    Jan 4, 2013
    Posts:
    77
    I have a number of scenarios where I need to understand the pixel resolution of my target texture to adjust for the pixel ratios between the width and height.

    Right now, I am using the Screen Node for this but I have two challenges with this: 1) multi-display support is required and 2) we need to use Render Textures in a number of scenarios.

    My question on the Screen Node, is where does this node come from? The default display or camera target texture or something else?

    Is there a better approach, such as the Screen Position Node that would get me the pixel resolution width and height or ratio?

    Thank you.
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    It’s the camera’s target texture. If you render to another render texture that’s not the same resolution, there’s nothing built in that tells you what that resolution is. The only guaranteed way to get that information is to pass it to the shader yourself as a property.

    One work around that is you can abuse derivatives. Take the Screen Position node’s default output, use a split node to get the x and y values (R and G) and pass those into DDX and DDY nodes respectively. That’ll give you how much the normalized screen space position changes from one pixel to the next, which means the target resolution is (1.0 / ddx(x), 1.0 / ddy(y)). And you can use those to get the current aspect ratio.
     
    JG-Denver likes this.
  3. JG-Denver

    JG-Denver

    Joined:
    Jan 4, 2013
    Posts:
    77
    Thank you, @bgolus, great answer as always. I have learned a lot from many of your forum responses, so this gives me a chance to thank you for that as well.

    I just finally put it together that the Screen Node is just exposing the built in shader variable, _ScreenParams, which says it is the camera texture. This will work for my purposes for now, but it is good to know how to go further.

    I am learning to make sure I get past the limited shader graph docs to fill in the gaps with the shaderlab docs when I see how it maps.