在Shader中,我們除了可以設定各種光線處理外,還可以增加紋理貼圖。
使用 settexture 命令可以為着色器指定紋理。
示例代碼:
Shader "Sbin/ff2" { // 貼圖采樣 properties { // 變量名("描述名",類型)=值 _Color("主體", color)=(1,1,1,1) _Ambient("環境光", color)=(0.3,0.3,0.3,0.3) _Specular("高光", color)=(1,1,1,1) // 變量名("描述名",range(區域最小值,區域最大值)=默認值 _Shininess("高光強度",range(0,8))=4 _Emission("自發光", color)=(1,1,1,1) _Constant("透明通道", color)=(1,1,1,0.3) _MainTex("紋理", 2d)="" _SecondTex("第二張紋理",2d)="" } SubShader { Tags { "Queue" = "Transparent" } pass { Blend SrcAlpha OneMinusSrcAlpha material { diffuse[_Color] ambient[_Ambient] specular[_Specular] shininess[_Shininess] emission[_Emission] } lighting on // 啟用光照 separatespecular on // 鏡面高光 // 紋理屬性 settexture[_MainTex] { // 合並 當前紋理 * 前面所有材質和關照的顏色 // primary 代表頂點光照后的顏色 // double 顏色*2 // quad 顏色*4 combine texture * primary double } // 第二張紋理 settexture[_SecondTex] { // 用當前采用到的紋理與之前所有采樣到的結果進行混合 //combine texture * previous double // , 號后面的參數,它只是取了紋理alpha通道, 前面所有的顏色alpha值失效 constantcolor[_Constant] combine texture * previous double, texture * constant } } } // FallBack "Diffuse" }
效果圖:
默認渲染順序圖:
指令說明:
settexture 應用紋理
combine 紋理混合時使用的計算方式
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 } } } }
constantColor 透明通道
Blend 進行阿爾法最后的混合,制作透明的游戲對象
Tags 控制渲染順序
【官方文檔中的一些說明】
Blend operations (混合操作)
可以使用的混合方式:
Add | Add source and destination together. 源和目標疊加在一起 |
Sub | Subtract destination from source. 從源上減去 |
RevSub | Subtract source from destination. |
Min | Use the smaller of source and destination. |
Max | Use the larger of source and destination. |
LogicalClear | Logical operation: Clear (0) DX11.1 only. |
LogicalSet | Logical operation: Set (1) DX11.1 only. |
LogicalCopy | Logical operation: Copy (s) DX11.1 only. |
LogicalCopyInverted | Logical operation: Copy inverted (!s) DX11.1 only. |
LogicalNoop | Logical operation: Noop (d) DX11.1 only. |
LogicalInvert | Logical operation: Invert (!d) DX11.1 only. |
LogicalAnd | Logical operation: And (s & d) DX11.1 only. |
LogicalNand | Logical operation: Nand !(s & d) DX11.1 only. |
LogicalOr | Logical operation: Or (s | d) DX11.1 only. |
LogicalNor | Logical operation: Nor !(s | d) DX11.1 only. |
LogicalXor | Logical operation: Xor (s ^ d) DX11.1 only. |
LogicalEquiv | Logical operation: Equivalence !(s ^ d) DX11.1 only. |
LogicalAndReverse | Logical operation: Reverse And (s & !d) DX11.1 only. |
LogicalAndInverted | Logical operation: Inverted And (!s & d) DX11.1 only. |
LogicalOrReverse | Logical operation: Reverse Or (s | !d) DX11.1 only. |
LogicalOrInverted | Logical operation: Inverted Or (!s | d) DX11.1 only. |
Blend factors (混合因子)
All following properties are valid for both SrcFactor & DstFactor in the Blend command.Source refers to the calculated color, Destination is the color already on the screen. The blend factors are ignored if BlendOp is using logical operations.
所有指令都是使用 SrcFactor & DstFactor 的方式進行混合。 源是指要計算的顏色, 目的地是指當前已經要顯示在屏幕上的顏色。 如果忽略了邏輯運算符則使用 BlendOp 方式。
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 (詳情)
下面是最常用的混合類型:
Blend SrcAlpha OneMinusSrcAlpha // 透明通道混合 Blend One One // Additive 疊加 Blend OneMinusDstColor One // Soft Additive 柔性疊加 Blend DstColor Zero // Multiplicative 相乘 Blend DstColor SrcColor // 2x Multiplicative
2倍乘法
Example (示例)
下面是一個小例子,可以在屏幕上添加一個紋理的紋理:
Shader "Simple Additive" { Properties { _MainTex ("Texture to blend", 2D) = "black" {} } SubShader { Tags { "Queue" = "Transparent" } Pass { Blend One One SetTexture [_MainTex] { combine texture } } } }
Tags 語法
Tags { "TagName1" = "Value1" "TagName2" = "Value2" }
Subshader 語法
Subshader { [Tags] [CommonState] Passdef [Passdef ...] }