MegaBezVector3KeyControl.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. using UnityEngine;
  2. using System.IO;
  3. [System.Serializable]
  4. public class MegaBezVector3Key // Should be derived from Key
  5. {
  6. public Vector3 val;
  7. public Vector3 intan;
  8. public Vector3 outtan;
  9. public Vector3 coef1;
  10. public Vector3 coef2;
  11. public Vector3 coef3;
  12. }
  13. [System.Serializable]
  14. public class MegaBezVector3KeyControl : MegaControl
  15. {
  16. public MegaBezVector3Key[] Keys;
  17. private const float SCALE = 4800.0f;
  18. public Vector3 f;
  19. public void Scale(float scl)
  20. {
  21. for ( int i = 0; i < Keys.Length; i++ )
  22. {
  23. Keys[i].val *= scl;
  24. Keys[i].intan *= scl;
  25. Keys[i].outtan *= scl;
  26. }
  27. InitKeys();
  28. }
  29. public void Scale(Vector3 scl)
  30. {
  31. for ( int i = 0; i < Keys.Length; i++ )
  32. {
  33. Keys[i].val.x *= scl.x;
  34. Keys[i].val.y *= scl.y;
  35. Keys[i].val.z *= scl.z;
  36. Keys[i].intan.x *= scl.x;
  37. Keys[i].intan.y *= scl.y;
  38. Keys[i].intan.z *= scl.z;
  39. Keys[i].outtan.x *= scl.x;
  40. Keys[i].outtan.y *= scl.y;
  41. Keys[i].outtan.z *= scl.z;
  42. }
  43. InitKeys();
  44. }
  45. public void Move(Vector3 scl)
  46. {
  47. for ( int i = 0; i < Keys.Length; i++ )
  48. {
  49. Keys[i].val += scl;
  50. Keys[i].intan += scl;
  51. Keys[i].outtan += scl;
  52. }
  53. InitKeys();
  54. }
  55. public void Rotate(Matrix4x4 tm)
  56. {
  57. for ( int i = 0; i < Keys.Length; i++ )
  58. {
  59. Keys[i].val = tm.MultiplyPoint3x4(Keys[i].val);
  60. Keys[i].intan = tm.MultiplyPoint3x4(Keys[i].intan);
  61. Keys[i].outtan = tm.MultiplyPoint3x4(Keys[i].outtan);
  62. }
  63. InitKeys();
  64. }
  65. public void InitKeys()
  66. {
  67. for ( int i = 0; i < Keys.Length - 1; i++ )
  68. {
  69. float dt = Times[i + 1] - Times[i];
  70. Vector3 hout = Keys[i].val + (Keys[i].outtan * SCALE) * (dt / 3.0f);
  71. Vector3 hin = Keys[i + 1].val + (Keys[i + 1].intan * SCALE) * (dt / 3.0f);
  72. Keys[i].coef1 = Keys[i + 1].val + 3.0f * (hout - hin) - Keys[i].val;
  73. Keys[i].coef2 = 3.0f * (hin - 2.0f * hout + Keys[i].val);
  74. Keys[i].coef3 = 3.0f * (hout - Keys[i].val);
  75. }
  76. }
  77. public void Interp(float alpha, int key)
  78. {
  79. if ( alpha == 0.0f )
  80. f = Keys[key].val;
  81. else
  82. {
  83. if ( alpha == 1.0f )
  84. f = Keys[key + 1].val;
  85. else
  86. {
  87. float tp2 = alpha * alpha;
  88. float tp3 = tp2 * alpha;
  89. f.x = Keys[key].coef1.x * tp3 + Keys[key].coef2.x * tp2 + Keys[key].coef3.x * alpha + Keys[key].val.x;
  90. f.y = Keys[key].coef1.y * tp3 + Keys[key].coef2.y * tp2 + Keys[key].coef3.y * alpha + Keys[key].val.y;
  91. f.z = Keys[key].coef1.z * tp3 + Keys[key].coef2.z * tp2 + Keys[key].coef3.z * alpha + Keys[key].val.z;
  92. }
  93. }
  94. }
  95. public override Vector3 GetVector3(float t)
  96. {
  97. if ( Times.Length == 1 )
  98. {
  99. return Keys[0].val;
  100. }
  101. int key = GetKey(t);
  102. float alpha = (t - Times[key]) / (Times[key + 1] - Times[key]);
  103. if ( alpha < 0.0f )
  104. alpha = 0.0f;
  105. else
  106. {
  107. if ( alpha > 1.0f )
  108. alpha = 1.0f;
  109. }
  110. // Do ease and hermite here maybe
  111. Interp(alpha, key);
  112. lastkey = key;
  113. lasttime = t;
  114. return f;
  115. }
  116. }