axmol/extensions/Particle3D/PU/CCPUForceField.cpp

360 lines
14 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 "CCPUForceField.h"
NS_CC_BEGIN
const Vec3 PUForceFieldCalculationFactory::DEFAULT_WORLDSIZE(500.0f, 500.0f, 500.0f);
//-----------------------------------------------------------------------
unsigned short PUForceFieldCalculationFactory::getOctaves() const
{
return _octaves;
}
//-----------------------------------------------------------------------
void PUForceFieldCalculationFactory::setOctaves(unsigned short octaves)
{
_octaves = octaves;
generate(getForceFieldSize(), _octaves, _frequency, _amplitude, _persistence, _worldSize);
}
//-----------------------------------------------------------------------
double PUForceFieldCalculationFactory::getFrequency() const
{
return _frequency;
}
//-----------------------------------------------------------------------
void PUForceFieldCalculationFactory::setFrequency(double frequency)
{
_frequency = frequency;
generate(getForceFieldSize(), _octaves, _frequency, _amplitude, _persistence, _worldSize);
}
//-----------------------------------------------------------------------
double PUForceFieldCalculationFactory::getAmplitude() const
{
return _amplitude;
}
//-----------------------------------------------------------------------
void PUForceFieldCalculationFactory::setAmplitude(double amplitude)
{
_amplitude = amplitude;
generate(getForceFieldSize(), _octaves, _frequency, _amplitude, _persistence, _worldSize);
}
//-----------------------------------------------------------------------
double PUForceFieldCalculationFactory::getPersistence() const
{
return _persistence;
}
//-----------------------------------------------------------------------
void PUForceFieldCalculationFactory::setPersistence(double persistence)
{
_persistence = persistence;
generate(getForceFieldSize(), _octaves, _frequency, _amplitude, _persistence, _worldSize);
}
//-----------------------------------------------------------------------
unsigned int PUForceFieldCalculationFactory::getForceFieldSize() const
{
2021-12-25 10:04:45 +08:00
return 1; // Return default cubic size
2019-11-23 20:27:39 +08:00
}
//-----------------------------------------------------------------------
void PUForceFieldCalculationFactory::setForceFieldSize(unsigned int forceFieldSize)
{
// The forcefield cannot be zero
if (forceFieldSize == 0)
return;
generate(forceFieldSize, _octaves, _frequency, _amplitude, _persistence, _worldSize);
}
//-----------------------------------------------------------------------
Vec3 PUForceFieldCalculationFactory::getWorldSize() const
{
return _worldSize;
}
//-----------------------------------------------------------------------
void PUForceFieldCalculationFactory::setWorldSize(const Vec3& worldSize)
{
// The worldsize cannot be zero
if (worldSize == Vec3::ZERO)
return;
_worldSize = worldSize;
generate(getForceFieldSize(), _octaves, _frequency, _amplitude, _persistence, _worldSize);
}
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
void PURealTimeForceFieldCalculationFactory::generate(unsigned int /*forceFieldSize*/,
2021-12-25 10:04:45 +08:00
unsigned short octaves,
double frequency,
double amplitude,
double persistence,
const Vec3& worldSize)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
_octaves = octaves;
_frequency = frequency;
_amplitude = amplitude;
2019-11-23 20:27:39 +08:00
_persistence = persistence;
2021-12-25 10:04:45 +08:00
_worldSize = worldSize;
2019-11-23 20:27:39 +08:00
_noise3D.initialise(octaves, frequency, amplitude, persistence);
if (worldSize != Vec3::ZERO)
{
2021-12-25 10:04:45 +08:00
_mapScale.x = 1.0f / worldSize.x; // Remark: forceFieldSize is not used, because it is a unit cube
2019-11-23 20:27:39 +08:00
_mapScale.y = 1.0f / worldSize.y;
_mapScale.z = 1.0f / worldSize.z;
}
}
//-----------------------------------------------------------------------
void PURealTimeForceFieldCalculationFactory::determineForce(const Vec3& position, Vec3& force, float delta)
{
_mappedPosition.x = _mapScale.x * position.x;
_mappedPosition.y = _mapScale.y * position.y;
_mappedPosition.z = _mapScale.z * position.z;
2021-12-25 10:04:45 +08:00
if (_mappedPosition.x < 0.0f || _mappedPosition.x > 1.0f || _mappedPosition.y < 0.0f || _mappedPosition.y > 1.0f ||
2019-11-23 20:27:39 +08:00
_mappedPosition.z < 0.0f || _mappedPosition.z > 1.0f)
{
// Position is outside the forcefield (outside the unit cube)
return;
}
2021-12-25 10:04:45 +08:00
force.x = (float)(_noise3D.noise(_mappedPosition.x + delta, _mappedPosition.y, _mappedPosition.z) -
_noise3D.noise(_mappedPosition.x - delta, _mappedPosition.y, _mappedPosition.z));
force.y = (float)(_noise3D.noise(_mappedPosition.x, _mappedPosition.y + delta, _mappedPosition.z) -
_noise3D.noise(_mappedPosition.x, _mappedPosition.y - delta, _mappedPosition.z));
force.z = (float)(_noise3D.noise(_mappedPosition.x, _mappedPosition.y, _mappedPosition.z + delta) -
_noise3D.noise(_mappedPosition.x, _mappedPosition.y, _mappedPosition.z - delta));
2019-11-23 20:27:39 +08:00
}
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
2021-12-25 10:04:45 +08:00
PUForceField::PUForceField()
: _octaves(2)
, _frequency(1.0f)
, _amplitude(1.0f)
, _persistence(1.0f)
, _worldSize(PUForceFieldCalculationFactory::DEFAULT_WORLDSIZE)
, _forceFieldSize(64)
, _forceFieldCalculationFactory(0)
, _forceFieldType(FF_REALTIME_CALC)
{}
2019-11-23 20:27:39 +08:00
//-----------------------------------------------------------------------
PUForceField::~PUForceField()
{
if (_forceFieldCalculationFactory)
{
delete _forceFieldCalculationFactory;
}
}
//-----------------------------------------------------------------------
void PUForceField::initialise(ForceFieldType type,
2021-12-25 10:04:45 +08:00
const Vec3& position,
unsigned int forceFieldSize,
unsigned short octaves,
double frequency,
double amplitude,
double persistence,
const Vec3& worldSize)
2019-11-23 20:27:39 +08:00
{
// Initialise first
initialise(type, forceFieldSize, octaves, frequency, amplitude, persistence, worldSize);
// Store the base and max position of the forcefield.
_forceFieldPositionBase = position;
_forceFieldPositionBase.x -= 0.5f * worldSize.x;
_forceFieldPositionBase.y -= 0.5f * worldSize.y;
_forceFieldPositionBase.z -= 0.5f * worldSize.z;
}
//-----------------------------------------------------------------------
void PUForceField::initialise(ForceFieldType type,
2021-12-25 10:04:45 +08:00
unsigned int forceFieldSize,
unsigned short octaves,
double frequency,
double amplitude,
double persistence,
const Vec3& worldSize)
2019-11-23 20:27:39 +08:00
{
// Create a factory
_forceFieldCalculationFactory = createForceFieldCalculationFactory(type);
// Create all necessary data for the force field
_forceFieldCalculationFactory->generate(forceFieldSize, octaves, frequency, amplitude, persistence, worldSize);
}
//-----------------------------------------------------------------------
const Vec3& PUForceField::getForceFieldPositionBase() const
{
return _forceFieldPositionBase;
}
//-----------------------------------------------------------------------
void PUForceField::setForceFieldPositionBase(const Vec3& position)
{
_forceFieldPositionBase = position;
}
//-----------------------------------------------------------------------
void PUForceField::determineForce(const Vec3& position, Vec3& force, float delta)
{
force.x = 0.0f;
force.y = 0.0f;
force.z = 0.0f;
if (_forceFieldCalculationFactory)
{
_forceFieldCalculationFactory->determineForce(position - _forceFieldPositionBase, force, delta);
}
}
//-----------------------------------------------------------------------
PUForceFieldCalculationFactory* PUForceField::getForceFieldCalculationFactory() const
{
return _forceFieldCalculationFactory;
}
//-----------------------------------------------------------------------
void PUForceField::setForceFieldCalculationFactory(PUForceFieldCalculationFactory* forceFieldCalculationFactory)
{
if (_forceFieldCalculationFactory)
{
delete _forceFieldCalculationFactory;
}
_forceFieldCalculationFactory = forceFieldCalculationFactory;
}
//-----------------------------------------------------------------------
PUForceFieldCalculationFactory* PUForceField::createForceFieldCalculationFactory(ForceFieldType type)
{
_forceFieldType = type;
if (type == FF_MATRIX_CALC)
{
// Use precreated matrix
2021-12-25 10:04:45 +08:00
// setForceFieldCalculationFactory(new MatrixForceFieldCalculationFactory());
2019-11-23 20:27:39 +08:00
return getForceFieldCalculationFactory();
}
else
{
// Use realtime calculation
2021-12-08 00:11:53 +08:00
setForceFieldCalculationFactory(new PURealTimeForceFieldCalculationFactory());
2019-11-23 20:27:39 +08:00
return getForceFieldCalculationFactory();
}
}
//-----------------------------------------------------------------------
PUForceField::ForceFieldType PUForceField::getForceFieldType() const
{
return _forceFieldType;
}
//-----------------------------------------------------------------------
void PUForceField::setForceFieldType(const PUForceField::ForceFieldType forceFieldType)
{
_forceFieldType = forceFieldType;
if (_forceFieldCalculationFactory)
{
initialise(_forceFieldType, _forceFieldSize, _octaves, _frequency, _amplitude, _persistence, _worldSize);
}
}
//-----------------------------------------------------------------------
unsigned short PUForceField::getOctaves() const
{
return _octaves;
}
//-----------------------------------------------------------------------
void PUForceField::setOctaves(unsigned short octaves)
{
_octaves = octaves;
if (_forceFieldCalculationFactory)
{
_forceFieldCalculationFactory->setOctaves(octaves);
}
}
//-----------------------------------------------------------------------
double PUForceField::getFrequency() const
{
return _frequency;
}
//-----------------------------------------------------------------------
void PUForceField::setFrequency(double frequency)
{
_frequency = frequency;
if (_forceFieldCalculationFactory)
{
_forceFieldCalculationFactory->setFrequency(frequency);
}
}
//-----------------------------------------------------------------------
double PUForceField::getAmplitude() const
{
return _amplitude;
}
//-----------------------------------------------------------------------
void PUForceField::setAmplitude(double amplitude)
{
_amplitude = amplitude;
if (_forceFieldCalculationFactory)
{
_forceFieldCalculationFactory->setAmplitude(amplitude);
}
}
//-----------------------------------------------------------------------
double PUForceField::getPersistence() const
{
return _persistence;
}
//-----------------------------------------------------------------------
void PUForceField::setPersistence(double persistence)
{
_persistence = persistence;
if (_forceFieldCalculationFactory)
{
_forceFieldCalculationFactory->setPersistence(persistence);
}
}
//-----------------------------------------------------------------------
unsigned int PUForceField::getForceFieldSize() const
{
return _forceFieldSize;
}
//-----------------------------------------------------------------------
void PUForceField::setForceFieldSize(unsigned int forceFieldSize)
{
_forceFieldSize = forceFieldSize;
if (_forceFieldCalculationFactory)
{
_forceFieldCalculationFactory->setForceFieldSize(forceFieldSize);
}
}
//-----------------------------------------------------------------------
Vec3 PUForceField::getWorldSize() const
{
return _worldSize;
}
//-----------------------------------------------------------------------
void PUForceField::setWorldSize(const Vec3& worldSize)
{
_worldSize = worldSize;
if (_forceFieldCalculationFactory)
{
_forceFieldCalculationFactory->setWorldSize(worldSize);
}
}
NS_CC_END