AnimatedModel.h 12 KB

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