You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

37 lines
999 B

5 years ago
  1. using UnityEngine;
  2. public static class Bezier {
  3. public static Vector3 GetPoint (Vector3 p0, Vector3 p1, Vector3 p2, float t) {
  4. t = Mathf.Clamp01(t);
  5. float oneMinusT = 1f - t;
  6. return
  7. oneMinusT * oneMinusT * p0 +
  8. 2f * oneMinusT * t * p1 +
  9. t * t * p2;
  10. }
  11. public static Vector3 GetFirstDerivative (Vector3 p0, Vector3 p1, Vector3 p2, float t) {
  12. return
  13. 2f * (1f - t) * (p1 - p0) +
  14. 2f * t * (p2 - p1);
  15. }
  16. public static Vector3 GetPoint (Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3, float t) {
  17. t = Mathf.Clamp01(t);
  18. float OneMinusT = 1f - t;
  19. return
  20. OneMinusT * OneMinusT * OneMinusT * p0 +
  21. 3f * OneMinusT * OneMinusT * t * p1 +
  22. 3f * OneMinusT * t * t * p2 +
  23. t * t * t * p3;
  24. }
  25. public static Vector3 GetFirstDerivative (Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3, float t) {
  26. t = Mathf.Clamp01(t);
  27. float oneMinusT = 1f - t;
  28. return
  29. 3f * oneMinusT * oneMinusT * (p1 - p0) +
  30. 6f * oneMinusT * t * (p2 - p1) +
  31. 3f * t * t * (p3 - p2);
  32. }
  33. }