UnityShader入门精要学习笔记(六):Unity中实现高光反射模型

时间:2022-05-06 10:21:17

一.逐顶点级高光反射

Shader "Custom/Edu/SpecularVertexLevel" {
Properties {
_Diffuse("Diffuse",Color) = (1,1,1,1)
_Specular("Specular",Color) = (1,1,1,1)
_Gloss("Gloss",Range(8.0,256)) = 20
}
SubShader {
Pass
{
Tags { "LightMode"="ForwardBase" }
LOD 200

CGPROGRAM
#pragma vertex vert
#pragma fragment frag

#include "Lighting.cginc"

fixed4 _Diffuse;
fixed4 _Specular;
float _Gloss;

struct a2v
{
float4 vertex:POSITION;
float3 normal:NORMAL;
};

struct v2f
{
float4 pos:SV_POSITION;
fixed3 color:TEXCOORD0;
};


v2f vert(a2v v)
{
v2f o;

o.pos = mul(UNITY_MATRIX_MVP,v.vertex);

fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;

float3 worldLightDir = normalize(_WorldSpaceLightPos0.xyz);

float3 worldNormal = normalize(mul(v.normal,(float3x3)_World2Object));

float3 viewDir = normalize(_WorldSpaceCameraPos.xyz - mul(_Object2World,v.vertex).xyz);

fixed3 reflectDir = normalize(reflect(-worldLightDir,worldNormal));

fixed3 diffuse = _LightColor0.rgb * _Diffuse.rgb * saturate(dot(worldNormal,worldLightDir));

float3 specular = _LightColor0.rgb * _Specular.rgb * pow(saturate(dot(reflectDir,viewDir)),_Gloss);

o.color = specular + ambient + diffuse;

return o;
}
fixed4 frag(v2f i):SV_Target
{
return fixed4(i.color,1.0);
}

ENDCG
}
}
FallBack "Diffuse"
}

函数:reflect(i,n)
参数:i,入射方向;n,法线方向。可以是float、float2、float3等类型
描述:给定入射方向i和法线方向n时,reflect函数可以返回反射反向

二.逐片元级高光反射

// Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld'
// Upgrade NOTE: replaced '_World2Object' with 'unity_WorldToObject'

Shader "Custom/Edu/SpecularPixelLevel" {
Properties {
_Diffuse("Diffuse",Color) = (1,1,1,1)
_Specular("Specular",Color) = (1,1,1,1)
_Gloss("Gloss",Range(8.0,256)) = 20
}
SubShader {
Pass
{
Tags { "LightMode"="ForwardBase" }
LOD 200

CGPROGRAM
#pragma vertex vert
#pragma fragment frag

#include "Lighting.cginc"

fixed4 _Diffuse;
fixed4 _Specular;
float _Gloss;

struct a2v
{
float4 vertex:POSITION;
float3 normal:NORMAL;
};

struct v2f
{
float4 pos:SV_POSITION;
float3 worldNormal:TEXCOORD0;
float3 worldPos:TEXCOORD1;
};


v2f vert(a2v v)
{
v2f o;
//将顶点坐标从模型空间转换到投影空间
o.pos = mul(UNITY_MATRIX_MVP,v.vertex);
o.worldNormal = mul(v.normal,(float3x3)_World2Object);
//将顶点坐标从模型空间转换到世界空间
o.worldPos = mul(_Object2World,v.vertex);
return o;
}
fixed4 frag(v2f i):SV_Target
{

fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.rgb;

float3 worldNormal = normalize(i.worldNormal);

float3 worldLightDir = normalize(_WorldSpaceLightPos0.xyz);

float3 viewDir = normalize(_WorldSpaceCameraPos.xyz - i.worldPos.xyz);

float3 reflectDir = normalize(reflect(-worldLightDir,worldNormal));

fixed3 diffuse = _LightColor0.rgb * _Diffuse.rgb * saturate(dot(worldNormal,worldLightDir));

fixed3 specular = _LightColor0.rgb * _Specular.rgb * pow(saturate(dot(reflectDir,viewDir)),_Gloss);

fixed3 color = ambient + diffuse + specular;

return fixed4(color,1.0);
}

ENDCG
}
}
FallBack "Diffuse"
}

三.Berlinn-Phong高光反射模型

Shader "Custom/Edu/SpecularBerlinnPhong" {
Properties {
_Diffuse("Diffuse",Color) = (1,1,1,1)
_Specular("Specular",Color) = (1,1,1,1)
_Gloss("Gloss",Range(8.0,256)) = 20
}
SubShader {
Pass
{
Tags { "LightMode"="ForwardBase" }
LOD 200

CGPROGRAM
#pragma vertex vert
#pragma fragment frag

#include "Lighting.cginc"

fixed4 _Diffuse;
fixed4 _Specular;
float _Gloss;

struct a2v
{
float4 vertex:POSITION;
float3 normal:NORMAL;
};

struct v2f
{
float4 pos:SV_POSITION;
float3 worldNormal:TEXCOORD0;
float3 worldPos:TEXCOORD1;
};


v2f vert(a2v v)
{
v2f o;
//将顶点坐标从模型空间转换到投影空间
o.pos = mul(UNITY_MATRIX_MVP,v.vertex);
o.worldNormal = mul(v.normal,(float3x3)unity_WorldToObject);
//将顶点坐标从模型空间转换到世界空间
o.worldPos = mul(unity_ObjectToWorld,v.vertex);
return o;
}
fixed4 frag(v2f i):SV_Target
{

fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.rgb;

float3 worldNormal = normalize(i.worldNormal);

float3 worldLightDir = normalize(_WorldSpaceLightPos0.xyz);

fixed3 diffuse = _LightColor0.rgb * _Diffuse.rgb * saturate(dot(worldNormal,worldLightDir));

float3 viewDir = normalize(_WorldSpaceCameraPos.xyz - i.worldPos.xyz);

//float3 reflectDir = normalize(reflect(-worldLightDir,worldNormal));
//fixed3 specular = _LightColor0.rgb * _Specular.rgb * pow(saturate(dot(reflectDir,viewDir)),_Gloss);

float3 halfDir = normalize(worldLightDir + viewDir);

fixed3 specular = _LightColor0.rgb * _Specular.rgb * pow(saturate(dot(worldNormal,halfDir)),_Gloss);

fixed3 color = ambient + diffuse + specular;

return fixed4(color,1.0);
}

ENDCG
}
}
FallBack "Diffuse"
}

四. 效果图

UnityShader入门精要学习笔记(六):Unity中实现高光反射模型

可以看出 Blinn-Phong光照模型的高光反射看起来更大、更亮一些。
而逐顶点的高光虽然效率高但是高光部分的边缘参差不齐,这是由于片元着色器三角遍历阶段对传入的顶点颜色进行了插值计算。