BillboardSet.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. //
  2. // Copyright (c) 2008-2013 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 "Color.h"
  24. #include "Drawable.h"
  25. #include "Rect.h"
  26. #include "VectorBuffer.h"
  27. namespace Urho3D
  28. {
  29. class IndexBuffer;
  30. class VertexBuffer;
  31. /// One billboard in the billboard set.
  32. struct URHO3D_API Billboard
  33. {
  34. /// Position.
  35. Vector3 position_;
  36. /// Two-dimensional size.
  37. Vector2 size_;
  38. /// UV coordinates.
  39. Rect uv_;
  40. /// Color.
  41. Color color_;
  42. /// Rotation.
  43. float rotation_;
  44. /// Enabled flag.
  45. bool enabled_;
  46. /// Sort distance.
  47. float sortDistance_;
  48. };
  49. static const unsigned MAX_BILLBOARDS = 65536 / 4;
  50. /// %Billboard component.
  51. class URHO3D_API BillboardSet : public Drawable
  52. {
  53. OBJECT(BillboardSet);
  54. public:
  55. /// Construct.
  56. BillboardSet(Context* context);
  57. /// Destruct.
  58. virtual ~BillboardSet();
  59. /// Register object factory.
  60. static void RegisterObject(Context* context);
  61. /// Calculate distance and prepare batches for rendering. May be called from worker thread(s), possibly re-entrantly.
  62. virtual void UpdateBatches(const FrameInfo& frame);
  63. /// Prepare geometry for rendering. Called from a worker thread if possible (no GPU update.)
  64. virtual void UpdateGeometry(const FrameInfo& frame);
  65. /// Return whether a geometry update is necessary, and if it can happen in a worker thread.
  66. virtual UpdateGeometryType GetUpdateGeometryType();
  67. /// Set material.
  68. void SetMaterial(Material* material);
  69. /// Set number of billboards.
  70. void SetNumBillboards(unsigned num);
  71. /// Set whether billboards are relative to the scene node.
  72. void SetRelative(bool enable);
  73. /// Set whether scene node scale affects billboards' size.
  74. void SetScaled(bool enable);
  75. /// Set whether billboards are sorted by distance.
  76. void SetSorted(bool enable);
  77. /// Set animation LOD bias.
  78. void SetAnimationLodBias(float bias);
  79. /// Mark for bounding box and vertex buffer update. Call after modifying the billboards.
  80. void Commit();
  81. /// Return material.
  82. Material* GetMaterial() const;
  83. /// Return number of billboards.
  84. unsigned GetNumBillboards() const { return billboards_.Size(); }
  85. /// Return all billboards.
  86. PODVector<Billboard>& GetBillboards() { return billboards_; }
  87. /// Return billboard by index.
  88. Billboard* GetBillboard(unsigned index);
  89. /// Return whether billboards are relative to the scene node.
  90. bool IsRelative() const { return relative_; }
  91. /// Return whether scene node scale affects billboards' size.
  92. bool IsScaled() const { return scaled_; }
  93. /// Return whether billboards are sorted.
  94. bool IsSorted() const { return sorted_; }
  95. /// Return animation LOD bias.
  96. float GetAnimationLodBias() const { return animationLodBias_; }
  97. /// Set material attribute.
  98. void SetMaterialAttr(ResourceRef value);
  99. /// Set billboards attribute.
  100. void SetBillboardsAttr(VariantVector value);
  101. /// Set billboards attribute for network replication.
  102. void SetNetBillboardsAttr(const PODVector<unsigned char>& value);
  103. /// Return material attribute.
  104. ResourceRef GetMaterialAttr() const;
  105. /// Return billboards attribute.
  106. VariantVector GetBillboardsAttr() const;
  107. /// Return billboards attribute for network replication.
  108. const PODVector<unsigned char>& GetNetBillboardsAttr() const;
  109. protected:
  110. /// Recalculate the world-space bounding box.
  111. virtual void OnWorldBoundingBoxUpdate();
  112. /// Mark billboard vertex buffer to need an update.
  113. void MarkPositionsDirty();
  114. /// Billboards.
  115. PODVector<Billboard> billboards_;
  116. /// Animation LOD bias.
  117. float animationLodBias_;
  118. /// Animation LOD timer.
  119. float animationLodTimer_;
  120. /// Billboards relative flag.
  121. bool relative_;
  122. /// Scale affects billboard scale flag.
  123. bool scaled_;
  124. /// Billboards sorted flag.
  125. bool sorted_;
  126. private:
  127. /// Resize billboard vertex and index buffers.
  128. void UpdateBufferSize();
  129. /// Rewrite billboard vertex buffer.
  130. void UpdateVertexBuffer(const FrameInfo& frame);
  131. /// Geometry.
  132. SharedPtr<Geometry> geometry_;
  133. /// Vertex buffer.
  134. SharedPtr<VertexBuffer> vertexBuffer_;
  135. /// Index buffer.
  136. SharedPtr<IndexBuffer> indexBuffer_;
  137. /// Buffers need resize flag.
  138. bool bufferSizeDirty_;
  139. /// Vertex buffer needs rewrite flag.
  140. bool bufferDirty_;
  141. /// Force update flag (ignore animation LOD momentarily.)
  142. bool forceUpdate_;
  143. /// Frame number on which was last sorted.
  144. unsigned sortFrameNumber_;
  145. /// Previous offset to camera for determining whether sorting is necessary.
  146. Vector3 previousOffset_;
  147. /// Billboard pointers for sorting.
  148. Vector<Billboard*> sortedBillboards_;
  149. /// Attribute buffer for network replication.
  150. mutable VectorBuffer attrBuffer_;
  151. };
  152. }