Shader "VrchatHoldem/DualAlbedoMask"
{
    Properties
    {
        [Header(Dual Albedo System)]
        _MainTex("Albedo 1", 2D) = "white" {}
        _Color1("Color 1 Tint", Color) = (1,1,1,1)
        _MainTex2("Albedo 2", 2D) = "white" {}
        _Color2("Color 2 Tint", Color) = (1,1,1,1)
        _MaskTex("Mask (R=blend)", 2D) = "grey" {}

        [Header(PBR Properties)]
        _MetalnessMap("Metalness Map (RGB=Metallic, A=Smoothness)", 2D) = "white" {}
        _Metallic("Metallic Scale", Range(0, 1)) = 1
        _Glossiness("Smoothness Scale", Range(0, 1)) = 1
        _OcclusionTex("Occlusion Map (R=occlusion)", 2D) = "white" {}
        _OcclusionStrength("Occlusion Strength", Range(0, 1)) = 1

        [Header(Rendering)]
        [Enum(UnityEngine.Rendering.CullMode)]_CullMode("Cull Mode", Int) = 2
    }

    SubShader
    {
        Tags { "RenderType" = "Opaque" }
        LOD 200
        Cull [_CullMode]

        CGPROGRAM
        #pragma surface surf FilamentedStandard fullforwardshadows
        #pragma target 3.0

        #include "UnityPBSLighting.cginc"

        sampler2D _MainTex;
        sampler2D _MainTex2;
        sampler2D _MaskTex;
        sampler2D _MetalnessMap;
        sampler2D _OcclusionTex;

        fixed4 _Color1;
        fixed4 _Color2;
        half _Metallic;
        half _Glossiness;
        half _OcclusionStrength;

        half D_GGX_Filament(half roughness, half NoH) {
            half a2 = roughness * roughness;
            half f = (NoH * a2 - NoH) * NoH + 1.0;
            return a2 / (UNITY_PI * f * f);
        }

        half V_SmithGGXCorrelated_Fast(half roughness, half NoV, half NoL) {
            return 0.5 / lerp(2.0 * NoL * NoV, NoL + NoV, roughness);
        }

        half3 F_Schlick_Filament(half3 f0, half3 f90, half VoH) {
            return f0 + (f90 - f0) * Pow5(1.0 - VoH);
        }

        half Fd_Burley(half roughness, half NoV, half NoL, half LoH) {
            half f90 = 0.5 + 2.0 * roughness * LoH * LoH;
            half lightScatter = 1.0 + (f90 - 1.0) * Pow5(1.0 - NoL);
            half viewScatter  = 1.0 + (f90 - 1.0) * Pow5(1.0 - NoV);
            return lightScatter * viewScatter;
        }

        struct Input
        {
            float2 uv_MainTex;
        };

        void surf(Input IN, inout SurfaceOutputStandard o)
        {
            // Sample both albedo textures
            fixed4 albedo1 = tex2D(_MainTex, IN.uv_MainTex) * _Color1;
            fixed4 albedo2 = tex2D(_MainTex2, IN.uv_MainTex) * _Color2;

            // Sample mask texture - use red channel for blending
            // R = 0: show albedo1, R = 1: show albedo2
            fixed mask = 1 - tex2D(_MaskTex, IN.uv_MainTex).r;

            // Blend between the two albedos
            fixed4 baseColor = lerp(albedo1, albedo2, mask);

            // Sample metalness map: RGB = metallic, Alpha = smoothness
            fixed4 metalMap = tex2D(_MetalnessMap, IN.uv_MainTex);
            half metallic = metalMap.rgb * _Metallic;
            half smoothness = metalMap.a * _Glossiness;

            // Sample occlusion map (red channel)
            half occlusion = lerp(1, tex2D(_OcclusionTex, IN.uv_MainTex).r, _OcclusionStrength);

            // Output
            o.Albedo = baseColor.rgb;
            o.Alpha = baseColor.a;
            o.Metallic = metallic;
            o.Smoothness = smoothness;
            o.Occlusion = occlusion;
        }

        inline fixed4 LightingFilamentedStandard(SurfaceOutputStandard s, fixed3 viewDir, UnityGI gi)
        {
            UnityLight light = gi.light;

            half3 diffColor = s.Albedo * (1.0 - s.Metallic);
            half3 f0 = lerp(0.04, s.Albedo, s.Metallic);
            half perceptualRoughness = 1.0 - s.Smoothness;

            half3 h = normalize(light.dir + viewDir);
            half NoL = saturate(dot(s.Normal, light.dir));
            half NoV = abs(dot(s.Normal, viewDir)) + 1e-5;
            half NoH = saturate(dot(s.Normal, h));
            half LoH = saturate(dot(light.dir, h));

            half D = D_GGX_Filament(perceptualRoughness, NoH);
            half V = V_SmithGGXCorrelated_Fast(perceptualRoughness, NoV, NoL);
            half3 f90 = saturate(16.33 * f0);
            half3 F = F_Schlick_Filament(f0, f90, LoH);

            half3 specBRDF = D * V * F;
            half3 diffBRDF = diffColor * Fd_Burley(perceptualRoughness, NoV, NoL, LoH);

            half3 energyComp = 1.0 + f0 * (perceptualRoughness * perceptualRoughness);

            // Unity intensity match — multiply by PI for both lobes
            specBRDF *= UNITY_PI;
            diffBRDF *= UNITY_PI;

            half3 direct = (diffBRDF + specBRDF * energyComp) * light.color * NoL;

            half surfaceReduction = 1.0 / (perceptualRoughness * perceptualRoughness + 1.0);
            half3 indirect = diffColor * gi.indirect.diffuse;
            indirect += surfaceReduction * gi.indirect.specular * lerp(f0, f90, Pow5(1.0 - NoV));

            half3 col = direct * s.Occlusion + indirect;

            return fixed4(col, s.Alpha);
        }

        inline void LightingFilamentedStandard_GI(SurfaceOutputStandard s, UnityGIInput data, inout UnityGI gi)
        {
            LightingStandard_GI(s, data, gi);
        }
        ENDCG
    }

    FallBack "VertexLit"
}