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

Introduction to the shader programming language

3.2.1. ShaderLab Cull

This post is also available in…

This property, compatible in both Built-in RP and Scriptable RP, controls which face of a polygon will be removed in the pixel depth processing. What does this mean? Recall that a polygon object has inner faces and outer ones. By default, the outer faces are visible (Cull Back); however, we can activate the inner faces by following the scheme shown below.

  • Cull Off. Both faces of the object are rendered. 
  • Cull Back. The back faces of the object are rendered, by default. 
  • Cull Front. The front faces of the object are rendered.

This command has three values ​​which are: Back, Front and Off. By default, “Back” is active, however, generally, the line of code associated with culling is not visible in the shader for optimization purposes. If we want to modify the culling options, we must add the word “Cull” followed by the mode we want to use. 

Shader “InspectorPath / shaderName”
{
    Properties {} ...
    SubShader
    {
        // Cull Off
        // Cull Front
        Cull Back
    }
}

We can also dynamically configure the Culling options in the Unity Inspector through the dependency “UnityEngine.Rendering.CullMode” which is declared from the Enum drawer and passed as an argument in the function.

Shader "InspectorPath/shaderName"
{
    Properties 
    {
        [Enum(UnityEngine.Rendering.CullMode)] 
        _Cull ("Cull", Float) = 0
    }
    SubShader
    {
        Cull [_Cull]
    }
}

Another helpful option occurs through the semantics SV_IsFrontFace, which allows us to project different colors and textures on both mesh faces. To do so, we simply declare a boolean variable and assign such semantics as an argument in the fragment shader stage.

fixed4 frag (v2f i, bool face : SV_IsFrontFace) : SV_Target
{
    fixed4 colFront = tex2D(_FrontTexture, i.uv);
    fixed4 colBack = tex2D(_BackTexture, i.uv);

    return face ? colFront : colBack;
}

It is worth mentioning that this option only works when the “Cull” command has been previously set to “Off” in the shader.

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.