<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>

<glsl_hacker>
	
  <window name="win3d01" title="MadShaders - Shadertoy/cells" 
          width="800" height="400" 
          gl_version_major="2" gl_version_minor="1" />
          
         
<gpu_program name="shadertoy_prog" >
	<raw_data_vs><![CDATA[	 
void main()
{	
	gl_TexCoord[0] = gl_MultiTexCoord0;
  gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;		
}
  ]]></raw_data_vs>
  
	<raw_data_ps><![CDATA[	 

// https://www.shadertoy.com/view/XsX3R4
  
uniform vec3      iResolution;     // viewport resolution (in pixels)
uniform float     iGlobalTime;     // shader playback time (in seconds)
uniform vec4      iMouse;          // mouse pixel coords. xy: current (if MLB down), zw: click

float time;
vec3  pos2;

vec3 rotate(vec3 r, float v){ return vec3(r.x*cos(v)+r.z*sin(v),r.y,r.z*cos(v)-r.x*sin(v));}

float terrain(vec3 pos){   
	pos2.xz = pos.xz = mod(pos.xz,400.0)-200.0;
	pos.xyz = rotate(pos.xyz,pos.z*0.003+pos.x*0.001+time*0.1);
	pos.xzy  = abs(pos.xyz)-50.0 + sin(time*0.3+1.3)*50.0;
	return 	60.0 + sin(pos.x*0.12+time)*sin(pos.y*0.14+time)*sin(pos.z*0.11+time)*5.0 - length(pos);
}

void main(void){
    time        = iGlobalTime*1.0;
    vec2 uv     = gl_FragCoord.xy/(iResolution.xx*0.5)-vec2(1.0+cos(time*0.3)*0.4,0.9);
    vec3 ray    = normalize(vec3(uv.x,uv.y,0.7));
    vec3 campos = vec3(cos(time*0.32)*100.0,sin(time*0.5)*20.0-300.0,cos(time*0.1)*500.0);
    vec3 pos    = campos;
    
    float test  = 0.0;
    float trans = 0.0;
	for(int i=0;i<60;i++){
        test = terrain(pos); 
        pos += ray*clamp(test,-50.0,-2.0);
        trans += smoothstep(0.0,4.0,abs(test));
    }
	
	float c1 = smoothstep(50.0,20.0,trans);
	float c2 = smoothstep(-0.0,70.0,test);
	float c3 = smoothstep(70.0,45.0,trans);
    vec3 col = c2*vec3(0.9,0.3,0.0) + c3*vec3(0.1,0.4,0.6) + c1;
	gl_FragColor = vec4(col-dot(uv,uv)*0.2,1.0);
}
]]></raw_data_ps>

</gpu_program>


 
	<script name="init_scene" run_mode="INIT" >
		<raw_data><![CDATA[	

app_dir = gh_utils.get_scripting_libs_dir() 		
dofile(app_dir .. "lua/Moon3D_v2.lua")

moon3d.init(2, 1)
moon3d.graphics.vsync(0)

bmfont = moon3d.font.create("trebuchet_20px.fnt", "data/")
bmfont_texture = moon3d.font.getTexture(bmfont)
moon3d.madshaders.setBmFontData(bmfont, bmfont_texture)

winW, winH = moon3d.window.getSize()

quad = moon3d.graphics.newQuad(winW, winH)
shadertoy_prog = moon3d.graphics.getGpuProgram("shadertoy_prog")

moon3d.madshaders.resetBenchmarkData()

		]]></raw_data>
	</script> 

  
	<script name="update_scene" run_mode="FRAME" >
		<raw_data><![CDATA[	

moon3d.startFrame(0, 0, 0, 1)

local global_time = moon3d.getTime()

moon3d.madshaders.updateBenchmarkData(global_time)

moon3d.camera.bindOrtho()

moon3d.graphics.bindGpuProgram(shadertoy_prog)
moon3d.madshaders.updateShadertoyCommonParams(shadertoy_prog, global_time)
moon3d.graphics.draw(quad)

moon3d.madshaders.displayBenchmarkInfoV2("Shadertoy/cells", global_time, 1, 1, 1, 1)

moon3d.endFrame()

		]]></raw_data>
	</script> 
	

	<script name="resize_scene" run_mode="SIZE" >
		<raw_data><![CDATA[	

moon3d.window.resize()
winW, winH = moon3d.window.getSize()
moon3d.graphics.resizeQuad(quad, winW, winH)

		]]></raw_data>
	</script> 
  
</glsl_hacker>
