CCPUAffector.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. #include "extensions/Particle3D/PU/CCPUAffector.h"
  23. #include "extensions/Particle3D/PU/CCPUEmitter.h"
  24. #include "extensions/Particle3D/PU/CCPUParticleSystem3D.h"
  25. NS_CC_BEGIN
  26. PUAffector::PUAffector()
  27. : _affectorScale(Vec3::ONE)
  28. , _affectSpecialisation(AFSP_DEFAULT)
  29. , _mass(1.0f)
  30. {
  31. }
  32. PUAffector::~PUAffector()
  33. {
  34. _particleSystem = nullptr;
  35. }
  36. void PUAffector::updatePUAffector(PUParticle3D* /*particle*/, float /*delta*/)
  37. {
  38. }
  39. const Vec3& PUAffector::getDerivedPosition()
  40. {
  41. PUParticleSystem3D *ps = static_cast<PUParticleSystem3D *>(_particleSystem);
  42. if (ps){
  43. Mat4 rotMat;
  44. Mat4::createRotation(ps->getDerivedOrientation(), &rotMat);
  45. _derivedPosition = ps->getDerivedPosition() + rotMat * Vec3(_position.x * _affectorScale.x, _position.y * _affectorScale.y, _position.z * _affectorScale.z);
  46. //_particleSystem->getNodeToWorldTransform().transformPoint(_position, &_derivedPosition);
  47. }
  48. else
  49. _derivedPosition = _position;
  50. return _derivedPosition;
  51. //if (mMarkedForEmission)
  52. //{
  53. // // Use the affector position, because it is emitted
  54. // // If a particle is emitted, position and derived position are the same
  55. // _derivedPosition = position;
  56. //}
  57. //else
  58. //{
  59. // // Add the techniques' derived position
  60. // _derivedPosition = mParentTechnique->getDerivedPosition() +
  61. // mParentTechnique->getParentSystem()->getDerivedOrientation() * (_mAffectorScale * position);
  62. //}
  63. //return _derivedPosition;
  64. }
  65. float PUAffector::calculateAffectSpecialisationFactor( const PUParticle3D* particle )
  66. {
  67. // Assume that particle->totalTimeToLive != 0, which is reasonable
  68. switch (_affectSpecialisation)
  69. {
  70. case AFSP_DEFAULT:
  71. return 1.0f;
  72. break;
  73. // This means that older particles will be affected MORE than just emitted particles
  74. case AFSP_TTL_INCREASE:
  75. {
  76. if (particle)
  77. {
  78. return particle->timeFraction;
  79. }
  80. else
  81. {
  82. return 1.0f;
  83. }
  84. }
  85. break;
  86. // This means that older particles will be affected LESS than just emitted particles
  87. case AFSP_TTL_DECREASE:
  88. {
  89. if (particle)
  90. {
  91. return 1.0f - particle->timeFraction;
  92. }
  93. else
  94. {
  95. return 1.0f;
  96. }
  97. }
  98. break;
  99. default:
  100. return 1.0f;
  101. break;
  102. }
  103. }
  104. void PUAffector::notifyStart()
  105. {
  106. }
  107. void PUAffector::notifyStop()
  108. {
  109. }
  110. void PUAffector::notifyPause()
  111. {
  112. }
  113. void PUAffector::notifyResume()
  114. {
  115. }
  116. void PUAffector::preUpdateAffector( float /*deltaTime*/ )
  117. {
  118. }
  119. void PUAffector::postUpdateAffector( float /*deltaTime*/ )
  120. {
  121. }
  122. void PUAffector::prepare()
  123. {
  124. }
  125. void PUAffector::unPrepare()
  126. {
  127. }
  128. void PUAffector::initParticleForEmission( PUParticle3D* /*particle*/ )
  129. {
  130. }
  131. void PUAffector::notifyRescaled(const Vec3& scale)
  132. {
  133. _affectorScale = scale;
  134. }
  135. void PUAffector::firstParticleUpdate( PUParticle3D* /*particle*/, float /*deltaTime*/ )
  136. {
  137. }
  138. void PUAffector::setMass( float mass )
  139. {
  140. _mass = mass;
  141. }
  142. float PUAffector::getMass() const
  143. {
  144. return _mass;
  145. }
  146. void PUAffector::copyAttributesTo( PUAffector* affector )
  147. {
  148. affector->setName(_name);
  149. affector->setAffectorType(_affectorType);
  150. affector->_position = _position;
  151. affector->_isEnabled = _isEnabled;
  152. affector->_particleSystem = _particleSystem;
  153. affector->_affectorScale = _affectorScale;
  154. affector->_affectSpecialisation = _affectSpecialisation;
  155. affector->_excludedEmitters = _excludedEmitters;
  156. }
  157. void PUAffector::addEmitterToExclude( const std::string& emitterName )
  158. {
  159. auto iter = std::find(_excludedEmitters.begin(), _excludedEmitters.end(), emitterName);
  160. if (iter == _excludedEmitters.end()){
  161. _excludedEmitters.push_back(emitterName);
  162. }
  163. }
  164. void PUAffector::removeEmitterToExclude( const std::string& emitterName )
  165. {
  166. auto iter = std::find(_excludedEmitters.begin(), _excludedEmitters.end(), emitterName);
  167. if (iter != _excludedEmitters.end()){
  168. _excludedEmitters.erase(iter);
  169. }
  170. }
  171. void PUAffector::process( PUParticle3D* particle, float delta, bool firstParticle )
  172. {
  173. if (firstParticle){
  174. firstParticleUpdate(particle, delta);
  175. }
  176. if (!_excludedEmitters.empty() && particle->parentEmitter){
  177. // Return if the emitter which emits this particle is part of the vector
  178. std::string emitterName = particle->parentEmitter->getName();
  179. auto iter = std::find(_excludedEmitters.begin(), _excludedEmitters.end(), emitterName);
  180. if (iter != _excludedEmitters.end())
  181. {
  182. return;
  183. }
  184. }
  185. updatePUAffector(particle, delta);
  186. }
  187. NS_CC_END