2015-02-11 18:14:22 +08:00
|
|
|
/****************************************************************************
|
2018-01-29 16:25:32 +08:00
|
|
|
Copyright (c) 2015-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-10-01 16:24:52 +08:00
|
|
|
https://axmolengine.github.io/
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "CCParticleSystem3D.h"
|
|
|
|
#include "CCParticle3DEmitter.h"
|
|
|
|
#include "CCParticle3DAffector.h"
|
|
|
|
#include "CCParticle3DRender.h"
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BEGIN
|
2015-02-11 18:14:22 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
Particle3D::Particle3D() : color(Vec4::ONE), rt_uv(Vec2::ONE), width(1.0f), height(1.0f), depth(1.0f) {}
|
2015-02-11 18:14:22 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
Particle3D::~Particle3D() {}
|
2015-02-11 18:14:22 +08:00
|
|
|
|
|
|
|
ParticleSystem3D::ParticleSystem3D()
|
2021-12-25 10:04:45 +08:00
|
|
|
: _state(State::STOP)
|
|
|
|
, _emitter(nullptr)
|
|
|
|
, _render(nullptr)
|
|
|
|
, _aliveParticlesCnt(0)
|
|
|
|
, _particleQuota(0)
|
|
|
|
, _blend(BlendFunc::ALPHA_NON_PREMULTIPLIED)
|
|
|
|
, _keepLocal(false)
|
|
|
|
, _isEnabled(true)
|
|
|
|
{}
|
2015-02-11 18:14:22 +08:00
|
|
|
ParticleSystem3D::~ParticleSystem3D()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
// stopParticle();
|
2015-02-11 18:14:22 +08:00
|
|
|
removeAllAffector();
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_RELEASE(_emitter);
|
|
|
|
AX_SAFE_RELEASE(_render);
|
2015-02-11 18:14:22 +08:00
|
|
|
}
|
|
|
|
|
2015-02-12 16:09:08 +08:00
|
|
|
void ParticleSystem3D::startParticleSystem()
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
if (_state != State::RUNNING)
|
|
|
|
{
|
|
|
|
if (_render)
|
|
|
|
_render->notifyStart();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-02-11 18:14:22 +08:00
|
|
|
scheduleUpdate();
|
|
|
|
_state = State::RUNNING;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-12 16:09:08 +08:00
|
|
|
void ParticleSystem3D::stopParticleSystem()
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
if (_state != State::STOP)
|
|
|
|
{
|
|
|
|
if (_render)
|
|
|
|
_render->notifyStop();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-02-11 18:14:22 +08:00
|
|
|
unscheduleUpdate();
|
|
|
|
_state = State::STOP;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-12 16:09:08 +08:00
|
|
|
void ParticleSystem3D::pauseParticleSystem()
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
if (_state == State::RUNNING)
|
|
|
|
{
|
|
|
|
_state = State::PAUSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-12 16:09:08 +08:00
|
|
|
void ParticleSystem3D::resumeParticleSystem()
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
if (_state == State::PAUSE)
|
|
|
|
{
|
|
|
|
_state = State::RUNNING;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ParticleSystem3D::setEmitter(Particle3DEmitter* emitter)
|
|
|
|
{
|
|
|
|
if (_emitter != emitter)
|
|
|
|
{
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_RELEASE(_emitter);
|
2015-02-11 18:14:22 +08:00
|
|
|
emitter->_particleSystem = this;
|
2021-12-25 10:04:45 +08:00
|
|
|
_emitter = emitter;
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_RETAIN(_emitter);
|
2015-02-11 18:14:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ParticleSystem3D::setRender(Particle3DRender* render)
|
|
|
|
{
|
|
|
|
if (_render != render)
|
|
|
|
{
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_RELEASE(_render);
|
2021-12-25 10:04:45 +08:00
|
|
|
_render = render;
|
2015-03-02 13:07:32 +08:00
|
|
|
_render->_particleSystem = this;
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_RETAIN(_render);
|
2015-02-11 18:14:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ParticleSystem3D::addAffector(Particle3DAffector* affector)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (affector && std::find(_affectors.begin(), _affectors.end(), affector) == _affectors.end())
|
|
|
|
{
|
2015-02-11 18:14:22 +08:00
|
|
|
affector->_particleSystem = this;
|
|
|
|
affector->retain();
|
2022-08-09 09:54:53 +08:00
|
|
|
_affectors.emplace_back(affector);
|
2015-02-11 18:14:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ParticleSystem3D::removeAffector(int index)
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT((unsigned int)index < _affectors.size(), "wrong index");
|
2015-02-11 18:14:22 +08:00
|
|
|
_affectors.erase(_affectors.begin() + index);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ParticleSystem3D::removeAllAffector()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
// release all affectors
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& it : _affectors)
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
2015-02-11 18:14:22 +08:00
|
|
|
it->release();
|
|
|
|
}
|
|
|
|
_affectors.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
Particle3DAffector* ParticleSystem3D::getAffector(int index)
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(index < (int)_affectors.size(), "wrong index");
|
2015-02-11 18:14:22 +08:00
|
|
|
return _affectors[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
void ParticleSystem3D::update(float delta)
|
|
|
|
{
|
|
|
|
if (_state != State::RUNNING)
|
|
|
|
return;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
Particle3D* particle = _particlePool.getFirst();
|
2015-02-11 18:14:22 +08:00
|
|
|
while (particle)
|
|
|
|
{
|
|
|
|
if (_emitter)
|
|
|
|
{
|
|
|
|
_emitter->updateEmitter(particle, delta);
|
|
|
|
}
|
|
|
|
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& it : _affectors)
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
2015-02-11 18:14:22 +08:00
|
|
|
it->updateAffector(particle, delta);
|
|
|
|
}
|
|
|
|
|
|
|
|
particle = _particlePool.getNext();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void ParticleSystem3D::draw(Renderer* renderer, const Mat4& transform, uint32_t /*flags*/)
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
2015-03-02 16:28:38 +08:00
|
|
|
if (getAliveParticleCount() && _render)
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
_render->render(renderer, transform, this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void ParticleSystem3D::setBlendFunc(const BlendFunc& blendFunc)
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
_blend = blendFunc;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
const BlendFunc& ParticleSystem3D::getBlendFunc() const
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
return _blend;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void ParticleSystem3D::setParticleQuota(unsigned int quota)
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
_particleQuota = quota;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int ParticleSystem3D::getParticleQuota() const
|
|
|
|
{
|
|
|
|
return _particleQuota;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void ParticleSystem3D::setKeepLocal(bool keepLocal)
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
_keepLocal = keepLocal;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void ParticleSystem3D::setEnabled(bool enabled)
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
_isEnabled = enabled;
|
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_END
|