确定 GPU 如何将片元着色器的输出与渲染目标进行合并。
此命令的功能取决于混合操作,您可以使用 BlendOp 命令进行设置。请注意,虽然所有图形 API 和硬件都支持混合功能,但对某些混合操作的支持较为有限。
启用混合会禁用 GPU 上的一些优化(主要是删除隐藏的表面/Early-Z),这些优化会增加 GPU 帧时间。
功能名称 | 内置渲染管线 | 通用渲染管线 (URP) | 高清渲染管线 (HDRP) | 自定义 SRP |
---|---|---|---|---|
Blend | 是 | 是 | 是 | 是 |
此命令会更改渲染状态。在 Pass
代码块中使用它可为该通道设置渲染状态,或者在 SubShader
代码块中使用它可为该子着色器中的所有通道设置渲染状态。
如果启用了混合,则会发生以下情况:
Add
。Add
、Sub
、RevSub
、Min
或 Max
,GPU 会将片元着色器的输出值乘以源系数。Add
、Sub
、RevSub
、Min
或 Max
,GPU 会将渲染目标中现有的值乘以目标系数。混合等式为:
finalValue = sourceFactor * sourceValue operation destinationFactor * destinationValue
在这个等式中:
finalValue
是 GPU 写入目标缓冲区的值。sourceFactor
在 Blend 命令中定义。sourceValue
是片元着色器输出的值。operation
是混合操作。destinationFactor
在 Blend 命令中定义。destinationValue
是目标缓冲区中现有的值。签名 | 示例语法 | 功能 |
---|---|---|
Blend <state> |
Blend Off |
禁用默认渲染目标的混合。这是默认值。 |
Blend <render target> <state> |
Blend 1 Off |
如上,但针对给定的渲染目标。(1) |
Blend <source factor> <destination factor> |
Blend One Zero |
启用默认渲染目标的混合。设置 RGBA 值的混合系数。 |
Blend <render target> <source factor> <destination factor> |
Blend 1 One Zero |
如上,但针对给定的渲染目标。(1) |
Blend <source factor RGB> <destination factor RGB>, <source factor alpha> <destination factor alpha> |
Blend One Zero, Zero One |
启用默认渲染目标的混合。为 RGB 和 Alpha 值设置单独的混合系数。(2) |
Blend <render target> <source factor RGB> <destination factor RGB>, <source factor alpha> <destination factor alpha> |
Blend 1 One Zero, Zero One |
如上,但针对给定的渲染目标。(1) (2) |
注意:
GL_ARB_draw_buffers_blend
或 OpenGL ES 3.2。参数 | 值 | 功能 |
---|---|---|
render target | 整数,范围 0 到 7 | 渲染目标索引。 |
state | Off |
禁用混合。 |
factor | One |
此输入的值是 one。该值用于使用源或目标的颜色的值。 |
Zero |
此输入的值是 zero。该值用于删除源或目标值。 | |
SrcColor |
GPU 将此输入的值乘以源颜色值。 | |
SrcAlpha |
GPU 将此输入的值乘以源 Alpha 值。 | |
SrcAlphaSaturate |
The GPU multiplies the value of this input by the minimum value of source alpha and (1 - destination alpha)
|
|
DstColor |
GPU 将此输入的值乘以帧缓冲区的源颜色值。 | |
DstAlpha |
GPU 将此输入的值乘以帧缓冲区的源 Alpha 值。 | |
OneMinusSrcColor |
GPU 将此输入的值乘以(1 - 源颜色)。 | |
OneMinusSrcAlpha |
GPU 将此输入的值乘以(1 - 源 Alpha)。 | |
OneMinusDstColor |
GPU 将此输入的值乘以(1 - 目标颜色)。 | |
OneMinusDstAlpha |
GPU 将此输入的值乘以(1 - 目标 Alpha)。 |
以下是最常见的混合类型的语法:
Blend SrcAlpha OneMinusSrcAlpha // 传统透明度
Blend One OneMinusSrcAlpha // 预乘透明度
Blend One One // 加法
Blend OneMinusDstColor One // 软加法
Blend DstColor Zero // 乘法
Blend DstColor SrcColor // 2x 乘法
Shader "Examples/CommandExample"
{
SubShader
{
// 此处是定义子着色器的代码的其余部分。
Pass
{
// 为此通道启用常规 Alpha 混合
Blend SrcAlpha OneMinusSrcAlpha
// 此处是定义通道的代码的其余部分。
}
}
}
此示例代码演示在 SubShader 代码块中使用此命令的语法。
Shader "Examples/CommandExample"
{
SubShader
{
// 为此子着色器启用常规 Alpha 混合
Blend SrcAlpha OneMinusSrcAlpha
// 此处是定义子着色器的代码的其余部分。
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.