StaticModelGroup.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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/StaticModel.h"
  24. namespace Urho3D
  25. {
  26. /// Renders several object instances while culling and receiving light as one unit. Can be used as a CPU-side optimization, but note that also regular StaticModels will use instanced rendering if possible.
  27. class URHO3D_API StaticModelGroup : public StaticModel
  28. {
  29. URHO3D_OBJECT(StaticModelGroup, StaticModel);
  30. public:
  31. /// Construct.
  32. explicit StaticModelGroup(Context* context);
  33. /// Destruct.
  34. ~StaticModelGroup() override;
  35. /// Register object factory. StaticModel must be registered first.
  36. /// @nobind
  37. static void RegisterObject(Context* context);
  38. /// Apply attribute changes that can not be applied immediately. Called after scene load or a network update.
  39. void ApplyAttributes() override;
  40. /// Process octree raycast. May be called from a worker thread.
  41. void ProcessRayQuery(const RayOctreeQuery& query, PODVector<RayQueryResult>& results) override;
  42. /// Calculate distance and prepare batches for rendering. May be called from worker thread(s), possibly re-entrantly.
  43. void UpdateBatches(const FrameInfo& frame) override;
  44. /// Return number of occlusion geometry triangles.
  45. unsigned GetNumOccluderTriangles() override;
  46. /// Draw to occlusion buffer. Return true if did not run out of triangles.
  47. bool DrawOcclusion(OcclusionBuffer* buffer) override;
  48. /// Add an instance scene node. It does not need any drawable components of its own.
  49. void AddInstanceNode(Node* node);
  50. /// Remove an instance scene node.
  51. void RemoveInstanceNode(Node* node);
  52. /// Remove all instance scene nodes.
  53. void RemoveAllInstanceNodes();
  54. /// Return number of instance nodes.
  55. /// @property
  56. unsigned GetNumInstanceNodes() const { return instanceNodes_.Size(); }
  57. /// Return instance node by index.
  58. /// @property{get_instanceNodes}
  59. Node* GetInstanceNode(unsigned index) const;
  60. /// Set node IDs attribute.
  61. void SetNodeIDsAttr(const VariantVector& value);
  62. /// Return node IDs attribute.
  63. const VariantVector& GetNodeIDsAttr() const;
  64. protected:
  65. /// Handle scene node enabled status changing.
  66. void OnNodeSetEnabled(Node* node) override;
  67. /// Recalculate the world-space bounding box.
  68. void OnWorldBoundingBoxUpdate() override;
  69. private:
  70. /// Ensure proper size of world transforms when nodes are added/removed. Also mark node IDs dirty.
  71. void UpdateNumTransforms();
  72. /// Update node IDs attribute from the actual nodes.
  73. void UpdateNodeIDs() const;
  74. /// Instance nodes.
  75. Vector<WeakPtr<Node> > instanceNodes_;
  76. /// World transforms of valid (existing and visible) instances.
  77. PODVector<Matrix3x4> worldTransforms_;
  78. /// IDs of instance nodes for serialization.
  79. mutable VariantVector nodeIDsAttr_;
  80. /// Number of valid instance node transforms.
  81. unsigned numWorldTransforms_{};
  82. /// Whether node IDs have been set and nodes should be searched for during ApplyAttributes.
  83. mutable bool nodesDirty_{};
  84. /// Whether nodes have been manipulated by the API and node ID attribute should be refreshed.
  85. mutable bool nodeIDsDirty_{};
  86. };
  87. }