Skip to content

Instantly share code, notes, and snippets.

@benjamingorman
Last active March 8, 2023 07:32
Show Gist options
  • Save benjamingorman/840f42cca525ab2cd6bf to your computer and use it in GitHub Desktop.
Save benjamingorman/840f42cca525ab2cd6bf to your computer and use it in GitHub Desktop.
Shader - Circle and Rectangle functions
// returns 1.0 if inside circle
float disk(vec2 r, vec2 center, float radius) {
return 1.0 - smoothstep( radius-0.005, radius+0.005, length(r-center));
}
// returns 1.0 if inside the disk
float rectangle(vec2 r, vec2 bottomLeft, vec2 topRight) {
float ret;
float d = 0.005;
ret = smoothstep(bottomLeft.x-d, bottomLeft.x+d, r.x);
ret *= smoothstep(bottomLeft.y-d, bottomLeft.y+d, r.y);
ret *= 1.0 - smoothstep(topRight.y-d, topRight.y+d, r.y);
ret *= 1.0 - smoothstep(topRight.x-d, topRight.x+d, r.x);
return ret;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment