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.
|
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
|
|
|
|
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/CCPUAffector.h"
|
|
|
|
#include "extensions/Particle3D/PU/CCPUEmitter.h"
|
|
|
|
#include "extensions/Particle3D/PU/CCPUParticleSystem3D.h"
|
2015-02-11 18:14:22 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
PUAffector::PUAffector() : _affectorScale(Vec3::ONE), _affectSpecialisation(AFSP_DEFAULT), _mass(1.0f) {}
|
2015-02-11 18:14:22 +08:00
|
|
|
|
2015-03-05 13:24:06 +08:00
|
|
|
PUAffector::~PUAffector()
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
_particleSystem = nullptr;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void PUAffector::updatePUAffector(PUParticle3D* /*particle*/, float /*delta*/) {}
|
2015-02-11 18:14:22 +08:00
|
|
|
|
2015-03-05 13:24:06 +08:00
|
|
|
const Vec3& PUAffector::getDerivedPosition()
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
PUParticleSystem3D* ps = static_cast<PUParticleSystem3D*>(_particleSystem);
|
|
|
|
if (ps)
|
|
|
|
{
|
2015-02-11 18:14:22 +08:00
|
|
|
Mat4 rotMat;
|
|
|
|
Mat4::createRotation(ps->getDerivedOrientation(), &rotMat);
|
2021-12-25 10:04:45 +08:00
|
|
|
_derivedPosition =
|
|
|
|
ps->getDerivedPosition() + rotMat * Vec3(_position.x * _affectorScale.x, _position.y * _affectorScale.y,
|
|
|
|
_position.z * _affectorScale.z);
|
2015-02-11 18:14:22 +08:00
|
|
|
//_particleSystem->getNodeToWorldTransform().transformPoint(_position, &_derivedPosition);
|
|
|
|
}
|
|
|
|
else
|
2015-03-02 13:07:32 +08:00
|
|
|
_derivedPosition = _position;
|
2015-02-11 18:14:22 +08:00
|
|
|
|
|
|
|
return _derivedPosition;
|
2015-03-02 13:07:32 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
// if (mMarkedForEmission)
|
2015-02-11 18:14:22 +08:00
|
|
|
//{
|
|
|
|
// // Use the affector position, because it is emitted
|
|
|
|
// // If a particle is emitted, position and derived position are the same
|
|
|
|
// _derivedPosition = position;
|
2021-12-25 10:04:45 +08:00
|
|
|
// }
|
|
|
|
// else
|
2015-02-11 18:14:22 +08:00
|
|
|
//{
|
|
|
|
// // Add the techniques' derived position
|
2021-12-25 10:04:45 +08:00
|
|
|
// _derivedPosition = mParentTechnique->getDerivedPosition() +
|
2015-02-11 18:14:22 +08:00
|
|
|
// mParentTechnique->getParentSystem()->getDerivedOrientation() * (_mAffectorScale * position);
|
2021-12-25 10:04:45 +08:00
|
|
|
// }
|
|
|
|
// return _derivedPosition;
|
2015-02-11 18:14:22 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
float PUAffector::calculateAffectSpecialisationFactor(const PUParticle3D* particle)
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
// Assume that particle->totalTimeToLive != 0, which is reasonable
|
|
|
|
switch (_affectSpecialisation)
|
|
|
|
{
|
|
|
|
case AFSP_DEFAULT:
|
|
|
|
return 1.0f;
|
|
|
|
break;
|
|
|
|
|
|
|
|
// This means that older particles will be affected MORE than just emitted particles
|
|
|
|
case AFSP_TTL_INCREASE:
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
|
|
|
if (particle)
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
return particle->timeFraction;
|
2015-02-11 18:14:22 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
return 1.0f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2015-02-11 18:14:22 +08:00
|
|
|
|
|
|
|
// This means that older particles will be affected LESS than just emitted particles
|
|
|
|
case AFSP_TTL_DECREASE:
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
|
|
|
if (particle)
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
return 1.0f - particle->timeFraction;
|
2015-02-11 18:14:22 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
return 1.0f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2015-02-11 18:14:22 +08:00
|
|
|
|
|
|
|
default:
|
|
|
|
return 1.0f;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void PUAffector::notifyStart() {}
|
2015-02-11 18:14:22 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void PUAffector::notifyStop() {}
|
2015-02-11 18:14:22 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void PUAffector::notifyPause() {}
|
2015-02-11 18:14:22 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void PUAffector::notifyResume() {}
|
2015-02-11 18:14:22 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void PUAffector::preUpdateAffector(float /*deltaTime*/) {}
|
2015-02-11 18:14:22 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void PUAffector::postUpdateAffector(float /*deltaTime*/) {}
|
2015-02-11 18:14:22 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void PUAffector::prepare() {}
|
2015-02-11 18:14:22 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void PUAffector::unPrepare() {}
|
2015-02-11 18:14:22 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void PUAffector::initParticleForEmission(PUParticle3D* /*particle*/) {}
|
2015-02-11 18:14:22 +08:00
|
|
|
|
2015-03-05 13:24:06 +08:00
|
|
|
void PUAffector::notifyRescaled(const Vec3& scale)
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
_affectorScale = scale;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void PUAffector::firstParticleUpdate(PUParticle3D* /*particle*/, float /*deltaTime*/) {}
|
2015-02-11 18:14:22 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void PUAffector::setMass(float mass)
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_mass = mass;
|
2015-02-11 18:14:22 +08:00
|
|
|
}
|
|
|
|
|
2015-03-05 13:24:06 +08:00
|
|
|
float PUAffector::getMass() const
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
return _mass;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void PUAffector::copyAttributesTo(PUAffector* affector)
|
2015-03-02 13:07:32 +08:00
|
|
|
{
|
|
|
|
affector->setName(_name);
|
2015-04-15 12:44:03 +08:00
|
|
|
affector->setAffectorType(_affectorType);
|
2021-12-25 10:04:45 +08:00
|
|
|
affector->_position = _position;
|
|
|
|
affector->_isEnabled = _isEnabled;
|
|
|
|
affector->_particleSystem = _particleSystem;
|
|
|
|
affector->_affectorScale = _affectorScale;
|
2015-03-02 13:07:32 +08:00
|
|
|
affector->_affectSpecialisation = _affectSpecialisation;
|
2021-12-25 10:04:45 +08:00
|
|
|
affector->_excludedEmitters = _excludedEmitters;
|
2015-03-02 13:07:32 +08:00
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
void PUAffector::addEmitterToExclude(std::string_view emitterName)
|
2015-03-02 13:07:32 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
auto iter = std::find(_excludedEmitters.begin(), _excludedEmitters.end(), emitterName);
|
|
|
|
if (iter == _excludedEmitters.end())
|
|
|
|
{
|
2021-12-26 23:26:34 +08:00
|
|
|
_excludedEmitters.push_back(std::string{emitterName});
|
2015-03-02 13:07:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
void PUAffector::removeEmitterToExclude(std::string_view emitterName)
|
2015-03-02 13:07:32 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
auto iter = std::find(_excludedEmitters.begin(), _excludedEmitters.end(), emitterName);
|
|
|
|
if (iter != _excludedEmitters.end())
|
|
|
|
{
|
2015-03-02 13:07:32 +08:00
|
|
|
_excludedEmitters.erase(iter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void PUAffector::process(PUParticle3D* particle, float delta, bool firstParticle)
|
2015-03-02 13:07:32 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (firstParticle)
|
|
|
|
{
|
2015-03-02 13:07:32 +08:00
|
|
|
firstParticleUpdate(particle, delta);
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!_excludedEmitters.empty() && particle->parentEmitter)
|
|
|
|
{
|
2015-03-02 13:07:32 +08:00
|
|
|
// Return if the emitter which emits this particle is part of the vector
|
2021-12-26 23:26:34 +08:00
|
|
|
auto emitterName = particle->parentEmitter->getName();
|
2021-12-28 11:00:34 +08:00
|
|
|
auto iter = std::find(_excludedEmitters.begin(), _excludedEmitters.end(), emitterName);
|
2015-03-02 13:07:32 +08:00
|
|
|
if (iter != _excludedEmitters.end())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
updatePUAffector(particle, delta);
|
|
|
|
}
|
|
|
|
|
2015-02-11 18:14:22 +08:00
|
|
|
NS_CC_END
|