Created
March 8, 2019 23:56
-
-
Save hakanai/2d433382ecab6cb48103dccc01fcbde5 to your computer and use it in GitHub Desktop.
Signed distance function for an arc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Distance from a point in 2D space to an arc starting at arc_r on the X axis | |
// and rotating through arc_theta in the positive direction. | |
float sdArc(float2 p, float arc_r, float arc_theta) | |
{ | |
float p_theta = atan2(p.y, p.x); | |
if (p_theta < 0.0) | |
{ | |
p_theta += UNITY_TWO_PI; | |
} | |
if (p_theta >= 0 && p_theta <= arc_theta) | |
{ | |
// Distance to intersection between ray from origin to point and arc | |
return abs(length(p) - arc_r); | |
} | |
else | |
{ | |
// Distance to starting point of arc | |
float d1 = length(p - float2(arc_r, 0)); | |
// Distance to ending point of arc | |
float d2 = length(p - PolarToCartesian(arc_r, arc_theta)); | |
return min(d1, d2); | |
} | |
} | |
// Distance from a point in 3D space to an arc starting at arc_r on the X axis | |
// and rotating through arc_theta in the positive direction on the Y axis. | |
// The thickness of the line can be provided as well at the moment, | |
// since it ended up commonly used at the caller anyway. | |
float sdArc(float3 p, float arc_r, float arc_theta, float line_r) | |
{ | |
return length(float2(sdArc(p.xy, arc_r, arc_theta), p.z)) - line_r; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment