CCPURibbonTrail.h 7.3 KB

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