Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- float map(vec3 pos)
- {
- vec3 q = pos;
- float d = length(pos) - 0.25;
- float d2 = pos.y - (-0.25);
- return min(d, d2);
- }
- float castRay(vec3 ro, vec3 rd)
- {
- float t = 0.0;
- for(int i = 0; i < 100; i++)
- {
- vec3 pos = ro + t * rd;
- float h = map(pos);
- if(h < 0.001)
- {
- break;
- }
- t += h;
- if(t > 20.0) break;
- }
- if(t > 20.0) t = -1.0;
- return t;
- }
- vec3 calcNormal(vec3 pos)
- {
- vec2 e = vec2(0.0001, 0.0);
- return normalize(vec3(
- map(pos+e.xyy) - map(pos-e.xyy),
- map(pos+e.yxy) - map(pos-e.yxy),
- map(pos+e.yyx) - map(pos-e.yyx)
- ));
- }
- void mainImage( out vec4 fragColor, in vec2 fragCoord )
- {
- // Normalized pixel coordinates (from 0 to 1)
- vec2 p = (2.0 * fragCoord- iResolution.xy) / iResolution.y;
- /*
- vec3 ro = vec3(0.0,0.0,1.0);
- vec3 rd = normalize(vec3(p, -1.5));
- */
- vec3 col = vec3(0.0);
- // Camera Look at
- float an = iTime;
- vec3 ro = vec3(1. * sin(an), 0.0, 1.0 * cos(an));
- float cameraZoom = 2.5;
- vec3 target = vec3(0.1, 0., 0.);
- vec3 ww = normalize(target - ro);
- vec3 uu = normalize(cross(ww, vec3(0.,1.,0.)));
- vec3 vv = normalize(cross(uu, ww));
- vec3 rd = normalize(p.x * uu + p.y * vv + cameraZoom * ww);
- // Rendering part
- float t = castRay(ro, rd);
- if(t > 0.)
- {
- vec3 pos = ro + t * rd;
- vec3 nor = calcNormal(pos);
- vec3 sundir = normalize(vec3(0.8, 0.4, 0.2));
- float sundif = clamp(dot(nor, sundir), 0.0, 1.0);
- float sun_sha = smoothstep(castRay(pos + nor * 0.001, sundir), 0.0, 1.0);
- float sky_dif = clamp(dot(nor,vec3(0.0,1.0,0.0)), 0.0, 1.0);;
- col = vec3(1.0, 0.7, 0.5) * sundif * sun_sha;
- col += vec3(0.0, 0.2, 0.4) * sky_dif;
- }
- // Output to screen
- fragColor = vec4(col,1.0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement