Shader "Unlit/Earth_EarthShader"
{
    Properties
    {
        _TextureNW ("Texture NW", 2D) = "white" {}
        _TextureSW ("Texture SW", 2D) = "white" {}
        _TextureNE ("Texture NE", 2D) = "white" {}
        _TextureSE ("Texture SE", 2D) = "white" {}
        _Resolution ("Resolution", Vector) = (0, 0, 0, 0)
        _Smoothing ("Smoothing", Range(0, 0.25)) = 0
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 100

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            #define PI 3.141592653589793

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

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

            sampler2D _TextureNW;
            sampler2D _TextureSW;
            sampler2D _TextureNE;
            sampler2D _TextureSE;

            float4 _TextureNW_ST;
            float4 _TextureSW_ST;
            float4 _TextureNE_ST;
            float4 _TextureSE_ST;

            float2 _Resolution;
            float _Smoothing;

            float2 sphereProjection(float3 position) 
            {
                float longitude = atan2(position.z, position.x);
                float latitude = asin(position.y);
            
                float u = (longitude + PI) / (2.0 * PI);
                float v = (latitude + PI / 2.0) / PI;

                return float2(u, v);
            }

            v2f vert (appdata v)
            {
                v2f OUT;
                OUT.vertex = UnityObjectToClipPos(v.vertex);
                OUT.uv = sphereProjection(v.vertex.xyz);

                return OUT;
            }

            fixed4 sample(float2 uv) {
                if (uv.x < 0.5) {
                    return (uv.y < 0.5) 
                        ? tex2D(_TextureSW, uv * float2(2, 2)) 
                        : tex2D(_TextureNW, uv * float2(2, 2) - float2(0, 1));
                }
                
                return (uv.y < 0.5) 
                    ? tex2D(_TextureSE, uv * float2(2, 2) - float2(1, 0)) 
                    : tex2D(_TextureNE, uv * float2(2, 2) - float2(1, 1));
            }

            fixed4 frag (v2f IN) : SV_Target
            {
                return float4(IN.uv.x, IN.uv.y, 0, 1);
                // return float4((IN.uv.x % (1.0 / 36)) * 36, (IN.uv.y % (1.0 / 18)) * 18, 0, 1);

                float deltaX = 1 / _Resolution.x;
                float deltaY = 1 / _Resolution.y;

                float4 colorO = sample(IN.uv) * (1 - _Smoothing * 4);
                float4 colorN = sample(float2(IN.uv.x, IN.uv.y + deltaY)) * _Smoothing;
                float4 colorS = sample(float2(IN.uv.x, IN.uv.y - deltaY)) * _Smoothing;
                float4 colorW = sample(float2(IN.uv.x - deltaX, IN.uv.y)) * _Smoothing;
                float4 colorE = sample(float2(IN.uv.x + deltaX, IN.uv.y)) * _Smoothing;

                return colorO + colorN + colorS + colorW + colorE;
            }
            ENDCG
        }
    }
}