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-07-09 22:23:34 +08:00
|
|
|
https://axis-project.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 "CCPULineEmitter.h"
|
|
|
|
#include "extensions/Particle3D/PU/CCPUParticleSystem3D.h"
|
|
|
|
#include "extensions/Particle3D/PU/CCPUUtil.h"
|
2015-02-11 18:14:22 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
// Constants
|
2015-03-05 13:24:06 +08:00
|
|
|
const Vec3 PULineEmitter::DEFAULT_END(0, 0, 0);
|
|
|
|
const float PULineEmitter::DEFAULT_MIN_INCREMENT = 0.0f;
|
|
|
|
const float PULineEmitter::DEFAULT_MAX_INCREMENT = 0.0f;
|
|
|
|
const float PULineEmitter::DEFAULT_MAX_DEVIATION = 0.0f;
|
2015-02-11 18:14:22 +08:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------
|
2021-12-25 10:04:45 +08:00
|
|
|
PULineEmitter::PULineEmitter()
|
|
|
|
: PUEmitter()
|
|
|
|
, _end(DEFAULT_END)
|
|
|
|
, _maxDeviation(DEFAULT_MAX_DEVIATION)
|
|
|
|
, _minIncrement(DEFAULT_MIN_INCREMENT)
|
|
|
|
, _maxIncrement(DEFAULT_MAX_INCREMENT)
|
|
|
|
, _increment(0.0f)
|
|
|
|
, _length(0.0f)
|
|
|
|
, _incrementsLeft(true)
|
|
|
|
, _first(true)
|
|
|
|
, _scaledMaxDeviation(0.0f)
|
|
|
|
, _scaledMinIncrement(0.0f)
|
|
|
|
, _scaledMaxIncrement(0.0f)
|
|
|
|
, _scaledLength(0.0f)
|
|
|
|
{}
|
2015-02-11 18:14:22 +08:00
|
|
|
//-----------------------------------------------------------------------
|
2021-12-25 10:04:45 +08:00
|
|
|
void PULineEmitter::notifyStart()
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
// Reset the incremental attributes to allow a restart.
|
2015-03-05 13:24:06 +08:00
|
|
|
PUEmitter::notifyStart();
|
2021-12-25 10:04:45 +08:00
|
|
|
_increment = 0.0f;
|
2015-02-11 18:14:22 +08:00
|
|
|
_incrementsLeft = true;
|
2021-12-25 10:04:45 +08:00
|
|
|
_first = true;
|
2015-02-11 18:14:22 +08:00
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2015-03-05 13:24:06 +08:00
|
|
|
void PULineEmitter::notifyRescaled(const Vec3& scale)
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
// Scale the internal attributes and use them, otherwise this results in too many calculations per particle
|
2015-03-05 13:24:06 +08:00
|
|
|
PUEmitter::notifyRescaled(scale);
|
2015-02-11 18:14:22 +08:00
|
|
|
float scaleLength = scale.length();
|
Optimize Vec3
small function Vec3 move to Vec3.inl
Added:
add(float xx, float yy, float zz);
setZero();
Change all code:
_vec3 = Vec3(x, y, z); -> _vec3.set(x, y, z);
Vec3 vec3 = Vec3(x, y, z); -> Vec3 vec3(x, y, z);
_vec3 += Vec3(x, y, z); -> _vec3.add(x, y, z);
_vec3 = Vec3::ZERO; -> _vec3.setZero();
2015-04-05 18:09:50 +08:00
|
|
|
_scaledEnd.set(_end.x * scale.x, _end.y * scale.y, _end.z * scale.z);
|
2015-02-11 18:14:22 +08:00
|
|
|
_scaledMaxDeviation = _maxDeviation * scaleLength;
|
|
|
|
_scaledMinIncrement = _minIncrement * scaleLength;
|
|
|
|
_scaledMaxIncrement = (_maxIncrement - _minIncrement) * scaleLength;
|
2021-12-25 10:04:45 +08:00
|
|
|
_scaledLength = _scaledEnd.length();
|
2015-02-11 18:14:22 +08:00
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2015-03-05 13:24:06 +08:00
|
|
|
unsigned short PULineEmitter::calculateRequestedParticles(float timeElapsed)
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
2015-03-05 13:24:06 +08:00
|
|
|
unsigned short requested = PUEmitter::calculateRequestedParticles(timeElapsed);
|
2015-02-11 18:14:22 +08:00
|
|
|
|
|
|
|
if (_scaledMaxIncrement > 0)
|
|
|
|
{
|
|
|
|
// Do not create particles if there are no increments left
|
|
|
|
if (!_incrementsLeft)
|
|
|
|
{
|
|
|
|
// Set back to 0 again
|
|
|
|
requested = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
////FIXME
|
|
|
|
//// For duration + repeat/delay: Clear the particles and start all over again.
|
2021-12-25 10:04:45 +08:00
|
|
|
// if (!_isEnabled)
|
2015-02-11 18:14:22 +08:00
|
|
|
//{
|
|
|
|
// notifyStart();
|
2021-12-25 10:04:45 +08:00
|
|
|
// mParentTechnique->lockAllParticles(); // Needed to set the first generated particle also first in the
|
2021-12-28 11:00:34 +08:00
|
|
|
// list with particles
|
2021-12-25 10:04:45 +08:00
|
|
|
// }
|
2015-02-11 18:14:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return requested;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2019-07-22 09:38:46 +08:00
|
|
|
const Vec3& PULineEmitter::getEnd() const
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
return _end;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2015-03-05 13:24:06 +08:00
|
|
|
void PULineEmitter::setEnd(const Vec3& end)
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
_end = end;
|
Optimize Vec3
small function Vec3 move to Vec3.inl
Added:
add(float xx, float yy, float zz);
setZero();
Change all code:
_vec3 = Vec3(x, y, z); -> _vec3.set(x, y, z);
Vec3 vec3 = Vec3(x, y, z); -> Vec3 vec3(x, y, z);
_vec3 += Vec3(x, y, z); -> _vec3.add(x, y, z);
_vec3 = Vec3::ZERO; -> _vec3.setZero();
2015-04-05 18:09:50 +08:00
|
|
|
_scaledEnd.set(_end.x * _emitterScale.x, _end.y * _emitterScale.y, _end.z * _emitterScale.z);
|
2015-02-11 18:14:22 +08:00
|
|
|
_length = _end.length();
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2019-07-22 09:38:46 +08:00
|
|
|
float PULineEmitter::getMaxIncrement() const
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
return _maxIncrement;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2015-03-05 13:24:06 +08:00
|
|
|
void PULineEmitter::setMaxIncrement(float maxIncrement)
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_maxIncrement = maxIncrement;
|
2015-02-11 18:14:22 +08:00
|
|
|
_scaledMaxIncrement = _maxIncrement * _emitterScale.length();
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2019-07-22 09:38:46 +08:00
|
|
|
float PULineEmitter::getMinIncrement() const
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
return _minIncrement;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2015-03-05 13:24:06 +08:00
|
|
|
void PULineEmitter::setMinIncrement(float minIncrement)
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_minIncrement = minIncrement;
|
2015-02-11 18:14:22 +08:00
|
|
|
_scaledMinIncrement = _minIncrement * _emitterScale.length();
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2019-07-22 09:38:46 +08:00
|
|
|
float PULineEmitter::getMaxDeviation() const
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
return _maxDeviation;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2015-03-05 13:24:06 +08:00
|
|
|
void PULineEmitter::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 * _emitterScale.length();
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2015-03-05 13:24:06 +08:00
|
|
|
void PULineEmitter::initParticlePosition(PUParticle3D* particle)
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
// Remark: Don't take the orientation of the node into account. The mEnd position is leading.
|
|
|
|
if (_autoDirection || (_scaledMaxDeviation > 0.0f && !_first))
|
|
|
|
{
|
|
|
|
// Generate a random vector perpendicular on the line if this is required
|
2021-12-25 10:04:45 +08:00
|
|
|
Vec3::cross(_end, Vec3(CCRANDOM_MINUS1_1(), CCRANDOM_MINUS1_1(), CCRANDOM_MINUS1_1()), &_perpendicular);
|
2015-02-11 18:14:22 +08:00
|
|
|
_perpendicular.normalize();
|
|
|
|
}
|
|
|
|
|
|
|
|
// If mMaxIncrement has been set, the particle emission follows a trajectory path along the line
|
|
|
|
float fraction = 0.0f;
|
|
|
|
if (_scaledMaxIncrement > 0.0f)
|
|
|
|
{
|
|
|
|
if (!_first)
|
|
|
|
{
|
|
|
|
_increment += (_scaledMinIncrement + CCRANDOM_0_1() * _scaledMaxIncrement);
|
|
|
|
if (_increment >= _scaledLength)
|
|
|
|
{
|
|
|
|
_incrementsLeft = false;
|
|
|
|
}
|
|
|
|
fraction = _increment / _scaledLength;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fraction = CCRANDOM_0_1();
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the deviation has been set, generate a position with a certain distance from the line
|
|
|
|
getDerivedPosition();
|
|
|
|
if (_scaledMaxDeviation > 0.0f && _incrementsLeft)
|
|
|
|
{
|
|
|
|
if (!_first)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
Vec3 basePosition = _derivedPosition + fraction * _scaledEnd;
|
|
|
|
particle->position = basePosition + _scaledMaxDeviation * CCRANDOM_0_1() * _perpendicular;
|
|
|
|
particle->originalPosition = basePosition; // Position is without deviation from the line,
|
2015-02-11 18:14:22 +08:00
|
|
|
// to make affectors a bit faster/easier.
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
particle->position = _derivedPosition;
|
2015-02-11 18:14:22 +08:00
|
|
|
particle->originalPosition = particle->position;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
particle->position = _derivedPosition + fraction * _scaledEnd;
|
2015-02-11 18:14:22 +08:00
|
|
|
particle->originalPosition = particle->position;
|
|
|
|
}
|
|
|
|
|
|
|
|
_first = false;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2015-03-05 13:24:06 +08:00
|
|
|
void PULineEmitter::initParticleDirection(PUParticle3D* particle)
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
|
|
|
if (_autoDirection)
|
|
|
|
{
|
|
|
|
float angle = 0.0f;
|
|
|
|
generateAngle(angle);
|
|
|
|
if (angle != 0.0f)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
// particle->direction = _perpendicular.randomDeviant(angle, mUpVector);
|
|
|
|
particle->direction = PUUtil::randomDeviant(_perpendicular, angle, _upVector);
|
2015-02-11 18:14:22 +08:00
|
|
|
particle->originalDirection = particle->direction;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
particle->direction = _perpendicular;
|
2015-02-11 18:14:22 +08:00
|
|
|
particle->originalDirection = particle->direction;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Use the standard way
|
2015-03-05 13:24:06 +08:00
|
|
|
PUEmitter::initParticleDirection(particle);
|
2015-02-11 18:14:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-05 13:24:06 +08:00
|
|
|
PULineEmitter* PULineEmitter::create()
|
2015-02-11 18:14:22 +08:00
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
auto pe = new PULineEmitter();
|
2015-02-11 18:14:22 +08:00
|
|
|
pe->autorelease();
|
|
|
|
return pe;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void PULineEmitter::copyAttributesTo(PUEmitter* emitter)
|
2015-03-02 13:07:32 +08:00
|
|
|
{
|
2015-03-05 13:24:06 +08:00
|
|
|
PUEmitter::copyAttributesTo(emitter);
|
2015-03-02 13:07:32 +08:00
|
|
|
|
2015-03-05 13:24:06 +08:00
|
|
|
PULineEmitter* lineEmitter = static_cast<PULineEmitter*>(emitter);
|
2021-12-25 10:04:45 +08:00
|
|
|
lineEmitter->setEnd(_end); // Force calculation of the length
|
2015-03-02 13:07:32 +08:00
|
|
|
lineEmitter->_length = _length;
|
|
|
|
lineEmitter->setMinIncrement(_minIncrement);
|
|
|
|
lineEmitter->setMaxIncrement(_maxIncrement);
|
|
|
|
lineEmitter->setMaxDeviation(_maxDeviation);
|
|
|
|
}
|
|
|
|
|
2015-03-05 13:24:06 +08:00
|
|
|
PULineEmitter* PULineEmitter::clone()
|
2015-03-02 13:07:32 +08:00
|
|
|
{
|
2015-03-05 13:24:06 +08:00
|
|
|
auto be = PULineEmitter::create();
|
2015-03-02 13:07:32 +08:00
|
|
|
copyAttributesTo(be);
|
|
|
|
return be;
|
|
|
|
}
|
|
|
|
|
2015-02-11 18:14:22 +08:00
|
|
|
NS_CC_END
|