:高光颜色
:入射光线的颜色和强度
:材质的高光反射颜色
:表面法线
:由以下公式可以获取
:光照方向
:视角方向
Shader "Custom/BlinnPhong" {
Properties {
_Color ("Diffuse Color", Color) = (1,1,1,1)
_MainTex ("Base(RGB)", 2D) = "white" {}
_SpecularColor ("Specular Color", Color) = (1,1,1,1)
_SpecularPower ("Specular Power", Range(0,1)) = 1
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 200
CGPROGRAM
#pragma surface surf CustomBlinnPhong
#pragma target 3.0
sampler2D _MainTex;
fixed4 _Color;
float4 _SpecularColor;
float _SpecularPower;
struct Input {
float2 uv_MainTex;
};
inline float4 LightingCustomBlinnPhong(SurfaceOutput s,fixed3 lightDir,half3 viewDir,fixed atten ){
float3 halfVector=normalize(lightDir+viewDir);
float diff=max(0,dot(s.Normal,lightDir));
float nh=max(0,dot(s.Normal,halfVector));
float4 spec=pow(nh,_SpecularPower)*_SpecularColor;
fixed4 c;
c.rgb=(s.Albedo*_LightColor0.rgb*diff)+(_LightColor0.rgb*_SpecularColor.rgb*spec)*(atten*2);
c.a=s.Alpha;
return c;
}
void surf (Input IN, inout SurfaceOutput o) {
fixed4 c = tex2D (_MainTex, IN.uv_MainTex)*_Color;
o.Albedo = c.rgb;
o.Alpha = c.a;
}
ENDCG
}
FallBack "Diffuse"
}
Shader "Custom/Unlit/VertexBlinnPhong"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_Diffuse ("Diffuse",Color)=(1,1,1,1)
_Specular("Specular",Color)=(1,1,1,1)
_Gloss("Gloss",Range(8.0,256))=20
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
Tags{"LightMode"="ForwardBase"}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#include "Lighting.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
float3 normal:NORMAL;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
fixed3 diffusecolor:COLOR;
fixed3 ambientcolor:COLOR2;
fixed3 specularcolor:COLOR1;
};
sampler2D _MainTex;
fixed4 _Diffuse;
fixed4 _Specular;
float _Gloss;
float4 _MainTex_ST;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
fixed3 ambient=UNITY_LIGHTMODEL_AMBIENT.xyz;
fixed3 worldNormal=normalize(mul(v.normal,(float3x3)unity_WorldToObject));
fixed3 worldLightDir=normalize(_WorldSpaceLightPos0.xyz);
fixed3 diffuse=_LightColor0.rgb * _Diffuse.rgb*saturate(dot(worldNormal,worldLightDir));
fixed3 viewDir=normalize(_WorldSpaceCameraPos.xyz-mul(unity_ObjectToWorld,v.vertex).xyz);
fixed3 h=normalize(viewDir+worldLightDir);
fixed specular=_LightColor0.rgb*_Specular.rgb*pow(saturate(dot(worldNormal,h)),_Gloss);
o.diffusecolor=diffuse;
o.specularcolor=specular;
o.ambientcolor=ambient;
return o;
}
fixed4 frag (v2f i) : SV_Target
{
fixed4 col = tex2D(_MainTex, i.uv);
fixed3 c=col.rgb*(i.ambientcolor+i.diffusecolor)+i.specularcolor;
return fixed4(c,1.0); col = tex2D(_MainTex, i.uv);
}
ENDCG
}
}
}
Shader "Custom/Unlit/FragmentBilnnPhong"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_Diffuse ("Diffuse",Color)=(1,1,1,1)
_Specular("Specular",Color)=(1,1,1,1)
_Gloss("Gloss",Range(8.0,256))=20
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
Tags{"LightMode"="ForwardBase"}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#include "Lighting.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
float3 normal:NORMAL;
};
struct v2f
{
float2 uv : TEXCOORD0;
float3 worldNormal:TEXCOORD1;
float3 worldPos:TEXCOORD2;
float4 vertex : SV_POSITION;
};
sampler2D _MainTex;
fixed4 _Diffuse;
fixed4 _Specular;
float _Gloss;
float4 _MainTex_ST;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
o.worldNormal=mul(v.normal,(float3x3)unity_WorldToObject);
o.worldPos=mul(unity_ObjectToWorld,v.vertex).xyz;
return o;
}
fixed4 frag (v2f i) : SV_Target
{
fixed3 ambient=UNITY_LIGHTMODEL_AMBIENT;
fixed3 worldNormal=normalize(i.worldNormal);
fixed3 worldLightDir=normalize(_WorldSpaceLightPos0.xyz);
fixed3 diffuse=_LightColor0.rgb *_Diffuse.rgb*saturate(dot(worldNormal,worldLightDir));
fixed3 viewDir=normalize(_WorldSpaceCameraPos.xyz-i.worldPos.xyz);
fixed3 h=normalize(viewDir+worldLightDir);
fixed3 specular=_LightColor0.rgb*_Specular.rgb*pow(saturate(dot(worldNormal,h)),_Gloss);
fixed3 color=ambient+diffuse+specular;
fixed4 col = tex2D(_MainTex, i.uv);
return fixed4(col.rgb*color.rgb,1.0);
}
ENDCG
}
}
}