Skip to content

Instantly share code, notes, and snippets.

@Santarh
Created April 17, 2012 19:19
Show Gist options
  • Save Santarh/2408402 to your computer and use it in GitHub Desktop.
Save Santarh/2408402 to your computer and use it in GitHub Desktop.
raymarching!
#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_pos = vec3( 0.0, 1.0, 1.0 );
float sphere( vec3 pos, float rad )
{
return length(pos) - rad;
}
float scene_distance( vec3 pos )
{
float d = sphere( pos, 1.0 );
return d;
}
vec3 scene_normal( vec3 pos )
{
float delta = 0.0001;
vec3 normal;
normal.x = scene_distance( vec3( pos.x + delta, pos.y, pos.z ) ) - scene_distance( vec3( pos.x - delta, pos.y, pos.z ) );
normal.y = scene_distance( vec3( pos.x, pos.y + delta, pos.z ) ) - scene_distance( vec3( pos.x, pos.y - delta, pos.z ) );
normal.z = scene_distance( vec3( pos.x, pos.y, pos.z + delta ) ) - scene_distance( vec3( pos.x, pos.y, pos.z - delta ) );
return normalize( normal );
}
void main( void )
{
// Coord
vec2 uv_raw = 2.0 * ( gl_FragCoord.xy / resolution.xy ) - 1.0;
vec2 uv = uv_raw;
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
bool is_hit = false;
vec3 ray_pos = eye_pos;
vec3 ray_vec = normalize( eye_vec + uv.x * eye_left + uv.y * eye_up );
for ( int i = 0; i < 100; ++i )
{
float d = scene_distance( ray_pos );
if ( d < 0.001 )
{
is_hit = true;
}
ray_pos += d * ray_vec;
}
// Color
vec3 rgb = vec3( 0.0 );
if ( is_hit )
{
vec3 normal = scene_normal( ray_pos );
vec3 blue = vec3( 0.0, 0.7, 0.97 );
rgb = dot( normal, light_pos ) * blue;
}
else
{
rgb = vec3( ( uv_raw.y + 1.0 ) * 0.5 );
}
// vignette
rgb *= smoothstep( 2.0 , 0.5, dot( uv_raw, uv_raw ) );
gl_FragColor = vec4( rgb, 1.0 );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment