AnimatedModel.h 9.9 KB

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