Skip to content

Instantly share code, notes, and snippets.

@b-mq
Last active July 15, 2020 12:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save b-mq/f76a505af3a07f676ba6ef3d22664887 to your computer and use it in GitHub Desktop.
Save b-mq/f76a505af3a07f676ba6ef3d22664887 to your computer and use it in GitHub Desktop.
// simple glsl ellipse function based on circle form BookOfShaders (https://thebookofshaders.com/07/)
float ellipse(in vec2 st, in vec2 position, in float radius, in float feather, in vec2 aspectratio){
// receive bigger of both numbers from aspect ratio
float max_val = max(aspectratio.x, aspectratio.y);
// clamping the aspect ratio ensures that the ellipse will deform but won't scale
// scaling should only result via changing the radius
vec2 clamped_ar = aspectratio / max_val;
// invert to ensure that higher numbers result in more stretching
vec2 ar = 1.0 / clamped_ar;
// calculate distance from center point, like in circle function
vec2 dist = (st * ar) - (position * ar);
// create a smooth feather with total size of 'feather'
float ellipse = 1.0 - smoothstep(radius - feather * 0.5,
radius + feather * 0.5,
dot(dist,dist) * 4.0);
return ellipse;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment