The following comes from the basis of light <UnityShader Getting Started Essentials> Chapter VI of Unity
1. Diffuse formula
Lambert law: the angle between the light intensity of the reflected light and the surface normal direction is proportional to the cosine value
C diffuse = (C Light * M diffuse ) * of saturate (DOT (n-, I))
C Light : the color of the incident light intensity and
M diffuse : albedo material
saturate (x): x, taken at the [0,1] in the range of
dot (x, y): x and y dot
n: a surface normal line
i: light source direction
to achieve:
Shader "Custom/Diffuse" {
Properties{
_Diffuse("Color", Color) = (1,1,1,1)
}
SubShader{
Pass
{
Tags{ "RenderType" = "Opaque" }
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "Lighting.cginc"
fixed4 _Diffuse;
struct Input {
float4 vertex:POSITION;
float3 normal:Normal;
};
struct v2f {
float4 pos:SV_POSITION;
fixed3 color : COLOR;
};
v2f vert(Input i)
{
v2f o;
//转换顶点坐标
o.pos = mul(UNITY_MATRIX_MVP, i.vertex);
//环境色
fixed3 ambine = UNITY_LIGHTMODEL_AMBIENT.xyz;
//法线
fixed3 worldNormal = normalize(mul(i.normal, (float3x3)unity_WorldToObject));
//入射光线
fixed3 worldLight = normalize(-_WorldSpaceLightPos0.xyz);
//漫反射公式
fixed3 diffuse = _LightColor0.rgb * _Diffuse.rgb * saturate(dot(worldNormal, worldLight));
//最终颜色
o.color = ambine + diffuse;
return o;
}
fixed4 frag(v2f i):SV_Target
{
return fixed4(i.color,1.0);
}
ENDCG
}
}
FallBack "Diffuse"
}
2. Semi-Lambert equation:
C diffuse = (C Light * M diffuse ) (DOT S * (n-, I) + O)
C Light : the color of the incident light intensity and
M diffuse : albedo material
dot (x , y): x and y by point
n: surface normal
i: light source direction
S: scale factor, typically 0.5
O: shift coefficient, typically 0.5
to achieve:
//在漫反射公式 实现代码中替换漫反射公式
fixed3 diffuse = _LightColor0.rgb * _Diffuse.rgb * saturate(dot(worldNormal, worldLight)*0.5+0.5);
3. Specular formula
C Specular = (C Light * M Specular ) * of saturate (DOT (V, the reflect (I, n-))) GLOSS
C Light : the color of the incident light and the intensity
M Specular : specular reflection coefficient of the material
of saturate ( x): x, taken at the [0,1] in the range of
dot (x, y): x and y dot
v: viewing direction
i: illumination direction
n: normal direction
reflect (i, n): the I ( incident direction), and n (normal direction) direction of the retroreflection
gloss: gloss, the smaller the highlight.
Shader "Custom/Specular" {
Properties{
_Diffuse("Color", Color) = (1,1,1,1)
_Specular("Specular",Color) = (1,1,1,1)
_Gloss("Gloss",Range(1,10)) = 1
}
SubShader{
Pass
{
Tags{ "RenderType" = "Opaque" }
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "Lighting.cginc"
fixed4 _Diffuse;
float _Gloss;
fixed4 _Specular;
struct Input {
float4 vertex:POSITION;
float3 normal:Normal;
};
struct v2f {
float4 pos:SV_POSITION;
fixed3 color : COLOR;
};
v2f vert(Input i)
{
v2f o;
//转换顶点坐标
o.pos = mul(UNITY_MATRIX_MVP, i.vertex);
//环境色
fixed3 ambine = UNITY_LIGHTMODEL_AMBIENT.xyz;
//法线
fixed3 worldNormal = normalize(mul(i.normal, (float3x3)unity_WorldToObject));
//入射光线
fixed3 worldLight = normalize(-_WorldSpaceLightPos0.xyz);
//漫反射公式
fixed3 diffuse = _LightColor0.rgb * _Diffuse.rgb * saturate(0.5f * dot(worldNormal, worldLight)+0.5f);
//反射方向 reflect(入射方向,法线)
fixed3 reflectDir = normalize(reflect(worldLight, worldNormal));
//视觉方向
fixed3 viewDir = normalize(-_WorldSpaceCameraPos.xyz - mul(unity_ObjectToWorld, i.vertex).xyz);
//高光公式
fixed3 specular = _LightColor0.rgb*_Specular.rgb*pow(saturate(dot(reflectDir, viewDir)), _Gloss);
//最终颜色
o.color = ambine + diffuse + specular;
return o;
}
fixed4 frag(v2f i):SV_Target
{
return fixed4(i.color,1.0);
}
ENDCG
}
}
FallBack "Diffuse"
}
4.Blinn-Phong formula
C Specular = (C Light * M Specular ) of saturate (DOT (n-, the normalize (I + V))) GLOSS
C Light : the color of the incident light intensity and
M Specular : specular reflection coefficient of the material
of saturate ( x): x, taken at the [0,1] in the range of
dot (x, y): x and y dot
v: viewing direction
i: illumination direction
normalize: normalized vector
gloss: gloss, greater highlights The smaller.
achieve:
//在高光反射公式中做如下修改
//反射方向 reflect(入射方向,法线)
//fixed3 reflectDir = normalize(reflect(worldLight, worldNormal));
//视觉方向
fixed3 viewDir = normalize(_WorldSpaceCameraPos.xyz - mul(unity_ObjectToWorld, i.vertex).xyz);
//高光公式
fixed3 specular = _LightColor0.rgb*_Specular.rgb*pow(saturate(dot(worldNormal, normalize(viewDir+worldLight))), _Gloss);
The self-luminous
C Emissive = M Emissive
M Emissive : a self-luminous colors
achieved:
//高光反射中,定义自发光颜色,在着色器输出最后颜色之前,把自发光颜色添加到输出颜色上即可
o.color = ambine + diffuse + specular+ _Emissive;