MegaMorphLink.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. public enum MegaLinkSrc
  4. {
  5. Position,
  6. LocalPosition,
  7. Rotation,
  8. LocalRotation,
  9. Scale,
  10. DotRotation,
  11. Angle,
  12. RotationXY,
  13. RotationXZ,
  14. RotationYZ,
  15. }
  16. [System.Serializable]
  17. public class MegaMorphLinkDesc
  18. {
  19. public string name = "";
  20. public Transform target;
  21. public int channel = 0;
  22. public MegaAxis axis = MegaAxis.X;
  23. public MegaLinkSrc src = MegaLinkSrc.LocalRotation;
  24. public float min = 0.0f;
  25. public float max = 90.0f;
  26. public bool useCurve = false;
  27. public AnimationCurve curve = new AnimationCurve(new Keyframe(0.0f, 0.0f), new Keyframe(1.0f, 1.0f));
  28. public bool late;
  29. public bool active;
  30. public Quaternion rot;
  31. public float low = 0.0f;
  32. public float high = 1.0f;
  33. float Ang(Quaternion rotationA, Quaternion rotationB, MegaLinkSrc t)
  34. {
  35. Vector3 forwardA = rotationA * Vector3.forward;
  36. Vector3 forwardB = rotationB * Vector3.forward;
  37. float angleA = 0.0f;
  38. float angleB = 0.0f;
  39. switch ( t )
  40. {
  41. case MegaLinkSrc.RotationXY:
  42. angleA = Mathf.Atan2(forwardA.x, forwardA.y) * Mathf.Rad2Deg;
  43. angleB = Mathf.Atan2(forwardB.x, forwardB.y) * Mathf.Rad2Deg;
  44. break;
  45. case MegaLinkSrc.RotationXZ:
  46. angleA = Mathf.Atan2(forwardA.x, forwardA.z) * Mathf.Rad2Deg;
  47. angleB = Mathf.Atan2(forwardB.x, forwardB.z) * Mathf.Rad2Deg;
  48. break;
  49. case MegaLinkSrc.RotationYZ:
  50. angleA = Mathf.Atan2(forwardA.y, forwardA.z) * Mathf.Rad2Deg;
  51. angleB = Mathf.Atan2(forwardB.y, forwardB.z) * Mathf.Rad2Deg;
  52. break;
  53. }
  54. return Mathf.DeltaAngle(angleA, angleB);
  55. }
  56. public float GetVal()
  57. {
  58. float val = 0.0f;
  59. if ( target )
  60. {
  61. switch ( src )
  62. {
  63. case MegaLinkSrc.Position: val = target.position[(int)axis]; break;
  64. case MegaLinkSrc.Rotation: val = target.rotation.eulerAngles[(int)axis]; break;
  65. case MegaLinkSrc.LocalPosition: val = target.localPosition[(int)axis]; break;
  66. case MegaLinkSrc.LocalRotation: val = target.localRotation.eulerAngles[(int)axis]; break;
  67. case MegaLinkSrc.Scale: val = target.localScale[(int)axis]; break;
  68. case MegaLinkSrc.DotRotation: val = Quaternion.Dot(target.localRotation, rot); break;
  69. case MegaLinkSrc.Angle: val = Quaternion.Angle(target.localRotation, rot); break;
  70. case MegaLinkSrc.RotationXY: val = Ang(target.localRotation, rot, src); break;
  71. case MegaLinkSrc.RotationXZ: val = Ang(target.localRotation, rot, src); break;
  72. case MegaLinkSrc.RotationYZ: val = Ang(target.localRotation, rot, src); break;
  73. }
  74. }
  75. return val;
  76. }
  77. public void Update(MegaMorph morph, bool islate)
  78. {
  79. if ( active && late == islate ) //&& target )
  80. {
  81. float alpha = Mathf.Clamp01((GetVal() - min) / (max - min));
  82. if ( useCurve )
  83. alpha = curve.Evaluate(alpha);
  84. morph.SetPercentLim(channel, Mathf.Lerp(low, high, alpha)); // * 100.0f);
  85. }
  86. }
  87. }
  88. [ExecuteInEditMode]
  89. public class MegaMorphLink : MonoBehaviour
  90. {
  91. public MegaMorph morph;
  92. public List<MegaMorphLinkDesc> links = new List<MegaMorphLinkDesc>();
  93. void Start()
  94. {
  95. if ( !morph )
  96. morph = GetComponent<MegaMorph>();
  97. }
  98. void UpdateLinks(bool islate)
  99. {
  100. if ( morph )
  101. {
  102. for ( int i = 0; i < links.Count; i++ )
  103. {
  104. links[i].Update(morph, islate);
  105. }
  106. }
  107. }
  108. void LateUpdate()
  109. {
  110. UpdateLinks(true);
  111. }
  112. void Update()
  113. {
  114. UpdateLinks(false);
  115. }
  116. }