StaticModel.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. //
  2. // Copyright (c) 2008-2020 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. static void RegisterObject(Context* context);
  46. /// Process octree raycast. May be called from a worker thread.
  47. void ProcessRayQuery(const RayOctreeQuery& query, PODVector<RayQueryResult>& results) override;
  48. /// Calculate distance and prepare batches for rendering. May be called from worker thread(s), possibly re-entrantly.
  49. void UpdateBatches(const FrameInfo& frame) override;
  50. /// Return the geometry for a specific LOD level.
  51. Geometry* GetLodGeometry(unsigned batchIndex, unsigned level) override;
  52. /// Return number of occlusion geometry triangles.
  53. unsigned GetNumOccluderTriangles() override;
  54. /// Draw to occlusion buffer. Return true if did not run out of triangles.
  55. bool DrawOcclusion(OcclusionBuffer* buffer) override;
  56. /// Set model.
  57. /// @manualbind
  58. virtual void SetModel(Model* model);
  59. /// Set material on all geometries.
  60. /// @property
  61. virtual void SetMaterial(Material* material);
  62. /// Set material on one geometry. Return true if successful.
  63. /// @property{set_materials}
  64. virtual bool SetMaterial(unsigned index, Material* material);
  65. /// Set occlusion LOD level. By default (M_MAX_UNSIGNED) same as visible.
  66. /// @property
  67. void SetOcclusionLodLevel(unsigned level);
  68. /// Apply default materials from a material list file. If filename is empty (default), the model's resource name with extension .txt will be used.
  69. void ApplyMaterialList(const String& fileName = String::EMPTY);
  70. /// Return model.
  71. /// @property
  72. Model* GetModel() const { return model_; }
  73. /// Return number of geometries.
  74. /// @property
  75. unsigned GetNumGeometries() const { return geometries_.Size(); }
  76. /// Return material from the first geometry, assuming all the geometries use the same material.
  77. /// @property
  78. virtual Material* GetMaterial() const { return GetMaterial(0); }
  79. /// Return material by geometry index.
  80. /// @property{get_materials}
  81. virtual Material* GetMaterial(unsigned index) const;
  82. /// Return occlusion LOD level.
  83. /// @property
  84. unsigned GetOcclusionLodLevel() const { return occlusionLodLevel_; }
  85. /// Determines if the given world space point is within the model geometry.
  86. bool IsInside(const Vector3& point) const;
  87. /// Determines if the given local space point is within the model geometry.
  88. bool IsInsideLocal(const Vector3& point) const;
  89. /// Set model attribute.
  90. void SetModelAttr(const ResourceRef& value);
  91. /// Set materials attribute.
  92. void SetMaterialsAttr(const ResourceRefList& value);
  93. /// Return model attribute.
  94. ResourceRef GetModelAttr() const;
  95. /// Return materials attribute.
  96. const ResourceRefList& GetMaterialsAttr() const;
  97. protected:
  98. /// Recalculate the world-space bounding box.
  99. void OnWorldBoundingBoxUpdate() override;
  100. /// Set local-space bounding box.
  101. void SetBoundingBox(const BoundingBox& box);
  102. /// Set number of geometries.
  103. void SetNumGeometries(unsigned num);
  104. /// Reset LOD levels.
  105. void ResetLodLevels();
  106. /// Choose LOD levels based on distance.
  107. void CalculateLodLevels();
  108. /// Extra per-geometry data.
  109. PODVector<StaticModelGeometryData> geometryData_;
  110. /// All geometries.
  111. Vector<Vector<SharedPtr<Geometry> > > geometries_;
  112. /// Model.
  113. SharedPtr<Model> model_;
  114. /// Occlusion LOD level.
  115. unsigned occlusionLodLevel_;
  116. /// Material list attribute.
  117. mutable ResourceRefList materialsAttr_;
  118. private:
  119. /// Handle model reload finished.
  120. void HandleModelReloadFinished(StringHash eventType, VariantMap& eventData);
  121. };
  122. }