(学习笔记)Unity Shader 法线计算方式

(学习笔记)Unity Shader 法线计算方式 

Shader "Custom/BumpMapping"
{
    Properties
    {
        _Tint("Color Tint",Color)=(1,1,1,1)
        _MainTex("Main Tex",2D)="white"{}
        _Normal("Normal Tex",2D)="bump"{}
        _BumpScale("Bump Scale",Float)=1
        _Specular("Specular",Color)=(1,1,1,1)
        _Gloss("Gloss",Range(8,256))=20
    }
    SubShader
    {
        Pass{
            Tags{"LightMode"="ForwardBase"}
            CGPROGRAM

            #pragma vertex vert
            #pragma fragment frag
            #include "Lighting.cginc"

            sampler2D _MainTex;
            float4 _MainTex_ST;
            sampler2D _Normal;
            float4 _Normal_ST;

            float _Gloss;
            float _BumpScale;
            fixed4 _Tint;
            fixed4 _Specular;

            struct a2v{
                float4 vertex :POSITION;
                float3 normal:NORMAL;
                float4 tangent:TANGENT;
                float4 texcoord:TEXCOORD0;
            };

            struct v2f{
                float4 pos:SV_POSITION;
                float4 uv:TEXCOORD0;
                float3 lightDir:TEXCOORD1;
                float3 viewDir:TEXCOORD2;
            };

            v2f vert(a2v v){
                v2f o;
                //对象空间转换到裁切空间
                o.pos=UnityObjectToClipPos(v.vertex);
                //贴图UV
                o.uv.xy=v.texcoord.xy*_MainTex_ST.xy+_MainTex_ST.zw;
                //法线UV
                o.uv.zw=v.texcoord.xy*_Normal_ST.xy+_Normal_ST.zw;
                //副法线计算,叉积(单位法线,单位切线)*切线方向
                float3 binormal=cross(normalize(v.normal),normalize(v.tangent).xyz)*v.tangent.w;
                //旋转矩阵 [切线,副法线,法线]
                float3x3 rotation=float3x3(v.tangent.xyz,binormal,v.normal);
                //光照方向=获取顶点的光照方向
                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);
                //法线的UV取像素
                fixed4 packedNormal=tex2D(_Normal,i.uv.zw);

                //获取对应像素的法线值
                fixed3 tangentNormal=UnpackNormal(packedNormal);
                //坐标缩放
                tangentNormal.xy*=_BumpScale;
                tangentNormal.z=sqrt(1.0-saturate(dot(tangentNormal.xy,tangentNormal.xy)));
                //切线光照方向,切线视角方向的合向量
                fixed3 halfDir=normalize(tangentLightDir+tangentViewDir);
                //取自发光 像素*颜色值
                fixed3 albedo=tex2D(_MainTex,i.uv).rgb*_Tint.rgb;
                //环境光
                fixed3 ambient=UNITY_LIGHTMODEL_AMBIENT.xyz;
                //漫反射计算
                fixed3 diffuse=_LightColor0.rgb*albedo*saturate(dot(tangentNormal,tangentLightDir));
                //高光计算
                fixed3 specular=_LightColor0.rgb*albedo*pow(saturate(dot(tangentNormal,halfDir)),_Gloss);
                //结果=自放光+漫反射+高光
                return fixed4(ambient+diffuse+specular,1.0); 
            }

            ENDCG
        }
    }
    FallBack "Diffuse"
}

以上摘录至UnityShader入门精要