【風宇沖】Unity3D教程寶典之Shader篇:第十四講Surface Shader

來源:互聯網
上載者:User

創文章如需轉載請註明:轉載自風宇沖Unity3D教程學院

                            Shader第十四講 Surface Shader

   用Shader來實現光照是比較複雜的,有不同的光類型,不同的陰影選項,不同的Render Path(forward和Deferred)。 Unity只是把光照模型封裝處理了,Shader的代碼還是用CG/HLSL編寫的。

                        例一:最簡單的Surface Shader

 

  1. Shader "Custom/T_3_0" {
  2. Properties {
  3. _MainTex ("Base (RGB)", 2D) = "white" {}
  4. }
  5. SubShader {
  6. CGPROGRAM
  7. #pragma surface surf Lambert
  8. sampler2D _MainTex;
  9. struct Input {
  10. float2 uv_MainTex;
  11. } ;
  12. void surf (Input IN, inout SurfaceOutput o) {
  13. half4 c = tex2D (_MainTex, IN.uv_MainTex);
  14. o.Albedo = c.rgb;
  15. o.Alpha = c.a;
  16. }
  17. ENDCG
  18. }  
  19. }
表面處理函數從頂點處理函數(這裡自動頂點處理)接收參數Input IN,輸出SurfaceOutput o至光照模型(這裡是Lambert)。這裡我們就做了三件事:(1)通過uv取得了貼圖對應uv的顏色c  (2)把c.rgb賦給o.Albedo  (3) 把c.a賦給o.Alpha這就完成了。之後讓任一物體使用該Shader,則不管加什麼光都能正確顯示。真的很簡單!
                               表面處理函數surf的輸入及輸出
(1)輸入
  1. struct Input {
  2. float2 uv_MainTex;
  3. float3 viewDir;
  4. float4 anyName:COLOR;
  5. float4 screenPos;
  6.         float3 worldPos;
  7. float3 worldRefl;
  8. } ;
uv_MainTex: uvviewDir: 入視角screenPos:裁剪空間位置worldPos:世界空間位置
(2)輸出

 

  • struct SurfaceOutput {
  •     half3 Albedo;
  •     half3 Normal;
  •     half3 Emission;
  •     half Specular;
  •     half Gloss;
  •     half Alpha;
  • };
Albedo:漫反射顏色Normal: 法線Emission:自發光顏色Specular:鏡面反射係數Gloss:Alpha:透明值更多的參數請參考文檔                                          Surface Shader參數Surface Shader可以通過加參數實現額外的控制,例如加頂點處理函數,alpha混合等。注意:如果使用的光照模型是Unity內建的Lambert或者BlinnPhong:alpha混合,alpha混合等功能必須使用Surface Shader參數。之前例如AlphaTest Greater 0.3 等代碼就不能再加了。如果加了的話,光照失效,物體為一片漆黑。如果使用的光照模型是自訂光照模型:只要在surf中給o.Alpha正常賦值(如賦貼圖顏色的a值),那麼之前例如AlphaTest Greater 0.3 等代碼依然可以照常使用。具體相關代碼可以參考下一講中的代碼。 (1)alpha混合直接加參數Alpha即可

#pragma surface surf BlinnPhong alpha

(2)alphatest

加入一個變數,這裡起名為_Cutoff, 然後在#pragma surface結尾處加上alphatest:_Cutoff。

效果是alpha值大於_Cutoff時,才會輸出顏色

 

 

  1. Shader "Custom/T_3_0" {
  2. Properties {
  3. _MainTex ("Base (RGB)", 2D) = "white" {}
  4. _Cutoff ("Alpha cutoff", Range(0,1)) =
    0.5
  5. }
  6. SubShader {
  7. CGPROGRAM
  8. #pragma surface surf Lambert alphatest:_Cutoff
  9. sampler2D _MainTex;
  10. struct Input {
  11. float2 uv_MainTex;
  12. } ;
  13. void surf (Input IN, inout SurfaceOutput o) {
  14. half4 c = tex2D (_MainTex, IN.uv_MainTex);
  15. o.Albedo = c.rgb;
  16. o.Alpha = c.a;
  17. }
  18. ENDCG
  19. }  
  20. }

(3)頂點函數

例:臃腫的模型

實現方法,將頂點向法線方向延伸一段距離

 

 

  1.   Shader "Example/Normal Extrusion" {
  2.     Properties {
  3.       _MainTex ("Texture", 2D) = "white" {}
  4.       _Amount ("Extrusion Amount", Range(-1,1)) = 0.5
  5.     }
  6.     SubShader {
  7.       Tags { "RenderType" = "Opaque" }
  8.       CGPROGRAM
  9.       #pragma surface surf Lambert vertex:vert
  10.       struct Input {
  11.           float2 uv_MainTex;
  12.       };
  13.       float _Amount;
  14.       void vert (inout appdata_full v) {
  15.           v.vertex.xyz += v.normal * _Amount;
  16.       }
  17.       sampler2D _MainTex;
  18.       void surf (Input IN, inout SurfaceOutput o) {
  19.           o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;
  20.       }
  21.       ENDCG
  22.     } 
  23.     Fallback "Diffuse"
  24.   }

(4)finalcolor

finalcolor:ColorFunction

對顏色輸出作最後的更改

 

  
  1. Shader
    "Example/Tint Final Color" {
  2.     Properties
    {
  3.       _MainTex
    ("Texture",
    2D) =
    "white"
    {}
  4.       _ColorTint
    ("Tint",
    Color) = (1.0,
    0.6,
    0.6,
    1.0)
  5.     }
  6.     SubShader
    {
  7.       CGPROGRAM
  8.       #pragma

    surface surf Lambert
    finalcolor:mycolor
  9.       struct

    Input {
  10.           float2
    uv_MainTex;
  11.       };
  12.       fixed4
    _ColorTint;
  13.    
      void

    mycolor (Input IN, SurfaceOutput o, inout fixed4 color)
  14.       {
  15. //在這裡對最後輸出的顏色color進行修改
  16.           color

    *= _ColorTint;
  17.       }
  18.       sampler2D
    _MainTex;
  19.       void

    surf (Input IN, inout SurfaceOutput o) {
  20.            o.Albedo
    = tex2D (_MainTex, IN.uv_MainTex).rgb;
  21.       }
  22.       ENDCG
  23.     } 
  24.  }
更多的參數請參考文檔

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.