在 Unity 中,您通常使用 HLSL 编写着色器程序。要将 HLSL 代码添加到您的着色器资源,应将该代码放在一个着色器代码块 中。
本页面包含有关使用着色器代码块的信息。有关编写 HLSL 本身的信息,请参阅在 Unity 中使用 HLSL。
注意:Unity 还支持使用其他语言编写着色器程序,不过通常不需要或不推荐这样做。有关更多信息,请参阅编写着色器。
功能 | 内置渲染管线 | 通用渲染管线 (URP) | 高清渲染管线 (HDRP) | 自定义可编程渲染管线 |
---|---|---|---|---|
HLSLPROGRAM | 是 | 是 | 是 | 是 |
HLSLINCLUDE | 是 | 是 | 是 | 是 |
CGPROGRAM | 是 | 否 | 否 | 是 与使用 SRP Core 包的自定义渲染管线不兼容。 |
CGINCLUDE | 是 | 否 | 否 | 是 与使用 SRP Core 包的自定义渲染管线不兼容。 |
要添加 HLSL 代码,您可以使用以下类型的着色器代码块:
要了解何时使用哪个,您必须了解它们的前缀(HLSL
或 CG
)及后缀(PROGRAM
或 INCLUDE
)。
以 HLSL
或 CG
为前缀的块之间的区别是:
CG
are older. They include several of Unity’s built-in shader include files by default, which can be convenient if you need this functionality. The built-in includes are only compatible with the Built-in Render Pipeline.HLSL
为前缀的着色器代码块较新。默认情况下,它们不包含 Unity 的内置着色器 include 文件,因此您必须手动包含要使用的任何库代码。它们适用于任何渲染管线。For information on Unity’s built-in shader include files, see Built-in shader include files.
以 PROGRAM
或 INCLUDE
为后缀的块之间的区别是:
PROGRAM
为后缀的着色器代码块被称为着色器程序块。您可以使用它们来编写着色器程序。您在这些块中编写 HLSL 着色器代码,然后将它们放在 ShaderLab 代码中的 Pass 块中。INCLUDE
为后缀的着色器代码块被称为着色器 include 块。您可以使用它们在同一源文件中的着色器程序块之间共享公共代码。您编写要在这些块中共享的 HLSL 着色器代码,然后将它们放置在您的 ShaderLab 代码中的 Pass、SubShader 或 Shader 块中。它的工作方式与在 HLSL 代码中使用 include 的方式类似。签名 | 功能 |
---|---|
HLSLPROGRAM [着色器程序的 HLSL 源代码] ENDHLSL
|
将 HLSL 着色器程序添加到包含此着色器程序块的 Pass。不包含 Unity 的内置着色器 include 文件。 |
CGPROGRAM [着色器程序的 HLSL 源代码] ENDCG
|
Adds the HLSL shader program to the Pass that includes this shader program block. Includes several of Unity’s built-in shader include files by default, enabling you to use built-in variables and functions. |
Shader "Examples/ExampleShader"
{
SubShader
{
Pass
{
Name "ExamplePassName"
Tags { "LightMode" = "ExampleLightModeTagValue" }
// 在此编写设置渲染状态的 ShaderLab 命令
HLSLPROGRAM
// 在此编写 HLSL 着色器代码
ENDHLSL
}
}
}
签名 | 功能 |
---|---|
HLSLINCLUDE [您要共享的 HLSL 代码] ENDHLSL
|
Unity 将此代码包含在 HLSLPROGRAM 块中定义的所有着色器程序中,可位于此源文件的任何位置。 |
CGINCLUDE [您要共享的 HLSL 代码] ENDCG
|
Unity 将此代码包含在 CGPROGRAM 块中定义的所有着色器程序中,可位于此源文件的任何位置。 |
Shader "Examples/ExampleShader"
{
SubShader
{
HLSLINCLUDE
// 在此编写要共享的 HLSL 代码
ENDHLSL
Pass
{
Name "ExampleFirstPassName"
Tags { "LightMode" = "ExampleLightModeTagValue" }
// 在此编写设置渲染状态的 ShaderLab 命令
HLSLPROGRAM
// 此 HLSL 着色器程序自动包含上面的 HLSLINCLUDE 块的内容
// 在此编写 HLSL 着色器代码
ENDHLSL
}
Pass
{
Name "ExampleSecondPassName"
Tags { "LightMode" = "ExampleLightModeTagValue" }
// 在此编写设置渲染状态的 ShaderLab 命令
HLSLPROGRAM
// 此 HLSL 着色器程序自动包含上面的 HLSLINCLUDE 块的内容
// 在此编写 HLSL 着色器代码
ENDHLSL
}
}
}
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.