SplineUtility.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class SplineUtility
  5. {
  6. public static void CalculateTangents(Vector3 point, Vector3 prevPoint, Vector3 nextPoint, Vector3 forward, float scale, out Vector3 rightTangent, out Vector3 leftTangent)
  7. {
  8. Vector3 v1 = (prevPoint - point).normalized;
  9. Vector3 v2 = (nextPoint - point).normalized;
  10. Vector3 v3 = v1 + v2;
  11. Vector3 cross = forward;
  12. if (prevPoint != nextPoint)
  13. {
  14. bool colinear = Mathf.Abs(v1.x * v2.y - v1.y * v2.x + v1.x * v2.z - v1.z * v2.x + v1.y * v2.z - v1.z * v2.y) < 0.01f;
  15. if (colinear)
  16. {
  17. rightTangent = v2 * scale;
  18. leftTangent = v1 * scale;
  19. return;
  20. }
  21. cross = Vector3.Cross(v1, v2);
  22. }
  23. rightTangent = Vector3.Cross(cross, v3).normalized * scale;
  24. leftTangent = -rightTangent;
  25. }
  26. public static int NextIndex(int index, int pointCount)
  27. {
  28. return Mod(index + 1, pointCount);
  29. }
  30. public static int PreviousIndex(int index, int pointCount)
  31. {
  32. return Mod(index - 1, pointCount);
  33. }
  34. private static int Mod(int x, int m)
  35. {
  36. int r = x % m;
  37. return r < 0 ? r + m : r;
  38. }
  39. }