BillboardSet.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Oorni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #pragma once
  24. #include "Color.h"
  25. #include "Drawable.h"
  26. #include "Rect.h"
  27. #include "VectorBuffer.h"
  28. namespace Urho3D
  29. {
  30. class IndexBuffer;
  31. class Graphics;
  32. class VertexBuffer;
  33. /// One billboard in the billboard set.
  34. struct Billboard
  35. {
  36. /// Position.
  37. Vector3 position_;
  38. /// Two-dimensional size.
  39. Vector2 size_;
  40. /// UV coordinates.
  41. Rect uv_;
  42. /// Color.
  43. Color color_;
  44. /// Rotation.
  45. float rotation_;
  46. /// Enabled flag.
  47. bool enabled_;
  48. /// Sort distance.
  49. float sortDistance_;
  50. };
  51. /// %Billboard component.
  52. class BillboardSet : public Drawable
  53. {
  54. OBJECT(BillboardSet);
  55. public:
  56. /// Construct.
  57. BillboardSet(Context* context);
  58. /// Destruct.
  59. virtual ~BillboardSet();
  60. /// Register object factory.
  61. static void RegisterObject(Context* context);
  62. /// Calculate distance and prepare batches for rendering. May be called from worker thread(s), possibly re-entrantly.
  63. virtual void UpdateBatches(const FrameInfo& frame);
  64. /// Prepare geometry for rendering. Called from a worker thread if possible (no GPU update.)
  65. virtual void UpdateGeometry(const FrameInfo& frame);
  66. /// Return whether a geometry update is necessary, and if it can happen in a worker thread.
  67. virtual UpdateGeometryType GetUpdateGeometryType();
  68. /// Set material.
  69. void SetMaterial(Material* material);
  70. /// Set number of billboards.
  71. void SetNumBillboards(unsigned num);
  72. /// Set whether billboards are relative to the scene node.
  73. void SetRelative(bool enable);
  74. /// Set whether scene node scale affects billboards' size.
  75. void SetScaled(bool enable);
  76. /// Set whether billboards are sorted by distance.
  77. void SetSorted(bool enable);
  78. /// Set animation LOD bias.
  79. void SetAnimationLodBias(float bias);
  80. /// Call after changing the billboards.
  81. void Updated();
  82. /// Return material.
  83. Material* GetMaterial() const;
  84. /// Return number of billboards.
  85. unsigned GetNumBillboards() const { return billboards_.Size(); }
  86. /// Return all billboards.
  87. PODVector<Billboard>& GetBillboards() { return billboards_; }
  88. /// Return billboard by index.
  89. Billboard* GetBillboard(unsigned index);
  90. /// Return whether billboards are relative to the scene node.
  91. bool IsRelative() const { return relative_; }
  92. /// Return whether scene node scale affects billboards' size.
  93. bool IsScaled() const { return scaled_; }
  94. /// Return whether billboards are sorted.
  95. bool IsSorted() const { return sorted_; }
  96. /// Return animation LOD bias.
  97. float GetAnimationLodBias() const { return animationLodBias_; }
  98. /// Set material attribute.
  99. void SetMaterialAttr(ResourceRef value);
  100. /// Set billboards attribute.
  101. void SetBillboardsAttr(VariantVector value);
  102. /// Set billboards attribute for network replication.
  103. void SetNetBillboardsAttr(const PODVector<unsigned char>& value);
  104. /// Return material attribute.
  105. ResourceRef GetMaterialAttr() const;
  106. /// Return billboards attribute.
  107. VariantVector GetBillboardsAttr() const;
  108. /// Return billboards attribute for network replication.
  109. const PODVector<unsigned char>& GetNetBillboardsAttr() const;
  110. protected:
  111. /// Recalculate the world-space bounding box.
  112. virtual void OnWorldBoundingBoxUpdate();
  113. /// Mark billboard vertex buffer to need an update.
  114. void MarkPositionsDirty();
  115. /// Billboards.
  116. PODVector<Billboard> billboards_;
  117. /// Animation LOD bias.
  118. float animationLodBias_;
  119. /// Animation LOD timer.
  120. float animationLodTimer_;
  121. /// Billboards relative flag.
  122. bool relative_;
  123. /// Scale affects billboard scale flag.
  124. bool scaled_;
  125. /// Billboards sorted flag.
  126. bool sorted_;
  127. private:
  128. /// Resize billboard vertex and index buffers.
  129. void UpdateBufferSize();
  130. /// Rewrite billboard vertex buffer.
  131. void UpdateVertexBuffer(const FrameInfo& frame);
  132. /// Geometry.
  133. SharedPtr<Geometry> geometry_;
  134. /// Vertex buffer.
  135. SharedPtr<VertexBuffer> vertexBuffer_;
  136. /// Index buffer.
  137. SharedPtr<IndexBuffer> indexBuffer_;
  138. /// Buffers need resize flag.
  139. bool bufferSizeDirty_;
  140. /// Vertex buffer needs rewrite flag.
  141. bool bufferDirty_;
  142. /// Force update flag (ignore animation LOD momentarily.)
  143. bool forceUpdate_;
  144. /// Frame number on which was last sorted.
  145. unsigned sortFrameNumber_;
  146. /// Previous offset to camera for determining whether sorting is necessary.
  147. Vector3 previousOffset_;
  148. /// Billboard pointers for sorting.
  149. Vector<Billboard*> sortedBillboards_;
  150. /// Attribute buffer for network replication.
  151. mutable VectorBuffer attrBuffer_;
  152. };
  153. }