AnimatedModel.h 9.5 KB

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