Skip to content

Instantly share code, notes, and snippets.

@CharStiles
Created May 30, 2020 20:22
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 CharStiles/c77d466cec2bc5185d9744a7cac508fc to your computer and use it in GitHub Desktop.
Save CharStiles/c77d466cec2bc5185d9744a7cac508fc to your computer and use it in GitHub Desktop.
const int steps = 16;
const float smallNumber = 0.001;
const float maxDist = 2.;
// Repeat in three dimensions
vec3 pMod3(inout vec3 p, vec3 size) {
vec3 c = floor((p + size*0.5)/size);
p = mod(p + size*0.5, size) - size*0.5;
return c;
}
float scene(vec3 position){
float ground = position.y +
sin(position.x * 10.)/10.+
cos(position.z * 10.)/10.+
0.5;
vec3 modSpace = vec3(4,1,2);
pMod3(position, modSpace);
float sphere = length(
vec3(position.x + sin(time),
position.y,
position.z + 1.0
))- 0.5;
return min( ground, sphere);
}
vec4 march(vec3 rayOrigin, vec3 direction){
vec3 ray = rayOrigin;
float dist = 0.;
float totalDist = 0.;
for(float i = 0. ; i < 1.0 ; i += 0.00001){
dist = scene(ray);
ray = ray + dist * direction;
totalDist+= dist;
if(dist < smallNumber){
return vec4(1.0 - (totalDist/maxDist));
}
if(totalDist > maxDist){
break;
}
}
return vec4(0.);
}
void main () {
vec2 pos = uv();
vec3 camOrigin = vec3(0,0,-1);
vec3 rayOrigin = vec3(camOrigin.xy + pos,camOrigin.z + 1.0);
vec3 direction = normalize(camOrigin + rayOrigin);
vec4 color = march(rayOrigin, direction);
gl_FragColor = clamp(color,0.,0.75);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment