Unity Blending

When rendering graphics, after all shaders have executed and all textures have been applied, pixels are written to the screen. How these pixels are combined with existing pixels is controlled by the Blend command. Used to generate transparent objects.

"Introduction to Unity Shader Essentials" roughly explains:

After the fragment passes the stencil test and the depth test, the blending step is performed. If mixing is turned on, the GPU will take out the source color (the color value of the fragment shader) and the target color (the color value in the color buffer) and use a mixing function to perform the mixing operation.

Hybrid Simplified Flowchart

Document syntax:

Blend Off: Turn off blending (this is the default)

Blend SrcFactor DstFactor: Configure and enable blending. The resulting color will be multiplied by the SrcFactor . The existing color on the screen is multiplied by DstFactor , and the two values ​​are added. (SrcFactor DstFactor fills in the mixing coefficient to control RGBA)

Blend SrcFactor DstFactor, SrcFactorA DstFactorA: Same as above, but use different coefficients to blend the alpha channel. (SrcFactor DstFactor fills in the mixing coefficient to control RGB , SrcFactorA DstFactorA fills in the mixing coefficient to control the Alpha (A) channel)

BlendOp Op: Instead of adding the blended colors together, perform different operations on them. (Op filling mixed operation control RGBA)

BlendOp OpColor, OpAlpha: Same as above, but use different blend operations for the color (RGB) channel and the alpha (A) channel. (OpColor filling and mixing operations control RGB, OpAlpha filling and mixing operations control Alpha (A) channel)

Mixed operation syntax (all that only support DX will not be repeated):

Add adds the source and target.

Sub subtracts target from source.

RevSub subtracts source from target.

Min uses the smaller of source and destination.

Max uses the larger of source and destination.

Mixing coefficient syntax:

One value is 1 - let the source or destination color pass through.

A Zero value of 0 - removes the source or target value.

SrcColor The value of this stage multiplied by the source color value.

SrcAlpha The value of this stage multiplied by the source Alpha value.

The DstColor value at this stage is multiplied by the framebuffer source color value.

DstAlpha The value of this stage is multiplied by the framebuffer source alpha value.

OneMinusSrcColor The value of this stage to be multiplied by (1 - source color).

OneMinusSrcAlpha The value of this stage multiplied by (1 - source Alpha).

OneMinusDstColor The value of this stage to be multiplied by (1 - destination color).

OneMinusDstAlpha The value of this stage multiplied by (1 - target Alpha).

The official documentation also intimately gives some common mixed types:

Blend SrcAlpha OneMinusSrcAlpha // 传统透明度
Blend One OneMinusSrcAlpha // 预乘透明度
Blend One One // 加法
Blend OneMinusDstColor One // 软加法
Blend DstColor Zero // 乘法
Blend DstColor SrcColor // 2x 乘法

Show results

1、Blend One One

Adds two color values

Displayed when the background panel is bluish

Displayed when the background panel is all white, the effect picture here is all white, which will be the same as the background color of the article and cannot be seen

Note that it becomes all white here, because the addition of the two color values ​​exceeds (1,1,1,1). So it is all white.

2. Blend SrcAlpha OneMinusSrcAlpha traditional transparency

The source color is multiplied by the source Alpha value, the target color is multiplied by 1-source color Aplha value, and then added

Sample project download

おすすめ

転載: blog.csdn.net/st75033562/article/details/129409187
おすすめ