MegaVertexAnimEditor.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. 
  2. using UnityEngine;
  3. using UnityEditor;
  4. using System.IO;
  5. using System.Collections.Generic;
  6. using System.Collections;
  7. // Support anim uvs here as well
  8. [CanEditMultipleObjects, CustomEditor(typeof(MegaVertexAnim))]
  9. public class MegaVertexAnimEditor : MegaModifierEditor //Editor
  10. {
  11. //bool showmodparams = true;
  12. static string lastpath = " ";
  13. public delegate bool ParseBinCallbackType(BinaryReader br, string id);
  14. public delegate void ParseClassCallbackType(string classname, BinaryReader br);
  15. MegaModifiers mods;
  16. List<MegaAnimatedVert> Verts = new List<MegaAnimatedVert>();
  17. void LoadVertexAnim()
  18. {
  19. MegaVertexAnim am = (MegaVertexAnim)target;
  20. mods = am.gameObject.GetComponent<MegaModifiers>();
  21. string filename = EditorUtility.OpenFilePanel("Vertex Animation File", lastpath, "mpc");
  22. if ( filename == null || filename.Length < 1 )
  23. return;
  24. lastpath = filename;
  25. // Clear what we have
  26. Verts.Clear();
  27. ParseFile(filename, AnimatedMeshCallback);
  28. am.Verts = Verts.ToArray();
  29. BitArray animated = new BitArray(mods.verts.Length);
  30. int count = 0;
  31. for ( int i = 0; i < Verts.Count; i++ )
  32. {
  33. for ( int v = 0; v < Verts[i].indices.Length; v++ )
  34. {
  35. if ( !animated[Verts[i].indices[v]] )
  36. {
  37. animated[Verts[i].indices[v]] = true;
  38. count++;
  39. }
  40. }
  41. }
  42. am.NoAnim = new int[mods.verts.Length - count];
  43. int index = 0;
  44. for ( int i = 0; i < animated.Count; i++ )
  45. {
  46. if ( !animated[i] )
  47. am.NoAnim[index++] = i;
  48. }
  49. am.maxtime = 0.0f;
  50. for ( int i = 0; i < Verts.Count; i++ )
  51. {
  52. float t = Verts[i].con.Times[Verts[i].con.Times.Length - 1];
  53. if ( t > am.maxtime )
  54. am.maxtime = t;
  55. }
  56. }
  57. void AnimatedMeshCallback(string classname, BinaryReader br)
  58. {
  59. switch ( classname )
  60. {
  61. case "AnimMesh": LoadAnimMesh(br); break;
  62. }
  63. }
  64. public void LoadAnimMesh(BinaryReader br)
  65. {
  66. MegaParse.Parse(br, ParseAnimMesh);
  67. }
  68. int[] FindVerts(Vector3 p)
  69. {
  70. List<int> indices = new List<int>();
  71. for ( int i = 0; i < mods.verts.Length; i++ )
  72. {
  73. float dist = Vector3.Distance(p, mods.verts[i]);
  74. if ( dist < 0.0001f ) //mods.verts[i].Equals(p) )
  75. indices.Add(i);
  76. }
  77. return indices.ToArray();
  78. }
  79. Vector3 Extents(Vector3[] verts, out Vector3 min, out Vector3 max)
  80. {
  81. Vector3 extent = Vector3.zero;
  82. min = Vector3.zero;
  83. max = Vector3.zero;
  84. if ( verts != null && verts.Length > 0 )
  85. {
  86. min = verts[0];
  87. max = verts[0];
  88. for ( int i = 1; i < verts.Length; i++ )
  89. {
  90. if ( verts[i].x < min.x ) min.x = verts[i].x;
  91. if ( verts[i].y < min.y ) min.y = verts[i].y;
  92. if ( verts[i].z < min.z ) min.z = verts[i].z;
  93. if ( verts[i].x > max.x ) max.x = verts[i].x;
  94. if ( verts[i].y > max.y ) max.y = verts[i].y;
  95. if ( verts[i].z > max.z ) max.z = verts[i].z;
  96. }
  97. extent = max - min;
  98. }
  99. return extent;
  100. }
  101. MegaAnimatedVert currentVert;
  102. float scl = 1.0f;
  103. public bool ParseAnimMesh(BinaryReader br, string id)
  104. {
  105. switch ( id )
  106. {
  107. case "Size":
  108. Vector3 sz = MegaParse.ReadP3(br);
  109. Vector3 min1,max1;
  110. Vector3 ex1 = Extents(mods.verts, out min1, out max1);
  111. int largest = 0;
  112. if ( sz.x > sz.y )
  113. {
  114. if ( sz.x > sz.z )
  115. largest = 0;
  116. else
  117. largest = 2;
  118. }
  119. else
  120. {
  121. if ( sz.y > sz.z )
  122. largest = 1;
  123. else
  124. largest = 2;
  125. }
  126. scl = ex1[largest] / sz[largest];
  127. break;
  128. case "V":
  129. Vector3 p = MegaParse.ReadP3(br) * scl;
  130. // Find all matching verts
  131. currentVert = new MegaAnimatedVert();
  132. currentVert.startVert = p;
  133. currentVert.indices = FindVerts(p);
  134. if ( currentVert.indices == null )
  135. Debug.Log("Error! No match found");
  136. Verts.Add(currentVert);
  137. break;
  138. case "Anim":
  139. //currentVert.con = MegaBezVector3KeyControl.LoadBezVector3KeyControl(br);
  140. currentVert.con = MegaParseBezVector3Control.LoadBezVector3KeyControl(br);
  141. currentVert.con.Scale(scl);
  142. break;
  143. default: return false;
  144. }
  145. return true;
  146. }
  147. public void ParseFile(string assetpath, ParseClassCallbackType cb)
  148. {
  149. FileStream fs = new FileStream(assetpath, FileMode.Open, FileAccess.Read, System.IO.FileShare.Read);
  150. BinaryReader br = new BinaryReader(fs);
  151. bool processing = true;
  152. while ( processing )
  153. {
  154. string classname = MegaParse.ReadString(br);
  155. if ( classname == "Done" )
  156. break;
  157. int chunkoff = br.ReadInt32();
  158. long fpos = fs.Position;
  159. cb(classname, br);
  160. fs.Position = fpos + chunkoff;
  161. }
  162. br.Close();
  163. }
  164. public override void OnInspectorGUI()
  165. {
  166. MegaVertexAnim am = (MegaVertexAnim)target;
  167. if ( GUILayout.Button("Import Vertex Anim File") )
  168. {
  169. LoadVertexAnim();
  170. EditorUtility.SetDirty(target);
  171. }
  172. // Basic mod stuff
  173. showmodparams = EditorGUILayout.Foldout(showmodparams, "Modifier Common Params");
  174. if ( showmodparams )
  175. {
  176. CommonModParamsBasic(am);
  177. #if false
  178. am.ModEnabled = EditorGUILayout.Toggle("Mod Enabled", am.ModEnabled);
  179. am.DisplayGizmo = EditorGUILayout.Toggle("Display Gizmo", am.DisplayGizmo);
  180. am.Order = EditorGUILayout.IntField("Order", am.Order);
  181. am.gizCol1 = EditorGUILayout.ColorField("Giz Col 1", am.gizCol1);
  182. am.gizCol2 = EditorGUILayout.ColorField("Giz Col 2", am.gizCol2);
  183. #endif
  184. }
  185. am.time = EditorGUILayout.FloatField("Time", am.time);
  186. am.maxtime = EditorGUILayout.FloatField("Loop Time", am.maxtime);
  187. am.animated = EditorGUILayout.Toggle("Animated", am.animated);
  188. am.speed = EditorGUILayout.FloatField("Speed", am.speed);
  189. am.LoopMode = (MegaRepeatMode)EditorGUILayout.EnumPopup("Loop Mode", am.LoopMode);
  190. am.blendMode = (MegaBlendAnimMode)EditorGUILayout.EnumPopup("Blend Mode", am.blendMode);
  191. if ( am.blendMode == MegaBlendAnimMode.Additive )
  192. am.weight = EditorGUILayout.FloatField("Weight", am.weight);
  193. if ( GUI.changed )
  194. EditorUtility.SetDirty(target);
  195. }
  196. }