After the basic vertex lighting has been calculated, textures are applied. In ShaderLab this is done using SetTexture command.

纹理在基本的顶点光照被计算后被应用。在着色器中通过SetTexture 命令来完成。

SetTexture commands have no effect when fragment programs are used; as in that case pixel operations are completely described in the shader.

SetTexture 命令在片面程序被使用时不会生效;这种模式下像素操作被完全描述在着色器中。

Texturing is the place to do old-style combiner effects. You can have multiple SetTexture commands inside a pass - all textures are applied in sequence, like layers in a painting program. SetTexture commands must be placed at the end of a Pass.

材质贴图可以用来做老风格的混合器效果。你能在一个通道中使用多个SetTexture 命令 - 所有纹理被顺序的应用,如同绘画程序中的层一样。SetTexture 命令必须放置在通道的末尾。

Syntax 语法


SetTexture 

[TexturePropertyName] 

{ 

Texture Block 

}

Assigns a texture.  TextureName must be defined as a texture property. How to apply the texture is defined inside the 

TextureBlock.


分配一个纹理,TextureName必须定义为一个纹理属性。如何应用纹理被定义在TextrueBlock中

The texture block controls how the texture is applied. Inside the texture block can be up to three commands: combine, matrix and constantColor.

纹理块控制纹理如何被应用。在纹理块中能执行3种命令:合并,矩阵和不变色。

Texture block combine command 纹理块合并命令


combine  src1 * 

src2

Multiplies src1 and src2 together. The result will be darker than either input.

将源1和源2的元素相乘。结果会比单独输出任何一个都要暗

combine  src1 + 

src2

Adds src1 and src2 together. The result will be lighter than either input.

将将源1和源2的元素相加。结果会比单独输出任何一个都要亮

combine  src1 - 

src2

Subtracts src2 from src1.

源1 减去源2

combine  src1 +- 

src2

Adds src1 to src2, then subtracts 0.5 (a signed add).

先相加,然后减去0.5(添加了一个符号)

