Animation.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. //
  2. // Copyright (c) 2008-2014 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "Quaternion.h"
  24. #include "Resource.h"
  25. #include "Ptr.h"
  26. #include "Vector3.h"
  27. namespace Urho3D
  28. {
  29. /// Skeletal animation keyframe.
  30. struct AnimationKeyFrame
  31. {
  32. /// Keyframe time.
  33. float time_;
  34. /// Bone position.
  35. Vector3 position_;
  36. /// Bone rotation.
  37. Quaternion rotation_;
  38. /// Bone scale.
  39. Vector3 scale_;
  40. };
  41. /// Skeletal animation track, stores keyframes of a single bone.
  42. struct AnimationTrack
  43. {
  44. /// Return keyframe index based on time and previous index.
  45. void GetKeyFrameIndex(float time, unsigned& index) const;
  46. /// Bone name.
  47. String name_;
  48. /// Bone name hash.
  49. StringHash nameHash_;
  50. /// Bitmask of included data (position, rotation, scale.)
  51. unsigned char channelMask_;
  52. /// Keyframes.
  53. Vector<AnimationKeyFrame> keyFrames_;
  54. };
  55. /// %Animation trigger point.
  56. struct AnimationTriggerPoint
  57. {
  58. /// Construct.
  59. AnimationTriggerPoint() :
  60. time_(0.0f)
  61. {
  62. }
  63. /// Trigger time.
  64. float time_;
  65. /// Trigger data.
  66. Variant data_;
  67. };
  68. static const unsigned char CHANNEL_POSITION = 0x1;
  69. static const unsigned char CHANNEL_ROTATION = 0x2;
  70. static const unsigned char CHANNEL_SCALE = 0x4;
  71. /// Skeletal animation resource.
  72. class URHO3D_API Animation : public Resource
  73. {
  74. OBJECT(Animation);
  75. public:
  76. /// Construct.
  77. Animation(Context* context);
  78. /// Destruct.
  79. virtual ~Animation();
  80. /// Register object factory.
  81. static void RegisterObject(Context* context);
  82. /// Load resource. Return true if successful.
  83. virtual bool Load(Deserializer& source);
  84. /// Save resource. Return true if successful.
  85. virtual bool Save(Serializer& dest) const;
  86. /// Set animation name.
  87. void SetAnimationName(const String& name);
  88. /// Set animation length.
  89. void SetLength(float length);
  90. /// Set all animation tracks.
  91. void SetTracks(const Vector<AnimationTrack>& tracks);
  92. /// Add a trigger point.
  93. void AddTrigger(float time, bool timeIsNormalized, const Variant& data);
  94. /// Remove a trigger point by index.
  95. void RemoveTrigger(unsigned index);
  96. /// Remove all trigger points.
  97. void RemoveAllTriggers();
  98. /// Resize trigger point vector.
  99. void SetNumTriggers(unsigned num);
  100. /// Return animation name.
  101. const String& GetAnimationName() const { return animationName_; }
  102. /// Return animation name hash.
  103. StringHash GetAnimationNameHash() const { return animationNameHash_; }
  104. /// Return animation length.
  105. float GetLength() const { return length_; }
  106. /// Return all animation tracks.
  107. const Vector<AnimationTrack>& GetTracks() const { return tracks_; }
  108. /// Return number of animation tracks.
  109. unsigned GetNumTracks() const { return tracks_.Size(); }
  110. /// Return animation track by index.
  111. const AnimationTrack* GetTrack(unsigned index) const;
  112. /// Return animation track by bone name.
  113. const AnimationTrack* GetTrack(const String& name) const;
  114. /// Return animation track by bone name hash.
  115. const AnimationTrack* GetTrack(StringHash nameHash) const;
  116. /// Return animation trigger points.
  117. const Vector<AnimationTriggerPoint>& GetTriggers() const { return triggers_; }
  118. /// Return number of animation trigger points.
  119. unsigned GetNumTriggers() const {return triggers_.Size(); }
  120. private:
  121. /// Animation name.
  122. String animationName_;
  123. /// Animation name hash.
  124. StringHash animationNameHash_;
  125. /// Animation length.
  126. float length_;
  127. /// Animation tracks.
  128. Vector<AnimationTrack> tracks_;
  129. /// Animation trigger points.
  130. Vector<AnimationTriggerPoint> triggers_;
  131. };
  132. }