AnimatedModel.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. class DebugRenderer;
  30. /// Animated model component.
  31. class 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);
  44. /// Load from XML data. Return true if successful.
  45. virtual bool LoadXML(const XMLElement& source);
  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. Animation is updated here.
  51. virtual void Update(const FrameInfo& frame);
  52. /// Calculate distance and LOD level for rendering. May be called from worker thread(s), possibly re-entrantly.
  53. virtual void UpdateDistance(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 should happen in a worker thread.
  57. virtual UpdateGeometryType GetUpdateGeometryType();
  58. /// Fill rendering batch with distance, geometry, material and world transform.
  59. virtual void GetBatch(Batch& batch, const FrameInfo& frame, unsigned batchIndex);
  60. /// Add debug geometry to the debug renderer.
  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 all animations.
  75. void RemoveAllAnimationStates();
  76. /// %Set animation LOD bias.
  77. void SetAnimationLodBias(float bias);
  78. /// %Set animation LOD distance factor when not visible (default 0 = do not update at all when invisible.)
  79. void SetInvisibleLodFactor(float factor);
  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 animation LOD distance factor when not visible.
  105. float GetInvisibleLodFactor() const { return invisibleLodFactor_; }
  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(ResourceRef value);
  122. /// %Set bones' animation enabled attribute.
  123. void SetBonesEnabledAttr(VariantVector value);
  124. /// %Set animation states attribute.
  125. void SetAnimationStatesAttr(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. protected:
  137. /// Handle node being assigned.
  138. virtual void OnNodeSet(Node* node);
  139. /// Handle node transform being dirtied.
  140. virtual void OnMarkedDirty(Node* node);
  141. /// Recalculate the world-space bounding box.
  142. virtual void OnWorldBoundingBoxUpdate();
  143. private:
  144. /// Assign skeleton and animation bone node references as a postprocess. Called by ApplyAttributes.
  145. void AssignBoneNodes();
  146. /// Mark animation and skinning to require an update.
  147. void MarkAnimationDirty();
  148. /// Mark animation and skinning to require a forced update (blending order changed.)
  149. void MarkAnimationOrderDirty();
  150. /// Mark morphs to require an update.
  151. void MarkMorphsDirty();
  152. /// %Set skeleton.
  153. void SetSkeleton(const Skeleton& skeleton, bool createBones);
  154. /// Refresh mapping of subgeometry bone indices.
  155. void RefreshGeometryBoneMappings();
  156. /// Clone geometries as required.
  157. void CloneGeometries();
  158. /// Recalculate animations. Called from UpdateNode().
  159. void UpdateAnimation(const FrameInfo& frame);
  160. /// Recalculate skinning.
  161. void UpdateSkinning();
  162. /// Reapply all vertex morphs.
  163. void UpdateMorphs();
  164. /// Apply a vertex morph.
  165. void ApplyMorph(VertexBuffer* buffer, void* lockedMorphRange, const VertexBufferMorph& morph, float weight);
  166. /// Handle model reload finished.
  167. void HandleModelReloadFinished(StringHash eventType, VariantMap& eventData);
  168. /// Skeleton.
  169. Skeleton skeleton_;
  170. /// Morph vertex buffers.
  171. Vector<SharedPtr<VertexBuffer> > morphVertexBuffers_;
  172. /// Vertex morphs.
  173. Vector<ModelMorph> morphs_;
  174. /// Animation states.
  175. Vector<SharedPtr<AnimationState> > animationStates_;
  176. /// Skinning matrices.
  177. PODVector<Matrix3x4> skinMatrices_;
  178. /// Mapping of subgeometry bone indices, used if more bones than skinning shader can manage.
  179. Vector<PODVector<unsigned> > geometryBoneMappings_;
  180. /// Subgeometry skinning matrices, used if more bones than skinning shader can manage.
  181. Vector<PODVector<Matrix3x4> > geometrySkinMatrices_;
  182. /// Subgeometry skinning matrix pointers, if more bones than skinning shader can manage.
  183. Vector<PODVector<Matrix3x4*> > geometrySkinMatrixPtrs_;
  184. /// Attribute buffer.
  185. mutable VectorBuffer attrBuffer_;
  186. /// The frame number animation LOD distance was last calculated on.
  187. unsigned animationLodFrameNumber_;
  188. /// Animation LOD bias.
  189. float animationLodBias_;
  190. /// Animation LOD timer.
  191. float animationLodTimer_;
  192. /// Animation LOD distance, the minimum of all LOD view distances last frame.
  193. float animationLodDistance_;
  194. /// Animation LOD distance factor when not visible.
  195. float invisibleLodFactor_;
  196. /// Animation dirty flag.
  197. bool animationDirty_;
  198. /// Animation order dirty flag.
  199. bool animationOrderDirty_;
  200. /// Vertex morphs dirty flag.
  201. bool morphsDirty_;
  202. /// Skinning dirty flag.
  203. bool skinningDirty_;
  204. /// Master model flag.
  205. bool isMaster_;
  206. /// Loading flag. During loading bone nodes are not created, as they will be serialized as child nodes.
  207. bool loading_;
  208. /// Bone nodes assignment pending flag.
  209. bool assignBonesPending_;
  210. };