1）法线贴图概念与使用

2）模型空间 vs 切线空间的法线纹理

## 一.法线贴图概念与使用

PS：作为程序我们不需要过多了解法线贴图的制作过程，仅需知道两种方式

1.用颜色贴图转法线贴图

2.先制作一个高模一个低模，通过高模的烘焙到低模上就可以生成法线贴图

## 二.模型空间 vs 切线空间的法线纹理

_BumpMap("法线纹理",2D) = "bump" {}
_BumpScale("凹凸程度",Range(0,5.0)) = 1.0

sampler2D _BumpMap;
float4 _BumpMap_ST;
float _BumpScale;

float4 tangent:TANGENT;
float4 texcoord:TEXCOORD0;

float4 uv : TEXCOORD0;
float3 lightDir : TEXCOORD1;
float3 viewDir : TEXCOORD2;

v2f vert(a2v v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.pos);
o.uv.xy = v.texcoord.xy*_MainTex_ST.xy + _MainTex_ST.zw;
o.uv.zw = v.texcoord.xy*_BumpMap_ST.xy + _BumpMap_ST.zw;
TANGENT_SPACE_ROTATION;
o.lightDir = mul(rotation, ObjSpaceLightDir(v.pos)).xyz;
o.viewDir = mul(rotation, ObjSpaceViewDir(v.pos)).xyz;
return o;
}

UnpackNormal进行的操作就是         packedNormal*2-1

（公式由来：RGB范围为0~1，法线范围为-1~1）

fixed4 frag(v2f i) : SV_Target
{

float3 lightDir = i.lightDir;
float3 viewDir = i.viewDir;

float4 packedNormal = tex2D(_BumpMap, i.uv.zw);
float3 targentNormal = UnpackNormal(packedNormal);
targentNormal.xy *= _BumpScale;
targentNormal.z = sqrt(1.0 - saturate(dot(targentNormal.xy, targentNormal.xy)));

float3 albedo = tex2D(_MainTex, i.uv.xy).rgb;
float3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz*albedo;

float3 diffuse = _LightColor0.rgb * _Diffuse.rgb * max(0, dot(targentNormal, lightDir)) * albedo;

return fixed4(ambient + diffuse,1.0);
}

Shader "sony/Shader168"
{
Properties
{
_Diffuse("漫反射系数",Color) = (1.0,1.0,1.0,1.0)
_MainTex("主纹理",2D) = "white"{}
_BumpMap("法线纹理",2D) = "bump" {}
_BumpScale("凹凸程度",Range(0,5.0)) = 1.0
}
{
Pass
{
Tags{ "LightMode" = "ForwardBase" }
CGPROGRAM
#include "lighting.cginc"
#pragma vertex vert
#pragma fragment frag
float4 _Diffuse;
sampler2D _MainTex;
float4 _MainTex_ST;
sampler2D _BumpMap;
float4 _BumpMap_ST;
float _BumpScale;

struct a2v
{
float4 pos : POSITION;
float3 normal : NORMAL;
float4 tangent:TANGENT;
float4 texcoord:TEXCOORD0;
};

struct v2f
{
float4 pos : SV_POSITION;
float4 uv : TEXCOORD0;
float3 lightDir : TEXCOORD1;
float3 viewDir : TEXCOORD2;
};
v2f vert(a2v v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.pos);
o.uv.xy = v.texcoord.xy*_MainTex_ST.xy + _MainTex_ST.zw;
o.uv.zw = v.texcoord.xy*_BumpMap_ST.xy + _BumpMap_ST.zw;
TANGENT_SPACE_ROTATION;
o.lightDir = mul(rotation, ObjSpaceLightDir(v.pos)).xyz;
o.viewDir = mul(rotation, ObjSpaceViewDir(v.pos)).xyz;
return o;
}
fixed4 frag(v2f i) : SV_Target
{

float3 lightDir = i.lightDir;
float3 viewDir = i.viewDir;

float3 targentNormal = UnpackNormal(tex2D(_BumpMap, i.uv.zw));
targentNormal.xy *= _BumpScale;
targentNormal.z = sqrt(1.0 - saturate(dot(targentNormal.xy, targentNormal.xy)));

float3 albedo = tex2D(_MainTex, i.uv.xy).rgb;
float3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz*albedo;

float3 diffuse = _LightColor0.rgb * _Diffuse.rgb * max(0, dot(targentNormal, lightDir)) * albedo;

return fixed4(ambient + diffuse,1.0);
}
ENDCG
}
}
}


struct v2f
{
float4 pos : SV_POSITION;
float4 uv : TEXCOORD0;
float3 TtoW0 : TEXCOORD1;
float3 TtoW1 : TEXCOORD2;
float3 TtoW2 : TEXCOORD3;
};

