BillboardSet.h 7.0 KB

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