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-07-09 22:23:34 +08:00
|
|
|
https://axis-project.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 "CCPUCircleEmitter.h"
|
|
|
|
#include "extensions/Particle3D/PU/CCPUParticleSystem3D.h"
|
|
|
|
#include "extensions/Particle3D/PU/CCPUUtil.h"
|
|
|
|
#include "base/ccRandom.h"
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
// Constants
|
|
|
|
const float PUCircleEmitter::DEFAULT_RADIUS = 100.0f;
|
2021-12-25 10:04:45 +08:00
|
|
|
const float PUCircleEmitter::DEFAULT_STEP = 0.1f;
|
|
|
|
const float PUCircleEmitter::DEFAULT_ANGLE = 0.0f;
|
|
|
|
const bool PUCircleEmitter::DEFAULT_RANDOM = true;
|
2019-11-23 20:27:39 +08:00
|
|
|
const Vec3 PUCircleEmitter::DEFAULT_NORMAL(0, 0, 0);
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------
|
2021-12-25 10:04:45 +08:00
|
|
|
PUCircleEmitter::PUCircleEmitter()
|
|
|
|
: PUEmitter()
|
|
|
|
, _radius(DEFAULT_RADIUS)
|
|
|
|
, _circleAngle(DEFAULT_ANGLE)
|
|
|
|
, _originalCircleAngle(DEFAULT_ANGLE)
|
|
|
|
, _step(DEFAULT_STEP)
|
|
|
|
, _x(0.0f)
|
|
|
|
, _z(0.0f)
|
|
|
|
, _random(DEFAULT_RANDOM)
|
|
|
|
, _orientation()
|
|
|
|
, _normal(DEFAULT_NORMAL)
|
|
|
|
{}
|
2019-11-23 20:27:39 +08:00
|
|
|
//-----------------------------------------------------------------------
|
|
|
|
float PUCircleEmitter::getRadius() const
|
|
|
|
{
|
|
|
|
return _radius;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
|
|
|
void PUCircleEmitter::setRadius(const float radius)
|
|
|
|
{
|
|
|
|
_radius = radius;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
|
|
|
float PUCircleEmitter::getCircleAngle() const
|
|
|
|
{
|
|
|
|
return _originalCircleAngle;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
|
|
|
void PUCircleEmitter::setCircleAngle(const float circleAngle)
|
|
|
|
{
|
|
|
|
_originalCircleAngle = circleAngle;
|
2021-12-25 10:04:45 +08:00
|
|
|
_circleAngle = circleAngle;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
|
|
|
float PUCircleEmitter::getStep() const
|
|
|
|
{
|
|
|
|
return _step;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
|
|
|
void PUCircleEmitter::setStep(const float step)
|
|
|
|
{
|
|
|
|
_step = step;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
|
|
|
bool PUCircleEmitter::isRandom() const
|
|
|
|
{
|
|
|
|
return _random;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
|
|
|
void PUCircleEmitter::setRandom(const bool random)
|
|
|
|
{
|
|
|
|
_random = random;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
|
|
|
const Quaternion& PUCircleEmitter::getOrientation() const
|
|
|
|
{
|
|
|
|
return _orientation;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
//-----------------------------------------------------------------------
|
2019-11-23 20:27:39 +08:00
|
|
|
const Vec3& PUCircleEmitter::getNormal() const
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
return _normal;
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
|
|
|
void PUCircleEmitter::setNormal(const Vec3& normal)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
//_orientation = Vec3::UNIT_Y.getRotationTo(normal, Vec3::UNIT_X);
|
|
|
|
_orientation = getRotationTo(Vec3::UNIT_Y, normal, Vec3::UNIT_X);
|
2021-12-25 10:04:45 +08:00
|
|
|
_normal = normal;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
2021-12-25 10:04:45 +08:00
|
|
|
void PUCircleEmitter::notifyStart()
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
// Reset the attributes to allow a restart.
|
|
|
|
_circleAngle = _originalCircleAngle;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
//-----------------------------------------------------------------------
|
2019-11-23 20:27:39 +08:00
|
|
|
void PUCircleEmitter::initParticlePosition(PUParticle3D* particle)
|
|
|
|
{
|
|
|
|
float angle = 0;
|
|
|
|
if (_random)
|
|
|
|
{
|
|
|
|
// Choose a random position on the circle.
|
|
|
|
angle = cocos2d::random(0.0, M_PI * 2.0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Follow the contour of the circle.
|
|
|
|
_circleAngle += _step;
|
|
|
|
_circleAngle = _circleAngle > M_PI * 2.0f ? _circleAngle - (M_PI * 2.0) : _circleAngle;
|
2021-12-25 10:04:45 +08:00
|
|
|
angle = _circleAngle;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
_x = cosf(angle);
|
|
|
|
_z = sinf(angle);
|
2021-12-25 10:04:45 +08:00
|
|
|
// ParticleSystem* sys = mParentTechnique->getParentSystem();
|
|
|
|
// if (sys)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
// Take both orientation of the node and its own orientation, based on the normal, into account
|
|
|
|
Mat4 rotMat;
|
2021-12-25 10:04:45 +08:00
|
|
|
Mat4::createRotation(static_cast<PUParticleSystem3D*>(_particleSystem)->getDerivedOrientation() * _orientation,
|
|
|
|
&rotMat);
|
|
|
|
particle->position = getDerivedPosition() +
|
|
|
|
/*sys->getDerivedOrientation() * */ rotMat *
|
|
|
|
(Vec3(_x * _radius * _emitterScale.x, 0, _z * _radius * _emitterScale.z));
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
// else
|
2019-11-23 20:27:39 +08:00
|
|
|
//{
|
2021-12-25 10:04:45 +08:00
|
|
|
// particle->position = getDerivedPosition() + _emitterScale * ( mOrientation * Vec3(mX * mRadius, 0, mZ * mRadius)
|
|
|
|
//);
|
|
|
|
// }
|
2019-11-23 20:27:39 +08:00
|
|
|
particle->originalPosition = particle->position;
|
|
|
|
}
|
|
|
|
//-----------------------------------------------------------------------
|
|
|
|
void PUCircleEmitter::initParticleDirection(PUParticle3D* particle)
|
|
|
|
{
|
|
|
|
if (_autoDirection)
|
|
|
|
{
|
|
|
|
// The value of the direction vector that has been set does not have a meaning for
|
|
|
|
// the circle emitter.
|
|
|
|
float angle = 0.0f;
|
|
|
|
generateAngle(angle);
|
|
|
|
if (angle != 0.0f)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
// particle->direction = (mOrientation * Vec3(mX, 0, mZ) ).randomDeviant(angle, mUpVector);
|
2019-11-23 20:27:39 +08:00
|
|
|
Mat4 mat;
|
|
|
|
Mat4::createRotation(_orientation, &mat);
|
|
|
|
Vec3 temp = mat * Vec3(_x, 0, _z);
|
|
|
|
|
|
|
|
particle->direction = PUUtil::randomDeviant(temp, angle, _upVector);
|
|
|
|
|
|
|
|
particle->originalDirection = particle->direction;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Mat4 rotMat;
|
|
|
|
Mat4::createRotation(_orientation, &rotMat);
|
|
|
|
particle->direction = rotMat * Vec3(_x, 0, _z);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Use the standard way
|
|
|
|
PUEmitter::initParticleDirection(particle);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PUCircleEmitter* PUCircleEmitter::create()
|
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
auto pe = new PUCircleEmitter();
|
2019-11-23 20:27:39 +08:00
|
|
|
pe->autorelease();
|
|
|
|
return pe;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
cocos2d::Quaternion PUCircleEmitter::getRotationTo(const Vec3& src,
|
|
|
|
const Vec3& dest,
|
|
|
|
const Vec3& fallbackAxis /*= Vec3::ZERO*/) const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
// Based on Stan Melax's article in Game Programming Gems
|
|
|
|
Quaternion q;
|
|
|
|
// Copy, since cannot modify local
|
|
|
|
Vec3 v0 = src;
|
|
|
|
Vec3 v1 = dest;
|
|
|
|
v0.normalize();
|
|
|
|
v1.normalize();
|
|
|
|
|
|
|
|
float d = v0.dot(v1);
|
|
|
|
// If dot == 1, vectors are the same
|
|
|
|
if (d >= 1.0f)
|
|
|
|
{
|
|
|
|
return Quaternion();
|
|
|
|
}
|
|
|
|
if (d < (1e-6f - 1.0f))
|
|
|
|
{
|
|
|
|
if (fallbackAxis != Vec3::ZERO)
|
|
|
|
{
|
|
|
|
// rotate 180 degrees about the fallback axis
|
|
|
|
q.set(fallbackAxis, (float)M_PI);
|
2021-12-25 10:04:45 +08:00
|
|
|
// q.FromAngleAxis(Radian(Math::PI), fallbackAxis);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Generate an axis
|
2021-12-25 10:04:45 +08:00
|
|
|
Vec3 axis /* = Vec3::UNIT_X.crossProduct(*this)*/;
|
2019-11-23 20:27:39 +08:00
|
|
|
Vec3::cross(Vec3::UNIT_X, src, &axis);
|
2021-12-25 10:04:45 +08:00
|
|
|
if (axis.lengthSquared() < (1e-06 * 1e-06)) // pick another if colinear
|
|
|
|
// axis = Vec3::UNIT_Y.crossProduct(*this);
|
2019-11-23 20:27:39 +08:00
|
|
|
Vec3::cross(Vec3::UNIT_Y, src, &axis);
|
|
|
|
axis.normalize();
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
// q.FromAngleAxis(Radian(Math::PI), axis);
|
2019-11-23 20:27:39 +08:00
|
|
|
q.set(axis, (float)M_PI);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*float s = Math::Sqrt( (1+d)*2 );*/
|
2021-12-25 10:04:45 +08:00
|
|
|
float s = sqrtf((1 + d) * 2);
|
2019-11-23 20:27:39 +08:00
|
|
|
float invs = 1 / s;
|
|
|
|
|
|
|
|
Vec3 c /*= v0.crossProduct(v1)*/;
|
|
|
|
Vec3::cross(v0, v1, &c);
|
|
|
|
|
|
|
|
q.x = c.x * invs;
|
|
|
|
q.y = c.y * invs;
|
|
|
|
q.z = c.z * invs;
|
|
|
|
q.w = s * 0.5f;
|
|
|
|
q.normalize();
|
|
|
|
}
|
|
|
|
return q;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void PUCircleEmitter::copyAttributesTo(PUEmitter* emitter)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
PUEmitter::copyAttributesTo(emitter);
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
PUCircleEmitter* circleEmitter = static_cast<PUCircleEmitter*>(emitter);
|
|
|
|
circleEmitter->_radius = _radius;
|
|
|
|
circleEmitter->_circleAngle = _circleAngle;
|
2019-11-23 20:27:39 +08:00
|
|
|
circleEmitter->_originalCircleAngle = _originalCircleAngle;
|
2021-12-25 10:04:45 +08:00
|
|
|
circleEmitter->_step = _step;
|
|
|
|
circleEmitter->_random = _random;
|
|
|
|
circleEmitter->_normal = _normal;
|
|
|
|
circleEmitter->_orientation = _orientation;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
PUCircleEmitter* PUCircleEmitter::clone()
|
|
|
|
{
|
|
|
|
auto be = PUCircleEmitter::create();
|
|
|
|
copyAttributesTo(be);
|
|
|
|
return be;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|