Unity Shader 入門(一) SurfaceShader

xzhuan原創(chuàng)稿件,轉載請注明出處!

//-------------------------------【Unity右鍵路徑】-----------------------------------------
Shader "Custom/MySurfaceShader"
{
   //-------------------------------【Shader屬性】-----------------------------------------
   Properties
   {
       // Color :顏色 Inspector面板里會有一個選取顏色的picker
       // Range :某范圍內的浮點數 Inspector面板里會出現一個Slider
       // 2D :   2D紋理
       // Rect : 創(chuàng)建非2次方邊長的紋理
       // Cube : 創(chuàng)建Cube Map,也是一種紋理
       // Float :浮點數,和Range的區(qū)別就是Range是一個范圍內的數,Float是單個數
       // Vector:向量 定義一個四元素的容器
       _Color ("Color", Color) = (1,1,1,1)
       _MainTex ("Albedo (RGB)", 2D) = "white" {}
       _Glossiness ("Smoothness", Range(0,1)) = 0.5
       _Metallic ("Metallic", Range(0,1)) = 0.0
   }

   //----------------------------【開始一個子著色器】---------------------------  
   SubShader
   {
       //----------------------------【標簽】---------------------------  
       //Queque 渲染順序       BackGround/Geometry
       //RenderType 渲染類型   Opaque不透明/Transparent透明
       Tags { "RenderType"="Opaque" }
       //Level of Detail
       LOD 200

       //CG程序 開始
       CGPROGRAM
       // Physically based Standard lighting model, and enable shadows on all light types
       //說明是surface shader,可以換成Vertex或Fragment,surf是表面處理函數,在代碼段的下面就有一個surf函數。Standard fullforwardshadows是光照模型函數。
       #pragma surface surf Standard fullforwardshadows

       // Use shader model 3.0 target, to get nicer looking lighting
       //設置編譯目標Shader model的版本
       #pragma target 3.0

       //在surf函數的上面,將properties里的變量用相同的名稱定義一遍。還有一個Input結構體,這是輸入結構
       sampler2D _MainTex;

       struct Input
       {
           float2 uv_MainTex;
       };

       half _Glossiness;
       half _Metallic;
       fixed4 _Color;

       // Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
       // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
       // #pragma instancing_options assumeuniformscaling
       UNITY_INSTANCING_BUFFER_START(Props)
       // put more per-instance properties here
       UNITY_INSTANCING_BUFFER_END(Props)

       void surf (Input IN, inout SurfaceOutputStandard o)
       {
           // Albedo comes from a texture tinted by color
           //先定義了一個4元的定點數c,tex2D是紋理采樣函數,第一個參數是紋理,第二個參數是uv坐標,函數的返回值乘以顏色
           fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
           //o這個變量就是著色器會輸出的變量,也就是存儲著我們眼睛會看到的效果的變量
           //后面的語句的意思就是將各個值賦給o里的不同值。rgb就是RGB顏色,Albedo是反射值,Metalic是金屬值,Smoothness是光滑值,Alpha是透明通道
           o.Albedo = c.rgb;
           // Metallic and smoothness come from slider variables
           o.Metallic = _Metallic;
           o.Smoothness = _Glossiness;
           o.Alpha = c.a;
       }
       //CG程序 結束
       ENDCG
   }

   //備胎: 表示前面的著色程序顯卡不支持的時候就用默認的“Diffuse”
   FallBack "Diffuse"
}

CG數據結構 類型

CG支持7種數據類型:
float 32位浮點數
half 16位浮點數
int 32位整型數
fixed 12位定點數
bool 布爾數據
sampler 紋理對象的句柄,公有sampler, sampler1D, sampler2D, sampler3D, samplerCUBE, 和 samplerRECT 六種
string 字符,其實沒有必要在CG中用到字符
向量類型 float2就是2元的float類型的向量,fixed4就是4元的fixed類型的向量
向量最長不超過4元
此外,CG還支持矩陣數據類型,比如:
float2×4 matrix; // 表示2×4階矩陣,包含8個float類型數據

surface shader的輸入輸出結構:
輸入結構就是存儲輸入的信息,輸出結構就是存儲要輸出的信息,也就是我們的眼睛會看見的效果。
struct Input
{
float2 uv_MainTex;
}
紋理坐標必須命名為“uv”+"紋理名稱",uv_MainTex就是名為_MainTex的紋理的uv坐標。

surface shader的默認的幾種輸出結構如下,輸出結構也是可以自定義的。
struct SurfaceOutput
{
fixed3 Albedo; // 漫反射顏色
fixed3 Normal; // 切線空間法線,如果賦值的話
fixed3 Emission; // 自發(fā)光顏色
half Specular; // 高光強度,范圍是0-1
fixed Gloss; // specular intensity
fixed Alpha; // 透明度
};
struct SurfaceOutputStandard
{
fixed3 Albedo; // 基礎 (漫反射或鏡面反射) 顏色
fixed3 Normal; // 切線空間法線,如果賦值的話
half3 Emission; // 自發(fā)光顏色
half Metallic; // 0=非金屬, 1=金屬
half Smoothness; // 0=粗糙, 1=光滑
half Occlusion; // 遮擋(默認1)
fixed Alpha; // 透明度
};
struct SurfaceOutputStandardSpecular
{
fixed3 Albedo; // 漫反射顏色
fixed3 Specular; // 鏡面反射顏色
fixed3 Normal; // 切線空間法線,如果賦值的話
half3 Emission; // 自發(fā)光顏色
half Smoothness; // 0=粗糙, 1=光滑
half Occlusion; // 遮擋(默認1)
fixed Alpha; // 透明度
};

那么定義變量的時候怎么知道該用哪種類型呢?

  1. 精度夠用就好
  2. 顏色和單位向量,使用fixed
  3. 其他情況,盡量使用half(即范圍在[-6萬, +6萬]內、精確到小數點后3.3位);否則才使用float。
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容