shader學習筆記二

surface shader中因爲使用cg語言,所以不需要pass通道。

Shader "Custom/myshader03" {
Properties {
_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中不需要編寫pass
SubShader {
Tags { "RenderType"="Opaque"  "queue"="transparent"} //渲染類型,是個不透明的物體."queue"="transparent"按透明物體隊列渲染
LOD 200 //層級細節

CGPROGRAM  //代碼塊起始,使用了CG語法

//#pragma(編譯指令)+surface(用此格式編寫)+surf(suifaceFunction(方法名)+Standard(LightonModel光照模型)+其他一些選項
//加alpha給上阿爾法通道實現透明
//刪除fullforwardshadows取消陰影的話還要刪掉末尾FallBack "Diffuse"

#pragma surface surf Standard fullforwardshadows  alpha

// 沒有這句默認使用shader model 2.0 target
#pragma target 3.0

sampler2D _MainTex;//屬性中類型爲2D,這要聲明爲二維紋理變量


//紋理座標結構體,必須以uv或uv2開頭
struct Input {
float2  uv_MainTex;
};


//屬性裏都要一一聲明
half  _Glossiness; 
half  _Metallic;//範圍要聲明爲一個浮點值
fixed4  _Color;//color類型聲明爲fixed4的一個四階向量


//surface函數總是無返回,inout意思是即使輸入又是輸出。 沒有則默認爲是int,意思是輸入的。out意思是最終爲輸出。
//SurfaceOutputStandard在官方手冊裏爲:
//4.x版本
//struct SurfaceOutput
//{
    // fixed3 Albedo;  // diffuse color
    // fixed3 Normal;  // tangent space normal, if written
    // fixed3 Emission;
    // half Specular;  // specular power in 0..1 range
    // fixed Gloss;    // specular intensity
    //fixed Alpha;    // alpha for transparencies
// };
//5.x版本
//struct SurfaceOutputStandard
//{
    //fixed3 Albedo;      // base (diffuse or specular) color 基本的漫反射和高光,結合了舊版本的Specular
    //fixed3 Normal;      // tangent space normal, if written
    //half3 Emission;
    // half Metallic;      // 0=non-metal, 1=metal 描述一個物體金屬化的程度
    //half Smoothness;    // 0=rough, 1=smooth 結合了舊版本的Gloss
    //half Occlusion;     // occlusion (default 1)
    //fixed Alpha;        // alpha for transparencies
//};
//struct SurfaceOutputStandardSpecular
//{
    //fixed3 Albedo;      // diffuse color
    // fixed3 Specular;    // specular color
    // fixed3 Normal;      // tangent space normal, if written
    // half3 Emission;
    //half Smoothness;    // 0=rough, 1=smooth
    //half Occlusion;     // occlusion (default 1)
    //fixed Alpha;        // alpha for transparencies
//};



void surf (Input IN, inout SurfaceOutputStandard o)
{
// Albedo comes from a texture tinted by color
fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
o.Albedo = c.rgb;
// Metallic and smoothness come from slider variables
o.Metallic = _Metallic;
o.Smoothness = _Glossiness;
o.Alpha = c.a;
}
ENDCG

FallBack "Diffuse" //如果特性不能被使用,使用FallBack定義的默認shader
}


在unity中找到以下文件打開:



下圖就是找到了光照函數名稱,真正有效的部分是lighting後面的部分,在編寫時要加上lighting



shader文件下載地址:http://download.csdn.net/detail/zkq666666/9385413

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