1.Unity3D 5.4以前,灯光函数的定义:
1.half4 Lighting<Name> (SurfaceOutput s, half3 lightDir, half atten); This is used in forward rendering path for light models that are not view direction dependent (e.g. diffuse).2. half4 Lighting<Name> (SurfaceOutput s, half3 lightDir, half3 viewDir, half atten); This is used in forward rendering path for light models that are view direction dependent.3. half4 Lighting<Name>_PrePass (SurfaceOutput s, half4 light); This is used in deferred lighting path.
2.Unity3D 5.5以后,灯光函数的定义:
half4 Lighting<Name> (SurfaceOutput s, UnityGI gi)half4 Lighting<Name> (SurfaceOutput s, half3 viewDir, UnityGI gi);half4 Lighting<Name>_Deferred (SurfaceOutput s, UnityGI gi, out half4 outDiffuseOcclusion, out half4 outSpecSmoothness, out half4 outNormal)half4 Lighting<Name>_PrePass (SurfaceOutput s, half4 light)
3.在 unitylightingcommon.cginc 中可以看到:
struct UnityGI
{UnityLight light;#ifdef DIRLIGHTMAP_SEPARATE#ifdef LIGHTMAP_ONUnityLight light2;#endif#ifdef DYNAMICLIGHTMAP_ONUnityLight light3;#endif#endifUnityIndirect indirect;
};
Shader "Custom/CustomLighting"
{Properties{_Color ("Color", Color) = (1,1,1,1)_MainTex ("Albedo (RGB)", 2D) = "white" {}_SpecPower("Specular Power",float) = 1}SubShader{Tags { "RenderType"="Opaque" }LOD 200CGPROGRAM// Physically based Standard lighting model, and enable shadows on all light types#pragma surface surf Simple// Use shader model 3.0 target, to get nicer looking lighting#pragma target 3.0sampler2D _MainTex;struct Input{float2 uv_MainTex;};fixed4 _Color;float _SpecPower;// 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 assumeuniformscalingUNITY_INSTANCING_BUFFER_START(Props)// put more per-instance properties hereUNITY_INSTANCING_BUFFER_END(Props)//SurfaceOutput 输出void surf (Input IN, inout SurfaceOutput o){// Albedo comes from a texture tinted by colorfixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;o.Albedo = c.rgb;o.Alpha = c.a;}//SurfaceOutput 输入//Simple:为灯光的入口函数//lightDir:灯光的方向 viewDir 观察角方向 atten 灯光衰减值half4 LightingSimple (SurfaceOutput s, half3 lightDir, half3 viewDir, half atten){float NDotL = dot(lightDir,s.Normal);half4 result = 0;//漫反射计算result.rgb = s.Albedo * _LightColor0 * NDotL * atten;//高光反射计算half3 H= viewDir - lightDir;float HDotN = dot(H,s.Normal);result.rgb = s.Albedo * _LightColor0 * NDotL * atten + HDotN*_LightColor0*s.Albedo*_SpecPower;result.a = s.Alpha;return result;}ENDCG}FallBack "Diffuse"
}