Unity Shader之模板測試

Unity Shader之模板測試

一沙一世界,一花一天堂

一、Stencil testing

 

渲染管線
渲染管線

    當片段着色器處理完一個片段之後,模板測試(Stencil Test)會開始執行,和深度測試一樣,它也可能會丟棄片段。接下來,被保留的片段會進入深度測試,它可能會丟棄更多的片段。模板測試是根據又一個緩衝來進行的,它叫做模板緩衝(Stencil Buffer),我們可以在渲染的時候更新它來獲得一些很有意思的效果。
    一個模板緩衝中,(通常)每個模板值(Stencil Value)是8位的。所以每個像素/片段一共能有256種不同的模板值。我們可以將這些模板值設置爲我們想要的值,然後當某一個片段有某一個模板值的時候,我們就可以選擇丟棄或是保留這個片段了。

 

二、模板函數

2.1 調用函數

  1. Stencil 
  2. { 
  3. Ref 1//Reference Value ReadMask 255 WriteMask 255 Comp Always //Comparison Function Pass Replace 
  4. Fail Keep 
  5. ZFail Replace 
  6. } 

2.2 參數說明

Ref :就是參考值,當參數允許賦值時,會把參考值賦給當前像素
ReadMask: 對當前參考值和已有值進行mask操作,默認值255,一般不用
WriteMask: 寫入Mask操作,默認值255,一般不用

Comp: 比較方法。是拿Ref參考值和當前像素緩存上的值進行比較。默認值always,即一直通過。
-- Greater - 大於

  • GEqual - 大於等於
  • Less - 小於
  • LEqual - 小於等於
  • Equal - 等於
  • NotEqual - 不等於
  • Always - 永遠通過
  • Never - 永遠通不過

Pass: 當模版測試和深度測試都通過時,進行處理

Fail :當模版測試和深度測試都失敗時,進行處理

ZFail: 當模版測試通過而深度測試失敗時,進行處理

pass,Fail,ZFail都屬於Stencil操作,他們參數統一如下:

  • Keep 保持(即不把參考值賦上去,直接不管)
  • Zero 歸零
  • Replace 替換(拿參考值替代原有值)
  • IncrSat 值增加1,但不溢出,如果到255,就不再加
  • DecrSat 值減少1,但不溢出,值到0就不再減
  • Invert 反轉所有位,如果1就會變成254
  • IncrWrap 值增加1,會溢出,所以255變成0
  • DecrWrap 值減少1,會溢出,所以0變成255

三、案例

3.1 描邊

 

