BabylonExporter.Animation.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using System;
  2. using System.Collections.Generic;
  3. using BabylonExport.Entities;
  4. using MaxSharp;
  5. namespace Max2Babylon
  6. {
  7. partial class BabylonExporter
  8. {
  9. private void ExportVector3Animation(string property, List<BabylonAnimation> animations,
  10. Func<int, float[]> extractValueFunc)
  11. {
  12. ExportAnimation(property, animations, extractValueFunc, BabylonAnimation.DataType.Vector3);
  13. }
  14. private void ExportQuaternionAnimation(string property, List<BabylonAnimation> animations,
  15. Func<int, float[]> extractValueFunc)
  16. {
  17. ExportAnimation(property, animations, extractValueFunc, BabylonAnimation.DataType.Quaternion);
  18. }
  19. private void ExportFloatAnimation(string property, List<BabylonAnimation> animations,
  20. Func<int, float[]> extractValueFunc)
  21. {
  22. ExportAnimation(property, animations, extractValueFunc, BabylonAnimation.DataType.Float);
  23. }
  24. private void ExportAnimation(string property, List<BabylonAnimation> animations, Func<int, float[]> extractValueFunc, BabylonAnimation.DataType dataType)
  25. {
  26. const int ticks = 160;
  27. var start = Loader.Core.AnimRange.Start;
  28. var end = Loader.Core.AnimRange.End;
  29. float[] previous = null;
  30. var keys = new List<BabylonAnimationKey>();
  31. for (var key = start; key <= end; key += ticks)
  32. {
  33. var current = extractValueFunc(key);
  34. if (key == start || key == end || !(previous.IsEqualTo(current)))
  35. {
  36. keys.Add(new BabylonAnimationKey()
  37. {
  38. frame = key / ticks,
  39. values = current
  40. });
  41. }
  42. previous = current;
  43. }
  44. if (keys.Count > 0)
  45. {
  46. var animationPresent = true;
  47. if (keys.Count == 2)
  48. {
  49. if (keys[0].values.IsEqualTo(keys[1].values))
  50. {
  51. animationPresent = false;
  52. }
  53. }
  54. if (animationPresent)
  55. {
  56. var babylonAnimation = new BabylonAnimation
  57. {
  58. dataType = dataType,
  59. name = property + " animation",
  60. keys = keys.ToArray(),
  61. framePerSecond = Loader.Global.FrameRate,
  62. loopBehavior = BabylonAnimation.LoopBehavior.Relative,
  63. property = property
  64. };
  65. animations.Add(babylonAnimation);
  66. }
  67. }
  68. }
  69. }
  70. }