Legacy Documentation: Version 4.6.2
Language: English
Performance Tips when Writing Shaders
Custom Material Editors

Rendering with Replaced Shaders

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Sumbission failed

For some reason your suggested change could not be submitted. Please try again in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Some rendering effects require rendering a scene with a different set of shaders. For example, good edge detection would need a texture with scene normals, so it could detect edges where surface orientations differ. Other effects might need a texture with scene depth, and so on. To achieve this, it is possible to render the scene with replaced shaders of all objects.

Shader replacement is done from scripting using Camera.RenderWithShader or Camera.SetReplacementShader functions. Both functions take a shader and a replacementTag.

It works like this: the camera renders the scene as it normally would. the objects still use their materials, but the actual shader that ends up being used is changed:

  • If replacementTag is empty, then all objects in the scene are rendered with the given replacement shader.
  • If replacementTag is not empty, then for each object that would be rendered:
    • The real object’s shader is queried for the tag value.
    • If it does not have that tag, object is not rendered.
    • A subshader is found in the replacement shader that has a given tag with the found value. If no such subshader is found, object is not rendered.
    • Now that subshader is used to render the object.

So if all shaders would have, for example, a “RenderType” tag with values like “Opaque”, “Transparent”, “Background”, “Overlay”, you could write a replacement shader that only renders solid objects by using one subshader with RenderType=Solid tag. The other tag types would not be found in the replacement shader, so the objects would be not rendered. Or you could write several subshaders for different “RenderType” tag values. Incidentally, all built-in Unity shaders have a “RenderType” tag set.

Lit shader replacement

When using shader replacement the scene is rendered using the render path that is configured on the camera. This means that the shader used for replacement can contain shadow and lighting passes (you can use surface shaders for shader replacement). This can be useful for doing rendering of special effects and scene debugging.

Shader replacement tags in built-in Unity shaders

All built-in Unity shaders have a “RenderType” tag set that can be used when rendering with replaced shaders. Tag values are the following:

  • Opaque: most of the shaders (Normal, Self Illuminated, Reflective, terrain shaders).
  • Transparent: most semitransparent shaders (Transparent, Particle, Font, terrain additive pass shaders).
  • TransparentCutout: masked transparency shaders (Transparent Cutout, two pass vegetation shaders).
  • Background: Skybox shaders.
  • Overlay: GUITexture, Halo, Flare shaders.
  • TreeOpaque: terrain engine tree bark.
  • TreeTransparentCutout: terrain engine tree leaves.
  • TreeBillboard: terrain engine billboarded trees.
  • Grass: terrain engine grass.
  • GrassBillboard: terrain engine billboarded grass.

Built-in scene depth/normals texture

A Camera has a built-in capability to render depth or depth+normals texture, if you need that in some of your effects. See Camera Depth Texture page. Note that in some cases (depending on the hardware), the depth and depth+normals textures can internally be rendered using shader replacement. So it is important to have the correct “RenderType” tag in your shaders.

Performance Tips when Writing Shaders
Custom Material Editors