AnimatedModel.h 11 KB

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