本页面包含有关在 ShaderLab 代码中使用 LOD
代码块为子着色器指定 LOD(细节级别)值的信息。
可以将 LOD 值指定给子着色器。此值指示其计算方面的需求。
在运行时,您可以为单个 Shader 对象或所有 Shader 对象设置着色器 LOD 的值。然后 Unity 优先考虑具有较低 LOD 值的子着色器。有关 Unity 如何选择何时使用子着色器的信息,请参阅 Shader对象:渲染期间的操作顺序。
注意: 尽管此方法以用于渲染网格的 LOD 功能命名,但是仍然存在重要区别:着色器 LOD 与相对于摄像机的距离无关,并且 Unity 不会自动计算着色器 LOD。必须手动设置最大着色器 LOD。
使用此方法来微调不同硬件上的着色器性能。在用户的硬件理论上支持,但无法很好地运行子着色器时,这很有用。
功能名称 | 内置渲染管线 | 通用渲染管线 (URP) | 高清渲染管线 (HDRP) | 自定义 SRP |
---|---|---|---|---|
ShaderLab:子着色器 LOD 代码块 | 是 | 是 | 是 | 是 |
在 ShaderLab 中,可以通过将 LOD
代码块置于 SubShader
代码块中来为子着色器指定 LOD 值。
签名 | 功能 |
---|---|
LOD [value] | 将给定的 LOD 值指定给子着色器。 |
注意:在您的 Shader
代码块中,必须将子着色器按 LOD 降序排列。例如,如果您有 LOD 值为 200、100 和 500 的子着色器,则必须先放置 LOD 值为 500 的子着色器,然后是 LOD 值为 200 的子着色器,然后是 LOD 值为 100 的子着色器。这是因为 Unity 选择所找到的第一个有效子着色器,所以如果它首先找到一个 LOD 较低的子着色器,它将始终使用它。
要为给定的 Shader 对象设置着色器 LOD,您可以使用 Shader.maximumLOD。要为所有 Shader 对象设置着色器 LOD,您可以使用 Shader.globalMaximumLOD。默认情况下,没有最大 LOD。
此示例代码创建了一个包含两个子着色器的 Shader 对象:一个 LOD 值为 200,一个值为 100。子着色器按 LOD 值的降序排列。
Shader "Examples/ExampleLOD"
{
SubShader
{
LOD 200
Pass
{
// 此处是定义通道的代码的其余部分。
}
}
SubShader
{
LOD 100
Pass
{
// 此处是定义通道的代码的其余部分。
}
}
}
在内置渲染管线中,Unity 的内置着色器具有以下 LOD 值:
LOD 值 | 着色器名称 |
---|---|
100 | Unlit/Texture Unlit/Color Unlit/Transparent Unlit/Transparent Cutout |
300 | Standard Standard (Specular Setup) Autodesk Interactive |
在内置渲染管线中,Unity 的内置旧版着色器具有以下 LOD 值:
LOD 值 | 着色器名称 |
---|---|
100 | 顶点光照 (VertexLit) |
150 | 贴花 (Decal) 反光顶点光照 (Reflective VertexLit) |
200 | 漫射 (Diffuse) |
250 | 漫射细节 (Diffuse Detail) 反光凹凸无光照 (Reflective Bumped Unlit) 反光凹凸顶点光照 (Reflective Bumped VertexLit) |
300 | 凹凸 (Bumped) 镜面反射 (Specular) |
400 | 凹凸镜面反射 (Bumped Specular) |
500 | 视差 (Parallax) |
600 | 视差镜面反射 (Parallax Specular) |
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.