【Unity Shader入门精要学习】基础纹理(五)

2019-09-15  本文已影响0人  小王子称号发放NPC

光照模型中法线纹理的使用

其实就是让法线纹理中的法线替代了原模型的顶点法线

切线空间中计算

将切线空间转换到世界空间

Shader "Unlit/NormalMap_WorldSpace"
{
    Properties
    {
        _Color("Color",Color) = (1.0,1.0,1.0,1.0)
        _MainTex("Main Tex",2D) = "white"{}
        _BumpTex("Bump Tex",2D) = "Bump"{}
        _BumpScale("Bump Scale",float) = -0.8
        _Specular("Specular",Color) = (1.0,1.0,1.0,1.0)
        _Gloss("Gloss",Range(30.0,100)) = 30.0
    }

    SubShader
    {
        Pass
        {
            Tags{"LightModel"="ForwardBase"}
            CGPROGRAM
            #include "Lighting.cginc"
            #include "UnityCG.cginc"

            #pragma vertex vert
            #pragma fragment frag

            fixed4 _Color;
            sampler2D _MainTex;
            float4 _MainTex_ST;
            sampler2D _BumpTex;
            float4 _BumpTex_ST;
            float _BumpScale;
            float _Gloss;
            fixed3 _Specular;

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

            struct a2f
            {
                float4 pos:SV_POSITION;
                float4 uv:TEXCOORD0;
                float4 tToW0:TEXCOORD1;
                float4 tToW1:TEXCOORD2;
                float4 tToW2:TEXCOORD3;
            };

            a2f vert(a2v v)
            {
                a2f o;
                o.pos = UnityObjectToClipPos(v.vertex);

                float3 worldPos = mul(unity_ObjectToWorld,v.vertex);
                float3 worldNormal = UnityObjectToWorldNormal(v.normal);
                float3 tangentWorld = UnityObjectToWorldDir(v.tangent.xyz);
                float3 binormalWorld = cross(worldNormal,tangentWorld) * v.tangent.w;
                // 构建从切线空间到世界空间的矩阵
                o.tToW0 = float4(tangentWorld.x,binormalWorld.x,worldNormal.x,worldPos.x);
                o.tToW1 = float4(tangentWorld.y,binormalWorld.y,worldNormal.y,worldPos.y);
                o.tToW2 = float4(tangentWorld.z,binormalWorld.z,worldNormal.z,worldPos.z);

                o.uv.xy = v.texcoord.xy * _MainTex_ST.xy + _MainTex_ST.zw;
                o.uv.zw = v.texcoord.xy * _BumpTex_ST.xy + _BumpTex_ST.zw;

                return o;
            }

            fixed4 frag(a2f i):SV_TARGET
            {
                float3 worldPos = float3(i.tToW0.w,i.tToW1.w,i.tToW2.w);
                fixed3 worldLightDir = normalize(UnityWorldSpaceLightDir(worldPos));
                fixed3 worldViewDir = normalize(UnityWorldSpaceViewDir(worldPos));

                //时刻注意这个法线纹理中的法线都是切线空间的,并且是单位向量,所以它们的模是1
                //在XY方向上扰动(其实就是伸缩)其实就是改变了法线向量的长度,从而控制表面的凹凸程度
                fixed3 packedNormal = UnpackNormal(tex2D(_BumpTex,i.uv.zw));
                packedNormal.xy *= _BumpScale;
                packedNormal.z = sqrt(1-saturate(dot(packedNormal.xy,packedNormal.xy)));

                fixed3 worldNormal = half3(dot(i.tToW0.xyz,packedNormal),dot(i.tToW1.xyz,packedNormal),dot(i.tToW2,packedNormal));
                fixed3 albedo = tex2D(_MainTex,i.uv.xy).rgb * _Color;//替代了漫反射颜色

                fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT * albedo;
                fixed3 diffuse = _LightColor0.rgb * albedo * saturate(dot(worldNormal,worldLightDir));

                fixed3 halfNormal = normalize(worldLightDir + worldViewDir);
                fixed3 specular = _LightColor0.rgb * _Specular.rgb * pow(saturate(dot(worldNormal,halfNormal)),_Gloss);
                return fixed4(ambient+diffuse+specular,1.0);
            }


            ENDCG
        }
    }

    Fallback "Specular"
}
上一篇下一篇

猜你喜欢

热点阅读