Skip to content

Instantly share code, notes, and snippets.

@CharStiles
Last active March 29, 2021 14:43
Show Gist options
  • Save CharStiles/b65791fdf9a8623c0eba032bd5002ce3 to your computer and use it in GitHub Desktop.
Save CharStiles/b65791fdf9a8623c0eba032bd5002ce3 to your computer and use it in GitHub Desktop.
#ifdef GL_ES
precision mediump float;
#endif
uniform vec2 u_resolution;
uniform vec2 u_mouse;
uniform float u_time;
#define maxDist 4.
vec3 cosPalette( float t )
{
// please play around with these numbers to get a better palette
vec3 brightness = vec3(0.3);
vec3 contrast = vec3(0.5);
vec3 osc = vec3(0.5,1.0,0.0);
vec3 phase = vec3(0.4,0.9,0.2);
return brightness + contrast*cos( 6.28318*(osc*t+phase) );
}
float scene(vec3 pos){
float xWav = sin(pos.x * 10.) * 0.1;
float zWav = sin(pos.z * 10.) * 0.1;
float ground = pos.y + 0.50 + xWav + zWav;
vec3 modSpace = vec3(1,1,1);
pos = mod(pos, modSpace) - 0.5*modSpace;
float sphere = length(pos) - 0.25;
return min(sphere, ground);
}
vec4 march(vec3 rayOrigin, vec3 dir){
vec3 ray = rayOrigin;
float totalDist = 0.;
float dist=0.;
for (int i = 0; i < 32 ; i++){
dist = scene(ray);
totalDist +=dist;
if(dist < 0.01){
vec3 color = cosPalette((ray.z/(ray.y+1.0))/200.0);
return vec4( 1. - (totalDist/maxDist)) * vec4(color,1);
}
ray += dist*dir;
}
return vec4(0);
}
// main is a reserved function that is going to be called first
void main(void)
{
vec2 normCoord = gl_FragCoord.xy/u_resolution;
float time = u_time/5.0; //slow down time
vec2 uv = -1. + 2. * normCoord;
uv.x *= u_resolution.x/u_resolution.y;
vec3 camPos = vec3(0,(sin(time)+1.0),u_time/20.0);
vec3 rayOrigin = camPos + vec3(uv,-0.5);
vec3 dir = normalize( rayOrigin - camPos); // direction the ray will travel
vec4 color = march(rayOrigin, dir);
gl_FragColor = color;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment