Shader "CrispyPin/Loops"
{
	Properties{}
	SubShader
	{
		Tags { "RenderType"="Opaque" }
		Cull Front
		LOD 100

		Pass
		{
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag

			#define MAX_STEPS 64
			#define MAX_DIST 256
			#define SURF_DIST 0.01

			#define SCENE_FN main
			#define SCENE_SCALE 0.045
			#define DISCARD_ON_MISS

			#include "lib/libgarbage.cginc"

			float flatsin(float t, float period = 2, float transition = 0.5, float bias = 0) {
				// period 2
				// smoothstep(0.25, 0.75, abs(mod(x -0.75, 2) - 1))
				// period 4
				// smoothstep(0.75, 1.25, abs(mod(x -1.25, 4) - 2))
				float start = (period/2-transition)/2;
				return smoothstep(start - bias, start + transition - bias, abs(fmod(t - start - transition - bias, period) - period/2));
			}

			SurfacePoint Loop(float3 p, Material m = DEFAULT_MAT) {
				return qSub(mBox(p, float3(2, 4, 6), m), mBox(p, float3(3, 2, 4)));
			}

			Material lCol(float t) {
				Material mA = mat(1, .5, .2);
				Material mB = mat(.2, .5, 1);
				return mixMat(mA, mB, flatsin(t, 6, 0.5, 1));
			}

			SurfacePoint main(float3 p) {
				SurfacePoint d;

				float t = _Time.y;
				Material mA = mat(1, .5, .2);
				Material mB = mat(.2, .5, 1);
				Material mCenter = mixMat(mA, mB, flatsin(t, 3, 0.5, .25));

				d =          Loop(p    -float3( 0, 0,flatsin(t    )*8 -4),mCenter);
				d = qUnion(d,Loop(p.yzx-float3( 0, 4,flatsin(t+1.5)*8 -4),lCol(t+2.5)));
				d = qUnion(d,Loop(p.zxy-float3( 4, 4,flatsin(t+1  )*8 -4),lCol(t+2)));
				d = qUnion(d,Loop(p    -float3( 4, 4,flatsin(t+1.5)*8   ),lCol(t+1.5)));
				d = qUnion(d,Loop(p.yzx-float3( 4, 0,flatsin(t+1  )*8   ),lCol(t+1)));
				d = qUnion(d,Loop(p.zxy-float3( 0, 0,flatsin(t+0.5)*8   ),lCol(t+0.5)));
				d = qUnion(d,Loop(p.yzx-float3( 0,-4,flatsin(t+1.5)*8 -4),lCol(t-0.5)));
				d = qUnion(d,Loop(p.zxy-float3(-4,-4,flatsin(t+1  )*8 -4),lCol(t-1)));
				d = qUnion(d,Loop(p    -float3(-4,-4,flatsin(t+1.5)*8 -8),lCol(t-1.5)));
				d = qUnion(d,Loop(p.yzx-float3(-4, 0,flatsin(t+1  )*8 -8),lCol(t-2)));
				d = qUnion(d,Loop(p.zxy-float3( 0, 0,flatsin(t+0.5)*8 -8),lCol(t-2.5)));

				return d;
			}
			ENDCG
		}
	}
}
