BillboardSet.h 8.2 KB

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