Version: 2019.4
ShaderLab: defining a Pass
ShaderLab: assigning tags to a Pass

ShaderLab: assigning a name to a Pass

This page contains information on using a Name block in your ShaderLabUnity’s language for defining the structure of Shader objects. More info
See in Glossary
code to assign a name to a Pass. For information on defining Passes, see ShaderLab: defining a Pass. For information on how a ShaderA program that runs on the GPU. More info
See in Glossary
object works, and the relationship between Shader objects, SubShaders and Passes, see Shader object fundamentals.

A Pass can have a name. You need to reference a Pass by name in the UsePass command, and in some C# APIs. The name of a Pass is visible in the Frame Debugger tool.

Render pipeline compatibility

Feature name Built-in Render Pipeline Universal Render Pipeline (URP) High Definition Render Pipeline (HDRP) Custom SRP
ShaderLab: Name block Yes Yes Yes Yes

Using the Name block

To assign a name to a Pass in ShaderLab, you place a Name block inside a Pass block.

Signature Function
Name "<name>" Sets the name of the Pass.

Internally, Unity converts the name to uppercase. When you reference the name in ShaderLab code, you must use the uppercase variant; for example, if the value is “example”, you must reference it as EXAMPLE.

If more than one Pass in the same SubShader has the same name, Unity uses the first Pass in the code.

Using the Pass name with C# scripts

To access the name of a Pass from C# scriptsA piece of code that allows you to create your own Components, trigger game events, modify Component properties over time and respond to user input in any way you like. More info
See in Glossary
, you can use APIs such as Material.FindPass, Material.GetPassName, or ShaderData.Pass.Name.

Note: Material.GetShaderPassEnabled and Material.SetShaderPassEnabled do not reference Passes by name; instead, they reference Passes using the value of the LightMode tag.

Examples

This example code creates a Shader object called ContainsNamedPass, which contains a Pass called ExampleNamedPass.

Shader "Examples/ContainsNamedPass"
{
    SubShader
    {
        Pass
        {    
              Name "ExampleNamedPass"
            
              // The rest of the code that defines the Pass goes here.
        }
    }
}

You can then use the following C# code to query the name of this Pass:

using UnityEngine;

public class GetPassName : MonoBehaviour
{
    // Place this script on a GameObject with a MeshRenderer component
    
    void Start() {
        // Get the material
        var material = GetComponent<MeshRenderer>().material;

        // Get the name of the first Pass in the active SubShader
        // of the Shader object assigned to the material
        var passName = material.GetPassName(0);

        // Print the name to the console
        Debug.Log(passName);
    }
}

ShaderLab: defining a Pass
ShaderLab: assigning tags to a Pass
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961