MegaVertexAnim.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. [System.Serializable]
  5. public class MegaAnimatedVert
  6. {
  7. public int[] indices;
  8. public Vector3 startVert;
  9. public MegaBezVector3KeyControl con;
  10. }
  11. // Should add multiple channels or animation to save copys
  12. [AddComponentMenu("Modifiers/Vertex Anim")]
  13. public class MegaVertexAnim : MegaModifier
  14. {
  15. public float time = 0.0f;
  16. public bool animated = false;
  17. public float speed = 1.0f;
  18. public float maxtime = 4.0f;
  19. public int[] NoAnim;
  20. public float weight = 1.0f;
  21. public MegaAnimatedVert[] Verts;
  22. float t;
  23. public MegaBlendAnimMode blendMode = MegaBlendAnimMode.Additive;
  24. public override string ModName() { return "AnimatedMesh"; }
  25. public override string GetHelpURL() { return "?page_id=1350"; }
  26. void Replace(MegaModifiers mc, int startvert, int endvert)
  27. {
  28. for ( int i = startvert; i < endvert; i++ )
  29. {
  30. MegaBezVector3KeyControl bc = (MegaBezVector3KeyControl)Verts[i].con;
  31. Vector3 off = bc.GetVector3(t);
  32. // ******* We must have duplicate verts in the indices array, so check that, if so same will apply to pc mod
  33. for ( int v = 0; v < Verts[i].indices.Length; v++ )
  34. sverts[Verts[i].indices[v]] = off;
  35. }
  36. }
  37. void ReplaceWeighted(MegaModifiers mc, int startvert, int endvert)
  38. {
  39. for ( int i = startvert; i < endvert; i++ )
  40. {
  41. MegaBezVector3KeyControl bc = (MegaBezVector3KeyControl)Verts[i].con;
  42. Vector3 off = bc.GetVector3(t);
  43. float w = mc.selection[Verts[i].indices[0]] * weight; //[wc];
  44. Vector3 p1 = verts[Verts[i].indices[0]];
  45. off = p1 + ((off - p1) * w);
  46. for ( int v = 0; v < Verts[i].indices.Length; v++ )
  47. sverts[Verts[i].indices[v]] = off;
  48. }
  49. }
  50. void Additive(MegaModifiers mc, int startvert, int endvert)
  51. {
  52. for ( int i = startvert; i < endvert; i++ )
  53. {
  54. MegaBezVector3KeyControl bc = (MegaBezVector3KeyControl)Verts[i].con;
  55. Vector3 basep = mc.verts[Verts[i].indices[0]];
  56. Vector3 off = bc.GetVector3(t) - basep;
  57. off = verts[Verts[i].indices[0]] + (off * weight);
  58. for ( int v = 0; v < Verts[i].indices.Length; v++ )
  59. {
  60. int idx = Verts[i].indices[v];
  61. sverts[idx] = off;
  62. }
  63. }
  64. }
  65. void AdditiveWeighted(MegaModifiers mc, int startvert, int endvert)
  66. {
  67. for ( int i = startvert; i < endvert; i++ )
  68. {
  69. MegaBezVector3KeyControl bc = (MegaBezVector3KeyControl)Verts[i].con;
  70. Vector3 basep = mc.verts[Verts[i].indices[0]];
  71. Vector3 off = bc.GetVector3(t) - basep;
  72. float w = mc.selection[Verts[i].indices[0]] * weight; //[wc];
  73. Vector3 p1 = verts[Verts[i].indices[0]];
  74. off = p1 + ((off - p1) * w);
  75. for ( int v = 0; v < Verts[i].indices.Length; v++ )
  76. {
  77. int idx = Verts[i].indices[v];
  78. sverts[idx] = off;
  79. }
  80. }
  81. }
  82. public override void Modify(MegaModifiers mc)
  83. {
  84. switch ( blendMode )
  85. {
  86. case MegaBlendAnimMode.Additive: Additive(mc, 0, Verts.Length); break;
  87. case MegaBlendAnimMode.Replace: Replace(mc, 0, Verts.Length); break;
  88. }
  89. if ( NoAnim != null )
  90. {
  91. for ( int i = 0; i < NoAnim.Length; i++ )
  92. {
  93. int index = NoAnim[i];
  94. sverts[index] = verts[index];
  95. }
  96. }
  97. }
  98. public MegaRepeatMode LoopMode = MegaRepeatMode.PingPong;
  99. public override bool ModLateUpdate(MegaModContext mc)
  100. {
  101. if ( animated )
  102. {
  103. if ( Application.isPlaying )
  104. time += Time.deltaTime * speed;
  105. }
  106. switch ( LoopMode )
  107. {
  108. case MegaRepeatMode.Loop: t = Mathf.Repeat(time, maxtime); break;
  109. case MegaRepeatMode.PingPong: t = Mathf.PingPong(time, maxtime); break;
  110. case MegaRepeatMode.Clamp: t = Mathf.Clamp(time, 0.0f, maxtime); break;
  111. }
  112. return Prepare(mc);
  113. }
  114. public override bool Prepare(MegaModContext mc)
  115. {
  116. return true;
  117. }
  118. public override void DoWork(MegaModifiers mc, int index, int start, int end, int cores)
  119. {
  120. ModifyCompressedMT(mc, index, cores);
  121. }
  122. public void ModifyCompressedMT(MegaModifiers mc, int tindex, int cores)
  123. {
  124. int step = NoAnim.Length / cores;
  125. int startvert = (tindex * step);
  126. int endvert = startvert + step;
  127. if ( tindex == cores - 1 )
  128. endvert = NoAnim.Length;
  129. if ( NoAnim != null )
  130. {
  131. for ( int i = startvert; i < endvert; i++ )
  132. {
  133. int index = NoAnim[i];
  134. sverts[index] = verts[index];
  135. }
  136. }
  137. step = Verts.Length / cores;
  138. startvert = (tindex * step);
  139. endvert = startvert + step;
  140. if ( tindex == cores - 1 )
  141. endvert = Verts.Length;
  142. switch ( blendMode )
  143. {
  144. case MegaBlendAnimMode.Additive: Additive(mc, startvert, endvert); break;
  145. case MegaBlendAnimMode.Replace: Replace(mc, startvert, endvert); break;
  146. }
  147. }
  148. }