ShaderLab: Legacy Texture Combiners

来源:互联网 发布:文学系知乎 编辑:程序博客网 时间:2024/06/06 00:46

ShaderLab: Legacy Texture Combiners

当基础的顶点光照计算完成后,纹理就被应用了。在 ShaderLab 中该项用 SetTexture 命令完成。

Note: SetTexture 命令在 fragment programs 被使用时,不会生效;这种情况下,像素操作会被完整的描述在shader中。目前推荐使用可编程shader来代替 SetTexture 命令。

固定管线纹理化是实现旧式组合效果的方式。在一个pass中你可以有多个 SetTexture 命令 - 所有纹理都会依次被应用,就像绘图程序中的图层。SetTexture 命令必须放置在一个 Pass 的末尾.

Syntax

SetTexture [TextureName] {Texture Block}

指定一个 texture。 TextureName 是必须被定义的属性。texture如何被应用被定义在TextureBlock。在 texture block 内部可以有至多两个命令: combineconstantColor

Texture block combine command

combine src1 * src2: 将 src1 和 src2 相乘。结果会比两个输入都暗一些。

combine src1 + src2: 将 src1 和 src2 相加。结果会被两个输入都亮一些。

combine src1 - src2: 从 src1 中减去 src2。

combine src1 lerp (src2) src3: 使用 src2 的 alpha 值在 src3 和 src1 之间插值。注意插值是反方向的:当 alpha 是一时用 src1 ,当 alpha 是零时用 src3。

combine src1 * src2 + src3: src1 和 src2 的 alpha值相乘,然后加上 src3。

所有的 src 属性可以是previous, constant, primarytexture 中的一个。

  • Previous 是之前 SetTexture 的结果。
  • Primary 是 lighting calculation 获取的颜色,或顶点颜色,如果它是 bound.
  • Texture 是被 TextureName 定义的纹理的颜色在 SetTexture (see above)中。
  • Constant 是定义在 ConstantColor 中的颜色。

Modifiers:

  • 上述公式可以使用关键字 DoubleQuad 来使结果明亮 两倍 或 四倍。
  • 所有的 src 属性, 除了 lerp 符号, 可以在任意之前加上 一个 - 来使结果变为负数。
  • 所有的 src 属性可以加上 alpha 来只取alpha通道。

Texture block constantColor command

ConstantColor color: 定义一个可以在 combine 命令中使用的静态颜色。

Functionality removed in Unity 5.0

Unity 5.0之前的版本,不支持在 texture 块中使用 matrix 命令进行纹理坐标转换。如果你需要这些功能,应该考虑重写一个 programmable shader 来代替你的 shader,来在 vertex shader 中实现 UV 转换。

同样的, 5.0 移除了有符号的加法 (a+-b), 乘加有符号数 (a*b+-c), 乘减 (a*b-c) 和 dot (dot3, dot3rgba) 纹理组合模式。如果你需要,那么在 pixel shader中进行数学运算来代替。

Details

在 fragment programs 存在之前, 旧的显卡为纹理提供一个分层的方法。纹理被一个一个的应用,来改变颜色,最终写入显示器。对每个纹理,它们通常与前面的操作结果组合起来。现如今,使用真正的 fragment programs 是明智的。

注意每个纹理阶段可能,也可能不会限制在 0..1 的范围,这取决于平台。这意味着 SetTexture的阶段可能产生大于1.0的值。

Separate Alpha & Color computation

默认情况下,combiner的公式被同时用于计算颜色的 RGB 和 alpha 部分。其实,你可以定义一个独立的公式来计算 alpha。就像这样:

SetTexture [_MainTex] { combine previous * texture, previous + texture }

这里, 我们乘以 RGB 颜色并 加上 alpha。

Specular highlights

默认情况下 primary 颜色是 diffuse, ambient 和 specular colors (as defined in the Lighting calculation)的总和。如果你在 pass 中指定了 SeparateSpecular On,镜面颜色会被添加到 combiner 计算之后,而不是之前。这是内建 VertexLit shader 的默认行为。

Graphics hardware support

