-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsdf.fxh
1428 lines (1200 loc) · 38.1 KB
/
sdf.fxh
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
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#define SDF_FXH
// largley based on hg_sdf GLSL lib by MERCURY (CC BY-NC 2016) http://mercury.sexy/hg_sdf
// simple operations
float U(float a, float b) {return min(a,b);}
float S(float a, float b) {return max(a,-b);}
float I(float a, float b) {return max(a,b);}
// simple operations w/ mat
float2 U(float2 a, float2 b) {return (a.x<b.x) ? a : b;}
float2 S(float2 a, float2 b) {return (a.x>-b.x) ? a : -b;}
float2 I(float2 a, float2 b) {return (a.x>b.x) ? a : b;}
////////////////////////////////////////////////////////////////
//
// HELPER FUNCTIONS/MACROS
//
////////////////////////////////////////////////////////////////
#ifndef PI
#define PI 3.1415926535897
#endif
#ifndef TWOPI
#define TWOPI 6.28318531
#endif
#ifndef TAU
#define TAU (2*PI)
#endif
#ifndef PHI
#define PHI (sqrt(5)*0.5 + 0.5)
#endif
// glsl style mod
#ifndef mod
#define mod(x, y) (x - y * floor((x) / y))
#endif
// Clamp to [0,1] - this operation is free under certain circumstances.
// For further information see
// http://www.humus.name/Articles/Persson_LowLevelThinking.pdf and
// http://www.humus.name/Articles/Persson_LowlevelShaderOptimization.pdf
//#define saturate(x) clamp(x, 0, 1)
// Sign function that doesn't return 0
float sgn(float x) {
return (x<0)?-1:1;
}
float2 sgn(float2 v) {
return float2((v.x<0)?-1:1, (v.y<0)?-1:1);
}
float square (float x) {
return x*x;
}
float2 square (float2 x) {
return x*x;
}
float3 square (float3 x) {
return x*x;
}
float lengthSqr(float3 x) {
return dot(x, x);
}
// Maximum/minumum elements of a floattor
float vmax(float2 v) {
return max(v.x, v.y);
}
float vmax(float3 v) {
return max(max(v.x, v.y), v.z);
}
float vmax(float4 v) {
return max(max(v.x, v.y), max(v.z, v.w));
}
float vmin(float2 v) {
return min(v.x, v.y);
}
float vmin(float3 v) {
return min(min(v.x, v.y), v.z);
}
float vmin(float4 v) {
return min(min(v.x, v.y), min(v.z, v.w));
}
float dot2( in float3 v ) { return dot(v,v); }
////////////////////////////////////////////////////////////////
//
// PRIMITIVE DISTANCE FUNCTIONS
//
////////////////////////////////////////////////////////////////
//
// Conventions:
//
// Everything that is a distance function is called fSomething.
// The first argument is always a point in 2 or 3-space called <p>.
// Unless otherwise noted, (if the object has an intrinsic "up"
// side or direction) the y axis is "up" and the object is
// centered at the origin.
//
////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
//
// 2D DISTANCE FUNCTIONS
//
////////////////////////////////////////////////////////////////
float draw2DSDF (float dist, float softness = 0.005)
{
return smoothstep(softness, 0, dist);
};
float fCircle(float2 p, float radius)
{
return length(p) - radius;
}
float fEclipse(float2 p, float2 r)
{
float a2 = r.x * r.x;
float b2 = r.y * r.y;
return (b2 * p.x * p.x + a2 * p.y * p.y - a2 * b2) / (a2 * b2);
}
// Cheap Box: distance to corners is overestimated
float fBox2Cheap(float2 p, float2 b)
{
return vmax(abs(p)-b);
}
// Box: correct distance to corners
float fBox(float2 p, float2 b)
{
float2 d = abs(p) - b;
return length(max(d, 0)) + vmax(min(d, 0));
}
//Rounded Box:
float fRBox(float2 p,float2 b,float rad ) { return length(max(abs(p)-b+rad,0.0))-rad; }
// Endless "corner"
float fCorner (float2 p)
{
return length(max(p, 0)) + vmax(min(p, 0));
}
float fLine(float2 p, float2 a, float2 b, float r)
{
float l = length(b -a);
float2 n = normalize(b - a);
float2 a_p = p - a;
return length(a_p - clamp(dot(a_p, n), 0, l)*n) - r;
}
float fTriangle(float2 p, float2 a, float2 b, float2 c)
{
float2 ab = b-a;
float2 ac = c-a;
float2 bc = c-b;
float2 as = p-a;
float2 bs = p-b;
float2 nab = normalize(float2(-ab.y, ab.x));
float2 nac = normalize(float2(ac.y, -ac.x));
float2 nbc = normalize(float2(-bc.y, bc.x));
float u = dot(as, nab);
float v = dot(as, nac);
float w = dot(bs, nbc);
return max(max(u, v), w);
}
float fHexagon(float2 p, float radius)
{
float2 q = abs(p);
return max((q.x * 0.866025 + q.y * 0.5), q.y) - radius;
}
////////////////////////////////////////////////////////////////
// https://github.com/keijiro/ShaderSketches
float fLineTruchet (float2 p, float width = 0.05)
{
float rnd = frac(sin(dot(floor(p), float2(21.98, 19.37))) * 4231.73);
rnd = floor(rnd * 2) / 2;
float phi = PI * (rnd +.25);
float2 pf = frac(p);
float2 dir = float2(cos(phi), sin(phi));
float d1 = abs(dot(pf - float2(0.5, 0), dir)); // line 1
float d2 = abs(dot(pf - float2(0.5, 1), dir)); // line 2
return min(d1, d2) - width * 0.5;
}
float fCircleTruchet (float2 p, float width = 0.05)
{
float rnd = frac(sin(dot(floor(p), float2(21.98, 19.37))) * 4231.73);
rnd = floor(rnd * 2) / 2;
float phi = PI * (rnd +.25);
float2 pf = frac(p);
float2 offs = float2(cos(phi), sin(phi)) * sqrt(2) / 2;
float d1 = abs(0.5 - distance(pf, float2(0.5 - offs))); // arc 1
float d2 = abs(0.5 - distance(pf, float2(0.5 + offs))); // arc 2
return min(d1, d2) - width * 0.5;
}
float fOctoTruchet (float2 p, float width = 0.05)
{
float rnd = frac(sin(dot(floor(p), float2(21.98, 19.37))) * 4231.73);
float flip = frac(rnd * 13.8273) > 0.5 ? 1 : -1;
rnd = floor(rnd * 2) * flip / 2;
float phi = PI * rnd;
float2 pf = frac(p) -.5;
float2 a1 = float2(cos(phi), sin(phi));
float2 a2 = float2(-a1.y, a1.x);
float2 a3 = float2(cos(phi + PI / 4), sin(phi + PI / 4));
float d1 = abs(min(min(dot( pf, a1), dot( pf, a2)), dot( pf, a3) - 0.2));
float d2 = abs(min(min(dot(-pf, a1), dot(-pf, a2)), dot(-pf, a3) - 0.2));
return min(d1, d2) - width * 0.5;
}
////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
//
// 3D DISTANCE FUNCTIONS
//
////////////////////////////////////////////////////////////////
float fSphere(float3 p, float r)
{
return length(p) - r;
}
// Plane with normal n (n is normalized) at some distance from the origin
float fPlane(float3 p, float distanceFromOrigin = 0, float3 n = float3(0,1,0))
{
return dot(p, n) + distanceFromOrigin;
}
//Unsigned
float fTriangle( float3 p, float3 a, float3 b, float3 c )
{
float3 ba = b - a; float3 pa = p - a;
float3 cb = c - b; float3 pb = p - b;
float3 ac = a - c; float3 pc = p - c;
float3 nor = cross( ba, ac );
return sqrt(
(sign(dot(cross(ba,nor),pa)) +
sign(dot(cross(cb,nor),pb)) +
sign(dot(cross(ac,nor),pc))<2.0)
?
min( min(
dot2(ba*clamp(dot(ba,pa)/dot2(ba),0.0,1.0)-pa),
dot2(cb*clamp(dot(cb,pb)/dot2(cb),0.0,1.0)-pb) ),
dot2(ac*clamp(dot(ac,pc)/dot2(ac),0.0,1.0)-pc) )
:
dot(nor,pa)*dot(nor,pa)/dot2(nor) );
}
// Cheap Box: distance to corners is overestimated
float fBoxCheap(float3 p, float3 b)
{ //cheap box
return vmax(abs(p) - b);
}
// Box: correct distance to corners
float fBox(float3 p, float3 b)
{
float3 d = abs(p) - b;
return length(max(d, 0)) + vmax(min(d, 0));
}
// Rounded Cubes:
float fRBox(float3 p,float3 b,float rad ) { return length(max(abs(p)-b+rad,0.0))-rad; }
// Box Frame
float fBoxFrame( float3 p, float3 b, float e)
{
p = abs(p )-b;
float3 q = abs(p+e)-e;
return min(min(
length(max(float3(p.x,q.y,q.z),0.0))+min(max(p.x,max(q.y,q.z)),0.0),
length(max(float3(q.x,p.y,q.z),0.0))+min(max(q.x,max(p.y,q.z)),0.0)),
length(max(float3(q.x,q.y,p.z),0.0))+min(max(q.x,max(q.y,p.z)),0.0));
}
// Blobby ball object. You've probably seen it somewhere. This is not a correct distance bound, beware.
float fBlob(float3 p)
{
p = abs(p);
if (p.x < max(p.y, p.z)) p = p.yzx;
if (p.x < max(p.y, p.z)) p = p.yzx;
float b = max(max(max(
dot(p, normalize(float3(1, 1, 1))),
dot(p.xz, normalize(float2(PHI+1, 1)))),
dot(p.yx, normalize(float2(1, PHI)))),
dot(p.xz, normalize(float2(1, PHI))));
float l = length(p);
return l - 1.5 - 0.2 * (1.5 / 2)* cos(min(sqrt(1.01 - b / l)*(PI / 0.25), PI));
}
// Cylinder standing upright on the xz plane
float fCylinder(float3 p, float r, float height)
{
float d = length(p.xz) - r;
d = max(d, abs(p.y) - height);
return d;
}
// Capsule: A Cylinder with round caps on both sides
float fCapsule(float3 p, float r, float c)
{
return lerp(length(p.xz) - r, length(float3(p.x, abs(p.y) - c, p.z)) - r, step(c, abs(p.y)));
}
// Distance to line segment between <a> and <b>, used for fCapsule() version 2below
float fLine(float3 p, float3 a, float3 b)
{
float3 ab = b - a;
float t = saturate(dot(p - a, ab) / dot(ab, ab));
return length((ab*t + a) - p);
}
// Capsule version 2: between two end points <a> and <b> with radius r
float fCapsule(float3 p, float3 a, float3 b, float r)
{
return fLine(p, a, b) - r;
}
// Torus in the XZ-plane
float fTorus(float3 p, float smallRadius, float largeRadius)
{
return length(float2(length(p.xz) - largeRadius, p.y)) - smallRadius;
}
// A circle line. Can also be used to make a torus by subtracting the smaller radius of the torus.
float fCircle(float3 p, float r)
{
float l = length(p.xz) - r;
return length(float2(p.y, l));
}
// A circular disc with no thickness (i.e. a cylinder with no height).
// Subtract some value to make a flat disc with rounded edge.
float fDisc(float3 p, float r)
{
float l = length(p.xz) - r;
return l < 0 ? abs(p.y) : length(float2(p.y, l));
}
float fTriPrism( float3 p, float2 h )
{
float3 q = abs(p);
return max(q.z-h.y,max(q.x*0.866025+p.y*0.5,-p.y)-h.x*0.5);
}
float fPyramid(float3 p, float3 h )
{
float box = fBox( p - float3(0,-2.0*h.z,0),2.0*h.z);
float d = 0.0;
d = max( d, abs( dot(p, float3( -h.x, h.y, 0 )) ));
d = max( d, abs( dot(p, float3( h.x, h.y, 0 )) ));
d = max( d, abs( dot(p, float3( 0, h.y, h.x )) ));
d = max( d, abs( dot(p, float3( 0, h.y,-h.x )) ));
float octa = d - h.z;
return max(-box,octa);
}
// Hexagonal prism, circumcircle variant
float fHexagonCircumcircle(float3 p, float2 h)
{
float3 q = abs(p);
return max(q.y - h.y, max(q.x*sqrt(3)*0.5 + q.z*0.5, q.z) - h.x);
//this is mathematically equivalent to this line, but less efficient:
//return max(q.y - h.y, max(dot(float2(cos(PI/3), sin(PI/3)), q.zx), q.z) - h.x);
}
// Hexagonal prism, incircle variant
float fHexagonIncircle(float3 p, float2 h)
{
return fHexagonCircumcircle(p, float2(h.x*sqrt(3)*0.5, h.y));
}
// Cone with correct distances to tip and base circle. Y is up, 0 is in the middle of the base.
float fCone(float3 p, float radius, float height)
{
float2 q = float2(length(p.xz), p.y);
float2 tip = q - float2(0, height);
float2 mantleDir = normalize(float2(height, radius));
float mantle = dot(tip, mantleDir);
float d = max(mantle, -q.y);
float projected = dot(tip, float2(mantleDir.y, -mantleDir.x));
// distance to tip
if ((q.y > height) && (projected < 0)) {
d = max(d, length(tip));
}
// distance to base ring
if ((q.x > radius) && (projected > length(float2(height, radius)))) {
d = max(d, length(q - float2(radius, 0)));
}
return d;
}
/////////////////////////////////////////////////////////////////////
// 3 point Bezier
// http://research.microsoft.com/en-us/um/people/hoppe/ravg.pdf
// https://www.shadertoy.com/view/ldj3Wh
// TODO add 2D versions
/////////////////////////////////////////////////////////////////////
float det( float2 a, float2 b ) { return a.x*b.y-b.x*a.y; }
float3 getClosest( float2 b0, float2 b1, float2 b2 )
{
float a = det(b0,b2);
float b = 2.0*det(b1,b0);
float d = 2.0*det(b2,b1);
float f = b*d - a*a;
float2 d21 = b2-b1;
float2 d10 = b1-b0;
float2 d20 = b2-b0;
float2 gf = 2.0*(b*d21+d*d10+a*d20); gf = float2(gf.y,-gf.x);
float2 pp = -f*gf/dot(gf,gf);
float2 d0p = b0-pp;
float ap = det(d0p,d20);
float bp = 2.0*det(d10,d0p);
float t = clamp( (ap+bp)/(2.0*a+b+d), 0.0 ,1.0 );
return float3( lerp(lerp(b0,b1,t), lerp(b1,b2,t),t), t );
}
//returns distance, phase
float2 fBezier(float3 p, float3 a, float3 b, float3 c)
{
float3 w = normalize( cross( c-b, a-b ) );
float3 u = normalize( c-b );
float3 v = normalize( cross( w, u ) );
float2 a2 = float2( dot(a-b,u), dot(a-b,v) );
float2 b2 = 0.0 ;
float2 c2 = float2( dot(c-b,u), dot(c-b,v) );
float3 p3 = float3( dot(p-b,u), dot(p-b,v), dot(p-b,w) );
float3 cp = getClosest( a2-p3.xy, b2-p3.xy, c2-p3.xy );
return float2( sqrt(dot(cp.xy,cp.xy)+p3.z*p3.z), cp.z);
}
//returns distance, phase
float2 fBezierBuffer(float3 p, StructuredBuffer<float3> controlBuffer, int controlCount)
{
uint segCount = controlCount / (uint)2;
if(controlCount % (uint)2 == 0) segCount --; // check if even or odd and truncate if needed
float segSize = 1/(float)segCount;
float2 result = 999999;
float2 bezSegment = fBezier(p, controlBuffer[0], controlBuffer[1], controlBuffer[2]);
bezSegment.y *= segSize;
if(result.x > bezSegment.x) result = bezSegment; //Union operation
for (uint i = 1; i < segCount; i++)
{
uint c0 = 2*i;
uint c1 = 2*i+1;
uint c2 = 2*i+2;
bezSegment = fBezier(p, controlBuffer[c0], controlBuffer[c1], controlBuffer[c2]);
bezSegment.y = bezSegment.y * segSize + segSize * i; // need to fix phase per segment
if(result.x > bezSegment.x) result = bezSegment; //Union operation
}
return result;
}
//returns distance, tangent
float4 fBezierTang(float3 p, float3 a, float3 b, float3 c)
{
float3 w = normalize( cross( c-b, a-b ) );
float3 u = normalize( c-b );
float3 v = normalize( cross( w, u ) );
float2 a2 = float2( dot(a-b,u), dot(a-b,v) );
float2 b2 = 0.0 ;
float2 c2 = float2( dot(c-b,u), dot(c-b,v) );
float3 p3 = float3( dot(p-b,u), dot(p-b,v), dot(p-b,w) );
float3 cp = getClosest( a2-p3.xy, b2-p3.xy, c2-p3.xy );
float3 tangent = lerp(b-a, c-b, c.z);
return float4( sqrt(dot(cp.xy,cp.xy)+p3.z*p3.z), tangent);
}
//returns distance, tangent
float4 fBezierBufferTang(float3 p, StructuredBuffer<float3> controlBuffer, int controlCount)
{
uint segCount = controlCount / (uint)2;
if(controlCount % (uint)2 == 0) segCount --; // check if even or odd and truncate if needed
float4 result = 999999;
float4 bezSegment = fBezierTang(p, controlBuffer[0], controlBuffer[1], controlBuffer[2]);
if(result.x > bezSegment.x) result = bezSegment;
for (uint i = 1; i < segCount; i++)
{
uint c0 = 2*i;
uint c1 = 2*i+1;
uint c2 = 2*i+2;
float4 bezSegment = fBezierTang(p, controlBuffer[c0], controlBuffer[c1], controlBuffer[c2]);
if(result.x > bezSegment.x) result = bezSegment; //Union operation
}
return result;
}
// 2D version
// quadratic bezier 2D curve evaluation
// posted by Trisomie21
//Helpers
float fBezierCuberoot( float x )
{
if( x<0.0 ) return -pow(-x,1.0/3.0);
return pow(x,1.0/3.0);
}
int fBezierSolveCubic(in float a, in float b, in float c, out float r[3])
{
float p = b - a*a / 3.0;
float q = a * (2.0*a*a - 9.0*b) / 27.0 + c;
float p3 = p*p*p;
float d = q*q + 4.0*p3 / 27.0;
float offset = -a / 3.0;
if(d >= 0.0) { // Single solution
float z = sqrt(d);
float u = (-q + z) / 2.0;
float v = (-q - z) / 2.0;
u = fBezierCuberoot(u);
v = fBezierCuberoot(v);
r[0] = offset + u + v;
return 1;
}
float u = sqrt(-p / 3.0);
float v = acos(-sqrt( -27.0 / p3) * q / 2.0) / 3.0;
float m = cos(v), n = sin(v)*1.732050808;
r[0] = offset + u * (m + m);
r[1] = offset - u * (n + m);
r[2] = offset + u * (n - m);
return 3;
}
// Here's the function
float fBezier(float2 p, float2 P0, float2 P1, float2 P2)
{
float dis = 1e20;
float2 sb = (P1 - P0) * 2.0;
float2 sc = P0 - P1 * 2.0 + P2;
float2 sd = P1 - P0;
float sA = 1.0 / dot(sc, sc);
float sB = 3.0 * dot(sd, sc);
float sC = 2.0 * dot(sd, sd);
float2 D = P0 - p;
float a = sA;
float b = sB;
float c = sC + dot(D, sc);
float d = dot(D, sd);
float res[3];
int n = fBezierSolveCubic(b*a, c*a, d*a, res);
float t = clamp(res[0],0.0, 1.0);
float2 pos = P0 + (sb + sc*t)*t;
dis = min(dis, length(pos - p));
if(n>1)
{
t = clamp(res[1],0.0, 1.0);
pos = P0 + (sb + sc*t)*t;
dis = min(dis, length(pos - p));
t = clamp(res[2],0.0, 1.0);
pos = P0 + (sb + sc*t)*t;
dis = min(dis, length(pos - p));
}
return dis;
}
/////////////////////////////////////////////////////////////////////
//
// "Generalized Distance Functions" by Akleman and Chen.
// see the Paper at https://www.viz.tamu.edu/faculty/ergun/research/implicitmodeling/papers/sm99.pdf
//
// This set of constants is used to construct a large variety of geometric primitives.
// Indices are shifted by 1 compared to the paper because we start counting at Zero.
// Some of those are slow whenever a driver decides to not unroll the loop,
// which seems to happen for fIcosahedron und fTruncatedIcosahedron on nvidia 350.12 at least.
// Specialized implementations can well be faster in all cases.
//
static const float3 GDFVectors[19] =
{
normalize(float3(1, 0, 0)),
normalize(float3(0, 1, 0)),
normalize(float3(0, 0, 1)),
normalize(float3(1, 1, 1 )),
normalize(float3(-1, 1, 1)),
normalize(float3(1, -1, 1)),
normalize(float3(1, 1, -1)),
normalize(float3(0, 1, PHI+1)),
normalize(float3(0, -1, PHI+1)),
normalize(float3(PHI+1, 0, 1)),
normalize(float3(-PHI-1, 0, 1)),
normalize(float3(1, PHI+1, 0)),
normalize(float3(-1, PHI+1, 0)),
normalize(float3(0, PHI, 1)),
normalize(float3(0, -PHI, 1)),
normalize(float3(1, 0, PHI)),
normalize(float3(-1, 0, PHI)),
normalize(float3(PHI, 1, 0)),
normalize(float3(-PHI, 1, 0))
};
// Version with variable exponent.
// This is slow and does not produce correct distances, but allows for bulging of objects.
float fGDF(float3 p, float r, float e, int begin, int end)
{
float d = 0;
for (int i = begin; i <= end; ++i)
d += pow(abs(dot(p, GDFVectors[i])), e);
return pow(d, 1/e) - r;
}
// Version with without exponent, creates objects with sharp edges and flat faces
float fGDF(float3 p, float r, int begin, int end)
{
float d = 0;
for (int i = begin; i <= end; ++i)
d = max(d, abs(dot(p, GDFVectors[i])));
return d - r;
}
// Primitives follow:
float fOctahedron(float3 p, float r, float e)
{
return fGDF(p, r, e, 3, 6);
}
float fDodecahedron(float3 p, float r, float e)
{
return fGDF(p, r, e, 13, 18);
}
float fIcosahedron(float3 p, float r, float e)
{
return fGDF(p, r, e, 3, 12);
}
float fTruncatedOctahedron(float3 p, float r, float e)
{
return fGDF(p, r, e, 0, 6);
}
float fTruncatedIcosahedron(float3 p, float r, float e)
{
return fGDF(p, r, e, 3, 18);
}
float fOctahedron(float3 p, float r)
{
return fGDF(p, r, 3, 6);
}
float fDodecahedron(float3 p, float r)
{
return fGDF(p, r, 13, 18);
}
float fIcosahedron(float3 p, float r)
{
return fGDF(p, r, 3, 12);
}
float fTruncatedOctahedron(float3 p, float r)
{
return fGDF(p, r, 0, 6);
}
float fTruncatedIcosahedron(float3 p, float r)
{
return fGDF(p, r, 3, 18);
}
// some fractal novelties
float fJulia(float3 p, float4 pars = float4(.5, 0, .75, 0), uint iter = 11)
{
//float4 c = 0.4*cos( float4(0.5,3.9,1.4,1.1) + time*float4(1.2,1.7,1.3,2.5) ) - float4(0.3,0.0,0.0,0.0);
float4 z = float4(p,0.0);
float md2 = 1.0;
float mz2 = dot(z,z);
float4 trap = float4(abs(z.xyz),dot(z,z));
for( uint i=0; i<iter; i++ )
{
// |dz|^2 -> 4*|dz|^2
md2 *= 4.0*mz2;
// z -> z2 + c
z = float4( z.x*z.x-dot(z.yzw,z.yzw),
2.0*z.x*z.yzw ) + pars;
trap = min( trap, float4(abs(z.xyz),dot(z,z)) );
mz2 = dot(z,z);
if(mz2>4.0) break;
}
//return length(pos) -.25;
return 0.25*sqrt(mz2/md2)*log(mz2);
}
float fMandelbulb(float3 p, float3 pars = float3(64, 16, 8), uint iter = 32)
{
float a = 1;
float3 w = p;
float m = dot(w,w);
float4 trap = float4(abs(w),m);
float dz = a;
for( uint i=0; i<iter; i++ )
{
float m2 = m*m;
float m4 = m2*m2;
dz = 8.0*sqrt(m4*m2*m)*dz + 1.0;
float x = w.x; float x2 = x*x; float x4 = x2*x2;
float y = w.y; float y2 = y*y; float y4 = y2*y2;
float z = w.z; float z2 = z*z; float z4 = z2*z2;
float k3 = x2 + z2;
float k2 = rsqrt( k3*k3*k3*k3*k3*k3*k3 );
float k1 = x4 + y4 + z4 - 6.0*y2*z2 - 6.0*x2*y2 + 2.0*z2*x2;
float k4 = x2 - y2 + z2;
w.x = p.x + pars.x *x*y*z*(x2-z2)*k4*(x4-6.0*x2*z2+z4)*k1*k2;
w.y = p.y + -pars.y *y2*k3*k4*k4 + k1*k1;
w.z = p.z + -pars.z*y*k4*(x4*x4 - 28.0*x4*x2*z2 + 70.0*x4*z4 - 28.0*x2*z2*z4 + z4*z4)*k1*k2;
trap = min( trap, float4(abs(w),m) );
m = dot(w,w);
if( m > 4.0 )
break;
}
return abs(0.25*log(m)*sqrt(m)/dz);
}
float fKali(float3 pos, float3 pars = float3 (.2, 1.6, 0.5), uint iter = 3)
{
float minRad2 = clamp(pars.x, 1.0e-9, 1.0);
float4 p = float4(pos,1);
float4 p0 = p; // p.w is the distance estimate
for (uint i = 0; i < iter; i++)
{
p.xyz = clamp(p.xyz, -1.0, 1.0) * 2.0 - p.xyz;
// sphere folding:
float r2 = dot(p.xyz, p.xyz);
if (r2 < minRad2) p /= minRad2;
else if (r2 < 1.0) p /= r2;
p *= clamp(max(minRad2/r2, minRad2), 0.0, 1.0);
// scale, translate
p = p*float4(pars.xxx, abs(pars.x)) / minRad2 + p0;
}
return abs((length(p.xyz) - pars.y) / p.w - pars.z);
}
float fKaliThorns(float3 p, float width=.12, float rotation = 1.8)
{
float dotp=dot(p,p);
p=p/dotp;
p=sin(p.xyz+float3(sin(1.+rotation)*2.,-rotation,-rotation*2.));
float d=length(p.yz)-width;
d=min(d,length(p.xz)-width);
d=min(d,length(p.xy)-width);
d=min(d,length(p*p*p)-width);
return d*dotp;
}
///////////////////////////////////////////////////////////////////////////////////////
// Mandelbox Distance Estimator (Rrrola's version).
///////////////////////////////////////////////////////////////////////////////////////
#ifndef TRANSFORM_FXH
#include <packs\happy.fxh\transform.fxh>
#endif
float fMandelbox(float3 pos, float MinRad2, float Scale, float3 Trans, float3 Julia, float3 PYR, int Iterations = 16)
{
float4 p = float4(pos,1);
float4 p0 = float4(Julia,1); // p.w is the distance estimate
float4 scale = float4(Scale, Scale, Scale, abs(Scale)) / MinRad2;
float absScalem1 = abs(Scale - 1.0);
float AbsScaleRaisedTo1mIters = pow(abs(Scale), float(1-Iterations));
float3x3 rot = rot3x3(PYR);
for (int i=0; i<Iterations; i++) {
p.xyz = mul(p.xyz, rot);
p.xyz=abs(p.xyz)+Trans;
float r2 = dot(p.xyz, p.xyz);
p *= clamp(max(MinRad2/r2, MinRad2), 0.0, 1.0); // dp3,div,max.sat,mul
p = p*scale + p0;
}
return ((length(p.xyz) - absScalem1) / p.w - AbsScaleRaisedTo1mIters);
}
///////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////
// Fractal Qube Ported from Fragmentarium 'BioCube' example by Darkbeam
///////////////////////////////////////////////////////////////////////////////////////
#ifndef TRANSFORM_FXH
#include <packs\happy.fxh\transform.fxh>
#endif
float fFractalQube(float3 p, float Scale, float3 Offset, float3 Offset2, float3 Rot, float Qube, int Iterations)
{
float3x3 fracRotation1 = Scale * rot3x3(Rot);
float t;
int n = 0;
float scalep = 1;
float3 z0 = p;
p = abs(p);
//z -= (1,1,1);
if (p.y>p.x) p.xy = p.yx;
if (p.z>p.x) p.xz = p.zx;
if (p.y>p.x) p.xy = p.yx;
float d = 1.0- p.x;
p = z0;
// Folds.
//Dodecahedral
while (n < Iterations)
{
p = mul(p, fracRotation1);
p = abs(p);
p -= Offset;
if (p.y>p.x) p.xy = p.yx;
if (p.z>p.x) p.xz = p.zx;
if (p.y>p.x) p.xy = p.yx;
p -= Offset2;
if (p.y>p.x) p.xy = p.yx;
if (p.z>p.x) p.xz = p.zx;
if (p.y>p.x) p.xy = p.yx;
n++; scalep *= Scale;
d = abs(min(Qube/n-d,(+p.x)/scalep));
}
return d;
}
///////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
//
// DOMAIN MANIPULATION OPERATORS
//
////////////////////////////////////////////////////////////////
//
// Conventions:
//
// Everything that modifies the domain is named pSomething.
//
// Many operate only on a subset of the three dimensions. For those,
// you must choose the dimensions that you want manipulated
// by supplying e.g. <p.x> or <p.zx>
//
// <inout p> is always the first argument and modified in place.
//
// Many of the operators partition space into cells. An identifier
// or cell index is returned, if possible. This return value is
// intended to be optionally used e.g. as a random seed to change
// parameters of the distance functions inside the cells.
//
// Unless stated otherwise, for cell index 0, <p> is unchanged and cells
// are centered on the origin so objects don't have to be moved to fit.
//
//
////////////////////////////////////////////////////////////////
// Rotate around a coordinate axis (i.e. in a plane perpendicular to that axis) by angle <a>.
// Read like this: R(p.xz, a) rotates "x towards z".
// This is fast if <a> is a compile-time constant and slower (but still practical) if not.
void pR(inout float2 p, float a)
{
p = cos(a)*p + sin(a)*float2(p.y, -p.x);
}
// Shortcut for 45-degrees rotation
void pR45(inout float2 p)
{
p = (p + float2(p.y, -p.x))*sqrt(0.5);
}
// Repeat space along one axis. Use like this to repeat along the x axis:
// <float cell = pMod1(p.x,5);> - using the return value is optional.
float pMod1(inout float p, float size)
{
float halfsize = size*0.5;
float c = floor((p + halfsize)/size);
p = mod(p + halfsize, size) - halfsize;
return c;
}
// Same, but mirror every second cell so they match at the boundaries
float pModMirror1(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 - 1;
return c;
}
// Repeat the domain only in positive direction. Everything in the negative half-space is unchanged.
float pModSingle1(inout float p, float size)
{
float halfsize = size*0.5;
float c = floor((p + halfsize)/size);
if (p >= 0)
p = mod(p + halfsize, size) - halfsize;
return c;
}
// Repeat only a few times: from indices <start> to <stop> (similar to above, but more flexible)
float pModInterval1(inout float p, float size, float start, float stop)
{
float halfsize = size*0.5;
float c = floor((p + halfsize)/size);
p = mod(p+halfsize, size) - halfsize;
if (c > stop) { //yes, this might not be the best thing numerically.
p += size*(c - stop);
c = stop;
}
if (c <start) {
p += size*(c - start);
c = start;
}
return c;
}
// Repeat around the origin by a fixed angle.
// For easier use, num of repetitions is use to specify the angle.
float pModPolar(inout float2 p, float repetitions)
{
float angle = 2*PI/repetitions;
float a = atan2(p.y, p.x) + angle/2.;
float r = length(p);
float c = floor(a/angle);
a = abs(a%angle) - angle/2.;
p = float2(cos(a), sin(a))*r;
// For an odd number of repetitions, fix cell index of the cell in -x direction
// (cell index would be e.g. -5 and 5 in the two halves of the cell):
if (abs(c) >= (repetitions/2)) c = abs(c);
return c;
}
// Repeat in two dimensions
float2 pMod2(inout float2 p, float2 size)
{