Created
August 1, 2020 20:25
-
-
Save solsarratea/b124d2e8a63546645dd8234f63f64527 to your computer and use it in GitHub Desktop.
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
float smallNumber = 0.01; | |
float scene(vec3 p){ | |
vec3 q = fract(p) *4. -1.; | |
float sphere = length(p)-abs(sin(time*0.1)*2.); | |
float planeDist =p.y +2.; | |
return min(sphere,planeDist); | |
} | |
vec3 estimateNormal(vec3 p) { | |
vec2 e = vec2 (smallNumber, 0.); | |
vec3 n = scene(p)- vec3( | |
scene(p-e.xyy), | |
scene(p-e.yxy), | |
scene(p-e.yyx)); | |
return normalize(n); | |
} | |
const float MD = 30.; | |
float trace(vec3 origin, vec3 r){ | |
vec3 lightDirection = vec3(0.5); | |
float roughness = 3.5; | |
float albedo = .1; | |
float t = 0.; | |
for(int i=0; i < 32; ++i){ | |
vec3 p = origin + r * t; | |
float d = scene(p); | |
t += d; | |
if (t > MD || d < 0.01) break; | |
} | |
return t; | |
} | |
float getLight(vec3 p){ | |
vec3 lightPos = vec3(cos(time/2.)*2.,4., sin(time/2.)*2.); | |
vec3 l = normalize(lightPos-p); | |
vec3 n = estimateNormal(p); | |
float d = trace(p+n*0.2, l); | |
float dif = clamp(dot(n,l),0.,1.); | |
if (d<length(lightPos - p)) dif *= .5; | |
return dif; | |
} | |
void main() | |
{ | |
vec3 pos = vec3(uv(),.8); | |
vec4 color; | |
vec3 camOrigin = vec3(0.,3., -9.); | |
vec3 rayOrigin = normalize(pos); | |
float t = trace(camOrigin, rayOrigin); | |
vec3 p = camOrigin + rayOrigin *t; | |
float dif = getLight(p); | |
color = vec4(dif); | |
gl_FragColor = color; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment