Created
June 30, 2020 00:19
-
-
Save ayamflow/ac1bc664f622ff0ce05b2a56bf40a4b1 to your computer and use it in GitHub Desktop.
Drawing/animating circle arc in glsl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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