Get an 20% OFF using the RELEASE code on your book purchase. For a limited time.

Introduction to the shader programming language

3.1.6. Render Type Tags

This post is also available in…

According to the official documentation in Unity,

“Use the RenderType tag to overwrite the behaviour of a shader”.

What does the above statement mean? Basically, with this Tag, we can change from one state to another in the SubShader, adding an effect on any material that matches a given Type.

To carry out its function, we need at least two shaders:

  1. A replacement one (color or effect that we want to add at run time).
  2. And another to be replaced (shader assigned to the material).

Its syntax is as follows:

Tags { "RenderType"="type" }

Like the Queue Tags, RenderType has different configurable values that vary depending on the task we want to perform. Among them, we can find.

  • Opaque.            Default.
  • Transparent.
  • TransparentCutout.
  • Background.
  • Overlay.
  • TreeOpaque.
  • TreeTransparentCutout.
  • TreeBillboard.
  • Grass.
  • GrassBillboard.

By default, the “Opaque” type is set every time we create a new shader. Likewise, most of the built-in shaders in Unity are assigned with this value since they do not have a configuration for transparencies. However, we can freely change this category; everything will depend on the effect we apply to a match.

To understand the concept thoroughly, we will do the following. In our project,

  1. We will make sure to create some 3D objects in the scene.
  2. We will create a C# script that we will call USBReplacementController.
  3. Then we will create a shader that we will call USB_replacement_shader.
  4. Finally, we will create a material that we will call USB_replaced_mat.

We will assign a shader over the material USB_replaced_mat dynamically using the Camera.SetReplacementShader. The material shader must have a Tag RenderType equal to the replacement shader to perform the function.

To exemplify, we will assign the Mobile/Unlit shader to USB_replaced_mat. This built-in shader has a Tag of type “RenderType” equal to “Opaque”. Therefore the shader USB_replacement_shader must match the same RenderType for the operation to be carried out.

(Fig. 024. Unlit shader (Supports Lightmap) has been assigned over the material USB_replaced_mat )

The USBReplacementController script must be assigned directly to the camera as a component. This controller will be in charge of replacing a shader with a replacement one, as long as they have the same configuration in the RenderType.

using System.Collection;
using System.Collections.Generic;
using UnityEngine;

[ExecuteInEditMode]
public class USBReplacementController : MonoBehaviour
{
    // replacement shader
    public Shader m_replacementShader;

    private void OnEnable()
    {
        if(m_replacementShader != null)
        {
            // the camera will replace all the shaders in the scene
            // whit the replacement one
            // the "RenderType" configuration must match in both
            // shader
            GetComponent<Camera>().SetReplacementShader.            (m_replacementShader,"RenderType");
        }
    }

    private void OnDisable()
    {
         // let's reset the default shader
         GetComponent<Camera>().ResetReplacementShader()
    }
}

It is worth mentioning that we have defined the [ExecuteInEditMode] function over the class. This property will allow us to preview changes in edit mode.

We will use USB_replacement_shader as a replacement shader.

As we already know, every time we create a new shader, it configures its RenderType equal to “Opaque”. Consequently, USB_replacement_shader may replace the Unlit shader we previously assigned to the material.
To preview the changes clearly, we will go to the fragment shader stage of USB_replacement_shader and add a red color, which we will multiply by the output color.

fixed4 frag (v2f i) : SV_Target
{
    fixed4 col = tex2D(_MainTex, i.uv);
    // add a red color
    fixed4 red = fixed4(1, 0, 0, 1);
    return col * red;
}

We must make sure to include USB_replacement_shader in the Shader type replacement variable found in the USBReplacementController script.

(Fig. 025. The USBReplacementController has been assigned to the camera)

And in addition, those objects that we added previously in the scene must have the material USB_replaced_mat.

(Fig. 026. The material USB_replaced_mat has been assigned to 3D objects; to a Quad, a cube and a sphere)

Since the USBReplacementController class has included the OnEnable and OnDisable functions, if we activate or deactivate the script, we can see how the built-in shader Unlit is replaced by USB_replacement_shader in edit mode, applying a red color in the rendering.

(Fig. 027. The USB_replacement_shader has replaced the built-in shader Unlit in the final rendering)

Follow us to stay informed about all the latest news, updates, and more.

Join the group to share your experiences with other developers.

Subscribe to our channel and keep learning game dev!

jettelly-logo

Jettelly Team

We are a team of indie developers with more than 9 years of experience in video games. As an independent studio, we have developed Nom Noms in which we published with Hyperbeard in 2019. We are currently developing The Unity Shader Bible.

Follow us on our social networks.