Last active
July 3, 2020 10:52
-
-
Save korchoon/1423459e3a2248a02ce0ff6853ac0471 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
static void CatmullRom(List<float3> input, List<float3> result) { | |
result.Clear(); | |
if (input.Count <= 2) { | |
foreach (var p in input) result.Add(p); | |
return; | |
} | |
var count = input.Count; | |
// first curve | |
CatmullRom(input[0], input[0], input[1], input[2]); | |
for (var i = 0; i + 3 < count; i++) | |
CatmullRom(input[i], input[i + 1], input[i + 2], input[i + 3]); | |
// last curve | |
CatmullRom(input[count - 3], input[count - 2], input[count - 1], input[count - 1]); | |
void CatmullRom(float3 p0, float3 p1, float3 p2, float3 p3) { | |
var c0 = p1; | |
var c1 = (-p0 + 6 * p1 + 1 * p2) * (1 / 12.0f); | |
var c2 = (p1 + 6 * p2 - p3) * (1 / 12.0f); | |
var c3 = p2; | |
Bezier(c0, c1, c2, c3); | |
} | |
void Bezier(float3 p0, float3 p1, float3 p2, float3 p3) { | |
const float segments = 12f; | |
for (int i = 1; i <= segments; i++) { | |
var t = i / segments; | |
var p = EvaluateCubicBezier(p0, p1, p2, p3, t); | |
result.Add(p); | |
} | |
} | |
float3 EvaluateCubicBezier(float3 p0, float3 p1, float3 p2, float3 p3, float t) { | |
t = math.clamp(t, 0f, 1f); // todo ? | |
var tr = 1 - t; | |
return tr * tr * tr * p0 + 3 * tr * tr * t * p1 + 3 * tr * t * t * p2 + t * t * t * p3; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment