Skip to content

Instantly share code, notes, and snippets.

@MateSteinforth
Last active March 16, 2024 19:38
Show Gist options
  • Select an option

  • Save MateSteinforth/93d3447f889b253e06ad6a52791efa22 to your computer and use it in GitHub Desktop.

Select an option

Save MateSteinforth/93d3447f889b253e06ad6a52791efa22 to your computer and use it in GitHub Desktop.
Rounded Box Outline SDF in SparkSL
//==============================================================================
// Rounded Box Outline SDF
//==============================================================================
float roundedBoxSDF(vec2 CenterPosition, vec2 Size, float Radius)
{
return length(max(abs(CenterPosition)-Size+Radius,0.0))-Radius;
}
float roundBoxOutline(vec2 size, float thickness, float radius)
{
vec2 uv = fragment(std::getVertexTexCoord());
float edgeSoftness = 0.002;
float distance = roundedBoxSDF(uv - vec2(0.5), size/2.0, radius);
float smoothedAlpha = 1.0 - smoothstep(-edgeSoftness, edgeSoftness, abs(distance) - thickness);
return smoothedAlpha;
}
vec4 main(vec2 size, float radius) {
return vec4(roundBoxOutline(size, 0.005, radius), 0., 0., 1.);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment