Skip to content

Instantly share code, notes, and snippets.

@yiwenl
Last active June 10, 2024 10:55
Show Gist options
  • Save yiwenl/bb87c2d7df8bc735dab017c808a381ab to your computer and use it in GitHub Desktop.
Save yiwenl/bb87c2d7df8bc735dab017c808a381ab to your computer and use it in GitHub Desktop.
Bezier curve in GLSL
// bezier curve with 2 control points
// A is the starting point, B, C are the control points, D is the destination
// t from 0 ~ 1
vec3 bezier(vec3 A, vec3 B, vec3 C, vec3 D, float t) {
vec3 E = mix(A, B, t);
vec3 F = mix(B, C, t);
vec3 G = mix(C, D, t);
vec3 H = mix(E, F, t);
vec3 I = mix(F, G, t);
vec3 P = mix(H, I, t);
return P;
}
// bezier curve with 3 control points
// A is the starting point, B, C, D are the control points, E is the destination
// t from 0 ~ 1
vec3 bezier(vec3 A, vec3 B, vec3 C, vec3 D, vec3 E, float t) {
vec3 A1 = mix(A, B, t);
vec3 B1 = mix(B, C, t);
vec3 C1 = mix(C, D, t);
vec3 D1 = mix(D, E, t);
vec3 A2 = mix(A1, B1, t);
vec3 B2 = mix(B1, C1, t);
vec3 C2 = mix(C1, D1, t);
vec3 A3 = mix(A2, B2, t);
vec3 B3 = mix(B2, C2, t);
vec3 P = mix(A3, B3, t);
return P;
}
// bezier curve with 4 control points
// A is the starting point, B, C, D, E are the control points, F is the destination
// t from 0 ~ 1
vec3 bezier(vec3 A, vec3 B, vec3 C, vec3 D, vec3 E, vec3 F, float t) {
vec3 A1 = mix(A, B, t);
vec3 B1 = mix(B, C, t);
vec3 C1 = mix(C, D, t);
vec3 D1 = mix(D, E, t);
vec3 E1 = mix(E, F, t);
vec3 A2 = mix(A1, B1, t);
vec3 B2 = mix(B1, C1, t);
vec3 C2 = mix(C1, D1, t);
vec3 D2 = mix(D1, E1, t);
vec3 A3 = mix(A2, B2, t);
vec3 B3 = mix(B2, C2, t);
vec3 C3 = mix(C2, D2, t);
vec3 A4 = mix(A3, B3, t);
vec3 B4 = mix(B3, C3, t);
vec3 P = mix(A4, B4, t);
return P;
}
// bezier curve with 5 control points
// A is the starting point, B, C, D, E, F are the control points, G is the destination
// t from 0 ~ 1
vec3 bezier(vec3 A, vec3 B, vec3 C, vec3 D, vec3 E, vec3 F, vec3 G, float t) {
vec3 A1 = mix(A, B, t);
vec3 B1 = mix(B, C, t);
vec3 C1 = mix(C, D, t);
vec3 D1 = mix(D, E, t);
vec3 E1 = mix(E, F, t);
vec3 F1 = mix(F, G, t);
vec3 A2 = mix(A1, B1, t);
vec3 B2 = mix(B1, C1, t);
vec3 C2 = mix(C1, D1, t);
vec3 D2 = mix(D1, E1, t);
vec3 E2 = mix(E1, F1, t);
vec3 A3 = mix(A2, B2, t);
vec3 B3 = mix(B2, C2, t);
vec3 C3 = mix(C2, D2, t);
vec3 D3 = mix(D2, E2, t);
vec3 A4 = mix(A3, B3, t);
vec3 B4 = mix(B3, C3, t);
vec3 C4 = mix(C3, D3, t);
vec3 A5 = mix(A4, B4, t);
vec3 B5 = mix(B4, C4, t);
vec3 P = mix(A5, B5, t);
return P;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment