在 Unity 中,当您使用的着色器属于图形管线 的一部分,通常用到 Shader 类的实例。我们将一个 Shader
类的实例称为 Shader 对象。
Shader 对象是 Unity 使用着色器程序的特定方式;它是着色器程序和其他信息的封装器。它允许您在同一个文件中定义多个着色器程序,并告诉 Unity 如何使用它们。
功能名称 | 内置渲染管线 | 通用渲染管线 (URP) | 高清渲染管线 (HDRP) | 自定义 SRP |
---|---|---|---|---|
Shader 对象 | 是 | 是 | 是 | 是 |
Shader 对象包含着色器程序、更改 GPU 上设置的指令(统称为渲染状态)以及告诉 Unity 如何使用它们的信息。
您将 Shader 对象与材质共同使用来确定场景的外观。
您可以通过两种方式创建 Shader 对象。分别具有各自类型的资源:
.shader
扩展名的文本文件。无论您以何种方式创建 Shader 对象,Unity 在内部都以相同的方式表示结果。
Shader 对象具有嵌套结构。它将信息按照名为子着色器和通道的结构进行组织。它将着色器程序组织成着色器变体。
Shader 对象包含:
您还可以定义附加信息,例如共享着色器代码,或是否使用自定义编辑器。有关定义 Shader 对象的信息,请参阅 ShaderLab:定义一个 Shader 对象。
子着色器用于将 Shader 对象分成多个部分,分别兼容不同的硬件、渲染管线和运行时设置。
一个子着色器包含:
您还可以定义其他信息,例如对其所有通道通用的渲染状态。有关可在子着色器中定义的所有信息,请参阅 ShaderLab:定义一个子着色器。
通道包含:
您还可以定义其他信息,例如名称。有关可在通道中定义的所有信息,请参阅 ShaderLab:定义一个通道。
通道包含的着色器程序被组织成着色器变体。着色器变体共享通用代码,通过启用或禁用给定关键字,使其具有不同功能。
通道中的着色器变体的数量取决于您在着色器代码中定义的关键字数量以及目标平台。每个通道包含至少一个变体。
有关更多信息,请参阅着色器关键字和变体。
这是 Unity 如何在所有渲染管线中使用 Shader 对象渲染几何体的高级描述。
在 Unity 使用 Shader 对象之前:
当 Unity 首次使用 Shader 对象渲染几何体时,或者当着色器 LOD 值或活动渲染管道更改时:
Unity 可以识别使用相同着色器变体的几何体并将其组织成批次实现更高效的渲染。每帧一次,对于每批几何体:
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.