StaticModel.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Copyright (c) 2008-2022 the Urho3D project
  2. // License: MIT
  3. #pragma once
  4. #include "../Graphics/Drawable.h"
  5. namespace Urho3D
  6. {
  7. class Model;
  8. /// Static model per-geometry extra data.
  9. struct StaticModelGeometryData
  10. {
  11. /// Geometry center.
  12. Vector3 center_;
  13. /// Current LOD level.
  14. unsigned lodLevel_;
  15. };
  16. /// Static model component.
  17. class URHO3D_API StaticModel : public Drawable
  18. {
  19. URHO3D_OBJECT(StaticModel, Drawable);
  20. public:
  21. /// Construct.
  22. explicit StaticModel(Context* context);
  23. /// Destruct.
  24. ~StaticModel() override;
  25. /// Register object factory. Drawable must be registered first.
  26. /// @nobind
  27. static void RegisterObject(Context* context);
  28. /// Process octree raycast. May be called from a worker thread.
  29. void ProcessRayQuery(const RayOctreeQuery& query, PODVector<RayQueryResult>& results) override;
  30. /// Calculate distance and prepare batches for rendering. May be called from worker thread(s), possibly re-entrantly.
  31. void UpdateBatches(const FrameInfo& frame) override;
  32. /// Return the geometry for a specific LOD level.
  33. Geometry* GetLodGeometry(unsigned batchIndex, unsigned level) override;
  34. /// Return number of occlusion geometry triangles.
  35. unsigned GetNumOccluderTriangles() override;
  36. /// Draw to occlusion buffer. Return true if did not run out of triangles.
  37. bool DrawOcclusion(OcclusionBuffer* buffer) override;
  38. /// Set model.
  39. /// @manualbind
  40. virtual void SetModel(Model* model);
  41. /// Set material on all geometries.
  42. /// @property
  43. virtual void SetMaterial(Material* material);
  44. /// Set material on one geometry. Return true if successful.
  45. /// @property{set_materials}
  46. virtual bool SetMaterial(unsigned index, Material* material);
  47. /// Set occlusion LOD level. By default (M_MAX_UNSIGNED) same as visible.
  48. /// @property
  49. void SetOcclusionLodLevel(unsigned level);
  50. /// Apply default materials from a material list file. If filename is empty (default), the model's resource name with extension .txt will be used.
  51. void ApplyMaterialList(const String& fileName = String::EMPTY);
  52. /// Return model.
  53. /// @property
  54. Model* GetModel() const { return model_; }
  55. /// Return number of geometries.
  56. /// @property
  57. unsigned GetNumGeometries() const { return geometries_.Size(); }
  58. /// Return material from the first geometry, assuming all the geometries use the same material.
  59. /// @property
  60. virtual Material* GetMaterial() const { return GetMaterial(0); }
  61. /// Return material by geometry index.
  62. /// @property{get_materials}
  63. virtual Material* GetMaterial(unsigned index) const;
  64. /// Return occlusion LOD level.
  65. /// @property
  66. unsigned GetOcclusionLodLevel() const { return occlusionLodLevel_; }
  67. /// Determines if the given world space point is within the model geometry.
  68. bool IsInside(const Vector3& point) const;
  69. /// Determines if the given local space point is within the model geometry.
  70. bool IsInsideLocal(const Vector3& point) const;
  71. /// Set model attribute.
  72. void SetModelAttr(const ResourceRef& value);
  73. /// Set materials attribute.
  74. void SetMaterialsAttr(const ResourceRefList& value);
  75. /// Return model attribute.
  76. ResourceRef GetModelAttr() const;
  77. /// Return materials attribute.
  78. const ResourceRefList& GetMaterialsAttr() const;
  79. protected:
  80. /// Recalculate the world-space bounding box.
  81. void OnWorldBoundingBoxUpdate() override;
  82. /// Set local-space bounding box.
  83. void SetBoundingBox(const BoundingBox& box);
  84. /// Set number of geometries.
  85. void SetNumGeometries(unsigned num);
  86. /// Reset LOD levels.
  87. void ResetLodLevels();
  88. /// Choose LOD levels based on distance.
  89. void CalculateLodLevels();
  90. /// Extra per-geometry data.
  91. PODVector<StaticModelGeometryData> geometryData_;
  92. /// All geometries.
  93. Vector<Vector<SharedPtr<Geometry>>> geometries_;
  94. /// Model.
  95. SharedPtr<Model> model_;
  96. /// Occlusion LOD level.
  97. unsigned occlusionLodLevel_;
  98. /// Material list attribute.
  99. mutable ResourceRefList materialsAttr_;
  100. private:
  101. /// Handle model reload finished.
  102. void HandleModelReloadFinished(StringHash eventType, VariantMap& eventData);
  103. };
  104. }