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
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-03-05 13:24:06 +08:00
|
|
|
#include "CCPULineAffector.h"
|
|
|
|
#include "extensions/Particle3D/PU/CCPUParticleSystem3D.h"
|
2015-02-11 18:14:22 +08:00
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BEGIN
|
2015-02-11 18:14:22 +08:00
|
|
|
|
|
|
|
// Constants
|
2015-03-05 13:24:06 +08:00
|
|
|
const float PULineAffector::DEFAULT_MAX_DEVIATION = 1.0f;
|
2021-12-25 10:04:45 +08:00
|
|
|
const float PULineAffector::DEFAULT_TIME_STEP = 0.1f;
|
2015-03-05 13:24:06 +08:00
|
|
|
const Vec3 PULineAffector::DEFAULT_END(0, 0, 0);
|
|
|
|
const float PULineAffector::DEFAULT_DRIFT = 0.0f;
|
2015-02-11 18:14:22 +08:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------
|
2021-12-25 10:04:45 +08:00
|
|
|
PULineAffector::PULineAffector()
|
|
|
|
: PUAffector()
|
|
|
|
, _maxDeviation(DEFAULT_MAX_DEVIATION)
|
|
|
|
, _scaledMaxDeviation(1.0f)
|
|
|
|
, _end(DEFAULT_END)
|
|
|
|
, _timeSinceLastUpdate(0.0f)
|
|
|
|
, _timeStep(DEFAULT_TIME_STEP)
|
|
|
|
, _drift(DEFAULT_DRIFT)
|
|
|
|
, _oneMinusDrift(1.0f)
|
|
|
|
, _update(true)
|
|
|
|
, _first(true)
|
|
|
|
{}
|
2015-02-11 18:14:22 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
PULineAffector::~PULineAffector(void) {}
|
2015-02-11 18:14:22 +08:00
|
|
|
//-----------------------------------------------------------------------
|
2019-07-22 09:38:46 +08:00
|
|
|
float PULineAffector::getMaxDeviation() const
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
return _maxDeviation;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2015-03-05 13:24:06 +08:00
|
|
|
void PULineAffector::setMaxDeviation(float maxDeviation)
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_maxDeviation = maxDeviation;
|
2015-02-11 18:14:22 +08:00
|
|
|
_scaledMaxDeviation = _maxDeviation * _affectorScale.length();
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2019-07-22 09:38:46 +08:00
|
|
|
const Vec3& PULineAffector::getEnd() const
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
return _end;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2015-03-05 13:24:06 +08:00
|
|
|
void PULineAffector::setEnd(const Vec3& end)
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
_end = end;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2019-07-22 09:38:46 +08:00
|
|
|
float PULineAffector::getTimeStep() const
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
return _timeStep;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2015-03-05 13:24:06 +08:00
|
|
|
void PULineAffector::setTimeStep(float timeStep)
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
_timeStep = timeStep;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2019-07-22 09:38:46 +08:00
|
|
|
float PULineAffector::getDrift() const
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
return _drift;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2015-03-05 13:24:06 +08:00
|
|
|
void PULineAffector::setDrift(float drift)
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_drift = drift;
|
2015-02-11 18:14:22 +08:00
|
|
|
_oneMinusDrift = 1.0f - drift;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2015-03-05 13:24:06 +08:00
|
|
|
void PULineAffector::notifyRescaled(const Vec3& scale)
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
_scaledMaxDeviation = _maxDeviation * scale.length();
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2015-03-05 13:24:06 +08:00
|
|
|
void PULineAffector::preUpdateAffector(float deltaTime)
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (/*technique->getNumberOfEmittedParticles()*/ static_cast<PUParticleSystem3D*>(_particleSystem)
|
|
|
|
->getAliveParticleCount() > 0)
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
_timeSinceLastUpdate += deltaTime;
|
|
|
|
while (_timeSinceLastUpdate > _timeStep)
|
|
|
|
{
|
|
|
|
_timeSinceLastUpdate -= _timeStep;
|
|
|
|
_update = true;
|
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
(static_cast<PUParticleSystem3D*>(_particleSystem))->rotationOffset(_end); // Always update
|
2015-02-11 18:14:22 +08:00
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void PULineAffector::updatePUAffector(PUParticle3D* particle, float /*deltaTime*/)
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
//_first = true;
|
2022-07-21 19:19:08 +08:00
|
|
|
// for (auto&& iter : _particleSystem->getParticles())
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
// PUParticle3D *particle = iter;
|
|
|
|
(static_cast<PUParticleSystem3D*>(_particleSystem))
|
|
|
|
->rotationOffset(particle->originalPosition); // Always update
|
2022-07-16 10:43:05 +08:00
|
|
|
if (_update && AXRANDOM_0_1() > 0.5f && !_first)
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
// Generate a random vector perpendicular on the line
|
|
|
|
Vec3 perpendicular;
|
2022-07-16 10:43:05 +08:00
|
|
|
Vec3::cross(_end, Vec3(AXRANDOM_MINUS1_1(), AXRANDOM_MINUS1_1(), AXRANDOM_MINUS1_1()), &perpendicular);
|
2015-02-11 18:14:22 +08:00
|
|
|
perpendicular.normalize();
|
|
|
|
|
|
|
|
// Determine a random point near the line.
|
2022-07-16 10:43:05 +08:00
|
|
|
Vec3 targetPosition = particle->originalPosition + _scaledMaxDeviation * AXRANDOM_0_1() * perpendicular;
|
2015-02-11 18:14:22 +08:00
|
|
|
|
|
|
|
/** Set the new position.
|
|
|
|
@remarks
|
2021-12-25 10:04:45 +08:00
|
|
|
This affector already takes rotational offset of the particle system into account. This means that there
|
|
|
|
is no need to set the particle system to keep_local to 'true'. The reason is that this is a specialized
|
|
|
|
affector that calculates a new particle position instead of a direction.
|
2015-02-11 18:14:22 +08:00
|
|
|
*/
|
|
|
|
particle->position = _drift * targetPosition + _oneMinusDrift * particle->position;
|
2021-12-25 10:04:45 +08:00
|
|
|
(static_cast<PUParticleSystem3D*>(_particleSystem))->rotationOffset(particle->position);
|
2015-02-11 18:14:22 +08:00
|
|
|
}
|
|
|
|
_first = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------
|
2016-11-16 09:48:37 +08:00
|
|
|
void PULineAffector::postUpdateAffector(float /*deltaTime*/)
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
_update = false;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void PULineAffector::firstParticleUpdate(PUParticle3D* /*particle*/, float /*deltaTime*/)
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
_first = true;
|
|
|
|
}
|
|
|
|
|
2015-03-05 13:24:06 +08:00
|
|
|
PULineAffector* PULineAffector::create()
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
auto pla = new PULineAffector();
|
2015-02-11 18:14:22 +08:00
|
|
|
pla->autorelease();
|
|
|
|
return pla;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void PULineAffector::copyAttributesTo(PUAffector* affector)
|
2015-03-02 13:07:32 +08:00
|
|
|
{
|
2015-03-05 13:24:06 +08:00
|
|
|
PUAffector::copyAttributesTo(affector);
|
2015-03-02 13:07:32 +08:00
|
|
|
|
2015-03-05 13:24:06 +08:00
|
|
|
PULineAffector* lineAffector = static_cast<PULineAffector*>(affector);
|
2015-03-02 13:07:32 +08:00
|
|
|
lineAffector->setMaxDeviation(_maxDeviation);
|
2021-12-25 10:04:45 +08:00
|
|
|
lineAffector->_end = _end;
|
|
|
|
lineAffector->_timeStep = _timeStep;
|
|
|
|
lineAffector->_drift = _drift;
|
2015-03-02 13:07:32 +08:00
|
|
|
lineAffector->_oneMinusDrift = _oneMinusDrift;
|
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_END
|