BillboardSet.h 8.5 KB

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