Skip to content

Instantly share code, notes, and snippets.

@mharju
Last active October 14, 2017 06:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mharju/38ea61b1a6d1d897eb53fb85ab83e5bf to your computer and use it in GitHub Desktop.
Save mharju/38ea61b1a6d1d897eb53fb85ab83e5bf to your computer and use it in GitHub Desktop.
float udBox( vec3 p, vec3 b ) {
return length(max(abs(p)-b,0.0));
}
float sdTorus( vec3 p, vec2 t )
{
vec2 q = vec2(length(p.xz)-t.x,p.y);
return length(q)-t.y;
}
vec3 rot(vec3 s, float th)
{
return mat3(1, 0, 0,
0, cos(th), -sin(th),
0, sin(th), cos(th)
) * s;
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
float t = -iTime;
vec3 eye = vec3(3. * cos(t), 0, 3. * sin(t));
vec3 up = vec3(0, 1, 0);
vec3 right = vec3(-sin(t), 0, cos(t));
float u = fragCoord.x * 2.0 / iResolution.x - 1.0;
float v = fragCoord.y * 2.0 / iResolution.x - 0.5;
vec3 forward = eye - right * u + up * v;
float f = length(forward);
vec3 ro = eye + forward * f;
vec3 rd = normalize(cross(right, up));
vec4 color = vec4(0.0); // Sky color
float ct = 0.0;
const int maxSteps = 32;
for(int i = 0; i < maxSteps; ++i)
{
vec3 p = ro + rd * ct;
float ds = length(p - 1.0 /*+ 0.5 * sin(iTime)*/) - 0.5;
p = rot(p, 0.5);
float dt = sdTorus(p, vec2(0.5));
float d = min(ds, dt);
if(d < 1e-5)
{
color = vec4(1.0 - float(i) / float(maxSteps));
break;
}
ct += d;
}
fragColor = color;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment