StaticModel.h 4.9 KB

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