Search
Last updated
Unknown
Note Images are not representative of the base functions these are visual effects applied to the SDF output that I discuss
here!
> Circle
1
2
3
4
5
6
7
| float SDFCircle(in vec2 UV, in float Radius)
{
//UV scale from 0 to 1 to -1 to 1
//UV = 2 * (UV - 0.5);
return length(UV) - Radius;
}
|

> Box
1
2
3
4
5
6
7
8
9
10
| float SDFBox(in vec2 UV, in vec2 Size, in float Roundness)
{
//Move UV scale from 0 to 1 to -1 to 1
//UV = 2 * (UV - 0.5);
vec2 BoxUV = abs(UV) - Size + Roundness;
float OuterBox = length(max(BoxUV, 0.0));
float InnerBox = min(max(BoxUV.x, BoxUV.y), 0.0);
return OuterBox + InnerBox - Roundness;
}
|

> Equilateral Triangle
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| float SDFTriangle(in vec2 UV, in float BaseSize, in float Roundness)
{
//Move UV scale from 0 to 1 to -1 to 1
//UV = 2 * (UV - 0.5);
float Helper = sqrt(3.0);
float RoundedSize = BaseSize - (Roundness * BaseSize);
float CornerRadius = (1.0 / Helper) * Roundness * BaseSize;
vec2 TriUV = vec2(abs(UV.x) - RoundedSize, UV.y + RoundedSize/Helper);
if( TriUV.x + Helper * TriUV.y > 0.0)
{
TriUV = vec2(TriUV.x - Helper * TriUV.y, -Helper * TriUV.x - TriUV.y) / 2.0;
}
TriUV.x -= clamp( TriUV.x, -2.0 * RoundedSize, 0.0);
return -length(TriUV) * sign(TriUV.y) - CornerRadius;
}
|

> Hexagon
1
2
3
4
5
6
7
8
9
10
11
12
| float SDFHexagon(in vec2 UV, in float Radius, in float Roundness)
{
//UV scale from 0 to 1 to -1 to 1
//UV = 2 * (UV - 0.5);
vec3 Helper = vec3(sqrt(3.0) / -2.0, 0.5, 1.0 / sqrt(3.0));
float CornerRadius = Radius * -Helper.x * Roundness;
float RadiusOffset = Radius - CornerRadius; UV = abs(UV);
UV -= 2.0 * min(dot(Helper.xy, UV), 0.0) * Helper.xy;
UV -= vec2(clamp(UV.x, -Helper.z * RadiusOffset, Helper.z * RadiusOffset), RadiusOffset);
return (length(UV) * sign(UV.y)) - CornerRadius;
}
|
