StaticModel.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 Atomic
  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. // ATOMIC BEGIN
  35. bool enabled_;
  36. Geometry* batchGeometry_;
  37. // ATOMIC END
  38. };
  39. /// Static model component.
  40. class ATOMIC_API StaticModel : public Drawable
  41. {
  42. ATOMIC_OBJECT(StaticModel, Drawable);
  43. public:
  44. /// Construct.
  45. StaticModel(Context* context);
  46. /// Destruct.
  47. ~StaticModel();
  48. /// Register object factory. Drawable must be registered first.
  49. static void RegisterObject(Context* context);
  50. /// Process octree raycast. May be called from a worker thread.
  51. virtual void ProcessRayQuery(const RayOctreeQuery& query, PODVector<RayQueryResult>& results);
  52. /// Calculate distance and prepare batches for rendering. May be called from worker thread(s), possibly re-entrantly.
  53. virtual void UpdateBatches(const FrameInfo& frame);
  54. /// Return the geometry for a specific LOD level.
  55. virtual Geometry* GetLodGeometry(unsigned batchIndex, unsigned level);
  56. /// Return number of occlusion geometry triangles.
  57. virtual unsigned GetNumOccluderTriangles();
  58. /// Draw to occlusion buffer. Return true if did not run out of triangles.
  59. virtual bool DrawOcclusion(OcclusionBuffer* buffer);
  60. /// Set model.
  61. virtual void SetModel(Model* model);
  62. /// Set material on all geometries.
  63. virtual void SetMaterial(Material* material);
  64. /// Set material on one geometry. Return true if successful.
  65. virtual bool SetMaterial(unsigned index, Material* material);
  66. /// Set occlusion LOD level. By default (M_MAX_UNSIGNED) same as visible.
  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. Model* GetModel() const { return model_; }
  72. /// Return number of geometries.
  73. unsigned GetNumGeometries() const { return geometries_.Size(); }
  74. /// Return material by geometry index.
  75. virtual Material* GetMaterial(unsigned index = 0) const;
  76. /// Return occlusion LOD level.
  77. unsigned GetOcclusionLodLevel() const { return occlusionLodLevel_; }
  78. /// Determines if the given world space point is within the model geometry.
  79. bool IsInside(const Vector3& point) const;
  80. /// Determines if the given local space point is within the model geometry.
  81. bool IsInsideLocal(const Vector3& point) const;
  82. /// Set model attribute.
  83. void SetModelAttr(const ResourceRef& value);
  84. /// Set materials attribute.
  85. void SetMaterialsAttr(const ResourceRefList& value);
  86. /// Return model attribute.
  87. ResourceRef GetModelAttr() const;
  88. /// Return materials attribute.
  89. const ResourceRefList& GetMaterialsAttr() const;
  90. // ATOMIC BEGIN
  91. /// Get whether a named submesh is visible
  92. bool GetGeometryVisible(const String& name);
  93. /// Show a named submesh
  94. void ShowGeometry(const String& name);
  95. /// Hide a named submesh
  96. void HideGeometry(const String& name);
  97. /// Returns true if any geometry is hidden in the model
  98. bool GetGeometryHidden() const { return geometryDisabled_; }
  99. void SetGeometryEnabledAttr(const VariantVector& value);
  100. const VariantVector& GetGeometryEnabledAttr() const;
  101. // ATOMIC END
  102. protected:
  103. /// Recalculate the world-space bounding box.
  104. virtual void OnWorldBoundingBoxUpdate();
  105. /// Set local-space bounding box.
  106. void SetBoundingBox(const BoundingBox& box);
  107. /// Set number of geometries.
  108. void SetNumGeometries(unsigned num);
  109. /// Reset LOD levels.
  110. void ResetLodLevels();
  111. /// Choose LOD levels based on distance.
  112. void CalculateLodLevels();
  113. /// Extra per-geometry data.
  114. PODVector<StaticModelGeometryData> geometryData_;
  115. /// All geometries.
  116. Vector<Vector<SharedPtr<Geometry> > > geometries_;
  117. /// Model.
  118. SharedPtr<Model> model_;
  119. /// Occlusion LOD level.
  120. unsigned occlusionLodLevel_;
  121. /// Material list attribute.
  122. mutable ResourceRefList materialsAttr_;
  123. // ATOMIC BEGIN
  124. // Apply geometry hiding when updating batches
  125. void UpdateBatchesHideGeometry();
  126. mutable VariantVector geometryEnabled_;
  127. /// true if any geometry has been disabled
  128. mutable bool geometryDisabled_;
  129. // ATOMIC END
  130. private:
  131. /// Handle model reload finished.
  132. void HandleModelReloadFinished(StringHash eventType, VariantMap& eventData);
  133. };
  134. }