BillboardSet.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. //
  2. // Copyright (c) 2008-2015 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/Drawable.h"
  24. #include "../IO/VectorBuffer.h"
  25. #include "../Math/Color.h"
  26. #include "../Math/Matrix3x4.h"
  27. #include "../Math/Rect.h"
  28. namespace Atomic
  29. {
  30. class IndexBuffer;
  31. class VertexBuffer;
  32. /// One billboard in the billboard set.
  33. class ATOMIC_API Billboard : public RefCounted
  34. {
  35. friend class BillboardSet;
  36. friend class ParticleEmitter;
  37. REFCOUNTED(Billboard);
  38. public:
  39. Billboard();
  40. virtual ~Billboard();
  41. Vector3 GetPosition() { return position_; }
  42. void SetPosition(Vector3 &position) { position_ = position; }
  43. Vector3 GetSize() { return size_; }
  44. void SetSize(Vector2 &size) { size_ = size; }
  45. Rect GetUV() { return uv_; }
  46. void SetUV(Rect &uv) { uv_ = uv; }
  47. Color GetColor() { return color_; }
  48. void SetColor(Color &color) { color_ = color; }
  49. float GetRotation() { return rotation_; }
  50. void SetRotation(float rotation) { rotation_ = rotation; }
  51. bool IsEnabled() { return enabled_; }
  52. void SetEnabled(bool enabled) { enabled_ = enabled; }
  53. float GetSortDistance() { return sortDistance_; }
  54. void SetSortDistance(float sortDistance) { sortDistance_ = sortDistance; }
  55. private:
  56. /// Position.
  57. Vector3 position_;
  58. /// Two-dimensional size.
  59. Vector2 size_;
  60. /// UV coordinates.
  61. Rect uv_;
  62. /// Color.
  63. Color color_;
  64. /// Rotation.
  65. float rotation_;
  66. /// Enabled flag.
  67. bool enabled_;
  68. /// Sort distance.
  69. float sortDistance_;
  70. };
  71. static const unsigned MAX_BILLBOARDS = 65536 / 4;
  72. /// %Billboard component.
  73. class ATOMIC_API BillboardSet : public Drawable
  74. {
  75. OBJECT(BillboardSet);
  76. public:
  77. /// Construct.
  78. BillboardSet(Context* context);
  79. /// Destruct.
  80. virtual ~BillboardSet();
  81. /// Register object factory.
  82. static void RegisterObject(Context* context);
  83. /// Process octree raycast. May be called from a worker thread.
  84. virtual void ProcessRayQuery(const RayOctreeQuery& query, PODVector<RayQueryResult>& results);
  85. /// Calculate distance and prepare batches for rendering. May be called from worker thread(s), possibly re-entrantly.
  86. virtual void UpdateBatches(const FrameInfo& frame);
  87. /// Prepare geometry for rendering. Called from a worker thread if possible (no GPU update.)
  88. virtual void UpdateGeometry(const FrameInfo& frame);
  89. /// Return whether a geometry update is necessary, and if it can happen in a worker thread.
  90. virtual UpdateGeometryType GetUpdateGeometryType();
  91. /// Set material.
  92. void SetMaterial(Material* material);
  93. /// Set number of billboards.
  94. void SetNumBillboards(unsigned num);
  95. /// Set whether billboards are relative to the scene node. Default true.
  96. void SetRelative(bool enable);
  97. /// Set whether scene node scale affects billboards' size. Default true.
  98. void SetScaled(bool enable);
  99. /// Set whether billboards are sorted by distance. Default false.
  100. void SetSorted(bool enable);
  101. /// Set how the billboards should rotate in relation to the camera. Default is to follow camera rotation on all axes (FC_ROTATE_XYZ.)
  102. void SetFaceCameraMode(FaceCameraMode mode);
  103. /// Set animation LOD bias.
  104. void SetAnimationLodBias(float bias);
  105. /// Mark for bounding box and vertex buffer update. Call after modifying the billboards.
  106. void Commit();
  107. /// Return material.
  108. Material* GetMaterial() const;
  109. /// Return number of billboards.
  110. unsigned GetNumBillboards() const { return billboards_.Size(); }
  111. /// Return all billboards.
  112. Vector<SharedPtr<Billboard>>& GetBillboards() { return billboards_; }
  113. /// Return billboard by index.
  114. Billboard* GetBillboard(unsigned index);
  115. /// Return whether billboards are relative to the scene node.
  116. bool IsRelative() const { return relative_; }
  117. /// Return whether scene node scale affects billboards' size.
  118. bool IsScaled() const { return scaled_; }
  119. /// Return whether billboards are sorted.
  120. bool IsSorted() const { return sorted_; }
  121. /// Return how the billboards rotate in relation to the camera.
  122. FaceCameraMode GetFaceCameraMode() const { return faceCameraMode_; }
  123. /// Return animation LOD bias.
  124. float GetAnimationLodBias() const { return animationLodBias_; }
  125. /// Set material attribute.
  126. void SetMaterialAttr(const ResourceRef& value);
  127. /// Set billboards attribute.
  128. void SetBillboardsAttr(const VariantVector& value);
  129. /// Set billboards attribute for network replication.
  130. void SetNetBillboardsAttr(const PODVector<unsigned char>& value);
  131. /// Return material attribute.
  132. ResourceRef GetMaterialAttr() const;
  133. /// Return billboards attribute.
  134. VariantVector GetBillboardsAttr() const;
  135. /// Return billboards attribute for network replication.
  136. const PODVector<unsigned char>& GetNetBillboardsAttr() const;
  137. protected:
  138. /// Recalculate the world-space bounding box.
  139. virtual void OnWorldBoundingBoxUpdate();
  140. /// Mark billboard vertex buffer to need an update.
  141. void MarkPositionsDirty();
  142. /// Billboards.
  143. Vector<SharedPtr<Billboard>> billboards_;
  144. /// Coordinate axes on which camera facing is done.
  145. Vector3 faceCameraAxes_;
  146. /// Animation LOD bias.
  147. float animationLodBias_;
  148. /// Animation LOD timer.
  149. float animationLodTimer_;
  150. /// Billboards relative flag.
  151. bool relative_;
  152. /// Scale affects billboard scale flag.
  153. bool scaled_;
  154. /// Billboards sorted flag.
  155. bool sorted_;
  156. /// Billboard rotation mode in relation to the camera.
  157. FaceCameraMode faceCameraMode_;
  158. private:
  159. /// Resize billboard vertex and index buffers.
  160. void UpdateBufferSize();
  161. /// Rewrite billboard vertex buffer.
  162. void UpdateVertexBuffer(const FrameInfo& frame);
  163. /// Geometry.
  164. SharedPtr<Geometry> geometry_;
  165. /// Vertex buffer.
  166. SharedPtr<VertexBuffer> vertexBuffer_;
  167. /// Index buffer.
  168. SharedPtr<IndexBuffer> indexBuffer_;
  169. /// Transform matrices for position and billboard orientation.
  170. Matrix3x4 transforms_[2];
  171. /// Buffers need resize flag.
  172. bool bufferSizeDirty_;
  173. /// Vertex buffer needs rewrite flag.
  174. bool bufferDirty_;
  175. /// Force update flag (ignore animation LOD momentarily.)
  176. bool forceUpdate_;
  177. /// Sorting flag. Triggers a vertex buffer rewrite for each view this billboard set is rendered from.
  178. bool sortThisFrame_;
  179. /// Frame number on which was last sorted.
  180. unsigned sortFrameNumber_;
  181. /// Previous offset to camera for determining whether sorting is necessary.
  182. Vector3 previousOffset_;
  183. /// Billboard pointers for sorting.
  184. Vector<Billboard*> sortedBillboards_;
  185. /// Attribute buffer for network replication.
  186. mutable VectorBuffer attrBuffer_;
  187. };
  188. }