BillboardSet.h 5.9 KB

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