Shader "Custom/Torus"
{
    Properties
    {
        _Color ("Color", Color) = (1, 1, 1, 1)
        _MainTex ("Albedo (RGB)", 2D) = "white" {}
        _TorusMask ("Torus Mask", 2D) = "white" {}
        _Emission ("Emission", Range(0, 1)) = 0.0
        _TilingAndOffset ("Tiling and Offset", Vector) = (1, 1, 0, 0)
        _ScrollSpeed ("Scroll Speed", Range(0, 1)) = 0.1
        _MaskStrength ("Mask Strength", Range(0, 100)) = 5.0
        _GlowIntensity ("Glow Intensity", Range(0, 1)) = 0.1
    }

    SubShader
    {
        Tags 
        { 
            "RenderType" = "Transparent"
            "Queue" = "Transparent"
        }

        Cull Off
        ZWrite Off
        Blend SrcAlpha OneMinusSrcAlpha

        Pass
        {
            Tags 
            { 
                "RenderType" = "Transparent"
                "Queue" = "Transparent"
            }

            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma multi_compile_instancing
            #include "UnityCG.cginc"

            struct appdata_t
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
                UNITY_VERTEX_INPUT_INSTANCE_ID
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
                UNITY_VERTEX_INPUT_INSTANCE_ID
            };

            sampler2D _MainTex;
            sampler2D _TorusMask;
            float4 _Color;
            float _Emission;
            float4 _TilingAndOffset;
            float _MaskStrength;
            float _GlowIntensity;

            UNITY_INSTANCING_BUFFER_START(Props)
            float _ScrollSpeed;
            UNITY_INSTANCING_BUFFER_END(Props)

            v2f vert (appdata_t v)
            {
                v2f o;

                UNITY_SETUP_INSTANCE_ID(v);
                o.vertex = UnityObjectToClipPos(v.vertex);
                UNITY_TRANSFER_INSTANCE_ID(v, o);

                // Apply scrolling offset based on time
                // Implement scrolling direction later?
                float2 scrollOffset = _Time.y * UNITY_ACCESS_INSTANCED_PROP(Props, _ScrollSpeed);
                o.uv = (v.uv * _TilingAndOffset.xy + _TilingAndOffset.zw) + scrollOffset;

                return o;
            }

            half4 frag (v2f i) : SV_Target
            {
                half4 maskColor = tex2D(_TorusMask, i.uv);
                half4 texColor = tex2D(_MainTex, i.uv);

                // Calculate the transparency based on the mask texture and Mask Strength
                half transparency = maskColor.r * _MaskStrength;

                half3 albedo = texColor.rgb * _Color.rgb;
                half3 emission = texColor.rgb * _Emission;

                // Apply the transparency to the alpha channel
                texColor.a = transparency;

                // Add the glow effect
                albedo += emission * _GlowIntensity;

                half3 finalColor = albedo;
                return half4(finalColor, texColor.a);
            }
            ENDCG
        }
    }
}