方式反射模型
Unity 提供的 光照函數
光照函數類型 | 函數說明 |
非視線 依賴 | half4 Lighting+Name(SurfaceOutput s, half3 lightDir, half atten); |
視線 依賴 | half4 Lighting+Name(SurfaceOutput s, half3 lightDir, half3 viewDir, half atten); |
- SurfaceOutput : 表面函數 輸出結構體
- lightDir : 光線方向
- viewDir : 視口方向
- atten : 光線衰減 率 0-1
Phong Specular 方式反射模型
Shader "Custom/chapter03/PhongSpecular"
{
Properties
{
_MianTint ("Diffuse Tint", Color) = (1,1,1,1)
_MainTex ("Albedo (RGB)", 2D) = "white" {}
_SpecularColor("Specular Color",Color) = (1,1,1,1)
_SpecPower("Specular Power",Range(0.1,30)) = 1
}
SubShader
{
Tags { "RenderType" = "Opaque" }
LOD 200
CGPROGRAM
// Physically based Standard lighting model, and enable shadows on all light types
#pragma surface surf CustomPhong
// Use shader model 3.0 target, to get nicer looking lighting
#pragma target 3.0
float4 _MianTint;
sampler2D _MainTex;
float4 _SpecularColor;
float _SpecPower;
struct Input
{
float2 uv_MainTex;
};
// 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 SurfaceOutput o)
{
// Albedo comes from a texture tinted by color
fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _MianTint;
o.Albedo = c.rgb;
o.Alpha = c.a;
}
/// 參數一 表面函數輸出結構體
/// 參數二 光線方向
/// 參數三 視口方向
/// 參數四 光線衰減 率 0-1
fixed4 LightingCustomPhong(SurfaceOutput s, fixed3 lightDir, half3 viewDir, fixed atten)
{
// 反射
float NdotL = dot(s.Normal, lightDir);
float3 refVector = normalize(2.0 * s.Normal * NdotL - lightDir);
// 鏡面反射
float Spec = pow(max(0, dot(refVector, viewDir)), _SpecPower);
float3 finalSpec = _SpecularColor.rgb * Spec;
//Final Effect
fixed4 c;
c.rgb = (s.Albedo * _LightColor0.rgb * max(0, NdotL)*atten) + (_LightColor0.rgb*finalSpec);
c.a = s.Alpha;
return c;
}
ENDCG
}
FallBack "Diffuse"
}