当前位置:   article > 正文

Unity Shader: Blend混合_unity shader blend

unity shader blend

Unity Shader:Blend 混合

Blending is used to make transparent objects.
在这里插入图片描述
When graphics are rendered, after all Shaders have executed and all Textures have been applied, the pixels are written to the screen. How they are combined with what is already there is controlled by the Blend command.

混合不仅仅用于透明度混合,我们要开启混合,在Unity中设置的同时也进行了开启,但如果在其他图形API中是需要我们自己手动开启的。
源颜色(Src,source color):由片元着色器产生的颜色值
目标颜色(Dst,Destination color):从颜色缓冲中读取到的颜色值
这里的颜色值都是包括RGBA四个通道

1 混合命令语法(Syntax)

Blend Off

Turn off blending (this is the default)
默认设置,关闭混合
示例:
Blend Off

Blend SrcFactor DstFactor

Configure and enable blending. The generated color is multiplied by the SrcFactor. The color already on screen is multiplied by DstFactor and the two are added together.
开启混合并设置混合因子。源颜色会乘以SrcFactor,而目标颜色会乘以DstFactor,然后把两者相加再存入颜色缓冲中
示例:
Blend SrcAlpha OneMinusSrcAlpha
透明度混合的命令

Blend SrcFactor DstFactor, SrcFactorA DstFactorA

Same as above, but use different factors for blending the alpha channel.
和上面几乎一样,只是使用了不同的因子来混合透明通道
示例:
Blend SrcAlpha OneMinusSrcAlpha, One Zero
混合后输出颜色的透明度就是源颜色的透明度

BlendOp Op

Instead of adding blended colors together, carry out a different operation on them.
并非是把源颜色和目标颜色简单相加后混合,而是使用Op(操作命令)对它们进行其他的操作
示例:
BlendOp Min
Blend One One
变暗效果(Darken)

BlendOp OpColor, OpAlpha

Same as above, but use different blend operation for color (RGB) and alpha (A) channels.
和上面几乎一样,只是使用了不同的混合操作来分别处理RGB通道和透明通道
示例:
BlendOp Add, Min

AlphaToMask On

Turns on alpha-to-coverage. When MSAA is used, alpha-to-coverage modifies multisample coverage mask proportionally to the pixel Shader result alpha value. This is typically used for less aliased outlines than regular alpha test; useful for vegetation and other alpha-tested Shaders.

2 混合因子

All following properties are valid for both SrcFactor & DstFactor in the Blend command. Source refers to the calculated color, Destination is the color already on the screen. The blend factors are ignored if BlendOp is using logical operations.

One

The value of one - use this to let either the source or the destination color come through fully.
因子的值为1。

Zero

The value zero - use this to remove either the source or the destination values.
因子的值为0.

SrcColor

The value of this stage is multiplied by the source color value.
因子为源颜色值。当用于混合RGB的混合等式时,使用SrcColor的RGB分量作为混合因子;当用于混合A通道的混合等式时,使用SrcColor的A分量作为混合因子

SrcAlpha

The value of this stage is multiplied by the source alpha value.
因子为源颜色的透明度值(A通道)

DstColor

The value of this stage is multiplied by frame buffer source color value.
因子为目标颜色值(未混合前缓冲区中的颜色值)。当用于混合RGB通道的混合等式时,使用DstColor的RGB分量作为混合因子;当用于混合A通道的混合等式时,使用DstColor的A分量作为混合因子

DstAlpha

The value of this stage is multiplied by frame buffer source alpha value.
因子为目标颜色的透明度值(A通道)

OneMinusSrcColor

The value of this stage is multiplied by (1 - source color).
因子为(1-源颜色)。当用于混合RGB通道的混合等式时,使用结果的RGB分量作为混合因子;当用于混合A通道的混合等式时,使用结果的A分量作为混合因子

OneMinusSrcAlpha

The value of this stage is multiplied by (1 - source alpha).
因子为(1-源颜色的透明度值)

OneMinusDstColor

The value of this stage is multiplied by (1 - source alpha).
因子为(1-目标颜色)。当用于混合RGB通道的混合等式时,使用结果的RGB分量作为混合因子;当用于混合A通道的混合等式时,使用结果的A分量作为混合因子

OneMinusDstAlpha

The value of this stage is multiplied by (1 - destination alpha).
因子为(1-目标颜色的透明度值)

3 混合操作

The following blend operations can be used:

Add

Add source and destination together.
将与分别与自己的混合因子相乘后的源颜色和目标颜色相加。默认的混合操作。
O_rgb=SrcFactor ×S_rgb+DstFactor ×D_rgb
O_a=SrcFactorA ×S_a+DstFactorA ×D_a

Sub

Subtract destination from source.
用混合后的源颜色减去混合后的目标颜色。
O_rgb=SrcFactor ×S_rgb−DstFactor ×D_rgb
O_a=SrcFactorA ×S_a−DstFactorA ×D_a

RevSub

Subtract source from destination.
用混合后的目标颜色减去混合后的源颜色
O_rgb=DstFactor ×D_rgb−SrcFactor ×S_rgb
O_a=DstFactorA ×D_a−SrcFactorA ×S_a

Min

Use the smaller of source and destination.
使用源颜色和目标颜色中较小的值,是逐分量比较的。
O_rgba=(min⁡(S_r,D_r ),min⁡(S_g,D_g ),min⁡(S_b,D_b ),min⁡(S_a,D_a ))

Max

Use the larger of source and destination.
使用源颜色和目标颜色中较大的值,是逐分量比较的。
O_rgba=(max⁡(S_r,D_r ),max⁡(S_g,D_g ),max⁡(S_b,D_b ),max⁡(S_a,D_a ))

其他逻辑操作

仅在DirectX11.1中支持,详见
https://docs.unity3d.com/Manual/SL-Blend.html

4 常见的混合类型

Blend SrcAlpha OneMinusSrcAlpha

正常(Normal),即透明度混合

Blend OneMinusDstColor One

柔和相加(Soft Additive)

Blend DstColor Zero

正片叠底(Multiply),即相乘

Blend DstColor SrcColor

两倍相乘(2x Multiply)

BlendOp Min 、 Blend One One

变暗(Darken)

BlendOp Max、 Blend One One

变亮(Lighten)

Blend OneMinusDstColor One

滤色(Screen)

Blend One OneMinusSrcColor

同上

Blend One One

线性减淡(Linear Dodge)

在这里插入图片描述

参考

https://docs.unity3d.com/Manual/SL-Blend.html

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/96683
推荐阅读
相关标签
  

闽ICP备14008679号