Animation.h 4.6 KB

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