BabylonExporter.Animation.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. using System;
  2. using System.Collections.Generic;
  3. using Autodesk.Max;
  4. using BabylonExport.Entities;
  5. namespace Max2Babylon
  6. {
  7. partial class BabylonExporter
  8. {
  9. const int Ticks = 160;
  10. private static BabylonAnimationKey GenerateFloatFunc(int index, IIKeyControl keyControl)
  11. {
  12. var key = Loader.Global.ILinFloatKey.Create();
  13. keyControl.GetKey(index, key);
  14. return new BabylonAnimationKey
  15. {
  16. frame = key.Time / Ticks,
  17. values = new []{key.Val}
  18. };
  19. }
  20. private static bool ExportFloatController(IControl control, string property, List<BabylonAnimation> animations)
  21. {
  22. return ExportController(control, property, animations, 0x2001, BabylonAnimation.DataType.Float, GenerateFloatFunc);
  23. }
  24. private static bool ExportQuaternionController(IControl control, string property, List<BabylonAnimation> animations)
  25. {
  26. IQuat previousQuat = null;
  27. return ExportController(control, property, animations, 0x2003, BabylonAnimation.DataType.Quaternion,
  28. (index, keyControl) =>
  29. {
  30. var key = Loader.Global.ILinRotKey.Create();
  31. keyControl.GetKey(index, key);
  32. var newQuat = key.Val;
  33. if (index > 0)
  34. {
  35. newQuat = previousQuat.Multiply(newQuat);
  36. }
  37. previousQuat = newQuat;
  38. return new BabylonAnimationKey
  39. {
  40. frame = key.Time / Ticks,
  41. values = newQuat.ToArray()
  42. };
  43. });
  44. }
  45. private static bool ExportVector3Controller(IControl control, string property, List<BabylonAnimation> animations)
  46. {
  47. var result = false;
  48. if (control == null)
  49. {
  50. return false;
  51. }
  52. if (control.XController != null || control.YController != null || control.ZController != null)
  53. {
  54. result |= ExportFloatController(control.XController, property + ".x", animations);
  55. result |= ExportFloatController(control.ZController, property + ".y", animations);
  56. result |= ExportFloatController(control.YController, property + ".z", animations);
  57. return result;
  58. }
  59. if (ExportController(control, property, animations, 0x2002, BabylonAnimation.DataType.Vector3,
  60. (index, keyControl) =>
  61. {
  62. var key = Loader.Global.ILinPoint3Key.Create();
  63. keyControl.GetKey(index, key);
  64. return new BabylonAnimationKey
  65. {
  66. frame = key.Time/Ticks,
  67. values = key.Val.ToArraySwitched()
  68. };
  69. }))
  70. {
  71. return true;
  72. }
  73. return ExportController(control, property, animations, 0x2004, BabylonAnimation.DataType.Vector3,
  74. (index, keyControl) =>
  75. {
  76. var key = Loader.Global.ILinScaleKey.Create();
  77. keyControl.GetKey(index, key);
  78. return new BabylonAnimationKey
  79. {
  80. frame = key.Time/Ticks,
  81. values = key.Val.S.ToArraySwitched()
  82. };
  83. });
  84. }
  85. private static bool ExportController(IControl control, string property, List<BabylonAnimation> animations, uint classId, BabylonAnimation.DataType dataType, Func<int, IIKeyControl, BabylonAnimationKey> generateFunc)
  86. {
  87. if (control == null)
  88. {
  89. return false;
  90. }
  91. var keyControl = control.GetInterface(InterfaceID.Keycontrol) as IIKeyControl;
  92. if (keyControl == null)
  93. {
  94. return false;
  95. }
  96. if (control.ClassID.PartA != classId)
  97. {
  98. return false;
  99. }
  100. var keys = new List<BabylonAnimationKey>();
  101. BabylonAnimation.LoopBehavior loopBehavior;
  102. switch (control.GetORT(2))
  103. {
  104. case 2:
  105. loopBehavior = BabylonAnimation.LoopBehavior.Cycle;
  106. break;
  107. default:
  108. loopBehavior = BabylonAnimation.LoopBehavior.Relative;
  109. break;
  110. }
  111. for (var index = 0; index < keyControl.NumKeys; index++)
  112. {
  113. keys.Add(generateFunc(index, keyControl));
  114. }
  115. if (keys.Count == 0)
  116. {
  117. return false;
  118. }
  119. var end = Loader.Core.AnimRange.End;
  120. if (keys[keys.Count - 1].frame != end / Ticks)
  121. {
  122. keys.Add(new BabylonAnimationKey()
  123. {
  124. frame = end / Ticks,
  125. values = keys[keys.Count - 1].values
  126. });
  127. }
  128. var babylonAnimation = new BabylonAnimation
  129. {
  130. dataType = dataType,
  131. name = property + " animation",
  132. keys = keys.ToArray(),
  133. framePerSecond = Loader.Global.FrameRate,
  134. loopBehavior = loopBehavior,
  135. property = property
  136. };
  137. animations.Add(babylonAnimation);
  138. return true;
  139. }
  140. private static void ExportVector3Animation(string property, List<BabylonAnimation> animations,
  141. Func<int, float[]> extractValueFunc)
  142. {
  143. ExportAnimation(property, animations, extractValueFunc, BabylonAnimation.DataType.Vector3);
  144. }
  145. private static void ExportQuaternionAnimation(string property, List<BabylonAnimation> animations,
  146. Func<int, float[]> extractValueFunc)
  147. {
  148. ExportAnimation(property, animations, extractValueFunc, BabylonAnimation.DataType.Quaternion);
  149. }
  150. private static void ExportFloatAnimation(string property, List<BabylonAnimation> animations,
  151. Func<int, float[]> extractValueFunc)
  152. {
  153. ExportAnimation(property, animations, extractValueFunc, BabylonAnimation.DataType.Float);
  154. }
  155. private static void ExportAnimation(string property, List<BabylonAnimation> animations, Func<int, float[]> extractValueFunc, BabylonAnimation.DataType dataType)
  156. {
  157. var start = Loader.Core.AnimRange.Start;
  158. var end = Loader.Core.AnimRange.End;
  159. float[] previous = null;
  160. var keys = new List<BabylonAnimationKey>();
  161. for (var key = start; key <= end; key += Ticks)
  162. {
  163. var current = extractValueFunc(key);
  164. if (key == start || key == end || !(previous.IsEqualTo(current)))
  165. {
  166. keys.Add(new BabylonAnimationKey()
  167. {
  168. frame = key / Ticks,
  169. values = current
  170. });
  171. }
  172. previous = current;
  173. }
  174. if (keys.Count > 0)
  175. {
  176. var animationPresent = true;
  177. if (keys.Count == 2)
  178. {
  179. if (keys[0].values.IsEqualTo(keys[1].values))
  180. {
  181. animationPresent = false;
  182. }
  183. }
  184. if (animationPresent)
  185. {
  186. if (keys[keys.Count - 1].frame != end / Ticks)
  187. {
  188. keys.Add(new BabylonAnimationKey()
  189. {
  190. frame = end / Ticks,
  191. values = keys[keys.Count - 1].values
  192. });
  193. }
  194. var babylonAnimation = new BabylonAnimation
  195. {
  196. dataType = dataType,
  197. name = property + " animation",
  198. keys = keys.ToArray(),
  199. framePerSecond = Loader.Global.FrameRate,
  200. loopBehavior = BabylonAnimation.LoopBehavior.Cycle,
  201. property = property
  202. };
  203. animations.Add(babylonAnimation);
  204. }
  205. }
  206. }
  207. }
  208. }