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

What does ScreenPosition.a means in shader graph?

Discussion in 'Shaders' started by garlicdipping101, Jun 11, 2020.

  1. garlicdipping101

    garlicdipping101

    Joined:
    Jun 3, 2020
    Posts:
    7
    I've search around and checked out Documentation, but cannot understand what exactly does ShaderGraph's ScreenPosition node's b and a components meaning.

    Isn't r and g components enough to represent 2d screen position?
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    For most of the modes, the b and a are hard coded 0.0 values.

    However in the "raw" mode this is passing along the original interpolated clip space position z and w. The z/w (or rather b/a in shader graph) is the raw depth buffer value for that pixel, similar to the raw value you'd get from the Scene Depth node. The w or a depends on if you're using a perspective or orthographic camera. With an orthographic projection the w is always 1.0. With a perspective camera it's the view depth, equivalent to the Position node's negative b if set to Space View (view space is -Z forward).

    Why is that the value for a? Because of how perspective correct interpolation works. The Screen Position node's rg values in the default mode are equal to rg/a in the raw mode. That's actually how the default mode calculates those values from the interpolated inputs. When using an orthographic projection you don't need to do any special handling of interpolated values, so the a is just always 1.0. As I said before, b/a is the depth, because the zw values are copies of the unmodified clip space position the vertex shader outputs and that the GPU normally uses to calculate the screen depth by doing that same z/w in the hardware.
     
  3. garlicdipping101

    garlicdipping101

    Joined:
    Jun 3, 2020
    Posts:
    7
    Whoa, that's something I should write down and read over and over again. Thanks for the answer!