Skip to content

Instantly share code, notes, and snippets.

@Santarh
Created April 16, 2012 03:57
Show Gist options
  • Save Santarh/2396279 to your computer and use it in GitHub Desktop.
Save Santarh/2396279 to your computer and use it in GitHub Desktop.
Raymarching - Simple sphere
#ifdef GL_ES
precision mediump float;
#endif
uniform float time;
uniform vec2 mouse;
uniform vec2 resolution;
vec3 eye_pos = vec3( 0.0, 0.0, -5.0 );
vec3 target_pos = vec3( 0.0, 0.0, 0.0 );
vec3 sphere_pos = vec3( 0.0 );
float sphere_rad = 1.0;
vec3 light_dir = vec3( 0.0, -1.0, 1.0 );
float sphere_distance( vec3 now_pos )
{
return distance( sphere_pos, now_pos ) - sphere_rad;
}
float sphere_lambert( vec3 surface_pos )
{
return 0.5 + 0.5 * dot( normalize(-light_dir), normalize(surface_pos - sphere_pos) );
}
void main( void )
{
vec2 uv = gl_FragCoord.xy / resolution.y - 0.5;
vec3 ray_pos = eye_pos;
vec3 ray_vec = normalize( vec3( uv, 1.0 ) );
for ( int i = 0; i < 50; ++i )
{
float d = sphere_distance( ray_pos );
ray_pos += d * ray_vec;
}
gl_FragColor = vec4( vec3( sphere_lambert(ray_pos) ), 1.0 );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment