Unity3d Shader開發(三)Pass(Texturing )

 紋理在基本的頂點光照被計算後被應用。在着色器中通過SetTexture 命令來完成。
    該文章出自【狗刨學習網】

    SetTexture 命令在片面程序被使用時不會生效;這種模式下像素操作被完全描述在着色器中。

    

    材質貼圖可以用來做老風格的混合器效果。你能在一個通道中使用多個SetTexture 命令 - 所有紋理被順序的應用,如同繪畫程序中的層一樣。SetTexture 命令必須放置在通道的末尾。

    Syntax 語法SetTexture [TexturePropertyName] { Texture Block }分配一個紋理,TextureName必須定義爲一個紋理屬性。如何應用紋理被定義在TextrueBlock中
    紋理塊控制紋理如何被應用。在紋理塊中能執行3種命令:合併,矩陣和不變色。


    combine src1 * src2    將源1和源2的元素相乘。結果會比單獨輸出任何一個都要暗
    combine src1 + src2
    將將源1和源2的元素相加。結果會比單獨輸出任何一個都要亮
    combine src1 - src2
    源1 減去 源2
    combine src1 +- src2    先相加,然後減去0.5(添加了一個符號)
    combine src1 lerp (src2src3
    使用源2的透明度通道值在源3和源1中進行差值,注意差值是反向的:當透明度值是1是使用源1,透明度爲0時使用源3
    combine src1 * src2 + src3
    源1和源2的透明度相乘,然後加上源3
    combine 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級渲染階段。下表簡述了硬件支持。

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

示例

    這個小例子使用了兩張紋理。首先設置第一個混合器只使用_MainTex,然後使用_BlendTex的Alpha通道來淡入_BlendTex的RGB顏色。
  1. <font color="#000000" face="宋體">Shader "Examples/2 Alpha Blended Textures" {
  2.     Properties {
  3.         _MainTex ("Base (RGB)", 2D) = "white" {}
  4.         _BlendTex ("Alpha Blended (RGBA) ", 2D) = "white" {}
  5.     }
  6.     SubShader {
  7.         Pass {
  8.             // Apply base texture
  9.             // 應用主紋理
  10.             SetTexture [_MainTex] {
  11.                 combine texture
  12.             }
  13.             // Blend in the alpha texture using the lerp operator
  14.             // 使用差值操作混合Alpha紋理
  15.             SetTexture [_BlendTex] {
  16.                 combine texture lerp (texture) previous
  17.             }
  18.         }
  19.     }
  20. }</font>
複製代碼





Alpha控制自發光

  1. <font color="#000000" face="宋體">Shader "Examples/Self-Illumination" {
  2.     Properties {
  3.         _MainTex ("Base (RGB) Self-Illumination (A)", 2D) = "white" {}
  4.     }
  5.     SubShader {
  6.         Pass {
  7.             // Set up basic white vertex lighting
  8.             //設置白色頂點光照
  9.             Material {
  10.                 Diffuse (1,1,1,1)
  11.                 Ambient (1,1,1,1)
  12.             }
  13.             Lighting On

  14.             // Use texture alpha to blend up to white (= full illumination)
  15.             // 使用紋理Alpha來混合白色(完全發光)
  16.             SetTexture [_MainTex] {
  17.                 constantColor (1,1,1,1)
  18.                 combine constant lerp(texture) previous
  19.             }
  20.             // Multiply in texture
  21.             // 和紋理相乘
  22.             SetTexture [_MainTex] {
  23.                 combine previous * texture
  24.             }
  25.         }
  26.     }
  27. }</font>
複製代碼


          

把頂點光照着色器的所有的光照屬性放入:
  1. <font color="#000000" face="宋體">Shader "Examples/Self-Illumination 3" {
  2.     Properties {
  3.         _IlluminCol ("Self-Illumination color (RGB)", Color) = (1,1,1,1)
  4.         _Color ("Main Color", Color) = (1,1,1,0)
  5.         _SpecColor ("Spec Color", Color) = (1,1,1,1)
  6.         _Emission ("Emmisive Color", Color) = (0,0,0,0)
  7.         _Shininess ("Shininess", Range (0.01, 1)) = 0.7
  8.         _MainTex ("Base (RGB)", 2D) = "white" {}
  9.     }

  10.     SubShader {
  11.         Pass {
  12.             // Set up basic vertex lighting
  13.             Material {
  14.                 Diffuse [_Color]
  15.                 Ambient [_Color]
  16.                 Shininess [_Shininess]
  17.                 Specular [_SpecColor]
  18.                 Emission [_Emission]
  19.             }
  20.             Lighting On

  21.             // Use texture alpha to blend up to white (= full illumination)
  22.             SetTexture [_MainTex] {
  23.                 constantColor [_IlluminCol]
  24.                 combine constant lerp(texture) previous
  25.             }
  26.             // Multiply in texture
  27.             SetTexture [_MainTex] {
  28.                 combine previous * texture
  29.             }
  30.         }
  31.     }
  32. } </font>
複製代碼

         

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章