本页面包含有关在 ShaderLab 代码中使用 Tags
代码块为通道分配标签的信息。它还包含有关使用 LightMode
标签的信息。
有关 Shader 对象的工作原理以及 Shader 对象、子着色器与通道之间关系的信息,请参阅 Shader 对象基础。
标签是可以分配给通道的键值对数据。Unity 使用预定义的标签和值来确定如何以及何时渲染给定的通道。您还可以使用自定义值创建自己的自定义通道标签,并从 C# 代码访问它们。
最常用的预定义通道标签是 LightMode
标签;用于所有渲染管线。其他通道标签因渲染管线而异。有关更多信息,请参阅以下页面:
对于内置渲染管线中的预定义通道标签,请参见内置渲染管线中的预定义通道标签。 有关通用渲染管线 (URP) 中预定义的通道标签,请参阅 URP 通道标签。
功能名称 | 内置渲染管线 | 通用渲染管线 (URP) | 高清渲染管线 (HDRP) | 自定义 SRP |
---|---|---|---|---|
ShaderLab:通道标签代码块 | 是 | 是 | 是 | 是 |
ShaderLab:LightMode 通道标签 | 是 | 是 | 是 | 是 |
要在 ShaderLab 中为通道指定标签,您可以在 Pass
代码块内放置一个 Tags
代码块。
请注意,子着色器和通道都使用 Tags
代码块,但其工作方式不同。向通道分配子着色器标签没有效果,反之亦然。区别在于放置 Tags
代码块的位置:
Tags
代码块置于 Pass
代码块内部。Tags
代码块置于 SubShader
代码块内部,但是在 Pass
代码块外部。有关向子着色器分配标签的信息,请参阅向子着色器分配标签。
签名 | 功能 |
---|---|
Tags {"<name1>" = "<value1>" "<name2>" = "<value2>"} |
将给定标签应用于通道。 可以定义所需数量的标签。 |
从 C# 脚本访问通道的名称,您可以使用 Shader.FindPassTagValue API。这适用于 Unity 的预定义通道标签,以及您创建的自定义通道标签。
注意:有几个 API 可以直接与 LightMode 通道标签一起使用。有关更多信息,请参阅通过 C# 脚本使用 LightMode 标签。
LightMode
标签是一个预定义的通道标签,Unity 使用它来确定是否在给定帧期间执行该通道,在该帧期间 Unity 何时执行该通道,以及 Unity 对输出执行哪些操作。
注意: LightMode
标签与 LightMode 枚举无关,后者与光照有关。
每个渲染管线都使用 LightMode
标签,但预定义的值及其含义各不相同。有关更多信息,请参阅语法和有效值。
在内置渲染管线中,如果不设置 LightMode
标签,Unity 会在没有任何光照或阴影的情况下渲染通道;这本质上相当于 LightMode
的值为 Always
。在可编程渲染管线中,您可以使用 SRPDefaultUnlit
值来引用没有 LightMode 标签的通道。
签名 | 功能 |
---|---|
“LightMode” = “[value]” | 设置此通道的 LightMode 值。 |
此标记的有效值取决于渲染管线。
对于内置渲染管线中的 LightMode 通道标签,请参见内置渲染管线中的预定义通道标签。 有关通用渲染管线 (URP) 中的 LightMode 通道标签值,请参阅 URP 通道标签:LightMode。
Material.SetShaderPassEnabled and ShaderTagId use the value of the LightMode
tag to determine how Unity handles a given Pass.
在可编程渲染管线中,您可以为 LightMode
标签创建自定义值。然后,通过配置一个 DrawingSettings 结构,您可以使用这些自定义值来确定在给定 ScriptableRenderContext.DrawRenderers 调用期间要绘制哪些通道。有关更多信息和代码示例,请参阅在自定义可编程渲染管线中创建一个简单的渲染循环。
Shader "Examples/ExampleLightMode"
{
SubShader
{
Pass
{
Tags { "LightMode" = "Always" }
// 此处是定义通道的代码的其余部分。
}
}
}
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.