支持fragment shader的现代显卡 (“shader model 2.0” on desktop, OpenGL ES 2.0 on mobile) 支持所有 SetTexture 模式和至少4个纹理阶段 (大部分支持 8 个)。如果你运行在一个很老的硬件设备上(made before 2003 on PC, or before iPhone3GS on mobile), 你可能只能有2个纹理阶段。Shader编写者,应该写单独的 SubShaders 来支持这些显卡。

Examples

Alpha Blending Two Textures

这个小例子使用两个纹理。首先它设置第一个 combiner 只用 _MainTex ,然后使用 _BlendTex 的 alpha 通道来淡入 _BlendTex 的RGB颜色

Shader "Examples/2 Alpha Blended Textures" {    Properties {        _MainTex ("Base (RGB)", 2D) = "white" {}        _BlendTex ("Alpha Blended (RGBA) ", 2D) = "white" {}    }    SubShader {        Pass {            // Apply base texture            SetTexture [_MainTex] {                combine texture            }            // Blend in the alpha texture using the lerp operator            SetTexture [_BlendTex] {                combine texture lerp (texture) previous            }        }    }}

Alpha Controlled Self-illumination

该shader使用 _MainTex 的 alpha部分 来决定哪里发光。它通过两个纹理阶段来实现;在第一阶段,该纹理的 alpha 值被用来混合顶点颜色和白色。第二阶段,将第一阶段结果与 _MainTex RGB值相乘。

Shader "Examples/Self-Illumination" {    Properties {        _MainTex ("Base (RGB) Self-Illumination (A)", 2D) = "white" {}    }    SubShader {        Pass {            // Set up basic white vertex lighting            Material {                Diffuse (1,1,1,1)                Ambient (1,1,1,1)            }            Lighting On            // Use texture alpha to blend up to white (= full illumination)            SetTexture [_MainTex] {                constantColor (1,1,1,1)                combine constant lerp(texture) previous            }            // Multiply in texture            SetTexture [_MainTex] {                combine previous * texture            }        }    }}

不过我们可以做些优化; 使用纯白代替混合,我们可以添加一个 self-illumination color 和 blend 给他。 注意使用 ConstantColor 从 纹理中 得到 _SolidColor 属性来混合。

Shader "Examples/Self-Illumination 2" {    Properties {        _IlluminCol ("Self-Illumination color (RGB)", Color) = (1,1,1,1)        _MainTex ("Base (RGB) Self-Illumination (A)", 2D) = "white" {}    }    SubShader {        Pass {            // Set up basic white vertex lighting            Material {                Diffuse (1,1,1,1)                Ambient (1,1,1,1)            }            Lighting On            // Use texture alpha to blend up to white (= full illumination)            SetTexture [_MainTex] {                // Pull the color property into this blender                constantColor [_IlluminCol]                // And use the texture's alpha to blend between it and                // vertex color                combine constant lerp(texture) previous            }            // Multiply in texture            SetTexture [_MainTex] {                combine previous * texture            }        }    }}

最后,我们使用该顶点光照shader所有的光照选项:

Shader "Examples/Self-Illumination 3" {    Properties {        _IlluminCol ("Self-Illumination color (RGB)", Color) = (1,1,1,1)        _Color ("Main Color", Color) = (1,1,1,0)        _SpecColor ("Spec Color", Color) = (1,1,1,1)        _Emission ("Emmisive Color", Color) = (0,0,0,0)        _Shininess ("Shininess", Range (0.01, 1)) = 0.7        _MainTex ("Base (RGB)", 2D) = "white" {}    }    SubShader {        Pass {            // Set up basic vertex lighting            Material {                Diffuse [_Color]                Ambient [_Color]                Shininess [_Shininess]                Specular [_SpecColor]                Emission [_Emission]            }            Lighting On            // Use texture alpha to blend up to white (= full illumination)            SetTexture [_MainTex] {                constantColor [_IlluminCol]                combine constant lerp(texture) previous            }            // Multiply in texture            SetTexture [_MainTex] {                combine previous * texture            }        }    }}

原创粉丝点击