您的位置:首页 > 移动开发 > Unity3D

Unity3d Shader开发(三)Pass(Texturing )

2013-09-29 12:03 357 查看
纹理在基本的顶点光照被计算后被应用。在着色器中通过SetTexture 命令来完成。

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



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

Syntax 语法

SetTexture [TexturePropertyName] { Texture Block }分配一个纹理,TextureName必须定义为一个纹理属性。如何应用纹理被定义在TextrueBlock中
纹理块控制纹理如何被应用。在纹理块中能执行3种命令:合并,矩阵和不变色。

Texture block combine command 纹理块合并命令

combine src1 * src2将源1和源2的元素相乘。结果会比单独输出任何一个都要暗combine src1 + src2将将源1和源2的元素相加。结果会比单独输出任何一个都要亮combine src1 - src2源1 减去 源2combine src1 +- src2先相加,然后减去0.5(添加了一个符号)combine src1 lerp (src2) src3使用源2的透明度通道值在源3和源1中进行差值,注意差值是反向的:当透明度值是1是使用源1,透明度为0时使用源3combine src1 * src2 + src3源1和源2的透明度相乘,然后加上源3combine src1 * src2 +- src3源1和源2的透明度相乘,然后和源3做符号加combine src1 * src2 - src3源1和源2的透明度相乘,然后和源3相减
所有源属性都可以是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 解释:

上述的公式都均能通过关键字 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 }

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

Specular highlights 反射高光

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

Graphics hardware support 图形硬件支持

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

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

CardStage countCombiner modes not supported
NVIDIA GeForce 3/4Ti and up4In OpenGL on Windows, src1*src2-src3 is not supported
NVIDIA TNT2, GeForce 256, GeForce 2, GeForce 4MX2In OpenGL on Windows, src1*src2-src3 is not supported
ATI Radeon 9500 and up4-88 in OpenGL, 4 in D3D9
ATI Radeon 8500-92504-66 in OpenGL, 4 in D3D9
ATI Radeon 75003
ATI Rage2src1*src2+src3
src1*src2+-src3
src1*src2-src3

示例

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

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




把顶点光照着色器的所有的光照属性放入:

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


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: