using System; using System.Collections.Generic; using System.Linq; using System.Text; using UnityEngine; namespace uc.Math { public static class Spline { public static class Bezier { public static Vector3 Position(Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3, float t) { // (1 - t) ^ 3 * A + 3 * (1 - t) ^ 2 * t * B + 3 * (1 - t) * t ^ 2 * C + t ^ 3 * D float _1mt = 1.0f - t, _1mt2 = _1mt * _1mt, t2 = t * t; return p1 * _1mt * _1mt2 + p0 * 3 * _1mt2 * t + p3 * 3 * _1mt * t2 + p2 * t2 * t; } public static Vector3 Tangent(Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3, float t) { // -3 * (A * (t - 1) ^ 2 + B * (-3 * t ^ 2 + 4 * t - 1) + t * (3 * C * t - 2 * C - D * t)) float _1mt = 1.0f - t, _1mt2 = _1mt * _1mt, t2 = t * t; return p1 * -3 * _1mt2 + p0 * (-6 * _1mt * t + 3 * _1mt2) + p3 * (6 * _1mt * t - 3 * t2) + p2 * 3 * t2; } public static Vector3 Normal(Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3, float t) { // -6 * (A * (t - 1) + B * (2 - 3 * t) + 3 * C * t - C - D * t) return -6 * (p1 * (1 - t) + p0 * (2 - 3 * t) + 3 * p3 * t - p3 - p2 * t); } } public static class CatmullRom { public static Vector3 Position(Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3, float t) { float t2 = t * t, t3 = t2 * t; return p1 * (1.5f * t3 - 2.5f * t2 + 1.0f) + p0 * (-0.5f * t3 + t2 - 0.5f * t) + p3 * (0.5f * t3 - 0.5f * t2) + p2 * (-1.5f * t3 + 2.0f * t2 + 0.5f * t); } public static Vector3 Tangent(Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3, float t) { float t2 = t * t; return p1 * (4.5f * t - 5.0f) * t + p0 * (-1.5f * t2 + 2.0f * t - 0.5f) + p3 * (1.5f * t - 1.0f) * t + p2 * (-4.5f * t2 + 4.0f * t + 0.5f); } public static Vector3 Normal(Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3, float t) { return p1 * (9.0f * t - 5.0f) - p0 * (2.0f - 3.0f * t) + 9.0f * p2 * t + 3.0f * p3 * t + 4.0f * p2 - p3; } } public static class Hermite { public static Vector3 Position(Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3, float t) { float t2 = t * t, t3 = t2 * t; return p1 * (2 * t3 - 3 * t2 + 1) + p0 * (t3 - 2 * t2 + t) + p3 * (t3 - t2) + p2 * (-2 * t3 + 3 * t2); } public static Vector3 Tangent(Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3, float t) { float t2 = t * t; return p1 * (6 * t2 - 6 * t) + p0 * (3 * t2 - 4 * t + 1) + p3 * (3 * t2 - 2 * t) + p2 * (-6 * t2 + 6 * t); } public static Vector3 Normal(Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3, float t) { return p1 * (12 * t - 6) + p0 * (6 * t - 4) + p3 * (6 * t - 2) + p2 * (-12 * t + 6); } } public static class KochanekBartels { public static Vector3 Position(Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3, float t) { float t2 = t * t, t3 = t2 * t; return p1 * (2 * t3 - 3 * t2 + 1) + p0 * (t3 - 2 * t2 + t) + p3 * (t3 - t2) + p2 * (-2 * t3 + 3 * t2); } public static Vector3 Tangent(Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3, float t) { float t2 = t * t; return p1 * (6 * t2 - 6 * t) + p0 * (3 * t2 - 4 * t + 1) + p3 * (3 * t2 - 2 * t) + p2 * (-6 * t2 + 6 * t); } public static Vector3 Normal(Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3, float t) { return p1 * (12 * t - 6) + p0 * (6 * t - 4) + p3 * (6 * t - 2) + p2 * (-12 * t + 6); } } } }