Animation.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Oorni
  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. namespace Urho3D
  29. {
  30. /// Skeletal animation keyframe.
  31. struct AnimationKeyFrame
  32. {
  33. /// Keyframe time.
  34. float time_;
  35. /// Bone position.
  36. Vector3 position_;
  37. /// Bone rotation.
  38. Quaternion rotation_;
  39. /// Bone scale.
  40. Vector3 scale_;
  41. };
  42. /// Skeletal animation track, stores keyframes of a single bone.
  43. struct AnimationTrack
  44. {
  45. /// Return keyframe index based on time and previous index.
  46. void GetKeyFrameIndex(float time, unsigned& index) const;
  47. /// Bone name.
  48. String name_;
  49. /// Bone name hash.
  50. StringHash nameHash_;
  51. /// Bitmask of included data (position, rotation, scale.)
  52. unsigned char channelMask_;
  53. /// Keyframes.
  54. Vector<AnimationKeyFrame> keyFrames_;
  55. };
  56. /// %Animation trigger point.
  57. struct AnimationTriggerPoint
  58. {
  59. /// Trigger time.
  60. float time_;
  61. /// Trigger data.
  62. Variant data_;
  63. };
  64. static const unsigned char CHANNEL_POSITION = 0x1;
  65. static const unsigned char CHANNEL_ROTATION = 0x2;
  66. static const unsigned char CHANNEL_SCALE = 0x4;
  67. /// Skeletal animation resource.
  68. class Animation : public Resource
  69. {
  70. OBJECT(Animation);
  71. public:
  72. /// Construct.
  73. Animation(Context* context);
  74. /// Destruct.
  75. virtual ~Animation();
  76. /// Register object factory.
  77. static void RegisterObject(Context* context);
  78. /// Load resource. Return true if successful.
  79. virtual bool Load(Deserializer& source);
  80. /// Save resource. Return true if successful.
  81. virtual bool Save(Serializer& dest);
  82. /// Set animation name.
  83. void SetAnimationName(const String& name);
  84. /// Set animation length.
  85. void SetLength(float length);
  86. /// Set all animation tracks.
  87. void SetTracks(const Vector<AnimationTrack>& tracks);
  88. /// Add a trigger point.
  89. void AddTrigger(float time, bool timeIsNormalized, const Variant& data);
  90. /// Remove a trigger point by index.
  91. void RemoveTrigger(unsigned index);
  92. /// Remove all trigger points.
  93. void RemoveAllTriggers();
  94. /// Return animation name.
  95. const String& GetAnimationName() const { return animationName_; }
  96. /// Return animation name hash.
  97. StringHash GetAnimationNameHash() const { return animationNameHash_; }
  98. /// Return animation length.
  99. float GetLength() const { return length_; }
  100. /// Return all animation tracks.
  101. const Vector<AnimationTrack>& GetTracks() const { return tracks_; }
  102. /// Return number of animation tracks.
  103. unsigned GetNumTracks() const { return tracks_.Size(); }
  104. /// Return animation track by index.
  105. const AnimationTrack* GetTrack(unsigned index) const;
  106. /// Return animation track by bone name.
  107. const AnimationTrack* GetTrack(const String& name) const;
  108. /// Return animation track by bone name hash.
  109. const AnimationTrack* GetTrack(StringHash nameHash) const;
  110. /// Return animation trigger points.
  111. const Vector<AnimationTriggerPoint>& GetTriggers() const { return triggers_; }
  112. /// Return number of animation trigger points.
  113. unsigned GetNumTriggers() const {return triggers_.Size(); }
  114. private:
  115. /// Animation name.
  116. String animationName_;
  117. /// Animation name hash.
  118. StringHash animationNameHash_;
  119. /// Animation length.
  120. float length_;
  121. /// Animation tracks.
  122. Vector<AnimationTrack> tracks_;
  123. /// Animation trigger points.
  124. Vector<AnimationTriggerPoint> triggers_;
  125. };
  126. }