StaticModel.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. bool GetLightmap() const { return lightmap_; }
  102. void SetLightmap(bool lightmap) { lightmap_ = lightmap; }
  103. float GetLightmapScale() const { return lightmapScale_; }
  104. void SetLightmapScale(float scale) { lightmapScale_ = scale; }
  105. unsigned GetLightmapSize() const { return lightmapSize_; }
  106. void SetLightmapSize(unsigned size) { lightmapSize_ = size; }
  107. unsigned GetLightmapIndex() const { return lightmapIndex_; }
  108. void SetLightmapIndex(unsigned idx) { lightmapIndex_ = idx; }
  109. const Vector4& GetLightmapTilingOffset() const { return lightmapTilingOffset_; }
  110. void SetLightmapTilingOffset(Vector4 tilingOffset) { lightmapTilingOffset_ = tilingOffset; }
  111. // ATOMIC END
  112. protected:
  113. /// Recalculate the world-space bounding box.
  114. virtual void OnWorldBoundingBoxUpdate();
  115. /// Set local-space bounding box.
  116. void SetBoundingBox(const BoundingBox& box);
  117. /// Set number of geometries.
  118. void SetNumGeometries(unsigned num);
  119. /// Reset LOD levels.
  120. void ResetLodLevels();
  121. /// Choose LOD levels based on distance.
  122. void CalculateLodLevels();
  123. /// Extra per-geometry data.
  124. PODVector<StaticModelGeometryData> geometryData_;
  125. /// All geometries.
  126. Vector<Vector<SharedPtr<Geometry> > > geometries_;
  127. /// Model.
  128. SharedPtr<Model> model_;
  129. /// Occlusion LOD level.
  130. unsigned occlusionLodLevel_;
  131. /// Material list attribute.
  132. mutable ResourceRefList materialsAttr_;
  133. // ATOMIC BEGIN
  134. // Apply geometry hiding when updating batches
  135. void UpdateBatchesHideGeometry();
  136. mutable VariantVector geometryEnabled_;
  137. /// true if any geometry has been disabled
  138. mutable bool geometryDisabled_;
  139. bool lightmap_;
  140. float lightmapScale_;
  141. unsigned lightmapSize_;
  142. unsigned lightmapIndex_;
  143. Vector4 lightmapTilingOffset_;
  144. // ATOMIC END
  145. private:
  146. /// Handle model reload finished.
  147. void HandleModelReloadFinished(StringHash eventType, VariantMap& eventData);
  148. };
  149. }