ShaderLab syntax: GrabPass
GrabPass is a special passtype - it grabs the contents of the screen where the object is about to be drawn into a texture. This texture can be used in subsequent passes to do advanced image based effects.
Syntax
The GrabPass belongs inside a subshader. All properties are optional.
GrabPass { TextureScale 0.5 TextureSize 256 BorderScale 0.3 // following are regular pass commands Tags { "LightMode" = "VertexLit" } Name "BASE" }
Properties
TextureSize | Specifies that you want the grabbed texture to have a certain pixel dimension. |
TextureScale | Specifies that you want the texture to be a certain scale of the object's screen size. This is the default behaviour. |
BorderScale | Specifies that you want to grab an extra region around the object. The value is relative to the object's bounding box. |
Details
You can grab the screen behind the object being rendered in order to use it in a later pass. This is done with the GrabPass command. In a subsequent pass, you can access the grabbed screen as a texture, distorting what is behind the object. This is typically used to create stained glass and other refraction-like effects.
- The region grabbed from the screen is available to subsequent passes as _GrabTexture texture property.
- After grabbing, BorderScale gets converted into screenspace coordinates as _GrabBorderPixels property. This is the maximum amount you can displace within subsequent passes.
Unity will reuse the screen texture between different objects doing GrabPass. This means that one refractive object will not refract another.
Example
So Much For So Little
Here is the most complex way ever of rendering nothing.
Shader "ComplexInvisible" { SubShader { // Draw ourselves after all opaque geometry Tags { "Queue" = "Transparent" } // Grab the screen behind the object into _GrabTexture, using default values GrabPass { } // Render the object with the texture generated above. Pass { SetTexture [_GrabTexture] { combine texture } } } }
This shader has two passes: First pass grabs whatever is behind the object at the time of rendering, then applies that in the second pass. Note that the _GrabTexture is configured to display at the exact position of the object - hence it becomes transparent.