法线贴图

Unity属性中添加法线贴图属性和凹凸强度系数

_NormalTex("NormalTex",2D)="white"{}
_NormalIndexd("AoTuQiangDu",float)=1

CG声明对应的变量和平移缩放变量

sampler2D _NormalTex;
float4 _NormalTex_ST;
float _NormalIndexd;

引进切线到顶点函数float4 tangent:TANGENT;

使用传入片元的uv参数的xy传递贴图uv坐标,zw传递法线贴图的uv坐标

f.uv.zw = v.texcoord.xy * _NormalTex_ST.xy + _NormalTex_ST.zw;

转变视野方向和光线方向到切线空间

TANGENT_SPACE_ROTATION;//一个宏,计算出rotation
f.lightDir = normalize(mul(rotation,ObjSpaceLightDir(v.vertex))).xyz;
f.viewDir = normalize(mul(rotation,ObjSpaceViewDir(v.vertex))).xyz;

片元中得出相应的法线

fixed3 tangentNormal = UnpackNormal(tex2D(_NormalTex,f.uv.wz));//像素图片(0-255)转化为方向的(-1,1)
tangentNormal.xy = tangentNormal.xy * _NormalIndexd;
tangentNormal = normalize(tangentNormal);

首页 我的博客
粤ICP备17103704号