ShaderLab: Blending

25 篇文章 0 订阅
25 篇文章 0 订阅

ShaderLab: Blending

Blending是用来实现透明物体的。

当图像被渲染,在所有shader运行后和所有纹理提供后,像素会被写到屏幕上。它们如何与已经存在的内容结合在一起,是由混合命令控制的。

Syntax

Blend Off: 关闭blending (this is the default)

Blend SrcFactor DstFactor: 启动 blending. 生成的颜色乘以 SrcFactor. 屏幕上已经存在的颜色乘以 DstFactor 然后将两者相加。

Blend SrcFactor DstFactor, SrcFactorA DstFactorA: 同上,但使用不同的因素来混合alpha通道.

BlendOp Op: 不要将混合色相加,对它们进行不同的操作。

BlendOp OpColor, OpAlpha: 同上,但使用不同的混合操作来混合颜色(RGB)和alpha(A)通道。

此外你可以设置upper-rendertarget blending modes. 当使用多目标渲染时(MRT), 上面的语法设置了相同的混合模式给所有目标. 下面的语法可以设置不同的混合模式给不同的渲染目标,N所在位置是渲染目标的索引(0..7)。这个特征在大多现代APIs/GPUs上有效(DX11/12, GLCore, Metal, PS4):

  • Blend N SrcFactor DstFactor
  • Blend N SrcFactor DstFactor, SrcFactorA DstFactorA
  • BlendOp N Op
  • BlendOp N OpColor, OpAlpha

AlphaToMask On:Turns on alpha-to-coverage. 当 MSAA 被使用, alpha-to-coverage多重采样范围遮罩按比例修改像素Shader的alpha值. 这通常用于得到比常规alpha test少的轮廓走样; 对于植被等其他alpha-tested Shaders有效。

Blend operations

以下blend操作可用:

operationsdescription
Add添加源和目标。
Sub目标加上源。
RevSub从目标减去源。
Min使用目标和源种较小的。
Max使用目标和源种较大的。
LogicalClearLogical operation: Clear (0) DX11.1 only.
LogicalSetLogical operation: Set (1) DX11.1 only.
LogicalCopyLogical operation: Copy (s) DX11.1 only.
LogicalCopyInvertedLogical operation: Copy inverted (!s) DX11.1 only.
LogicalNoopLogical operation: Noop (d) DX11.1 only.
LogicalInvertLogical operation: Invert (!d) DX11.1 only.
LogicalAndLogical operation: And (s & d) DX11.1 only.
LogicalNandLogical operation: Nand !(s & d) DX11.1 only.
LogicalOrLogical operation: Or (s
LogicalNorLogical operation: Nor !(s
LogicalXorLogical operation: Xor (s ^ d) DX11.1 only.
LogicalEquivLogical operation: Equivalence !(s ^ d) DX11.1 only.
LogicalAndReverseLogical operation: Reverse And (s & !d) DX11.1 only.
LogicalAndInvertedLogical operation: Inverted And (!s & d) DX11.1 only.
LogicalOrReverseLogical operation: Reverse Or (s
LogicalOrInvertedLogical operation: Inverted Or (!s

Blend factors

以下的所有属性对于在Blend command的SrcFactor & DstFactor都有效。 Source 是指计算的颜色, Destination 是已经在屏幕上的颜色. 如果BlendOp被使用作为逻辑操作,混合系数会被忽略。

factorsdescription
OneThe value of one - 使用此项可以使源或目标颜色完全通过。
ZeroThe value zero - 使用此方法可删除源或目标值。
SrcColor此阶段的值乘以源颜色值.
SrcAlpha此阶段的值乘以源alpha值.
DstColor此阶段的值乘以帧缓冲区源色值.
DstAlpha此阶段的值乘以帧缓冲区源alpha值.
OneMinusSrcColorThe value of this stage is multiplied by (1 - source color).
OneMinusSrcAlphaThe value of this stage is multiplied by (1 - source alpha).
OneMinusDstColorThe value of this stage is multiplied by (1 - destination color).
OneMinusDstAlphaThe value of this stage is multiplied by (1 - destination alpha).

Details

以下是最常见的混合类型:

Blend SrcAlpha OneMinusSrcAlpha // Traditional transparency
Blend One OneMinusSrcAlpha // Premultiplied transparency
Blend One One // Additive
Blend OneMinusDstColor One // Soft Additive
Blend DstColor Zero // Multiplicative
Blend DstColor SrcColor // 2x Multiplicative

Alpha blending, alpha testing, alpha-to-coverage

主要用于绘制完全不透明或完全透明的物体, 透明度是由纹理的alpha通道定义的 (e.g. leaves, grass, chain fences etc.), 常用的几种方法:

Alpha blending

常规alpha混合

这通常意味着对象必须被视为“半透明”,因此不能使用一些渲染特性 (例如: 延迟阴影,不能接收阴影)。 凹形或重叠alpha-blended对象,经常也有绘制顺序问题。

通常, alpha-blended Shaders 也设置 transparent render queue, 并关闭深度写入(depth writes). So the Shader code looks like:

// inside SubShader
Tags { "Queue"="Transparent" "RenderType"="Transparent" "IgnoreProjector"="True" }

// inside Pass
ZWrite Off
Blend SrcAlpha OneMinusSrcAlpha

Alpha testing/cutout

clip() in pixel Shader

通过在像素Shader中使用 clip() HLSL 指令, 一个像素可以被“丢弃”或不基于某些标准。这意味着对象仍然可以被视为完全不透明的, 并且没有绘制顺序的问题. 然而, 这意味着所有的像素都是不透明或全透明的 导致混叠(“锯齿”)。

经常, alpha-tested Shaders 也设置 cutout render queue, so the Shader code looks like this:

// inside SubShader
Tags { "Queue"="AlphaTest" "RenderType"="TransparentCutout" "IgnoreProjector"="True" }

// inside CGPROGRAM in the fragment Shader:
clip(textureColor.a - alphaCutoffValue);

Alpha-to-coverage

AlphaToMask On, at 4xMSAA

当使用多重采样抗锯齿(MSAA, see QualitySettings), 它是有可能提高alpha testing通过alpha-to-coverage GPU 功能。这提高了边缘的外观,这取决于所使用的 MSAA 等级.

这个功能在大部分是全透明或不透明并且有很薄的“部分透明”区域的纹理上效果很好(grass, leaves and similar).

Often, alpha-to-coverage Shaders also set cutout render queue. So the Shader code looks like:

// inside SubShader
Tags { "Queue"="AlphaTest" "RenderType"="TransparentCutout" "IgnoreProjector"="True" }

// inside Pass
AlphaToMask On

Example

这是一个小的例子Shader用来添加纹理到任何已经存在屏幕上的东西:

Shader "Simple Additive" {
    Properties {
        _MainTex ("Texture to blend", 2D) = "black" {}
    }
    SubShader {
        Tags { "Queue" = "Transparent" }
        Pass {
            Blend One One
            SetTexture [_MainTex] { combine texture }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值