axmol/extensions/Particle3D/PU/CCPULineAffector.cpp

179 lines
6.4 KiB
C++
Raw Normal View History

2019-11-23 20:27:39 +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
2019-11-23 20:27:39 +08:00
http://www.cocos2d-x.org
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +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
2019-11-23 20:27:39 +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
2019-11-23 20:27:39 +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 "CCPULineAffector.h"
#include "extensions/Particle3D/PU/CCPUParticleSystem3D.h"
NS_CC_BEGIN
// Constants
const float PULineAffector::DEFAULT_MAX_DEVIATION = 1.0f;
2021-12-25 10:04:45 +08:00
const float PULineAffector::DEFAULT_TIME_STEP = 0.1f;
2019-11-23 20:27:39 +08:00
const Vec3 PULineAffector::DEFAULT_END(0, 0, 0);
const float PULineAffector::DEFAULT_DRIFT = 0.0f;
//-----------------------------------------------------------------------
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)
{}
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
PULineAffector::~PULineAffector(void) {}
2019-11-23 20:27:39 +08:00
//-----------------------------------------------------------------------
float PULineAffector::getMaxDeviation() const
{
return _maxDeviation;
}
//-----------------------------------------------------------------------
void PULineAffector::setMaxDeviation(float maxDeviation)
{
2021-12-25 10:04:45 +08:00
_maxDeviation = maxDeviation;
2019-11-23 20:27:39 +08:00
_scaledMaxDeviation = _maxDeviation * _affectorScale.length();
}
//-----------------------------------------------------------------------
const Vec3& PULineAffector::getEnd() const
{
return _end;
}
//-----------------------------------------------------------------------
void PULineAffector::setEnd(const Vec3& end)
{
_end = end;
}
//-----------------------------------------------------------------------
float PULineAffector::getTimeStep() const
{
return _timeStep;
}
//-----------------------------------------------------------------------
void PULineAffector::setTimeStep(float timeStep)
{
_timeStep = timeStep;
}
//-----------------------------------------------------------------------
float PULineAffector::getDrift() const
{
return _drift;
}
//-----------------------------------------------------------------------
void PULineAffector::setDrift(float drift)
{
2021-12-25 10:04:45 +08:00
_drift = drift;
2019-11-23 20:27:39 +08:00
_oneMinusDrift = 1.0f - drift;
}
//-----------------------------------------------------------------------
void PULineAffector::notifyRescaled(const Vec3& scale)
{
_scaledMaxDeviation = _maxDeviation * scale.length();
}
//-----------------------------------------------------------------------
void PULineAffector::preUpdateAffector(float deltaTime)
{
2021-12-25 10:04:45 +08:00
if (/*technique->getNumberOfEmittedParticles()*/ static_cast<PUParticleSystem3D*>(_particleSystem)
->getAliveParticleCount() > 0)
2019-11-23 20:27:39 +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
2019-11-23 20:27:39 +08:00
}
//-----------------------------------------------------------------------
2021-12-25 10:04:45 +08:00
void PULineAffector::updatePUAffector(PUParticle3D* particle, float /*deltaTime*/)
2019-11-23 20:27:39 +08:00
{
//_first = true;
2021-12-25 10:04:45 +08:00
// for (auto iter : _particleSystem->getParticles())
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
// PUParticle3D *particle = iter;
(static_cast<PUParticleSystem3D*>(_particleSystem))
->rotationOffset(particle->originalPosition); // Always update
2019-11-23 20:27:39 +08:00
if (_update && CCRANDOM_0_1() > 0.5f && !_first)
{
// Generate a random vector perpendicular on the line
Vec3 perpendicular;
Vec3::cross(_end, Vec3(CCRANDOM_MINUS1_1(), CCRANDOM_MINUS1_1(), CCRANDOM_MINUS1_1()), &perpendicular);
perpendicular.normalize();
// Determine a random point near the line.
Vec3 targetPosition = particle->originalPosition + _scaledMaxDeviation * CCRANDOM_0_1() * perpendicular;
/** 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.
2019-11-23 20:27:39 +08:00
*/
particle->position = _drift * targetPosition + _oneMinusDrift * particle->position;
2021-12-25 10:04:45 +08:00
(static_cast<PUParticleSystem3D*>(_particleSystem))->rotationOffset(particle->position);
2019-11-23 20:27:39 +08:00
}
_first = false;
}
}
//-----------------------------------------------------------------------
void PULineAffector::postUpdateAffector(float /*deltaTime*/)
{
_update = false;
}
2021-12-25 10:04:45 +08:00
void PULineAffector::firstParticleUpdate(PUParticle3D* /*particle*/, float /*deltaTime*/)
2019-11-23 20:27:39 +08:00
{
_first = true;
}
PULineAffector* PULineAffector::create()
{
2021-12-08 00:11:53 +08:00
auto pla = new PULineAffector();
2019-11-23 20:27:39 +08:00
pla->autorelease();
return pla;
}
2021-12-25 10:04:45 +08:00
void PULineAffector::copyAttributesTo(PUAffector* affector)
2019-11-23 20:27:39 +08:00
{
PUAffector::copyAttributesTo(affector);
PULineAffector* lineAffector = static_cast<PULineAffector*>(affector);
lineAffector->setMaxDeviation(_maxDeviation);
2021-12-25 10:04:45 +08:00
lineAffector->_end = _end;
lineAffector->_timeStep = _timeStep;
lineAffector->_drift = _drift;
2019-11-23 20:27:39 +08:00
lineAffector->_oneMinusDrift = _oneMinusDrift;
}
NS_CC_END