Skip to content

Commit 0132043

Browse files
committed
builtin shape sdfs
1 parent fccee70 commit 0132043

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
namespace nbl
2+
{
3+
namespace hlsl
4+
{
5+
namespace shapes
6+
{
7+
struct Circle_t
8+
{
9+
float2 center;
10+
float radius;
11+
12+
Circle_t(float2 center, float radius) :
13+
center(center),
14+
radius(radius)
15+
{}
16+
17+
float getSignedDistance(float2 p)
18+
{
19+
return distance(p, center) - radius;
20+
}
21+
};
22+
}
23+
}
24+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
namespace nbl
2+
{
3+
namespace hlsl
4+
{
5+
namespace shapes
6+
{
7+
struct Line_t
8+
{
9+
float2 start, end;
10+
11+
// https://www.shadertoy.com/view/stcfzn with modifications
12+
float getSignedDistance(float2 p, float thickness)
13+
{
14+
const float l = length(end - start);
15+
const float2 d = (end - start) / l;
16+
float2 q = p - (start + end) * 0.5;
17+
q = mul(float2x2(d.x, d.y, -d.y, d.x), q);
18+
q = abs(q) - float2(l * 0.5, thickness);
19+
return length(max(q, 0.0)) + min(max(q.x, q.y), 0.0);
20+
}
21+
};
22+
}
23+
}
24+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <nbl/builtin/hlsl/shapes/line.hlsl>
2+
#include <nbl/builtin/hlsl/shapes/circle.hlsl>
3+
4+
namespace nbl
5+
{
6+
namespace hlsl
7+
{
8+
namespace shapes
9+
{
10+
struct RoundedLine_t
11+
{
12+
float2 start, end;
13+
14+
float getSignedDistance(float2 p, float thickness)
15+
{
16+
Circle_t startCircle = {start, thickness};
17+
Circle_t endCircle = {end, thickness};
18+
Line_t mainLine = {start, end};
19+
const float startCircleSD = startCircle.getSignedDistance(p);
20+
const float endCircleSD = endCircle.getSignedDistance(p);
21+
const float lineSD = mainLine.getSignedDistance(p, thickness);
22+
return min(lineSD, min(startCircleSD, endCircleSD));
23+
}
24+
};
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)