BabylonExporter.Animation.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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 = (int)dataType,
  131. name = property + " animation",
  132. keys = keys.ToArray(),
  133. framePerSecond = Loader.Global.FrameRate,
  134. loopBehavior = (int)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. static void RemoveLinearAnimationKeys(List<BabylonAnimationKey> keys)
  156. {
  157. for (int ixFirst = keys.Count - 3; ixFirst >= 0; --ixFirst)
  158. {
  159. while (keys.Count - ixFirst >= 3)
  160. {
  161. if (!RemoveAnimationKey(keys, ixFirst))
  162. {
  163. break;
  164. }
  165. }
  166. }
  167. }
  168. static float[] weightedLerp(int frame0, int frame1, int frame2, float[] value0, float[] value2)
  169. {
  170. double weight2 = (frame1 - frame0) / (double)(frame2 - frame0);
  171. double weight0 = 1 - weight2;
  172. float[] result = new float[value0.Length];
  173. for (int i = 0; i < result.Length; ++i)
  174. {
  175. result[i] = (float)(value0[i] * weight0 + value2[i] * weight2);
  176. }
  177. return result;
  178. }
  179. private static bool RemoveAnimationKey(List<BabylonAnimationKey> keys, int ixFirst)
  180. {
  181. var first = keys[ixFirst];
  182. var middle = keys[ixFirst + 1];
  183. var last = keys[ixFirst + 2];
  184. // first pass, frame equality
  185. if (first.values.IsEqualTo(last.values) && first.values.IsEqualTo(middle.values))
  186. {
  187. keys.RemoveAt(ixFirst + 1);
  188. return true;
  189. }
  190. // second pass : linear interpolation detection
  191. var computedMiddleValue = weightedLerp(first.frame, middle.frame, last.frame, first.values, last.values);
  192. if (computedMiddleValue.IsEqualTo(middle.values))
  193. {
  194. keys.RemoveAt(ixFirst + 1);
  195. return true;
  196. }
  197. return false;
  198. }
  199. private static void ExportAnimation(string property, List<BabylonAnimation> animations, Func<int, float[]> extractValueFunc, BabylonAnimation.DataType dataType)
  200. {
  201. var exportNonOptimizedAnimations = Loader.Core.RootNode.GetBoolProperty("babylonjs_exportnonoptimizedanimations");
  202. var start = Loader.Core.AnimRange.Start;
  203. var end = Loader.Core.AnimRange.End;
  204. float[] previous = null;
  205. var keys = new List<BabylonAnimationKey>();
  206. for (var key = start; key <= end; key += Ticks)
  207. {
  208. var current = extractValueFunc(key);
  209. if (exportNonOptimizedAnimations && previous != null && previous.IsEqualTo(current))
  210. {
  211. continue; // Do not add key
  212. }
  213. keys.Add(new BabylonAnimationKey()
  214. {
  215. frame = key / Ticks,
  216. values = current
  217. });
  218. previous = current;
  219. }
  220. if (!exportNonOptimizedAnimations)
  221. {
  222. RemoveLinearAnimationKeys(keys);
  223. }
  224. if (keys.Count > 1)
  225. {
  226. var animationPresent = true;
  227. if (keys.Count == 2)
  228. {
  229. if (keys[0].values.IsEqualTo(keys[1].values))
  230. {
  231. animationPresent = false;
  232. }
  233. }
  234. if (animationPresent)
  235. {
  236. if (keys[keys.Count - 1].frame != end / Ticks)
  237. {
  238. keys.Add(new BabylonAnimationKey()
  239. {
  240. frame = end / Ticks,
  241. values = keys[keys.Count - 1].values
  242. });
  243. }
  244. var babylonAnimation = new BabylonAnimation
  245. {
  246. dataType = (int)dataType,
  247. name = property + " animation",
  248. keys = keys.ToArray(),
  249. framePerSecond = Loader.Global.FrameRate,
  250. loopBehavior = (int)BabylonAnimation.LoopBehavior.Cycle,
  251. property = property
  252. };
  253. animations.Add(babylonAnimation);
  254. }
  255. }
  256. }
  257. }
  258. }