BillboardSet.h 9.4 KB

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