BillboardSet.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #pragma once
  4. #include "../Graphics/Drawable.h"
  5. #include "../IO/VectorBuffer.h"
  6. #include "../Math/Color.h"
  7. #include "../Math/Matrix3x4.h"
  8. #include "../Math/Rect.h"
  9. namespace Urho3D
  10. {
  11. class IndexBuffer;
  12. class VertexBuffer;
  13. /// One billboard in the billboard set.
  14. /// @nocount
  15. struct URHO3D_API Billboard
  16. {
  17. /// Position.
  18. Vector3 position_;
  19. /// Two-dimensional size. If BillboardSet has fixed screen size enabled, this is measured in pixels instead of world units.
  20. Vector2 size_;
  21. /// UV coordinates.
  22. Rect uv_;
  23. /// Color.
  24. Color color_;
  25. /// Rotation.
  26. float rotation_;
  27. /// Direction (For direction based billboard only).
  28. Vector3 direction_;
  29. /// Enabled flag.
  30. bool enabled_;
  31. /// Sort distance. Used internally.
  32. float sortDistance_;
  33. /// Scale factor for fixed screen size mode. Used internally.
  34. float screenScaleFactor_;
  35. };
  36. /// %Billboard component.
  37. class URHO3D_API BillboardSet : public Drawable
  38. {
  39. URHO3D_OBJECT(BillboardSet, Drawable);
  40. public:
  41. /// Construct.
  42. explicit BillboardSet(Context* context);
  43. /// Destruct.
  44. ~BillboardSet() override;
  45. /// Register object factory.
  46. /// @nobind
  47. static void RegisterObject(Context* context);
  48. /// Process octree raycast. May be called from a worker thread.
  49. void ProcessRayQuery(const RayOctreeQuery& query, Vector<RayQueryResult>& results) override;
  50. /// Calculate distance and prepare batches for rendering. May be called from worker thread(s), possibly re-entrantly.
  51. void UpdateBatches(const FrameInfo& frame) override;
  52. /// Prepare geometry for rendering. Called from a worker thread if possible (no GPU update).
  53. void UpdateGeometry(const FrameInfo& frame) override;
  54. /// Return whether a geometry update is necessary, and if it can happen in a worker thread.
  55. UpdateGeometryType GetUpdateGeometryType() override;
  56. /// Set material.
  57. /// @property
  58. void SetMaterial(Material* material);
  59. /// Set number of billboards.
  60. /// @property
  61. void SetNumBillboards(i32 num);
  62. /// Set whether billboards are relative to the scene node. Default true.
  63. /// @property
  64. void SetRelative(bool enable);
  65. /// Set whether scene node scale affects billboards' size. Default true.
  66. /// @property
  67. void SetScaled(bool enable);
  68. /// Set whether billboards are sorted by distance. Default false.
  69. /// @property
  70. void SetSorted(bool enable);
  71. /// Set whether billboards have fixed size on screen (measured in pixels) regardless of distance to camera. Default false.
  72. /// @property
  73. void SetFixedScreenSize(bool enable);
  74. /// Set how the billboards should rotate in relation to the camera. Default is to follow camera rotation on all axes (FC_ROTATE_XYZ).
  75. /// @property
  76. void SetFaceCameraMode(FaceCameraMode mode);
  77. /// Set minimal angle between billboard normal and look-at direction.
  78. /// @property
  79. void SetMinAngle(float angle);
  80. /// Set animation LOD bias.
  81. /// @property
  82. void SetAnimationLodBias(float bias);
  83. /// Mark for bounding box and vertex buffer update. Call after modifying the billboards.
  84. void Commit();
  85. /// Return material.
  86. /// @property
  87. Material* GetMaterial() const;
  88. /// Return number of billboards.
  89. /// @property
  90. i32 GetNumBillboards() const { return billboards_.Size(); }
  91. /// Return all billboards.
  92. Vector<Billboard>& GetBillboards() { return billboards_; }
  93. /// Return billboard by index.
  94. /// @property{get_billboards}
  95. Billboard* GetBillboard(i32 index);
  96. /// Return whether billboards are relative to the scene node.
  97. /// @property
  98. bool IsRelative() const { return relative_; }
  99. /// Return whether scene node scale affects billboards' size.
  100. /// @property
  101. bool IsScaled() const { return scaled_; }
  102. /// Return whether billboards are sorted.
  103. /// @property
  104. bool IsSorted() const { return sorted_; }
  105. /// Return whether billboards are fixed screen size.
  106. /// @property
  107. bool IsFixedScreenSize() const { return fixedScreenSize_; }
  108. /// Return how the billboards rotate in relation to the camera.
  109. /// @property
  110. FaceCameraMode GetFaceCameraMode() const { return faceCameraMode_; }
  111. /// Return minimal angle between billboard normal and look-at direction.
  112. /// @property
  113. float GetMinAngle() const { return minAngle_; }
  114. /// Return animation LOD bias.
  115. /// @property
  116. float GetAnimationLodBias() const { return animationLodBias_; }
  117. /// Set material attribute.
  118. void SetMaterialAttr(const ResourceRef& value);
  119. /// Set billboards attribute.
  120. void SetBillboardsAttr(const VariantVector& value);
  121. /// Set billboards attribute for network replication.
  122. void SetNetBillboardsAttr(const Vector<byte>& value);
  123. /// Return material attribute.
  124. ResourceRef GetMaterialAttr() const;
  125. /// Return billboards attribute.
  126. VariantVector GetBillboardsAttr() const;
  127. /// Return billboards attribute for network replication.
  128. const Vector<byte>& GetNetBillboardsAttr() const;
  129. protected:
  130. /// Recalculate the world-space bounding box.
  131. void OnWorldBoundingBoxUpdate() override;
  132. /// Mark billboard vertex buffer to need an update.
  133. void MarkPositionsDirty();
  134. /// Billboards.
  135. Vector<Billboard> billboards_;
  136. /// Animation LOD bias.
  137. float animationLodBias_;
  138. /// Animation LOD timer.
  139. float animationLodTimer_;
  140. /// Billboards relative flag.
  141. bool relative_;
  142. /// Scale affects billboard scale flag.
  143. bool scaled_;
  144. /// Billboards sorted flag.
  145. bool sorted_;
  146. /// Billboards fixed screen size flag.
  147. bool fixedScreenSize_;
  148. /// Billboard rotation mode in relation to the camera.
  149. FaceCameraMode faceCameraMode_;
  150. /// Minimal angle between billboard normal and look-at direction.
  151. float minAngle_;
  152. private:
  153. /// Resize billboard vertex and index buffers.
  154. void UpdateBufferSize();
  155. /// Rewrite billboard vertex buffer.
  156. void UpdateVertexBuffer(const FrameInfo& frame);
  157. /// Calculate billboard scale factors in fixed screen size mode.
  158. void CalculateFixedScreenSize(const FrameInfo& frame);
  159. /// Geometry.
  160. SharedPtr<Geometry> geometry_;
  161. /// Vertex buffer.
  162. SharedPtr<VertexBuffer> vertexBuffer_;
  163. /// Index buffer.
  164. SharedPtr<IndexBuffer> indexBuffer_;
  165. /// Transform matrices for position and billboard orientation.
  166. Matrix3x4 transforms_[2];
  167. /// Buffers need resize flag.
  168. bool bufferSizeDirty_;
  169. /// Vertex buffer needs rewrite flag.
  170. bool bufferDirty_;
  171. /// Force update flag (ignore animation LOD momentarily).
  172. bool forceUpdate_;
  173. /// Update billboard geometry type.
  174. bool geometryTypeUpdate_;
  175. /// Sorting flag. Triggers a vertex buffer rewrite for each view this billboard set is rendered from.
  176. bool sortThisFrame_;
  177. /// Whether was last rendered from an ortho camera.
  178. bool hasOrthoCamera_;
  179. /// Frame number on which was last sorted.
  180. i32 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. }