第三章 (1)自定義光照模型

                                              自定義光照模型


這 Unity 老版本中 使用比較多的 還是 Lambert 光照模型

新版的 Unity 中使用的是 Standard 光照模型居多

 我們在這裏 手動實現一個 光照模型

Shader "Custom/chapter03/LambertShader"
{
    Properties
    {
        _MainTex ("Albedo (RGB)", 2D) = "white" {}
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 200

        CGPROGRAM
    

	// 預編譯器指令 
	// 新建 自定義光照模型的 名稱 --SimpleLambert
	// 讓編譯器 去 尋找 對應的 光照模型的 函數 LightingSimpleLambert
        #pragma surface surf SimpleLambert 

        // Use shader model 3.0 target, to get nicer looking lighting
        #pragma target 3.0

        sampler2D _MainTex;

        struct Input
        {
            float2 uv_MainTex;
        };


 
        UNITY_INSTANCING_BUFFER_START(Props)
            // put more per-instance properties here
        UNITY_INSTANCING_BUFFER_END(Props)

        void surf (Input IN, inout SurfaceOutput o)
        { 
            fixed4 c = tex2D (_MainTex, IN.uv_MainTex);
            o.Albedo = c.rgb; 
            o.Alpha = c.a;
        }


	/// 實現自定義 光照模型  函數  函數 是由: Lighting + 自定義光照模型的名詞
	/// 輸入的第一個參數  是 surf 的返回值  SurfaceOutput
	/// 第二個參數是  入射 光光線角度
	/// 第三個參數爲  光線 衰減率
	/// _LightColor0 第一個光源  _LightColor1 第二個光源 ....
	///
	///
	/// 物體表面 漫反射 強弱  等於 光線入射方向 和 表面法線 夾角 點乘
	///
	///
	///

	///返回值是 half4
	half4 LightingSimpleLambert( SurfaceOutput s, half3 lightDir, half atten) 
	{
		half NdotL = dot(s.Normal, lightDir);
		half4 c;
		c.rgb = s.Albedo * _LightColor0.rgb * (NdotL * atten);
		c.a = s.Alpha;
		return c;
	}

        ENDCG
    }
    FallBack "Diffuse"
}

// 預編譯器指令
// 新建 自定義光照模型的 名稱 --SimpleLambert
// 讓編譯器 去 尋找 對應的 光照模型的 函數 LightingSimpleLambert
 #pragma surface surf SimpleLambert

/// 實現自定義 光照模型  函數  函數 是由: Lighting + 自定義光照模型的名詞
/// 輸入的第一個參數  是 surf 的返回值  SurfaceOutput
/// 第二個參數是  入射 光光線角度
/// 第三個參數爲  光線 衰減率
/// _LightColor0 第一個光源  _LightColor1 第二個光源 ....
///
///
/// 物體表面 漫反射 強弱  等於 光線入射方向 和 表面法線 夾角 點乘
///
///
///

///返回值是 half4
half4 LightingSimpleLambert( SurfaceOutput s, half3 lightDir, half atten)
{
      half NdotL = dot(s.Normal, lightDir);
      half4 c;
      c.rgb = s.Albedo * _LightColor0.rgb * (NdotL * atten);
      c.a = s.Alpha;
      return c;
}

 

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