Created
October 1, 2018 21:45
-
-
Save leinonen/3d24aa9a3bbdbd3823400b1249aab0cd to your computer and use it in GitHub Desktop.
small raymarcher. nothing fancy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifdef GL_ES | |
precision mediump float; | |
#endif | |
uniform float time; | |
uniform vec2 resolution; | |
#define PI 3.1415926535898 | |
#define EPS 0.01 | |
const float clipFar = 16.0; | |
mat2 rot2( float angle ) { float c = cos( angle ); float s = sin( angle ); return mat2( c, s,-s, c); } | |
float bump(vec3 p) { return sin(p.x*PI*.25)*sin(p.y*PI*.25)*sin(p.z*PI*2.5);} | |
float map(vec3 p) { | |
vec3 p2 = p; | |
p.xy *= rot2( p.z * PI/6. + time*PI/2. ); | |
float b = bump(p*PI/12.)*.5; | |
return length(p.xy + b) - clipFar; | |
} | |
void main( void ) { | |
vec2 uv = (2.0*gl_FragCoord.xy/resolution.xy - 1.0) * vec2(resolution.x/resolution.y, 1.0); | |
vec3 lookAt = vec3(0,0, time/6.); | |
vec3 camPos = lookAt + vec3(0, 0, lookAt.z - 12.0); | |
vec3 lightPos = lookAt + vec3(0, 0, lookAt.z - 0.0); | |
vec3 forward = normalize(camPos); | |
vec3 right = vec3(forward.z, 0., -forward.x ); | |
vec3 up = cross(forward, right); | |
vec3 rd = normalize(forward + uv.x*right + uv.y*up); | |
rd.xy *= rot2(time * PI / 9.); | |
rd.yz *= rot2(time * PI / 18.); | |
float t = 0.0; | |
for (int i = 0 ; i < 120; i++) { | |
float k = map(camPos + rd * t); | |
t += k; | |
// t += k * 0.35; | |
if ((k < 0.0) || (t > clipFar)) { | |
break; | |
} | |
} | |
vec3 final = vec3(0); | |
vec3 p = camPos + rd * t; | |
vec3 normal = normalize(vec3( | |
map(vec3(p.x+EPS,p.y,p.z)) - map(vec3(p.x-EPS,p.y,p.z)), | |
map(vec3(p.x,p.y+EPS,p.z)) - map(vec3(p.x,p.y-EPS,p.z)), | |
map(vec3(p.x,p.y,p.z+EPS)) - map(vec3(p.x,p.y,p.z-EPS)) | |
)); | |
vec3 lightDirection = normalize(lightPos - p); | |
vec3 eyeDirection = normalize(camPos - p); | |
final += 0.05 + 0.95 * max( 0.0, dot(normal, lightDirection) ); | |
final += pow(max( 0.0, dot(reflect(-lightDirection, normal), eyeDirection) ), 112.0); | |
final.rg *= rot2(p.z*PI/6.); | |
final *= 3.; | |
// final *= fract(p); | |
// gl_FragColor = vec4(clamp(mix(final, .2*vec3(.96,.9,.92), t/clipFar), 0.0, 1.0), 1.0); | |
gl_FragColor = vec4(pow(final, vec3(1.0 / .6)) , 1.0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment