文章目录
- 前言
- 一、回顾一下上一篇中公式
- 二、在Shader中实现
- 1.appdata中定义NORMAL与TANGENT语义.
- 2.v2f中声明三个变量用于组成成切线空间下的旋转矩阵.
- 3.在顶点着色器中执行:
- 4.在片断着色器中计算出世界空间下的法线,然后再拿去进行需要的计算:
- 三、最终效果
前言
我们在这篇文章中,实现法线贴图的正确采样
- Unity中Shader法线贴图(下)理论篇
一、回顾一下上一篇中公式
二、在Shader中实现
1.appdata中定义NORMAL与TANGENT语义.
half3 normal : NORMAL;
float4 tangent : TANGENT;
2.v2f中声明三个变量用于组成成切线空间下的旋转矩阵.
float3 tSpace0:TEXCOORD4;
float3 tSpace1:TEXCOORD5;
float3 tSpace2:TEXCOORD6;
3.在顶点着色器中执行:
- 切线从本地空间转化到世界空间
half3 worldTangent = UnityObjectToWorldDir(v.tangent);
//v.tangent.w:DCC软件中顶点UV值中的V值翻转情况.
//unity_WorldTransformParams.w:模型缩放是否有奇数负值.
half tangentSign = v.tangent.w * unity_WorldTransformParams.w;
- 由叉积计算出副切线
half3 worldBinormal = cross(worldNormal, worldTangent) * tangentSign;
- 得到 MT
o.tSpace0 = float3(worldTangent.x,worldBinormal.x,worldNormal.x);
o.tSpace1 = float3(worldTangent.y,worldBinormal.y,worldNormal.y);
o.tSpace2 = float3(worldTangent.z,worldBinormal.z,worldNormal.z);
4.在片断着色器中计算出世界空间下的法线,然后再拿去进行需要的计算:
- 利用点积操作,得出世界空间下的法线(法线纹理)
half3 normalTex = UnpackNormalWithScale(tex2D(_NormalTex,i.uv),scale);
half3 worldNormal = half3(dot(i.tSpace0,normalTex),dot(i.tSpace1,normalTex),dot(i.tSpace2,normalTex));
三、最终效果
最终代码:
//纹理的多级渐远 Mipmap
//纹理的环绕方式
//法线贴图
Shader "MyShader/P2_1_8"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
[KeywordEnum (Repeat,Clamp)]_WrapMode("WrapMode",int) = 0
[IntRange]_Mipmap ("Mipmap",Range(0,10)) = 0
//法线贴图
[Normal]_NormalTex("NormalTex",2D) = "bump" {}
//在属性面板定义立方体纹理
_CubeMap("CubeMap",Cube) = "white" {}
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma shader_feature _WRAPMODE_REPEAT _WRAPMODE_CLAMP
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
half3 normal : NORMAL;
float4 tangent : TANGENT;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
float3 localPos : TEXCOORD1;
float3 worldPos : TEXCOORD2;
half3 worldNormal : TEXCOORD3;
float3 tSpace0:TEXCOORD4;
float3 tSpace1:TEXCOORD5;
float3 tSpace2:TEXCOORD6;
};
sampler2D _MainTex;
float4 _MainTex_ST;
half _Mipmap;
samplerCUBE _CubeMap;
sampler2D _NormalTex;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
o.localPos = v.vertex.xyz;
o.worldPos = mul(unity_ObjectToWorld,v.vertex);
o.worldNormal = UnityObjectToWorldNormal(v.normal);
half3 worldTangent = UnityObjectToWorldDir(v.tangent);
//v.tangent.w:DCC软件中顶点UV值中的V值翻转情况.
//unity_WorldTransformParams.w:模型缩放是否有奇数负值.
half tangentSign = v.tangent.w * unity_WorldTransformParams.w;
half3 worldBinormal = cross(o.worldNormal, worldTangent) * tangentSign;
o.tSpace0 = float3(worldTangent.x,worldBinormal.x,o.worldNormal.x);
o.tSpace1 = float3(worldTangent.y,worldBinormal.y,o.worldNormal.y);
o.tSpace2 = float3(worldTangent.z,worldBinormal.z,o.worldNormal.z);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
//WrapMode
#if _WRAPMODE_REPEAT
i.uv = frac(i.uv);
#elif _WRAPMODE_CLAMP
//法一:
//i.uv = clamp(i.uv,0,1);
//法二:
i.uv = saturate(i.uv);
#endif
float4 uvMipmap = fixed4(i.uv,0,_Mipmap);
fixed4 col = tex2Dlod(_MainTex, uvMipmap);
//法线纹理
fixed3 normalTex = UnpackNormal(tex2D(_NormalTex,i.uv));
//max(0,dot(N,L))
fixed3 N1 = normalize(normalTex);
fixed3 L = _WorldSpaceLightPos0.xyz;
//return fixed4(normalTex,1);
//计算出世界空间下的法线
half3 worldNormal = half3(dot(i.tSpace0,normalTex),dot(i.tSpace1,normalTex),dot(i.tSpace2,normalTex));
return max(0,dot(worldNormal,L));
//CubeMap
fixed4 cubemap = texCUBE(_CubeMap,i.localPos);
//V,N,R
fixed3 V = normalize(i.worldPos - _WorldSpaceCameraPos);
fixed3 N = normalize(i.worldNormal);
fixed3 R = reflect(V,N);
cubemap = texCUBE(_CubeMap,R);
return cubemap;
return col;
}
ENDCG
}
}
}