Skip to content

Instantly share code, notes, and snippets.

@yomotsu
Created December 3, 2016 19:13
Show Gist options
  • Save yomotsu/33a6cc4aaff1a9ca3a9df6bf85d8c3f1 to your computer and use it in GitHub Desktop.
Save yomotsu/33a6cc4aaff1a9ca3a9df6bf85d8c3f1 to your computer and use it in GitHub Desktop.
ray marching
precision mediump float;
uniform vec2 resolution; // resolution (width, height)
uniform vec2 mouse; // mouse (0.0 ~ 1.0)
uniform float time; // time (1second == 1.0)
uniform sampler2D backbuffer; // previous scene texture
float EPSILON = .001;
mat3 rotY ( float rad ) {
return mat3(
cos( rad ), 0, -sin( rad ),
0, 1, 0,
sin( rad ), 0, cos( rad )
);
}
float udRoundBox ( vec3 p ) {
vec3 b = vec3( 1., .5, .5 );
float r = .1;
return length( max( abs( p ) - b, 0. ) ) - r;
}
float distanceFn ( vec3 p ) {
return udRoundBox( p );
}
void main ( void ) {
vec2 coord = ( gl_FragCoord.xy * 2. - resolution ) / min( resolution.x, resolution.y );
vec3 color = vec3( 0. );
mat3 rot = rotY( time * 4. );
vec3 pos = vec3( 0, 0, -10 ) * rot;
vec3 dir = normalize( vec3( coord, 1. ) * rot );
for ( int i = 0; i < 32; i ++ ) {
float d = distanceFn( pos );
if ( d < 0.1 ) {
color= vec3( 1., 0., 0. );
pos += dir * .1;
}
pos += dir * d;
}
gl_FragColor = vec4( color, 1. );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment