Model.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #pragma once
  4. #include "../Container/ArrayPtr.h"
  5. #include "../Container/Ptr.h"
  6. #include "../Graphics/Skeleton.h"
  7. #include "../GraphicsAPI/GraphicsDefs.h"
  8. #include "../Math/BoundingBox.h"
  9. #include "../Resource/Resource.h"
  10. namespace Urho3D
  11. {
  12. class Geometry;
  13. class IndexBuffer;
  14. class Graphics;
  15. class VertexBuffer;
  16. /// Vertex buffer morph data.
  17. struct VertexBufferMorph
  18. {
  19. /// Vertex elements.
  20. VertexElements elementMask_;
  21. /// Number of vertices.
  22. i32 vertexCount_;
  23. /// Morphed vertices data size as bytes.
  24. i32 dataSize_;
  25. /// Morphed vertices. Stored packed as <index, data> pairs.
  26. SharedArrayPtr<byte> morphData_;
  27. };
  28. /// Definition of a model's vertex morph.
  29. struct ModelMorph
  30. {
  31. /// Morph name.
  32. String name_;
  33. /// Morph name hash.
  34. StringHash nameHash_;
  35. /// Current morph weight.
  36. float weight_;
  37. /// Morph data per vertex buffer.
  38. HashMap<i32, VertexBufferMorph> buffers_;
  39. };
  40. /// Description of vertex buffer data for asynchronous loading.
  41. struct VertexBufferDesc
  42. {
  43. /// Vertex count.
  44. i32 vertexCount_;
  45. /// Vertex declaration.
  46. Vector<VertexElement> vertexElements_;
  47. /// Vertex data size.
  48. i32 dataSize_;
  49. /// Vertex data.
  50. SharedArrayPtr<byte> data_;
  51. };
  52. /// Description of index buffer data for asynchronous loading.
  53. struct IndexBufferDesc
  54. {
  55. /// Index count.
  56. i32 indexCount_;
  57. /// Index size.
  58. i32 indexSize_;
  59. /// Index data size.
  60. i32 dataSize_;
  61. /// Index data.
  62. SharedArrayPtr<byte> data_;
  63. };
  64. /// Description of a geometry for asynchronous loading.
  65. struct GeometryDesc
  66. {
  67. /// Primitive type.
  68. PrimitiveType type_;
  69. /// Vertex buffer ref.
  70. i32 vbRef_;
  71. /// Index buffer ref.
  72. i32 ibRef_;
  73. /// Index start.
  74. i32 indexStart_;
  75. /// Index count.
  76. i32 indexCount_;
  77. };
  78. /// 3D model resource.
  79. class URHO3D_API Model : public ResourceWithMetadata
  80. {
  81. URHO3D_OBJECT(Model, ResourceWithMetadata);
  82. public:
  83. /// Construct.
  84. explicit Model(Context* context);
  85. /// Destruct.
  86. ~Model() override;
  87. /// Register object factory.
  88. /// @nobind
  89. static void RegisterObject(Context* context);
  90. /// Load resource from stream. May be called from a worker thread. Return true if successful.
  91. bool BeginLoad(Deserializer& source) override;
  92. /// Finish resource loading. Always called from the main thread. Return true if successful.
  93. bool EndLoad() override;
  94. /// Save resource. Return true if successful.
  95. bool Save(Serializer& dest) const override;
  96. /// Set local-space bounding box.
  97. /// @property
  98. void SetBoundingBox(const BoundingBox& box);
  99. /// Set vertex buffers and their morph ranges.
  100. bool SetVertexBuffers(const Vector<SharedPtr<VertexBuffer>>& buffers, const Vector<i32>& morphRangeStarts,
  101. const Vector<i32>& morphRangeCounts);
  102. /// Set index buffers.
  103. bool SetIndexBuffers(const Vector<SharedPtr<IndexBuffer>>& buffers);
  104. /// Set number of geometries.
  105. /// @property
  106. void SetNumGeometries(i32 num);
  107. /// Set number of LOD levels in a geometry.
  108. /// @property
  109. bool SetNumGeometryLodLevels(i32 index, i32 num);
  110. /// Set geometry.
  111. bool SetGeometry(i32 index, i32 lodLevel, Geometry* geometry);
  112. /// Set geometry center.
  113. /// @property{set_geometryCenters}
  114. bool SetGeometryCenter(i32 index, const Vector3& center);
  115. /// Set skeleton.
  116. void SetSkeleton(const Skeleton& skeleton);
  117. /// Set bone mappings when model has more bones than the skinning shader can handle.
  118. void SetGeometryBoneMappings(const Vector<Vector<i32>>& geometryBoneMappings);
  119. /// Set vertex morphs.
  120. void SetMorphs(const Vector<ModelMorph>& morphs);
  121. /// Clone the model. The geometry data is deep-copied and can be modified in the clone without affecting the original.
  122. SharedPtr<Model> Clone(const String& cloneName = String::EMPTY) const;
  123. /// Return bounding box.
  124. /// @property
  125. const BoundingBox& GetBoundingBox() const { return boundingBox_; }
  126. /// Return skeleton.
  127. /// @property
  128. Skeleton& GetSkeleton() { return skeleton_; }
  129. /// Return vertex buffers.
  130. const Vector<SharedPtr<VertexBuffer>>& GetVertexBuffers() const { return vertexBuffers_; }
  131. /// Return index buffers.
  132. const Vector<SharedPtr<IndexBuffer>>& GetIndexBuffers() const { return indexBuffers_; }
  133. /// Return number of geometries.
  134. /// @property
  135. i32 GetNumGeometries() const { return geometries_.Size(); }
  136. /// Return number of LOD levels in geometry.
  137. /// @property
  138. i32 GetNumGeometryLodLevels(i32 index) const;
  139. /// Return geometry pointers.
  140. const Vector<Vector<SharedPtr<Geometry>>>& GetGeometries() const { return geometries_; }
  141. /// Return geometry center points.
  142. const Vector<Vector3>& GetGeometryCenters() const { return geometryCenters_; }
  143. /// Return geometry by index and LOD level. The LOD level is clamped if out of range.
  144. Geometry* GetGeometry(i32 index, i32 lodLevel) const;
  145. /// Return geometry center by index.
  146. /// @property{get_geometryCenters}
  147. const Vector3& GetGeometryCenter(i32 index) const
  148. {
  149. assert(index >= 0);
  150. return index < geometryCenters_.Size() ? geometryCenters_[index] : Vector3::ZERO;
  151. }
  152. /// Return geometery bone mappings.
  153. const Vector<Vector<i32>>& GetGeometryBoneMappings() const { return geometryBoneMappings_; }
  154. /// Return vertex morphs.
  155. const Vector<ModelMorph>& GetMorphs() const { return morphs_; }
  156. /// Return number of vertex morphs.
  157. /// @property
  158. i32 GetNumMorphs() const { return morphs_.Size(); }
  159. /// Return vertex morph by index.
  160. const ModelMorph* GetMorph(i32 index) const;
  161. /// Return vertex morph by name.
  162. const ModelMorph* GetMorph(const String& name) const;
  163. /// Return vertex morph by name hash.
  164. const ModelMorph* GetMorph(StringHash nameHash) const;
  165. /// Return vertex buffer morph range start.
  166. i32 GetMorphRangeStart(i32 bufferIndex) const;
  167. /// Return vertex buffer morph range vertex count.
  168. i32 GetMorphRangeCount(i32 bufferIndex) const;
  169. private:
  170. /// Bounding box.
  171. BoundingBox boundingBox_;
  172. /// Skeleton.
  173. Skeleton skeleton_;
  174. /// Vertex buffers.
  175. Vector<SharedPtr<VertexBuffer>> vertexBuffers_;
  176. /// Index buffers.
  177. Vector<SharedPtr<IndexBuffer>> indexBuffers_;
  178. /// Geometries.
  179. Vector<Vector<SharedPtr<Geometry>>> geometries_;
  180. /// Geometry bone mappings.
  181. Vector<Vector<i32>> geometryBoneMappings_;
  182. /// Geometry centers.
  183. Vector<Vector3> geometryCenters_;
  184. /// Vertex morphs.
  185. Vector<ModelMorph> morphs_;
  186. /// Vertex buffer morph range start.
  187. Vector<i32> morphRangeStarts_;
  188. /// Vertex buffer morph range vertex count.
  189. Vector<i32> morphRangeCounts_;
  190. /// Vertex buffer data for asynchronous loading.
  191. Vector<VertexBufferDesc> loadVBData_;
  192. /// Index buffer data for asynchronous loading.
  193. Vector<IndexBufferDesc> loadIBData_;
  194. /// Geometry definitions for asynchronous loading.
  195. Vector<Vector<GeometryDesc>> loadGeometries_;
  196. };
  197. }