forked from mrange/glsl-snippets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnippets.glsl
682 lines (583 loc) · 20.5 KB
/
snippets.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
// -----------------------------------------------------------------------------
// Licenses
// CC0 - https://creativecommons.org/share-your-work/public-domain/cc0/
// MIT - https://mit-license.org/
// WTFPL - https://en.wikipedia.org/wiki/WTFPL
// Unknown - No license identified, does not mean public domain
// -----------------------------------------------------------------------------
// License: CC0, author: Mårten Rånge, found: https://github.com/mrange/glsl-snippets
#define TIME time
#define TTIME (TAU*TIME)
#define RESOLUTION resolution
#define PI 3.141592654
#define PI_2 (0.5*PI)
#define PI_4 (0.25*PI)
#define TAU (2.0*PI)
#define ROT(a) mat2(cos(a), sin(a), -sin(a), cos(a))
#define PSIN(x) (0.5+0.5*sin(x))
#define PCOS(x) (0.5+0.5*cos(x))
#define BPM 120.0
#define BTIME_1ST 0.0
#define BTIME(n) (BTIME_1ST+(n)*60.0/BPM)
#define SCA(a) vec2(sin(a), cos(a))
#define DOT2(x) dot(x, x)
// License: CC0, author: Mårten Rånge, found: https://github.com/mrange/glsl-snippets
mat3 rotX(float a) {
float c = cos(a);
float s = sin(a);
return mat3(
1.0 , 0.0 , 0.0
, 0.0 , +c , +s
, 0.0 , -s , +c
);
}
// License: CC0, author: Mårten Rånge, found: https://github.com/mrange/glsl-snippets
mat3 rotY(float a) {
float c = cos(a);
float s = sin(a);
return mat3(
+c , 0.0 , +s
, 0.0 , 1.0 , 0.0
, -s , 0.0 , +c
);
}
// License: CC0, author: Mårten Rånge, found: https://github.com/mrange/glsl-snippets
mat3 rotZ(float a) {
float c = cos(a);
float s = sin(a);
return mat3(
+c , +s , 0.0
, -s , +c , 0.0
, 0.0 , 0.0 , 1.0
);
}
// License: CC0, author: Mårten Rånge, found: https://github.com/mrange/glsl-snippets
float ref(inout vec3 p, vec3 r) {
float d = dot(p, r);
p -= r*min(0.0, d)*2.0;
return d < 0.0 ? 0.0 : 1.0;
}
// License: WTFPL, author: sam hocevar, found: https://stackoverflow.com/a/17897228/418488
const vec4 hsv2rgb_K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
vec3 hsv2rgb(vec3 c) {
vec3 p = abs(fract(c.xxx + hsv2rgb_K.xyz) * 6.0 - hsv2rgb_K.www);
return c.z * mix(hsv2rgb_K.xxx, clamp(p - hsv2rgb_K.xxx, 0.0, 1.0), c.y);
}
// License: WTFPL, author: sam hocevar, found: https://stackoverflow.com/a/17897228/418488
// Macro version of above to enable compile-time constants
#define HSV2RGB(c) (c.z * mix(hsv2rgb_K.xxx, clamp(abs(fract(c.xxx + hsv2rgb_K.xyz) * 6.0 - hsv2rgb_K.www) - hsv2rgb_K.xxx, 0.0, 1.0), c.y))
// License: WTFPL, author: sam hocevar, found: https://stackoverflow.com/a/17897228/418488
vec3 rgb2hsv(vec3 c) {
const vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);
vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g));
vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r));
float d = q.x - min(q.w, q.y);
float e = 1.0e-10;
return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);
}
// License: Unknown, author: nmz (twitter: @stormoid), found: https://www.shadertoy.com/view/NdfyRM
vec3 sRGB(vec3 t) {
return mix(1.055*pow(t, vec3(1./2.4)) - 0.055, 12.92*t, step(t, vec3(0.0031308)));
}
// License: Unknown, author: Matt Taylor (https://github.com/64), found: https://64.github.io/tonemapping/
vec3 aces_approx(vec3 v) {
v = max(v, 0.0);
v *= 0.6f;
float a = 2.51f;
float b = 0.03f;
float c = 2.43f;
float d = 0.59f;
float e = 0.14f;
return clamp((v*(a*v+b))/(v*(c*v+d)+e), 0.0f, 1.0f);
}
// License: Unknown, author: nmz (twitter: @stormoid), found: https://www.shadertoy.com/view/NdfyRM
float getsat(vec3 c) {
float mi = min(min(c.x, c.y), c.z);
float ma = max(max(c.x, c.y), c.z);
return (ma - mi)/(ma+ 1e-7);
}
// License: Unknown, author: nmz (twitter: @stormoid), found: https://www.shadertoy.com/view/NdfyRM
#define DSP_STR 1.5
vec3 rgb_lerp(in vec3 a, in vec3 b, in float x) {
//Interpolated base color (with singularity fix)
vec3 ic = mix(a, b, x) + vec3(1e-6,0.,0.);
//Saturation difference from ideal scenario
float sd = abs(getsat(ic) - mix(getsat(a), getsat(b), x));
//Displacement direction
vec3 dir = normalize(vec3(2.*ic.x - ic.y - ic.z, 2.*ic.y - ic.x - ic.z, 2.*ic.z - ic.y - ic.x));
//Simple Lighntess
float lgt = dot(vec3(1.0), ic);
//Extra scaling factor for the displacement
float ff = dot(dir, normalize(ic));
//Displace the color
ic += DSP_STR*dir*sd*ff*lgt;
return clamp(ic,0.,1.);
}
// License: CC0, author: Mårten Rånge, found: https://github.com/mrange/glsl-snippets
vec3 saturate(in vec3 a) { return clamp(a, 0.0, 1.0); }
vec2 saturate(in vec2 a) { return clamp(a, 0.0, 1.0); }
float saturate(in float a) { return clamp(a, 0.0, 1.0); }
// License: CC0, author: Mårten Rånge, found: https://github.com/mrange/glsl-snippets
float dot2(vec2 x) {
return dot(x, x);
}
// License: MIT, author: Inigo Quilez, found: https://www.iquilezles.org/www/articles/smin/smin.htm
float pmin(float a, float b, float k) {
float h = clamp(0.5+0.5*(b-a)/k, 0.0, 1.0);
return mix(b, a, h) - k*h*(1.0-h);
}
// License: CC0, author: Mårten Rånge, found: https://github.com/mrange/glsl-snippets
float pmax(float a, float b, float k) {
return -pmin(-a, -b, k);
}
// License: CC0, author: Mårten Rånge, found: https://github.com/mrange/glsl-snippets
float pabs(float a, float k) {
return pmax(a, -a, k);
}
// License: CC0, author: Mårten Rånge, found: https://github.com/mrange/glsl-snippets
vec2 toPolar(vec2 p) {
return vec2(length(p), atan(p.y, p.x));
}
// License: CC0, author: Mårten Rånge, found: https://github.com/mrange/glsl-snippets
vec2 toRect(vec2 p) {
return vec2(p.x*cos(p.y), p.x*sin(p.y));
}
// License: CC0, author: Mårten Rånge, found: https://github.com/mrange/glsl-snippets
vec3 toSpherical(vec3 p) {
float r = length(p);
float t = acos(p.z/r);
float ph = atan(p.y, p.x);
return vec3(r, t, ph);
}
// License: CC0, author: Mårten Rånge, found: https://github.com/mrange/glsl-snippets
vec3 toRect(vec3 p) {
float s = sin(p.z);
float x = s*cos(p.y);
float y = s*sin(p.y);
float z = cos(p.z);
return p.x*vec3(x, y, z);
}
// License: MIT OR CC-BY-NC-4.0, author: mercury, found: https://mercury.sexy/hg_sdf/
float mod1(inout float p, float size) {
float halfsize = size*0.5;
float c = floor((p + halfsize)/size);
p = mod(p + halfsize, size) - halfsize;
return c;
}
// License: MIT OR CC-BY-NC-4.0, author: mercury, found: https://mercury.sexy/hg_sdf/
vec2 mod2(inout vec2 p, vec2 size) {
vec2 c = floor((p + size*0.5)/size);
p = mod(p + size*0.5,size) - size*0.5;
return c;
}
// License: MIT OR CC-BY-NC-4.0, author: mercury, found: https://mercury.sexy/hg_sdf/
float modMirror1(inout float p, float size) {
float halfsize = size*0.5;
float c = floor((p + halfsize)/size);
p = mod(p + halfsize,size) - halfsize;
p *= mod(c, 2.0)*2.0 - 1.0;
return c;
}
// License: Unknown, author: Martijn Steinrucken, found: https://www.youtube.com/watch?v=VmrIDyYiJBA
vec2 hextile(inout vec2 p) {
// See Art of Code: Hexagonal Tiling Explained!
// https://www.youtube.com/watch?v=VmrIDyYiJBA
const vec2 sz = vec2(1.0, sqrt(3.0));
const vec2 hsz = 0.5*sz;
vec2 p1 = mod(p, sz)-hsz;
vec2 p2 = mod(p - hsz, sz)-hsz;
vec2 p3 = dot(p1, p1) < dot(p2, p2) ? p1 : p2;
vec2 n = ((p3 - p + hsz)/sz);
p = p3;
n -= vec2(0.5);
// Rounding to make hextile 0,0 well behaved
return round(n*2.0)*0.5;
}
// License: CC0, author: Mårten Rånge, found: https://github.com/mrange/glsl-snippets
float smoothKaleidoscope(inout vec2 p, float sm, float rep) {
vec2 hp = p;
vec2 hpp = toPolar(hp);
float rn = modMirror1(hpp.y, TAU/rep);
float sa = PI/rep - pabs(PI/rep - abs(hpp.y), sm);
hpp.y = sign(hpp.y)*(sa);
hp = toRect(hpp);
p = hp;
return rn;
}
// License: CC0, author: Mårten Rånge, found: https://github.com/mrange/glsl-snippets
float circle(vec2 p, float r) {
return length(p) - r;
}
// License: CC0, author: Mårten Rånge, found: https://github.com/mrange/glsl-snippets
float vesica(vec2 p, vec2 sz) {
if (sz.x < sz.y) {
sz = sz.yx;
} else {
p = p.yx;
}
vec2 sz2 = sz*sz;
float d = (sz2.x-sz2.y)/(2.0*sz.y);
float r = sqrt(sz2.x+d*d);
float b = sz.x;
p = abs(p);
return ((p.y-b)*d>p.x*b) ? length(p-vec2(0.0,b))
: length(p-vec2(-d,0.0))-r;
}
// License: MIT, author: Inigo Quilez, found: https://iquilezles.org/www/articles/distfunctions2d/distfunctions2d.htm
float box(vec2 p, vec2 b) {
vec2 d = abs(p)-b;
return length(max(d,0.0)) + min(max(d.x,d.y),0.0);
}
// License: MIT, author: Inigo Quilez, found: https://iquilezles.org/www/articles/distfunctions2d/distfunctions2d.htm
float isosceles(vec2 p, vec2 q) {
p.x = abs(p.x);
vec2 a = p - q*clamp(dot(p,q)/dot(q,q), 0.0, 1.0);
vec2 b = p - q*vec2(clamp(p.x/q.x, 0.0, 1.0), 1.0);
float s = -sign(q.y);
vec2 d = min(vec2(dot(a,a), s*(p.x*q.y-p.y*q.x)),
vec2(dot(b,b), s*(p.y-q.y)));
return -sqrt(d.x)*sign(d.y);
}
// License: MIT, author: Inigo Quilez, found: https://iquilezles.org/www/articles/distfunctions2d/distfunctions2d.htm
float box(vec2 p, vec2 b) {
vec2 d = abs(p)-b;
return length(max(d,0.0)) + min(max(d.x,d.y),0.0);
}
// License: MIT, author: Inigo Quilez, found: https://iquilezles.org/www/articles/distfunctions2d/distfunctions2d.htm
float hexagon(vec2 p, float r) {
const vec3 k = 0.5*vec3(-sqrt(3.0), 1.0, sqrt(4.0/3.0));
p = abs(p);
p -= 2.0*min(dot(k.xy,p),0.0)*k.xy;
p -= vec2(clamp(p.x, -k.z*r, k.z*r), r);
return length(p)*sign(p.y);
}
// License: MIT, author: Inigo Quilez, found: https://iquilezles.org/www/articles/distfunctions2d/distfunctions2d.htm
float horseshoe(vec2 p, vec2 c, float r, vec2 w) {
p.x = abs(p.x);
float l = length(p);
p = mat2(-c.x, c.y,
c.y, c.x)*p;
p = vec2((p.y>0.0)?p.x:l*sign(-c.x),
(p.x>0.0)?p.y:l);
p = vec2(p.x,abs(p.y-r))-w;
return length(max(p,0.0)) + min(0.0,max(p.x,p.y));
}
// License: MIT, author: Inigo Quilez, found: https://iquilezles.org/www/articles/distfunctions2d/distfunctions2d.htm
float segment(vec2 p, vec2 a, vec2 b) {
vec2 pa = p-a, ba = b-a;
float h = clamp(dot(pa,ba)/dot(ba,ba), 0.0, 1.0);
return length(pa - ba*h);
}
// License: MIT, author: Inigo Quilez, found: https://www.shadertoy.com/view/wsGSD3
float snowflake(vec2 p) {
const vec2 k = vec2(0.5,-sqrt(3.0))/2.0;
p = p.yx;
p = abs(p);
p -= 2.0*min(dot(k,p),0.0)*k;
p = abs(p);
float d = segment(p, vec2(.00, 0), vec2(.75, 0));
d = min(d, segment(p, vec2(.50, 0), vec2(.50, 0) + .10));
d = min(d, segment(p, vec2(.25, 0), vec2(.25, 0) + .15));
return d;
}
// License: MIT, author: Inigo Quilez, found: https://iquilezles.org/www/articles/distfunctions2d/distfunctions2d.htm
float parabola(vec2 pos, float k) {
pos.x = abs(pos.x);
float ik = 1.0/k;
float p = ik*(pos.y - 0.5*ik)/3.0;
float q = 0.25*ik*ik*pos.x;
float h = q*q - p*p*p;
float r = sqrt(abs(h));
float x = (h>0.0) ?
pow(q+r,1.0/3.0) - pow(abs(q-r),1.0/3.0)*sign(r-q) :
2.0*cos(atan(r,q)/3.0)*sqrt(p);
return length(pos-vec2(x,k*x*x)) * sign(pos.x-x);
}
// License: MIT OR CC-BY-NC-4.0, author: mercury, found: https://mercury.sexy/hg_sdf/
float corner(vec2 p) {
vec2 v = min(p, vec2(0));
return length(max(p, vec2(0))) + max(v.x, v.y);
}
// License: MIT, author: Inigo Quilez, found: https://iquilezles.org/www/articles/distfunctions2d/distfunctions2d.htm
float roundedBox(vec2 p, vec2 b, vec4 r) {
r.xy = (p.x>0.0)?r.xy : r.zw;
r.x = (p.y>0.0)?r.x : r.y;
vec2 q = abs(p)-b+r.x;
return min(max(q.x,q.y),0.0) + length(max(q,0.0)) - r.x;
}
// License: Unknown, author: Unknown, found: shadertoy somewhere, don't remember where
float dfcos(float x) {
return sqrt(x*x+1.0)*0.8-1.8;
}
// License: Unknown, author: Unknown, found: shadertoy somewhere, don't remember where
float dfcos(vec2 p, float freq) {
// Approximate distance to cos
float x = p.x;
float y = p.y;
x *= freq;
float x1 = abs(mod(x+PI,TAU)-PI);
float x2 = abs(mod(x ,TAU)-PI);
float a = 0.18*freq;
x1 /= max( y*a+1.0-a,1.0);
x2 /= max(-y*a+1.0-a,1.0);
return (mix(-dfcos(x2)-1.0,dfcos(x1)+1.0,clamp(y*0.5+0.5,0.0,1.0)))/max(freq*0.8,1.0)+max(abs(y)-1.0,0.0)*sign(y);
}
// License: MIT, author: Inigo Quilez, found: https://iquilezles.org/www/articles/distfunctions2d/distfunctions2d.htm
float bezier(vec2 pos, vec2 A, vec2 B, vec2 C) {
vec2 a = B - A;
vec2 b = A - 2.0*B + C;
vec2 c = a * 2.0;
vec2 d = A - pos;
float kk = 1.0/dot(b,b);
float kx = kk * dot(a,b);
float ky = kk * (2.0*dot(a,a)+dot(d,b)) / 3.0;
float kz = kk * dot(d,a);
float res = 0.0;
float p = ky - kx*kx;
float p3 = p*p*p;
float q = kx*(2.0*kx*kx-3.0*ky) + kz;
float h = q*q + 4.0*p3;
if(h >= 0.0) {
h = sqrt(h);
vec2 x = (vec2(h,-h)-q)/2.0;
vec2 uv = sign(x)*pow(abs(x), vec2(1.0/3.0));
float t = clamp(uv.x+uv.y-kx, 0.0, 1.0);
res = dot2(d + (c + b*t)*t);
} else {
float z = sqrt(-p);
float v = acos(q/(p*z*2.0)) / 3.0;
float m = cos(v);
float n = sin(v)*1.732050808;
vec3 t = clamp(vec3(m+m,-n-m,n-m)*z-kx,0.0,1.0);
res = min(dot2(d+(c+b*t.x)*t.x),
dot2(d+(c+b*t.y)*t.y));
// the third root cannot be the closest
// res = min(res,dot2(d+(c+b*t.z)*t.z));
}
return sqrt(res);
}
// License: MIT, author: Inigo Quilez, found: https://www.iquilezles.org/www/articles/spherefunctions/spherefunctions.htm
float raySphere(vec3 ro, vec3 rd, vec4 sph) {
vec3 oc = ro - sph.xyz;
float b = dot(oc, rd);
float c = dot(oc, oc) - sph.w*sph.w;
float h = b*b - c;
if(h<0.0) return -1.0;
h = sqrt(h);
return -b - h;
}
// License: MIT, author: Inigo Quilez, found: https://www.iquilezles.org/www/articles/spherefunctions/spherefunctions.htm
vec2 raySphere2(vec3 ro, vec3 rd, vec4 sph) {
vec3 oc = ro - sph.xyz;
float b = dot(oc, rd);
float c = dot(oc, oc) - sph.w*sph.w;
float h = b*b - c;
if(h<0.0) return vec2(-1.0);
h = sqrt(h);
return vec2(-b - h, -b + h);
}
// License: MIT, author: Inigo Quilez, found: https://www.iquilezles.org/www/articles/spherefunctions/spherefunctions.htm
float raySphereDensity(vec3 ro, vec3 rd, vec4 sph, float dbuffer) {
float ndbuffer = dbuffer/sph.w;
vec3 rc = (ro - sph.xyz)/sph.w;
float b = dot(rd,rc);
float c = dot(rc,rc) - 1.0;
float h = b*b - c;
if(h<0.0) return 0.0;
h = sqrt(h);
float t1 = -b - h;
float t2 = -b + h;
if(t2<0.0 || t1>ndbuffer) return 0.0;
t1 = max(t1, 0.0);
t2 = min(t2, ndbuffer);
float i1 = -(c*t1 + b*t1*t1 + t1*t1*t1/3.0);
float i2 = -(c*t2 + b*t2*t2 + t2*t2*t2/3.0);
return (i2-i1)*(3.0/4.0);
}
// License: MIT, author: Inigo Quilez, found: https://www.iquilezles.org/www/articles/intersectors/intersectors.htm
float rayTorus(vec3 ro, vec3 rd, vec2 tor) {
float po = 1.0;
float Ra2 = tor.x*tor.x;
float ra2 = tor.y*tor.y;
float m = dot(ro,ro);
float n = dot(ro,rd);
// bounding sphere
{
float h = n*n - m + (tor.x+tor.y)*(tor.x+tor.y);
if(h<0.0) return -1.0;
//float t = -n-sqrt(h); // could use this to compute intersections from ro+t*rd
}
// find quartic equation
float k = (m - ra2 - Ra2)/2.0;
float k3 = n;
float k2 = n*n + Ra2*rd.z*rd.z + k;
float k1 = k*n + Ra2*ro.z*rd.z;
float k0 = k*k + Ra2*ro.z*ro.z - Ra2*ra2;
#ifndef TORUS_REDUCE_PRECISION
// prevent |c1| from being too close to zero
if(abs(k3*(k3*k3 - k2) + k1) < 0.01)
{
po = -1.0;
float tmp=k1; k1=k3; k3=tmp;
k0 = 1.0/k0;
k1 = k1*k0;
k2 = k2*k0;
k3 = k3*k0;
}
#endif
float c2 = 2.0*k2 - 3.0*k3*k3;
float c1 = k3*(k3*k3 - k2) + k1;
float c0 = k3*(k3*(-3.0*k3*k3 + 4.0*k2) - 8.0*k1) + 4.0*k0;
c2 /= 3.0;
c1 *= 2.0;
c0 /= 3.0;
float Q = c2*c2 + c0;
float R = 3.0*c0*c2 - c2*c2*c2 - c1*c1;
float h = R*R - Q*Q*Q;
float z = 0.0;
if(h < 0.0) {
// 4 intersections
float sQ = sqrt(Q);
z = 2.0*sQ*cos(acos(R/(sQ*Q)) / 3.0);
} else {
// 2 intersections
float sQ = pow(sqrt(h) + abs(R), 1.0/3.0);
z = sign(R)*abs(sQ + Q/sQ);
}
z = c2 - z;
float d1 = z - 3.0*c2;
float d2 = z*z - 3.0*c0;
if(abs(d1) < 1.0e-4) {
if(d2 < 0.0) return -1.0;
d2 = sqrt(d2);
} else {
if(d1 < 0.0) return -1.0;
d1 = sqrt(d1/2.0);
d2 = c1/d1;
}
//----------------------------------
float result = 1e20;
h = d1*d1 - z + d2;
if(h > 0.0) {
h = sqrt(h);
float t1 = -d1 - h - k3; t1 = (po<0.0)?2.0/t1:t1;
float t2 = -d1 + h - k3; t2 = (po<0.0)?2.0/t2:t2;
if(t1 > 0.0) result=t1;
if(t2 > 0.0) result=min(result,t2);
}
h = d1*d1 - z - d2;
if(h > 0.0) {
h = sqrt(h);
float t1 = d1 - h - k3; t1 = (po<0.0)?2.0/t1:t1;
float t2 = d1 + h - k3; t2 = (po<0.0)?2.0/t2:t2;
if(t1 > 0.0) result=min(result,t1);
if(t2 > 0.0) result=min(result,t2);
}
return result;
}
// License: MIT, author: Inigo Quilez, found: https://www.iquilezles.org/www/articles/intersectors/intersectors.htm
vec3 torusNormal(vec3 pos, vec2 tor) {
return normalize(pos*(dot(pos,pos)- tor.y*tor.y - tor.x*tor.x*vec3(1.0,1.0,-1.0)));
}
// License: MIT, author: Pascal Gilcher, found: https://www.shadertoy.com/view/flSXRV
float atan_approx(float y, float x) {
float cosatan2 = x / (abs(x) + abs(y));
float t = PI_2 - cosatan2 * PI_2;
return y < 0.0 ? -t : t;
}
// License: Unknown, author: Unknown, found: don't remember
float tanh_approx(float x) {
// Found this somewhere on the interwebs
// return tanh(x);
float x2 = x*x;
return clamp(x*(27.0 + x2)/(27.0+9.0*x2), -1.0, 1.0);
}
// License: Unknown, author: Unknown, found: don't remember
float hash(float co) {
return fract(sin(co*12.9898) * 13758.5453);
}
// License: Unknown, author: Unknown, found: don't remember
float hash(vec2 co) {
return fract(sin(dot(co.xy ,vec2(12.9898,58.233))) * 13758.5453);
}
// License: Unknown, author: Unknown, found: don't remember
vec2 hash2(vec2 p) {
p = vec2(dot (p, vec2 (127.1, 311.7)), dot (p, vec2 (269.5, 183.3)));
return fract(sin(p)*43758.5453123);
}
// License: Unknown, author: Unknown, found: don't remember
float hash3(vec3 r) {
return fract(sin(dot(r.xy,vec2(1.38984*sin(r.z),1.13233*cos(r.z))))*653758.5453);
}
// License: CC0, author: Mårten Rånge, found: https://github.com/mrange/glsl-snippets
vec2 shash2(vec2 p) {
return -1.0+2.0*hash2(p);
}
// License: CC0, author: Mårten Rånge, found: https://github.com/mrange/glsl-snippets
vec4 alphaBlend(vec4 back, vec4 front) {
// Based on: https://en.wikipedia.org/wiki/Alpha_compositing
float w = front.w + back.w*(1.0-front.w);
vec3 xyz = (front.xyz*front.w + back.xyz*back.w*(1.0-front.w))/w;
return w > 0.0 ? vec4(xyz, w) : vec4(0.0);
}
// License: CC0, author: Mårten Rånge, found: https://github.com/mrange/glsl-snippets
vec3 alphaBlend(vec3 back, vec4 front) {
// Based on: https://en.wikipedia.org/wiki/Alpha_compositing
return mix(back, front.xyz, front.w);
}
// simplified version of Dave Hoskins blur
vec3 dblur(vec2 q,float rad) {
vec3 acc=vec3(0);
const float m = 0.002;
vec2 pixel=vec2(m*RESOLUTION.y/RESOLUTION.x,m);
vec2 angle=vec2(0,rad);
rad=1.;
const int iter = 30;
for (int j=0; j<iter; ++j) {
rad += 1./rad;
angle*=brot;
vec4 col=texture(prevFrame,q+pixel*(rad-1.)*angle);
acc+=col.xyz;
}
return acc*(1.0/float(iter));
}
// License: MIT, author: Inigo Quilez, found: https://www.shadertoy.com/view/XslGRr
float noise(vec2 p) {
// Found at https://www.shadertoy.com/view/sdlXWX
// Which then redirected to IQ shader
vec2 i = floor(p);
vec2 f = fract(p);
vec2 u = f*f*(3.-2.*f);
float n =
mix( mix( dot(shash2(i + vec2(0.,0.) ), f - vec2(0.,0.)),
dot(shash2(i + vec2(1.,0.) ), f - vec2(1.,0.)), u.x),
mix( dot(shash2(i + vec2(0.,1.) ), f - vec2(0.,1.)),
dot(shash2(i + vec2(1.,1.) ), f - vec2(1.,1.)), u.x), u.y);
return 2.0*n;
}
// License: MIT, author: Inigo Quilez, found: https://www.shadertoy.com/view/XslGRr
float vnoise(vec2 p) {
vec2 i = floor( p );
vec2 f = fract( p );
vec2 u = f*f*(3.0-2.0*f);
float a = hash( i + vec2(0.0,0.0) );
float b = hash( i + vec2(1.0,0.0) );
float c = hash( i + vec2(0.0,1.0) );
float d = hash( i + vec2(1.0,1.0) );
float m0 = mix(a, b, u.x);
float m1 = mix(c, d, u.x);
float m2 = mix(m0, m1, u.y);
return m2;
}
// License: MIT, author: Inigo Quilez, found: https://www.iquilezles.org/www/index.htm
vec3 postProcess(vec3 col, vec2 q) {
// Found this somewhere on the interwebs
col = clamp(col, 0.0, 1.0);
// Gamma correction
col = pow(col, 1.0/vec3(2.2));
col = col*0.6+0.4*col*col*(3.0-2.0*col);
col = mix(col, vec3(dot(col, vec3(0.33))), -0.4);
// Vignetting
col*= 0.5+0.5*pow(19.0*q.x*q.y*(1.0-q.x)*(1.0-q.y),0.7);
return col;
}