-
Notifications
You must be signed in to change notification settings - Fork 0
/
OrbitTrapping.glsl
62 lines (47 loc) · 1.67 KB
/
OrbitTrapping.glsl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
uniform vec2 iResolution;
uniform float scale;
uniform float iterations;
uniform vec2 transl;
uniform dvec2 trap;
uniform float contrast;
uniform vec2 g1;
uniform vec2 g2;
vec2 map(vec2 value, vec2 inMin, vec2 inMax, vec2 outMin, vec2 outMax) {
return outMin + (outMax - outMin) * (value - inMin) / (inMax - inMin);
}
//https://github.com/hughsk/glsl-hsv2rgb/blob/master/index.glsl
vec3 hsv2rgb(vec3 c) {
vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);
return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);
}
dvec2 multComplex(dvec2 p1, dvec2 p2) {
return dvec2(p1.x * p2.x - p1.y * p2.y, p1.x * p2.y + p1.y * p2.x);
}
vec3 iterate(dvec2 c) {
float minPointTrap = 100000000;
float bailout = 2.0;
dvec2 z = vec2(0);
float i = 0.0;
for(; i < iterations; i++) {
z = multComplex(z, z) + c;
if(length(z) > bailout) {
break;
}
double pointTrap = length(z - trap);
minPointTrap = min(minPointTrap, float(pointTrap));
}
float value = i/iterations;
minPointTrap = sqrt(minPointTrap)*contrast*iterations;
vec2 gradient = map(min(vec2(minPointTrap), 1.0), vec2(0.0), vec2(1.0), g1, g2); // map hue and saturation in a gradient between g1 and g2
return hsv2rgb(vec3(gradient, value));
}
void mainImage( out vec4 fragColor, in vec2 fragCoord ) {
dvec2 c = (fragCoord - iResolution.xy/2.0)/iResolution.x;
c *= dvec2(scale);
c += dvec2(transl);
fragColor = vec4(iterate(c), 1.0);
}
void main() { // Call shadertoy main function since this shader was originally written on shadertoy
mainImage(gl_FragColor, gl_FragCoord.xy);
}