11# version 330
22
3+ #define ZERO 0
4+
35// Input vertex attributes (from vertex shader)
46in vec2 fragTexCoord;
57in vec4 fragColor;
@@ -13,10 +15,12 @@ uniform vec3 camPos;
1315uniform vec3 camDir;
1416uniform vec2 screenCenter;
1517
16- #define ZERO 0
18+ // Output fragment color
19+ out vec4 finalColor;
1720
1821// https://learnopengl.com/Advanced-OpenGL/Depth-testing
19- float CalcDepth(in vec3 rd, in float Idist){
22+ float CalcDepth(in vec3 rd, in float Idist)
23+ {
2024 float local_z = dot (normalize (camDir),rd)* Idist;
2125 return (1.0 / (local_z) - 1.0 / 0.01 )/ (1.0 / 1000.0 - 1.0 / 0.01 );
2226}
@@ -26,15 +30,13 @@ float sdHorseshoe(in vec3 p, in vec2 c, in float r, in float le, vec2 w)
2630{
2731 p.x = abs (p.x);
2832 float l = length (p.xy);
29- p.xy = mat2 (- c.x, c.y,
30- c.y, c.x)* p.xy;
31- p.xy = vec2 ((p.y> 0.0 || p.x> 0.0 )? p.x: l* sign (- c.x),
32- (p.x> 0.0 )? p.y: l);
33- p.xy = vec2 (p.x,abs (p.y- r))- vec2 (le,0.0 );
33+ p.xy = mat2 (- c.x, c.y, c.y, c.x)* p.xy;
34+ p.xy = vec2 (((p.y > 0.0 ) || (p.x > 0.0 ))? p.x : l* sign (- c.x), (p.x> 0.0 )? p.y : l);
35+ p.xy = vec2 (p.x, abs (p.y - r)) - vec2 (le, 0.0 );
3436
35- vec2 q = vec2 (length (max (p.xy,0.0 )) + min (0.0 ,max (p.x,p.y)),p.z);
37+ vec2 q = vec2 (length (max (p.xy, 0.0 )) + min (0.0 , max (p.x, p.y)), p.z);
3638 vec2 d = abs (q) - w;
37- return min (max (d.x,d.y),0.0 ) + length (max (d,0.0 ));
39+ return min (max (d.x, d.y), 0.0 ) + length (max (d, 0.0 ));
3840}
3941
4042// r = sphere's radius
@@ -44,17 +46,16 @@ float sdSixWayCutHollowSphere(vec3 p, float r, float h, float t)
4446{
4547 // Six way symetry Transformation
4648 vec3 ap = abs (p);
47- if (ap.x < max (ap.y, ap.z)){
49+ if (ap.x < max (ap.y, ap.z))
50+ {
4851 if (ap.y < ap.z) ap.xz = ap.zx;
4952 else ap.xy = ap.yx;
5053 }
5154
5255 vec2 q = vec2 (length (ap.yz), ap.x);
53-
5456 float w = sqrt (r* r- h* h);
5557
56- return ((h* q.x< w* q.y) ? length (q- vec2 (w,h)) :
57- abs (length (q)- r)) - t;
58+ return ((h* q.x < w* q.y)? length (q - vec2 (w, h)) : abs (length (q) - r)) - t;
5859}
5960
6061// https://iquilezles.org/articles/boxfunctions
@@ -65,44 +66,47 @@ vec2 iBox(in vec3 ro, in vec3 rd, in vec3 rad)
6566 vec3 k = abs (m)* rad;
6667 vec3 t1 = - n - k;
6768 vec3 t2 = - n + k;
68- return vec2 ( max ( max (t1.x, t1.y), t1.z),
69- min (min (t2.x, t2.y), t2.z));
69+
70+ return vec2 ( max ( max (t1.x, t1.y), t1.z), min (min (t2.x, t2.y), t2.z));
7071}
7172
7273vec2 opU(vec2 d1, vec2 d2)
7374{
74- return (d1.x< d2.x) ? d1 : d2;
75+ return (d1.x < d2.x)? d1 : d2;
7576}
7677
77- vec2 map(in vec3 pos){
78- vec2 res = vec2 (sdHorseshoe( pos- vec3 (- 1.0 ,0.08 , 1.0 ), vec2 (cos (1.3 ),sin (1.3 )), 0.2 , 0.3 , vec2 (0.03 ,0.5 )), 11.5 ) ;
79- res = opU(res, vec2 (sdSixWayCutHollowSphere( pos- vec3 (0.0 , 1.0 , 0.0 ), 4.0 , 3.5 , 0.5 ), 4.5 )) ;
78+ vec2 map(in vec3 pos)
79+ {
80+ vec2 res = vec2 (sdHorseshoe(pos - vec3 (- 1.0 , 0.08 , 1.0 ), vec2 (cos (1.3 ), sin (1.3 )), 0.2 , 0.3 , vec2 (0.03 ,0.5 )), 11.5 );
81+ res = opU(res, vec2 (sdSixWayCutHollowSphere(pos- vec3 (0.0 , 1.0 , 0.0 ), 4.0 , 3.5 , 0.5 ), 4.5 ));
82+
8083 return res;
8184}
8285
8386// https://www.shadertoy.com/view/Xds3zN
84- vec2 raycast(in vec3 ro, in vec3 rd){
85- vec2 res = vec2 (- 1.0 ,- 1.0 );
87+ vec2 raycast(in vec3 ro, in vec3 rd)
88+ {
89+ vec2 res = vec2 (- 1.0 , - 1.0 );
8690
8791 float tmin = 1.0 ;
8892 float tmax = 20.0 ;
8993
9094 // raytrace floor plane
9195 float tp1 = (- ro.y)/ rd.y;
92- if (tp1> 0.0 )
96+ if (tp1 > 0.0 )
9397 {
9498 tmax = min (tmax, tp1);
9599 res = vec2 (tp1, 1.0 );
96100 }
97101
98102 float t = tmin;
99- for (int i= 0 ; i< 70 ; i++ )
103+ for (int i = 0 ; i < 70 ; i++ )
100104 {
101- if (t> tmax) break ;
102- vec2 h = map(ro+ rd* t);
103- if (abs (h.x) < (0.0001 * t))
105+ if (t > tmax) break ;
106+ vec2 h = map(ro + rd* t);
107+ if (abs (h.x ) < (0.0001 * t))
104108 {
105- res = vec2 (t,h.y);
109+ res = vec2 (t, h.y);
106110 break ;
107111 }
108112 t += h.x;
@@ -111,28 +115,28 @@ vec2 raycast(in vec3 ro, in vec3 rd){
111115 return res;
112116}
113117
114-
115118// https://iquilezles.org/articles/rmshadows
116119float calcSoftshadow(in vec3 ro, in vec3 rd, in float mint, in float tmax)
117120{
118121 // bounding volume
119- float tp = (0.8 - ro.y)/ rd.y; if (tp> 0.0 ) tmax = min (tmax, tp);
122+ float tp = (0.8 - ro.y)/ rd.y; if (tp > 0.0 ) tmax = min (tmax, tp);
120123
121124 float res = 1.0 ;
122125 float t = mint;
123- for (int i= ZERO; i< 24 ; i++ )
126+ for (int i = ZERO; i < 24 ; i++ )
124127 {
125128 float h = map(ro + rd* t).x;
126- float s = clamp (8.0 * h/ t,0.0 ,1.0 );
129+ float s = clamp (8.0 * h/ t, 0.0 , 1.0 );
127130 res = min (res, s);
128131 t += clamp (h, 0.01 , 0.2 );
129- if (res< 0.004 || t > tmax) break ;
132+ if (( res < 0.004 ) || (t > tmax) ) break ;
130133 }
134+
131135 res = clamp (res, 0.0 , 1.0 );
136+
132137 return res* res* (3.0 - 2.0 * res);
133138}
134139
135-
136140// https://iquilezles.org/articles/normalsSDF
137141vec3 calcNormal(in vec3 pos)
138142{
@@ -148,14 +152,15 @@ float calcAO(in vec3 pos, in vec3 nor)
148152{
149153 float occ = 0.0 ;
150154 float sca = 1.0 ;
151- for (int i= ZERO; i< 5 ; i++ )
155+ for (int i = ZERO; i < 5 ; i++ )
152156 {
153157 float h = 0.01 + 0.12 * float (i)/ 4.0 ;
154158 float d = map(pos + h* nor).x;
155159 occ += (h- d)* sca;
156160 sca *= 0.95 ;
157161 if (occ> 0.35 ) break ;
158162 }
163+
159164 return clamp (1.0 - 3.0 * occ, 0.0 , 1.0 )* (0.5 + 0.5 * nor.y);
160165}
161166
@@ -165,9 +170,9 @@ float checkersGradBox(in vec2 p)
165170 // filter kernel
166171 vec2 w = fwidth (p) + 0.001 ;
167172 // analytical integral (box filter)
168- vec2 i = 2.0 * (abs (fract ((p- 0.5 * w)* 0.5 )- 0.5 )- abs (fract ((p+ 0.5 * w)* 0.5 )- 0.5 ))/ w;
173+ vec2 i = 2.0 * (abs (fract ((p - 0.5 * w)* 0.5 )- 0.5 ) - abs (fract ((p + 0.5 * w)* 0.5 ) - 0.5 ))/ w;
169174 // xor pattern
170- return 0.5 - 0.5 * i.x* i.y;
175+ return ( 0.5 - 0.5 * i.x* i.y) ;
171176}
172177
173178// https://www.shadertoy.com/view/tdS3DG
@@ -180,7 +185,7 @@ vec4 render(in vec3 ro, in vec3 rd)
180185 vec2 res = raycast(ro,rd);
181186 float t = res.x;
182187 float m = res.y;
183- if (m> -0.5 )
188+ if (m > - 0.5 )
184189 {
185190 vec3 pos = ro + t* rd;
186191 vec3 nor = (m< 1.5 ) ? vec3 (0.0 ,1.0 ,0.0 ) : calcNormal(pos);
@@ -190,7 +195,7 @@ vec4 render(in vec3 ro, in vec3 rd)
190195 col = 0.2 + 0.2 * sin (m* 2.0 + vec3 (0.0 ,1.0 ,2.0 ));
191196 float ks = 1.0 ;
192197
193- if (m< 1.5 )
198+ if (m < 1.5 )
194199 {
195200 float f = checkersGradBox(3.0 * pos.xz);
196201 col = 0.15 + f* vec3 (0.05 );
@@ -207,14 +212,14 @@ vec4 render(in vec3 ro, in vec3 rd)
207212 vec3 lig = normalize (vec3 (- 0.5 , 0.4 , - 0.6 ));
208213 vec3 hal = normalize (lig- rd);
209214 float dif = clamp (dot (nor, lig), 0.0 , 1.0 );
210- // if (dif>0.0001)
215+ // if (dif>0.0001)
211216 dif *= calcSoftshadow(pos, lig, 0.02 , 2.5 );
212217 float spe = pow (clamp (dot (nor, hal), 0.0 , 1.0 ),16.0 );
213218 spe *= dif;
214219 spe *= 0.04 + 0.96 * pow (clamp (1.0 - dot (hal,lig),0.0 ,1.0 ),5.0 );
215220 // spe *= 0.04+0.96*pow(clamp(1.0-sqrt(0.5*(1.0-dot(rd,lig))),0.0,1.0),5.0);
216221 lin += col* 2.20 * dif* vec3 (1.30 ,1.00 ,0.70 );
217- lin += 5.00 * spe* vec3 (1.30 ,1.00 ,0.70 )* ks;
222+ lin += 5.00 * spe* vec3 (1.30 ,1.00 ,0.70 )* ks;
218223 }
219224 // sky
220225 {
@@ -249,7 +254,8 @@ vec4 render(in vec3 ro, in vec3 rd)
249254 return vec4 (vec3 (clamp (col,0.0 ,1.0 )),t);
250255}
251256
252- vec3 CalcRayDir(vec2 nCoord){
257+ vec3 CalcRayDir(vec2 nCoord)
258+ {
253259 vec3 horizontal = normalize (cross (camDir,vec3 (.0 , 1.0 , .0 )));
254260 vec3 vertical = normalize (cross (horizontal,camDir));
255261 return normalize (camDir + horizontal* nCoord.x + vertical* nCoord.y);
@@ -279,6 +285,7 @@ void main()
279285 color = res.xyz;
280286 depth = CalcDepth(rd,res.w);
281287 }
282- gl_FragColor = vec4 (color , 1.0 );
288+
289+ finalColor = vec4 (color , 1.0 );
283290 gl_FragDepth = depth;
284291}
0 commit comments