AnimatedModel.h 8.7 KB

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