Shader "Custom/EdgeDetectionShader"
{
    Properties{
        _MainTex("Texture", 2D) = "white" {}
        _EdgeColor("Edge Color", Color) = (1,0,0,1)
        _EdgeThreshold("Edge Threshold", Range(0.01, 0.5)) = 0.1
    }
        SubShader{
            Pass {
                CGPROGRAM
                #pragma vertex vert
                #pragma fragment frag
                #include "UnityCG.cginc"

                struct appdata {
                    float4 vertex : POSITION;
                    float2 uv : TEXCOORD0;
                };

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

                sampler2D _OriginalTex;
                sampler2D _MainTex;
                float4 _MainTex_ST;
                float4 _MainTex_TexelSize;
                float4 _EdgeColor;
                float _EdgeThreshold;

                v2f vert(appdata v) {
                    v2f o;
                    o.vertex = UnityObjectToClipPos(v.vertex);
                    o.uv = TRANSFORM_TEX(v.uv, _MainTex);
                    return o;
                }

                fixed4 frag(v2f i) : SV_Target{
                    // Get the UV coordinates for the current pixel
                    float2 uv = i.uv;

                    // Get the color of the current pixel
                    fixed4 col = tex2D(_MainTex, uv);

                    // Get the size of a single texel. This will be used to get neighboring pixels in UV space
                    float2 texelSize = _MainTex_TexelSize.xy;

                    // Define the sobel operator for x and y directions
                    float sobelX[9] = {
                        -1, 0, 1,
                        -2, 0, 2,
                        -1, 0, 1
                    };

                    float sobelY[9] = {
                        -1, -2, -1,
                        0, 0, 0,
                        1, 2, 1
                    };

                    // These will accumulate the results of applying the sobel operator
                    float3 gx = 0;
                    float3 gy = 0;

                    int index = 0;

                    // Loop over all neighboring pixels in a 3x3 grid
                    for (int y = -1; y <= 1; y++)
                    {
                        for (int x = -1; x <= 1; x++)
                        {
                            // Get the color of the neighboring pixel
                            fixed4 neighborColor = tex2D(_MainTex, uv + float2(x, y) * texelSize);

                            // Use the color components of the neighboring pixel
                            float3 colorComponents = neighborColor.rgb;

                            // Apply the sobel operator to each color component. The color component of the pixel is weighted by the corresponding
                            // value in the sobel array. This will effectively highlight edges and transitions
                            gx += colorComponents * sobelX[index];
                            gy += colorComponents * sobelY[index];

                            // Move to the next index in the sobel arrays
                            index++;
                        }
                    }

                    // Calculate the total edge strength by combining the x and y results for each color component.
                    // The maximum value across the color components is used.
                    float3 edgeDetectionComponents = sqrt(gx * gx + gy * gy);
                    float edgeDetection = max(max(edgeDetectionComponents.r, edgeDetectionComponents.g), edgeDetectionComponents.b);

                    // If the edge strength exceeds a threshold, return the edge color. Otherwise, return the original
                    // pixel color. This will effectively highlight edges in the image with the chosen color
                    fixed4 originalCol = tex2D(_OriginalTex, uv);

                    if (edgeDetection > _EdgeThreshold) {
                        return _EdgeColor;
                    }
                    else {
                        return float4(0,0,0,0); // Completely transparent pixel
                    }
                }
                ENDCG
            }
        }
}