StaticModelGroup.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. //
  2. // Copyright (c) 2008-2020 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. static void RegisterObject(Context* context);
  37. /// Apply attribute changes that can not be applied immediately. Called after scene load or a network update.
  38. void ApplyAttributes() override;
  39. /// Process octree raycast. May be called from a worker thread.
  40. void ProcessRayQuery(const RayOctreeQuery& query, PODVector<RayQueryResult>& results) override;
  41. /// Calculate distance and prepare batches for rendering. May be called from worker thread(s), possibly re-entrantly.
  42. void UpdateBatches(const FrameInfo& frame) override;
  43. /// Return number of occlusion geometry triangles.
  44. unsigned GetNumOccluderTriangles() override;
  45. /// Draw to occlusion buffer. Return true if did not run out of triangles.
  46. bool DrawOcclusion(OcclusionBuffer* buffer) override;
  47. /// Add an instance scene node. It does not need any drawable components of its own.
  48. void AddInstanceNode(Node* node);
  49. /// Remove an instance scene node.
  50. void RemoveInstanceNode(Node* node);
  51. /// Remove all instance scene nodes.
  52. void RemoveAllInstanceNodes();
  53. /// Return number of instance nodes.
  54. /// @property
  55. unsigned GetNumInstanceNodes() const { return instanceNodes_.Size(); }
  56. /// Return instance node by index.
  57. /// @property{get_instanceNodes}
  58. Node* GetInstanceNode(unsigned index) const;
  59. /// Set node IDs attribute.
  60. void SetNodeIDsAttr(const VariantVector& value);
  61. /// Return node IDs attribute.
  62. const VariantVector& GetNodeIDsAttr() const;
  63. protected:
  64. /// Handle scene node enabled status changing.
  65. void OnNodeSetEnabled(Node* node) override;
  66. /// Recalculate the world-space bounding box.
  67. void OnWorldBoundingBoxUpdate() override;
  68. private:
  69. /// Ensure proper size of world transforms when nodes are added/removed. Also mark node IDs dirty.
  70. void UpdateNumTransforms();
  71. /// Update node IDs attribute from the actual nodes.
  72. void UpdateNodeIDs() const;
  73. /// Instance nodes.
  74. Vector<WeakPtr<Node> > instanceNodes_;
  75. /// World transforms of valid (existing and visible) instances.
  76. PODVector<Matrix3x4> worldTransforms_;
  77. /// IDs of instance nodes for serialization.
  78. mutable VariantVector nodeIDsAttr_;
  79. /// Number of valid instance node transforms.
  80. unsigned numWorldTransforms_{};
  81. /// Whether node IDs have been set and nodes should be searched for during ApplyAttributes.
  82. mutable bool nodesDirty_{};
  83. /// Whether nodes have been manipulated by the API and node ID attribute should be refreshed.
  84. mutable bool nodeIDsDirty_{};
  85. };
  86. }