渲染管線
渲染管線

 

  1. Shader "ShaderCookbook/stencil_outline" { 
  2. Properties { 
  3. _MainTex("Texture", 2D) = "white" {} 
  4. _OutLineWidth("Width", float) = 0.01  
  5. _OutLineColor("Color", color) = (1, 1, 1, 1

  6. SubShader { 
  7. Tags { 
  8. "RenderType" = "Opaque" 

  9. LOD 100 
  10.  
  11. Stencil { 
  12. Ref 0 Comp Equal Pass IncrSat 

  13.  
  14. Pass { 
  15. CGPROGRAM 
  16. #pragma vertex vert 
  17. #pragma fragment frag 
  18. #include "UnityCG.cginc" 
  19.  
  20. struct appdata { 
  21. float4 vertex: POSITION; 
  22. float2 uv: TEXCOORD0; 
  23. }; 
  24.  
  25. struct v2f { 
  26. float2 uv: TEXCOORD0; 
  27. float4 vertex: SV_POSITION; 
  28. }; 
  29.  
  30. sampler2D _MainTex; 
  31. float4 _MainTex_ST; 
  32. v2f vert(appdata v) { 
  33. v2f o; 
  34. o.vertex = UnityObjectToClipPos(v.vertex); 
  35. o.uv = TRANSFORM_TEX(v.uv, _MainTex); 
  36. return o; 

  37. fixed4 frag(v2f i) : SV_Target { 
  38. // sample the texture 
  39. fixed4 col = tex2D(_MainTex, i.uv); 
  40. return col; 

  41. ENDCG 

  42.  
  43. Pass { 
  44. CGPROGRAM 
  45. #pragma vertex vert 
  46. #pragma fragment frag 
  47.  
  48. struct appdata { 
  49. float4 vertex: POSITION; 
  50. float4 normal: NORMAL; 
  51. }; 
  52.  
  53. struct v2f { 
  54. float4 vertex: SV_POSITION; 
  55. }; 
  56.  
  57. fixed4 _OutLineColor; 
  58. float _OutLineWidth; 
  59. v2f vert(appdata v) { 
  60. v2f o; 
  61. v.vertex = v.vertex + normalize(v.normal) * _OutLineWidth; 
  62. o.vertex = UnityObjectToClipPos(v.vertex); 
  63. return o; 

  64. fixed4 frag(v2f i) : SV_Target { 
  65. // sample the texture 
  66. fixed4 col = _OutLineColor; 
  67. return col; 

  68. ENDCG 



3.2 百寶箱

 


渲染管線

 

遮罩層:

  1. Shader "ShaderCookbook/StencilEnumMask" 

  2. Properties 

  3. _MainTex ("Texture", 2D) = "white" {} 
  4. [ForceInt] 
  5. _StencilRef("StencilRef",float) = 0 
  6. [Enum(UnityEngine.Rendering.CompareFunction)] 
  7. _StencilComp("StencilComp",int) =0 
  8. [Enum(UnityEngine.Rendering.StencilOp)] 
  9. _StencilOp("StencilOp",int)=0 
  10.  
  11. [ForceInt] 
  12. _StencilReadMask("ReadMask",int)=255 
  13. [ForceInt] 
  14. _StencilWriteMask("WriteMask",int)=255 
  15. [MaterialToggle] 
  16. _ZWrite("zWrite",float)=0 

  17. SubShader 

  18. Tags { "RenderType"="StencilMaskOpaque" 
  19. "Queue" = "Geometry-100
  20. "IgnoreProjector" = "True" } 
  21. LOD 100 
  22.  
  23.  
  24. Pass 

  25. ColorMask 0 
  26. ZWrite [_ZWrite] 
  27.  
  28. Stencil{ 
  29. Ref [_StencilRef] 
  30. Comp[_StencilComp] 
  31. Pass[_StencilOp] 
  32. ReadMask[_StencilReadMask] 
  33. WriteMask[_StencilWriteMask] 

  34.  
  35. CGPROGRAM 
  36. #pragma vertex vert 
  37. #pragma fragment frag 
  38. // make fog work 
  39. #pragma multi_compile_fog 
  40. #include "UnityCG.cginc" 
  41.  
  42.  
  43. struct appdata 

  44. float4 vertex : POSITION; 
  45. float2 uv : TEXCOORD0; 
  46. }; 
  47.  
  48. struct v2f 

  49. float2 uv : TEXCOORD0; 
  50. UNITY_FOG_COORDS(1
  51. float4 vertex : SV_POSITION; 
  52. }; 
  53.  
  54. sampler2D _MainTex; 
  55. float4 _MainTex_ST; 
  56. v2f vert (appdata v) 

  57. v2f o; 
  58. o.vertex = UnityObjectToClipPos(v.vertex); 
  59. o.uv = TRANSFORM_TEX(v.uv, _MainTex); 
  60. UNITY_TRANSFER_FOG(o,o.vertex); 
  61. return o; 

  62. fixed4 frag (v2f i) : SV_Target 

  63. // sample the texture 
  64. fixed4 col = tex2D(_MainTex, i.uv); 
  65. // apply fog 
  66. UNITY_APPLY_FOG(i.fogCoord, col); 
  67. return col; 

  68. ENDCG 



顯示層:

  1.  
  2.  
  3. Shader "ShaderCookbook/StencilEnum" 

  4. Properties 

  5. _MainTex ("Texture", 2D) = "white" {} 
  6. [ForceInt] 
  7. _StencilRef("StencilRef",float) = 0 
  8. [Enum(UnityEngine.Rendering.CompareFunction)] 
  9. _StencilComp("StencilComp",int) =0 
  10. [Enum(UnityEngine.Rendering.StencilOp)] 
  11. _StencilOp("StencilOp",int)=0 
  12.  
  13. [ForceInt] 
  14. _StencilReadMask("ReadMask",int)=255 
  15. [ForceInt] 
  16. _StencilWriteMask("WriteMask",int)=255 

  17. SubShader 

  18. Tags { "RenderType"="opaque" } 
  19. LOD 100 
  20.  
  21.  
  22. Pass 

  23.  
  24. Stencil{ 
  25. Ref [_StencilRef] 
  26. Comp[_StencilComp] 
  27. Pass[_StencilOp] 
  28. ReadMask[_StencilReadMask] 
  29. WriteMask[_StencilWriteMask] 

  30.  
  31. CGPROGRAM 
  32. #pragma vertex vert 
  33. #pragma fragment frag 
  34. // make fog work 
  35. #pragma multi_compile_fog 
  36. #include "UnityCG.cginc" 
  37.  
  38.  
  39. struct appdata 

  40. float4 vertex : POSITION; 
  41. float2 uv : TEXCOORD0; 
  42. }; 
  43.  
  44. struct v2f 

  45. float2 uv : TEXCOORD0; 
  46. UNITY_FOG_COORDS(1
  47. float4 vertex : SV_POSITION; 
  48. }; 
  49.  
  50. sampler2D _MainTex; 
  51. float4 _MainTex_ST; 
  52. v2f vert (appdata v) 

  53. v2f o; 
  54. o.vertex = UnityObjectToClipPos(v.vertex); 
  55. o.uv = TRANSFORM_TEX(v.uv, _MainTex); 
  56. UNITY_TRANSFER_FOG(o,o.vertex); 
  57. return o; 

  58. fixed4 frag (v2f i) : SV_Target 

  59. // sample the texture 
  60. fixed4 col = tex2D(_MainTex, i.uv); 
  61. // apply fog 
  62. UNITY_APPLY_FOG(i.fogCoord, col); 
  63. return col; 

  64. ENDCG 



Ad

這裏推薦一款可視化shader編程工具,對美術同學非常友好,就像建模工具中的材質編輯器一樣

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