v2f vert(a2v v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.uv.xy = v.texcoord.xy*_MainTex_ST.xy + _MainTex_ST.zw;
o.uv.zw = v.texcoord.xy*_BumpMap_ST.xy + _BumpMap_ST.zw;

float3 worldTangent = UnityObjectToWorldDir(v.tangent.xyz);
float3 worldNormal = UnityObjectToWorldNormal(v.normal);
float3 worldBinormal = cross(worldNormal, worldTangent) * v.tangent.w;

o.TtoW0 = float3(worldTangent.x, worldBinormal.x, worldNormal.x);
o.TtoW1 = float3(worldTangent.y, worldBinormal.y, worldNormal.y);
o.TtoW2 = float3(worldTangent.z, worldBinormal.z, worldNormal.z);

return o;
}

fixed4 frag(v2f i) : SV_Target
{
float3 worldPos = i.pos;
float3 lightDir = UnityWorldSpaceLightDir(worldPos);
float3 viewDir = UnityWorldSpaceViewDir(worldPos);

float3 bump = UnpackNormal(tex2D(_BumpMap, i.uv.zw));
bump.xy *= _BumpScale;
bump.z = sqrt(1.0 - saturate(dot(bump.xy, bump.xy)));
bump = normalize(half3(dot(i.TtoW0, bump), dot(i.TtoW1, bump), dot(i.TtoW2, bump)));

float3 albedo = tex2D(_MainTex, i.uv).rgb;
float3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz*albedo;

float3 diffuse = _LightColor0.rgb * _Diffuse.rgb * max(0, dot(bump, lightDir)) * albedo;

return fixed4(ambient + diffuse,1.0);
}

Shader "sony/Shader170"
{
Properties
{
_Diffuse("漫反射系数",Color) = (1.0,1.0,1.0,1.0)
_MainTex("主纹理",2D) = "white"{}
_BumpMap("法线纹理",2D) = "bump" {}
_BumpScale("凹凸程度",Range(0,5.0)) = 1.0
}
{
Pass
{
Tags{ "LightMode" = "ForwardBase" }
CGPROGRAM
#include "lighting.cginc"
#pragma vertex vert
#pragma fragment frag
float4 _Diffuse;
sampler2D _MainTex;
sampler2D _BumpMap;
float4 _MainTex_ST;
float4 _BumpMap_ST;
float _BumpScale;
struct a2v
{
float4 vertex : POSITION;
float3 normal : NORMAL;
float4 tangent:TANGENT;
float4 texcoord:TEXCOORD0;
};
struct v2f
{
float4 pos : SV_POSITION;
float4 uv : TEXCOORD0;
float3 TtoW0 : TEXCOORD1;
float3 TtoW1 : TEXCOORD2;
float3 TtoW2 : TEXCOORD3;
};
v2f vert(a2v v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.uv.xy = v.texcoord.xy*_MainTex_ST.xy + _MainTex_ST.zw;
o.uv.zw = v.texcoord.xy*_BumpMap_ST.xy + _BumpMap_ST.zw;

float3 worldTangent = UnityObjectToWorldDir(v.tangent.xyz);
float3 worldNormal = UnityObjectToWorldNormal(v.normal);
float3 worldBinormal = cross(worldNormal, worldTangent) * v.tangent.w;

o.TtoW0 = float3(worldTangent.x, worldBinormal.x, worldNormal.x);
o.TtoW1 = float3(worldTangent.y, worldBinormal.y, worldNormal.y);
o.TtoW2 = float3(worldTangent.z, worldBinormal.z, worldNormal.z);

return o;
}
fixed4 frag(v2f i) : SV_Target
{
float3 worldPos = i.pos;
float3 lightDir = UnityWorldSpaceLightDir(worldPos);
float3 viewDir = UnityWorldSpaceViewDir(worldPos);

float3 bump = UnpackNormal(tex2D(_BumpMap, i.uv.zw));
bump.xy *= _BumpScale;
bump.z = sqrt(1.0 - saturate(dot(bump.xy, bump.xy)));
bump = normalize(half3(dot(i.TtoW0, bump), dot(i.TtoW1, bump), dot(i.TtoW2, bump)));

float3 albedo = tex2D(_MainTex, i.uv).rgb;
float3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz*albedo;

float3 diffuse = _LightColor0.rgb * _Diffuse.rgb * max(0, dot(bump, lightDir)) * albedo;

return fixed4(ambient + diffuse,1.0);
}
ENDCG
}
}
}


PS：有时候你会看到一些黑白的高度图，工作原理是越白的地方越高

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120