Unity 中的所有着色器文件都是用名为“ShaderLab”的声明性语言编写的。在文件中,通过嵌套括号
语法声明着色器的各个描述,例如,Material Inspector 中显示的着色器属性,要执行哪些类型的硬件回退,以及要使用哪些类型的混合模式。实际着色器代码
编写在同一着色器文件中的 CGPROGRAM
代码片段中。有关更多信息,请参阅编写表面着色器和编写顶点和片元着色器。
本页以及子页面将介绍嵌套括号“ShaderLab”语法。CGPROGRAM
代码片段是用常规
HLSL/Cg 着色语言编写的,请参阅相关文档页面。
Shader 是着色器文件的根命令。每个文件必须定义一个(且仅一个)Shader。Shader 指定当对象的材质使用此着色器时如何渲染此类对象。
Shader "name" { [Properties] Subshaders [Fallback] [CustomEditor] }
定义着色器。此着色器将以指定的名称显示在材质检视面板中。着色器可以选择性地定义在材质检视面板中显示的属性列表。在此之后是子着色器的列表,以及可选的回退和/或自定义编辑器声明。
着色器可以具有属性列表。着色器中声明的所有属性都会显示在 Unity 中的材质检视面板中。典型的属性包括对象颜色、纹理或者着色器要使用的任意值。
每个着色器均包含一组子着色器。必须至少包含一个子着色器。加载着色器时,Unity 将检查子着色器的列表,并选取最终用户的机器支持的第一个子着色器。如果不支持任何子着色器,Unity 将尝试使用回退着色器。
不同显卡具有不同功能。这给游戏开发者带来了一个永恒的问题;您希望游戏在最新款硬件上呈现出色的画面,但又不希望这些出色画面只能在数量仅占 3% 的最新款硬件上呈现。这时候就需要使用子着色器。可创建一个子着色器,使其包含您所能想象的所有奇幻图形特效,然后为型号更老的显卡添加更多子着色器。这些子着色器可能会以更慢的速度来实现您想要的效果,或者可能选择不实现某些细节。
着色器的“细节级别”(LOD) 和“着色器替换”是两种同样基于子着色器的技术,请参阅着色器 LOD 和着色器替换以了解想象信息。
下面是最简单的着色器之一:
// 有色顶点光照
Shader "Simple colored lighting"
{
// 单个颜色属性
Properties {
_Color ("Main Color", Color) = (1,.5,.5,1)
}
// 定义一个子着色器
SubShader
{
// 子着色器中的单个通道
Pass
{
// 使用固定函数每顶点光照
Material
{
Diffuse [_Color]
}
Lighting On
}
}
}
此着色器定义了颜色属性 _Color__(在材质检视面板中显示为 _Main Color_),默认值为 (1,0.5,0.5,1)__。然后,定义了单个子着色器。该子着色器包含一个 Pass,用于启用固定函数顶点光照并为其设置基本材质。
请参阅表面着色器示例或 顶点和片元着色器示例中提供的更复杂示例。
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.