AnimatedModel.h 11 KB

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