在 Unity 中,可使用 HLSL 编程语言来编写着色器程序。
Unity 最初使用 Cg 语言,因此会使用 Unity 某些着色器关键字的名称 (CGPROGRAM
) 和文件扩展名 (.cginc
)。Unity 已不再使用 Cg,但仍支持这些关键字和文件扩展名。请注意,所有着色器程序代码都必须是有效的 HLSL,即使代码使用与 Cg 相关的关键字和文件扩展名也如此。
使用 HLSLPROGRAM
与使用 CGPROGRAM
之间的唯一区别在于 Unity 在编译着色器程序时自动包含的文件。这是为了向后兼容。有关更多信息,请参阅内置着色器 include 文件。
HLSL 语言本身有两种语法:“旧版”的 DX9 样式语法以及更现代的 DX10+ 样式语法。
不同之处主要在于纹理采样函数的工作方式:
sampler2D
、tex2D()
和类似函数。此语法适用于所有平台。Texture2D
、SamplerState
和 .Sample()
函数。由于纹理和采样器在 OpenGL 中_不是_不同对象,因此该语法的某些形式在 OpenGL 平台上无效。在 Unity 中,可使用预定义宏来声明和采样纹理,从而避免发生 HLSL 语法平台支持问题。Unity 将根据编译着色器的目标平台,将这些宏扩展为最合适的语法。
不同平台使用不同的着色器编译器来编译着色器程序,如下所述:
可以使用预定义着色器宏来确定 Unity 在使用哪个编译器(例如,使用仅一种编译器支持的 HLSL 语法)或解决编译器错误。
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.