ShaderLab syntax: Blending 混合
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.
在所有着色器执行完毕,所有纹理都被应用,所有像素准备被呈现到屏幕之后, 使用Blend命令来操作这些像素进行混合。
指导:
Blend 混合是将源色和目标色以某种方式混合生成特效的技术。混合常用来绘制透明或半透明的物体。在混合中起关键作用的α值实际上是将源色和目标色按给定比率进行混合,以达到不同程度的透明。α值为0则完全透明,α值为1则完全不透明。混合操作只能在RGBA模式下进行,颜色索引模式下无法指定α值。物体的绘制顺序会影响到OpenGL的混合处理。
参考文章:http://hi.baidu.com/ÖÙÁÁÁÁ/blog/item/5fc565445e19714a500ffebb.html
Syntax 语法
Blend Off
- Turn off blending
- 关闭混合
- Blend SrcFactor DstFactor 混合 起始因子
- Configure & 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.
- 配置并开启混合。计算产生的颜色和srcFactore相乘。已经在屏幕上的颜色和dstFactor相乘,然后2个颜色相加。
- Blend SrcFactor DstFactor, SrcFactorA DstFactorA
- Same as above, but use different factors for blending the alpha channel.
- 和上面相同,但是有些因子不同。 SrcFactorA DstFactorA 这些因子使用alpha通道进行混合。
- BlendOp Min | Max | Sub | RevSub
- Instead of adding blended colors together, do a different operation on them.
- 不是将加入的颜色混合在一起,而是对他们做不同的操作。
Properties 属性
All following properties are valid for both SrcFactor & DstFactor. Source refers to the calculated color, Destination is the color already on the screen.
下面列出的所有属性是对SrcFactor & DstFactor有效。Source 是指计算出的颜色,Destination 指的是已经的屏幕上的颜色。
One | The value of one - use this to let either the source or the destination color come through fully.
|
Zero | The value zero - use this to remove either the source or the destination values.
|
SrcColor | The value of this stage is multiplied by the source color value.
|
SrcAlpha | The value of this stage is multiplied by the source alpha value.
|
DstColor | The value of this stage is multiplied by frame buffer source color value.
|
DstAlpha | The value of this stage is multiplied by frame buffer source alpha value.
|
OneMinusSrcColor | The value of this stage is multiplied by (1 - source color).
|
OneMinusSrcAlpha | The value of this stage is multiplied by (1 - source alpha).
|
OneMinusDstColor | The value of this stage is multiplied by (1 - destination color).
|
OneMinusDstAlpha | The value of this stage is multiplied by (1 - destination alpha).
|
Details 详情
Below are the most common blend types:
下列是最经常使用的混合类型
Blend SrcAlpha OneMinusSrcAlpha // Alpha blending alpha混合 Blend One One // Additive 相加混合 Blend One OneMinusDstColor // Soft Additive 柔和相加混合 Blend DstColor Zero // Multiplicative 相乘混合 Blend DstColor SrcColor // 2x Multiplicative 2倍相乘混合
Example 例子
Here is a small example shader that adds a texture to whatever is on the screen already:
这里是1个着色器的小例子:将一个纹理添加到屏幕上,无论它是否已经在屏幕上。
Shader "Simple Additive" { Properties { _MainTex ("Texture to blend", 2D) = "black" {} } SubShader { Tags { "Queue" = "Transparent" } Pass { Blend One One SetTexture [_MainTex] { combine texture } } } }
And a more complex one, Glass. This is a two-pass shader:
1个更复杂的着色器:玻璃。它含有2个pass
- The first pass renders a lit, alpha-blended texture on to the screen. The alpha channel decides the transparency.
第一个pass渲染光照和将纹理以alpha混合的方式渲染到屏幕上。Alpha通道决定其透明度。
The second pass renders a reflection cubemap on top of the alpha-blended window, using additive transparency.
第二个pass渲染一个反射立方体贴图在alpha混合得视窗的顶部,使用附加的透明度。
Shader "Glass" { Properties { _Color ("Main Color", Color) = (1,1,1,1) _MainTex ("Base (RGB) Transparency (A)", 2D) = "white" {} _Reflections ("Base (RGB) Gloss (A)", Cube) = "skybox" { TexGen CubeReflect } } SubShader { Tags { "Queue" = "Transparent" } Pass { Blend SrcAlpha OneMinusSrcAlpha Material { Diffuse [_Color] } Lighting On SetTexture [_MainTex] { combine texture * primary double, texture * primary } } Pass { Blend One One Material { Diffuse [_Color] } Lighting On SetTexture [_Reflections] { combine texture Matrix [_Reflection] } } } }