Skip to content

Instantly share code, notes, and snippets.

@Santarh
Created April 17, 2012 18:20
Show Gist options
  • Save Santarh/2407967 to your computer and use it in GitHub Desktop.
Save Santarh/2407967 to your computer and use it in GitHub Desktop.
Blue Earth
#ifdef GL_ES
precision mediump float;
#endif
uniform float time;
uniform vec2 mouse;
uniform vec2 resolution;
vec3 y_up = vec3( 0.0, 1.0, 0.0 );
vec3 eye_pos = vec3( 0.0, 0.0, -3.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 clamp( dot( normalize(-light_dir), normalize(surface_pos - sphere_pos) ), 0.0, 1.0 );
}
void main( void )
{
// Coord
vec2 uv = 2.0 * ( gl_FragCoord.xy / resolution.xy ) - 1.0;
uv.x *= resolution.x / resolution.y;
// Camera
eye_pos = 3.0 * vec3( cos(time), 0.0, sin(time) );
vec3 eye_vec = normalize( target_pos - eye_pos );
vec3 eye_left = normalize( cross( y_up, eye_vec ) );
vec3 eye_up = normalize( cross( eye_vec, eye_left ) );
// Raymarching
vec3 ray_pos = eye_pos;
vec3 ray_vec = normalize( eye_vec + uv.x * eye_left + uv.y * eye_up );
float delta = 0.5;
for ( int i = 0; i < 50; ++i )
{
float dist = sphere_distance( ray_pos );
ray_pos += delta * dist * ray_vec;
}
// Color
vec3 col = vec3( 0.0 );
if ( sphere_distance( ray_pos ) < delta )
{
float lambert = sphere_lambert( ray_pos );
vec3 blue = vec3( 0.0, 0.70, 0.97 );
col = vec3( blue * lambert );
}
else
{
col = vec3( 0.0 );
}
gl_FragColor = vec4( col, 1.0 );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment