描述:子着色器允许您将着色器对象分离为与不同硬件、渲染管线和运行时设置兼容的部分。
子着色器包含:
- 有关此子着色器与哪些硬件、渲染管线和运行时设置兼容的信息
- 子着色器标记,它们是键值对,提供有关子着色器的信息
- 一张或多张Pass
SubShader
{
}
Tags { “[name1]” = “[value1]” “[name2]” = “[value2]”}
Queue tag
描述:该标签告诉 Unity 要将其渲染的几何体用于哪个渲染队列。渲染队列是决定 Unity 渲染几何体顺序的因素之一
Signature | Function |
---|---|
“Queue” = “[queue name]” | 使用命名的渲染队列 |
“Queue” = “[queue name] + [offset]” | 使用与命名队列有给定偏移量的未命名队列。这有用的一个例子是透明水,您应该在不透明对象之后但在透明对象之前绘制它 |
Signature | Value (类型) | Function |
---|---|---|
[queue name] | Background | 指定后台呈现队列 |
Geometry | 指定几何体渲染队列 | |
AlphaTest | 指定 AlphaTest 呈现队列 | |
Transparent | 指定透明呈现队列 | |
Overlay | 指定叠加呈现队列 | |
[offset] | integer | 指定 Unity 相对于命名队列呈现未命名队列的索引 |
RenderPipeline tag
Signature | Value (类型) | Function |
---|---|---|
[name] | UniversalRenderPipeline | This SubShader is compatible with URP only. |
HighDefinitionRenderPipeline | This SubShader is compatible with HDRP only. | |
(any other value, or not declared) | This SubShader is not compatible with URP or HDRP. |
Tags { "RenderPipeline" = "UniversalRenderPipeline" }
(any other value, or not declared) This SubShader is not compatible with URP or HDRP.
RenderType tag
Signature | Function |
---|---|
“RenderType” = “[renderType]” | B使设置此子着色器的渲染类型值用命名的渲染队列 |
ForceNoShadowCasting tag
DisableBatching tag
IgnoreProjector tag
PreviewType tag
CanUseSpriteAtlas tag
描述:使用此技术可以微调不同硬件上的着色器性能。当 SubShader 理论上由用户的硬件支持,但硬件无法很好地运行它时,这很有用.Unity优先考虑值较低的子着色器.
顺序:当 Unity 首次使用着色器对象渲染几何体时,或者当着色器 LOD 值或活动渲染管线更改时:
Unity 遍历所有子着色器的列表并检查它们以确定它们是否:与设备硬件兼容;等于或低于当前着色器详细位置价值;并与活动渲染管线兼容。
- 如果列表包含一个或多个满足这些要求的子着色器,则会选择第一个子着色器。这是活动的子着色器。
- 如果列表不包含任何满足所有要求的子着色器:
- 如果列表包含一个或多个满足硬件要求(但不满足 LOD 或渲染管线要求)的子着色器,Unity 将选择第一个子着色器。这是活动的子着色器。
- 如果列表中不包含任何满足硬件要求的子着色器,Unity 将显示错误着色器。
eg:LOD 200
描述:一个Pass包含:
- 标签,它们是键值对,提供有关Pass的信息
- 在运行着色器程序之前更新渲染状态的说明
- 着色器程序,组织成一个或多个着色器变体
Pass
{
}
Name ""
Name "ExampleNamedPass"
PackageRequirements{"com.unity.render-pipelines.universal": "[10.2.1,11.0]""com.unity.textmeshpro": "3.2"}
Pass{ Name "ExamplePassName"Tags { "ExampleTagKey" = "ExampleTagValue" }// ShaderLab commands go here.// HLSL code goes here.}
Shader "Unlit/SimpleUnlitTexturedShader"
{Properties{// we have removed support for texture tiling/offset,// so make them not be displayed in material inspector[NoScaleOffset] _MainTex ("Texture", 2D) = "white" {}}SubShader{Pass{CGPROGRAM// use "vert" function as the vertex shader#pragma vertex vert// use "frag" function as the pixel (fragment) shader#pragma fragment frag// vertex shader inputsstruct appdata{float4 vertex : POSITION; // vertex positionfloat2 uv : TEXCOORD0; // texture coordinate};// vertex shader outputs ("vertex to fragment")struct v2f{float2 uv : TEXCOORD0; // texture coordinatefloat4 vertex : SV_POSITION; // clip space position};// vertex shaderv2f vert (appdata v){v2f o;// transform position to clip space// (multiply with model*view*projection matrix)o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);// just pass the texture coordinateo.uv = v.uv;return o;}// texture we will samplesampler2D _MainTex;// pixel shader; returns low precision ("fixed4" type)// color ("SV_Target" semantic)fixed4 frag (v2f i) : SV_Target{// sample texture and return itfixed4 col = tex2D(_MainTex, i.uv);return col;}ENDCG}}
}