Shader "CrispyPin/JuliaFractalDistorted"
{
	Properties
	{
		[HideInInspector]
		_MainTex ("Albedo (RGB)", 2D) = "white" {}
		_MaxIter ("Max iterations", int) = 100
		_RotSpeed ("Rotation speed", float) = 0.2
		_RadiusX ("Rotation radius X", Range(0, 2)) = 1
		_RadiusY ("Rotation radius Y", Range(0, 2)) = 1
		_Emission ("Emission", Range(0, 1)) = 0.2
	}
	SubShader
	{
		Tags { "RenderType"="Opaque" }
		Cull off
		LOD 200

		CGPROGRAM
		// Physically based Standard lighting model, and enable shadows on all light types
		#pragma surface surf Standard fullforwardshadows

		// Use shader model 3.0 target, to get nicer looking lighting
		#pragma target 3.0

		sampler2D _MainTex;

		struct Input
		{
			float2 uv_MainTex;
			float3 worldPos;
		};

		half _RadiusX;
		half _RadiusY;
		half _Emission;
		float _RotSpeed;
		int _MaxIter;

		// // Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
		// // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
		// // #pragma instancing_options assumeuniformscaling
		// UNITY_INSTANCING_BUFFER_START(Props)
		// 	// put more per-instance properties here
		// UNITY_INSTANCING_BUFFER_END(Props)

		void surf (Input IN, inout SurfaceOutputStandard o)
		{
			float2 pos = IN.uv_MainTex * 4 - 2;
			float angle = _Time.y * _RotSpeed + IN.worldPos.x * IN.worldPos.y;
			float2 offset = float2(sin(angle) * _RadiusX, cos(angle) * _RadiusY);

			int i = 0;
			while (pos.x * pos.x + pos.y * pos.y < 4 && i < _MaxIter)
			{
				float temp_x = pos.x * pos.x - pos.y * pos.y;
				pos.y = 2 * pos.x * pos.y + offset.y;
				pos.x = temp_x + offset.x;
				i++;
			}

			fixed3 col =
				min(
					pow(
						fixed3(5, 2, 11) * i / (float)_MaxIter
					, 2)
				, 1);
			o.Albedo = col;
			o.Emission = col * _Emission;
		}
		ENDCG
	}
	FallBack "Diffuse"
}
