BabylonExporter.Animation.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 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 bool ExportFloatController(IControl control, string property, List<BabylonAnimation> animations)
  21. {
  22. return ExportController(control, property, animations, 0x2001, BabylonAnimation.DataType.Float, GenerateFloatFunc);
  23. }
  24. private 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 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 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. var babylonAnimation = new BabylonAnimation
  116. {
  117. dataType = dataType,
  118. name = property + " animation",
  119. keys = keys.ToArray(),
  120. framePerSecond = Loader.Global.FrameRate,
  121. loopBehavior = loopBehavior,
  122. property = property
  123. };
  124. animations.Add(babylonAnimation);
  125. return true;
  126. }
  127. private void ExportVector3Animation(string property, List<BabylonAnimation> animations,
  128. Func<int, float[]> extractValueFunc)
  129. {
  130. ExportAnimation(property, animations, extractValueFunc, BabylonAnimation.DataType.Vector3);
  131. }
  132. private void ExportQuaternionAnimation(string property, List<BabylonAnimation> animations,
  133. Func<int, float[]> extractValueFunc)
  134. {
  135. ExportAnimation(property, animations, extractValueFunc, BabylonAnimation.DataType.Quaternion);
  136. }
  137. private void ExportFloatAnimation(string property, List<BabylonAnimation> animations,
  138. Func<int, float[]> extractValueFunc)
  139. {
  140. ExportAnimation(property, animations, extractValueFunc, BabylonAnimation.DataType.Float);
  141. }
  142. private void ExportAnimation(string property, List<BabylonAnimation> animations, Func<int, float[]> extractValueFunc, BabylonAnimation.DataType dataType)
  143. {
  144. var start = Loader.Core.AnimRange.Start;
  145. var end = Loader.Core.AnimRange.End;
  146. float[] previous = null;
  147. var keys = new List<BabylonAnimationKey>();
  148. for (var key = start; key <= end; key += Ticks)
  149. {
  150. var current = extractValueFunc(key);
  151. if (key == start || key == end || !(previous.IsEqualTo(current)))
  152. {
  153. keys.Add(new BabylonAnimationKey()
  154. {
  155. frame = key / Ticks,
  156. values = current
  157. });
  158. }
  159. previous = current;
  160. }
  161. if (keys.Count > 0)
  162. {
  163. var animationPresent = true;
  164. if (keys.Count == 2)
  165. {
  166. if (keys[0].values.IsEqualTo(keys[1].values))
  167. {
  168. animationPresent = false;
  169. }
  170. }
  171. if (animationPresent)
  172. {
  173. var babylonAnimation = new BabylonAnimation
  174. {
  175. dataType = dataType,
  176. name = property + " animation",
  177. keys = keys.ToArray(),
  178. framePerSecond = Loader.Global.FrameRate,
  179. loopBehavior = BabylonAnimation.LoopBehavior.Relative,
  180. property = property
  181. };
  182. animations.Add(babylonAnimation);
  183. }
  184. }
  185. }
  186. }
  187. }