Shader "URP/SceneBase/JR_Tree_Opaque"
{
    Properties
    {
        [MainColor] _BaseColor("Color", Color) = (1,1,1,1)
        [MainTexture] _BaseMap("Albedo", 2D) = "white" {}
        _NormalMap("Normal Map", 2D) = "bump" {}
        _normalScale("Normal Scale", Range(0,3)) = 1
        [Main(SpecularAo,_)]
        _DebugShowAo("(显示AO开关)高光+AO", float) = 0
        [Sub(SpecularAo)]_VertexColorMul ("AO颜色", Color) = (1,1,1,1)
        [Sub(SpecularAo)]_AoMin("Ao最低亮度",Range(-0.5,0.5)) = 0
        [Sub(SpecularAo)]_AoMax("Ao最高亮度",Range(0.5,2)) = 1
        [Sub(SpecularAo)]_Roughness ("粗糙度", Range (0, 1)) = 1
        [Main(BlendGrass,_USE_BLEND)]
        _USE_Blend("根部混合草", float) = 0
        [Sub(BlendGrass)]_BlendTex("Blend Texture", 2D) = "white" {}
        [Sub(BlendGrass)]_BlendMin ("混合最小值", Range(-0.5,0.5)) = 0
        [Sub(BlendGrass)]_BlendMax ("混合最大值", Range(0,1)) = 1
        [Sub(BlendGrass)]_BlendColor ("混合颜色", Color) = (1,1,1,1)
        [ToggleOff] _SpecularHighlights("Specular Highlights", Float) = 1.0
        //[ToggleOff] _EnvironmentReflections("Environment Reflections", Float) = 1.0
    }
    SubShader
    {
        Name "ForwardLit"
        Tags{"LightMode" = "UniversalForward"}

        ZWrite On
        ZTest LEqual
        Cull[_Cull]
        LOD 100

        Pass
        {
            HLSLPROGRAM

            #pragma shader_feature_local _SPECULARHIGHLIGHTS_OFF
            // #pragma shader_feature_local _ENVIRONMENTREFLECTIONS_OFF
            // #pragma shader_feature_local _DETAILNOMAL
            // #pragma shader_feature_local _USEPACKMAP
            // #pragma shader_feature_local _ALPHATEST_ON
            // #pragma shader_feature_local _POM_ON
            // #pragma shader_feature_local _USE_Blend
            #pragma multi_compile _ SHADOWS_SHADOWMASK
            #pragma multi_compile _ LIGHTMAP_ON
            #pragma shader_feature_local _USE_BLEND

            #define _MAIN_LIGHT_SHADOWS
            #define _ENVIRONMENTREFLECTIONS_OFF 1

            #pragma vertex LitPassVertex
            #pragma fragment TreeFragment
            // make fog work
            #pragma multi_compile_fog
            

            // #include "UnityCG.cginc"
            //#include "JR_Scene_PbrLitPass.hlsl"
            #include "Assets/URP/Shader/HLSL/Scene/Pbr/JR_Scene_PbrLight.hlsl"
            #include "Assets/URP/Shader/HLSL/Environment/Sky&Fog/VolumetricFogForObject.hlsl"
            float4 _BaseMap_ST;
            half4 _BaseColor;
            half4 _VertexColorMul,_BlendColor;

            half _Roughness;
            //half _Emission;
            half _normalScale;
            half _BlendMin,_BlendMax;
            half _AoMin,_AoMax,_DebugShowAo;
            half4 _NightRainValue; // x:emission y:roughness z:color w:normal
            TEXTURE2D(_BlendTex);            SAMPLER(sampler_BlendTex);
            half2 _BlendTex_ST;
            TEXTURE2D(_BaseMap);            SAMPLER(sampler_BaseMap);
            TEXTURE2D(_NormalMap);            SAMPLER(sampler_NormalMap);
            struct Attributes
            {
                float4 positionOS   : POSITION;
                float3 normalOS     : NORMAL;
                float4 tangentOS    : TANGENT;
                half4 color         : COLOR;
                float2 texcoord     : TEXCOORD0;
                float2 lightmapUV   : TEXCOORD1;
                UNITY_VERTEX_INPUT_INSTANCE_ID
            };

            struct Varyings
            {
                float2 uv                       : TEXCOORD0;
                DECLARE_LIGHTMAP_OR_SH(lightmapUV, vertexSH, 1);

                float3 positionWS               : TEXCOORD2;

                float4 normalWS                 : TEXCOORD3;    // xyz: normal, w: viewDir.x
                float4 tangentWS                : TEXCOORD4;    // xyz: tangent, w: viewDir.y
                float4 bitangentWS              : TEXCOORD5;    // xyz: bitangent, w: viewDir.z
                half4 calaPos                   : TEXCOORD6;
                float4 shadowCoord              : TEXCOORD7;
                half3 viewDirWS                 : TEXCOORD8;
                float4 fogCoord                 : TEXCOORD9;
                half4 color                     : COLOR;

                float4 positionCS               : SV_POSITION;
                UNITY_VERTEX_INPUT_INSTANCE_ID
                UNITY_VERTEX_OUTPUT_STEREO
            };
            Varyings LitPassVertex(Attributes input)
            {
                Varyings output = (Varyings)0;

                UNITY_SETUP_INSTANCE_ID(input);
                UNITY_TRANSFER_INSTANCE_ID(input, output);
                UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);

                VertexPositionInputs vertexInput = GetVertexPositionInputs(input.positionOS.xyz);
                VertexNormalInputs normalInput = GetVertexNormalInputs(input.normalOS, input.tangentOS);
                half3 viewDirWS = GetCameraPositionWS() - vertexInput.positionWS;

                output.uv = TRANSFORM_TEX(input.texcoord, _BaseMap);

                output.normalWS = half4(normalInput.normalWS, viewDirWS.x);
                output.tangentWS = half4(normalInput.tangentWS, viewDirWS.y);
                output.bitangentWS = half4(normalInput.bitangentWS, viewDirWS.z);

                OUTPUT_LIGHTMAP_UV(input.lightmapUV, unity_LightmapST, output.lightmapUV);
                OUTPUT_SH(output.normalWS.xyz, output.vertexSH);

                output.positionWS = vertexInput.positionWS;
                output.calaPos = VertCalaShadow(output.positionWS.xyzz, _DirX, _DirY);

                output.shadowCoord = GetShadowCoord(vertexInput);

                output.positionCS = vertexInput.positionCS;

                output.fogCoord = VolumetricFog_GetFogCoord(vertexInput.positionNDC, vertexInput.positionVS.z);
                output.color = input.color;

                return output;
            }
            PbrInputData InitializeInputData(Varyings i, half3 normalTS)
            {
                PbrInputData inputData = (PbrInputData)0;
                inputData.positionWS = i.positionWS;
                inputData.normalWS = TransformTangentToWorld(normalTS,
                    half3x3(i.tangentWS.xyz, i.bitangentWS.xyz, i.normalWS.xyz));
                inputData.normalWS = normalize(inputData.normalWS);
                half3 viewDirWS = half3(i.normalWS.w, i.tangentWS.w, i.bitangentWS.w);
                inputData.viewDirectionWS = normalize(viewDirWS);
                inputData.shadowCoord = TransformWorldToShadowCoord(inputData.positionWS);
                inputData.calaPos = i.calaPos;

                inputData.bakedGI = SAMPLE_GI(i.lightmapUV, i.vertexSH, inputData.normalWS);

    #if defined(LIGHTMAP_ON) && defined(SHADOWS_SHADOWMASK)  
                inputData.bakedAtten = SampleShadowMask(i.lightmapUV);
    #endif
                return inputData;
            }
            half4 TreeFragment(Varyings input) : SV_Target
            {
                UNITY_SETUP_INSTANCE_ID(input);
                UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
                //normal
                half4 N = SAMPLE_TEXTURE2D(_NormalMap, sampler_NormalMap, input.uv);
                half3 normal = UnpackNormalScale(N,_normalScale);
                normal.z = sqrt(1 - normal.x*normal.x - normal.y*normal.y);
                normal = normalize(normal);
                PbrInputData inputData = InitializeInputData(input, normal);
                //albedo
                half4 albedoAlpha = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, input.uv);
                //Blend
            #ifdef _USE_BLEND
                half3 BlendTex = SAMPLE_TEXTURE2D(_BlendTex, sampler_BlendTex, input.uv*_BlendTex_ST.xx).rgb * _BlendColor.rgb;
                input.color.r = smoothstep(_BlendMin,_BlendMax,input.color.r);
                albedoAlpha.rgb = lerp(BlendTex,albedoAlpha.rgb,input.color.r);
            #endif
                //ao
                half3 Ao = input.color.aaa;
				Ao = smoothstep(_AoMin,_AoMax, Ao);
				albedoAlpha.rgb *= lerp(_VertexColorMul.rgb, 1, Ao);
                //pbr
                half roughness = (1 - N.b) * _Roughness * (1 - _NightRainValue.y);
                half4 color = UniversalFragmentPBR(inputData, albedoAlpha, 0, 0, roughness, 1, 0, 1);
                //ShowAo
				color.rgb = _DebugShowAo?Ao:color.rgb;
                // color.rgb = VolumetricFog_GetFog(color.rgb, input.fogCoord);

                return color;
            }

            ENDHLSL
        }

        Pass
        {
            Name "ShadowCaster"
            Tags{"LightMode" = "ShadowCaster"}

            ZWrite On
            ZTest LEqual
            Cull[_Cull]

            HLSLPROGRAM
            // Required to compile gles 2.0 with standard srp library
            #pragma prefer_hlslcc gles
            #pragma exclude_renderers d3d11_9x
            #pragma target 2.0

            // -------------------------------------
            // Material Keywords
            #pragma shader_feature _ALPHATEST_ON

            //--------------------------------------
            // GPU Instancing
            #pragma multi_compile_instancing
            #pragma shader_feature _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A

            #pragma vertex ShadowPassVertex
            #pragma fragment ShadowPassFragment

            #include "Packages/com.unity.render-pipelines.universal/Shaders/LitInput.hlsl"
            #include "Packages/com.unity.render-pipelines.universal/Shaders/ShadowCasterPass.hlsl"
            ENDHLSL
        }

        Pass
        {
            Name "DepthOnly"
            Tags{"LightMode" = "DepthOnly"}

            ZWrite On
            ColorMask 0
            Cull[_Cull]

            HLSLPROGRAM
            // Required to compile gles 2.0 with standard srp library
            #pragma prefer_hlslcc gles
            #pragma exclude_renderers d3d11_9x
            #pragma target 2.0

            #pragma vertex DepthOnlyVertex
            #pragma fragment DepthOnlyFragment

            // -------------------------------------
            // Material Keywords
            #pragma shader_feature _ALPHATEST_ON
            #pragma shader_feature _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A

            //--------------------------------------
            // GPU Instancing
            #pragma multi_compile_instancing

            #include "Packages/com.unity.render-pipelines.universal/Shaders/LitInput.hlsl"
            #include "Packages/com.unity.render-pipelines.universal/Shaders/DepthOnlyPass.hlsl"
            ENDHLSL
        }

        // This pass it not used during regular rendering, only for lightmap baking.
        Pass
        {
            Name "Meta"
            Tags{"LightMode" = "Meta"}

            Cull Off

            HLSLPROGRAM
            // Required to compile gles 2.0 with standard srp library
            #pragma prefer_hlslcc gles
            #pragma exclude_renderers d3d11_9x

            #pragma vertex UniversalVertexMeta
            #pragma fragment UniversalFragmentMeta

            #pragma shader_feature _SPECULAR_SETUP
            #pragma shader_feature _EMISSION
            #pragma shader_feature _METALLICSPECGLOSSMAP
            #pragma shader_feature _ALPHATEST_ON
            #pragma shader_feature _ _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A

            #pragma shader_feature _SPECGLOSSMAP

            #include "Packages/com.unity.render-pipelines.universal/Shaders/LitInput.hlsl"
            #include "Packages/com.unity.render-pipelines.universal/Shaders/LitMetaPass.hlsl"

            ENDHLSL
        }
    }
    FallBack "Hidden/Universal Render Pipeline/FallbackError"
    //CustomEditor "PBRShaderGUI"
    CustomEditor "FZDL.ShaderDrawer.LWGUI"
}
