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
|
|
|
|
2022-02-24 18:51:36 +08:00
|
|
|
https://adxeproject.github.io/
|
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 "CCPUScaleAffector.h"
|
|
|
|
#include "extensions/Particle3D/PU/CCPUParticleSystem3D.h"
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
// Constants
|
2021-12-25 10:04:45 +08:00
|
|
|
const float PUScaleAffector::DEFAULT_X_SCALE = 1.0f;
|
|
|
|
const float PUScaleAffector::DEFAULT_Y_SCALE = 1.0f;
|
|
|
|
const float PUScaleAffector::DEFAULT_Z_SCALE = 1.0f;
|
2019-11-23 20:27:39 +08:00
|
|
|
const float PUScaleAffector::DEFAULT_XYZ_SCALE = 1.0f;
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------
|
2021-12-25 10:04:45 +08:00
|
|
|
PUScaleAffector::PUScaleAffector()
|
|
|
|
: PUAffector()
|
|
|
|
, _dynScaleXSet(false)
|
|
|
|
, _dynScaleYSet(false)
|
|
|
|
, _dynScaleZSet(false)
|
|
|
|
, _dynScaleXYZSet(false)
|
|
|
|
, _sinceStartSystem(false)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_dynScaleX = new PUDynamicAttributeFixed();
|
|
|
|
_dynScaleY = new PUDynamicAttributeFixed();
|
|
|
|
_dynScaleZ = new PUDynamicAttributeFixed();
|
2021-12-08 00:11:53 +08:00
|
|
|
_dynScaleXYZ = new PUDynamicAttributeFixed();
|
2019-11-23 20:27:39 +08:00
|
|
|
(static_cast<PUDynamicAttributeFixed*>(_dynScaleX))->setValue(DEFAULT_X_SCALE);
|
|
|
|
(static_cast<PUDynamicAttributeFixed*>(_dynScaleY))->setValue(DEFAULT_Y_SCALE);
|
|
|
|
(static_cast<PUDynamicAttributeFixed*>(_dynScaleZ))->setValue(DEFAULT_Z_SCALE);
|
|
|
|
(static_cast<PUDynamicAttributeFixed*>(_dynScaleXYZ))->setValue(DEFAULT_XYZ_SCALE);
|
|
|
|
_latestTimeElapsed = 1.0f;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
|
|
|
PUScaleAffector::~PUScaleAffector()
|
|
|
|
{
|
|
|
|
if (_dynScaleX)
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(_dynScaleX);
|
|
|
|
}
|
|
|
|
if (_dynScaleY)
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(_dynScaleY);
|
|
|
|
}
|
|
|
|
if (_dynScaleZ)
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(_dynScaleZ);
|
|
|
|
}
|
|
|
|
if (_dynScaleXYZ)
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(_dynScaleXYZ);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
|
|
|
void PUScaleAffector::setDynScaleX(PUDynamicAttribute* dynScaleX)
|
|
|
|
{
|
|
|
|
if (_dynScaleX)
|
|
|
|
CC_SAFE_DELETE(_dynScaleX);
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
_dynScaleX = dynScaleX;
|
2019-11-23 20:27:39 +08:00
|
|
|
_dynScaleXSet = true;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2021-12-25 10:04:45 +08:00
|
|
|
void PUScaleAffector::resetDynScaleX(bool resetToDefault)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
if (resetToDefault)
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(_dynScaleX);
|
2021-12-08 00:11:53 +08:00
|
|
|
_dynScaleX = new PUDynamicAttributeFixed();
|
2019-11-23 20:27:39 +08:00
|
|
|
(static_cast<PUDynamicAttributeFixed*>(_dynScaleX))->setValue(DEFAULT_X_SCALE);
|
|
|
|
_dynScaleXSet = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_dynScaleXSet = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
|
|
|
void PUScaleAffector::setDynScaleY(PUDynamicAttribute* dynScaleY)
|
|
|
|
{
|
|
|
|
if (_dynScaleY)
|
|
|
|
CC_SAFE_DELETE(_dynScaleY);
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
_dynScaleY = dynScaleY;
|
2019-11-23 20:27:39 +08:00
|
|
|
_dynScaleYSet = true;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
|
|
|
void PUScaleAffector::resetDynScaleY(bool resetToDefault)
|
|
|
|
{
|
|
|
|
if (resetToDefault)
|
|
|
|
{
|
|
|
|
|
|
|
|
CC_SAFE_DELETE(_dynScaleY);
|
2021-12-08 00:11:53 +08:00
|
|
|
_dynScaleY = new PUDynamicAttributeFixed();
|
2019-11-23 20:27:39 +08:00
|
|
|
(static_cast<PUDynamicAttributeFixed*>(_dynScaleY))->setValue(DEFAULT_X_SCALE);
|
|
|
|
_dynScaleYSet = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_dynScaleYSet = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
|
|
|
void PUScaleAffector::setDynScaleZ(PUDynamicAttribute* dynScaleZ)
|
|
|
|
{
|
|
|
|
if (_dynScaleZ)
|
|
|
|
CC_SAFE_DELETE(_dynScaleZ);
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
_dynScaleZ = dynScaleZ;
|
2019-11-23 20:27:39 +08:00
|
|
|
_dynScaleZSet = true;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
|
|
|
void PUScaleAffector::resetDynScaleZ(bool resetToDefault)
|
|
|
|
{
|
|
|
|
if (resetToDefault)
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(_dynScaleZ);
|
2021-12-08 00:11:53 +08:00
|
|
|
_dynScaleZ = new PUDynamicAttributeFixed();
|
2019-11-23 20:27:39 +08:00
|
|
|
(static_cast<PUDynamicAttributeFixed*>(_dynScaleZ))->setValue(DEFAULT_X_SCALE);
|
|
|
|
_dynScaleYSet = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_dynScaleYSet = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
|
|
|
void PUScaleAffector::setDynScaleXYZ(PUDynamicAttribute* dynScaleXYZ)
|
|
|
|
{
|
|
|
|
if (_dynScaleXYZ)
|
|
|
|
CC_SAFE_DELETE(_dynScaleXYZ);
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
_dynScaleXYZ = dynScaleXYZ;
|
2019-11-23 20:27:39 +08:00
|
|
|
_dynScaleXYZSet = true;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
|
|
|
void PUScaleAffector::resetDynScaleXYZ(bool resetToDefault)
|
|
|
|
{
|
|
|
|
if (resetToDefault)
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(_dynScaleXYZ);
|
2021-12-08 00:11:53 +08:00
|
|
|
_dynScaleXYZ = new PUDynamicAttributeFixed();
|
2019-11-23 20:27:39 +08:00
|
|
|
(static_cast<PUDynamicAttributeFixed*>(_dynScaleXYZ))->setValue(DEFAULT_XYZ_SCALE);
|
|
|
|
_dynScaleXYZSet = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_dynScaleXYZSet = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
|
|
|
float PUScaleAffector::calculateScale(PUDynamicAttribute* dynScale, PUParticle3D* particle)
|
|
|
|
{
|
|
|
|
if (!particle)
|
|
|
|
return 0.0f;
|
|
|
|
|
|
|
|
if (_sinceStartSystem)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
// If control points are used (curved type), the first value of each control point is seconds from the start of
|
|
|
|
// the system
|
|
|
|
return _dynamicAttributeHelper.calculate(
|
|
|
|
dynScale, (static_cast<PUParticleSystem3D*>(_particleSystem))->getTimeElapsedSinceStart());
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
// If control points are used (curved type), the first value of each control point is the fraction of the
|
|
|
|
// particle lifetime [0..1]
|
2019-11-23 20:27:39 +08:00
|
|
|
return _dynamicAttributeHelper.calculate(dynScale, particle->timeFraction);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void PUScaleAffector::updatePUAffector(PUParticle3D* particle, float deltaTime)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
//// Only continue if the particle is a visual particle
|
2021-12-25 10:04:45 +08:00
|
|
|
// if (particle->particleType != Particle::PT_VISUAL)
|
2019-11-23 20:27:39 +08:00
|
|
|
// return;
|
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;
|
|
|
|
float ds = 0;
|
|
|
|
float width = 0;
|
|
|
|
float height = 0;
|
|
|
|
float depth = 0;
|
|
|
|
float dimension = 0; // Added in V1.4
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
if (_dynScaleXYZSet)
|
|
|
|
{
|
|
|
|
ds = calculateScale(_dynScaleXYZ, particle) * deltaTime * calculateAffectSpecialisationFactor(particle);
|
|
|
|
dimension = particle->width + ds * _affectorScale.x;
|
|
|
|
if (dimension > 0)
|
|
|
|
{
|
|
|
|
width = dimension;
|
|
|
|
}
|
|
|
|
dimension = particle->height + ds * _affectorScale.y;
|
|
|
|
if (dimension > 0)
|
|
|
|
{
|
|
|
|
height = dimension;
|
|
|
|
}
|
|
|
|
dimension = particle->depth + ds * _affectorScale.z;
|
|
|
|
if (dimension > 0)
|
|
|
|
{
|
|
|
|
depth = dimension;
|
|
|
|
}
|
|
|
|
particle->setOwnDimensions(width, height, depth);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (_dynScaleXSet)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
ds = calculateScale(_dynScaleX, particle) * deltaTime;
|
2019-11-23 20:27:39 +08:00
|
|
|
dimension = particle->width + ds * _affectorScale.x;
|
|
|
|
if (dimension > 0)
|
|
|
|
{
|
|
|
|
width = dimension;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (_dynScaleYSet)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
ds = calculateScale(_dynScaleY, particle) * deltaTime;
|
2019-11-23 20:27:39 +08:00
|
|
|
dimension = particle->height + ds * _affectorScale.y;
|
|
|
|
if (dimension > 0)
|
|
|
|
{
|
|
|
|
height = dimension;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (_dynScaleZSet)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
ds = calculateScale(_dynScaleZ, particle) * deltaTime;
|
2019-11-23 20:27:39 +08:00
|
|
|
dimension = particle->depth + ds * _affectorScale.z;
|
|
|
|
if (dimension > 0)
|
|
|
|
{
|
|
|
|
depth = dimension;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
particle->setOwnDimensions(width, height, depth);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PUScaleAffector* PUScaleAffector::create()
|
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
auto psa = new PUScaleAffector();
|
2019-11-23 20:27:39 +08:00
|
|
|
psa->autorelease();
|
|
|
|
return psa;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void PUScaleAffector::copyAttributesTo(PUAffector* affector)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
PUAffector::copyAttributesTo(affector);
|
|
|
|
|
|
|
|
PUScaleAffector* scaleAffector = static_cast<PUScaleAffector*>(affector);
|
|
|
|
scaleAffector->setDynScaleX(getDynScaleX()->clone());
|
|
|
|
scaleAffector->setDynScaleY(getDynScaleY()->clone());
|
|
|
|
scaleAffector->setDynScaleZ(getDynScaleZ()->clone());
|
|
|
|
scaleAffector->setDynScaleXYZ(getDynScaleXYZ()->clone());
|
2021-12-25 10:04:45 +08:00
|
|
|
scaleAffector->_dynScaleXSet = _dynScaleXSet;
|
|
|
|
scaleAffector->_dynScaleYSet = _dynScaleYSet;
|
|
|
|
scaleAffector->_dynScaleZSet = _dynScaleZSet;
|
|
|
|
scaleAffector->_dynScaleXYZSet = _dynScaleXYZSet;
|
2019-11-23 20:27:39 +08:00
|
|
|
scaleAffector->_sinceStartSystem = _sinceStartSystem;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|