A·B 两个向量的点乘就是A的转置乘以B: (A)TB 。
对法线做空间变换时,若模型本身做了xyz轴上的非均匀缩放。则法线值会产生偏移,但切线并不会受到影响。
在切线空间里计算光照的好处:只需要在顶点shader里面计算切线空间下光线方向和视角方向,在片段shader里面采样的法线已经是切线空间了,不用做矩阵转换。
如果不在切线空间里面计算光照,那么在片段shader里面采样法线贴图,就需要变换空间,需要做大量的矩阵运算,比较耗时。所以在切线空间计算光照会比较好。
切线空间计算光照代码:(TBN矩阵,记得做矩阵运算的时候顺序放对)
Shader "Unlit/012"
{
Properties
{
_MainTex("MainTex",2D) = "white" {}
_BumpMap("Normal Map",2D) = "bump" {}
_BumpScale("Bump Scale",float) = 1
_Diffuse("Diffuse",Color) = (1,1,1,1)
_Specular("Specular",Color) = (1,1,1,1)
_Gloss("Gloss",Range(1,256)) = 32
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#include "Lighting.cginc"
fixed4 _Diffuse;
fixed4 _Specular;
float _Gloss;
sampler2D _MainTex;
float4 _MainTex_ST;
sampler2D _BumpMap;
float4 _BumpMap_ST;
float _BumpScale;
struct v2f
{
float4 vertex:SV_POSITION;
fixed3 lightDir:TEXCOORD0;
fixed3 viewDir:TEXCOORD1;
float2 uv:TEXCOORD2;
float2 normalUV:TEXCOORD3;
};
v2f vert(appdata_tan v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.texcoord.xy * _MainTex_ST.xy + _MainTex_ST.zw;
o.normalUV = TRANSFORM_TEX(v.texcoord,_BumpMap);
//副切线
//float3 binoraml = cross(normalize(v.normal),normallize(v.tangent.xyz)) * v.tangent.w;
//float3x3 rotation = float3x3(v.tangent.xyz,binormal,v.normal);
TANGENT_SPACE_ROTATION;
//切线空间光源方向以及视角方向
o.lightDir = mul(rotation,ObjSpaceLightDir(v.vertex)).xyz;
o.viewDir = mul(rotation,ObjSpaceViewDir(v.vertex)).xyz;
return o;
}
fixed4 frag(v2f i):SV_TARGET
{
fixed3 tangentLightDir = normalize(i.lightDir);
fixed3 tangentViewDir = normalize(i.viewDir);
fixed4 packedNormal = tex2D(_BumpMap,i.normalUV);
//没有设置normalMap
//fixed3 tangentNormal;
//tangentNormal.xy = (packedNormal.xy * 2 - 1) * _BumpScale;
//tangentNormal.z = sqrt(1 - saturate(dot(tangentNormal.xy,tangentNormal.xy)));
//设置了normalMap
fixed3 tangentNormal = UnpackNormal(packedNormal);
tangentNormal.xy *= _BumpScale;
//环境光
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
fixed3 albedo = tex2D(_MainTex,i.uv);
fixed3 diffuse = _LightColor0.rgb * albedo * _Diffuse.rgb * (dot(tangentLightDir,tangentNormal)*0.5 + 0.5);
fixed3 halfDir = normalize(tangentLightDir + tangentViewDir);
fixed3 specular = _LightColor0.rgb * _Specular.rgb * pow(saturate(dot(halfDir,tangentNormal)),_Gloss);
fixed3 color = diffuse + ambient + specular;
return fixed4(color,1);
}
ENDCG
}
}
}
世界空间计算光照代码:
Shader "Unlit/013"
{
Properties
{
_MainTex("MainTex",2D) = "white" {}
_BumpMap("Normal Map",2D) = "bump" {}
_BumpScale("Bump Scale",float) = 1
_Diffuse("Diffuse",Color) = (1,1,1,1)
_Specular("Specular",Color) = (1,1,1,1)
_UseDirInVertex("UseDirInVertex",Range(0,1)) = 0
_Gloss("Gloss", Range(1,256)) = 5
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#include "Lighting.cginc"
sampler2D _MainTex;
float4 _MainTex_ST;
sampler2D _BumpMap;
float4 _BumpMap_ST;
float _BumpScale;
fixed4 _Diffuse;
fixed4 _Specular;
float _Gloss;
float _UseDirInVertex;
struct v2f
{
float4 vertex:SV_POSITION;
float4 uv:TEXCOORD0;
float4 TtiW1:TEXCOORD1;
float4 TtiW2:TEXCOORD2;
float4 TtiW3:TEXCOORD3;
fixed3 lightDir:TEXCOORD4;
fixed3 viewDir:TEXCOORD5;
};
v2f vert(appdata_tan v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv.xy = v.texcoord.xy * _MainTex_ST.xy + _MainTex_ST.zw;
o.uv.zw = TRANSFORM_TEX(v.texcoord,_BumpMap);
//计算世界坐标下的顶点位置,法线,切线,副法线
float3 worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
fixed3 worldNormal = UnityObjectToWorldNormal(v.normal);
fixed3 worldTangent = UnityObjectToWorldDir(v.tangent.xyz);
fixed3 worldBinormal = cross(worldNormal,worldTangent) * v.tangent.w;
o.TtiW1 = float4(worldTangent.x,worldBinormal.x,worldNormal.x,worldPos.x);
o.TtiW2 = float4(worldTangent.y,worldBinormal.y,worldNormal.y,worldPos.y);
o.TtiW3 = float4(worldTangent.z,worldBinormal.z,worldNormal.z,worldPos.z);
//世界空间光源方向以及视角方向
o.lightDir = UnityWorldSpaceLightDir(worldPos);
o.viewDir = UnityWorldSpaceViewDir(worldPos);
return o;
}
fixed4 frag(v2f i):SV_TARGET
{
float3 worldPos = float3(i.TtiW1.w,i.TtiW2.w,i.TtiW3.w);
fixed3 lightDir = _UseDirInVertex > 0 ? i.lightDir : UnityWorldSpaceLightDir(worldPos);
fixed3 viewDir = _UseDirInVertex > 0 ? i.viewDir : UnityWorldSpaceViewDir(worldPos);
fixed3 worldLightDir = normalize(lightDir);
fixed3 worldViewDir = normalize(viewDir);
fixed4 packedNormal = tex2D(_BumpMap,i.uv.zw);
fixed3 tangentNormal = UnpackNormal(packedNormal);
tangentNormal.xy *= _BumpScale;
fixed3 worldNormal = normalize(float3(dot(i.TtiW1.xyz,tangentNormal),dot(i.TtiW2.xyz,tangentNormal),dot(i.TtiW3.xyz,tangentNormal)));
//环境光
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
fixed3 albedo = tex2D(_MainTex,i.uv.xy).rgb;
fixed3 diffuse = _LightColor0.rgb * albedo * _Diffuse.rgb * (dot(worldLightDir,worldNormal)*0.5 + 0.5);
fixed3 halfDir = normalize(worldLightDir + worldViewDir);
fixed3 specular = _LightColor0.rgb * _Specular.rgb * pow(saturate(dot(halfDir,worldNormal)),_Gloss);
fixed3 color = diffuse + ambient + specular;
return fixed4(color,1);
}
ENDCG
}
}
}