Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define rot(a) mat2(cos(a),sin(a),-sin(a),cos(a))
- float orbit;
- float map (vec3 p)
- {
- p.yz *= rot(iTime * 0.02);
- p.xz *= rot(iTime * 0.05);
- p = abs(p) - 3.0;
- if (p.x < p.z)
- p.xz = p.zx;
- if (p.y < p.z)
- p.yz = p.zy;
- if (p.x<p.y)
- p.xy = p.yx;
- float s = 3.0;
- vec3 off = p * 0.3;
- for (int i = 0; i < 3; i++)
- {
- p = 1.0 - abs(p - 0.5);
- float k = -1.01 * max(1.38 / dot(p, p), 1.38);
- s *= abs(k);
- p *= k;
- p += off;
- p.zx *= rot(-1.01);
- }
- orbit = log(s);
- float a = 0.2;
- p -= clamp(p, -a, a);
- return length(p)/s;
- }
- vec3 calcNormal(vec3 pos)
- {
- vec2 e = vec2(1, -1) * 0.02;
- return 1.5 * normalize(
- e.xyy * map(pos + 1.1 * e.xyy) + e.yyx * map(pos + 0.88 * e.yyx) +
- e.yxy * map(pos + 0.9 * e.yxy) + e.xxx * map(pos + 1.08 * e.xxx)
- );
- }
- float march(vec3 ro, vec3 rd, float near, float far)
- {
- float t = near,d;
- for(int i = 0; i < 25; i++)
- {
- t += d = map(ro + rd * t);
- if (d < 0.05) return t;
- if (t >= far) return far;
- }
- return far;
- }
- float calcShadow(vec3 light, vec3 ld, float len)
- {
- float depth = march(light, ld, 0.0, len);
- return step(len - depth, 0.1);
- }
- void mainImage( out vec4 fragColor, in vec2 fragCoord )
- {
- vec2 uv = (fragCoord * 2.0 - iResolution.xy) / iResolution.y;
- vec3 ro = vec3(0, 0, 9.0 + sin(0.3 * iTime + 0.4 * sin(0.2 * iTime)));
- vec3 rd = normalize(vec3(uv, -2.0));
- vec3 col = vec3(0);
- const float maxd = 50.0;
- float t = march(ro, rd, 0.0, maxd);
- if(t < maxd)
- {
- vec3 p = ro + rd * t;
- col = 2.*clamp(mix(vec3(1),
- 2.0 * (cos(vec3(2, 5, 12) + orbit * 2.0 + p * 0.3) * 0.5 + 0.5),
- 0.7), 0.0, 1.0);
- vec3 n = calcNormal(p);
- vec3 lightPos = vec3(10);
- vec3 li = lightPos - p;
- float len = length(li);
- li /= len;
- float dif = clamp(dot(n, li), 0.5, 1.0);
- float sha = calcShadow(lightPos, -li, len);
- col *= max(sha * dif, 0.4);
- float rimd = pow(clamp(1.0 - dot(reflect(-li, n), -rd), 0.0, 1.0), 2.5);
- float frn = rimd + 2.2 * (1.0 - rimd);
- col *= frn * 0.7;
- col *= max(0.5 + 0.5 * n.y, 0.0);
- col *= exp2(-2.0 * pow(max(0.0, 1.0 - map(p + n * 0.3)/0.3), 2.0));
- col += vec3(0.5, 0.4, 0.9) * pow(clamp(dot(reflect(rd, n), li), 0.0, 1.0), 10.0);
- col = mix(vec3(0), col, exp(-t * t * 0.001));
- }
- fragColor.xyz = clamp(col,0.,1.);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement