在片元着色器中通过对纹理的采样得到切线空间下的法线,然后再与切线空间下的视角方向,光照方向等进行计算,得到最终的光照结果。
Shader "Custom/Texture/TangentNormalTextute"
{
Properties
{
//主纹理的贴图
_MainTex ("Texture", 2D) = "white" {}
_Color("Color",Color)=(1,1,1,1)
//法线贴图
_BumpMap("NormalMap",2D)="white"{}
//控制凹凸程度
_BumpScale("BumpScale",Float)=1.0
//高光颜色
_Specular("Specular",Color)=(1,1,1,1)
//高光系数
_Gloss("Gloss",Range(8.0,256))=20
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
Tags{"LightMode"="ForwardBase"}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#include "Lighting.cginc"
sampler2D _MainTex;
float4 _MainTex_ST;
fixed4 _Color;
sampler2D _BumpMap;
float4 _BumpMap_ST;
float _BumpScale;
fixed4 _Specular;
float _Gloss;
struct appdata
{
float4 vertex : POSITION;
float4 uv : TEXCOORD0;
float3 normal:NORMAL;
float4 tangent:TANGENT;
};
struct v2f
{
float4 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
float3 lightDir:TEXCOORD1;
float3 viewDir:TEXCOORD2;
};
v2f vert (appdata v)
{
v2f o;
//将顶点位置转化到剪切空间
o.vertex = UnityObjectToClipPos(v.vertex);
//计算主纹理的缩放和平移值
o.uv.xy = TRANSFORM_TEX(v.uv, _MainTex);
//计算法线的缩放和平移值
o.uv.zw= TRANSFORM_TEX(v.uv, _BumpMap);
//float3 binormal=cross(normalize(v.normal),normalize(v.tangent.xyz))*v.tangent.w;
//float3x3 rotation=float3(v.tangent.xyz,binormal,v.normal);
//与上面注释代码功能相同,直接计算得到rotation变换矩阵
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 pcakedNormal=tex2D(_BumpMap,i.uv.zw);
fixed3 tangentNormal;
tangentNormal=UnpackNormal(pcakedNormal);
//改变凹凸程度
tangentNormal.xy*=_BumpScale;
tangentNormal.z=sqrt(1.0-saturate(dot(tangentNormal.xy,tangentNormal.xy)));
//材质反射率
fixed3 albedo=tex2D(_MainTex, i.uv).rgb*_Color.rgb;
fixed3 ambient=UNITY_LIGHTMODEL_AMBIENT.xyz*albedo;
fixed3 diffuse=_LightColor0.rgb * albedo*max(0,dot(tangentNormal,tangentLightDir));
fixed3 halfDir=normalize(tangentLightDir+tangentViewDir);
fixed3 specular=_LightColor0.rgb*_Specular.rgb*pow(max(0,dot(tangentNormal,halfDir)),_Gloss);
return fixed4(ambient+diffuse+specular,1.0);
}
ENDCG
}
}
}
在顶点着色器中计算从切线空间到世界空间的变换矩阵,并把它传递个片元着色器。变换矩阵的计算可以有顶点的切线、副切线和法线在世界空间下的表示来得到。最后我们只需要在片元着色器中把法线纹理中的法线方向从切线空间变换到世界空间下即可。
// Upgrade NOTE: replaced ‘_Object2World’ with ‘unity_ObjectToWorld’
Shader “Custom/Texture/WorldNormalTextute”
{
Properties
{
_MainTex (“Texture”, 2D) = “white” {}
_Color(“Color”,Color)=(1,1,1,1)
_BumpMap(“NormalMap”,2D)=”white”{}
_BumpScale(“BumpScale”,Float)=1.0
_Specular(“Specular”,Color)=(1,1,1,1)
_Gloss(“Gloss”,Range(8.0,256))=20
}
SubShader
{
Tags { “RenderType”=”Opaque” }
LOD 100
Pass
{
Tags{"LightMode"="ForwardBase"}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#include "Lighting.cginc"
sampler2D _MainTex;
float4 _MainTex_ST;
fixed4 _Color;
sampler2D _BumpMap;
float4 _BumpMap_ST;
float _BumpScale;
fixed4 _Specular;
float _Gloss;
struct appdata
{
float4 vertex : POSITION;
float4 uv : TEXCOORD0;
float3 normal:NORMAL;
float4 tangent:TANGENT;
};
struct v2f
{
float4 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
float4 TtoW1:TEXCOORD1;
float4 TtoW2:TEXCOORD2;
float4 TtoW3:TEXCOORD3;
};
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv.xy = TRANSFORM_TEX(v.uv, _MainTex);
o.uv.zw= TRANSFORM_TEX(v.uv, _BumpMap);
float3 worldPos=mul(unity_ObjectToWorld,v.vertex);
float3 worldNormal=UnityObjectToWorldNormal(v.normal);
float3 worldTangent=UnityObjectToWorldDir(v.tangent.xyz);
float3 worldBinormal=cross(worldNormal,worldTangent)*v.tangent.w;
o.TtoW1=float4(worldTangent.x,worldBinormal.x,worldNormal.x,worldPos.x);
o.TtoW2=float4(worldTangent.y,worldBinormal.y,worldNormal.y,worldPos.y);
o.TtoW3=float4(worldTangent.z,worldBinormal.z,worldNormal.z,worldPos.z);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
float3 worldPos=fixed3(i.TtoW1.w,i.TtoW2.w,i.TtoW3.w);
fixed3 lightDir=normalize(UnityWorldSpaceLightDir(worldPos));
fixed3 viewDir=normalize(UnityWorldSpaceViewDir(worldPos));
fixed3 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.TtoW1.xyz,bump),dot(i.TtoW2.xyz,bump),dot(i.TtoW3.xyz,bump)));
fixed3 albedo=tex2D(_MainTex, i.uv).rgb*_Color.rgb;
fixed3 ambient=UNITY_LIGHTMODEL_AMBIENT.xyz*albedo;
fixed3 diffuse=_LightColor0.rgb * albedo*max(0,dot(bump,lightDir));
fixed3 halfDir=normalize(lightDir+viewDir);
fixed3 specular=_LightColor0.rgb*_Specular.rgb*pow(max(0,dot(bump,halfDir)),_Gloss);
return fixed4(ambient+diffuse+specular,1.0);
}
ENDCG
}
}