CCPUParticleSystem3D.cpp 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436
  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/CCPUParticleSystem3D.h"
  23. #include "extensions/Particle3D/PU/CCPUEmitter.h"
  24. #include "extensions/Particle3D/PU/CCPUEmitterManager.h"
  25. #include "extensions/Particle3D/PU/CCPUAffector.h"
  26. #include "extensions/Particle3D/PU/CCPUAffectorManager.h"
  27. #include "extensions/Particle3D/CCParticle3DRender.h"
  28. #include "extensions/Particle3D/PU/CCPUScriptCompiler.h"
  29. #include "extensions/Particle3D/PU/CCPUMaterialManager.h"
  30. #include "extensions/Particle3D/PU/CCPUTranslateManager.h"
  31. #include "extensions/Particle3D/PU/CCPUListener.h"
  32. #include "extensions/Particle3D/PU/CCPUObserver.h"
  33. #include "extensions/Particle3D/PU/CCPUObserverManager.h"
  34. #include "extensions/Particle3D/PU/CCPUBehaviour.h"
  35. #include "platform/CCFileUtils.h"
  36. NS_CC_BEGIN
  37. float PUParticle3D::DEFAULT_TTL = 10.0f;
  38. float PUParticle3D::DEFAULT_MASS = 1.0f;
  39. float PUParticle3D::calculateVelocity() const
  40. {
  41. if (originalScaledDirectionLength != 0)
  42. {
  43. return originalVelocity * (direction.length() / originalScaledDirectionLength);
  44. }
  45. else
  46. {
  47. // Assume originalScaledDirectionLength to be 1.0 (unit vector)
  48. return originalVelocity * direction.length();
  49. }
  50. }
  51. void PUParticle3D::setOwnDimensions( float newWidth, float newHeight, float newDepth )
  52. {
  53. ownDimensions = true;
  54. if (newWidth)
  55. width = newWidth;
  56. if (newHeight)
  57. height = newHeight;
  58. if (newDepth)
  59. depth = newDepth;
  60. calculateBoundingSphereRadius();
  61. //parentEmitter->getParentTechnique()->_notifyParticleResized();
  62. }
  63. void PUParticle3D::calculateBoundingSphereRadius()
  64. {
  65. //radius = 0.5 * Math::Sqrt(width*width + height*height + depth*depth);
  66. radius = 0.5f * std::max(depth, std::max(width, height)); // approximation
  67. }
  68. void PUParticle3D::initForEmission()
  69. {
  70. eventFlags = 0;
  71. timeFraction = 0.0f;
  72. /* Note, that this flag must only be set as soon as the particle is emitted. As soon as the particle has
  73. been moved once, the flag must be removed again.
  74. */
  75. addEventFlags(PUParticle3D::PEF_EMITTED);
  76. // Reset freeze flag
  77. freezed = false;
  78. if (!behaviours.empty()){
  79. for (auto &it : behaviours) {
  80. it->initParticleForEmission(this);
  81. }
  82. }
  83. }
  84. void PUParticle3D::initForExpiration( float timeElapsed )
  85. {
  86. if (!behaviours.empty()){
  87. for (auto &it : behaviours) {
  88. it->initParticleForExpiration(this, timeElapsed);
  89. }
  90. }
  91. }
  92. void PUParticle3D::process( float timeElapsed )
  93. {
  94. timeFraction = (totalTimeToLive - timeToLive) / totalTimeToLive;
  95. if (!behaviours.empty()){
  96. for (auto &it : behaviours) {
  97. it->updateBehaviour(this, timeElapsed);
  98. }
  99. }
  100. }
  101. PUParticle3D::PUParticle3D():
  102. particleEntityPtr(nullptr),
  103. parentEmitter(nullptr),
  104. visualData(nullptr),
  105. particleType(PT_VISUAL),
  106. originalDirectionLength(0.0f),
  107. originalVelocity(0.0f),
  108. originalScaledDirectionLength(0.0f),
  109. rotationAxis(Vec3::UNIT_Z),
  110. //color(Vec4::ONE),
  111. originalColor(Vec4::ONE),
  112. //zRotation(0.0f),
  113. zRotationSpeed(0.0f),
  114. rotationSpeed(0.0f),
  115. radius(0.87f),
  116. ownDimensions(false),
  117. eventFlags(0),
  118. freezed(false),
  119. timeToLive(DEFAULT_TTL),
  120. totalTimeToLive(DEFAULT_TTL),
  121. timeFraction(0.0f),
  122. mass(DEFAULT_MASS),
  123. textureAnimationTimeStep(0.1f),
  124. textureAnimationTimeStepCount(0.0f),
  125. textureCoordsCurrent(0),
  126. textureAnimationDirectionUp(true),
  127. depthInView(0.0f),
  128. zRotation(0.0f)
  129. //widthInWorld(width),
  130. //heightInWorld(height),
  131. //depthInWorld(depth)
  132. {
  133. }
  134. PUParticle3D::~PUParticle3D()
  135. {
  136. for (auto it : behaviours) {
  137. it->release();
  138. }
  139. //CC_SAFE_RELEASE(particleEntityPtr);
  140. }
  141. void PUParticle3D::copyBehaviours( const ParticleBehaviourList &list )
  142. {
  143. for (auto it : list){
  144. auto behaviour = it->clone();
  145. behaviour->retain();
  146. behaviours.push_back(behaviour);
  147. }
  148. }
  149. //-----------------------------------------------------------------------
  150. const float PUParticleSystem3D::DEFAULT_WIDTH = 50;
  151. const float PUParticleSystem3D::DEFAULT_HEIGHT = 50;
  152. const float PUParticleSystem3D::DEFAULT_DEPTH = 50;
  153. const unsigned int PUParticleSystem3D::DEFAULT_PARTICLE_QUOTA = 500;
  154. const unsigned int PUParticleSystem3D::DEFAULT_EMITTED_EMITTER_QUOTA = 50;
  155. const unsigned int PUParticleSystem3D::DEFAULT_EMITTED_SYSTEM_QUOTA = 10;
  156. const float PUParticleSystem3D::DEFAULT_MAX_VELOCITY = 9999.0f;
  157. PUParticleSystem3D::PUParticleSystem3D()
  158. : _emittedEmitterQuota(DEFAULT_EMITTED_EMITTER_QUOTA)
  159. , _emittedSystemQuota(DEFAULT_EMITTED_SYSTEM_QUOTA)
  160. , _prepared(false)
  161. , _poolPrepared(false)
  162. , _particleSystemScaleVelocity(1.0f)
  163. , _timeElapsedSinceStart(0.0f)
  164. , _defaultWidth(DEFAULT_WIDTH)
  165. , _defaultHeight(DEFAULT_HEIGHT)
  166. , _defaultDepth(DEFAULT_DEPTH)
  167. , _maxVelocity(DEFAULT_MAX_VELOCITY)
  168. , _maxVelocitySet(false)
  169. , _isMarkedForEmission(false)
  170. , _parentParticleSystem(nullptr)
  171. {
  172. _particleQuota = DEFAULT_PARTICLE_QUOTA;
  173. }
  174. PUParticleSystem3D::~PUParticleSystem3D()
  175. {
  176. stopParticleSystem();
  177. unPrepared();
  178. _particlePool.removeAllDatas();
  179. for (auto iter : _emittedEmitterParticlePool){
  180. auto pool = iter.second;
  181. auto lockedList = pool.getUnActiveDataList();
  182. for (auto iter2 : lockedList){
  183. static_cast<PUParticle3D *>(iter2)->particleEntityPtr->release();
  184. }
  185. iter.second.removeAllDatas();
  186. }
  187. for (auto iter : _emittedSystemParticlePool){
  188. auto pool = iter.second;
  189. auto lockedList = pool.getUnActiveDataList();
  190. for (auto iter2 : lockedList){
  191. static_cast<PUParticle3D *>(iter2)->particleEntityPtr->release();
  192. }
  193. iter.second.removeAllDatas();
  194. }
  195. //release all emitters
  196. for (auto it : _emitters) {
  197. it->release();
  198. }
  199. _emitters.clear();
  200. for (auto it : _observers){
  201. it->release();
  202. }
  203. for (auto it : _behaviourTemplates) {
  204. it->release();
  205. }
  206. _observers.clear();
  207. }
  208. PUParticleSystem3D* PUParticleSystem3D::create()
  209. {
  210. auto pups = new (std::nothrow) PUParticleSystem3D();
  211. pups->autorelease();
  212. return pups;
  213. }
  214. PUParticleSystem3D* PUParticleSystem3D::create( const std::string &filePath, const std::string &materialPath )
  215. {
  216. PUParticleSystem3D *ret = new (std::nothrow) PUParticleSystem3D();
  217. if (ret && ret->initWithFilePathAndMaterialPath(filePath, materialPath))
  218. {
  219. ret->autorelease();
  220. return ret;
  221. }
  222. else
  223. {
  224. CC_SAFE_DELETE(ret);
  225. return nullptr;
  226. }
  227. }
  228. PUParticleSystem3D* PUParticleSystem3D::create( const std::string &filePath )
  229. {
  230. PUParticleSystem3D *ret = new (std::nothrow) PUParticleSystem3D();
  231. if (ret && ret->initWithFilePath(filePath))
  232. {
  233. ret->autorelease();
  234. return ret;
  235. }
  236. else
  237. {
  238. CC_SAFE_DELETE(ret);
  239. return nullptr;
  240. }
  241. }
  242. bool PUParticleSystem3D::initWithFilePath( const std::string &filePath )
  243. {
  244. std::string fullPath = FileUtils::getInstance()->fullPathForFilename(filePath);
  245. convertToUnixStylePath(fullPath);
  246. std::string::size_type pos = fullPath.find_last_of("/");
  247. std::string materialFolder = "materials";
  248. if (pos != std::string::npos){
  249. std::string temp = fullPath.substr(0, pos);
  250. pos = temp.find_last_of("/");
  251. if (pos != std::string::npos){
  252. materialFolder = temp.substr(0, pos + 1) + materialFolder;
  253. }
  254. }
  255. static std::vector<std::string> loadedFolder;
  256. if (std::find(loadedFolder.begin(), loadedFolder.end(), materialFolder) == loadedFolder.end())
  257. {
  258. PUMaterialCache::Instance()->loadMaterialsFromSearchPaths(materialFolder);
  259. loadedFolder.push_back(materialFolder);
  260. }
  261. if (!initSystem(fullPath)){
  262. return false;
  263. }
  264. return true;
  265. }
  266. bool PUParticleSystem3D::initWithFilePathAndMaterialPath( const std::string &filePath, const std::string &materialPath )
  267. {
  268. std::string matfullPath = FileUtils::getInstance()->fullPathForFilename(materialPath);
  269. convertToUnixStylePath(matfullPath);
  270. PUMaterialCache::Instance()->loadMaterials(matfullPath);
  271. std::string fullPath = FileUtils::getInstance()->fullPathForFilename(filePath);
  272. convertToUnixStylePath(fullPath);
  273. if (!initSystem(fullPath)){
  274. return false;
  275. }
  276. return true;
  277. }
  278. void PUParticleSystem3D::startParticleSystem()
  279. {
  280. stopParticleSystem();
  281. if (_state != State::RUNNING)
  282. {
  283. forceStopParticleSystem();
  284. if (_render)
  285. _render->notifyStart();
  286. for (auto &it : _observers){
  287. it->notifyStart();
  288. }
  289. for (auto& it : _emitters) {
  290. auto emitter = static_cast<PUEmitter*>(it);
  291. emitter->notifyStart();
  292. }
  293. for (auto& it : _affectors) {
  294. auto affector = static_cast<PUAffector*>(it);
  295. affector->notifyStart();
  296. }
  297. scheduleUpdate();
  298. _state = State::RUNNING;
  299. }
  300. for (auto iter : _children)
  301. {
  302. PUParticleSystem3D *system = dynamic_cast<PUParticleSystem3D *>(iter);
  303. if (system){
  304. system->_parentParticleSystem = this;
  305. system->startParticleSystem();
  306. }
  307. }
  308. }
  309. void PUParticleSystem3D::stopParticleSystem()
  310. {
  311. if (_state != State::STOP)
  312. {
  313. _state = State::STOP;
  314. }
  315. for (auto iter : _children)
  316. {
  317. PUParticleSystem3D *system = dynamic_cast<PUParticleSystem3D *>(iter);
  318. if (system)
  319. system->stopParticleSystem();
  320. }
  321. }
  322. void PUParticleSystem3D::pauseParticleSystem()
  323. {
  324. if (_state == State::RUNNING)
  325. {
  326. //if (_emitter)
  327. //{
  328. // auto emitter = static_cast<PUEmitter*>(_emitter);
  329. // emitter->notifyPause();
  330. //}
  331. for (auto& it : _emitters) {
  332. auto emitter = static_cast<PUEmitter*>(it);
  333. emitter->notifyPause();
  334. }
  335. for (auto& it : _affectors) {
  336. auto affector = static_cast<PUAffector*>(it);
  337. affector->notifyPause();
  338. }
  339. _state = State::PAUSE;
  340. }
  341. for (auto iter : _children)
  342. {
  343. PUParticleSystem3D *system = dynamic_cast<PUParticleSystem3D *>(iter);
  344. if (system)
  345. system->pauseParticleSystem();
  346. }
  347. }
  348. void PUParticleSystem3D::resumeParticleSystem()
  349. {
  350. if (_state == State::PAUSE)
  351. {
  352. //if (_emitter)
  353. //{
  354. // auto emitter = static_cast<PUEmitter*>(_emitter);
  355. // emitter->notifyResume();
  356. //}
  357. for (auto& it : _emitters) {
  358. auto emitter = static_cast<PUEmitter*>(it);
  359. emitter->notifyResume();
  360. }
  361. for (auto& it : _affectors) {
  362. auto affector = static_cast<PUAffector*>(it);
  363. affector->notifyResume();
  364. }
  365. _state = State::RUNNING;
  366. }
  367. for (auto iter : _children)
  368. {
  369. PUParticleSystem3D *system = dynamic_cast<PUParticleSystem3D *>(iter);
  370. if (system)
  371. system->resumeParticleSystem();
  372. }
  373. }
  374. void PUParticleSystem3D::update(float delta)
  375. {
  376. if (!_isEnabled || _isMarkedForEmission) return;
  377. if (_state != State::RUNNING){
  378. if (_state == State::PAUSE)
  379. return;
  380. else if (_state == State::STOP && getAliveParticleCount() <= 0){
  381. forceStopParticleSystem();
  382. return;
  383. }
  384. }
  385. forceUpdate(delta);
  386. }
  387. void PUParticleSystem3D::forceUpdate( float delta )
  388. {
  389. if (!_emitters.empty())
  390. calulateRotationOffset();
  391. prepared();
  392. Vec3 currentPos = getDerivedPosition();
  393. _latestPositionDiff = currentPos - _latestPosition;
  394. _latestPosition = currentPos;
  395. _latestOrientation = getDerivedOrientation();
  396. if (!_emitters.empty()){
  397. emitParticles(delta);
  398. preUpdator(delta);
  399. updator(delta);
  400. postUpdator(delta);
  401. }
  402. _timeElapsedSinceStart += delta;
  403. }
  404. float PUParticleSystem3D::getParticleSystemScaleVelocity() const
  405. {
  406. return _particleSystemScaleVelocity;
  407. }
  408. void PUParticleSystem3D::rotationOffset( Vec3& pos )
  409. {
  410. Mat4 rotMat;
  411. Mat4::createRotation(_rotationOffset, &rotMat);
  412. pos = _rotationCentre + rotMat * (pos - _rotationCentre);
  413. }
  414. void PUParticleSystem3D::prepared()
  415. {
  416. if (!_prepared){
  417. //if (_emitter && _emitter->isEnabled())
  418. //{
  419. // auto emitter = static_cast<PUEmitter*>(_emitter);
  420. // emitter->prepare();
  421. //}
  422. if (_render)
  423. static_cast<PURender *>(_render)->prepare();
  424. for (auto it : _behaviourTemplates) {
  425. it->prepare();
  426. }
  427. for (auto it : _emitters) {
  428. //if (it->isEnabled())
  429. (static_cast<PUEmitter*>(it))->prepare();
  430. }
  431. for (auto it : _affectors) {
  432. //if (it->isEnabled())
  433. (static_cast<PUAffector*>(it))->prepare();
  434. }
  435. if (!_poolPrepared){
  436. for (auto it : _emitters) {
  437. //if (it->isEnabled())
  438. PUEmitter *emitter = static_cast<PUEmitter*>(it);
  439. if (emitter->getEmitsType() == PUParticle3D::PT_EMITTER){
  440. PUEmitter *emitted = static_cast<PUEmitter*>(emitter->getEmitsEntityPtr());
  441. for (unsigned int i = 0; i < _emittedEmitterQuota; ++i){
  442. auto p = new (std::nothrow) PUParticle3D();
  443. p->particleType = PUParticle3D::PT_EMITTER;
  444. p->particleEntityPtr = emitted->clone();
  445. p->particleEntityPtr->retain();
  446. p->copyBehaviours(_behaviourTemplates);
  447. _emittedEmitterParticlePool[emitted->getName()].addData(p);
  448. }
  449. }
  450. else if (emitter->getEmitsType() == PUParticle3D::PT_TECHNIQUE){
  451. PUParticleSystem3D *emitted = static_cast<PUParticleSystem3D*>(emitter->getEmitsEntityPtr());
  452. for (unsigned int i = 0; i < _emittedSystemQuota; ++i){
  453. PUParticleSystem3D *clonePS = emitted->clone();
  454. auto p = new (std::nothrow) PUParticle3D();
  455. p->particleType = PUParticle3D::PT_TECHNIQUE;
  456. p->particleEntityPtr = clonePS;
  457. p->particleEntityPtr->retain();
  458. p->copyBehaviours(_behaviourTemplates);
  459. _emittedSystemParticlePool[emitted->getName()].addData(p);
  460. clonePS->prepared();
  461. }
  462. //emitted->stopParticle();
  463. }
  464. }
  465. for (unsigned int i = 0; i < _particleQuota; ++i){
  466. auto p = new (std::nothrow) PUParticle3D();
  467. p->copyBehaviours(_behaviourTemplates);
  468. _particlePool.addData(p);
  469. }
  470. _poolPrepared = true;
  471. }
  472. _prepared = true;
  473. _timeElapsedSinceStart = 0.0f;
  474. _latestPosition = getDerivedPosition(); // V1.3.1
  475. if (_parentParticleSystem){
  476. _particleSystemScaleVelocity = _parentParticleSystem->getParticleSystemScaleVelocity();
  477. }
  478. }
  479. if (!_emitters.empty())
  480. notifyRescaled(getDerivedScale());
  481. }
  482. void PUParticleSystem3D::unPrepared()
  483. {
  484. //if (_emitter && _emitter->isEnabled())
  485. //{
  486. // auto emitter = static_cast<PUEmitter*>(_emitter);
  487. // emitter->unPrepare();
  488. //}
  489. if (_prepared){
  490. if (_render)
  491. static_cast<PURender *>(_render)->unPrepare();
  492. for (auto it : _behaviourTemplates) {
  493. it->unPrepare();
  494. }
  495. for (auto it : _emitters) {
  496. if (it->isEnabled())
  497. (static_cast<PUEmitter*>(it))->unPrepare();
  498. }
  499. for (auto it : _affectors) {
  500. if (it->isEnabled())
  501. (static_cast<PUAffector*>(it))->unPrepare();
  502. }
  503. _particlePool.lockAllDatas();
  504. for (auto &iter : _emittedEmitterParticlePool){
  505. PUParticle3D *particle = static_cast<PUParticle3D *>(iter.second.getFirst());
  506. while (particle)
  507. {
  508. static_cast<PUEmitter*>(particle->particleEntityPtr)->unPrepare();
  509. particle = static_cast<PUParticle3D *>(iter.second.getNext());
  510. }
  511. iter.second.lockAllDatas();
  512. }
  513. for (auto &iter : _emittedSystemParticlePool){
  514. PUParticle3D *particle = static_cast<PUParticle3D *>(iter.second.getFirst());
  515. while (particle)
  516. {
  517. static_cast<PUParticleSystem3D*>(particle->particleEntityPtr)->unPrepared();
  518. particle = static_cast<PUParticle3D *>(iter.second.getNext());
  519. }
  520. iter.second.lockAllDatas();
  521. }
  522. _prepared = false;
  523. }
  524. }
  525. void PUParticleSystem3D::preUpdator( float elapsedTime )
  526. {
  527. //if (_emitter && _emitter->isEnabled())
  528. //{
  529. // auto emitter = static_cast<PUEmitter*>(_emitter);
  530. // emitter->preUpdateEmitter(elapsedTime);
  531. //}
  532. //bool hasNoEmitted = true;
  533. for (auto it : _emitters) {
  534. if (!it->isEmitterDone()){
  535. (static_cast<PUEmitter*>(it))->preUpdateEmitter(elapsedTime);
  536. //hasNoEmitted = false;
  537. }
  538. }
  539. //if (hasNoEmitted){
  540. // if (_particlePool.getActiveParticleList().empty())
  541. // stopParticle();
  542. //}
  543. for (auto it : _affectors) {
  544. if (it->isEnabled()){
  545. (static_cast<PUAffector*>(it))->preUpdateAffector(elapsedTime);
  546. }
  547. }
  548. for (auto it : _observers){
  549. if (it->isEnabled()){
  550. it->preUpdateObserver(elapsedTime);
  551. }
  552. }
  553. for (auto &iter : _emittedEmitterParticlePool){
  554. PUParticle3D *particle = static_cast<PUParticle3D *>(iter.second.getFirst());
  555. while (particle)
  556. {
  557. static_cast<PUEmitter*>(particle->particleEntityPtr)->preUpdateEmitter(elapsedTime);
  558. particle = static_cast<PUParticle3D *>(iter.second.getNext());
  559. }
  560. }
  561. for (auto &iter : _emittedSystemParticlePool){
  562. PUParticle3D *particle = static_cast<PUParticle3D *>(iter.second.getFirst());
  563. while (particle)
  564. {
  565. static_cast<PUParticleSystem3D*>(particle->particleEntityPtr)->preUpdator(elapsedTime);
  566. particle = static_cast<PUParticle3D *>(iter.second.getNext());
  567. }
  568. }
  569. }
  570. void PUParticleSystem3D::updator( float elapsedTime )
  571. {
  572. bool firstActiveParticle = true;
  573. bool firstParticle = true;
  574. processParticle(_particlePool, firstActiveParticle, firstParticle, elapsedTime);
  575. for (auto &iter : _emittedEmitterParticlePool){
  576. processParticle(iter.second, firstActiveParticle, firstParticle, elapsedTime);
  577. }
  578. for (auto &iter : _emittedSystemParticlePool){
  579. processParticle(iter.second, firstActiveParticle, firstParticle, elapsedTime);
  580. }
  581. }
  582. void PUParticleSystem3D::postUpdator( float elapsedTime )
  583. {
  584. //if (_emitter && _emitter->isEnabled())
  585. //{
  586. // auto emitter = static_cast<PUEmitter*>(_emitter);
  587. // emitter->postUpdateEmitter(elapsedTime);
  588. //}
  589. for (auto it : _emitters) {
  590. if (it->isEnabled()){
  591. (static_cast<PUEmitter*>(it))->postUpdateEmitter(elapsedTime);
  592. }
  593. }
  594. for (auto it : _affectors) {
  595. if (it->isEnabled())
  596. {
  597. auto affector = static_cast<PUAffector*>(it);
  598. affector->postUpdateAffector(elapsedTime);
  599. }
  600. }
  601. for (auto it : _observers){
  602. if (it->isEnabled()){
  603. it->postUpdateObserver(elapsedTime);
  604. }
  605. }
  606. for (auto &iter : _emittedEmitterParticlePool){
  607. PUParticle3D *particle = static_cast<PUParticle3D *>(iter.second.getFirst());
  608. while (particle)
  609. {
  610. static_cast<PUEmitter*>(particle->particleEntityPtr)->postUpdateEmitter(elapsedTime);
  611. particle = static_cast<PUParticle3D *>(iter.second.getNext());
  612. }
  613. }
  614. for (auto &iter : _emittedSystemParticlePool){
  615. PUParticle3D *particle = static_cast<PUParticle3D *>(iter.second.getFirst());
  616. while (particle)
  617. {
  618. static_cast<PUParticleSystem3D*>(particle->particleEntityPtr)->postUpdator(elapsedTime);
  619. particle = static_cast<PUParticle3D *>(iter.second.getNext());
  620. }
  621. }
  622. }
  623. void PUParticleSystem3D::emitParticles( float elapsedTime )
  624. {
  625. //Vec3 scale = getDerivedScale();
  626. for (auto iter : _emitters){
  627. //if (!_emitter) return;
  628. //auto emitter = static_cast<PUEmitter*>(_emitter);
  629. auto emitter = iter;
  630. //emitter->notifyRescaled(scale);
  631. if (!iter->isMarkedForEmission()){
  632. unsigned short requested = emitter->calculateRequestedParticles(elapsedTime);
  633. executeEmitParticles(emitter, requested, elapsedTime);
  634. }
  635. }
  636. }
  637. float PUParticleSystem3D::getDefaultWidth() const
  638. {
  639. return _defaultWidth;
  640. }
  641. void PUParticleSystem3D::setDefaultWidth( const float width )
  642. {
  643. _defaultWidth = width;
  644. }
  645. float PUParticleSystem3D::getDefaultHeight() const
  646. {
  647. return _defaultHeight;
  648. }
  649. void PUParticleSystem3D::setDefaultHeight( const float height )
  650. {
  651. _defaultHeight = height;
  652. }
  653. float PUParticleSystem3D::getDefaultDepth() const
  654. {
  655. return _defaultDepth;
  656. }
  657. void PUParticleSystem3D::setDefaultDepth( const float depth )
  658. {
  659. _defaultDepth = depth;
  660. }
  661. bool PUParticleSystem3D::isExpired( PUParticle3D* particle, float timeElapsed )
  662. {
  663. bool expired = particle->timeToLive < timeElapsed;
  664. if (expired)
  665. {
  666. // Set the flag to indicate that the particle has been expired
  667. particle->addEventFlags(PUParticle3D::PEF_EXPIRED);
  668. }
  669. return expired;
  670. }
  671. cocos2d::Vec3 PUParticleSystem3D::getDerivedPosition()
  672. {
  673. //if (_parentParticleSystem && _parentParticleSystem->isKeepLocal()) return Vec3::ZERO;
  674. //if (_keepLocal) return Vec3::ZERO;
  675. if (_isMarkedForEmission){
  676. return Vec3(_position.x, _position.y, _positionZ);
  677. }else{
  678. Mat4 mat = getNodeToWorldTransform();
  679. return Vec3(mat.m[12], mat.m[13], mat.m[14]);
  680. }
  681. }
  682. cocos2d::Quaternion PUParticleSystem3D::getDerivedOrientation()
  683. {
  684. //if (_parentParticleSystem && _parentParticleSystem->isKeepLocal()) return Quaternion();
  685. //if (_keepLocal) return Quaternion();
  686. if (_isMarkedForEmission){
  687. return getRotationQuat();
  688. }else{
  689. Quaternion q;
  690. Mat4 mat = getNodeToWorldTransform();
  691. mat.decompose(nullptr, &q, nullptr);
  692. return q;
  693. }
  694. }
  695. cocos2d::Vec3 PUParticleSystem3D::getDerivedScale()
  696. {
  697. //if (_parentParticleSystem && _parentParticleSystem->isKeepLocal()) return Vec3::ONE;
  698. //if (_keepLocal) return Vec3::ONE;
  699. if (_isMarkedForEmission){
  700. return Vec3(_scaleX, _scaleY, _scaleZ);
  701. }else{
  702. Vec3 s;
  703. Mat4 mat = getNodeToWorldTransform();
  704. mat.decompose(&s, nullptr, nullptr);
  705. return s;
  706. }
  707. }
  708. float PUParticleSystem3D::getMaxVelocity() const
  709. {
  710. return _maxVelocity;
  711. }
  712. void PUParticleSystem3D::setMaxVelocity( float maxVelocity )
  713. {
  714. _maxVelocity = maxVelocity;
  715. _maxVelocitySet = true;
  716. }
  717. bool PUParticleSystem3D::initSystem( const std::string &filePath )
  718. {
  719. bool isFirstCompile = true;
  720. auto list = PUScriptCompiler::Instance()->compile(filePath, isFirstCompile);
  721. if (list == nullptr || list->empty()) return false;
  722. PUTranslateManager::Instance()->translateParticleSystem(this, list);
  723. //std::string data = FileUtils::getInstance()->getStringFromFile(filePath);
  724. return true;
  725. }
  726. void PUParticleSystem3D::addEmitter( PUEmitter* emitter )
  727. {
  728. if (emitter && std::find(_emitters.begin(), _emitters.end(), emitter) == _emitters.end()){
  729. emitter->_particleSystem = this;
  730. emitter->retain();
  731. _emitters.push_back(emitter);
  732. }
  733. }
  734. PUAffector* PUParticleSystem3D::getAffector( const std::string &name )
  735. {
  736. for (auto iter : _affectors){
  737. auto pa = static_cast<PUAffector *>(iter);
  738. if (pa->getName() == name)
  739. return pa;
  740. }
  741. return nullptr;
  742. }
  743. PUEmitter* PUParticleSystem3D::getEmitter( const std::string &name )
  744. {
  745. for (auto iter : _emitters){
  746. auto pe = static_cast<PUEmitter *>(iter);
  747. if (pe->getName() == name)
  748. return pe;
  749. }
  750. return nullptr;
  751. }
  752. void PUParticleSystem3D::addListener( PUListener *listener )
  753. {
  754. auto iter = std::find(_listeners.begin(), _listeners.end(), listener);
  755. if (iter == _listeners.end()){
  756. _listeners.push_back(listener);
  757. }
  758. }
  759. void PUParticleSystem3D::removeListener( PUListener *listener )
  760. {
  761. auto iter = std::find(_listeners.begin(), _listeners.end(), listener);
  762. if (iter != _listeners.end()){
  763. _listeners.erase(iter);
  764. }
  765. }
  766. void PUParticleSystem3D::forceEmission( PUEmitter* emitter, unsigned requested )
  767. {
  768. // Fast rejection: Don't emit if the technique is not enabled
  769. if (!_isEnabled)
  770. return;
  771. executeEmitParticles(emitter, requested, 0);
  772. }
  773. void PUParticleSystem3D::executeEmitParticles( PUEmitter* emitter, unsigned requested, float elapsedTime )
  774. {
  775. if (_state == State::STOP) return;
  776. if (emitter->getEmitsType() == PUParticle3D::PT_VISUAL){
  777. emitParticles(_particlePool, emitter, requested, elapsedTime);
  778. }else if (emitter->getEmitsType() == PUParticle3D::PT_EMITTER){
  779. emitParticles(_emittedEmitterParticlePool[emitter->getEmitsName()], emitter, requested, elapsedTime);
  780. }else if (emitter->getEmitsType() == PUParticle3D::PT_TECHNIQUE){
  781. emitParticles(_emittedSystemParticlePool[emitter->getEmitsName()], emitter, requested, elapsedTime);
  782. }
  783. }
  784. void PUParticleSystem3D::emitParticles( ParticlePool &pool, PUEmitter* emitter, unsigned requested, float elapsedTime )
  785. {
  786. Vec3 scale = getDerivedScale();
  787. Mat4 rotMat;
  788. Mat4::createRotation(getDerivedOrientation(), &rotMat);
  789. float timePoint = 0.0f;
  790. float timeInc = elapsedTime / requested;
  791. for (unsigned short i = 0; i < requested; ++i)
  792. {
  793. PUParticle3D *particle = static_cast<PUParticle3D *>(pool.createData());
  794. if (!particle)
  795. return;
  796. particle->initForEmission();
  797. emitter->initParticleForEmission(particle);
  798. particle->direction = (rotMat * Vec3(particle->direction.x, particle->direction.y, particle->direction.z));
  799. particle->originalDirection = (rotMat * Vec3(particle->originalDirection.x, particle->originalDirection.y, particle->originalDirection.z));
  800. for (auto& it : _affectors) {
  801. if (it->isEnabled())
  802. {
  803. (static_cast<PUAffector*>(it))->initParticleForEmission(particle);
  804. }
  805. }
  806. initParticleForEmission(particle);
  807. particle->position.add(particle->direction.x * scale.x * _particleSystemScaleVelocity * timePoint
  808. , particle->direction.y * scale.y * _particleSystemScaleVelocity * timePoint
  809. , particle->direction.z * scale.z * _particleSystemScaleVelocity * timePoint);
  810. // Increment time fragment
  811. timePoint += timeInc;
  812. }
  813. }
  814. void PUParticleSystem3D::addObserver( PUObserver *observer )
  815. {
  816. auto iter = std::find(_observers.begin(), _observers.end(), observer);
  817. if (iter == _observers.end()){
  818. observer->retain();
  819. observer->_particleSystem = this;
  820. _observers.push_back(observer);
  821. }
  822. }
  823. PUObserver* PUParticleSystem3D::getObserver( const std::string &name )
  824. {
  825. for (auto iter : _observers){
  826. auto po = static_cast<PUObserver *>(iter);
  827. if (po->getName() == name)
  828. return po;
  829. }
  830. return nullptr;
  831. }
  832. void PUParticleSystem3D::notifyRescaled(const Vec3 &scl)
  833. {
  834. if (_render)
  835. _render->notifyRescaled(scl);
  836. for (auto it : _emitters) {
  837. (static_cast<PUEmitter*>(it))->notifyRescaled(scl);
  838. }
  839. for (auto it : _affectors) {
  840. (static_cast<PUAffector*>(it))->notifyRescaled(scl);
  841. }
  842. for (auto it : _observers){
  843. it->notifyRescaled(scl);
  844. }
  845. for (auto &iter : _emittedEmitterParticlePool){
  846. PUParticle3D *particle = static_cast<PUParticle3D *>(iter.second.getFirst());
  847. while (particle)
  848. {
  849. static_cast<PUEmitter*>(particle->particleEntityPtr)->notifyRescaled(scl);
  850. particle = static_cast<PUParticle3D *>(iter.second.getNext());
  851. }
  852. }
  853. for (auto &iter : _emittedSystemParticlePool){
  854. PUParticle3D *particle = static_cast<PUParticle3D *>(iter.second.getFirst());
  855. while (particle)
  856. {
  857. static_cast<PUParticleSystem3D*>(particle->particleEntityPtr)->notifyRescaled(scl);
  858. particle = static_cast<PUParticle3D *>(iter.second.getNext());
  859. }
  860. }
  861. }
  862. void PUParticleSystem3D::initParticleForExpiration( PUParticle3D* particle, float timeElapsed )
  863. {
  864. if (particle->particleType == PUParticle3D::PT_EMITTER){
  865. PUEmitter *emitter = static_cast<PUEmitter *>(particle->particleEntityPtr);
  866. emitter->unPrepare();
  867. }else if (particle->particleType == PUParticle3D::PT_TECHNIQUE){
  868. PUParticleSystem3D *system = static_cast<PUParticleSystem3D *>(particle->particleEntityPtr);
  869. system->unPrepared();
  870. }
  871. particle->initForExpiration(timeElapsed);
  872. for (auto it : _listeners){
  873. it->particleExpired(this, particle);
  874. }
  875. ///** Externs are also called to perform expiration activities. If needed, affectors and emitters may be added, but at the moment
  876. // there is no reason for (and we don't want to waste cpu resources).
  877. //*/
  878. //if (!mExterns.empty())
  879. //{
  880. // ExternIterator itExtern;
  881. // ExternIterator itExternEnd = mExterns.end();
  882. // for (itExtern = mExterns.begin(); itExtern != itExternEnd; ++itExtern)
  883. // {
  884. // (*itExtern)->_initParticleForExpiration(particle);
  885. // }
  886. //}
  887. }
  888. void PUParticleSystem3D::initParticleForEmission( PUParticle3D* particle )
  889. {
  890. for (auto it : _listeners){
  891. it->particleEmitted(this, particle);
  892. }
  893. }
  894. void PUParticleSystem3D::addBehaviourTemplate( PUBehaviour *behaviour )
  895. {
  896. auto iter = std::find(_behaviourTemplates.begin(), _behaviourTemplates.end(), behaviour);
  897. if (iter == _behaviourTemplates.end()){
  898. behaviour->retain();
  899. behaviour->_particleSystem = this;
  900. _behaviourTemplates.push_back(behaviour);
  901. }
  902. }
  903. void PUParticleSystem3D::convertToUnixStylePath( std::string &path )
  904. {
  905. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
  906. for (auto &iter : path){
  907. if (iter == '\\') iter = '/';
  908. }
  909. #else
  910. CC_UNUSED_PARAM(path);
  911. #endif
  912. }
  913. void PUParticleSystem3D::clearAllParticles()
  914. {
  915. _particlePool.lockAllDatas();
  916. for (auto &iter : _emittedEmitterParticlePool){
  917. iter.second.lockAllDatas();
  918. }
  919. for (auto &iter : _emittedSystemParticlePool){
  920. iter.second.lockAllDatas();
  921. }
  922. }
  923. void PUParticleSystem3D::copyAttributesTo(PUParticleSystem3D* system )
  924. {
  925. system->removeAllEmitter();
  926. system->removeAllAffector();
  927. system->removerAllObserver();
  928. system->removeAllBehaviourTemplate();
  929. system->removeAllListener();
  930. system->_particlePool.removeAllDatas();
  931. for (auto iter : system->_emittedEmitterParticlePool){
  932. iter.second.removeAllDatas();
  933. }
  934. for (auto iter : system->_emittedSystemParticlePool){
  935. iter.second.removeAllDatas();
  936. }
  937. system->setName(_name);
  938. system->_state = _state;
  939. if (_render)
  940. system->setRender(static_cast<PURender *>(_render)->clone());
  941. system->_particleQuota = _particleQuota;
  942. system->_blend = _blend;
  943. system->_keepLocal = _keepLocal;
  944. system->_isEnabled = _isEnabled;
  945. for (auto iter : _affectors){
  946. PUAffector *affector = static_cast<PUAffector *>(iter);
  947. PUAffector *copy = PUAffectorManager::Instance()->createAffector(affector->getAffectorType());
  948. affector->copyAttributesTo(copy);
  949. system->addAffector(copy);
  950. }
  951. for (auto iter : _emitters){
  952. PUEmitter *emitter = static_cast<PUEmitter *>(iter);
  953. PUEmitter *copy = PUEmitterManager::Instance()->createEmitter(emitter->getEmitterType());
  954. emitter->copyAttributesTo(copy);
  955. system->addEmitter(copy);
  956. }
  957. for (auto iter : _observers){
  958. PUObserver *observer = static_cast<PUObserver *>(iter);
  959. PUObserver *copy = PUObserverManager::Instance()->createObserver(observer->getObserverType());
  960. observer->copyAttributesTo(copy);
  961. system->addObserver(copy);
  962. }
  963. for (auto iter : _behaviourTemplates){
  964. PUBehaviour *behaviour = static_cast<PUBehaviour *>(iter);
  965. PUBehaviour *copy = behaviour->clone();
  966. system->addBehaviourTemplate(copy);
  967. }
  968. system->_emittedEmitterQuota = _emittedEmitterQuota;
  969. system->_emittedSystemQuota = _emittedSystemQuota;
  970. system->_prepared = false;
  971. system->_particleSystemScaleVelocity = _particleSystemScaleVelocity;
  972. system->_defaultWidth = _defaultWidth;
  973. system->_defaultHeight = _defaultHeight;
  974. system->_defaultDepth = _defaultDepth;
  975. system->_maxVelocity = _maxVelocity;
  976. system->_maxVelocitySet = _maxVelocitySet;
  977. system->_matName = _matName;
  978. system->_isMarkedForEmission = _isMarkedForEmission;
  979. system->_parentParticleSystem = _parentParticleSystem;
  980. }
  981. PUParticleSystem3D* PUParticleSystem3D::clone()
  982. {
  983. auto ps = PUParticleSystem3D::create();
  984. copyAttributesTo(ps);
  985. for (auto &iter : _children){
  986. PUParticleSystem3D *child = dynamic_cast<PUParticleSystem3D *>(iter);
  987. if (child)
  988. ps->addChild(child->clone());
  989. }
  990. return ps;
  991. }
  992. void PUParticleSystem3D::removeAllEmitter()
  993. {
  994. for (auto iter : _emitters){
  995. iter->release();
  996. }
  997. _emitters.clear();
  998. }
  999. void PUParticleSystem3D::removerAllObserver()
  1000. {
  1001. for (auto iter : _observers){
  1002. iter->release();
  1003. }
  1004. _observers.clear();
  1005. }
  1006. void PUParticleSystem3D::removeAllBehaviourTemplate()
  1007. {
  1008. for (auto iter : _behaviourTemplates){
  1009. iter->release();
  1010. }
  1011. _behaviourTemplates.clear();
  1012. }
  1013. void PUParticleSystem3D::removeAllListener()
  1014. {
  1015. _listeners.clear();
  1016. }
  1017. void PUParticleSystem3D::draw( Renderer *renderer, const Mat4 &transform, uint32_t flags )
  1018. {
  1019. if (!_isEnabled) return;
  1020. if (getAliveParticleCount() <= 0) return;
  1021. if (_render)
  1022. _render->render(renderer, transform, this);
  1023. if (!_emittedSystemParticlePool.empty())
  1024. {
  1025. for (auto &iter : _emittedSystemParticlePool)
  1026. {
  1027. PUParticle3D *particle = static_cast<PUParticle3D *>(iter.second.getFirst());
  1028. while (particle)
  1029. {
  1030. static_cast<PUParticleSystem3D *>(particle->particleEntityPtr)->draw(renderer, transform, flags);
  1031. particle = static_cast<PUParticle3D *>(iter.second.getNext());
  1032. }
  1033. }
  1034. }
  1035. }
  1036. void PUParticleSystem3D::processParticle( ParticlePool &pool, bool &firstActiveParticle, bool &firstParticle, float elapsedTime )
  1037. {
  1038. Vec3 scale = getDerivedScale();
  1039. PUParticle3D *particle = static_cast<PUParticle3D *>(pool.getFirst());
  1040. //Mat4 ltow = getNodeToWorldTransform();
  1041. //Vec3 scl;
  1042. //Quaternion rot;
  1043. //ltow.decompose(&scl, &rot, nullptr);
  1044. while (particle){
  1045. if (!isExpired(particle, elapsedTime)){
  1046. particle->process(elapsedTime);
  1047. //if (_emitter && _emitter->isEnabled())
  1048. // _emitter->updateEmitter(particle, elapsedTime);
  1049. for (auto it : _emitters) {
  1050. if (it->isEnabled() && !it->isMarkedForEmission()){
  1051. (static_cast<PUEmitter*>(it))->updateEmitter(particle, elapsedTime);
  1052. }
  1053. }
  1054. for (auto& it : _affectors) {
  1055. if (it->isEnabled()){
  1056. (static_cast<PUAffector*>(it))->process(particle, elapsedTime, firstActiveParticle);
  1057. }
  1058. }
  1059. if (_render)
  1060. static_cast<PURender *>(_render)->updateRender(particle, elapsedTime, firstActiveParticle);
  1061. if (_isEnabled && particle->particleType != PUParticle3D::PT_VISUAL){
  1062. if (particle->particleType == PUParticle3D::PT_EMITTER){
  1063. auto emitter = static_cast<PUEmitter *>(particle->particleEntityPtr);
  1064. emitter->setLocalPosition(particle->position);
  1065. executeEmitParticles(emitter, emitter->calculateRequestedParticles(elapsedTime), elapsedTime);
  1066. }else if (particle->particleType == PUParticle3D::PT_TECHNIQUE){
  1067. auto system = static_cast<PUParticleSystem3D *>(particle->particleEntityPtr);
  1068. system->setPosition3D(particle->position);
  1069. system->setRotationQuat(particle->orientation);
  1070. //system->setScaleX(scl.x);system->setScaleY(scl.y);system->setScaleZ(scl.z);
  1071. system->forceUpdate(elapsedTime);
  1072. }
  1073. }
  1074. firstActiveParticle = false;
  1075. // Keep latest position
  1076. particle->latestPosition = particle->position;
  1077. //if (_maxVelocitySet && particle->calculateVelocity() > _maxVelocity)
  1078. //{
  1079. // particle->direction *= (_maxVelocity / particle->direction.length());
  1080. //}
  1081. //// Update the position with the direction.
  1082. //particle->position += (particle->direction * _particleSystemScaleVelocity * elapsedTime);
  1083. //particle->positionInWorld = particle->position;
  1084. //particle->orientationInWorld = particle->orientation;
  1085. //particle->widthInWorld = particle->width;
  1086. //particle->heightInWorld = particle->height;
  1087. //particle->depthInWorld = particle->depth;
  1088. //bool keepLocal = _keepLocal;
  1089. //PUParticleSystem3D *parent = dynamic_cast<PUParticleSystem3D *>(getParent());
  1090. //if (parent) keepLocal = keepLocal || parent->isKeepLocal();
  1091. //if (keepLocal){
  1092. // ltow.transformPoint(particle->positionInWorld, &particle->positionInWorld);
  1093. // Vec3 ori;
  1094. // ltow.transformVector(Vec3(particle->orientation.x, particle->orientation.y, particle->orientation.z), &ori);
  1095. // particle->orientationInWorld.x = ori.x; particle->orientationInWorld.y = ori.y; particle->orientationInWorld.z = ori.z;
  1096. // particle->widthInWorld = scl.x * particle->width;
  1097. // particle->heightInWorld = scl.y * particle->height;
  1098. // particle->depthInWorld = scl.z * particle->depth;
  1099. //}
  1100. processMotion(particle, elapsedTime, scale, firstActiveParticle);
  1101. }
  1102. else{
  1103. initParticleForExpiration(particle, elapsedTime);
  1104. pool.lockLatestData();
  1105. }
  1106. for (auto it : _observers){
  1107. if (it->isEnabled()){
  1108. it->updateObserver(particle, elapsedTime, firstParticle);
  1109. }
  1110. }
  1111. if (particle->hasEventFlags(PUParticle3D::PEF_EXPIRED))
  1112. {
  1113. particle->setEventFlags(0);
  1114. particle->addEventFlags(PUParticle3D::PEF_EXPIRED);
  1115. }
  1116. else
  1117. {
  1118. particle->setEventFlags(0);
  1119. }
  1120. particle->timeToLive -= elapsedTime;
  1121. firstParticle = false;
  1122. particle = static_cast<PUParticle3D *>(pool.getNext());
  1123. }
  1124. }
  1125. bool PUParticleSystem3D::makeParticleLocal( PUParticle3D* particle )
  1126. {
  1127. if (!particle)
  1128. return true;
  1129. if (!_keepLocal)
  1130. return false;
  1131. particle->position += _latestPositionDiff;
  1132. return true;
  1133. }
  1134. void PUParticleSystem3D::processMotion( PUParticle3D* particle, float timeElapsed, const Vec3 &scl, bool /*firstParticle*/ )
  1135. {
  1136. if (particle->isFreezed())
  1137. return;
  1138. /** Because everything is calculated in worldspace we recalculate it back to the techniques' origin (the result is still
  1139. worldspace) just by adding a relative offset of the particle system, technique, emitter or visual particle.
  1140. Change in V1.4:
  1141. The additional PEF_EMITTED check fixes the problem with scenenodese moving forward at extremely fast speeds
  1142. The bug is due to how PU code processes keep_local, and the calculation shouldn't be applied to newly emitted particles.
  1143. */
  1144. if (!particle->hasEventFlags(PUParticle3D::PEF_EMITTED))
  1145. {
  1146. if (!particle->parentEmitter->makeParticleLocal(particle))
  1147. {
  1148. if (!makeParticleLocal(particle))
  1149. {
  1150. _parentParticleSystem->makeParticleLocal(particle);
  1151. }
  1152. }
  1153. }
  1154. /** Adjust position of the particle if the parent has rotated (only in case of particle system with 'keep local').
  1155. PU 1.4: Added check that technique may not be local, otherwise particles are rotated twice as fast.
  1156. */
  1157. if (_parentParticleSystem->isKeepLocal() && !_keepLocal)
  1158. {
  1159. /** Ignore some rendersystems, because they are rotated by Ogre itself.
  1160. Entities for example are always rotated (even if not local), based on the node orientation. A Billboard not.
  1161. */
  1162. if (_render && !static_cast<PURender *>(_render)->autoRotate)
  1163. {
  1164. _parentParticleSystem->rotationOffset(particle->position);
  1165. }
  1166. }
  1167. // Added for 1.3
  1168. if (particle->hasEventFlags(PUParticle3D::PEF_EMITTED))
  1169. return;
  1170. // Adjust the velocity to the allowed maximum.
  1171. if (_maxVelocitySet && particle->calculateVelocity() > _maxVelocity)
  1172. {
  1173. particle->direction *= (_maxVelocity / particle->direction.length());
  1174. }
  1175. // Update the position with the direction.
  1176. particle->position.add(particle->direction.x * scl.x * _particleSystemScaleVelocity * timeElapsed
  1177. , particle->direction.y * scl.y * _particleSystemScaleVelocity * timeElapsed
  1178. , particle->direction.z * scl.z * _particleSystemScaleVelocity * timeElapsed);
  1179. }
  1180. void PUParticleSystem3D::calulateRotationOffset( void )
  1181. {
  1182. if (_isMarkedForEmission)
  1183. {
  1184. // Use the uber particle system as centre of rotation and not the particle systems' own position.
  1185. _rotationCentre = _parentParticleSystem->getDerivedPosition();
  1186. }
  1187. else
  1188. {
  1189. // Use its own position
  1190. _rotationCentre = getDerivedPosition();
  1191. }
  1192. /** Use the derived orientation, which is the particle systems' own scenenode orientation,
  1193. or the orientation of the uber particle system, if this particle system is emitted itself.
  1194. */
  1195. Quaternion latestOrientationInverse = _latestOrientation;
  1196. latestOrientationInverse.inverse();
  1197. _rotationOffset = getDerivedOrientation() * latestOrientationInverse;
  1198. }
  1199. int PUParticleSystem3D::getAliveParticleCount() const
  1200. {
  1201. int sz = 0;
  1202. sz += _particlePool.getActiveDataList().size();
  1203. if (!_emittedEmitterParticlePool.empty()){
  1204. for (auto &iter : _emittedEmitterParticlePool){
  1205. sz += iter.second.getActiveDataList().size();
  1206. }
  1207. }
  1208. if (_emittedSystemParticlePool.empty())
  1209. return sz;
  1210. for (auto &iter : _emittedSystemParticlePool){
  1211. auto pool = iter.second;
  1212. sz += pool.getActiveDataList().size();
  1213. PUParticle3D *particle = static_cast<PUParticle3D *>(pool.getFirst());
  1214. while (particle)
  1215. {
  1216. sz += static_cast<PUParticleSystem3D*>(particle->particleEntityPtr)->getAliveParticleCount();
  1217. particle = static_cast<PUParticle3D *>(pool.getNext());
  1218. }
  1219. }
  1220. return sz;
  1221. }
  1222. void PUParticleSystem3D::forceStopParticleSystem()
  1223. {
  1224. if (_render)
  1225. _render->notifyStop();
  1226. for (auto &it : _observers){
  1227. it->notifyStop();
  1228. }
  1229. for (auto& it : _emitters) {
  1230. auto emitter = static_cast<PUEmitter*>(it);
  1231. emitter->notifyStop();
  1232. }
  1233. for (auto& it : _affectors) {
  1234. auto affector = static_cast<PUAffector*>(it);
  1235. affector->notifyStop();
  1236. }
  1237. unscheduleUpdate();
  1238. unPrepared();
  1239. }
  1240. NS_CC_END