StaticModel.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. //
  2. // Copyright (c) 2008-2022 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "../Graphics/Drawable.h"
  24. namespace Urho3D
  25. {
  26. class Model;
  27. /// Static model per-geometry extra data.
  28. struct StaticModelGeometryData
  29. {
  30. /// Geometry center.
  31. Vector3 center_;
  32. /// Current LOD level.
  33. unsigned lodLevel_;
  34. };
  35. /// Static model component.
  36. class URHO3D_API StaticModel : public Drawable
  37. {
  38. URHO3D_OBJECT(StaticModel, Drawable);
  39. public:
  40. /// Construct.
  41. explicit StaticModel(Context* context);
  42. /// Destruct.
  43. ~StaticModel() override;
  44. /// Register object factory. Drawable must be registered first.
  45. /// @nobind
  46. static void RegisterObject(Context* context);
  47. /// Process octree raycast. May be called from a worker thread.
  48. void ProcessRayQuery(const RayOctreeQuery& query, PODVector<RayQueryResult>& results) override;
  49. /// Calculate distance and prepare batches for rendering. May be called from worker thread(s), possibly re-entrantly.
  50. void UpdateBatches(const FrameInfo& frame) override;
  51. /// Return the geometry for a specific LOD level.
  52. Geometry* GetLodGeometry(unsigned batchIndex, unsigned level) override;
  53. /// Return number of occlusion geometry triangles.
  54. unsigned GetNumOccluderTriangles() override;
  55. /// Draw to occlusion buffer. Return true if did not run out of triangles.
  56. bool DrawOcclusion(OcclusionBuffer* buffer) override;
  57. /// Set model.
  58. /// @manualbind
  59. virtual void SetModel(Model* model);
  60. /// Set material on all geometries.
  61. /// @property
  62. virtual void SetMaterial(Material* material);
  63. /// Set material on one geometry. Return true if successful.
  64. /// @property{set_materials}
  65. virtual bool SetMaterial(unsigned index, Material* material);
  66. /// Set occlusion LOD level. By default (M_MAX_UNSIGNED) same as visible.
  67. /// @property
  68. void SetOcclusionLodLevel(unsigned level);
  69. /// Apply default materials from a material list file. If filename is empty (default), the model's resource name with extension .txt will be used.
  70. void ApplyMaterialList(const String& fileName = String::EMPTY);
  71. /// Return model.
  72. /// @property
  73. Model* GetModel() const { return model_; }
  74. /// Return number of geometries.
  75. /// @property
  76. unsigned GetNumGeometries() const { return geometries_.Size(); }
  77. /// Return material from the first geometry, assuming all the geometries use the same material.
  78. /// @property
  79. virtual Material* GetMaterial() const { return GetMaterial(0); }
  80. /// Return material by geometry index.
  81. /// @property{get_materials}
  82. virtual Material* GetMaterial(unsigned index) const;
  83. /// Return occlusion LOD level.
  84. /// @property
  85. unsigned GetOcclusionLodLevel() const { return occlusionLodLevel_; }
  86. /// Determines if the given world space point is within the model geometry.
  87. bool IsInside(const Vector3& point) const;
  88. /// Determines if the given local space point is within the model geometry.
  89. bool IsInsideLocal(const Vector3& point) const;
  90. /// Set model attribute.
  91. void SetModelAttr(const ResourceRef& value);
  92. /// Set materials attribute.
  93. void SetMaterialsAttr(const ResourceRefList& value);
  94. /// Return model attribute.
  95. ResourceRef GetModelAttr() const;
  96. /// Return materials attribute.
  97. const ResourceRefList& GetMaterialsAttr() const;
  98. protected:
  99. /// Recalculate the world-space bounding box.
  100. void OnWorldBoundingBoxUpdate() override;
  101. /// Set local-space bounding box.
  102. void SetBoundingBox(const BoundingBox& box);
  103. /// Set number of geometries.
  104. void SetNumGeometries(unsigned num);
  105. /// Reset LOD levels.
  106. void ResetLodLevels();
  107. /// Choose LOD levels based on distance.
  108. void CalculateLodLevels();
  109. /// Extra per-geometry data.
  110. PODVector<StaticModelGeometryData> geometryData_;
  111. /// All geometries.
  112. Vector<Vector<SharedPtr<Geometry> > > geometries_;
  113. /// Model.
  114. SharedPtr<Model> model_;
  115. /// Occlusion LOD level.
  116. unsigned occlusionLodLevel_;
  117. /// Material list attribute.
  118. mutable ResourceRefList materialsAttr_;
  119. private:
  120. /// Handle model reload finished.
  121. void HandleModelReloadFinished(StringHash eventType, VariantMap& eventData);
  122. };
  123. }