优化实现Mobile/Bumped Diffuse

在上一篇帖子的基础上增加一张法线贴图即可:

Shader "James/Scene/Bumped_Diffuse"
{
    Properties
    {
        _MainTex ("Base (RGB)", 2D) = "white" {}
        _BumpTex("Normal Map", 2D) = "bump" {}
        _MainLightColor("主光颜色", Color) = (1,1,1,1)
        _MainLightDir("主光方向", Vector) = (1,1,0,0)
         _MainLightBrightness ("主光强度", Range(0, 10)) = 1
    }
    
    SubShader
    {
        Tags { "RenderType"="Opaque" "Queue"="Geometry" }
        LOD 200
        
        Pass
        {
            Tags { "LightMode"="ForwardBase" }
            Lighting Off
            
            CGPROGRAM
            #pragma fragmentoption ARB_precision_hint_fastest
            
            #pragma vertex vert
            #pragma fragment frag
            #pragma multi_compile_fwdbase
            #pragma multi_compile_fog
            
            #include "UnityCG.cginc"

            float4 _MainLightColor;
            float4 _MainLightDir;
            float _MainLightBrightness;

            uniform sampler2D _MainTex, _BumpTex;
            uniform half4 _MainTex_ST, _BumpTex_ST;

            struct vertexIN_base
            {
                float4 vertex : POSITION;
                float3 normal : NORMAL;
                float4 tangent : TANGENT;
                float2 texcoord : TEXCOORD0;
            };
            
            struct v2f_base
            {
                float4 pos : SV_POSITION;
                half2  uv : TEXCOORD0;
                half4 tangentWorld : TEXCOORD1;
                half4 normalWorld : TEXCOORD2;
                half4 binormalWorld : TEXCOORD3;
                UNITY_FOG_COORDS(5)
            };

            v2f_base vert(vertexIN_base v)
            {
                v2f_base o;
                o.pos = UnityObjectToClipPos(v.vertex);
                o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);

                o.tangentWorld.xyz = normalize(mul(unity_ObjectToWorld, half4(v.tangent.xyz, 0.0)).xyz);
                o.normalWorld.xyz = normalize(mul(unity_ObjectToWorld, half4(v.normal, 0.0)).xyz);
                o.binormalWorld.xyz = cross(o.normalWorld.xyz, o.tangentWorld.xyz) * v.tangent.w;

                UNITY_TRANSFER_FOG(o,o.pos);
                return o; 
            }
            
            fixed4 frag(v2f_base i) : COLOR
            {
                float3 bump = UnpackNormal(tex2D(_BumpTex,TRANSFORM_TEX(i.uv, _BumpTex)));
                half3x3 local2WorldTranspose = half3x3(i.tangentWorld.xyz, i.binormalWorld.xyz, i.normalWorld.xyz);
                half3 worldNormal = normalize(mul(bump, local2WorldTranspose));

                float diffuse = max(0, dot(normalize(worldNormal), normalize(_MainLightDir)));

                fixed4 mainColor = tex2D(_MainTex, i.uv);
                fixed4 clr = mainColor * _MainLightColor * diffuse * _MainLightBrightness;

                UNITY_APPLY_FOG(i.fogCoord,clr);

                return clr;
            }
            ENDCG
        }
    }
    FallBack Off
}

猜你喜欢

转载自www.cnblogs.com/sifenkesi/p/9922889.html