Skip to content

Instantly share code, notes, and snippets.

@0b5vr
Created November 13, 2014 13:07
Show Gist options
  • Save 0b5vr/5aa8fffdc60c1ccdc877 to your computer and use it in GitHub Desktop.
Save 0b5vr/5aa8fffdc60c1ccdc877 to your computer and use it in GitHub Desktop.
Simple ray marching
precision mediump float;
uniform float t; // time
uniform vec2 r; // resolution
vec2 v=vec2(0.,1.);
float f(vec3 p)
{
return length(p)-.6;
}
void main(void)
{
vec3
uv=vec3((gl_FragCoord.xy-r/2.)/r.x,1.),
eye=v.xxy*3.,
cen=v.xxx,
dir=normalize(vec3(cen-eye)),
air=v.xyx,
sid=normalize(cross(dir,air)),
top=normalize(cross(sid,dir)),
ray=uv.x*sid+uv.y*top+dir,
p=eye,d;
for(int c=0;c<8;c++)
{
d.x=f(p);
d.y+=d.x;
p+=ray*d.x;
}
if(d.x<1E-1)
{
vec3 col=vec3(ray.x*.8,ray.y*.8,ray.x*.4+ray.y*.4)*4.0;
gl_FragColor = vec4(col,1.);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment