axmol/extensions/Particle3D/PU/CCPURibbonTrailRender.cpp

424 lines
14 KiB
C++
Raw Normal View History

2015-03-02 13:07:32 +08:00
/****************************************************************************
2015-03-02 16:05:26 +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
2015-03-02 13:07:32 +08:00
http://www.cocos2d-x.org
2021-12-25 10:04:45 +08:00
2015-03-02 13:07:32 +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
2015-03-02 13:07:32 +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
2015-03-02 13:07:32 +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.
****************************************************************************/
2015-03-02 16:05:26 +08:00
#include "extensions/Particle3D/CCParticleSystem3D.h"
2015-03-05 13:24:06 +08:00
#include "extensions/Particle3D/PU/CCPURibbonTrailRender.h"
#include "extensions/Particle3D/PU/CCPUUtil.h"
#include "extensions/Particle3D/PU/CCPUSimpleSpline.h"
2015-03-02 13:07:32 +08:00
#include "renderer/CCMeshCommand.h"
#include "renderer/CCRenderer.h"
#include "renderer/CCTextureCache.h"
#include "base/CCDirector.h"
#include "3d/CCSprite3D.h"
#include "3d/CCMesh.h"
#include "2d/CCCamera.h"
#include <sstream>
NS_CC_BEGIN
// Constants
2021-12-25 10:04:45 +08:00
const bool PURibbonTrailRender::DEFAULT_USE_VERTEX_COLOURS = true;
const size_t PURibbonTrailRender::DEFAULT_MAX_ELEMENTS = 10;
const float PURibbonTrailRender::DEFAULT_LENGTH = 400;
const float PURibbonTrailRender::DEFAULT_WIDTH = 5;
2015-03-05 13:24:06 +08:00
const bool PURibbonTrailRender::DEFAULT_RANDOM_INITIAL_COLOUR = true;
const Vec4 PURibbonTrailRender::DEFAULT_INITIAL_COLOUR(1, 1, 1, 1);
const Vec4 PURibbonTrailRender::DEFAULT_COLOUR_CHANGE(0.5, 0.5, 0.5, 0.5);
2021-12-26 23:26:34 +08:00
PURibbonTrailRender* PURibbonTrailRender::create(std::string_view texFile)
2015-03-02 13:07:32 +08:00
{
2021-12-08 00:11:53 +08:00
auto br = new PURibbonTrailRender();
2015-03-02 13:07:32 +08:00
br->autorelease();
br->_texFile = texFile;
return br;
}
2021-12-25 10:04:45 +08:00
void PURibbonTrailRender::render(Renderer* renderer, const Mat4& transform, ParticleSystem3D* particleSystem)
2015-03-02 13:07:32 +08:00
{
if (!_isVisible || !_trail)
return;
2021-12-25 10:04:45 +08:00
bool needDraw = false;
const ParticlePool& particlePool = particleSystem->getParticlePool();
if (!particlePool.empty())
{
2015-03-02 13:07:32 +08:00
updateParticles(particlePool);
needDraw = true;
}
2021-12-25 10:04:45 +08:00
const PUParticleSystem3D::ParticlePoolMap& emitterPool =
static_cast<PUParticleSystem3D*>(particleSystem)->getEmittedEmitterParticlePool();
if (!emitterPool.empty())
{
for (const auto& iter : emitterPool)
{
2015-03-02 13:07:32 +08:00
updateParticles(iter.second);
needDraw = true;
}
}
2021-12-25 10:04:45 +08:00
const PUParticleSystem3D::ParticlePoolMap& systemPool =
static_cast<PUParticleSystem3D*>(particleSystem)->getEmittedSystemParticlePool();
if (!systemPool.empty())
{
for (const auto& iter : systemPool)
{
2015-03-02 13:07:32 +08:00
updateParticles(iter.second);
needDraw = true;
}
}
if (needDraw)
_trail->render(renderer, transform, particleSystem);
}
2021-12-25 10:04:45 +08:00
PURibbonTrailRender::PURibbonTrailRender()
: _quota(0)
, _trail(0)
, _useVertexColours(DEFAULT_USE_VERTEX_COLOURS)
, _maxChainElements(DEFAULT_MAX_ELEMENTS)
, _trailLength(DEFAULT_LENGTH)
, _trailWidth(DEFAULT_WIDTH)
, _randomInitialColor(DEFAULT_RANDOM_INITIAL_COLOUR)
, _setLength(false)
, _setWidth(false)
, _initialColor(DEFAULT_INITIAL_COLOUR)
, _colorChange(DEFAULT_COLOUR_CHANGE)
, _childNode(0)
2015-03-02 13:07:32 +08:00
{
2021-12-25 10:04:45 +08:00
std::stringstream ss;
2015-03-02 13:07:32 +08:00
ss << this;
_ribbonTrailName = "RibbonTrail" + ss.str();
2021-12-25 10:04:45 +08:00
autoRotate = false;
2015-03-02 13:07:32 +08:00
}
2015-03-05 13:24:06 +08:00
PURibbonTrailRender::~PURibbonTrailRender()
2015-03-02 13:07:32 +08:00
{
if (!_particleSystem)
return;
destroyAll();
}
2021-12-25 10:04:45 +08:00
void PURibbonTrailRender::particleEmitted(PUParticleSystem3D* /*particleSystem*/, PUParticle3D* /*particle*/)
2015-03-02 13:07:32 +08:00
{
// Assigning visual data already done in _updateRenderQueue()
}
2021-12-25 10:04:45 +08:00
void PURibbonTrailRender::particleExpired(PUParticleSystem3D* /*particleSystem*/, PUParticle3D* particle)
2015-03-02 13:07:32 +08:00
{
if (particle->visualData)
{
2021-12-25 10:04:45 +08:00
PURibbonTrailVisualData* ribbonTrailRendererVisualData =
static_cast<PURibbonTrailVisualData*>(particle->visualData);
2015-03-02 13:07:32 +08:00
ribbonTrailRendererVisualData->setVisible(false);
}
}
//-----------------------------------------------------------------------
bool PURibbonTrailRender::isUseVertexColors() const
2015-03-02 13:07:32 +08:00
{
return _useVertexColours;
2021-12-25 10:04:45 +08:00
}
2015-03-02 13:07:32 +08:00
//-----------------------------------------------------------------------
2015-03-05 13:24:06 +08:00
void PURibbonTrailRender::setUseVertexColors(bool useVertexColours)
2015-03-02 13:07:32 +08:00
{
_useVertexColours = useVertexColours;
2021-12-25 10:04:45 +08:00
}
2015-03-02 13:07:32 +08:00
//-----------------------------------------------------------------------
size_t PURibbonTrailRender::getMaxChainElements() const
2015-03-02 13:07:32 +08:00
{
return _maxChainElements;
2021-12-25 10:04:45 +08:00
}
2015-03-02 13:07:32 +08:00
//-----------------------------------------------------------------------
2015-03-05 13:24:06 +08:00
void PURibbonTrailRender::setMaxChainElements(size_t maxChainElements)
2015-03-02 13:07:32 +08:00
{
_maxChainElements = maxChainElements;
2021-12-25 10:04:45 +08:00
}
2015-03-02 13:07:32 +08:00
//-----------------------------------------------------------------------
float PURibbonTrailRender::getTrailLength() const
2015-03-02 13:07:32 +08:00
{
return _trailLength;
2021-12-25 10:04:45 +08:00
}
2015-03-02 13:07:32 +08:00
//-----------------------------------------------------------------------
2015-03-05 13:24:06 +08:00
void PURibbonTrailRender::setTrailLength(float trailLength)
2015-03-02 13:07:32 +08:00
{
_trailLength = trailLength;
2021-12-25 10:04:45 +08:00
_setLength = true;
}
2015-03-02 13:07:32 +08:00
//-----------------------------------------------------------------------
float PURibbonTrailRender::getTrailWidth() const
2015-03-02 13:07:32 +08:00
{
return _trailWidth;
2021-12-25 10:04:45 +08:00
}
2015-03-02 13:07:32 +08:00
//-----------------------------------------------------------------------
2015-03-05 13:24:06 +08:00
void PURibbonTrailRender::setTrailWidth(float trailWidth)
2015-03-02 13:07:32 +08:00
{
_trailWidth = trailWidth;
2021-12-25 10:04:45 +08:00
_setWidth = true;
}
2015-03-02 13:07:32 +08:00
//-----------------------------------------------------------------------
bool PURibbonTrailRender::isRandomInitialColor() const
2015-03-02 13:07:32 +08:00
{
return _randomInitialColor;
2021-12-25 10:04:45 +08:00
}
2015-03-02 13:07:32 +08:00
//-----------------------------------------------------------------------
2015-03-05 13:24:06 +08:00
void PURibbonTrailRender::setRandomInitialColor(bool randomInitialColour)
2015-03-02 13:07:32 +08:00
{
_randomInitialColor = randomInitialColour;
2021-12-25 10:04:45 +08:00
}
2015-03-02 13:07:32 +08:00
//-----------------------------------------------------------------------
const Vec4& PURibbonTrailRender::getInitialColor() const
2015-03-02 13:07:32 +08:00
{
return _initialColor;
2021-12-25 10:04:45 +08:00
}
2015-03-02 13:07:32 +08:00
//-----------------------------------------------------------------------
2015-03-05 13:24:06 +08:00
void PURibbonTrailRender::setInitialColor(const Vec4& initialColour)
2015-03-02 13:07:32 +08:00
{
_initialColor = initialColour;
2021-12-25 10:04:45 +08:00
}
2015-03-02 13:07:32 +08:00
//-----------------------------------------------------------------------
const Vec4& PURibbonTrailRender::getColorChange() const
2015-03-02 13:07:32 +08:00
{
return _colorChange;
2021-12-25 10:04:45 +08:00
}
2015-03-02 13:07:32 +08:00
//-----------------------------------------------------------------------
2015-03-05 13:24:06 +08:00
void PURibbonTrailRender::setColorChange(const Vec4& colourChange)
2015-03-02 13:07:32 +08:00
{
_colorChange = colourChange;
2021-12-25 10:04:45 +08:00
}
2015-03-02 13:07:32 +08:00
//-----------------------------------------------------------------------
2015-03-05 13:24:06 +08:00
void PURibbonTrailRender::prepare()
2015-03-02 13:07:32 +08:00
{
if (!_particleSystem)
return;
// Register itself to the technique
if (_particleSystem)
{
2021-12-25 10:04:45 +08:00
// Although it is safe to assume that technique == mParentTechnique, use the mParentTechnique, because the
// mParentTechnique is also used for unregistering.
static_cast<PUParticleSystem3D*>(_particleSystem)->addListener(this);
2015-03-02 13:07:32 +08:00
}
_quota = _particleSystem->getParticleQuota();
if (!_childNode)
{
// Create a childnode if not created earlier
2021-12-25 10:04:45 +08:00
std::stringstream ss;
2015-03-02 13:07:32 +08:00
ss << this;
std::string childNodeNodeName = "ParticleUniverse" + ss.str();
2021-12-25 10:04:45 +08:00
//_childNode =
2021-12-28 11:00:34 +08:00
// mParentTechnique->getParentSystem()->getParentSceneNode()->createChildSceneNode(childNodeNodeName);
2015-03-02 13:07:32 +08:00
//_childNode->setInheritOrientation(false);
auto parent = _particleSystem->getParent();
if (parent)
{
_childNode = Node::create();
parent->addChild(_childNode);
}
}
if (_childNode)
{
// Create RibbonTrail
2021-12-25 10:04:45 +08:00
// Ogre::SceneManager* sceneManager = mParentTechnique->getParentSystem()->getSceneManager();
2021-12-08 00:11:53 +08:00
_trail = new PURibbonTrail(_ribbonTrailName, _texFile);
2015-03-02 13:07:32 +08:00
_trail->setNumberOfChains(_quota);
_trail->setMaxChainElements(_maxChainElements);
if (_setLength)
{
_trail->setTrailLength(_rendererScale.y * _trailLength);
}
else
{
2021-12-25 10:04:45 +08:00
_trail->setTrailLength(_rendererScale.y *
static_cast<PUParticleSystem3D*>(_particleSystem)->getDefaultHeight());
2015-03-02 13:07:32 +08:00
}
_trail->setUseVertexColours(_useVertexColours);
// Create number of VisualData objects
std::string sceneNodeName;
2021-12-25 10:04:45 +08:00
std::stringstream ss;
2015-03-02 13:07:32 +08:00
ss << this;
for (size_t i = 0; i < _quota; i++)
{
2021-12-25 10:04:45 +08:00
// sceneNodeName = "ParticleUniverse" + ss.str() + StringConverter::toString(i);
// RibbonTrailRendererVisualData* visualData =
// PU_NEW_T(RibbonTrailRendererVisualData,
2021-12-28 11:00:34 +08:00
// MEMCATEGORY_SCENE_OBJECTS)(_childNode->createChildSceneNode(sceneNodeName), _trail);
2021-12-25 10:04:45 +08:00
// visualData->node->setInheritOrientation(false);
2015-03-02 13:07:32 +08:00
auto node = Node::create();
_childNode->addChild(node);
2021-12-08 00:11:53 +08:00
PURibbonTrailVisualData* visualData = new PURibbonTrailVisualData(node, _trail);
2021-12-25 10:04:45 +08:00
visualData->index = i;
_allVisualData.push_back(visualData); // Managed by this renderer
_visualData.push_back(visualData); // Used to assign to a particle
2015-03-02 13:07:32 +08:00
if (_randomInitialColor)
{
_trail->setInitialColour(i, CCRANDOM_0_1(), CCRANDOM_0_1(), CCRANDOM_0_1());
}
else
{
_trail->setInitialColour(i, _initialColor);
}
_trail->setColourChange(i, _colorChange);
if (_setWidth)
{
_trail->setInitialWidth(i, _rendererScale.x * _trailWidth);
}
else
{
2021-12-25 10:04:45 +08:00
_trail->setInitialWidth(
i, _rendererScale.x * static_cast<PUParticleSystem3D*>(_particleSystem)->getDefaultWidth());
2015-03-02 13:07:32 +08:00
}
}
//_childNode->attachObject(_trail);
_trail->setAttachedNode(_childNode);
_trail->setDepthTest(_depthTest);
_trail->setDepthWrite(_depthWrite);
}
}
2015-03-05 13:24:06 +08:00
void PURibbonTrailRender::unPrepare()
2015-03-02 13:07:32 +08:00
{
destroyAll();
}
2021-12-25 10:04:45 +08:00
void PURibbonTrailRender::updateRender(PUParticle3D* /*particle*/, float deltaTime, bool /*firstParticle*/)
2015-03-02 13:07:32 +08:00
{
if (_trail)
_trail->update(deltaTime);
}
//-----------------------------------------------------------------------
void PURibbonTrailRender::destroyAll()
2015-03-02 13:07:32 +08:00
{
if (!_particleSystem || !_trail || !_childNode)
return;
// Remove the listener
2021-12-25 10:04:45 +08:00
static_cast<PUParticleSystem3D*>(_particleSystem)->removeListener(this);
2015-03-02 13:07:32 +08:00
// Detach the Ribbontrail
if (_childNode && _trail)
{
_trail->setAttachedNode(nullptr);
//_childNode->detachObject(_trail);
}
// Delete the Ribbontrail
CC_SAFE_DELETE(_trail);
// Delete the visual data
2015-03-05 13:24:06 +08:00
std::vector<PURibbonTrailVisualData*>::const_iterator it;
std::vector<PURibbonTrailVisualData*>::const_iterator itEnd = _allVisualData.end();
2015-03-02 13:07:32 +08:00
for (it = _allVisualData.begin(); it != itEnd; ++it)
{
delete *it;
}
_allVisualData.clear();
_visualData.clear();
// Destroy the children of the childnode, but not the childnode itself, because this gives a delete-order problem
if (_childNode)
{
//_childNode->removeAndDestroyAllChildren();
_childNode->removeAllChildren();
if (_childNode->getParent())
_childNode->getParent()->removeChild(_childNode);
_childNode = nullptr;
}
}
2021-12-25 10:04:45 +08:00
void PURibbonTrailRender::notifyRescaled(const Vec3& scale)
2015-03-02 13:07:32 +08:00
{
2015-03-05 13:24:06 +08:00
PURender::notifyRescaled(scale);
2015-03-02 13:07:32 +08:00
if (_trail)
{
// Use the average length (is there a good alternative?
_trail->setTrailLength(scale.y * _trailLength);
size_t numberOfChains = _trail->getNumberOfChains();
for (size_t i = 0; i < numberOfChains; ++i)
{
_trail->setInitialWidth(i, scale.x * _trailWidth);
}
}
}
2015-03-05 13:24:06 +08:00
PURibbonTrailRender* PURibbonTrailRender::clone()
2015-03-02 13:07:32 +08:00
{
2015-03-05 13:24:06 +08:00
auto tr = PURibbonTrailRender::create(_texFile);
2015-03-02 13:07:32 +08:00
copyAttributesTo(tr);
return tr;
}
2021-12-25 10:04:45 +08:00
void PURibbonTrailRender::copyAttributesTo(PURibbonTrailRender* trailRender)
2015-03-02 13:07:32 +08:00
{
PURender::copyAttributesTo(trailRender);
2015-03-02 13:07:32 +08:00
trailRender->setUseVertexColors(_useVertexColours);
trailRender->setMaxChainElements(_maxChainElements);
trailRender->setTrailLength(_trailLength);
trailRender->setTrailWidth(_trailWidth);
trailRender->setRandomInitialColor(_randomInitialColor);
trailRender->setInitialColor(_initialColor);
trailRender->setColorChange(_colorChange);
}
2021-12-25 10:04:45 +08:00
void PURibbonTrailRender::updateParticles(const ParticlePool& pool)
2015-03-02 13:07:32 +08:00
{
2015-03-05 13:24:06 +08:00
PURibbonTrailVisualData* visualData = nullptr;
2015-03-02 13:07:32 +08:00
for (auto iter : pool.getActiveDataList())
{
2021-12-25 10:04:45 +08:00
auto particle = static_cast<PUParticle3D*>(iter);
2015-03-02 13:07:32 +08:00
if (!particle->visualData && !_visualData.empty())
{
particle->visualData = _visualData.back();
_visualData.pop_back();
}
2015-03-05 13:24:06 +08:00
visualData = static_cast<PURibbonTrailVisualData*>(particle->visualData);
2015-03-02 13:07:32 +08:00
if (visualData)
{
Node* node = visualData->node;
node->setPosition3D(particle->position);
// Set the width of the trail if required
if (particle->particleType == PUParticle3D::PT_VISUAL)
{
if (particle->ownDimensions)
{
_trail->setInitialWidth(visualData->index, particle->width);
}
}
visualData->setVisible(true);
}
}
}
NS_CC_END