// //Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'

Shader "Conversion/CubemapToEquirectangularCropped" {

	Properties{
		  _MainTex("Cubemap (RGB)", CUBE) = "" {}
		  _EquiRotation("Value", Float) = 1.0
		  _EquiRotationY("Value", Float) = 1.0
	}

		Subshader{
			Pass {
				ZTest Always Cull Off ZWrite Off
				Fog { Mode off }

				CGPROGRAM
					#pragma vertex vert
					#pragma fragment frag
					#pragma fragmentoption ARB_precision_hint_fastest
		//#pragma fragmentoption ARB_precision_hint_nicest
		#include "UnityCG.cginc"

		#define PI    3.141592653589793
		#define TWOPI 6.283185307179587

		struct v2f {
			float4 pos : POSITION;
			float2 uv : TEXCOORD0;
		};

		samplerCUBE _MainTex;

		float _EquiRotation;
		float _EquiRotationY;

		// Vertex shader
		v2f vert(appdata_img v) {
                v2f o;
                o.pos = UnityObjectToClipPos(v.vertex);

                // Convert from 3D coordinates to spherical coordinates
                float theta = atan2(-v.vertex.x, v.vertex.z);
                float phi = acos(-v.vertex.y / length(v.vertex)); // Note the negative sign for Y
				


                // Spherical Rotation is between (-1, 1), so you can add +0.5 to the shader to rotate it!
                // Map spherical coordinates to UV coordinates for the positive X face
                o.uv = float2((theta / TWOPI) + _EquiRotation, (phi / (PI/3)) + _EquiRotationY);
                return o;
        }


		fixed4 frag(v2f i) : COLOR {
			float3 unit = float3(1.0, 1.0, 0.0);

			// Adjust UV for negative X face with equirectangular distortion
			float phi = i.uv.y * PI/2;
			float theta = (i.uv.x * PI);

			unit.x = sin(phi) * cos(theta); 
			unit.y = -cos(phi);
			unit.z = sin(phi) * sin(theta);



			return texCUBE(_MainTex, unit);
		}



			ENDCG
		}
	}
		Fallback Off
}