Search Unity

multiple passes and their uses

Discussion in 'Shaders' started by Jagwire, May 21, 2015.

  1. Jagwire

    Jagwire

    Joined:
    Dec 5, 2013
    Posts:
    59
    What might a shader need multiple passes for? I think I could see a blurring technique, but what other applications might there be?

    As an aside, is there a way to "send data" from one pass to another?

    thanks!
     
  2. hadynlander

    hadynlander

    Joined:
    Jan 24, 2014
    Posts:
    41
    I think there are a lot of the uses that will be unique / dependent on context, though some more common examples might be:
    • You want to render a texture/colour/whatever over the top of shadows, which have been rendered on an earlier pass. I once needed to do this for a fog of war effect - I needed to draw the fog colour in a second pass so that it applied over the top of the base texture + shadows.
    • You want to use a grabpass. You can use these to render whatever sits behind your object in the first pass, which can then be used for fun and profit in the second pass. A common example would be rendering and then distorting whatever lies beyond a water surface, or frosted glass, etc.
    • You want to create a volumetric effect that depends on the depth from the front to the back of your object - i.e. a sphere of volumetric fog, thick in the middle and faded towards the edges. This can be done by performing a depth-only first pass with frontface culling, which will render the far side of your object to the depth buffer. You then render the front faces in the second pass, comparing their camera depth against the previously rendered depth buffer to figure out how "thick" the volume is at any particular part of the surface.
    The ability to render different passes in different render queues is also potentially quite useful - i.e. for drawing "xray vision" outlines wherever a character/object is occluded by other geometry.

    To answer the second question - yes. In fact, there are at least two ways to do it that I'm aware of. The first is to use those aforementioned grabpasses. The second is to use blend commands. These control how the colours from one pass are blended with those from the previous pass - whether they multiply/add/subtract/etc. These are somewhat similar to Photoshop blending modes.

    Hope that helps!
     
    Todd-Wasson and Jagwire like this.
  3. Jagwire

    Jagwire

    Joined:
    Dec 5, 2013
    Posts:
    59
    Thanks, this helps a bunch!