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 Standard fullforwardshadows
        #pragma target 3.0

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

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

        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;
        }
        ENDCG
    }

    FallBack "VertexLit"
}