axmol/extensions/Particle3D/PU/CCPUParticleSystem3D.cpp

1581 lines
46 KiB
C++
Raw Normal View History

2015-02-11 18:14:22 +08:00
/****************************************************************************
2015-03-02 16:05:26 +08:00
Copyright (C) 2013 Henry van Merode. All rights reserved.
Copyright (c) 2015-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
2021-12-25 10:04:45 +08:00
2015-02-11 18:14:22 +08:00
http://www.cocos2d-x.org
2021-12-25 10:04:45 +08:00
2015-02-11 18:14:22 +08:00
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
2021-12-25 10:04:45 +08:00
2015-02-11 18:14:22 +08:00
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
2021-12-25 10:04:45 +08:00
2015-02-11 18:14:22 +08:00
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
2015-03-05 13:24:06 +08:00
#include "extensions/Particle3D/PU/CCPUParticleSystem3D.h"
#include "extensions/Particle3D/PU/CCPUEmitter.h"
#include "extensions/Particle3D/PU/CCPUEmitterManager.h"
#include "extensions/Particle3D/PU/CCPUAffector.h"
#include "extensions/Particle3D/PU/CCPUAffectorManager.h"
2015-03-02 16:05:26 +08:00
#include "extensions/Particle3D/CCParticle3DRender.h"
2015-03-05 13:24:06 +08:00
#include "extensions/Particle3D/PU/CCPUScriptCompiler.h"
#include "extensions/Particle3D/PU/CCPUMaterialManager.h"
#include "extensions/Particle3D/PU/CCPUTranslateManager.h"
#include "extensions/Particle3D/PU/CCPUListener.h"
#include "extensions/Particle3D/PU/CCPUObserver.h"
#include "extensions/Particle3D/PU/CCPUObserverManager.h"
#include "extensions/Particle3D/PU/CCPUBehaviour.h"
2015-02-11 18:14:22 +08:00
#include "platform/CCFileUtils.h"
NS_CC_BEGIN
2021-12-25 10:04:45 +08:00
float PUParticle3D::DEFAULT_TTL = 10.0f;
2015-02-11 18:14:22 +08:00
float PUParticle3D::DEFAULT_MASS = 1.0f;
float PUParticle3D::calculateVelocity() const
{
if (originalScaledDirectionLength != 0)
{
return originalVelocity * (direction.length() / originalScaledDirectionLength);
}
else
{
// Assume originalScaledDirectionLength to be 1.0 (unit vector)
return originalVelocity * direction.length();
}
}
2021-12-25 10:04:45 +08:00
void PUParticle3D::setOwnDimensions(float newWidth, float newHeight, float newDepth)
2015-02-11 18:14:22 +08:00
{
ownDimensions = true;
if (newWidth)
width = newWidth;
if (newHeight)
height = newHeight;
if (newDepth)
depth = newDepth;
calculateBoundingSphereRadius();
2021-12-25 10:04:45 +08:00
// parentEmitter->getParentTechnique()->_notifyParticleResized();
2015-02-11 18:14:22 +08:00
}
void PUParticle3D::calculateBoundingSphereRadius()
{
2021-12-25 10:04:45 +08:00
// radius = 0.5 * Math::Sqrt(width*width + height*height + depth*depth);
radius = 0.5f * std::max(depth, std::max(width, height)); // approximation
2015-02-11 18:14:22 +08:00
}
void PUParticle3D::initForEmission()
{
2021-12-25 10:04:45 +08:00
eventFlags = 0;
2015-02-11 18:14:22 +08:00
timeFraction = 0.0f;
/* Note, that this flag must only be set as soon as the particle is emitted. As soon as the particle has
been moved once, the flag must be removed again.
*/
addEventFlags(PUParticle3D::PEF_EMITTED);
// Reset freeze flag
freezed = false;
2015-03-02 13:07:32 +08:00
2021-12-25 10:04:45 +08:00
if (!behaviours.empty())
{
for (auto& it : behaviours)
{
it->initParticleForEmission(this);
}
2015-03-02 13:07:32 +08:00
}
}
2021-12-25 10:04:45 +08:00
void PUParticle3D::initForExpiration(float timeElapsed)
2015-03-02 13:07:32 +08:00
{
2021-12-25 10:04:45 +08:00
if (!behaviours.empty())
{
for (auto& it : behaviours)
{
it->initParticleForExpiration(this, timeElapsed);
}
2015-03-02 13:07:32 +08:00
}
}
2021-12-25 10:04:45 +08:00
void PUParticle3D::process(float timeElapsed)
2015-03-02 13:07:32 +08:00
{
timeFraction = (totalTimeToLive - timeToLive) / totalTimeToLive;
2021-12-25 10:04:45 +08:00
if (!behaviours.empty())
{
for (auto& it : behaviours)
{
it->updateBehaviour(this, timeElapsed);
}
2015-03-02 13:07:32 +08:00
}
2015-02-11 18:14:22 +08:00
}
2021-12-25 10:04:45 +08:00
PUParticle3D::PUParticle3D()
: particleEntityPtr(nullptr)
, parentEmitter(nullptr)
, visualData(nullptr)
, particleType(PT_VISUAL)
, originalDirectionLength(0.0f)
, originalVelocity(0.0f)
, originalScaledDirectionLength(0.0f)
, rotationAxis(Vec3::UNIT_Z)
,
// color(Vec4::ONE),
originalColor(Vec4::ONE)
,
// zRotation(0.0f),
zRotationSpeed(0.0f)
, rotationSpeed(0.0f)
, radius(0.87f)
, ownDimensions(false)
, eventFlags(0)
, freezed(false)
, timeToLive(DEFAULT_TTL)
, totalTimeToLive(DEFAULT_TTL)
, timeFraction(0.0f)
, mass(DEFAULT_MASS)
, textureAnimationTimeStep(0.1f)
, textureAnimationTimeStepCount(0.0f)
, textureCoordsCurrent(0)
, textureAnimationDirectionUp(true)
, depthInView(0.0f)
, zRotation(0.0f)
// widthInWorld(width),
// heightInWorld(height),
// depthInWorld(depth)
{}
2015-03-02 13:07:32 +08:00
PUParticle3D::~PUParticle3D()
2015-02-11 18:14:22 +08:00
{
2021-12-25 10:04:45 +08:00
for (auto it : behaviours)
{
2015-03-02 13:07:32 +08:00
it->release();
}
2021-12-25 10:04:45 +08:00
// CC_SAFE_RELEASE(particleEntityPtr);
2015-03-02 13:07:32 +08:00
}
2021-12-25 10:04:45 +08:00
void PUParticle3D::copyBehaviours(const ParticleBehaviourList& list)
2015-03-02 13:07:32 +08:00
{
2021-12-25 10:04:45 +08:00
for (auto it : list)
{
2015-03-02 13:07:32 +08:00
auto behaviour = it->clone();
behaviour->retain();
behaviours.push_back(behaviour);
}
2015-02-11 18:14:22 +08:00
}
2015-03-02 13:07:32 +08:00
2015-02-11 18:14:22 +08:00
//-----------------------------------------------------------------------
2021-12-25 10:04:45 +08:00
const float PUParticleSystem3D::DEFAULT_WIDTH = 50;
const float PUParticleSystem3D::DEFAULT_HEIGHT = 50;
const float PUParticleSystem3D::DEFAULT_DEPTH = 50;
const unsigned int PUParticleSystem3D::DEFAULT_PARTICLE_QUOTA = 500;
2015-03-02 13:07:32 +08:00
const unsigned int PUParticleSystem3D::DEFAULT_EMITTED_EMITTER_QUOTA = 50;
2021-12-25 10:04:45 +08:00
const unsigned int PUParticleSystem3D::DEFAULT_EMITTED_SYSTEM_QUOTA = 10;
const float PUParticleSystem3D::DEFAULT_MAX_VELOCITY = 9999.0f;
2015-02-11 18:14:22 +08:00
PUParticleSystem3D::PUParticleSystem3D()
2021-12-25 10:04:45 +08:00
: _emittedEmitterQuota(DEFAULT_EMITTED_EMITTER_QUOTA)
, _emittedSystemQuota(DEFAULT_EMITTED_SYSTEM_QUOTA)
, _prepared(false)
, _poolPrepared(false)
, _particleSystemScaleVelocity(1.0f)
, _timeElapsedSinceStart(0.0f)
, _defaultWidth(DEFAULT_WIDTH)
, _defaultHeight(DEFAULT_HEIGHT)
, _defaultDepth(DEFAULT_DEPTH)
, _maxVelocity(DEFAULT_MAX_VELOCITY)
, _maxVelocitySet(false)
, _isMarkedForEmission(false)
, _parentParticleSystem(nullptr)
2015-02-11 18:14:22 +08:00
{
_particleQuota = DEFAULT_PARTICLE_QUOTA;
}
PUParticleSystem3D::~PUParticleSystem3D()
{
2015-03-02 13:07:32 +08:00
stopParticleSystem();
2015-02-11 18:14:22 +08:00
unPrepared();
2015-03-02 13:07:32 +08:00
_particlePool.removeAllDatas();
2021-12-25 10:04:45 +08:00
for (auto iter : _emittedEmitterParticlePool)
{
auto pool = iter.second;
2015-03-02 13:07:32 +08:00
auto lockedList = pool.getUnActiveDataList();
2021-12-25 10:04:45 +08:00
for (auto iter2 : lockedList)
{
static_cast<PUParticle3D*>(iter2)->particleEntityPtr->release();
2015-03-02 13:07:32 +08:00
}
iter.second.removeAllDatas();
}
2021-12-25 10:04:45 +08:00
for (auto iter : _emittedSystemParticlePool)
{
auto pool = iter.second;
2015-03-02 13:07:32 +08:00
auto lockedList = pool.getUnActiveDataList();
2021-12-25 10:04:45 +08:00
for (auto iter2 : lockedList)
{
static_cast<PUParticle3D*>(iter2)->particleEntityPtr->release();
2015-03-02 13:07:32 +08:00
}
iter.second.removeAllDatas();
}
2021-12-25 10:04:45 +08:00
// release all emitters
for (auto it : _emitters)
{
2015-02-11 18:14:22 +08:00
it->release();
}
_emitters.clear();
2015-03-02 13:07:32 +08:00
2021-12-25 10:04:45 +08:00
for (auto it : _observers)
{
2015-03-02 13:07:32 +08:00
it->release();
}
2021-12-25 10:04:45 +08:00
for (auto it : _behaviourTemplates)
{
2015-03-02 13:07:32 +08:00
it->release();
}
_observers.clear();
2015-02-11 18:14:22 +08:00
}
PUParticleSystem3D* PUParticleSystem3D::create()
{
2021-12-08 00:11:53 +08:00
auto pups = new PUParticleSystem3D();
2015-02-11 18:14:22 +08:00
pups->autorelease();
return pups;
}
2021-12-25 10:04:45 +08:00
PUParticleSystem3D* PUParticleSystem3D::create(const std::string& filePath, const std::string& materialPath)
2015-02-11 18:14:22 +08:00
{
2021-12-25 10:04:45 +08:00
PUParticleSystem3D* ret = new PUParticleSystem3D();
2021-12-08 00:11:53 +08:00
if (ret->initWithFilePathAndMaterialPath(filePath, materialPath))
{
ret->autorelease();
return ret;
}
else
{
CC_SAFE_DELETE(ret);
return nullptr;
2015-02-11 18:14:22 +08:00
}
}
2021-12-25 10:04:45 +08:00
PUParticleSystem3D* PUParticleSystem3D::create(const std::string& filePath)
{
2021-12-25 10:04:45 +08:00
PUParticleSystem3D* ret = new PUParticleSystem3D();
2021-12-08 00:11:53 +08:00
if (ret->initWithFilePath(filePath))
{
ret->autorelease();
return ret;
}
else
{
CC_SAFE_DELETE(ret);
return nullptr;
}
}
2021-12-25 10:04:45 +08:00
bool PUParticleSystem3D::initWithFilePath(const std::string& filePath)
2015-02-11 18:14:22 +08:00
{
std::string fullPath = FileUtils::getInstance()->fullPathForFilename(filePath);
convertToUnixStylePath(fullPath);
std::string::size_type pos = fullPath.find_last_of('/');
2015-02-11 18:14:22 +08:00
std::string materialFolder = "materials";
2021-12-25 10:04:45 +08:00
if (pos != std::string::npos)
{
2015-02-11 18:14:22 +08:00
std::string temp = fullPath.substr(0, pos);
2021-12-25 10:04:45 +08:00
pos = temp.find_last_of('/');
if (pos != std::string::npos)
{
2015-02-11 18:14:22 +08:00
materialFolder = temp.substr(0, pos + 1) + materialFolder;
}
}
static std::vector<std::string> loadedFolder;
if (std::find(loadedFolder.begin(), loadedFolder.end(), materialFolder) == loadedFolder.end())
{
2015-03-05 13:24:06 +08:00
PUMaterialCache::Instance()->loadMaterialsFromSearchPaths(materialFolder);
2015-02-11 18:14:22 +08:00
loadedFolder.push_back(materialFolder);
}
2021-12-25 10:04:45 +08:00
if (!initSystem(fullPath))
{
return false;
}
return true;
}
2021-12-25 10:04:45 +08:00
bool PUParticleSystem3D::initWithFilePathAndMaterialPath(const std::string& filePath, const std::string& materialPath)
{
std::string matfullPath = FileUtils::getInstance()->fullPathForFilename(materialPath);
convertToUnixStylePath(matfullPath);
PUMaterialCache::Instance()->loadMaterials(matfullPath);
std::string fullPath = FileUtils::getInstance()->fullPathForFilename(filePath);
convertToUnixStylePath(fullPath);
2021-12-25 10:04:45 +08:00
if (!initSystem(fullPath))
{
return false;
2015-02-11 18:14:22 +08:00
}
return true;
2015-02-11 18:14:22 +08:00
}
2015-02-12 16:09:08 +08:00
void PUParticleSystem3D::startParticleSystem()
2015-02-11 18:14:22 +08:00
{
2015-02-12 16:09:08 +08:00
stopParticleSystem();
2015-02-11 18:14:22 +08:00
2015-03-02 13:07:32 +08:00
if (_state != State::RUNNING)
{
2015-03-02 17:47:26 +08:00
forceStopParticleSystem();
2015-03-02 13:07:32 +08:00
if (_render)
_render->notifyStart();
2015-02-11 18:14:22 +08:00
2021-12-25 10:04:45 +08:00
for (auto& it : _observers)
{
2015-03-02 13:07:32 +08:00
it->notifyStart();
}
2021-12-25 10:04:45 +08:00
for (auto& it : _emitters)
{
2015-03-05 13:24:06 +08:00
auto emitter = static_cast<PUEmitter*>(it);
2015-03-02 13:07:32 +08:00
emitter->notifyStart();
}
2015-02-11 18:14:22 +08:00
2021-12-25 10:04:45 +08:00
for (auto& it : _affectors)
{
2015-03-05 13:24:06 +08:00
auto affector = static_cast<PUAffector*>(it);
2015-03-02 13:07:32 +08:00
affector->notifyStart();
2015-02-11 18:14:22 +08:00
}
2015-03-02 13:07:32 +08:00
scheduleUpdate();
_state = State::RUNNING;
2015-02-11 18:14:22 +08:00
}
for (auto iter : _children)
{
2021-12-25 10:04:45 +08:00
PUParticleSystem3D* system = dynamic_cast<PUParticleSystem3D*>(iter);
if (system)
{
2015-03-02 13:07:32 +08:00
system->_parentParticleSystem = this;
2015-02-12 16:09:08 +08:00
system->startParticleSystem();
2015-03-02 13:07:32 +08:00
}
2015-02-11 18:14:22 +08:00
}
}
2015-02-12 16:09:08 +08:00
void PUParticleSystem3D::stopParticleSystem()
2015-02-11 18:14:22 +08:00
{
2015-03-02 13:07:32 +08:00
if (_state != State::STOP)
{
_state = State::STOP;
2015-02-11 18:14:22 +08:00
}
for (auto iter : _children)
{
2021-12-25 10:04:45 +08:00
PUParticleSystem3D* system = dynamic_cast<PUParticleSystem3D*>(iter);
2015-02-11 18:14:22 +08:00
if (system)
2015-02-12 16:09:08 +08:00
system->stopParticleSystem();
2015-02-11 18:14:22 +08:00
}
}
2015-02-12 16:09:08 +08:00
void PUParticleSystem3D::pauseParticleSystem()
2015-02-11 18:14:22 +08:00
{
if (_state == State::RUNNING)
{
2021-12-25 10:04:45 +08:00
// if (_emitter)
2015-02-11 18:14:22 +08:00
//{
2021-12-25 10:04:45 +08:00
// auto emitter = static_cast<PUEmitter*>(_emitter);
// emitter->notifyPause();
// }
2015-02-11 18:14:22 +08:00
2021-12-25 10:04:45 +08:00
for (auto& it : _emitters)
{
2015-03-05 13:24:06 +08:00
auto emitter = static_cast<PUEmitter*>(it);
2015-02-11 18:14:22 +08:00
emitter->notifyPause();
}
2021-12-25 10:04:45 +08:00
for (auto& it : _affectors)
{
2015-03-05 13:24:06 +08:00
auto affector = static_cast<PUAffector*>(it);
2015-02-11 18:14:22 +08:00
affector->notifyPause();
}
_state = State::PAUSE;
}
for (auto iter : _children)
{
2021-12-25 10:04:45 +08:00
PUParticleSystem3D* system = dynamic_cast<PUParticleSystem3D*>(iter);
2015-02-11 18:14:22 +08:00
if (system)
2015-02-12 16:09:08 +08:00
system->pauseParticleSystem();
2015-02-11 18:14:22 +08:00
}
}
2015-02-12 16:09:08 +08:00
void PUParticleSystem3D::resumeParticleSystem()
2015-02-11 18:14:22 +08:00
{
if (_state == State::PAUSE)
{
2021-12-25 10:04:45 +08:00
// if (_emitter)
2015-02-11 18:14:22 +08:00
//{
2021-12-25 10:04:45 +08:00
// auto emitter = static_cast<PUEmitter*>(_emitter);
// emitter->notifyResume();
// }
2015-02-11 18:14:22 +08:00
2021-12-25 10:04:45 +08:00
for (auto& it : _emitters)
{
2015-03-05 13:24:06 +08:00
auto emitter = static_cast<PUEmitter*>(it);
2015-02-11 18:14:22 +08:00
emitter->notifyResume();
}
2021-12-25 10:04:45 +08:00
for (auto& it : _affectors)
{
2015-03-05 13:24:06 +08:00
auto affector = static_cast<PUAffector*>(it);
2015-02-11 18:14:22 +08:00
affector->notifyResume();
}
_state = State::RUNNING;
}
for (auto iter : _children)
{
2021-12-25 10:04:45 +08:00
PUParticleSystem3D* system = dynamic_cast<PUParticleSystem3D*>(iter);
2015-02-11 18:14:22 +08:00
if (system)
2015-02-12 16:09:08 +08:00
system->resumeParticleSystem();
2015-02-11 18:14:22 +08:00
}
}
void PUParticleSystem3D::update(float delta)
{
2021-12-25 10:04:45 +08:00
if (!_isEnabled || _isMarkedForEmission)
return;
if (_state != State::RUNNING)
{
if (_state == State::PAUSE)
2015-02-11 18:14:22 +08:00
return;
2021-12-25 10:04:45 +08:00
else if (_state == State::STOP && getAliveParticleCount() <= 0)
{
2015-03-02 17:47:26 +08:00
forceStopParticleSystem();
return;
2015-02-11 18:14:22 +08:00
}
}
2015-03-02 13:07:32 +08:00
forceUpdate(delta);
}
2021-12-25 10:04:45 +08:00
void PUParticleSystem3D::forceUpdate(float delta)
2015-03-02 13:07:32 +08:00
{
if (!_emitters.empty())
2015-03-02 13:07:32 +08:00
calulateRotationOffset();
prepared();
2021-12-25 10:04:45 +08:00
Vec3 currentPos = getDerivedPosition();
_latestPositionDiff = currentPos - _latestPosition;
2021-12-25 10:04:45 +08:00
_latestPosition = currentPos;
_latestOrientation = getDerivedOrientation();
2021-12-25 10:04:45 +08:00
if (!_emitters.empty())
{
2015-03-02 13:07:32 +08:00
emitParticles(delta);
preUpdator(delta);
updator(delta);
postUpdator(delta);
}
2015-02-11 18:14:22 +08:00
_timeElapsedSinceStart += delta;
}
float PUParticleSystem3D::getParticleSystemScaleVelocity() const
{
return _particleSystemScaleVelocity;
}
2021-12-25 10:04:45 +08:00
void PUParticleSystem3D::rotationOffset(Vec3& pos)
2015-02-11 18:14:22 +08:00
{
Mat4 rotMat;
Mat4::createRotation(_rotationOffset, &rotMat);
pos = _rotationCentre + rotMat * (pos - _rotationCentre);
}
void PUParticleSystem3D::prepared()
{
2021-12-25 10:04:45 +08:00
if (!_prepared)
{
// if (_emitter && _emitter->isEnabled())
2015-02-11 18:14:22 +08:00
//{
2021-12-25 10:04:45 +08:00
// auto emitter = static_cast<PUEmitter*>(_emitter);
// emitter->prepare();
// }
2015-02-11 18:14:22 +08:00
2015-03-02 13:07:32 +08:00
if (_render)
2021-12-25 10:04:45 +08:00
static_cast<PURender*>(_render)->prepare();
2015-03-02 13:07:32 +08:00
2021-12-25 10:04:45 +08:00
for (auto it : _behaviourTemplates)
{
2015-03-02 13:07:32 +08:00
it->prepare();
}
2021-12-25 10:04:45 +08:00
for (auto it : _emitters)
{
// if (it->isEnabled())
(static_cast<PUEmitter*>(it))->prepare();
2015-02-11 18:14:22 +08:00
}
2021-12-25 10:04:45 +08:00
for (auto it : _affectors)
{
// if (it->isEnabled())
(static_cast<PUAffector*>(it))->prepare();
2015-02-11 18:14:22 +08:00
}
2021-12-25 10:04:45 +08:00
if (!_poolPrepared)
{
for (auto it : _emitters)
{
// if (it->isEnabled())
PUEmitter* emitter = static_cast<PUEmitter*>(it);
if (emitter->getEmitsType() == PUParticle3D::PT_EMITTER)
{
PUEmitter* emitted = static_cast<PUEmitter*>(emitter->getEmitsEntityPtr());
for (unsigned int i = 0; i < _emittedEmitterQuota; ++i)
{
auto p = new PUParticle3D();
p->particleType = PUParticle3D::PT_EMITTER;
2015-03-02 13:07:32 +08:00
p->particleEntityPtr = emitted->clone();
p->particleEntityPtr->retain();
p->copyBehaviours(_behaviourTemplates);
_emittedEmitterParticlePool[emitted->getName()].addData(p);
}
}
2021-12-25 10:04:45 +08:00
else if (emitter->getEmitsType() == PUParticle3D::PT_TECHNIQUE)
{
PUParticleSystem3D* emitted = static_cast<PUParticleSystem3D*>(emitter->getEmitsEntityPtr());
for (unsigned int i = 0; i < _emittedSystemQuota; ++i)
{
PUParticleSystem3D* clonePS = emitted->clone();
auto p = new PUParticle3D();
p->particleType = PUParticle3D::PT_TECHNIQUE;
p->particleEntityPtr = clonePS;
2015-03-02 13:07:32 +08:00
p->particleEntityPtr->retain();
p->copyBehaviours(_behaviourTemplates);
_emittedSystemParticlePool[emitted->getName()].addData(p);
clonePS->prepared();
}
2021-12-25 10:04:45 +08:00
// emitted->stopParticle();
2015-03-02 13:07:32 +08:00
}
}
2021-12-25 10:04:45 +08:00
for (unsigned int i = 0; i < _particleQuota; ++i)
{
2021-12-08 00:11:53 +08:00
auto p = new PUParticle3D();
2015-03-02 13:07:32 +08:00
p->copyBehaviours(_behaviourTemplates);
_particlePool.addData(p);
}
_poolPrepared = true;
2015-02-11 18:14:22 +08:00
}
2015-03-02 13:07:32 +08:00
2021-12-25 10:04:45 +08:00
_prepared = true;
2015-03-02 13:07:32 +08:00
_timeElapsedSinceStart = 0.0f;
2021-12-25 10:04:45 +08:00
_latestPosition = getDerivedPosition(); // V1.3.1
if (_parentParticleSystem)
{
_particleSystemScaleVelocity = _parentParticleSystem->getParticleSystemScaleVelocity();
2015-03-04 13:19:42 +08:00
}
2015-02-11 18:14:22 +08:00
}
2015-03-02 13:07:32 +08:00
if (!_emitters.empty())
notifyRescaled(getDerivedScale());
2015-02-11 18:14:22 +08:00
}
void PUParticleSystem3D::unPrepared()
{
2021-12-25 10:04:45 +08:00
// if (_emitter && _emitter->isEnabled())
2015-02-11 18:14:22 +08:00
//{
2021-12-25 10:04:45 +08:00
// auto emitter = static_cast<PUEmitter*>(_emitter);
// emitter->unPrepare();
// }
if (_prepared)
{
2015-03-02 17:47:26 +08:00
if (_render)
2021-12-25 10:04:45 +08:00
static_cast<PURender*>(_render)->unPrepare();
for (auto it : _behaviourTemplates)
{
2015-03-02 17:47:26 +08:00
it->unPrepare();
}
2021-12-25 10:04:45 +08:00
for (auto it : _emitters)
{
2015-03-02 17:47:26 +08:00
if (it->isEnabled())
2015-03-05 13:24:06 +08:00
(static_cast<PUEmitter*>(it))->unPrepare();
2015-03-02 17:47:26 +08:00
}
2021-12-25 10:04:45 +08:00
for (auto it : _affectors)
{
2015-03-02 17:47:26 +08:00
if (it->isEnabled())
2015-03-05 13:24:06 +08:00
(static_cast<PUAffector*>(it))->unPrepare();
2015-03-02 17:47:26 +08:00
}
2021-12-25 10:04:45 +08:00
2015-03-02 17:47:26 +08:00
_particlePool.lockAllDatas();
2021-12-25 10:04:45 +08:00
for (auto& iter : _emittedEmitterParticlePool)
{
PUParticle3D* particle = static_cast<PUParticle3D*>(iter.second.getFirst());
2015-03-02 17:47:26 +08:00
while (particle)
{
2015-03-05 13:24:06 +08:00
static_cast<PUEmitter*>(particle->particleEntityPtr)->unPrepare();
2021-12-25 10:04:45 +08:00
particle = static_cast<PUParticle3D*>(iter.second.getNext());
2015-03-02 17:47:26 +08:00
}
iter.second.lockAllDatas();
2015-03-02 13:07:32 +08:00
}
2021-12-25 10:04:45 +08:00
for (auto& iter : _emittedSystemParticlePool)
{
PUParticle3D* particle = static_cast<PUParticle3D*>(iter.second.getFirst());
2015-03-02 17:47:26 +08:00
while (particle)
{
static_cast<PUParticleSystem3D*>(particle->particleEntityPtr)->unPrepared();
2021-12-25 10:04:45 +08:00
particle = static_cast<PUParticle3D*>(iter.second.getNext());
2015-03-02 17:47:26 +08:00
}
iter.second.lockAllDatas();
2015-03-02 13:07:32 +08:00
}
2015-03-02 17:47:26 +08:00
_prepared = false;
2015-03-02 13:07:32 +08:00
}
2015-02-11 18:14:22 +08:00
}
2021-12-25 10:04:45 +08:00
void PUParticleSystem3D::preUpdator(float elapsedTime)
2015-02-11 18:14:22 +08:00
{
2021-12-25 10:04:45 +08:00
// if (_emitter && _emitter->isEnabled())
2015-02-11 18:14:22 +08:00
//{
2021-12-25 10:04:45 +08:00
// auto emitter = static_cast<PUEmitter*>(_emitter);
// emitter->preUpdateEmitter(elapsedTime);
// }
// bool hasNoEmitted = true;
for (auto it : _emitters)
{
if (!it->isEmitterDone())
{
2015-03-05 13:24:06 +08:00
(static_cast<PUEmitter*>(it))->preUpdateEmitter(elapsedTime);
2021-12-25 10:04:45 +08:00
// hasNoEmitted = false;
2015-02-11 18:14:22 +08:00
}
}
2021-12-25 10:04:45 +08:00
// if (hasNoEmitted){
// if (_particlePool.getActiveParticleList().empty())
// stopParticle();
// }
2015-02-11 18:14:22 +08:00
2021-12-25 10:04:45 +08:00
for (auto it : _affectors)
{
if (it->isEnabled())
{
2015-03-05 13:24:06 +08:00
(static_cast<PUAffector*>(it))->preUpdateAffector(elapsedTime);
2015-02-11 18:14:22 +08:00
}
}
2021-12-25 10:04:45 +08:00
for (auto it : _observers)
{
if (it->isEnabled())
{
2015-03-02 13:07:32 +08:00
it->preUpdateObserver(elapsedTime);
2015-02-11 18:14:22 +08:00
}
2015-03-02 13:07:32 +08:00
}
2015-02-11 18:14:22 +08:00
2021-12-25 10:04:45 +08:00
for (auto& iter : _emittedEmitterParticlePool)
{
PUParticle3D* particle = static_cast<PUParticle3D*>(iter.second.getFirst());
2015-03-02 13:07:32 +08:00
while (particle)
2015-02-11 18:14:22 +08:00
{
2015-03-05 13:24:06 +08:00
static_cast<PUEmitter*>(particle->particleEntityPtr)->preUpdateEmitter(elapsedTime);
2021-12-25 10:04:45 +08:00
particle = static_cast<PUParticle3D*>(iter.second.getNext());
2015-02-11 18:14:22 +08:00
}
2015-03-02 13:07:32 +08:00
}
2021-12-25 10:04:45 +08:00
for (auto& iter : _emittedSystemParticlePool)
{
PUParticle3D* particle = static_cast<PUParticle3D*>(iter.second.getFirst());
2015-03-02 13:07:32 +08:00
while (particle)
2015-02-11 18:14:22 +08:00
{
2015-03-02 13:07:32 +08:00
static_cast<PUParticleSystem3D*>(particle->particleEntityPtr)->preUpdator(elapsedTime);
2021-12-25 10:04:45 +08:00
particle = static_cast<PUParticle3D*>(iter.second.getNext());
2015-02-11 18:14:22 +08:00
}
2015-03-02 13:07:32 +08:00
}
}
2015-02-11 18:14:22 +08:00
2021-12-25 10:04:45 +08:00
void PUParticleSystem3D::updator(float elapsedTime)
2015-03-02 13:07:32 +08:00
{
bool firstActiveParticle = true;
2021-12-25 10:04:45 +08:00
bool firstParticle = true;
2015-03-02 13:07:32 +08:00
processParticle(_particlePool, firstActiveParticle, firstParticle, elapsedTime);
2015-02-11 18:14:22 +08:00
2021-12-25 10:04:45 +08:00
for (auto& iter : _emittedEmitterParticlePool)
{
2015-03-02 13:07:32 +08:00
processParticle(iter.second, firstActiveParticle, firstParticle, elapsedTime);
}
2021-12-25 10:04:45 +08:00
for (auto& iter : _emittedSystemParticlePool)
{
2015-03-02 13:07:32 +08:00
processParticle(iter.second, firstActiveParticle, firstParticle, elapsedTime);
2015-02-11 18:14:22 +08:00
}
}
2021-12-25 10:04:45 +08:00
void PUParticleSystem3D::postUpdator(float elapsedTime)
2015-02-11 18:14:22 +08:00
{
2021-12-25 10:04:45 +08:00
// if (_emitter && _emitter->isEnabled())
2015-02-11 18:14:22 +08:00
//{
2021-12-25 10:04:45 +08:00
// auto emitter = static_cast<PUEmitter*>(_emitter);
// emitter->postUpdateEmitter(elapsedTime);
// }
2015-02-11 18:14:22 +08:00
2021-12-25 10:04:45 +08:00
for (auto it : _emitters)
{
if (it->isEnabled())
{
2015-03-05 13:24:06 +08:00
(static_cast<PUEmitter*>(it))->postUpdateEmitter(elapsedTime);
2015-02-11 18:14:22 +08:00
}
}
2021-12-25 10:04:45 +08:00
for (auto it : _affectors)
{
2015-02-11 18:14:22 +08:00
if (it->isEnabled())
{
2015-03-05 13:24:06 +08:00
auto affector = static_cast<PUAffector*>(it);
2015-02-11 18:14:22 +08:00
affector->postUpdateAffector(elapsedTime);
}
}
2015-03-02 13:07:32 +08:00
2021-12-25 10:04:45 +08:00
for (auto it : _observers)
{
if (it->isEnabled())
{
2015-03-02 13:07:32 +08:00
it->postUpdateObserver(elapsedTime);
}
}
2021-12-25 10:04:45 +08:00
for (auto& iter : _emittedEmitterParticlePool)
{
PUParticle3D* particle = static_cast<PUParticle3D*>(iter.second.getFirst());
2015-03-02 13:07:32 +08:00
while (particle)
{
2015-03-05 13:24:06 +08:00
static_cast<PUEmitter*>(particle->particleEntityPtr)->postUpdateEmitter(elapsedTime);
2021-12-25 10:04:45 +08:00
particle = static_cast<PUParticle3D*>(iter.second.getNext());
2015-03-02 13:07:32 +08:00
}
}
2021-12-25 10:04:45 +08:00
for (auto& iter : _emittedSystemParticlePool)
{
PUParticle3D* particle = static_cast<PUParticle3D*>(iter.second.getFirst());
2015-03-02 13:07:32 +08:00
while (particle)
{
static_cast<PUParticleSystem3D*>(particle->particleEntityPtr)->postUpdator(elapsedTime);
2021-12-25 10:04:45 +08:00
particle = static_cast<PUParticle3D*>(iter.second.getNext());
2015-03-02 13:07:32 +08:00
}
}
2015-02-11 18:14:22 +08:00
}
2021-12-25 10:04:45 +08:00
void PUParticleSystem3D::emitParticles(float elapsedTime)
2015-02-11 18:14:22 +08:00
{
2021-12-25 10:04:45 +08:00
// Vec3 scale = getDerivedScale();
for (auto iter : _emitters)
{
// if (!_emitter) return;
// auto emitter = static_cast<PUEmitter*>(_emitter);
2015-02-11 18:14:22 +08:00
auto emitter = iter;
2021-12-25 10:04:45 +08:00
// emitter->notifyRescaled(scale);
if (!iter->isMarkedForEmission())
{
2015-03-02 13:07:32 +08:00
unsigned short requested = emitter->calculateRequestedParticles(elapsedTime);
executeEmitParticles(emitter, requested, elapsedTime);
2015-02-11 18:14:22 +08:00
}
}
}
float PUParticleSystem3D::getDefaultWidth() const
2015-02-11 18:14:22 +08:00
{
return _defaultWidth;
}
2021-12-25 10:04:45 +08:00
void PUParticleSystem3D::setDefaultWidth(const float width)
2015-02-11 18:14:22 +08:00
{
_defaultWidth = width;
}
float PUParticleSystem3D::getDefaultHeight() const
2015-02-11 18:14:22 +08:00
{
return _defaultHeight;
}
2021-12-25 10:04:45 +08:00
void PUParticleSystem3D::setDefaultHeight(const float height)
2015-02-11 18:14:22 +08:00
{
_defaultHeight = height;
}
float PUParticleSystem3D::getDefaultDepth() const
2015-02-11 18:14:22 +08:00
{
return _defaultDepth;
}
2021-12-25 10:04:45 +08:00
void PUParticleSystem3D::setDefaultDepth(const float depth)
2015-02-11 18:14:22 +08:00
{
_defaultDepth = depth;
}
2021-12-25 10:04:45 +08:00
bool PUParticleSystem3D::isExpired(PUParticle3D* particle, float timeElapsed)
2015-02-11 18:14:22 +08:00
{
bool expired = particle->timeToLive < timeElapsed;
if (expired)
{
// Set the flag to indicate that the particle has been expired
particle->addEventFlags(PUParticle3D::PEF_EXPIRED);
}
return expired;
}
cocos2d::Vec3 PUParticleSystem3D::getDerivedPosition()
{
2021-12-25 10:04:45 +08:00
// if (_parentParticleSystem && _parentParticleSystem->isKeepLocal()) return Vec3::ZERO;
// if (_keepLocal) return Vec3::ZERO;
if (_isMarkedForEmission)
{
2015-03-02 13:07:32 +08:00
return Vec3(_position.x, _position.y, _positionZ);
2021-12-25 10:04:45 +08:00
}
else
{
2015-03-02 13:07:32 +08:00
Mat4 mat = getNodeToWorldTransform();
return Vec3(mat.m[12], mat.m[13], mat.m[14]);
}
2015-02-11 18:14:22 +08:00
}
cocos2d::Quaternion PUParticleSystem3D::getDerivedOrientation()
{
2021-12-25 10:04:45 +08:00
// if (_parentParticleSystem && _parentParticleSystem->isKeepLocal()) return Quaternion();
// if (_keepLocal) return Quaternion();
if (_isMarkedForEmission)
{
2015-03-02 13:07:32 +08:00
return getRotationQuat();
2021-12-25 10:04:45 +08:00
}
else
{
2015-03-02 13:07:32 +08:00
Quaternion q;
Mat4 mat = getNodeToWorldTransform();
mat.decompose(nullptr, &q, nullptr);
return q;
}
2015-02-11 18:14:22 +08:00
}
cocos2d::Vec3 PUParticleSystem3D::getDerivedScale()
{
2021-12-25 10:04:45 +08:00
// if (_parentParticleSystem && _parentParticleSystem->isKeepLocal()) return Vec3::ONE;
// if (_keepLocal) return Vec3::ONE;
if (_isMarkedForEmission)
{
2015-03-02 13:07:32 +08:00
return Vec3(_scaleX, _scaleY, _scaleZ);
2021-12-25 10:04:45 +08:00
}
else
{
2015-03-02 13:07:32 +08:00
Vec3 s;
Mat4 mat = getNodeToWorldTransform();
mat.decompose(&s, nullptr, nullptr);
return s;
}
2015-02-11 18:14:22 +08:00
}
float PUParticleSystem3D::getMaxVelocity() const
{
return _maxVelocity;
}
2021-12-25 10:04:45 +08:00
void PUParticleSystem3D::setMaxVelocity(float maxVelocity)
2015-02-11 18:14:22 +08:00
{
2021-12-25 10:04:45 +08:00
_maxVelocity = maxVelocity;
2015-02-11 18:14:22 +08:00
_maxVelocitySet = true;
}
2021-12-25 10:04:45 +08:00
bool PUParticleSystem3D::initSystem(const std::string& filePath)
2015-02-11 18:14:22 +08:00
{
bool isFirstCompile = true;
2021-12-25 10:04:45 +08:00
auto list = PUScriptCompiler::Instance()->compile(filePath, isFirstCompile);
if (list == nullptr || list->empty())
return false;
2015-03-05 13:24:06 +08:00
PUTranslateManager::Instance()->translateParticleSystem(this, list);
2021-12-25 10:04:45 +08:00
// std::string data = FileUtils::getInstance()->getStringFromFile(filePath);
2015-02-11 18:14:22 +08:00
return true;
}
2021-12-25 10:04:45 +08:00
void PUParticleSystem3D::addEmitter(PUEmitter* emitter)
2015-02-11 18:14:22 +08:00
{
2021-12-25 10:04:45 +08:00
if (emitter && std::find(_emitters.begin(), _emitters.end(), emitter) == _emitters.end())
{
2015-02-11 18:14:22 +08:00
emitter->_particleSystem = this;
emitter->retain();
_emitters.push_back(emitter);
}
}
2021-12-25 10:04:45 +08:00
PUAffector* PUParticleSystem3D::getAffector(const std::string& name)
2015-03-02 13:07:32 +08:00
{
2021-12-25 10:04:45 +08:00
for (auto iter : _affectors)
{
auto pa = static_cast<PUAffector*>(iter);
2015-03-02 13:07:32 +08:00
if (pa->getName() == name)
return pa;
}
return nullptr;
}
2021-12-25 10:04:45 +08:00
PUEmitter* PUParticleSystem3D::getEmitter(const std::string& name)
2015-03-02 13:07:32 +08:00
{
2021-12-25 10:04:45 +08:00
for (auto iter : _emitters)
{
auto pe = static_cast<PUEmitter*>(iter);
2015-03-02 13:07:32 +08:00
if (pe->getName() == name)
return pe;
}
return nullptr;
}
2021-12-25 10:04:45 +08:00
void PUParticleSystem3D::addListener(PUListener* listener)
2015-03-02 13:07:32 +08:00
{
auto iter = std::find(_listeners.begin(), _listeners.end(), listener);
2021-12-25 10:04:45 +08:00
if (iter == _listeners.end())
{
2015-03-02 13:07:32 +08:00
_listeners.push_back(listener);
}
}
2021-12-25 10:04:45 +08:00
void PUParticleSystem3D::removeListener(PUListener* listener)
2015-03-02 13:07:32 +08:00
{
auto iter = std::find(_listeners.begin(), _listeners.end(), listener);
2021-12-25 10:04:45 +08:00
if (iter != _listeners.end())
{
2015-03-02 13:07:32 +08:00
_listeners.erase(iter);
}
}
2021-12-25 10:04:45 +08:00
void PUParticleSystem3D::forceEmission(PUEmitter* emitter, unsigned requested)
2015-03-02 13:07:32 +08:00
{
// Fast rejection: Don't emit if the technique is not enabled
if (!_isEnabled)
return;
executeEmitParticles(emitter, requested, 0);
}
2021-12-25 10:04:45 +08:00
void PUParticleSystem3D::executeEmitParticles(PUEmitter* emitter, unsigned requested, float elapsedTime)
2015-03-02 13:07:32 +08:00
{
2021-12-25 10:04:45 +08:00
if (_state == State::STOP)
return;
if (emitter->getEmitsType() == PUParticle3D::PT_VISUAL)
{
2015-03-02 13:07:32 +08:00
emitParticles(_particlePool, emitter, requested, elapsedTime);
2021-12-25 10:04:45 +08:00
}
else if (emitter->getEmitsType() == PUParticle3D::PT_EMITTER)
{
2015-03-02 13:07:32 +08:00
emitParticles(_emittedEmitterParticlePool[emitter->getEmitsName()], emitter, requested, elapsedTime);
2021-12-25 10:04:45 +08:00
}
else if (emitter->getEmitsType() == PUParticle3D::PT_TECHNIQUE)
{
2015-03-02 13:07:32 +08:00
emitParticles(_emittedSystemParticlePool[emitter->getEmitsName()], emitter, requested, elapsedTime);
}
}
2021-12-25 10:04:45 +08:00
void PUParticleSystem3D::emitParticles(ParticlePool& pool, PUEmitter* emitter, unsigned requested, float elapsedTime)
2015-03-02 13:07:32 +08:00
{
2015-03-04 15:18:03 +08:00
Vec3 scale = getDerivedScale();
2015-03-02 13:07:32 +08:00
Mat4 rotMat;
Mat4::createRotation(getDerivedOrientation(), &rotMat);
float timePoint = 0.0f;
2021-12-25 10:04:45 +08:00
float timeInc = elapsedTime / requested;
2015-03-02 13:07:32 +08:00
for (unsigned short i = 0; i < requested; ++i)
{
2021-12-25 10:04:45 +08:00
PUParticle3D* particle = static_cast<PUParticle3D*>(pool.createData());
if (!particle)
2015-03-02 13:07:32 +08:00
return;
particle->initForEmission();
emitter->initParticleForEmission(particle);
2015-03-04 13:19:42 +08:00
particle->direction = (rotMat * Vec3(particle->direction.x, particle->direction.y, particle->direction.z));
2021-12-25 10:04:45 +08:00
particle->originalDirection = (rotMat * Vec3(particle->originalDirection.x, particle->originalDirection.y,
particle->originalDirection.z));
2015-03-02 13:07:32 +08:00
2021-12-25 10:04:45 +08:00
for (auto& it : _affectors)
{
2015-03-02 13:07:32 +08:00
if (it->isEnabled())
{
2015-03-05 13:24:06 +08:00
(static_cast<PUAffector*>(it))->initParticleForEmission(particle);
2015-03-02 13:07:32 +08:00
}
}
initParticleForEmission(particle);
2021-12-25 10:04:45 +08:00
particle->position.add(particle->direction.x * scale.x * _particleSystemScaleVelocity * timePoint,
particle->direction.y * scale.y * _particleSystemScaleVelocity * timePoint,
particle->direction.z * scale.z * _particleSystemScaleVelocity * timePoint);
2015-03-02 13:07:32 +08:00
// Increment time fragment
timePoint += timeInc;
}
}
2021-12-25 10:04:45 +08:00
void PUParticleSystem3D::addObserver(PUObserver* observer)
2015-03-02 13:07:32 +08:00
{
auto iter = std::find(_observers.begin(), _observers.end(), observer);
2021-12-25 10:04:45 +08:00
if (iter == _observers.end())
{
2015-03-02 13:07:32 +08:00
observer->retain();
observer->_particleSystem = this;
_observers.push_back(observer);
}
}
2021-12-25 10:04:45 +08:00
PUObserver* PUParticleSystem3D::getObserver(const std::string& name)
2015-03-02 13:07:32 +08:00
{
2021-12-25 10:04:45 +08:00
for (auto iter : _observers)
{
auto po = static_cast<PUObserver*>(iter);
2015-03-02 13:07:32 +08:00
if (po->getName() == name)
return po;
}
return nullptr;
}
2021-12-25 10:04:45 +08:00
void PUParticleSystem3D::notifyRescaled(const Vec3& scl)
2015-03-02 13:07:32 +08:00
{
if (_render)
_render->notifyRescaled(scl);
2021-12-25 10:04:45 +08:00
for (auto it : _emitters)
{
2015-03-05 13:24:06 +08:00
(static_cast<PUEmitter*>(it))->notifyRescaled(scl);
2015-03-02 13:07:32 +08:00
}
2021-12-25 10:04:45 +08:00
for (auto it : _affectors)
{
2015-03-05 13:24:06 +08:00
(static_cast<PUAffector*>(it))->notifyRescaled(scl);
2015-03-02 13:07:32 +08:00
}
2021-12-25 10:04:45 +08:00
for (auto it : _observers)
{
2015-03-02 13:07:32 +08:00
it->notifyRescaled(scl);
}
2021-12-25 10:04:45 +08:00
for (auto& iter : _emittedEmitterParticlePool)
{
PUParticle3D* particle = static_cast<PUParticle3D*>(iter.second.getFirst());
2015-03-02 13:07:32 +08:00
while (particle)
{
2015-03-05 13:24:06 +08:00
static_cast<PUEmitter*>(particle->particleEntityPtr)->notifyRescaled(scl);
2021-12-25 10:04:45 +08:00
particle = static_cast<PUParticle3D*>(iter.second.getNext());
2015-03-02 13:07:32 +08:00
}
}
2021-12-25 10:04:45 +08:00
for (auto& iter : _emittedSystemParticlePool)
{
PUParticle3D* particle = static_cast<PUParticle3D*>(iter.second.getFirst());
2015-03-02 13:07:32 +08:00
while (particle)
{
static_cast<PUParticleSystem3D*>(particle->particleEntityPtr)->notifyRescaled(scl);
2021-12-25 10:04:45 +08:00
particle = static_cast<PUParticle3D*>(iter.second.getNext());
2015-03-02 13:07:32 +08:00
}
}
}
2021-12-25 10:04:45 +08:00
void PUParticleSystem3D::initParticleForExpiration(PUParticle3D* particle, float timeElapsed)
2015-03-02 13:07:32 +08:00
{
2021-12-25 10:04:45 +08:00
if (particle->particleType == PUParticle3D::PT_EMITTER)
{
PUEmitter* emitter = static_cast<PUEmitter*>(particle->particleEntityPtr);
2015-03-02 13:07:32 +08:00
emitter->unPrepare();
2021-12-25 10:04:45 +08:00
}
else if (particle->particleType == PUParticle3D::PT_TECHNIQUE)
{
PUParticleSystem3D* system = static_cast<PUParticleSystem3D*>(particle->particleEntityPtr);
2015-03-02 13:07:32 +08:00
system->unPrepared();
}
particle->initForExpiration(timeElapsed);
2021-12-25 10:04:45 +08:00
for (auto it : _listeners)
{
2015-03-02 13:07:32 +08:00
it->particleExpired(this, particle);
}
2021-12-25 10:04:45 +08:00
///** Externs are also called to perform expiration activities. If needed, affectors and emitters may be added, but
///at the moment
// there is no reason for (and we don't want to waste cpu resources).
2015-03-02 13:07:32 +08:00
//*/
2021-12-25 10:04:45 +08:00
// if (!mExterns.empty())
2015-03-02 13:07:32 +08:00
//{
// ExternIterator itExtern;
// ExternIterator itExternEnd = mExterns.end();
// for (itExtern = mExterns.begin(); itExtern != itExternEnd; ++itExtern)
// {
// (*itExtern)->_initParticleForExpiration(particle);
// }
//}
}
2021-12-25 10:04:45 +08:00
void PUParticleSystem3D::initParticleForEmission(PUParticle3D* particle)
2015-03-02 13:07:32 +08:00
{
2021-12-25 10:04:45 +08:00
for (auto it : _listeners)
{
2015-03-02 13:07:32 +08:00
it->particleEmitted(this, particle);
}
}
2021-12-25 10:04:45 +08:00
void PUParticleSystem3D::addBehaviourTemplate(PUBehaviour* behaviour)
2015-03-02 13:07:32 +08:00
{
auto iter = std::find(_behaviourTemplates.begin(), _behaviourTemplates.end(), behaviour);
2021-12-25 10:04:45 +08:00
if (iter == _behaviourTemplates.end())
{
2015-03-02 13:07:32 +08:00
behaviour->retain();
behaviour->_particleSystem = this;
_behaviourTemplates.push_back(behaviour);
}
}
2021-12-25 10:04:45 +08:00
void PUParticleSystem3D::convertToUnixStylePath(std::string& path)
2015-02-11 18:14:22 +08:00
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
2021-12-25 10:04:45 +08:00
for (auto& iter : path)
{
if (iter == '\\')
iter = '/';
2015-02-11 18:14:22 +08:00
}
#else
CC_UNUSED_PARAM(path);
2015-02-11 18:14:22 +08:00
#endif
}
2015-03-02 13:07:32 +08:00
void PUParticleSystem3D::clearAllParticles()
{
_particlePool.lockAllDatas();
2021-12-25 10:04:45 +08:00
for (auto& iter : _emittedEmitterParticlePool)
{
2015-03-02 13:07:32 +08:00
iter.second.lockAllDatas();
}
2021-12-25 10:04:45 +08:00
for (auto& iter : _emittedSystemParticlePool)
{
2015-03-02 13:07:32 +08:00
iter.second.lockAllDatas();
}
}
2021-12-25 10:04:45 +08:00
void PUParticleSystem3D::copyAttributesTo(PUParticleSystem3D* system)
2015-03-02 13:07:32 +08:00
{
system->removeAllEmitter();
system->removeAllAffector();
system->removerAllObserver();
system->removeAllBehaviourTemplate();
system->removeAllListener();
system->_particlePool.removeAllDatas();
2021-12-25 10:04:45 +08:00
for (auto iter : system->_emittedEmitterParticlePool)
{
2015-03-02 13:07:32 +08:00
iter.second.removeAllDatas();
}
2021-12-25 10:04:45 +08:00
for (auto iter : system->_emittedSystemParticlePool)
{
2015-03-02 13:07:32 +08:00
iter.second.removeAllDatas();
}
system->setName(_name);
system->_state = _state;
if (_render)
2021-12-25 10:04:45 +08:00
system->setRender(static_cast<PURender*>(_render)->clone());
2015-03-02 13:07:32 +08:00
system->_particleQuota = _particleQuota;
2021-12-25 10:04:45 +08:00
system->_blend = _blend;
system->_keepLocal = _keepLocal;
system->_isEnabled = _isEnabled;
2015-03-02 13:07:32 +08:00
2021-12-25 10:04:45 +08:00
for (auto iter : _affectors)
{
PUAffector* affector = static_cast<PUAffector*>(iter);
PUAffector* copy = PUAffectorManager::Instance()->createAffector(affector->getAffectorType());
2015-03-02 13:07:32 +08:00
affector->copyAttributesTo(copy);
system->addAffector(copy);
}
2021-12-25 10:04:45 +08:00
for (auto iter : _emitters)
{
PUEmitter* emitter = static_cast<PUEmitter*>(iter);
PUEmitter* copy = PUEmitterManager::Instance()->createEmitter(emitter->getEmitterType());
2015-03-02 13:07:32 +08:00
emitter->copyAttributesTo(copy);
system->addEmitter(copy);
}
2021-12-25 10:04:45 +08:00
for (auto iter : _observers)
{
PUObserver* observer = static_cast<PUObserver*>(iter);
PUObserver* copy = PUObserverManager::Instance()->createObserver(observer->getObserverType());
2015-03-02 13:07:32 +08:00
observer->copyAttributesTo(copy);
system->addObserver(copy);
}
2021-12-25 10:04:45 +08:00
for (auto iter : _behaviourTemplates)
{
PUBehaviour* behaviour = static_cast<PUBehaviour*>(iter);
PUBehaviour* copy = behaviour->clone();
2015-03-02 13:07:32 +08:00
system->addBehaviourTemplate(copy);
}
2021-12-25 10:04:45 +08:00
system->_emittedEmitterQuota = _emittedEmitterQuota;
system->_emittedSystemQuota = _emittedSystemQuota;
system->_prepared = false;
2015-03-02 13:07:32 +08:00
system->_particleSystemScaleVelocity = _particleSystemScaleVelocity;
2021-12-25 10:04:45 +08:00
system->_defaultWidth = _defaultWidth;
system->_defaultHeight = _defaultHeight;
system->_defaultDepth = _defaultDepth;
system->_maxVelocity = _maxVelocity;
system->_maxVelocitySet = _maxVelocitySet;
system->_matName = _matName;
system->_isMarkedForEmission = _isMarkedForEmission;
system->_parentParticleSystem = _parentParticleSystem;
2015-03-02 13:07:32 +08:00
}
PUParticleSystem3D* PUParticleSystem3D::clone()
{
auto ps = PUParticleSystem3D::create();
copyAttributesTo(ps);
2021-12-25 10:04:45 +08:00
for (auto& iter : _children)
{
PUParticleSystem3D* child = dynamic_cast<PUParticleSystem3D*>(iter);
if (child)
ps->addChild(child->clone());
}
2015-03-02 13:07:32 +08:00
return ps;
}
void PUParticleSystem3D::removeAllEmitter()
{
2021-12-25 10:04:45 +08:00
for (auto iter : _emitters)
{
2015-03-02 13:07:32 +08:00
iter->release();
}
_emitters.clear();
}
void PUParticleSystem3D::removerAllObserver()
{
2021-12-25 10:04:45 +08:00
for (auto iter : _observers)
{
2015-03-02 13:07:32 +08:00
iter->release();
}
_observers.clear();
}
void PUParticleSystem3D::removeAllBehaviourTemplate()
{
2021-12-25 10:04:45 +08:00
for (auto iter : _behaviourTemplates)
{
2015-03-02 13:07:32 +08:00
iter->release();
}
_behaviourTemplates.clear();
}
void PUParticleSystem3D::removeAllListener()
{
_listeners.clear();
}
2021-12-25 10:04:45 +08:00
void PUParticleSystem3D::draw(Renderer* renderer, const Mat4& transform, uint32_t flags)
2015-03-02 13:07:32 +08:00
{
2021-12-25 10:04:45 +08:00
if (!_isEnabled)
return;
if (getAliveParticleCount() <= 0)
return;
2015-03-02 13:07:32 +08:00
if (_render)
_render->render(renderer, transform, this);
if (!_emittedSystemParticlePool.empty())
{
2021-12-25 10:04:45 +08:00
for (auto& iter : _emittedSystemParticlePool)
{
2021-12-25 10:04:45 +08:00
PUParticle3D* particle = static_cast<PUParticle3D*>(iter.second.getFirst());
2015-03-02 13:07:32 +08:00
while (particle)
{
2021-12-25 10:04:45 +08:00
static_cast<PUParticleSystem3D*>(particle->particleEntityPtr)->draw(renderer, transform, flags);
particle = static_cast<PUParticle3D*>(iter.second.getNext());
2015-03-02 13:07:32 +08:00
}
}
}
}
2021-12-25 10:04:45 +08:00
void PUParticleSystem3D::processParticle(ParticlePool& pool,
bool& firstActiveParticle,
bool& firstParticle,
float elapsedTime)
2015-03-02 13:07:32 +08:00
{
2021-12-25 10:04:45 +08:00
Vec3 scale = getDerivedScale();
PUParticle3D* particle = static_cast<PUParticle3D*>(pool.getFirst());
// Mat4 ltow = getNodeToWorldTransform();
// Vec3 scl;
// Quaternion rot;
// ltow.decompose(&scl, &rot, nullptr);
while (particle)
{
if (!isExpired(particle, elapsedTime))
{
2015-03-02 13:07:32 +08:00
particle->process(elapsedTime);
2021-12-25 10:04:45 +08:00
// if (_emitter && _emitter->isEnabled())
// _emitter->updateEmitter(particle, elapsedTime);
2015-03-02 13:07:32 +08:00
2021-12-25 10:04:45 +08:00
for (auto it : _emitters)
{
if (it->isEnabled() && !it->isMarkedForEmission())
{
2015-03-05 13:24:06 +08:00
(static_cast<PUEmitter*>(it))->updateEmitter(particle, elapsedTime);
2015-03-02 13:07:32 +08:00
}
}
2021-12-25 10:04:45 +08:00
for (auto& it : _affectors)
{
if (it->isEnabled())
{
2015-03-05 13:24:06 +08:00
(static_cast<PUAffector*>(it))->process(particle, elapsedTime, firstActiveParticle);
2015-03-02 13:07:32 +08:00
}
}
if (_render)
2021-12-25 10:04:45 +08:00
static_cast<PURender*>(_render)->updateRender(particle, elapsedTime, firstActiveParticle);
2015-03-02 13:07:32 +08:00
2021-12-25 10:04:45 +08:00
if (_isEnabled && particle->particleType != PUParticle3D::PT_VISUAL)
{
if (particle->particleType == PUParticle3D::PT_EMITTER)
{
auto emitter = static_cast<PUEmitter*>(particle->particleEntityPtr);
2015-03-02 13:07:32 +08:00
emitter->setLocalPosition(particle->position);
executeEmitParticles(emitter, emitter->calculateRequestedParticles(elapsedTime), elapsedTime);
2021-12-25 10:04:45 +08:00
}
else if (particle->particleType == PUParticle3D::PT_TECHNIQUE)
{
auto system = static_cast<PUParticleSystem3D*>(particle->particleEntityPtr);
2015-03-02 13:07:32 +08:00
system->setPosition3D(particle->position);
system->setRotationQuat(particle->orientation);
2021-12-25 10:04:45 +08:00
// system->setScaleX(scl.x);system->setScaleY(scl.y);system->setScaleZ(scl.z);
2015-03-02 13:07:32 +08:00
system->forceUpdate(elapsedTime);
}
}
firstActiveParticle = false;
// Keep latest position
particle->latestPosition = particle->position;
2021-12-25 10:04:45 +08:00
// if (_maxVelocitySet && particle->calculateVelocity() > _maxVelocity)
2015-03-02 13:07:32 +08:00
//{
2021-12-25 10:04:45 +08:00
// particle->direction *= (_maxVelocity / particle->direction.length());
// }
2015-03-02 13:07:32 +08:00
//// Update the position with the direction.
2021-12-25 10:04:45 +08:00
// particle->position += (particle->direction * _particleSystemScaleVelocity * elapsedTime);
// particle->positionInWorld = particle->position;
// particle->orientationInWorld = particle->orientation;
// particle->widthInWorld = particle->width;
// particle->heightInWorld = particle->height;
// particle->depthInWorld = particle->depth;
// bool keepLocal = _keepLocal;
// PUParticleSystem3D *parent = dynamic_cast<PUParticleSystem3D *>(getParent());
// if (parent) keepLocal = keepLocal || parent->isKeepLocal();
// if (keepLocal){
// ltow.transformPoint(particle->positionInWorld, &particle->positionInWorld);
// Vec3 ori;
// ltow.transformVector(Vec3(particle->orientation.x, particle->orientation.y, particle->orientation.z),
// &ori); particle->orientationInWorld.x = ori.x; particle->orientationInWorld.y = ori.y;
// particle->orientationInWorld.z = ori.z; particle->widthInWorld = scl.x * particle->width;
// particle->heightInWorld = scl.y * particle->height;
// particle->depthInWorld = scl.z * particle->depth;
// }
processMotion(particle, elapsedTime, scale, firstActiveParticle);
2015-03-02 13:07:32 +08:00
}
2021-12-25 10:04:45 +08:00
else
{
2015-03-02 13:07:32 +08:00
initParticleForExpiration(particle, elapsedTime);
pool.lockLatestData();
}
2021-12-25 10:04:45 +08:00
for (auto it : _observers)
{
if (it->isEnabled())
{
2015-03-02 13:07:32 +08:00
it->updateObserver(particle, elapsedTime, firstParticle);
}
}
if (particle->hasEventFlags(PUParticle3D::PEF_EXPIRED))
{
particle->setEventFlags(0);
particle->addEventFlags(PUParticle3D::PEF_EXPIRED);
}
else
{
particle->setEventFlags(0);
}
particle->timeToLive -= elapsedTime;
firstParticle = false;
2021-12-25 10:04:45 +08:00
particle = static_cast<PUParticle3D*>(pool.getNext());
2015-03-02 13:07:32 +08:00
}
}
2021-12-25 10:04:45 +08:00
bool PUParticleSystem3D::makeParticleLocal(PUParticle3D* particle)
2015-03-02 13:07:32 +08:00
{
if (!particle)
return true;
if (!_keepLocal)
return false;
particle->position += _latestPositionDiff;
return true;
}
2021-12-25 10:04:45 +08:00
void PUParticleSystem3D::processMotion(PUParticle3D* particle,
float timeElapsed,
const Vec3& scl,
bool /*firstParticle*/)
2015-03-02 13:07:32 +08:00
{
if (particle->isFreezed())
2021-12-25 10:04:45 +08:00
return;
2015-03-02 13:07:32 +08:00
2021-12-25 10:04:45 +08:00
/** Because everything is calculated in worldspace we recalculate it back to the techniques' origin (the result is
still worldspace) just by adding a relative offset of the particle system, technique, emitter or visual particle.
2015-03-02 13:07:32 +08:00
Change in V1.4:
2021-12-25 10:04:45 +08:00
The additional PEF_EMITTED check fixes the problem with scenenodese moving forward at extremely fast speeds
The bug is due to how PU code processes keep_local, and the calculation shouldn't be applied to newly emitted
particles.
2015-03-02 13:07:32 +08:00
*/
if (!particle->hasEventFlags(PUParticle3D::PEF_EMITTED))
{
if (!particle->parentEmitter->makeParticleLocal(particle))
{
if (!makeParticleLocal(particle))
{
_parentParticleSystem->makeParticleLocal(particle);
}
}
}
/** Adjust position of the particle if the parent has rotated (only in case of particle system with 'keep local').
PU 1.4: Added check that technique may not be local, otherwise particles are rotated twice as fast.
*/
if (_parentParticleSystem->isKeepLocal() && !_keepLocal)
{
/** Ignore some rendersystems, because they are rotated by Ogre itself.
Entities for example are always rotated (even if not local), based on the node orientation. A Billboard not.
*/
2021-12-25 10:04:45 +08:00
if (_render && !static_cast<PURender*>(_render)->autoRotate)
2015-03-02 13:07:32 +08:00
{
_parentParticleSystem->rotationOffset(particle->position);
}
}
// Added for 1.3
if (particle->hasEventFlags(PUParticle3D::PEF_EMITTED))
return;
// Adjust the velocity to the allowed maximum.
if (_maxVelocitySet && particle->calculateVelocity() > _maxVelocity)
{
particle->direction *= (_maxVelocity / particle->direction.length());
}
// Update the position with the direction.
2021-12-25 10:04:45 +08:00
particle->position.add(particle->direction.x * scl.x * _particleSystemScaleVelocity * timeElapsed,
particle->direction.y * scl.y * _particleSystemScaleVelocity * timeElapsed,
particle->direction.z * scl.z * _particleSystemScaleVelocity * timeElapsed);
2015-03-02 13:07:32 +08:00
}
2021-12-25 10:04:45 +08:00
void PUParticleSystem3D::calulateRotationOffset(void)
2015-03-02 13:07:32 +08:00
{
if (_isMarkedForEmission)
{
// Use the uber particle system as centre of rotation and not the particle systems' own position.
_rotationCentre = _parentParticleSystem->getDerivedPosition();
}
else
{
// Use its own position
_rotationCentre = getDerivedPosition();
}
/** Use the derived orientation, which is the particle systems' own scenenode orientation,
or the orientation of the uber particle system, if this particle system is emitted itself.
*/
Quaternion latestOrientationInverse = _latestOrientation;
latestOrientationInverse.inverse();
_rotationOffset = getDerivedOrientation() * latestOrientationInverse;
}
int PUParticleSystem3D::getAliveParticleCount() const
2015-03-02 13:07:32 +08:00
{
int sz = 0;
sz += _particlePool.getActiveDataList().size();
2021-12-25 10:04:45 +08:00
if (!_emittedEmitterParticlePool.empty())
{
for (auto& iter : _emittedEmitterParticlePool)
{
2015-03-02 13:07:32 +08:00
sz += iter.second.getActiveDataList().size();
}
}
2021-12-25 10:04:45 +08:00
if (_emittedSystemParticlePool.empty())
2015-03-02 13:07:32 +08:00
return sz;
2021-12-25 10:04:45 +08:00
for (auto& iter : _emittedSystemParticlePool)
{
2015-03-02 13:07:32 +08:00
auto pool = iter.second;
sz += pool.getActiveDataList().size();
2021-12-25 10:04:45 +08:00
PUParticle3D* particle = static_cast<PUParticle3D*>(pool.getFirst());
2015-03-02 13:07:32 +08:00
while (particle)
{
sz += static_cast<PUParticleSystem3D*>(particle->particleEntityPtr)->getAliveParticleCount();
2021-12-25 10:04:45 +08:00
particle = static_cast<PUParticle3D*>(pool.getNext());
2015-03-02 13:07:32 +08:00
}
}
return sz;
}
2015-03-02 17:47:26 +08:00
void PUParticleSystem3D::forceStopParticleSystem()
{
if (_render)
_render->notifyStop();
2015-03-02 17:47:26 +08:00
2021-12-25 10:04:45 +08:00
for (auto& it : _observers)
{
it->notifyStop();
}
2015-03-02 17:47:26 +08:00
2021-12-25 10:04:45 +08:00
for (auto& it : _emitters)
{
auto emitter = static_cast<PUEmitter*>(it);
emitter->notifyStop();
}
2015-03-02 17:47:26 +08:00
2021-12-25 10:04:45 +08:00
for (auto& it : _affectors)
{
auto affector = static_cast<PUAffector*>(it);
affector->notifyStop();
2015-03-02 17:47:26 +08:00
}
unscheduleUpdate();
unPrepared();
2015-03-02 17:47:26 +08:00
}
2015-02-11 18:14:22 +08:00
NS_CC_END