Animation Keys.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /******************************************************************************
  2. Use 'AnimationKeys' to store animation keys:
  3. -target orientation
  4. -relative rotation
  5. -offset position
  6. -scale
  7. /******************************************************************************/
  8. #if EE_PRIVATE
  9. #define HAS_ANIM_TANGENT 0
  10. #define HAS_ANIM_ROT 0
  11. #define HAS_ANIM_COLOR 0
  12. struct AnimParams
  13. {
  14. Bool loop ,
  15. linear;
  16. Flt length,
  17. time ;
  18. void set(Bool loop, Bool linear, Flt length, Flt time)
  19. {
  20. T.loop =loop;
  21. T.linear=linear;
  22. T.length=length;
  23. T.time =(loop ? Frac(time, length) : time);
  24. }
  25. void set(C Animation &animation, Flt time);
  26. AnimParams() {}
  27. AnimParams(Bool loop, Bool linear, Flt length, Flt time) {set(loop, linear, length, time);}
  28. AnimParams(C Animation &animation, Flt time ) {set(animation , time);}
  29. };
  30. #endif
  31. typedef AnimationKeys AnimKeys;
  32. struct AnimationKeys // Animation Keys - set of animation keyframes for a single bone
  33. {
  34. struct Key
  35. {
  36. Flt time; // time position (seconds)
  37. };
  38. struct Orn : Key // Target Orientation
  39. {
  40. Orient orn; // orientation
  41. #if EE_PRIVATE
  42. void save(MemPtr<TextNode> nodes)C; // save text
  43. #endif
  44. };
  45. struct Pos : Key // Offset Position
  46. {
  47. Vec pos; // position offset
  48. #if EE_PRIVATE
  49. void save(MemPtr<TextNode> nodes)C; // save text
  50. #endif
  51. };
  52. struct Scale : Key // Scale
  53. {
  54. Vec scale; // scale factor
  55. #if EE_PRIVATE
  56. void save(MemPtr<TextNode> nodes)C; // save text
  57. #endif
  58. };
  59. Mems<Orn > orns;
  60. Mems<Pos > poss;
  61. Mems<Scale> scales;
  62. // get
  63. Bool is()C {return orns.elms() || poss.elms() || scales.elms();} // if has any keys
  64. // transform
  65. AnimKeys& mirrorX(); // mirror keyframes in x axis
  66. // operations
  67. AnimKeys& scale (Flt scale ); // scale position offset key values by 'scale'
  68. AnimKeys& scaleTime (Flt scale, Flt anim_length ); // scale time positions of keyframes "each frame time *= scale", 'anim_length'=new animation length (after scale), it's used for clipping time values
  69. AnimKeys& slideTime (Flt dt, Flt anim_length ); // slide time positions of keyframes, 'anim_length'=animation length
  70. AnimKeys& reverse ( Flt anim_length ); // reverse animation, 'anim_length'=animation length
  71. AnimKeys& sortFrames ( ); // sort frames in time order, this should be called after manually modifying the keyframes and changing their time positions
  72. AnimKeys& setTangents(Bool anim_loop, Flt anim_length ); // recalculate tangents, 'anim_loop'=if animation is looped, 'anim_length'=animation length
  73. AnimKeys& optimize (Bool anim_loop, Bool anim_linear, Flt anim_length, Flt angle_eps=EPS_ANIM_ANGLE, Flt pos_eps=EPS_ANIM_POS, Flt scale_eps=EPS_ANIM_SCALE, C Orient *bone=null, C Orient *bone_parent=null); // optimize animation by removing similar keyframes, 'angle_eps'=angular epsilon 0..PI, 'pos_eps'=position epsilon 0..Inf, 'scale_eps'=scale epsilon 0..Inf, 'color_eps'=color epsilon 0..1, 'bone'=un-transformed orientation of skeleton bone (if specified then it will be used to check if orientation keyframe can be removed if there's only one), 'bone_parent'=un-transformed orientation of skeleton bone parent, 'anim_loop'=if animation is looped, 'anim_linear'=if animation is linear, 'anim_length'=animation length
  74. AnimKeys& clip (Bool anim_loop, Bool anim_linear, Flt anim_length, Flt start_time, Flt end_time ); // clip animation to 'start_time' .. 'end_time', this will remove all keyframes which aren't located in selected range, 'anim_loop'=if animation is looped, 'anim_linear'=if animation is linear, 'anim_length'=animation length
  75. void includeTimes(MemPtr<Flt, 16384> orn_times, MemPtr<Flt, 16384> pos_times, MemPtr<Flt, 16384> scale_times)C;
  76. #if EE_PRIVATE
  77. // get
  78. Bool timeRange(Flt &min, Flt &max)C; // get min/max time value out of all keyframes, false on fail (if there are no keyframes)
  79. Bool orn (Orient &orn , C AnimParams &params)C; // get orientation at specified time, false on fail (if there are no keyframes)
  80. Bool rot (AxisRoll &rot , C AnimParams &params)C; // get rotation at specified time, false on fail (if there are no keyframes)
  81. Bool pos (Vec &pos , C AnimParams &params)C; // get position at specified time, false on fail (if there are no keyframes)
  82. Bool scale(Vec &scale, C AnimParams &params)C; // get scale factor at specified time, false on fail (if there are no keyframes)
  83. Bool color(Vec4 &color, C AnimParams &params)C; // get color at specified time, false on fail (if there are no keyframes)
  84. // transform
  85. AnimKeys& transform(C Matrix3 &matrix); // transform keyframes by 'matrix', 'matrix' must be normalized
  86. // convert
  87. AnimKeys& convertRotToOrn(C Skeleton &skeleton, Int skel_bone_index, Bool looped, Flt anim_length); // convert relative rotations to target orientations according to given 'skeleton', 'skel_bone_index'=index of skeleton bone to which this keys belong to (-1=root bone), 'looped'=if animation is looped
  88. AnimKeys& convertOrnToRot(C Skeleton &skeleton, Int skel_bone_index, Bool looped, Flt anim_length); // convert target orientations to relative rotations according to given 'skeleton', 'skel_bone_index'=index of skeleton bone to which this keys belong to (-1=root bone), 'looped'=if animation is looped
  89. // io
  90. Bool saveData (File &f)C;
  91. Bool loadData (File &f) ;
  92. void loadData3(File &f) ;
  93. void loadData2(File &f) ;
  94. void loadData1(File &f) ;
  95. void loadData0(File &f) ;
  96. void save(MemPtr<TextNode> nodes)C; // save text
  97. #endif
  98. AnimKeys& del(); // delete manually
  99. };
  100. /******************************************************************************/