- 法線紋理TextureType設置爲Normal Map(使用Unity內置的Shader必須設置爲Normal Map)
- 設置爲Normal Map的貼圖在不同平臺Unity會採用不同的壓縮方式
- 法線的z方向始終爲正,法線紋理存儲的是單位向量,所以只需要保存法線的x, y方向
- 對法線貼圖進行紋理採樣後需要使用UnpackNormal來獲得真正的法線(DXT5nm格式壓縮的紋理a對應x,g對應y,r,b被捨棄)
- 法線紋理每個分量取值範圍是[0,1],而法線每個分量取值範圍是[-1, 1],所以normal = pixel * 2 - 1
Shader "Achonor/NormalMapInTangentSpace"
{
Properties
{
_Color ("Color", Color) = (1.0, 1.0, 1.0, 1.0)
_MainTex ("Main Tex", 2D) = "white" {}
_BumpMap ("Normal Map", 2D) = "bump" {}
_BumpScale("Bump Scale", Float) = 1.0
_Specular("Specular", Color) = (1.0, 1.0, 1.0, 1.0)
_Gloss("Gloss", Range(8, 256)) = 20
}
SubShader
{
Pass{
Tags{"LightMode"="ForwardBase"}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "Lighting.cginc"
struct a2v{
float4 vertex : POSITION;
float3 normal : NORMAL;
float4 tangent : TANGENT;
float4 texcoord : TEXCOORD0;
};
struct v2f{
float4 pos : SV_POSITION;
fixed3 tangentViewDir : TEXCOORD0;
fixed3 tangentLightDir : TEXCOORD1;
float4 uv : TEXCOORD2;
};
fixed3 _Color;
sampler2D _MainTex;
float4 _MainTex_ST;
sampler2D _BumpMap;
float4 _BumpMap_ST;
float _BumpScale;
fixed3 _Specular;
float _Gloss;
v2f vert(a2v v){
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
//計算uv
o.uv.xy = TRANSFORM_TEX(v.texcoord, _MainTex);
o.uv.zw = TRANSFORM_TEX(v.texcoord, _BumpMap);
TANGENT_SPACE_ROTATION;
o.tangentViewDir = normalize(mul(rotation, ObjSpaceViewDir(v.vertex)));
o.tangentLightDir = normalize(mul(rotation, ObjSpaceLightDir(v.vertex)));
return o;
}
fixed4 frag(v2f o) : SV_Target{
fixed3 tangentViewDir = normalize(o.tangentViewDir);
fixed3 tangentLightDir = normalize(o.tangentLightDir);
//紋理採樣
fixed3 albedo = tex2D(_MainTex, o.uv.xy).rgb * _Color.rgb;
//環境光
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.rgb * albedo.rgb;
//計算法線
//fixed3 tangentNormal = tex2D(_BumpMap, o.uv.zw).rgb;
//tangentNormal.xy = (tangentNormal.xy * 2 - 1) * _BumpScale;
fixed3 tangentNormal = UnpackNormal(tex2D(_BumpMap, o.uv.zw));
tangentNormal.xy *= _BumpScale;
tangentNormal.z = sqrt(1 - saturate(dot(tangentNormal.xy, tangentNormal.xy)));
//漫反射
fixed3 duffise = _LightColor0.rgb * albedo.rgb * saturate(dot(tangentLightDir, tangentNormal));
//高光反射
fixed3 halfDir = normalize(tangentViewDir + tangentLightDir);
fixed3 specular = _LightColor0.rgb * _Specular.rgb * pow(saturate(dot(halfDir, tangentNormal)), _Gloss);
return fixed4(ambient + duffise + specular, 1);
}
ENDCG
}
}
}