MegaMorphChan.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. [System.Serializable]
  5. public class MegaMorphChan //: ScriptableObject
  6. {
  7. public string mName = "Empty";
  8. public float Percent = 0.0f;
  9. public bool mActiveOverride = true;
  10. public bool mUseLimit = false;
  11. public float mSpinmax = 100.0f;
  12. public float mSpinmin = 0.0f;
  13. public Vector3[] mDeltas;
  14. public float mCurvature = 0.5f;
  15. public bool showparams = true;
  16. public bool showtargets = true;
  17. public List<MegaMorphTarget> mTargetCache;
  18. public MegaBezFloatKeyControl control = null;
  19. public int[] mapping;
  20. public bool cubic;
  21. public int[] morphedVerts;
  22. public Vector3[] oPoints;
  23. float speed = 0.0f;
  24. float targetPercent = 0.0f;
  25. public float weight = 1.0f;
  26. // for threaded version
  27. public float fChannelPercent;
  28. public int targ; //float lastPercent;
  29. public float fProgression;
  30. public int segment;
  31. public Vector3[] p1;
  32. public Vector3[] p2;
  33. public Vector3[] p3;
  34. public Vector3[] p4;
  35. public Vector3[] diff;
  36. static public void Copy(MegaMorphChan from, MegaMorphChan to)
  37. {
  38. to.mName = from.mName;
  39. to.Percent = from.Percent;
  40. to.mActiveOverride = from.mActiveOverride;
  41. to.mUseLimit = from.mUseLimit;
  42. to.mSpinmax = from.mSpinmax;
  43. to.mSpinmin = from.mSpinmin;
  44. to.mDeltas = from.mDeltas;
  45. to.mCurvature = from.mCurvature;
  46. to.showparams = from.showparams;
  47. to.showtargets = from.showtargets;
  48. to.mTargetCache = from.mTargetCache;
  49. to.control = from.control;
  50. to.mapping = from.mapping;
  51. to.cubic = from.cubic;
  52. to.morphedVerts = from.morphedVerts;
  53. to.oPoints = from.oPoints;
  54. to.speed = from.speed;
  55. to.targetPercent = from.targetPercent;
  56. to.fChannelPercent = from.fChannelPercent;
  57. to.targ = from.targ;
  58. to.fProgression = from.fProgression;
  59. to.segment = from.segment;
  60. to.p1 = from.p1;
  61. to.p2 = from.p2;
  62. to.p3 = from.p3;
  63. to.p4 = from.p4;
  64. to.diff = from.diff;
  65. to.weight = from.weight;
  66. }
  67. public void SetTarget(float target, float spd)
  68. {
  69. speed = spd;
  70. targetPercent = target;
  71. }
  72. public void UpdatePercent()
  73. {
  74. if ( speed != 0.0f )
  75. {
  76. if ( Percent < targetPercent )
  77. {
  78. Percent += speed * Time.deltaTime;
  79. if ( Percent >= targetPercent )
  80. {
  81. Percent = targetPercent;
  82. speed = 0.0f;
  83. }
  84. }
  85. else
  86. {
  87. Percent -= speed * Time.deltaTime;
  88. if ( Percent <= targetPercent )
  89. {
  90. Percent = targetPercent;
  91. speed = 0.0f;
  92. }
  93. }
  94. }
  95. }
  96. public float GetTargetPercent(int which)
  97. {
  98. if ( which < -1 || which >= mTargetCache.Count ) return 0.0f;
  99. if ( which == -1 ) return mTargetCache[0].percent; //mTargetPercent;
  100. return mTargetCache[which + 1].percent;
  101. }
  102. // needs to check each vert, if all targets equal opoint then unmorphed so remove
  103. public void CompressChannel()
  104. {
  105. }
  106. public void ResetPercent()
  107. {
  108. // Make a function to reset percents
  109. int num = mTargetCache.Count;
  110. for ( int i = 0; i < mTargetCache.Count; i++ )
  111. {
  112. mTargetCache[i].percent = ((float)(i + 1) / (float)num) * 100.0f;
  113. }
  114. }
  115. // delta is only used if we have a single target
  116. public void Rebuild(MegaMorph mp)
  117. {
  118. if ( mTargetCache != null && mTargetCache.Count > 0 && mp.oPoints != null && mTargetCache[0].points != null )
  119. {
  120. if ( mp.oPoints.Length == mTargetCache[0].points.Length )
  121. {
  122. //Debug.Log("oplen " + mp.oPoints.Length + " tclen " + mTargetCache[0].points.Length);
  123. mDeltas = new Vector3[mp.oPoints.Length];
  124. for ( int i = 0; i < mTargetCache[0].points.Length; i++ )
  125. {
  126. mDeltas[i] = (mTargetCache[0].points[i] - mp.oPoints[i]) / 100.0f;
  127. }
  128. }
  129. }
  130. }
  131. public MegaMorphTarget GetTarget(string name)
  132. {
  133. if ( mTargetCache == null )
  134. return null;
  135. for ( int i = 0; i < mTargetCache.Count; i++ )
  136. {
  137. if ( mTargetCache[i].name == name )
  138. return mTargetCache[i];
  139. }
  140. return null;
  141. }
  142. public void ChannelMapping(MegaMorph mr)
  143. {
  144. mapping = new int[mr.oPoints.Length];
  145. for ( int i = 0; i < mr.oPoints.Length; i++ )
  146. {
  147. mapping[i] = i;
  148. }
  149. }
  150. }