combine  src1 lerp (

src2

src3

Interpolates between src3 and src1, using the alpha of src2. Note that the interpolation is opposite direction: src1 is used when alpha is one, and src3 is used when alpha is zero.

使用源2的透明度通道值在源3和源1中进行差值,注意差值是反向的:当透明度值是1是使用源1,透明度为0时使用源3

combine  src1 * 

src2 + 

src3

Multiplies src1 with the alpha component of src2, then adds src3.

源1和源2的透明度相乘,然后加上源3

combine  src1 * 

src2 +- 

src3

Multiplies src1 with the alpha component of src2, then does a signed add with src3.

源1和源2的透明度相乘,然后和源3做符号加

combine  src1 * 

src2 - 

src3

Multiplies src1 with the alpha component of src2, then subtracts src3.

源1和源2的透明度相乘,然后和源3相减

All the src properties can be either one of previousconstantprimary or texture.

所有源属性都可以是previous, constant, primary or texture其中的一个。

  • Previous is the the result of the previous SetTexture.上一次SetTexture的结果
  • Primary is the color from the lighting calculation or the vertex color if it is bound.来自光照计算的颜色或是当它绑定时的顶点颜色
  • Texture is the color of the texture specified by [_TextureName] in the SetTexture (see above).
    在SetTexture中被定义的纹理的颜色
  • Constant is the color specified in ConstantColor.
    被ConstantColor定义的颜色

Modifiers 解释:

  • The formulas specified above can optionally be followed by the keywords Double or Quad to make the resulting color 2x or 4x as bright.
    上述的公式都均能通过关键字 Double 或是 Quad 将最终颜色调高亮度2倍或4倍。
  • All the src properties, except lerp argument, can optionally be preceded by one - to make the resulting color negated.
    所有的src属性,除了差值参数都能被标记一个-符号来使最终颜色反相。
  • All the src properties can be followed by alpha to take only the alpha channel.
    所有src属性能通过跟随 alpha 标签来表示只取用alpha通道。

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


ConstantColor 

color

Defines a constant color that can be used in the combine command.

定义在combine命令中能被使用的不变颜色

Texture block matrix command (纹理块matrix命令)


matrix 

[MatrixPropertyName]

Transforms texture coordinates used in this command with the given matrix.

使用给定矩阵变换纹理坐标

Details 细节

Older graphics cards use a layered approach to textures. The textures are applied one after each other, modifying the color that will be written to the screen. For each texture, the texture is typically combined with the result of the previous operation.

较老的显卡对纹理使用分层的方案。纹理在每一层后被应用一次颜色的修改。对每一个纹理,一般来说纹理都是和上一次操作的结果混合。


Note that on "true fixed function" devices (OpenGL, OpenGL ES 1.1, Wii) the value of each SetTexture stage is clamped to 0..1 range. Everywhere else (Direct3D, OpenGL ES 2.0) the range may or may not be higher. This might affect SetTexture stages that can produce values higher than 1.0.

注意,在“真正的固定功能”设备(OpenGL, OpenGL ES 1.1, Wii),每个SetTexture阶段的值被限制为0..1范围。其他地方(Direct3D, OpenGL ES 2.0)该范围可能或不可能更高。这可能会影响SetTexture阶段,可能产生的值高于1.0。

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

By default, the combiner formula is used for calculating both the RGB and alpha component of the color. Optionally, you can specify a separate formula for the alpha calculation. This looks like this:

缺省情况下,混合公式被同时用于计算纹理的RGB通道和透明度。同时,你也能指定只针对透明度进行计算,如下:

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

Here, we multiply the RGB colors and add the alpha.

如上所述,我们对RGB的颜色做乘然后对透明度相加

Specular highlights 反射高光

By default the primary color is the sum of the diffuse, ambient and specular colors (as defined in the Lighting calculation). If you specify SeparateSpecular On in the pass options, the specular color will be added in after the combiner calculation, rather than before. This is the default behavior of the built-in VertexLit shader.

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

Graphics hardware support 图形硬件支持

Some old graphics cards might not support some texture combine modes, and different cards have different number of SetTexture stages available. The shader author should write separateSubShaders for the cards he or she wants to support.

一些旧的显示卡不能支持某些纹理混合模式,不同的卡有不同数目的SetTexture阶段可用。着色器作者应该为他们想支持的卡分开写SubShaders 。

Graphics cards with pixel shader 1.1 support (NVIDIA GeForce 3 and up, ATI Radeon 8500 and up, Intel 9xx) support all combiner modes and have at least 4 texture stages available. The following table summarizes the hardware support:

支持像素着色器1.1版本的图形卡(...)支持所有的混合器模式并且可以拥有至少4级渲染阶段。下表简述了硬件支持。

Card

Stage count

Combiner modes not supported

NVIDIA GeForce 3/4Ti and up

4

In OpenGL on Windows, src1*src2-src3 is not supported

NVIDIA TNT2, GeForce 256, GeForce 2, GeForce 4MX

2

In OpenGL on Windows, src1*src2-src3 is not supported

ATI Radeon 9500 and up

4-8

8 in OpenGL, 4 in D3D9

ATI Radeon 8500-9250

4-6

6 in OpenGL, 4 in D3D9

ATI Radeon 7500

3


ATI Rage

2

src1*src2+src3

src1*src2+-src3

src1*src2-src3

Examples 示例

Alpha Blending Two Textures (Alpha混合两个纹理)

This small examples takes two textures. First it sets the first combiner to just take the_MainTex, then is uses the alpha channel of _BlendTex to fade in the RGB colors of _BlendTex

这个小例子使用了两张纹理。首先设置第一个混合器只使用_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控制自发光)

This shader uses the alpha component of the _MainTex to decide where to apply lighting. It does this by applying the texture to two stages; In the first stage, the alpha value of the texture is used to blend between the vertex color and solid white. In the second stage, the RGB values of the texture are multiplied in.

这个着色器使用_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
            }
        }
    }
}

We can do something else for free here, though; instead of blending to solid white, we can add a self-illumination color and blend to that. Note the use of ConstantColor to get a _SolidColor from the properties into the texture blending.

我们能在这里尽可能自由的做些事,除了混合纯白色,我们还能添加一个自发光颜色然后和它混合和。注意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)
            }
           
 
           // 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
            }
        }
   
 
 And finally, we take all the lighting properties of the vertexlit shader and pull that in:
 最后,我们把顶点光照着色器的所有的光照属性放入:
 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]
             }
                       // 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
             }
         }
     }
 }