AnimatedModel.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. //
  2. // Copyright (c) 2008-2015 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 "../Atomic3D/Model.h"
  24. #include "../Atomic3D/Skeleton.h"
  25. #include "../Atomic3D/StaticModel.h"
  26. namespace Atomic
  27. {
  28. class Animation;
  29. class AnimationState;
  30. /// Animated model component.
  31. class ATOMIC_API AnimatedModel : public StaticModel
  32. {
  33. OBJECT(AnimatedModel);
  34. friend class AnimationState;
  35. public:
  36. /// Construct.
  37. AnimatedModel(Context* context);
  38. /// Destruct.
  39. virtual ~AnimatedModel();
  40. /// Register object factory. Drawable must be registered first.
  41. static void RegisterObject(Context* context);
  42. /// Load from binary data. Return true if successful.
  43. virtual bool Load(Deserializer& source, bool setInstanceDefault = false);
  44. /// Load from XML data. Return true if successful.
  45. virtual bool LoadXML(const XMLElement& source, bool setInstanceDefault = false);
  46. /// Apply attribute changes that can not be applied immediately. Called after scene load or a network update.
  47. virtual void ApplyAttributes();
  48. /// Process octree raycast. May be called from a worker thread.
  49. virtual void ProcessRayQuery(const RayOctreeQuery& query, PODVector<RayQueryResult>& results);
  50. /// Update before octree reinsertion. Is called from a worker thread.
  51. virtual void Update(const FrameInfo& frame);
  52. /// Calculate distance and prepare batches for rendering. May be called from worker thread(s), possibly re-entrantly.
  53. virtual void UpdateBatches(const FrameInfo& frame);
  54. /// Prepare geometry for rendering. Called from a worker thread if possible (no GPU update.)
  55. virtual void UpdateGeometry(const FrameInfo& frame);
  56. /// Return whether a geometry update is necessary, and if it can happen in a worker thread.
  57. virtual UpdateGeometryType GetUpdateGeometryType();
  58. /// Visualize the component as debug geometry.
  59. virtual void DrawDebugGeometry(DebugRenderer* debug, bool depthTest);
  60. /// Set model.
  61. void SetModel(Model* model, bool createBones = true);
  62. /// Add an animation.
  63. AnimationState* AddAnimationState(Animation* animation);
  64. /// Remove an animation by animation pointer.
  65. void RemoveAnimationState(Animation* animation);
  66. /// Remove an animation by animation name.
  67. void RemoveAnimationState(const String& animationName);
  68. /// Remove an animation by animation name hash.
  69. void RemoveAnimationState(StringHash animationNameHash);
  70. /// Remove an animation by AnimationState pointer.
  71. void RemoveAnimationState(AnimationState* state);
  72. /// Remove an animation by index.
  73. void RemoveAnimationState(unsigned index);
  74. /// Remove all animations.
  75. void RemoveAllAnimationStates();
  76. /// Set animation LOD bias.
  77. void SetAnimationLodBias(float bias);
  78. /// Set whether to update animation and the bounding box when not visible. Recommended to enable for physically controlled models like ragdolls.
  79. void SetUpdateInvisible(bool enable);
  80. /// Set vertex morph weight by index.
  81. void SetMorphWeight(unsigned index, float weight);
  82. /// Set vertex morph weight by name.
  83. void SetMorphWeight(const String& name, float weight);
  84. /// Set vertex morph weight by name hash.
  85. void SetMorphWeight(StringHash nameHash, float weight);
  86. /// Reset all vertex morphs to zero.
  87. void ResetMorphWeights();
  88. /// Return skeleton.
  89. Skeleton& GetSkeleton() { return skeleton_; }
  90. /// Return all animation states.
  91. const Vector<SharedPtr<AnimationState> >& GetAnimationStates() const { return animationStates_; }
  92. /// Return number of animation states.
  93. unsigned GetNumAnimationStates() const { return animationStates_.Size(); }
  94. /// Return animation state by animation pointer.
  95. AnimationState* GetAnimationState(Animation* animation) const;
  96. /// Return animation state by animation name.
  97. AnimationState* GetAnimationState(const String& animationName) const;
  98. /// Return animation state by animation name hash.
  99. AnimationState* GetAnimationState(const StringHash animationNameHash) const;
  100. /// Return animation state by index.
  101. AnimationState* GetAnimationState(unsigned index) const;
  102. /// Return animation LOD bias.
  103. float GetAnimationLodBias() const { return animationLodBias_; }
  104. /// Return whether to update animation when not visible.
  105. bool GetUpdateInvisible() const { return updateInvisible_; }
  106. /// Return all vertex morphs.
  107. const Vector<ModelMorph>& GetMorphs() const { return morphs_; }
  108. /// Return all morph vertex buffers.
  109. const Vector<SharedPtr<VertexBuffer> >& GetMorphVertexBuffers() const { return morphVertexBuffers_; }
  110. /// Return number of vertex morphs.
  111. unsigned GetNumMorphs() const { return morphs_.Size(); }
  112. /// Return vertex morph weight by index.
  113. float GetMorphWeight(unsigned index) const;
  114. /// Return vertex morph weight by name.
  115. float GetMorphWeight(const String& name) const;
  116. /// Return vertex morph weight by name hash.
  117. float GetMorphWeight(StringHash nameHash) const;
  118. /// Return whether is the master (first) animated model.
  119. bool IsMaster() const { return isMaster_; }
  120. /// Set model attribute.
  121. void SetModelAttr(const ResourceRef& value);
  122. /// Set bones' animation enabled attribute.
  123. void SetBonesEnabledAttr(const VariantVector& value);
  124. /// Set animation states attribute.
  125. void SetAnimationStatesAttr(const VariantVector& value);
  126. /// Set morphs attribute.
  127. void SetMorphsAttr(const PODVector<unsigned char>& value);
  128. /// Return model attribute.
  129. ResourceRef GetModelAttr() const;
  130. /// Return bones' animation enabled attribute.
  131. VariantVector GetBonesEnabledAttr() const;
  132. /// Return animation states attribute.
  133. VariantVector GetAnimationStatesAttr() const;
  134. /// Return morphs attribute.
  135. const PODVector<unsigned char>& GetMorphsAttr() const;
  136. /// Return per-geometry bone mappings.
  137. const Vector<PODVector<unsigned> >& GetGeometryBoneMappings() const { return geometryBoneMappings_; }
  138. /// Return per-geometry skin matrices. If empty, uses global skinning
  139. const Vector<PODVector<Matrix3x4> >& GetGeometrySkinMatrices() const { return geometrySkinMatrices_; }
  140. /// Globally enable/disable bone creation, useful for when in the editor
  141. static void SetBoneCreationEnabled(bool enabled);
  142. protected:
  143. /// Handle node being assigned.
  144. virtual void OnNodeSet(Node* node);
  145. /// Handle node transform being dirtied.
  146. virtual void OnMarkedDirty(Node* node);
  147. /// Recalculate the world-space bounding box.
  148. virtual void OnWorldBoundingBoxUpdate();
  149. private:
  150. /// Assign skeleton and animation bone node references as a postprocess. Called by ApplyAttributes.
  151. void AssignBoneNodes();
  152. /// Remove (old) skeleton root bone.
  153. void RemoveRootBone();
  154. /// Mark animation and skinning to require an update.
  155. void MarkAnimationDirty();
  156. /// Mark animation and skinning to require a forced update (blending order changed.)
  157. void MarkAnimationOrderDirty();
  158. /// Mark morphs to require an update.
  159. void MarkMorphsDirty();
  160. /// Set skeleton.
  161. void SetSkeleton(const Skeleton& skeleton, bool createBones);
  162. /// Set mapping of subgeometry bone indices.
  163. void SetGeometryBoneMappings();
  164. /// Clone geometries for vertex morphing.
  165. void CloneGeometries();
  166. /// Copy morph vertices.
  167. void CopyMorphVertices(void* dest, void* src, unsigned vertexCount, VertexBuffer* clone, VertexBuffer* original);
  168. /// Recalculate animations. Called from Update().
  169. void UpdateAnimation(const FrameInfo& frame);
  170. /// Recalculate the bone bounding box.
  171. void UpdateBoneBoundingBox();
  172. /// Recalculate skinning.
  173. void UpdateSkinning();
  174. /// Reapply all vertex morphs.
  175. void UpdateMorphs();
  176. /// Apply a vertex morph.
  177. void ApplyMorph
  178. (VertexBuffer* buffer, void* destVertexData, unsigned morphRangeStart, const VertexBufferMorph& morph, float weight);
  179. /// Handle model reload finished.
  180. void HandleModelReloadFinished(StringHash eventType, VariantMap& eventData);
  181. /// Skeleton.
  182. Skeleton skeleton_;
  183. /// Morph vertex buffers.
  184. Vector<SharedPtr<VertexBuffer> > morphVertexBuffers_;
  185. /// Vertex morphs.
  186. Vector<ModelMorph> morphs_;
  187. /// Animation states.
  188. Vector<SharedPtr<AnimationState> > animationStates_;
  189. /// Skinning matrices.
  190. PODVector<Matrix3x4> skinMatrices_;
  191. /// Mapping of subgeometry bone indices, used if more bones than skinning shader can manage.
  192. Vector<PODVector<unsigned> > geometryBoneMappings_;
  193. /// Subgeometry skinning matrices, used if more bones than skinning shader can manage.
  194. Vector<PODVector<Matrix3x4> > geometrySkinMatrices_;
  195. /// Subgeometry skinning matrix pointers, if more bones than skinning shader can manage.
  196. Vector<PODVector<Matrix3x4*> > geometrySkinMatrixPtrs_;
  197. /// Bounding box calculated from bones.
  198. BoundingBox boneBoundingBox_;
  199. /// Attribute buffer.
  200. mutable VectorBuffer attrBuffer_;
  201. /// The frame number animation LOD distance was last calculated on.
  202. unsigned animationLodFrameNumber_;
  203. /// Morph vertex element mask.
  204. unsigned morphElementMask_;
  205. /// Animation LOD bias.
  206. float animationLodBias_;
  207. /// Animation LOD timer.
  208. float animationLodTimer_;
  209. /// Animation LOD distance, the minimum of all LOD view distances last frame.
  210. float animationLodDistance_;
  211. /// Update animation when invisible flag.
  212. bool updateInvisible_;
  213. /// Animation dirty flag.
  214. bool animationDirty_;
  215. /// Animation order dirty flag.
  216. bool animationOrderDirty_;
  217. /// Vertex morphs dirty flag.
  218. bool morphsDirty_;
  219. /// Skinning dirty flag.
  220. bool skinningDirty_;
  221. /// Bone bounding box dirty flag.
  222. bool boneBoundingBoxDirty_;
  223. /// Master model flag.
  224. bool isMaster_;
  225. /// Loading flag. During loading bone nodes are not created, as they will be serialized as child nodes.
  226. bool loading_;
  227. /// Bone nodes assignment pending flag.
  228. bool assignBonesPending_;
  229. /// Whether bone creation is enabled, globally
  230. static bool boneCreationEnabled_;
  231. };
  232. }