Skip to content

Instantly share code, notes, and snippets.

@ayamflow
Created June 30, 2020 00:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ayamflow/ac1bc664f622ff0ce05b2a56bf40a4b1 to your computer and use it in GitHub Desktop.
Save ayamflow/ac1bc664f622ff0ce05b2a56bf40a4b1 to your computer and use it in GitHub Desktop.
Drawing/animating circle arc in glsl
const float PI = 3.141592653589793;
vec2 rotateUV(vec2 uv, float rotation)
{
float mid = 0.5;
return vec2(
cos(rotation) * (uv.x - mid) + sin(rotation) * (uv.y - mid) + mid,
cos(rotation) * (uv.y - mid) - sin(rotation) * (uv.x - mid) + mid
);
}
float halfSlice(vec2 uv, float angle) {
float circle = 1.0 - length(uv - 0.5);
circle = step(0.5, circle);
circle -= step(uv.x, 0.5);
circle -= step(rotateUV(uv, PI - angle).x, 0.5);
circle = clamp(circle, 0.0, 1.0);
return circle;
}
float slice(vec2 uv, float angle) {
if (angle <= PI) return halfSlice(uv, angle);
float remainingAngle = angle - PI;
vec2 ruv = rotateUV(uv, PI);
return halfSlice(ruv, remainingAngle) + halfSlice(uv, PI);
}
void main() {
float angle = (0.5 + 0.5 * sin(time)) * PI * 2.0;
float circle = slice(vUv, angle);
gl_FragColor = vec4(circle);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment