Created
December 9, 2021 06:24
-
-
Save heyomidk/46cf15b11ab7ffed4200df87d52c186b to your computer and use it in GitHub Desktop.
SDF - Dashed Circle (HLSL), source: https://www.shadertoy.com/view/7tyGWw
This file contains hidden or 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
float arcRadius = length(p); | |
// Compute the radial distance field coming out from the center of a torus | |
const float hw = (RadiusOuter-RadiusInner)*0.5; // half width from center of torus to edge | |
float radialDF = arcRadius - (RadiusOuter-hw); // creates a torus | |
radialDF = abs(radialDF)-hw; // give an inside and outside to the torus | |
// Compute the gradient along the length of the arc | |
float arcGradient = atan2(p.y,p.x)/6.283185; | |
arcGradient = frac(arcGradient*NumDashes); | |
arcGradient = abs((arcGradient-0.5)*2.0); // make the gradient a linear, continuous 0-1-0-1 repeating pattern | |
arcGradient = arcGradient-DashRatio; // split the arc into positive and negative sections | |
float theta = arcGradient * (3.1415927/NumDashes); // gets us a signed angle from the nearest dash edge, in radians | |
// figure out the point closest to us on the nearest dash edge | |
float s,c; | |
sincos(theta,s,c); | |
float edgeProjection = clamp(c*arcRadius,RadiusInner,RadiusOuter); // project point p but bound it between the innner and outer radii of the dash | |
float2 nearestEdgeP = mul(float2x2(c,-s,s,c),p) * edgeProjection/arcRadius; | |
float edgeDF = length(p-nearestEdgeP); | |
return (theta >0.0) ? edgeDF : | |
(radialDF>0.0) ? radialDF : | |
max(-edgeDF,radialDF); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment