Bezier.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // --------------------------------------------------------------------------------------------------------------------
  2. // <copyright file="Bezier.cs" company="Exit Games GmbH">
  3. // Part of: Photon Unity Networking Demos
  4. // </copyright>
  5. // <summary>
  6. // Original: http://catlikecoding.com/unity/tutorials/curves-and-splines/
  7. // Used in SlotRacer Demo
  8. // </summary>
  9. // <author>developer@exitgames.com</author>
  10. // --------------------------------------------------------------------------------------------------------------------
  11. using UnityEngine;
  12. namespace Photon.Pun.Demo.SlotRacer.Utils
  13. {
  14. public static class Bezier
  15. {
  16. public static Vector3 GetPoint(Vector3 p0, Vector3 p1, Vector3 p2, float t)
  17. {
  18. t = Mathf.Clamp01(t);
  19. float oneMinusT = 1f - t;
  20. return
  21. oneMinusT * oneMinusT * p0 +
  22. 2f * oneMinusT * t * p1 +
  23. t * t * p2;
  24. }
  25. public static Vector3 GetFirstDerivative(Vector3 p0, Vector3 p1, Vector3 p2, float t)
  26. {
  27. return
  28. 2f * (1f - t) * (p1 - p0) +
  29. 2f * t * (p2 - p1);
  30. }
  31. public static Vector3 GetPoint(Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3, float t)
  32. {
  33. t = Mathf.Clamp01(t);
  34. float OneMinusT = 1f - t;
  35. return
  36. OneMinusT * OneMinusT * OneMinusT * p0 +
  37. 3f * OneMinusT * OneMinusT * t * p1 +
  38. 3f * OneMinusT * t * t * p2 +
  39. t * t * t * p3;
  40. }
  41. public static Vector3 GetFirstDerivative(Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3, float t)
  42. {
  43. t = Mathf.Clamp01(t);
  44. float oneMinusT = 1f - t;
  45. return
  46. 3f * oneMinusT * oneMinusT * (p1 - p0) +
  47. 6f * oneMinusT * t * (p2 - p1) +
  48. 3f * t * t * (p3 - p2);
  49. }
  50. }
  51. }