CCPURibbonTrail.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /****************************************************************************
  2. Copyright (C) 2013 Henry van Merode. All rights reserved.
  3. Copyright (c) 2015-2017 Chukong Technologies Inc.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #ifndef __CC_PU_PARTICLE_3D_RIBBON_TRAIL_H__
  22. #define __CC_PU_PARTICLE_3D_RIBBON_TRAIL_H__
  23. #include "base/CCRef.h"
  24. #include "math/CCMath.h"
  25. #include "extensions/Particle3D/PU/CCPUBillboardChain.h"
  26. #include <vector>
  27. NS_CC_BEGIN
  28. class PURibbonTrail : public PUBillboardChain
  29. {
  30. public:
  31. /** Constructor
  32. @param name The name to give this object
  33. @param maxElements The maximum number of elements per chain
  34. @param numberOfChains The number of separate chain segments contained in this object,
  35. ie the maximum number of nodes that can have trails attached
  36. @param useTextureCoords If true, use texture coordinates from the chain elements
  37. @param useVertexColors If true, use vertex colors from the chain elements (must
  38. be true if you intend to use fading)
  39. */
  40. PURibbonTrail(const std::string& name, const std::string &texFile = "", size_t maxElements = 20, size_t numberOfChains = 1,
  41. bool useTextureCoords = true, bool useColours = true);
  42. /// destructor
  43. virtual ~PURibbonTrail();
  44. typedef std::vector<Node*> NodeList;
  45. /** Add a node to be tracked.
  46. @param n The node that will be tracked.
  47. */
  48. virtual void addNode(Node* n);
  49. /** Remove tracking on a given node. */
  50. virtual void removeNode(Node* n);
  51. /** Get the chain index for a given Node being tracked. */
  52. virtual size_t getChainIndexForNode(const Node* n);
  53. void setAttachedNode(Node *parent) { _parentNode = parent; }
  54. /** Set the length of the trail.
  55. @remarks
  56. This sets the length of the trail, in world units. It also sets how
  57. far apart each segment will be, ie length / max_elements.
  58. @param len The length of the trail in world units
  59. */
  60. virtual void setTrailLength(float len);
  61. /** Get the length of the trail. */
  62. virtual float getTrailLength(void) const { return _trailLength; }
  63. /** @copydoc BillboardChain::setMaxChainElements */
  64. void setMaxChainElements(size_t maxElements) override;
  65. /** @copydoc BillboardChain::setNumberOfChains */
  66. virtual void setNumberOfChains(size_t numChains) override;
  67. /** @copydoc BillboardChain::clearChain */
  68. void clearChain(size_t chainIndex) override;
  69. /** Set the starting ribbon colour for a given segment.
  70. @param chainIndex The index of the chain
  71. @param col The initial colour
  72. @note
  73. Only used if this instance is using vertex colours.
  74. */
  75. virtual void setInitialColour(size_t chainIndex, const Vec4& col);
  76. /** Set the starting ribbon colour.
  77. @param chainIndex The index of the chain
  78. @param r,b,g,a The initial colour
  79. @note
  80. Only used if this instance is using vertex colours.
  81. */
  82. virtual void setInitialColour(size_t chainIndex, float r, float g, float b, float a = 1.0);
  83. /** Get the starting ribbon colour. */
  84. virtual const Vec4& getInitialColour(size_t chainIndex) const;
  85. /** Enables / disables fading the trail using colour.
  86. @param chainIndex The index of the chain
  87. @param valuePerSecond The amount to subtract from colour each second
  88. */
  89. virtual void setColourChange(size_t chainIndex, const Vec4& valuePerSecond);
  90. /** Set the starting ribbon width in world units.
  91. @param chainIndex The index of the chain
  92. @param width The initial width of the ribbon
  93. */
  94. virtual void setInitialWidth(size_t chainIndex, float width);
  95. /** Get the starting ribbon width in world units. */
  96. virtual float getInitialWidth(size_t chainIndex) const;
  97. /** Set the change in ribbon width per second.
  98. @param chainIndex The index of the chain
  99. @param widthDeltaPerSecond The amount the width will reduce by per second
  100. */
  101. virtual void setWidthChange(size_t chainIndex, float widthDeltaPerSecond);
  102. /** Get the change in ribbon width per second. */
  103. virtual float getWidthChange(size_t chainIndex) const;
  104. /** Enables / disables fading the trail using colour.
  105. @param chainIndex The index of the chain
  106. @param r,g,b,a The amount to subtract from each colour channel per second
  107. */
  108. virtual void setColourChange(size_t chainIndex, float r, float g, float b, float a);
  109. /** Get the per-second fading amount */
  110. virtual const Vec4& getColourChange(size_t chainIndex) const;
  111. void update(float deltaTime);
  112. /// @see Node::Listener::nodeUpdated
  113. void nodeUpdated(const Node* node);
  114. /// @see Node::Listener::nodeDestroyed
  115. void nodeDestroyed(const Node* node);
  116. /// Perform any fading / width delta required; internal method
  117. void timeUpdate(float time);
  118. protected:
  119. /// Manage updates to the time controller
  120. void manageController(void);
  121. /// Node has changed position, update
  122. void updateTrail(size_t index, const Node* node);
  123. /// Reset the tracked chain to initial state
  124. void resetTrail(size_t index, const Node* node);
  125. /// Reset all tracked chains to initial state
  126. void resetAllTrails(void);
  127. protected:
  128. /// List of nodes being trailed
  129. NodeList _nodeList;
  130. /// Mapping of nodes to chain segments
  131. typedef std::vector<size_t> IndexVector;
  132. /// Ordered like mNodeList, contains chain index
  133. IndexVector _nodeToChainSegment;
  134. // chains not in use
  135. IndexVector _freeChains;
  136. // fast lookup node->chain index
  137. // we use positional map too because that can be useful
  138. typedef std::map<const Node*, size_t> NodeToChainSegmentMap;
  139. NodeToChainSegmentMap _nodeToSegMap;
  140. /// Total length of trail in world units
  141. float _trailLength;
  142. /// length of each element
  143. float _elemLength;
  144. /// Squared length of each element
  145. float _squaredElemLength;
  146. typedef std::vector<Vec4> ColorValueList;
  147. typedef std::vector<float> RealList;
  148. /// Initial colour of the ribbon
  149. ColorValueList _initialColor;
  150. /// fade amount per second
  151. ColorValueList _deltaColor;
  152. /// Initial width of the ribbon
  153. RealList _initialWidth;
  154. /// Delta width of the ribbon
  155. RealList _deltaWidth;
  156. Node *_parentNode;
  157. bool _needTimeUpdate;
  158. };
  159. NS_CC_END
  160. #endif