Shader - Pass (二)

来源:互联网 发布:js代码整理工具 编辑:程序博客网 时间:2024/05/22 02:29
                                Texturing

这里写图片描述

纹理在基本的顶点光照被计算后被应用。在着色器中通过SetTexture 命令来完成。
材质贴图可以用来做老风格的混合器效果。你能在一个通道中使用多个SetTexture 命令 - 所有纹理被顺序的应用,如同绘画程序中的层一样。SetTexture 命令必须放置在通道的末尾。

Syntax 语法

SetTexture [TexturePropertyName] { Texture Block } 分配一个纹理,TextureName必须定义为一个纹理属性。如何应用纹理被定义在TextrueBlock中

在纹理块中能执行3种命令:合并,矩阵和不变色。

  • Texture block combine command 纹理块合并命令

    1. combine src1 * src2:将源1和源2的元素相乘。结果会比单独输出任何一个都要暗
    2. combine src1 + src2:将将源1和源2的元素相加。结果会比单独输出任何一个都要亮
    3. combine src1 - src2:源1 减去 源2
    4. combine src1 +- src2:先相加,然后减去0.5(添加了一个符号)
    5. combine src1 lerp (src2) src3:使用源2的透明度通道值在源3和源1中进行差值,注意差值是反向的:当透明度值是1是使用源1,透明度为0时使用源3
    6. combine src1 * src2 + src3:源1和源2的透明度相乘,然后加上源3
    7. combine src1 * src2 +- src3:源1和源2的透明度相乘,然后和源3做符号加
    8. combine src1 * src2 - src3:源1和源2的透明度相乘,然后和源3相减
    9. 所有源属性都可以是previous, constant, primary or texture其中的一个。
    10. Previous :上一次SetTexture的结果
    11. Primary :来自光照计算的颜色或是当它绑定时的顶点颜色
    12. Texture :在SetTexture中被定义的纹理的颜色
    13. Constant :被ConstantColor定义的颜色

Modifiers 解释:
上述的公式都均能通过关键字 Double 或是 Quad 将最终颜色调高亮度2倍或4倍。
所有的src属性,除了差值参数都能被标记一个-符号来使最终颜色反相。
所有src属性能通过跟随 alpha 标签来表示只取用alpha通道。

  • Texture block constantColor command (纹理块constantColor 命令)

     ConstantColor color:定义在combine命令中能被使用的不变颜色
  • Texture block matrix command (纹理块matrix命令)

      matrix [MatrixPropertyName]:使用给定矩阵变换纹理坐标

Details 细节
较老的显卡对纹理使用分层的方案。纹理在每一层后被应用一次颜色的修改。对每一个纹理,一般来说纹理都是和上一次操作的结果混合。
这里写图片描述
注意,在“真正的固定功能”设备(OpenGL, OpenGL ES 1.1, Wii),每个SetTexture阶段的值被限制为0..1范围。其他地方(Direct3D, OpenGL ES 2.0)该范围可能或不可能更高。这可能会影响SetTexture阶段,可能产生的值高于1.0。

  • Separate Alpha & Color computation 分离的透明度和颜色混合

    缺省情况下,混合公式被同时用于计算纹理的RGB通道和透明度。同时,你也能指定只针对透明度进行计算,如:SetTexture [_MainTex] { combine previous * texture, previous + texture }

    Specular highlights 反射高光:默认情况下primary颜色是漫反射,阴影色和高光颜色(在光线计算中定义)的加和。如果你将通道设置中的SeparateSpecular 打开,高光色会在混合计算后被加入,而不是之前。这是内置顶点着色器的默认行为。

Examples 示例

这个小例子使用了两张纹理。首先设置第一个混合器只使用_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            // 使用差值操作混合Alpha纹理            SetTexture [_BlendTex] {                combine texture lerp (texture) previous            }        }    }}

Alpha Controlled Self-illumination (Alpha控制自发光)

这个着色器使用_MainTex的Alpha来描述什么地方应用光照。它通过分两个阶段应用纹理来实现;第一个阶段,纹理的Alpha值被用来在顶点颜色和纯白色之间混合。第二阶段,纹理的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)            // 使用纹理Alpha来混合白色(完全发光)            SetTexture [_MainTex] {                constantColor (1,1,1,1)                combine constant lerp(texture) previous            }            // Multiply in texture            // 和纹理相乘            SetTexture [_MainTex] {                combine previous * texture            }        }    }}

我们能在这里尽可能自由的做些事,除了混合纯白色,我们还能添加一个自发光颜色然后和它混合和。注意ConstantColor的使用能从属性中获取一个纯色并应用到纹理混合中去。

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                // 使用纹理的alpha通道混合顶点颜色                combine constant lerp(texture) previous            }            // Multiply in texture            SetTexture [_MainTex] {                combine previous * texture            }        }    }}

最后,我们把顶点光照着色器的所有的光照属性放入:

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            }        }    }} 
0 0