StaticModelGroup.h 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright (c) 2008-2022 the Urho3D project
  2. // License: MIT
  3. #pragma once
  4. #include "../Graphics/StaticModel.h"
  5. namespace Urho3D
  6. {
  7. /// 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.
  8. class URHO3D_API StaticModelGroup : public StaticModel
  9. {
  10. URHO3D_OBJECT(StaticModelGroup, StaticModel);
  11. public:
  12. /// Construct.
  13. explicit StaticModelGroup(Context* context);
  14. /// Destruct.
  15. ~StaticModelGroup() override;
  16. /// Register object factory. StaticModel must be registered first.
  17. /// @nobind
  18. static void RegisterObject(Context* context);
  19. /// Apply attribute changes that can not be applied immediately. Called after scene load or a network update.
  20. void ApplyAttributes() override;
  21. /// Process octree raycast. May be called from a worker thread.
  22. void ProcessRayQuery(const RayOctreeQuery& query, PODVector<RayQueryResult>& results) override;
  23. /// Calculate distance and prepare batches for rendering. May be called from worker thread(s), possibly re-entrantly.
  24. void UpdateBatches(const FrameInfo& frame) override;
  25. /// Return number of occlusion geometry triangles.
  26. unsigned GetNumOccluderTriangles() override;
  27. /// Draw to occlusion buffer. Return true if did not run out of triangles.
  28. bool DrawOcclusion(OcclusionBuffer* buffer) override;
  29. /// Add an instance scene node. It does not need any drawable components of its own.
  30. void AddInstanceNode(Node* node);
  31. /// Remove an instance scene node.
  32. void RemoveInstanceNode(Node* node);
  33. /// Remove all instance scene nodes.
  34. void RemoveAllInstanceNodes();
  35. /// Return number of instance nodes.
  36. /// @property
  37. unsigned GetNumInstanceNodes() const { return instanceNodes_.Size(); }
  38. /// Return instance node by index.
  39. /// @property{get_instanceNodes}
  40. Node* GetInstanceNode(unsigned index) const;
  41. /// Set node IDs attribute.
  42. void SetNodeIDsAttr(const VariantVector& value);
  43. /// Return node IDs attribute.
  44. const VariantVector& GetNodeIDsAttr() const;
  45. protected:
  46. /// Handle scene node enabled status changing.
  47. void OnNodeSetEnabled(Node* node) override;
  48. /// Recalculate the world-space bounding box.
  49. void OnWorldBoundingBoxUpdate() override;
  50. private:
  51. /// Ensure proper size of world transforms when nodes are added/removed. Also mark node IDs dirty.
  52. void UpdateNumTransforms();
  53. /// Update node IDs attribute from the actual nodes.
  54. void UpdateNodeIDs() const;
  55. /// Instance nodes.
  56. Vector<WeakPtr<Node>> instanceNodes_;
  57. /// World transforms of valid (existing and visible) instances.
  58. PODVector<Matrix3x4> worldTransforms_;
  59. /// IDs of instance nodes for serialization.
  60. mutable VariantVector nodeIDsAttr_;
  61. /// Number of valid instance node transforms.
  62. unsigned numWorldTransforms_{};
  63. /// Whether node IDs have been set and nodes should be searched for during ApplyAttributes.
  64. mutable bool nodesDirty_{};
  65. /// Whether nodes have been manipulated by the API and node ID attribute should be refreshed.
  66. mutable bool nodeIDsDirty_{};
  67. };
  68. }