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.
|
|
|
|
Copyright (c) 2015 Chukong Technologies Inc.
|
2015-02-11 18:14:22 +08:00
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
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:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
//-----------------------------------------------------------------------
|
2015-03-05 13:24:06 +08:00
|
|
|
PULineEmitter::PULineEmitter(void) :
|
|
|
|
PUEmitter(),
|
2015-02-11 18:14:22 +08:00
|
|
|
_end(DEFAULT_END),
|
|
|
|
_perpendicular(Vec3::ZERO),
|
|
|
|
_maxDeviation(DEFAULT_MAX_DEVIATION),
|
|
|
|
_minIncrement(DEFAULT_MIN_INCREMENT),
|
|
|
|
_maxIncrement(DEFAULT_MAX_INCREMENT),
|
|
|
|
_increment(0.0f),
|
|
|
|
_length(0.0f),
|
|
|
|
_incrementsLeft(true),
|
|
|
|
_first(true),
|
|
|
|
_scaledEnd(Vec3::ZERO),
|
|
|
|
_scaledMaxDeviation(0.0f),
|
|
|
|
_scaledMinIncrement(0.0f),
|
|
|
|
_scaledMaxIncrement(0.0f),
|
|
|
|
_scaledLength(0.0f)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2015-03-05 13:24:06 +08:00
|
|
|
void PULineEmitter::notifyStart (void)
|
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();
|
2015-02-11 18:14:22 +08:00
|
|
|
_increment = 0.0f;
|
|
|
|
_incrementsLeft = true;
|
|
|
|
_first = true;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
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;
|
|
|
|
_scaledLength = _scaledEnd.length();
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
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.
|
|
|
|
//if (!_isEnabled)
|
|
|
|
//{
|
|
|
|
// notifyStart();
|
|
|
|
// mParentTechnique->lockAllParticles(); // Needed to set the first generated particle also first in the list with particles
|
|
|
|
//}
|
|
|
|
}
|
|
|
|
|
|
|
|
return requested;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2015-03-05 13:24:06 +08:00
|
|
|
const Vec3& PULineEmitter::getEnd(void) 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();
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2015-03-05 13:24:06 +08:00
|
|
|
float PULineEmitter::getMaxIncrement(void) 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
|
|
|
{
|
|
|
|
_maxIncrement = maxIncrement;
|
|
|
|
_scaledMaxIncrement = _maxIncrement * _emitterScale.length();
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2015-03-05 13:24:06 +08:00
|
|
|
float PULineEmitter::getMinIncrement(void) 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
|
|
|
{
|
|
|
|
_minIncrement = minIncrement;
|
|
|
|
_scaledMinIncrement = _minIncrement * _emitterScale.length();
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2015-03-05 13:24:06 +08:00
|
|
|
float PULineEmitter::getMaxDeviation(void) 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
|
|
|
{
|
|
|
|
_maxDeviation = maxDeviation;
|
|
|
|
_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
|
|
|
|
Vec3::cross(_end, Vec3(CCRANDOM_MINUS1_1(),
|
|
|
|
CCRANDOM_MINUS1_1(),
|
|
|
|
CCRANDOM_MINUS1_1()), &_perpendicular);
|
|
|
|
_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)
|
|
|
|
{
|
|
|
|
Vec3 basePosition = _derivedPosition + fraction * _scaledEnd;
|
|
|
|
particle->position = basePosition + _scaledMaxDeviation * CCRANDOM_0_1() * _perpendicular;
|
|
|
|
particle->originalPosition = basePosition; // Position is without deviation from the line,
|
|
|
|
// to make affectors a bit faster/easier.
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
particle->position = _derivedPosition;
|
|
|
|
particle->originalPosition = particle->position;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
particle->position = _derivedPosition + fraction * _scaledEnd;
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
//particle->direction = _perpendicular.randomDeviant(angle, mUpVector);
|
2015-03-05 13:24:06 +08:00
|
|
|
particle->direction = PUUtil::randomDeviant(_perpendicular, angle, _upVector);
|
2015-02-11 18:14:22 +08:00
|
|
|
particle->originalDirection = particle->direction;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
particle->direction = _perpendicular;
|
|
|
|
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
|
|
|
{
|
2015-03-05 13:24:06 +08:00
|
|
|
auto pe = new (std::nothrow) PULineEmitter();
|
2015-02-11 18:14:22 +08:00
|
|
|
pe->autorelease();
|
|
|
|
return pe;
|
|
|
|
}
|
|
|
|
|
2015-03-05 13:24:06 +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);
|
2015-03-02 13:07:32 +08:00
|
|
|
lineEmitter->setEnd(_end); // Force calculation of the length
|
|
|
|
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
|