高光遮罩的原理可以简单的理解为:漫反射叠加上一个采样过遮罩纹理的高光次幂
Shader "Unlit/UnlitShader_MaskTexture"
{
Properties
{
_MainTex ("Texture" ,2D) = "white" {}
_NormalMap ("NormalMap" ,2D) = "bump"{}
_SpecularMask ("SpecularMask" ,2D) = "white"{}
_Color ("Color" ,Color) = (1.0,1.0,1.0,1.0)
_Specular ("Specular" ,Color) = (1.0,1.0,1.0,1.0)
_BumpScale ("BumpScale" ,Range(0.0,20.0)) = 1.0
_SpecularScale ("_SpecularScale",Range(0.0,10.0)) = 1.0
_Gloss ("Gloss" ,Range(0.0,256.0)) = 20
}
SubShader
{
Tags { "RenderType"="Opaque" }
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#include "Lighting.cginc"
sampler2D _MainTex;
sampler2D _NormalMap;
sampler2D _SpecularMask;
fixed4 _Color;
fixed4 _Specular;
fixed4 _MainTex_ST;
float _BumpScale;
float _SpecularScale;
float _Gloss;
struct VertexInput
{
float4 vertex : POSITION;
float3 normal : NORMAL;
float4 tangent : TANGENT;
float2 uv : TEXCOORD0;
};
struct VertexOutput
{
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
float3 lDir : TEXCOORD1;
float3 vDir : TEXCOORD2;
};
VertexOutput vert (VertexInput v)
{
VertexOutput o;
o.pos = UnityObjectToClipPos(v.vertex);
o.uv.xy = v.uv.xy * _MainTex_ST.xy + _MainTex_ST.zw;
TANGENT_SPACE_ROTATION;
o.lDir = mul(rotation,ObjSpaceLightDir(v.vertex)).xyz;
o.vDir = mul(rotation,ObjSpaceViewDir(v.vertex)).xyz;
return o;
}
fixed4 frag (VertexOutput i) : SV_Target
{
fixed3 lDirTS = normalize(i.lDir);
fixed3 vDirTS = normalize(i.vDir);
fixed3 nDirTS = UnpackNormal(tex2D(_NormalMap ,i.uv));
fixed3 hDir = normalize(lDirTS + vDirTS);
fixed3 ndotlTS = dot(nDirTS,lDirTS);
fixed3 ndoth = dot(nDirTS,hDir);
nDirTS.xy *= _BumpScale;
nDirTS.z = sqrt(1.0 - saturate(dot(nDirTS.xy,nDirTS.xy)));
fixed3 albedo = tex2D(_MainTex,i.uv).rgb * _Color.rgb;
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz * albedo;
fixed3 diffuse = _LightColor0.rgb * albedo * max(0.0,ndotlTS);
fixed specularMask = tex2D(_SpecularMask,i.uv).r * _SpecularScale;
fixed3 specular = _LightColor0.rgb * _Specular.rgb * pow(max(0,ndoth) , _Gloss) * specularMask;
return fixed4(ambient + diffuse + specular ,1.0);
}
ENDCG
}
}
FallBack"Specular"
}