AnimatedModel.h 9.0 KB

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