-
-
Notifications
You must be signed in to change notification settings - Fork 231
/
SDF.cginc
640 lines (522 loc) · 14.7 KB
/
SDF.cginc
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
#ifndef PROCEDURAL_TOOLKIT_SDF_INCLUDED
#define PROCEDURAL_TOOLKIT_SDF_INCLUDED
//
// Collection of signed distance functions
//
#include "UnityCG.cginc"
#include "Common.cginc"
//
// Space transformations
//
//
// Tile 1D
//
float Tile(float p, float tiling)
{
return frac(p*tiling);
}
float TileIO(inout float p, float tiling)
{
p *= tiling;
float cell = floor(p);
p = frac(p);
return cell;
}
float MirrorTile(float p, float tiling)
{
p *= tiling;
float cell = floor(p);
p = frac(p);
p = lerp(p, 1.0 - p, abs(fmod(cell, 2.0)));
return p;
}
float MirrorTileIO(inout float p, float tiling)
{
p *= tiling;
float cell = floor(p);
p = frac(p);
p = lerp(p, 1.0 - p, abs(fmod(cell, 2.0)));
return cell;
}
//
// Tile 2D
//
float2 Tile(float2 p, float2 tiling)
{
return frac(p*tiling);
}
float2 Tile(float2 p, float tilingX, float tilingY)
{
return Tile(p, float2(tilingX, tilingY));
}
float2 TileIO(inout float2 p, float2 tiling)
{
p *= tiling;
float2 cell = floor(p);
p = frac(p);
return cell;
}
float2 TileIO(inout float2 p, float tilingX, float tilingY)
{
return TileIO(p, float2(tilingX, tilingY));
}
float2 MirrorTile(float2 p, float2 tiling)
{
p *= tiling;
float2 cell = floor(p);
p = frac(p);
p = lerp(p, float2(1.0, 1.0) - p, abs(fmod(cell, float2(2.0, 2.0))));
return p;
}
float2 MirrorTile(float2 p, float tilingX, float tilingY)
{
return MirrorTile(p, float2(tilingX, tilingY));
}
float2 MirrorTileIO(inout float2 p, float2 tiling)
{
p *= tiling;
float2 cell = floor(p);
p = frac(p);
p = lerp(p, float2(1.0, 1.0) - p, abs(fmod(cell, float2(2.0, 2.0))));
return cell;
}
float2 MirrorTileIO(inout float2 p, float tilingX, float tilingY)
{
return MirrorTileIO(p, float2(tilingX, tilingY));
}
float2 BrickTile(float2 p, float2 tiling, float xOffset)
{
p *= tiling;
p.x -= abs(fmod(floor(p.y), 2.0))*xOffset;
p = frac(p);
return p;
}
float2 BrickTile(float2 p, float tilingX, float tilingY, float xOffset)
{
return BrickTile(p, float2(tilingX, tilingY), xOffset);
}
float2 BrickTileIO(inout float2 p, float2 tiling, float xOffset)
{
p *= tiling;
p.x -= abs(fmod(floor(p.y), 2.0))*xOffset;
float2 cell = floor(p);
p = frac(p);
return cell;
}
float2 BrickTileIO(inout float2 p, float tilingX, float tilingY, float xOffset)
{
return BrickTileIO(p, float2(tilingX, tilingY), xOffset);
}
float2 RadialTile(float2 p, float segments)
{
float segmentAngle = UNITY_TWO_PI / segments;
float halfSegmentAngle = segmentAngle*0.5;
float angleRadians = atan2(-p.x, -p.y) + UNITY_PI + halfSegmentAngle;
float repeat = fmod(angleRadians, segmentAngle) - halfSegmentAngle;
p = float2(sin(repeat), cos(repeat))*length(p);
return p;
}
float RadialTileIO(inout float2 p, float segments)
{
float segmentAngle = UNITY_TWO_PI/segments;
float halfSegmentAngle = segmentAngle*0.5;
float angleRadians = atan2(-p.x, -p.y) + UNITY_PI + halfSegmentAngle;
float cell = fmod(floor(angleRadians/segmentAngle), segments);
float repeat = fmod(angleRadians, segmentAngle) - halfSegmentAngle;
p = float2(sin(repeat), cos(repeat))*length(p);
return cell;
}
//
// Rotate 2D space
//
float2 RotateCW(float2 p, float angleRadians)
{
return cos(angleRadians)*p + sin(angleRadians)*float2(-p.y, p.x);
}
float2 RotateCCW(float2 p, float angleRadians)
{
return cos(angleRadians)*p + sin(angleRadians)*float2(p.y, -p.x);
}
float2 RotateCW45(float2 p)
{
return (p + float2(-p.y, p.x))*sqrt(0.5);
}
float2 RotateCCW45(float2 p)
{
return (p + float2(p.y, -p.x))*sqrt(0.5);
}
float2 RotateCW90(float2 p)
{
return float2(-p.y, p.x);
}
float2 RotateCCW90(float2 p)
{
return float2(p.y, -p.x);
}
//
// Distance operations
//
float Union(float a, float b)
{
return min(a, b);
}
float Intersection(float a, float b)
{
return max(a, b);
}
float Difference(float a, float b)
{
return max(a, -b);
}
float Xor(float a, float b)
{
return max(min(a, b), min(-a, -b));
}
//
// Half-space
//
float HalfSpace(float2 p, float2 normal)
{
return dot(p, normal);
}
float HalfSpaceStep(float2 p, float2 normal)
{
return step(HalfSpace(p, normal), 0.0);
}
float HalfSpaceSmoothStep(float2 p, float2 normal)
{
return InverseSmoothStep0(HalfSpace(p, normal));
}
float HalfSpaceSmoothStep(float2 p, float2 normal, float aa)
{
return InverseSmoothStep0(HalfSpace(p, normal), aa);
}
//
// Space segment
//
float SpaceSegment(float2 p, float angleRadians)
{
float2 rotatedP = RotateCW(p, angleRadians);
float cornerStep = step(p.y, 0.0)*step(rotatedP.y, 0.0);
float segmentStep = 1.0 - cornerStep;
float h1 = HalfSpace(p, float2(-1.0, 0.0))*segmentStep;
float h2 = HalfSpace(rotatedP, float2(1.0, 0.0))*segmentStep;
float segment = angleRadians > UNITY_PI ? Union(h1, h2) : Intersection(h1, h2);
float corner = -length(p)*cornerStep*sign(angleRadians - UNITY_PI);
return segment + corner;
}
float SpaceSegmentStep(float2 p, float angleRadians)
{
return step(SpaceSegment(p, angleRadians), 0.0);
}
float SpaceSegmentSmoothStep(float2 p, float angleRadians)
{
return InverseSmoothStep0(SpaceSegment(p, angleRadians));
}
float SpaceSegmentSmoothStep(float2 p, float angleRadians, float aa)
{
return InverseSmoothStep0(SpaceSegment(p, angleRadians), aa);
}
//
// Circle
//
float Circle(float2 p, float radius)
{
return length(p) - radius;
}
float CircleStep(float2 p, float radius)
{
return step(Circle(p, radius), 0.0);
}
float CircleSmoothStep(float2 p, float radius)
{
return InverseSmoothStep0(Circle(p, radius));
}
float CircleSmoothStep(float2 p, float radius, float aa)
{
return InverseSmoothStep0(Circle(p, radius), aa);
}
//
// Ring
//
float Ring(float2 p, float radius, float width)
{
return abs(length(p) - radius + width) - width;
}
float RingStep(float2 p, float radius, float width)
{
return step(Ring(p, radius, width), 0.0);
}
float RingSmoothStep(float2 p, float radius, float width)
{
return InverseSmoothStep0(Ring(p, radius, width));
}
float RingSmoothStep(float2 p, float radius, float width, float aa)
{
return InverseSmoothStep0(Ring(p, radius, width), aa);
}
//
// Cheap ellipse with inexact distance to poles
//
float EllipseCheap(float2 p, float2 size)
{
return (length(p/size) - 1.0)*min(size.x, size.y);
}
float EllipseCheapStep(float2 p, float2 size)
{
return step(EllipseCheap(p, size), 0.0);
}
float EllipseCheapSmoothStep(float2 p, float2 size)
{
return InverseSmoothStep0(EllipseCheap(p, size));
}
float EllipseCheapSmoothStep(float2 p, float2 size, float aa)
{
return InverseSmoothStep0(EllipseCheap(p, size), aa);
}
//
// Capsule
//
float Capsule(float2 p, float2 a, float2 b, float radius)
{
float2 toP = p - a;
float2 direction = b - a;
float h = saturate(dot(toP, direction)/dot(direction, direction));
return length(toP - direction*h) - radius;
}
float CapsuleStep(float2 p, float2 a, float2 b, float radius)
{
return step(Capsule(p, a, b, radius), 0.0);
}
float CapsuleSmoothStep(float2 p, float2 a, float2 b, float radius)
{
return InverseSmoothStep0(Capsule(p, a, b, radius));
}
float CapsuleSmoothStep(float2 p, float2 a, float2 b, float radius, float aa)
{
return InverseSmoothStep0(Capsule(p, a, b, radius), aa);
}
//
// Cheap rectangle with inexact distance to corners
//
float RectangleCheap(float2 p, float2 size)
{
float2 d = abs(p) - size;
return max(d.x, d.y);
}
float RectangleCheapStep(float2 p, float2 size)
{
return step(RectangleCheap(p, size), 0.0);
}
float RectangleCheapSmoothStep(float2 p, float2 size)
{
return InverseSmoothStep0(RectangleCheap(p, size));
}
float RectangleCheapSmoothStep(float2 p, float2 size, float aa)
{
return InverseSmoothStep0(RectangleCheap(p, size), aa);
}
//
// Rectangle
//
float Rectangle(float2 p, float2 size)
{
float2 d = abs(p) - size;
float inside = min(max(d.x, d.y), 0.0);
float outside = length(max(d, float2(0.0, 0.0)));
return inside + outside;
}
float RectangleStep(float2 p, float2 size)
{
return step(Rectangle(p, size), 0.0);
}
float RectangleSmoothStep(float2 p, float2 size)
{
return InverseSmoothStep0(Rectangle(p, size));
}
float RectangleSmoothStep(float2 p, float2 size, float aa)
{
return InverseSmoothStep0(Rectangle(p, size), aa);
}
//
// Rectangle frame
//
float RectangleFrame(float2 p, float2 size, float width)
{
float2 d = abs(p) - size;
float inside = min(max(d.x, d.y), 0.0);
float outside = length(max(d, float2(0.0, 0.0)));
return abs(inside + outside + width) - width;
}
float RectangleFrameStep(float2 p, float2 size, float width)
{
return step(RectangleFrame(p, size, width), 0.0);
}
float RectangleFrameSmoothStep(float2 p, float2 size, float width)
{
return InverseSmoothStep0(RectangleFrame(p, size, width));
}
float RectangleFrameSmoothStep(float2 p, float2 size, float width, float aa)
{
return InverseSmoothStep0(RectangleFrame(p, size, width), aa);
}
//
// Round rectangle
//
float RoundRectangle(float2 p, float2 size, float radius)
{
float2 d = abs(p) - size + float2(radius, radius);
float inside = min(max(d.x, d.y), 0.0) - radius;
float outside = length(max(d, float2(0.0, 0.0)));
return inside + outside;
}
float RoundRectangleStep(float2 p, float2 size, float radius)
{
return step(RoundRectangle(p, size, radius), 0.0);
}
float RoundRectangleSmoothStep(float2 p, float2 size, float radius)
{
return InverseSmoothStep0(RoundRectangle(p, size, radius));
}
float RoundRectangleSmoothStep(float2 p, float2 size, float radius, float aa)
{
return InverseSmoothStep0(RoundRectangle(p, size, radius), aa);
}
//
// Round rectangle frame
//
float RoundRectangleFrame(float2 p, float2 size, float width, float radius)
{
float2 d = abs(p) - size + float2(radius, radius);
float inside = min(max(d.x, d.y), 0.0) - radius;
float outside = length(max(d, float2(0.0, 0.0)));
return abs(inside + outside + width) - width;
}
float RoundRectangleFrameStep(float2 p, float2 size, float width, float radius)
{
return step(RoundRectangleFrame(p, size, width, radius), 0.0);
}
float RoundRectangleFrameSmoothStep(float2 p, float2 size, float width, float radius)
{
return InverseSmoothStep0(RoundRectangleFrame(p, size, width, radius));
}
float RoundRectangleFrameSmoothStep(float2 p, float2 size, float width, float radius, float aa)
{
return InverseSmoothStep0(RoundRectangleFrame(p, size, width, radius), aa);
}
//
// Cheap polygon with inexact distance to vertices
//
float PolygonCheap(float2 p, float vertices, float radius)
{
float segmentAngle = UNITY_TWO_PI/vertices;
float halfSegmentAngle = segmentAngle*0.5;
float angleRadians = atan2(p.x, p.y);
float repeat = fmod(abs(angleRadians), segmentAngle) - halfSegmentAngle;
float inradius = radius*cos(halfSegmentAngle);
float circle = length(p);
float y = cos(repeat)*circle - inradius;
return y;
}
float PolygonCheapStep(float2 p, float2 vertices, float radius)
{
return step(PolygonCheap(p, vertices, radius), 0.0);
}
float PolygonCheapSmoothStep(float2 p, float2 vertices, float radius)
{
return InverseSmoothStep0(PolygonCheap(p, vertices, radius));
}
float PolygonCheapSmoothStep(float2 p, float2 vertices, float radius, float aa)
{
return InverseSmoothStep0(PolygonCheap(p, vertices, radius), aa);
}
//
// Polygon
//
float Polygon(float2 p, float vertices, float radius)
{
float segmentAngle = UNITY_TWO_PI/vertices;
float halfSegmentAngle = segmentAngle*0.5;
float angleRadians = atan2(p.x, p.y);
float repeat = fmod(abs(angleRadians), segmentAngle) - halfSegmentAngle;
float inradius = radius*cos(halfSegmentAngle);
float circle = length(p);
float x = sin(repeat)*circle;
float y = cos(repeat)*circle - inradius;
float inside = min(y, 0.0);
float corner = radius*sin(halfSegmentAngle);
float outside = length(float2(max(abs(x) - corner, 0.0), y))*step(0.0, y);
return inside + outside;
}
float PolygonStep(float2 p, float2 vertices, float radius)
{
return step(Polygon(p, vertices, radius), 0.0);
}
float PolygonSmoothStep(float2 p, float2 vertices, float radius)
{
return InverseSmoothStep0(Polygon(p, vertices, radius));
}
float PolygonSmoothStep(float2 p, float2 vertices, float radius, float aa)
{
return InverseSmoothStep0(Polygon(p, vertices, radius), aa);
}
//
// Cheap star polygon with inexact distance to vertices
//
float StarPolygonCheap(float2 p, float vertices, float radius, float starAngle)
{
float segmentAngle = UNITY_TWO_PI/vertices;
float halfSegmentAngle = segmentAngle*0.5;
float angleRadians = atan2(p.x, p.y);
float repeat = abs(frac(angleRadians/segmentAngle - 0.5) - 0.5)*segmentAngle;
float circle = length(p);
float x = sin(repeat)*circle;
float y = cos(repeat)*circle - radius;
float uvRotation = halfSegmentAngle + starAngle;
y = cos(uvRotation)*y + sin(uvRotation)*x;
return y;
}
float StarPolygonCheapStep(float2 p, float2 vertices, float radius, float starAngle)
{
return step(StarPolygonCheap(p, vertices, radius, starAngle), 0.0);
}
float StarPolygonCheapSmoothStep(float2 p, float2 vertices, float radius, float starAngle)
{
return InverseSmoothStep0(StarPolygonCheap(p, vertices, radius, starAngle));
}
float StarPolygonCheapSmoothStep(float2 p, float2 vertices, float radius, float starAngle, float aa)
{
return InverseSmoothStep0(StarPolygonCheap(p, vertices, radius, starAngle), aa);
}
//
// Star polygon
//
float StarPolygon(float2 p, float vertices, float radius, float starAngle)
{
float segmentAngle = UNITY_TWO_PI/vertices;
float halfSegmentAngle = segmentAngle*0.5;
float angleRadians = atan2(p.x, p.y);
float repeat = abs(frac(angleRadians/segmentAngle - 0.5) - 0.5)*segmentAngle;
float circle = length(p);
float x = sin(repeat)*circle;
float y = cos(repeat)*circle - radius;
float uvRotation = halfSegmentAngle + starAngle;
float2 uv = cos(uvRotation)*float2(x, y) + sin(uvRotation)*float2(-y, x);
float corner = radius*sin(halfSegmentAngle)/cos(starAngle);
float inside = -length(float2(max(uv.x - corner, 0.0), uv.y))*step(0.0, -uv.y);
float outside = length(float2(min(uv.x, 0.0), uv.y))*step(0.0, uv.y);
return inside + outside;
}
float StarPolygonStep(float2 p, float2 vertices, float radius, float starAngle)
{
return step(StarPolygon(p, vertices, radius, starAngle), 0.0);
}
float StarPolygonSmoothStep(float2 p, float2 vertices, float radius, float starAngle)
{
return InverseSmoothStep0(StarPolygon(p, vertices, radius, starAngle));
}
float StarPolygonSmoothStep(float2 p, float2 vertices, float radius, float starAngle, float aa)
{
return InverseSmoothStep0(StarPolygon(p, vertices, radius, starAngle), aa);
}
#endif