Skeleton.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. //
  2. // Copyright (c) 2008-2020 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. #include "../Precompiled.h"
  23. #include "../Graphics/Skeleton.h"
  24. #include "../IO/Log.h"
  25. #include "../DebugNew.h"
  26. namespace Urho3D
  27. {
  28. Skeleton::Skeleton() :
  29. rootBoneIndex_(M_MAX_UNSIGNED)
  30. {
  31. }
  32. Skeleton::~Skeleton() = default;
  33. bool Skeleton::Load(Deserializer& source)
  34. {
  35. ClearBones();
  36. if (source.IsEof())
  37. return false;
  38. unsigned bones = source.ReadUInt();
  39. bones_.Reserve(bones);
  40. for (unsigned i = 0; i < bones; ++i)
  41. {
  42. Bone newBone;
  43. newBone.name_ = source.ReadString();
  44. newBone.nameHash_ = newBone.name_;
  45. newBone.parentIndex_ = source.ReadUInt();
  46. newBone.initialPosition_ = source.ReadVector3();
  47. newBone.initialRotation_ = source.ReadQuaternion();
  48. newBone.initialScale_ = source.ReadVector3();
  49. source.Read(&newBone.offsetMatrix_.m00_, sizeof(Matrix3x4));
  50. // Read bone collision data
  51. newBone.collisionMask_ = BoneCollisionShapeFlags(source.ReadUByte());
  52. if (newBone.collisionMask_ & BONECOLLISION_SPHERE)
  53. newBone.radius_ = source.ReadFloat();
  54. if (newBone.collisionMask_ & BONECOLLISION_BOX)
  55. newBone.boundingBox_ = source.ReadBoundingBox();
  56. if (newBone.parentIndex_ == i)
  57. rootBoneIndex_ = i;
  58. bones_.Push(newBone);
  59. }
  60. return true;
  61. }
  62. bool Skeleton::Save(Serializer& dest) const
  63. {
  64. if (!dest.WriteUInt(bones_.Size()))
  65. return false;
  66. for (unsigned i = 0; i < bones_.Size(); ++i)
  67. {
  68. const Bone& bone = bones_[i];
  69. dest.WriteString(bone.name_);
  70. dest.WriteUInt(bone.parentIndex_);
  71. dest.WriteVector3(bone.initialPosition_);
  72. dest.WriteQuaternion(bone.initialRotation_);
  73. dest.WriteVector3(bone.initialScale_);
  74. dest.Write(bone.offsetMatrix_.Data(), sizeof(Matrix3x4));
  75. // Collision info
  76. dest.WriteUByte(bone.collisionMask_);
  77. if (bone.collisionMask_ & BONECOLLISION_SPHERE)
  78. dest.WriteFloat(bone.radius_);
  79. if (bone.collisionMask_ & BONECOLLISION_BOX)
  80. dest.WriteBoundingBox(bone.boundingBox_);
  81. }
  82. return true;
  83. }
  84. void Skeleton::Define(const Skeleton& src)
  85. {
  86. ClearBones();
  87. bones_ = src.bones_;
  88. // Make sure we clear node references, if they exist
  89. // (AnimatedModel will create new nodes on its own)
  90. for (Vector<Bone>::Iterator i = bones_.Begin(); i != bones_.End(); ++i)
  91. i->node_.Reset();
  92. rootBoneIndex_ = src.rootBoneIndex_;
  93. }
  94. void Skeleton::SetRootBoneIndex(unsigned index)
  95. {
  96. if (index < bones_.Size())
  97. rootBoneIndex_ = index;
  98. else
  99. URHO3D_LOGERROR("Root bone index out of bounds");
  100. }
  101. void Skeleton::ClearBones()
  102. {
  103. bones_.Clear();
  104. rootBoneIndex_ = M_MAX_UNSIGNED;
  105. }
  106. void Skeleton::Reset()
  107. {
  108. for (Vector<Bone>::Iterator i = bones_.Begin(); i != bones_.End(); ++i)
  109. {
  110. if (i->animated_ && i->node_)
  111. i->node_->SetTransform(i->initialPosition_, i->initialRotation_, i->initialScale_);
  112. }
  113. }
  114. void Skeleton::ResetSilent()
  115. {
  116. for (Vector<Bone>::Iterator i = bones_.Begin(); i != bones_.End(); ++i)
  117. {
  118. if (i->animated_ && i->node_)
  119. i->node_->SetTransformSilent(i->initialPosition_, i->initialRotation_, i->initialScale_);
  120. }
  121. }
  122. Bone* Skeleton::GetRootBone()
  123. {
  124. return GetBone(rootBoneIndex_);
  125. }
  126. unsigned Skeleton::GetBoneIndex(const StringHash& boneNameHash) const
  127. {
  128. const unsigned numBones = bones_.Size();
  129. for (unsigned i = 0; i < numBones; ++i)
  130. {
  131. if (bones_[i].nameHash_ == boneNameHash)
  132. return i;
  133. }
  134. return M_MAX_UNSIGNED;
  135. }
  136. unsigned Skeleton::GetBoneIndex(const Bone* bone) const
  137. {
  138. if (bones_.Empty() || bone < &bones_.Front() || bone > &bones_.Back())
  139. return M_MAX_UNSIGNED;
  140. return static_cast<unsigned>(bone - &bones_.Front());
  141. }
  142. unsigned Skeleton::GetBoneIndex(const String& boneName) const
  143. {
  144. return GetBoneIndex(StringHash(boneName));
  145. }
  146. Bone* Skeleton::GetBoneParent(const Bone* bone)
  147. {
  148. if (GetBoneIndex(bone) == bone->parentIndex_)
  149. return nullptr;
  150. else
  151. return GetBone(bone->parentIndex_);
  152. }
  153. Bone* Skeleton::GetBone(unsigned index)
  154. {
  155. return index < bones_.Size() ? &bones_[index] : nullptr;
  156. }
  157. Bone* Skeleton::GetBone(const String& name)
  158. {
  159. return GetBone(StringHash(name));
  160. }
  161. Bone* Skeleton::GetBone(const char* name)
  162. {
  163. return GetBone(StringHash(name));
  164. }
  165. Bone* Skeleton::GetBone(const StringHash& boneNameHash)
  166. {
  167. const unsigned index = GetBoneIndex(boneNameHash);
  168. return index < bones_.Size() ? &bones_[index] : nullptr;
  169. }
  170. }