- 法线纹理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
}
}
}