Animation.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. //
  2. // Copyright (c) 2008-2013 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. /// Trigger time.
  59. float time_;
  60. /// Trigger data.
  61. Variant data_;
  62. };
  63. static const unsigned char CHANNEL_POSITION = 0x1;
  64. static const unsigned char CHANNEL_ROTATION = 0x2;
  65. static const unsigned char CHANNEL_SCALE = 0x4;
  66. /// Skeletal animation resource.
  67. class Animation : public Resource
  68. {
  69. OBJECT(Animation);
  70. public:
  71. /// Construct.
  72. Animation(Context* context);
  73. /// Destruct.
  74. virtual ~Animation();
  75. /// Register object factory.
  76. static void RegisterObject(Context* context);
  77. /// Load resource. Return true if successful.
  78. virtual bool Load(Deserializer& source);
  79. /// Save resource. Return true if successful.
  80. virtual bool Save(Serializer& dest);
  81. /// Set animation name.
  82. void SetAnimationName(const String& name);
  83. /// Set animation length.
  84. void SetLength(float length);
  85. /// Set all animation tracks.
  86. void SetTracks(const Vector<AnimationTrack>& tracks);
  87. /// Add a trigger point.
  88. void AddTrigger(float time, bool timeIsNormalized, const Variant& data);
  89. /// Remove a trigger point by index.
  90. void RemoveTrigger(unsigned index);
  91. /// Remove all trigger points.
  92. void RemoveAllTriggers();
  93. /// Return animation name.
  94. const String& GetAnimationName() const { return animationName_; }
  95. /// Return animation name hash.
  96. StringHash GetAnimationNameHash() const { return animationNameHash_; }
  97. /// Return animation length.
  98. float GetLength() const { return length_; }
  99. /// Return all animation tracks.
  100. const Vector<AnimationTrack>& GetTracks() const { return tracks_; }
  101. /// Return number of animation tracks.
  102. unsigned GetNumTracks() const { return tracks_.Size(); }
  103. /// Return animation track by index.
  104. const AnimationTrack* GetTrack(unsigned index) const;
  105. /// Return animation track by bone name.
  106. const AnimationTrack* GetTrack(const String& name) const;
  107. /// Return animation track by bone name hash.
  108. const AnimationTrack* GetTrack(StringHash nameHash) const;
  109. /// Return animation trigger points.
  110. const Vector<AnimationTriggerPoint>& GetTriggers() const { return triggers_; }
  111. /// Return number of animation trigger points.
  112. unsigned GetNumTriggers() const {return triggers_.Size(); }
  113. private:
  114. /// Animation name.
  115. String animationName_;
  116. /// Animation name hash.
  117. StringHash animationNameHash_;
  118. /// Animation length.
  119. float length_;
  120. /// Animation tracks.
  121. Vector<AnimationTrack> tracks_;
  122. /// Animation trigger points.
  123. Vector<AnimationTriggerPoint> triggers_;
  124. };
  125. }