RibbonTrail.h 8.4 KB

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