BillboardSet.h 6.4 KB

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