uniform vec2 iResolution;
uniform float iTime;

uniform sampler2D gm_BaseTexture;

uniform bool gm_PS_FogEnabled;
uniform vec4 gm_FogColour;
uniform bool gm_AlphaTestEnabled;
uniform float gm_AlphaRefValue;

void DoAlphaTest(vec4 SrcColour)
{
	if (gm_AlphaTestEnabled)
	{
		if (SrcColour.a <= gm_AlphaRefValue)
		{
			discard;
		}
	}
}

void DoFog(inout vec4 SrcColour, float fogval)
{
	if (gm_PS_FogEnabled)
	{
		SrcColour = mix(SrcColour, gm_FogColour, clamp(fogval, 0.0, 1.0)); 
	}
}

#define _YY_GLSL_ 1
varying vec2 fragCoord; 



void pMod1(inout float p, float size) {
	float halfsize = size*0.5;
	p = mod(p + halfsize, size) - halfsize;
}

void pMod3(inout vec3 p, vec3 size) {
    
	p = mod(p - size*0.5, size) - size*0.5;
}

float sphere(vec3 p, float radius){
    return length(p)-radius;

}


float map(vec3 p)
{
    vec3 q = p;
	vec3 qa = p;
    pMod3(q, vec3(0.8, 1., 0.23));
	pMod3(qa, vec3(0.8, 1., 0.18));
    pMod1(p.x, 1.);
    
    float s1 = sphere(p, 0.75); 
    float s2 = sphere(q, 0.5);  	    	
    float s3 = sphere(qa, 0.555);
    
  	float df1 = min(min(s1, s2),s3); // Union

    return df1;
}


float trace(vec3 origin, vec3 r) 
{
  float t = 0.0;
    for (int i = 0; i < 64; ++i) {
        vec3 p = origin + r * t;
        float d = map(p);
        t += d*0.22;
        }
    return t;
}


void main()
{
    vec2 fragCoord = openfl_TextureCoordv * iResolution;

    vec2 uv = fragCoord/iResolution.xy;
    vec3 color = vec3(0.324, 0.12, 0.536);
    uv = uv *2.-1.; // Remap the space to -1. to 1.
    uv.x *= iResolution.x/iResolution.y;
    
    	
   	float FOV = 1.25;
   	vec3 ray = normalize(vec3(uv, FOV));
    
    
    vec3 origin = vec3(iTime, 0.0, -1.75);
    float t = trace(origin, ray);
    
    float expFog = 0.5 / (t*t* 0.95);
    
    vec3 fc = vec3(expFog);
    
    if ( t < -0.9 ) {
        color = 0.25-(0.5/t+color);
        vec3 maincolor = exp(-(fc+color)*3.6);
        color = (1.0 - maincolor);
        color = cos(color) * maincolor;
    }
    else
        color = vec3(0);
    
    gl_FragColor = vec4(color,dot(color, vec3(0.5)) * exp(-expFog));
}
