AnimatedModel.h 8.6 KB

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