RibbonTrail.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. /// \file
  23. #pragma once
  24. #include "../Graphics/Drawable.h"
  25. namespace Urho3D
  26. {
  27. enum TrailType
  28. {
  29. TT_FACE_CAMERA = 0,
  30. TT_BONE
  31. };
  32. class IndexBuffer;
  33. class VertexBuffer;
  34. /// Trail is consisting of series of tails. Two connected points make a tail.
  35. struct URHO3D_API TrailPoint
  36. {
  37. /// Construct a zero-initialized TrailPoint.
  38. TrailPoint() = default;
  39. /// Construct a TrailPoint with the given position and forward vector.
  40. TrailPoint(const Vector3& position, const Vector3& forward);
  41. /// Position.
  42. Vector3 position_;
  43. /// Forward vector.
  44. Vector3 forward_;
  45. /// Parent position. Trail bone type uses this.
  46. Vector3 parentPos_;
  47. /// Elapsed length inside the trail.
  48. float elapsedLength_{};
  49. /// Next point to make a tail.
  50. TrailPoint* next_{};
  51. /// Tail time to live.
  52. float lifetime_{};
  53. /// Distance for sorting.
  54. float sortDistance_{};
  55. };
  56. /// Drawable component that creates a tail.
  57. class URHO3D_API RibbonTrail : public Drawable
  58. {
  59. URHO3D_OBJECT(RibbonTrail, Drawable);
  60. public:
  61. /// Construct.
  62. explicit RibbonTrail(Context* context);
  63. /// Destruct.
  64. ~RibbonTrail() override;
  65. /// Register object factory.
  66. static void RegisterObject(Context* context);
  67. /// Process octree raycast. May be called from a worker thread.
  68. void ProcessRayQuery(const RayOctreeQuery& query, PODVector<RayQueryResult>& results) override;
  69. /// Handle enabled/disabled state change.
  70. void OnSetEnabled() override;
  71. /// Update before octree reinsertion. Is called from a main thread.
  72. void Update(const FrameInfo &frame) override;
  73. /// Calculate distance and prepare batches for rendering. May be called from worker thread(s), possibly re-entrantly.
  74. void UpdateBatches(const FrameInfo& frame) override;
  75. /// Prepare geometry for rendering. Called from a worker thread if possible (no GPU update).
  76. void UpdateGeometry(const FrameInfo& frame) override;
  77. /// Return whether a geometry update is necessary, and if it can happen in a worker thread.
  78. UpdateGeometryType GetUpdateGeometryType() override;
  79. /// Set material.
  80. /// @property
  81. void SetMaterial(Material* material);
  82. /// Set material attribute.
  83. void SetMaterialAttr(const ResourceRef& value);
  84. /// Set distance between points.
  85. /// @property
  86. void SetVertexDistance(float length);
  87. /// Set width of the tail. Only works for face camera trail type.
  88. /// @property
  89. void SetWidth(float width);
  90. /// Set vertex blended color for start of trail.
  91. /// @property
  92. void SetStartColor(const Color& color);
  93. /// Set vertex blended scale for end of trail.
  94. /// @property
  95. void SetEndColor(const Color& color);
  96. /// Set vertex blended color for start of trail.
  97. /// @property
  98. void SetStartScale(float startScale);
  99. /// Set vertex blended scale for end of trail.
  100. /// @property
  101. void SetEndScale(float endScale);
  102. /// Set how the trail behave.
  103. /// @property
  104. void SetTrailType(TrailType type);
  105. /// Set base velocity applied to the trail.
  106. /// @property
  107. void SetBaseVelocity(const Vector3& baseVelocity);
  108. /// Set whether tails are sorted by distance. Default false.
  109. /// @property
  110. void SetSorted(bool enable);
  111. /// Set tail time to live.
  112. /// @property
  113. void SetLifetime(float time);
  114. /// Set whether trail should be emitting.
  115. /// @property
  116. void SetEmitting(bool emitting);
  117. /// Set whether to update when trail emitter are not visible.
  118. /// @property
  119. void SetUpdateInvisible(bool enable);
  120. /// Set number of column for every tails. Can be useful for fixing distortion at high angle.
  121. /// @property
  122. void SetTailColumn(unsigned tailColumn);
  123. /// Set animation LOD bias.
  124. /// @property
  125. void SetAnimationLodBias(float bias);
  126. /// Mark for bounding box and vertex buffer update. Call after modifying the trails.
  127. void Commit();
  128. /// Return material.
  129. /// @property
  130. Material* GetMaterial() const;
  131. /// Return material attribute.
  132. ResourceRef GetMaterialAttr() const;
  133. /// Get distance between points.
  134. /// @property
  135. float GetVertexDistance() const { return vertexDistance_; }
  136. /// Get width of the trail.
  137. /// @property
  138. float GetWidth() const { return width_; }
  139. /// Get vertex blended color for start of trail.
  140. /// @property
  141. const Color& GetStartColor() const { return startColor_; }
  142. /// Get vertex blended color for end of trail.
  143. /// @property
  144. const Color& GetEndColor() const { return endColor_; }
  145. /// Get vertex blended scale for start of trail.
  146. /// @property
  147. float GetStartScale() const { return startScale_; }
  148. /// Get vertex blended scale for end of trail.
  149. /// @property
  150. float GetEndScale() const { return endScale_; }
  151. /// Return whether tails are sorted.
  152. /// @property
  153. bool IsSorted() const { return sorted_; }
  154. /// Return tail time to live.
  155. /// @property
  156. float GetLifetime() const {return lifetime_;}
  157. /// Return animation LOD bias.
  158. /// @property
  159. float GetAnimationLodBias() const { return animationLodBias_; }
  160. /// Return how the trail behave.
  161. /// @property
  162. TrailType GetTrailType() const { return trailType_; }
  163. /// Return base trail velocity.
  164. /// @property
  165. const Vector3& GetBaseVelocity() const { return baseVelocity_; }
  166. /// Return number of column for tails.
  167. /// @property
  168. unsigned GetTailColumn() const { return tailColumn_; }
  169. /// Return whether is currently emitting.
  170. /// @property
  171. bool IsEmitting() const { return emitting_ ; }
  172. /// Return whether to update when trail emitter are not visible.
  173. /// @property
  174. bool GetUpdateInvisible() const { return updateInvisible_; }
  175. protected:
  176. /// Handle node being assigned.
  177. void OnSceneSet(Scene* scene) override;
  178. /// Recalculate the world-space bounding box.
  179. void OnWorldBoundingBoxUpdate() override;
  180. /// Mark vertex buffer to need an update.
  181. void MarkPositionsDirty();
  182. /// Tails.
  183. PODVector<TrailPoint> points_;
  184. /// Tails sorted flag.
  185. bool sorted_;
  186. /// Animation LOD bias.
  187. float animationLodBias_;
  188. /// Animation LOD timer.
  189. float animationLodTimer_;
  190. /// Trail type.
  191. TrailType trailType_;
  192. /// Base velocity applied to the trail.
  193. Vector3 baseVelocity_;
  194. private:
  195. /// Handle scene post-update event.
  196. void HandleScenePostUpdate(StringHash eventType, VariantMap& eventData);
  197. /// Resize RibbonTrail vertex and index buffers.
  198. void UpdateBufferSize();
  199. /// Rewrite RibbonTrail vertex buffer.
  200. void UpdateVertexBuffer(const FrameInfo& frame);
  201. /// Update/Rebuild tail mesh only if position changed (called by UpdateBatches()).
  202. void UpdateTail(float timeStep);
  203. /// Geometry.
  204. SharedPtr<Geometry> geometry_;
  205. /// Vertex buffer.
  206. SharedPtr<VertexBuffer> vertexBuffer_;
  207. /// Index buffer.
  208. SharedPtr<IndexBuffer> indexBuffer_;
  209. /// Transform matrices for position and orientation.
  210. Matrix3x4 transforms_;
  211. /// Buffers need resize flag.
  212. bool bufferSizeDirty_;
  213. /// Vertex buffer needs rewrite flag.
  214. bool bufferDirty_;
  215. /// Previous position of tail.
  216. Vector3 previousPosition_;
  217. /// Distance between points. Basically is tail length.
  218. float vertexDistance_;
  219. /// Width of trail.
  220. float width_;
  221. /// Number of points.
  222. unsigned numPoints_;
  223. /// Color for start of trails.
  224. Color startColor_;
  225. /// Color for end of trails.
  226. Color endColor_;
  227. /// Scale for start of trails.
  228. float startScale_;
  229. /// End for start of trails.
  230. float endScale_;
  231. /// Last scene timestep.
  232. float lastTimeStep_;
  233. /// Lifetime.
  234. float lifetime_;
  235. /// Number of columns for every tails.
  236. unsigned tailColumn_;
  237. /// Rendering framenumber on which was last updated.
  238. unsigned lastUpdateFrameNumber_;
  239. /// Need update flag.
  240. bool needUpdate_;
  241. /// Previous offset to camera for determining whether sorting is necessary.
  242. Vector3 previousOffset_;
  243. /// Trail pointers for sorting.
  244. Vector<TrailPoint*> sortedPoints_;
  245. /// Force update flag (ignore animation LOD momentarily).
  246. bool forceUpdate_;
  247. /// Currently emitting flag.
  248. bool emitting_;
  249. /// Update when invisible flag.
  250. bool updateInvisible_;
  251. /// End of trail point for smoother tail disappearance.
  252. TrailPoint endTail_;
  253. /// The time the tail become end of trail.
  254. float startEndTailTime_;
  255. };
  256. }