Skip to content

Instantly share code, notes, and snippets.

@Zammy
Last active April 12, 2024 10:52
Show Gist options
  • Save Zammy/3a882be5d48dfee77334fde485766946 to your computer and use it in GitHub Desktop.
Save Zammy/3a882be5d48dfee77334fde485766946 to your computer and use it in GitHub Desktop.
Rectangle drawing function GLSL
//all params in normalized units
vec3 drawRect(in vec2 st,
in vec2 center,
in float width,
in float height,
in float thickness,
in vec3 fillColor,
in vec3 strokeColor)
{
vec3 color = vec3(0);
float halfWidth = width * .5;
float halfHeight = height * .5;
float halfTickness = thickness * .5;
vec2 bottomLeft = vec2(center.x - halfWidth, center.y - halfHeight);
vec2 topRight = vec2(center.x + halfWidth, center.y + halfHeight);
//STROKE
vec2 stroke = vec2(0.0);
stroke += step(bottomLeft-halfTickness, st) * (1.0 - step(bottomLeft+halfTickness, st));
stroke += step(topRight-halfTickness, st) * (1.0 - step(topRight+halfTickness, st));
vec2 strokeLimit = step(bottomLeft-halfTickness, st) * (1.0 - step(topRight+halfTickness, st));
stroke *= strokeLimit.x * strokeLimit.y;
color = mix (color, strokeColor, min(stroke.x + stroke.y, 1.0));
//
//FILL
vec2 fill = vec2(0.0);
fill += step(bottomLeft+halfTickness, st) * (1.0 - step(topRight-halfTickness, st));
vec2 fillLimit = step(bottomLeft+halfTickness, st) * (1.0 - step(topRight-halfTickness, st));
fill *= fillLimit.x * fillLimit.y;
color = mix (color, fillColor, min(fill.x + fill.y, 1.0));
//
return color;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment