法线纹理
法线纹理中存储的是表面法线的方向。
需要使用如下公式将,法线方向的分量[-1,1]映射到像素分量[0,1]:
pixel=normal*0.5+0.5
当在Shader中对法线纹理进行采样后,需要对结果使用公式进行一次反映射,用于得到原先的法线方向:
normal=pixel∗2−1
方向是相对于坐标空间来说的,因此法线纹理依据不同的坐标空间,还分为:
模型空间的法线纹理(object-space normal map):直接将模型空间中的表面法线存储在一张纹理中。
切线空间的法线纹理(tangent-space normal map):模型顶点的切线空间来存储法线。每个模型顶点都有自己的切线空间,其原点是顶点本身,z轴就是法线方向,x轴就是顶点的切线方向,y轴就是法线和切线叉积所得,也就是副切线。
法线贴图是为了使贴图变得更有质感,能够显示出轻微的凹凸感,给人更真实的感觉。
先将普通图片导入ps中,再选择滤镜下的3D,里面有个生成法线贴图的功能,生成后导出,放到unity中就可以用了。
切线空间的法线贴图
基本思路是:在片元着色器中通过纹理采样得到切线空间下的法线,然后再与切线空间下的视角方向、光照方向等进行计算,得到最终的计算结果。
需要先在顶点着色器中把视角方向和光照方向从模型空间变换到切线空间,即需要知道从模型空间到切线空间的变换矩阵。
从模型空间到切线空间的变换矩阵就是从切线空间到模型空间的变换矩阵的逆矩阵,把切线、副切线、法线按顺序排列即可以得到。
Shader "MyShader/bump"
{
Properties
{
_Specular("specular",Color)=(1,1,1,1)
_Gloss("gloss",Range(1,256))=20
_Diffuse("diffuse",Color)=(1,1,1,1)
_MainTex ("Texture", 2D) = "white" {}
_BumpMap("Normal Map",2D)="bump"{}
_BumpScale("BumpScale",float)=1
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#include "UnityLightingCommon.cginc"
sampler2D _MainTex;
float4 _MainTex_ST;
sampler2D _BumpMap;
float4 _BumpMap_ST;
float _BumpScale;
fixed4 _Diffuse;
fixed4 _Specular;
float _Gloss;
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
float2 normalUv : TEXCOORD1;
float3 lightDir : TEXCOORD2;
float3 viewDir : TEXCOORD3;
};
v2f vert (appdata_tan v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
o.normalUv = TRANSFORM_TEX(v.texcoord, _BumpMap);
//float3 binormal = closs(normalize(v.normal), normalize(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 tangentLight = normalize(i.lightDir);
fixed3 tangentView = normalize(i.viewDir);
//环境光
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.rgb;
//贴图采样
fixed3 col = tex2D(_MainTex, i.uv).rgb;
fixed4 packedNormal = tex2D(_BumpMap, i.normalUv);
//图片没有设置成normal map
//fixed3 tangentNormal;
//tangentNormal.xy = (packedNormal.xy * 2 - 1)*_BumpScale;
//tangentNormal.z = sqrt(1 - saturate(dot(tangentNormal.xy, tangentNormal.xy)));
//图片设置成normal map
fixed3 tangentNormal = UnpackNormal(packedNormal);
tangentNormal.xy *= _BumpScale;
//tangentNormal.z = sqrt(1 - saturate(dot(tangentNormal.xy, tangentNormal.xy)));
//漫反射
fixed3 diffuse = _LightColor0.rgb*col*_Diffuse.rgb*(saturate(dot(tangentLight, tangentNormal))*0.5+0.5);
//高光反射
fixed3 halfDir = normalize(tangentLight + tangentView);
fixed3 specular = _LightColor0.rgb*_Specular*pow(saturate(dot(tangentNormal, halfDir)), _Gloss);
fixed3 color = diffuse+ambient+specular;
return fixed4(color,1);
}
ENDCG
}
}
}
世界空间的法线贴图
在顶点着色器中计算从切线空间到世界空间的变换矩阵,并传递给片元着色器。变换矩阵的计算可以由顶点的切线、副切线和法线在世界空间的表示来得到。
Shader "MyShader/bump2"
{
Properties
{
_Specular("specular",Color)=(1,1,1,1)
_Gloss("gloss",Range(1,256))=20
_Diffuse("diffuse",Color)=(1,1,1,1)
_MainTex ("Texture", 2D) = "white" {}
_BumpMap("Normal Map",2D)="bump"{}
_BumpScale("BumpScale",float)=1
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#include "UnityLightingCommon.cginc"
sampler2D _MainTex;
float4 _MainTex_ST;
sampler2D _BumpMap;
float4 _BumpMap_ST;
float _BumpScale;
fixed4 _Diffuse;
fixed4 _Specular;
float _Gloss;
struct v2f
{
float4 vertex : SV_POSITION;
float4 uv : TEXCOORD0;
float4 TtoW0:TEXCOORD1;
float4 TtoW1:TEXCOORD2;
float4 TtoW2:TEXCOORD3;
};
v2f vert (appdata_tan v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv.xy = TRANSFORM_TEX(v.texcoord, _MainTex);
o.uv.zw = TRANSFORM_TEX(v.texcoord, _BumpMap);
fixed3 worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;// 世界空间下的顶点位置
fixed3 worldTangent = UnityObjectToWorldDir(v.tangent.xyz);// 世界空间切线
fixed3 worldNormal = UnityObjectToWorldNormal(v.normal);// 世界空间法线
fixed3 worldBinormal = cross(worldTangent, worldNormal)*v.tangent.w;// 世界空间副切线
o.TtoW0 = float4(worldTangent.x, worldBinormal.x, worldNormal.x, worldPos.x);
o.TtoW1 = float4(worldTangent.y, worldBinormal.y, worldNormal.y, worldPos.y);
o.TtoW2 = float4(worldTangent.z, worldBinormal.z, worldNormal.z, worldPos.z);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
float3 worldPos = float3(i.TtoW0.z,i.TtoW1.z,i.TtoW2.z);// 世界空间下的顶点坐标
fixed3 lightDir = normalize(UnityWorldSpaceLightDir(worldPos));// 世界空间的光照
fixed3 viewDir = normalize(UnityWorldSpaceViewDir(worldPos));// 世界空间的视角
//环境光
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.rgb;
//贴图采样
fixed3 col = tex2D(_MainTex, i.uv.xy).rgb;
fixed4 packedNormal = tex2D(_BumpMap, i.uv.zw);
//图片没有设置成normal map
//fixed3 tangentNormal;
//tangentNormal.xy = (packedNormal.xy * 2 - 1)*_BumpScale;
//tangentNormal.z = sqrt(1 - saturate(dot(tangentNormal.xy, tangentNormal.xy)));
//图片设置成normal map ,使用UnpackNormal对法线纹理进行采样和解码(需要把法线纹理的格式识别为Normal map)
fixed3 tangentNormal = UnpackNormal(packedNormal);
tangentNormal.xy *= _BumpScale;
//tangentNormal.z = sqrt(1 - saturate(dot(tangentNormal.xy, tangentNormal.xy)));
// 使用TtoW0/TtoW1/TtoW2存储的变换矩阵把法线变换到世界空间下。
fixed3 worldNormal = normalize(float3(dot(i.TtoW0.xyz, tangentNormal), dot(i.TtoW1.xyz, tangentNormal), dot(i.TtoW2.xyz, tangentNormal)));
//漫反射
fixed3 diffuse = _LightColor0.rgb*col*_Diffuse.rgb*(saturate(dot(lightDir, worldNormal))*0.5+0.5);
//高光反射
fixed3 halfDir = normalize(lightDir + viewDir);
fixed3 specular = _LightColor0.rgb*_Specular*pow(saturate(dot(worldNormal, halfDir)), _Gloss);
fixed3 color = diffuse+ambient+specular;
return fixed4(color,1);
}
ENDCG
}
}
}