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
//https://iarri.github.io/Shadertoy2GM/

varying vec2 fragCoord; 
#define MAX_MARCHING_STEPS 24

float distScene(vec3 position){
    position = fract(position)*2.-1.;
    float sphere = length(position) - .15;
    return sphere;
}
float trace(vec3 ro, vec3 rd){
	float depth = 0.;
    for(int i = 0;i < MAX_MARCHING_STEPS;i++){
    	vec3 current = ro + depth * rd;
        float t = distScene(current);
        depth += t*.5;
    }
    return depth;
}

void main()
{
    vec2 fragCoord = openfl_TextureCoordv * iResolution;
    vec2 uv = fragCoord/iResolution.xy;
    uv -= 0.5;
    uv.x *= iResolution.x / iResolution.y;
    vec3 ro = vec3( iTime*.2, iTime*.2, iTime*0.1);
    
    float angle = iTime*.001;
    ro.xz *= mat2(cos(angle), -sin(angle), sin(angle), cos(angle));
    vec3 rd = normalize(vec3(uv,1.));
    float t = trace(ro, rd);
    float fog = 1./(1.+t*t*.1);
    gl_FragColor = vec4((vec3(uv.x+1.5,uv.y-1.,1.)*fog),1.0);
}