MarcusMogg
12/23/2017 - 5:53 AM

Specular-Phong

使用Blinn-Phong的高光反射

struct a2v {
	float4 vertex : POSITION;
	float3 normal : NORMAL;
};
			
struct v2f {
	float4 pos : SV_POSITION;
	float3 worldNormal : TEXCOORD0;
	float4 worldPos : TEXCOORD1;
};
	
v2f vert(a2v v) {
	v2f o;
	o.pos = UnityObjectToClipPos(v.vertex);
	
	// Use the build-in funtion to compute the normal in world space
	o.worldNormal = UnityObjectToWorldNormal(v.normal);
	
	o.worldPos = mul(unity_ObjectToWorld, v.vertex);
	
	return o;
}
			
fixed4 frag(v2f i) : SV_Target {
	fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
	
	fixed3 worldNormal = normalize(i.worldNormal);
	//  Use the build-in funtion to compute the light direction in world space
	// Remember to normalize the result
	fixed3 worldLightDir = normalize(UnityWorldSpaceLightDir(i.worldPos));
	
	fixed3 diffuse = _LightColor0.rgb * _Diffuse.rgb * max(0, dot(worldNormal, worldLightDir));
	
	// Use the build-in funtion to compute the view direction in world space
	// Remember to normalize the result
	fixed3 viewDir = normalize(UnityWorldSpaceViewDir(i.worldPos));
	fixed3 halfDir = normalize(worldLightDir + viewDir);
	fixed3 specular = _LightColor0.rgb * _Specular.rgb * pow(max(0, dot(worldNormal, halfDir)), _Gloss);
	
	return fixed4(ambient + diffuse + specular, 1.0);
}