Shader "URP/SceneBase/Bumped Diffuse"
{
    Properties
    {
       _Color("Main Color", Color) = (1,1,1,1)
        _MainTex("Base (RGB)", 2D) = "white" {}
        _BumpMap("Normalmap", 2D) = "bump" {}
    }

    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 100

        Pass
        {

            Tags{ "LightMode" = "UniversalForward" }
            HLSLPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            // make fog work
            #pragma multi_compile_fog

        #pragma shader_feature _NORMALMAP

        #pragma multi_compile _ DIRLIGHTMAP_COMBINED
        #pragma multi_compile LIGHTMAP_OFF LIGHTMAP_ON
        #pragma multi_compile_instancing
        #pragma multi_compile SHADOWS_SHADOWMASK

        #pragma multi_compile _ _MAIN_LIGHT_SHADOWS
        #pragma multi_compile _ _MAIN_LIGHT_SHADOWS_CASCADE

        #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl" 

        #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/UnityInput.hlsl"
        #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"

        #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/ShaderGraphFunctions.hlsl"

        #include "../../Common/Template Shader/Define.hlsl"

        CBUFFER_START(UnityPerMaterial)
        sampler2D _MainTex;
        float4 _MainTex_ST;
        sampler2D _BumpMap;
        half4 _Color;
        CBUFFER_END

           struct Attributes
            {
                float4 positionOS   : POSITION;
                float4 color:COLOR;
                float3 normalOS     : NORMAL;
                float4 tangentOS    : TANGENT;
                float2 texcoord     : TEXCOORD0;
                float2 lightmapUV   : TEXCOORD1;
                UNITY_VERTEX_INPUT_INSTANCE_ID
             };

            struct v2f
            {
                float4 vertex : SV_POSITION;
                float4 color:COLOR;

                float2 uv : TEXCOORD0;
                float3x3 tangentTransform:TEXCOORD1;//占用3个语义 2 3 4
                float3 positionWS : TEXCOORD4;
                float3 normalWS : TEXCOORD5;

#ifdef _MAIN_LIGHT_SHADOWS
                float4 shadowCoord : TEXCOORD6;
#endif
                float fogCoord : TEXCOORD7;

                DECLARE_LIGHTMAP_OR_SH(lightmapUV, vertexSH, 8);

                UNITY_VERTEX_INPUT_INSTANCE_ID
                UNITY_VERTEX_OUTPUT_STEREO
            };



            v2f vert (Attributes v)
            {
                v2f o;
                UNITY_SETUP_INSTANCE_ID(v);
                UNITY_TRANSFER_INSTANCE_ID(v, o);
                UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);

                o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);

                VertexPositionInputs vertexInput = GetVertexPositionInputs(v.positionOS.xyz);
                o.positionWS = vertexInput.positionWS;
                o.vertex = vertexInput.positionCS;
                o.fogCoord = vertexInput.positionCS.z;

                VertexNormalInputs normalInput = GetVertexNormalInputs(v.normalOS, v.tangentOS);
                o.normalWS = normalInput.normalWS;
                o.tangentTransform = float3x3(normalInput.tangentWS, normalInput.bitangentWS, normalInput.normalWS);

#if defined(_MAIN_LIGHT_SHADOWS) && !defined(_RECEIVE_SHADOWS_OFF)
                o.shadowCoord = GetShadowCoord(vertexInput);
#endif
                OUTPUT_LIGHTMAP_UV(v.lightmapUV, unity_LightmapST, o.lightmapUV);
                OUTPUT_SH(o.normalWS.xyz, o.vertexSH);

                return o;
            }

            half4 frag (v2f i) : SV_Target
            {
            
			UNITY_SETUP_INSTANCE_ID(i);
			UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(i);

#ifdef _MAIN_LIGHT_SHADOWS
			Light light = GetMainLight(i.shadowCoord);
#else
			Light light = GetMainLight();
#endif
            half4 col = tex2D(_MainTex, i.uv);
            half4 bump = tex2D(_BumpMap, i.uv);
            half3 tangentNormal = UnpackNormal(bump);
            float3 L = light.direction;
            float3 N = normalize(mul(tangentNormal.rgb, i.tangentTransform));
            half ndh = max(0, dot(N, L));
            half3 directDiffuse = ndh * light.color;

#ifdef LIGHTMAP_ON
            half3 bakeGI = SAMPLE_GI(i.lightmapUV, i.vertexSH, i.normalWS);
            half4 shadowmask = SampleShadowMask(i.lightmapUV);
            shadowmask = ShadowFade(light, i.positionWS, shadowmask);
            MixRealtimeAndBakedGI(light, i.normalWS, bakeGI, shadowmask, i.positionWS);

            directDiffuse *= light.shadowAttenuation;
            col.rgb = col.rgb * directDiffuse + bakeGI;

#else
            directDiffuse *= light.shadowAttenuation;
            col.rgb = col.rgb * directDiffuse + col.rgb * unity_Ambient.rgb;
#endif
            float density = ComputeFogFactor(i.fogCoord);
            col.rgb = MixFog(col.rgb, density);
             return col;
            }
            ENDHLSL
        }

        UsePass "Universal Render Pipeline/Lit/DepthOnly"
        UsePass "Universal Render Pipeline/Lit/ShadowCaster"
    }
}
