Skip to content

Instantly share code, notes, and snippets.

@loopyd
Last active December 9, 2022 09:36
Show Gist options
  • Save loopyd/6f649facbddfb30dd0fb6b33d997fd4a to your computer and use it in GitHub Desktop.
Save loopyd/6f649facbddfb30dd0fb6b33d997fd4a to your computer and use it in GitHub Desktop.
[GLSL] Catmull-rom as expressed by ChatGPT
// As expressed by ChatGPT -> https://openai.com/blog/chatgpt/
// Calculates a point along a Catmull-Rom spline
// given a set of control points, a tension parameter,
// and a step value in the range [0, 1].
vec2 catmullRom(vec2 p0, vec2 p1, vec2 p2, vec2 p3, float t, float tension) {
vec2 a = 0.5 * (tension * (p2 - p0) + 2.0 * p1 + tension * (p3 - p1)) * t * t * t +
(2.0 * tension * p0 - 3.0 * tension * p1 + (tension + 2.0) * p2 - tension * p3) * t * t +
(tension * p1 + p3 - tension * p2) * t +
2.0 * p1;
return a;
}
// Calculate the tangent with tension taken under account
vec2 catmullRomTangent(vec2 p0, vec2 p1, vec2 p2, vec2 p3, float t, float tension) {
vec2 a = 3.0 * tension * (p2 - p0) * t * t +
2.0 * (2.0 * tension * p0 - 3.0 * tension * p1 + (tension + 2.0) * p2 - tension * p3) * t +
tension * (p1 - p3) +
2.0 * p1;
return a;
}
// Calculate the normal using the tangent
vec2 catmullRomNormal(vec2 p0, vec2 p1, vec2 p2, vec2 p3, float t, float tension) {
vec2 tangent = catmullRomTangent(p0, p1, p2, p3, t, tension);
vec2 normal = vec2(-tangent.y, tangent.x);
return normal;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment