axmol/extensions/Particle3D/PU/CCPUDynamicAttribute.cpp

453 lines
16 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
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 "CCPUDynamicAttribute.h"
#include "base/ccRandom.h"
#include "platform/CCStdC.h"
NS_CC_BEGIN
//-----------------------------------------------------------------------
2021-12-25 10:04:45 +08:00
PUDynamicAttribute::PUDynamicAttribute() {}
//-----------------------------------------------------------------------
PUDynamicAttribute::~PUDynamicAttribute() {}
2019-11-23 20:27:39 +08:00
//-----------------------------------------------------------------------
2021-12-25 10:04:45 +08:00
PUDynamicAttribute::DynamicAttributeType PUDynamicAttribute::getType() const
2019-11-23 20:27:39 +08:00
{
return _type;
}
//-----------------------------------------------------------------------
2021-12-25 10:04:45 +08:00
void PUDynamicAttribute::setType(PUDynamicAttribute::DynamicAttributeType type)
2019-11-23 20:27:39 +08:00
{
_type = type;
}
//-----------------------------------------------------------------------
2021-12-25 10:04:45 +08:00
PUDynamicAttributeFixed::PUDynamicAttributeFixed() : _value(0)
2019-11-23 20:27:39 +08:00
{
_type = PUDynamicAttribute::DAT_FIXED;
}
//-----------------------------------------------------------------------
2021-12-25 10:04:45 +08:00
PUDynamicAttributeFixed::PUDynamicAttributeFixed(const PUDynamicAttributeFixed& dynamicAttributeFixed)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
_type = PUDynamicAttribute::DAT_FIXED;
2019-11-23 20:27:39 +08:00
_value = dynamicAttributeFixed._value;
}
//-----------------------------------------------------------------------
2021-12-25 10:04:45 +08:00
PUDynamicAttributeFixed::~PUDynamicAttributeFixed() {}
2019-11-23 20:27:39 +08:00
//-----------------------------------------------------------------------
2021-12-25 10:04:45 +08:00
void PUDynamicAttributeFixed::setValue(float value)
2019-11-23 20:27:39 +08:00
{
_value = value;
}
//-----------------------------------------------------------------------
2021-12-25 10:04:45 +08:00
float PUDynamicAttributeFixed::getValue(float /*x*/)
2019-11-23 20:27:39 +08:00
{
return _value;
}
2021-12-25 10:04:45 +08:00
void PUDynamicAttributeFixed::copyAttributesTo(PUDynamicAttribute* dynamicAttribute)
2019-11-23 20:27:39 +08:00
{
if (!dynamicAttribute || dynamicAttribute->getType() != PUDynamicAttribute::DAT_FIXED)
return;
PUDynamicAttributeFixed* dynAttr = static_cast<PUDynamicAttributeFixed*>(dynamicAttribute);
2021-12-25 10:04:45 +08:00
dynAttr->_value = _value;
2019-11-23 20:27:39 +08:00
}
PUDynamicAttributeFixed* PUDynamicAttributeFixed::clone()
{
2021-12-08 00:11:53 +08:00
auto af = new PUDynamicAttributeFixed();
2019-11-23 20:27:39 +08:00
this->copyAttributesTo(af);
return af;
}
//-----------------------------------------------------------------------
2021-12-25 10:04:45 +08:00
PUDynamicAttributeRandom::PUDynamicAttributeRandom() : _min(0), _max(0)
2019-11-23 20:27:39 +08:00
{
_type = PUDynamicAttribute::DAT_RANDOM;
}
//-----------------------------------------------------------------------
2021-12-25 10:04:45 +08:00
PUDynamicAttributeRandom::PUDynamicAttributeRandom(const PUDynamicAttributeRandom& dynamicAttributeRandom)
2019-11-23 20:27:39 +08:00
{
_type = PUDynamicAttribute::DAT_RANDOM;
2021-12-25 10:04:45 +08:00
_min = dynamicAttributeRandom._min;
_max = dynamicAttributeRandom._max;
2019-11-23 20:27:39 +08:00
}
//-----------------------------------------------------------------------
2021-12-25 10:04:45 +08:00
PUDynamicAttributeRandom::~PUDynamicAttributeRandom() {}
2019-11-23 20:27:39 +08:00
//-----------------------------------------------------------------------
2021-12-25 10:04:45 +08:00
float PUDynamicAttributeRandom::getMin() const
2019-11-23 20:27:39 +08:00
{
return _min;
}
//-----------------------------------------------------------------------
2021-12-25 10:04:45 +08:00
void PUDynamicAttributeRandom::setMin(float min)
2019-11-23 20:27:39 +08:00
{
_min = min;
}
//-----------------------------------------------------------------------
2021-12-25 10:04:45 +08:00
float PUDynamicAttributeRandom::getMax() const
2019-11-23 20:27:39 +08:00
{
return _max;
}
//-----------------------------------------------------------------------
2021-12-25 10:04:45 +08:00
void PUDynamicAttributeRandom::setMax(float max)
2019-11-23 20:27:39 +08:00
{
_max = max;
}
//-----------------------------------------------------------------------
2021-12-25 10:04:45 +08:00
void PUDynamicAttributeRandom::setMinMax(float min, float max)
2019-11-23 20:27:39 +08:00
{
_min = min;
_max = max;
}
//-----------------------------------------------------------------------
2021-12-25 10:04:45 +08:00
float PUDynamicAttributeRandom::getValue(float /*x*/)
2019-11-23 20:27:39 +08:00
{
return cocos2d::random(_min, _max);
}
2021-12-25 10:04:45 +08:00
void PUDynamicAttributeRandom::copyAttributesTo(PUDynamicAttribute* dynamicAttribute)
2019-11-23 20:27:39 +08:00
{
if (!dynamicAttribute || dynamicAttribute->getType() != PUDynamicAttribute::DAT_RANDOM)
return;
PUDynamicAttributeRandom* dynAttr = static_cast<PUDynamicAttributeRandom*>(dynamicAttribute);
2021-12-25 10:04:45 +08:00
dynAttr->_min = _min;
dynAttr->_max = _max;
2019-11-23 20:27:39 +08:00
}
PUDynamicAttributeRandom* PUDynamicAttributeRandom::clone()
{
2021-12-08 00:11:53 +08:00
auto ar = new PUDynamicAttributeRandom();
2019-11-23 20:27:39 +08:00
this->copyAttributesTo(ar);
return ar;
}
//-----------------------------------------------------------------------
2021-12-25 10:04:45 +08:00
PUDynamicAttributeCurved::PUDynamicAttributeCurved() : _range(0), _interpolationType(IT_LINEAR)
2019-11-23 20:27:39 +08:00
{
_type = PUDynamicAttribute::DAT_CURVED;
}
//-----------------------------------------------------------------------
2021-12-25 10:04:45 +08:00
PUDynamicAttributeCurved::PUDynamicAttributeCurved(PUInterpolationType interpolationType)
: _range(0), _interpolationType(interpolationType)
2019-11-23 20:27:39 +08:00
{
_type = PUDynamicAttribute::DAT_CURVED;
}
//-----------------------------------------------------------------------
2021-12-25 10:04:45 +08:00
PUDynamicAttributeCurved::PUDynamicAttributeCurved(const PUDynamicAttributeCurved& dynamicAttributeCurved)
: _range(dynamicAttributeCurved._range)
, _spline(dynamicAttributeCurved._spline)
, _interpolationType(dynamicAttributeCurved._interpolationType)
2019-11-23 20:27:39 +08:00
{
_type = PUDynamicAttribute::DAT_CURVED;
// Copy controlpoints
for (const auto& controlPoint : dynamicAttributeCurved._controlPoints)
{
2021-12-25 10:04:45 +08:00
_controlPoints.push_back(controlPoint);
2019-11-23 20:27:39 +08:00
}
processControlPoints();
}
//-----------------------------------------------------------------------
2021-12-25 10:04:45 +08:00
PUDynamicAttributeCurved::~PUDynamicAttributeCurved() {}
2019-11-23 20:27:39 +08:00
//-----------------------------------------------------------------------
2021-12-25 10:04:45 +08:00
void PUDynamicAttributeCurved::setInterpolationType(PUInterpolationType interpolationType)
2019-11-23 20:27:39 +08:00
{
if (interpolationType != _interpolationType)
{
// If switched to another InterpolationType, the already existing ControlPoints will be removed.
removeAllControlPoints();
_interpolationType = interpolationType;
}
}
//-----------------------------------------------------------------------
2021-12-25 10:04:45 +08:00
PUInterpolationType PUDynamicAttributeCurved::getInterpolationType() const
2019-11-23 20:27:39 +08:00
{
return _interpolationType;
}
//-----------------------------------------------------------------------
2021-12-25 10:04:45 +08:00
float PUDynamicAttributeCurved::getValue(float x)
2019-11-23 20:27:39 +08:00
{
switch (_interpolationType)
{
case IT_LINEAR:
2021-12-25 10:04:45 +08:00
{
// Search the interval in which 'x' resides and apply linear interpolation
if (_controlPoints.empty())
return 0;
ControlPointList::iterator it1 = findNearestControlPointIterator(x);
ControlPointList::iterator it2 = it1 + 1;
if (it2 != _controlPoints.end())
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
// Calculate fraction: y = y1 + ((y2 - y1) * (x - x1)/(x2 - x1))
return (*it1).y + (((*it2).y - (*it1).y) * (x - (*it1).x) / ((*it2).x - (*it1).x));
}
else
{
return (*it1).y;
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
}
break;
2019-11-23 20:27:39 +08:00
case IT_SPLINE:
2021-12-25 10:04:45 +08:00
{
// Fit using spline
if (_spline.getNumPoints() < 1)
return 0;
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
float fraction = x / _range;
return (_spline.interpolate(fraction < 1.0f ? fraction : 1.0f)).y;
}
break;
2019-11-23 20:27:39 +08:00
}
return 0;
}
//-----------------------------------------------------------------------
2021-12-25 10:04:45 +08:00
void PUDynamicAttributeCurved::addControlPoint(float x, float y)
2019-11-23 20:27:39 +08:00
{
_controlPoints.push_back(Vec2(x, y));
}
//-----------------------------------------------------------------------
2021-12-25 10:04:45 +08:00
const PUDynamicAttributeCurved::ControlPointList& PUDynamicAttributeCurved::getControlPoints() const
2019-11-23 20:27:39 +08:00
{
return _controlPoints;
}
//-----------------------------------------------------------------------
void PUDynamicAttributeCurved::processControlPoints()
{
if (_controlPoints.empty())
return;
std::sort(_controlPoints.begin(), _controlPoints.end(), PUControlPointSorter());
_range = (*getLastValidIterator()).x - (*getFirstValidIterator()).x;
if (_interpolationType == IT_SPLINE)
{
// Add all sorted control points to the spline
PUDynamicAttributeCurved::ControlPointList::iterator it;
_spline.clear();
for (it = _controlPoints.begin(); it != _controlPoints.end(); ++it)
_spline.addPoint(Vec3((*it).x, (*it).y, 0));
}
}
//-----------------------------------------------------------------------
size_t PUDynamicAttributeCurved::getNumControlPoints() const
{
return _controlPoints.size();
}
//-----------------------------------------------------------------------
void PUDynamicAttributeCurved::removeAllControlPoints()
{
_controlPoints.clear();
}
//-----------------------------------------------------------------------
PUDynamicAttributeCurved::ControlPointList::iterator PUDynamicAttributeCurved::findNearestControlPointIterator(float x)
{
// Assume that the ControlPointList is not empty
ControlPointList::iterator it;
ControlPointList::iterator itEnd = _controlPoints.end();
for (it = _controlPoints.begin(); it != itEnd; ++it)
{
if (x < (*it).x)
{
if (it == _controlPoints.begin())
return it;
else
return --it;
}
}
// If not found return the last valid iterator
return --it;
}
//-----------------------------------------------------------------------
PUDynamicAttributeCurved::ControlPointList::iterator PUDynamicAttributeCurved::getFirstValidIterator()
{
return _controlPoints.begin();
}
//-----------------------------------------------------------------------
PUDynamicAttributeCurved::ControlPointList::iterator PUDynamicAttributeCurved::getLastValidIterator()
{
return _controlPoints.end() - 1;
}
2021-12-25 10:04:45 +08:00
void PUDynamicAttributeCurved::copyAttributesTo(PUDynamicAttribute* dynamicAttribute)
2019-11-23 20:27:39 +08:00
{
if (!dynamicAttribute || dynamicAttribute->getType() != PUDynamicAttribute::DAT_CURVED)
return;
PUDynamicAttributeCurved* dynAttr = static_cast<PUDynamicAttributeCurved*>(dynamicAttribute);
2021-12-25 10:04:45 +08:00
dynAttr->_interpolationType = _interpolationType;
dynAttr->_spline = _spline;
dynAttr->_range = _range;
2019-11-23 20:27:39 +08:00
// Copy controlpoints
2021-12-25 10:04:45 +08:00
PUDynamicAttributeCurved::ControlPointList::const_iterator it; // Need const_iterator here!
2019-11-23 20:27:39 +08:00
PUDynamicAttributeCurved::ControlPointList::const_iterator itEnd = _controlPoints.end();
for (it = _controlPoints.begin(); it != itEnd; ++it)
{
Vec2 controlPoint = *it;
dynAttr->_controlPoints.push_back(controlPoint);
}
dynAttr->processControlPoints();
}
PUDynamicAttributeCurved* PUDynamicAttributeCurved::clone()
{
2021-12-08 00:11:53 +08:00
auto ac = new PUDynamicAttributeCurved();
2019-11-23 20:27:39 +08:00
this->copyAttributesTo(ac);
return ac;
}
//-----------------------------------------------------------------------
2021-12-25 10:04:45 +08:00
PUDynamicAttributeOscillate::PUDynamicAttributeOscillate()
: _oscillationType(PUDynamicAttributeOscillate::OSCT_SINE)
, _frequency(1.0f)
, _phase(0.0f)
, _base(0.0f)
, _amplitude(1.0f)
2019-11-23 20:27:39 +08:00
{
_type = PUDynamicAttribute::DAT_OSCILLATE;
}
//-----------------------------------------------------------------------
2021-12-25 10:04:45 +08:00
PUDynamicAttributeOscillate::PUDynamicAttributeOscillate(
const PUDynamicAttributeOscillate& /*dynamicAttributeOscillate*/)
2019-11-23 20:27:39 +08:00
{
_type = PUDynamicAttribute::DAT_OSCILLATE;
}
//-----------------------------------------------------------------------
2021-12-25 10:04:45 +08:00
PUDynamicAttributeOscillate::~PUDynamicAttributeOscillate() {}
2019-11-23 20:27:39 +08:00
//-----------------------------------------------------------------------
2021-12-25 10:04:45 +08:00
PUDynamicAttributeOscillate::OscillationType PUDynamicAttributeOscillate::getOscillationType() const
2019-11-23 20:27:39 +08:00
{
return _oscillationType;
}
//-----------------------------------------------------------------------
2021-12-25 10:04:45 +08:00
void PUDynamicAttributeOscillate::setOscillationType(PUDynamicAttributeOscillate::OscillationType oscillationType)
2019-11-23 20:27:39 +08:00
{
_oscillationType = oscillationType;
}
//-----------------------------------------------------------------------
2021-12-25 10:04:45 +08:00
float PUDynamicAttributeOscillate::getFrequency() const
2019-11-23 20:27:39 +08:00
{
return _frequency;
}
//-----------------------------------------------------------------------
2021-12-25 10:04:45 +08:00
void PUDynamicAttributeOscillate::setFrequency(float frequency)
2019-11-23 20:27:39 +08:00
{
_frequency = frequency;
}
//-----------------------------------------------------------------------
2021-12-25 10:04:45 +08:00
float PUDynamicAttributeOscillate::getPhase() const
2019-11-23 20:27:39 +08:00
{
return _phase;
}
//-----------------------------------------------------------------------
2021-12-25 10:04:45 +08:00
void PUDynamicAttributeOscillate::setPhase(float phase)
2019-11-23 20:27:39 +08:00
{
_phase = phase;
}
//-----------------------------------------------------------------------
2021-12-25 10:04:45 +08:00
float PUDynamicAttributeOscillate::getBase() const
2019-11-23 20:27:39 +08:00
{
return _base;
}
//-----------------------------------------------------------------------
2021-12-25 10:04:45 +08:00
void PUDynamicAttributeOscillate::setBase(float base)
2019-11-23 20:27:39 +08:00
{
_base = base;
}
//-----------------------------------------------------------------------
2021-12-25 10:04:45 +08:00
float PUDynamicAttributeOscillate::getAmplitude() const
2019-11-23 20:27:39 +08:00
{
return _amplitude;
}
//-----------------------------------------------------------------------
2021-12-25 10:04:45 +08:00
void PUDynamicAttributeOscillate::setAmplitude(float amplitude)
2019-11-23 20:27:39 +08:00
{
_amplitude = amplitude;
}
//-----------------------------------------------------------------------
2021-12-25 10:04:45 +08:00
float PUDynamicAttributeOscillate::getValue(float x)
2019-11-23 20:27:39 +08:00
{
switch (_oscillationType)
{
case OSCT_SINE:
2021-12-25 10:04:45 +08:00
{
return _base + _amplitude * sin(_phase + _frequency * x * M_PI * 2.0f);
}
break;
2019-11-23 20:27:39 +08:00
case OSCT_SQUARE:
2021-12-25 10:04:45 +08:00
{
float val = sin(_phase + _frequency * x * M_PI * 2.0f);
if (std::abs(val) < 0.00001f)
val = val > 0 ? 1 : -1;
return _base + _amplitude * val;
}
break;
2019-11-23 20:27:39 +08:00
}
return 0;
}
2021-12-25 10:04:45 +08:00
void PUDynamicAttributeOscillate::copyAttributesTo(PUDynamicAttribute* dynamicAttribute)
2019-11-23 20:27:39 +08:00
{
if (!dynamicAttribute || dynamicAttribute->getType() != PUDynamicAttribute::DAT_OSCILLATE)
return;
PUDynamicAttributeOscillate* dynAttr = static_cast<PUDynamicAttributeOscillate*>(dynamicAttribute);
2021-12-25 10:04:45 +08:00
dynAttr->_oscillationType = _oscillationType;
dynAttr->_frequency = _frequency;
dynAttr->_phase = _phase;
dynAttr->_base = _base;
dynAttr->_amplitude = _amplitude;
2019-11-23 20:27:39 +08:00
}
PUDynamicAttributeOscillate* PUDynamicAttributeOscillate::clone()
{
2021-12-08 00:11:53 +08:00
auto ao = new PUDynamicAttributeOscillate();
2019-11-23 20:27:39 +08:00
this->copyAttributesTo(ao);
return ao;
}
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
float PUDynamicAttributeHelper::calculate(PUDynamicAttribute* dyn, float x, float defaultValue)
{
if (dyn)
{
return dyn->getValue(x);
}
return defaultValue;
}
NS_CC_END