axmol/extensions/Particle3D/PU/CCPUBeamRender.cpp

373 lines
13 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 Chukong Technologies Inc.
2015-03-02 13:07:32 +08:00
http://www.cocos2d-x.org
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:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
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/CCPUBeamRender.h"
#include "extensions/Particle3D/PU/CCPUParticleSystem3D.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 "renderer/CCGLProgramState.h"
#include "renderer/CCGLProgramCache.h"
#include "renderer/CCVertexIndexBuffer.h"
#include "base/CCDirector.h"
#include "3d/CCSprite3D.h"
#include "3d/CCMesh.h"
#include "2d/CCCamera.h"
#include <sstream>
NS_CC_BEGIN
// Constants
2015-03-05 13:24:06 +08:00
const bool PUBeamRender::DEFAULT_USE_VERTEX_COLOURS = false;
const size_t PUBeamRender::DEFAULT_MAX_ELEMENTS = 10;
const float PUBeamRender::DEFAULT_UPDATE_INTERVAL = 0.1f;
const float PUBeamRender::DEFAULT_DEVIATION = 300;
const size_t PUBeamRender::DEFAULT_NUMBER_OF_SEGMENTS = 2;
const PUBillboardChain::TexCoordDirection PUBeamRender::DEFAULT_TEXTURE_DIRECTION = PUBillboardChain::TCD_V;
PUBeamRender* PUBeamRender::create( const std::string &texFile )
2015-03-02 13:07:32 +08:00
{
2015-03-05 13:24:06 +08:00
auto br = new (std::nothrow) PUBeamRender();
2015-03-02 13:07:32 +08:00
br->autorelease();
br->_texFile = texFile;
return br;
}
2015-03-05 13:24:06 +08:00
void PUBeamRender::render( Renderer* renderer, const Mat4 &transform, ParticleSystem3D* particleSystem )
2015-03-02 13:07:32 +08:00
{
const ParticlePool &particlePool = particleSystem->getParticlePool();
if (!_isVisible || particlePool.empty() || !_billboardChain)
return;
Vec3 basePosition = static_cast<PUParticleSystem3D *>(_particleSystem)->getDerivedPosition();
for (auto iter : particlePool.getActiveDataList())
{
auto particle = static_cast<PUParticle3D *>(iter);
auto visualData = static_cast<PUParticle3DBeamVisualData*>(particle->visualData);
if (visualData){
Vec3 end = particle->position - basePosition;
PUSimpleSpline spline;
// Add points
spline.addPoint(Vec3::ZERO);
for (size_t numDev = 0; numDev < _numberOfSegments; ++numDev)
{
spline.addPoint(visualData->half[numDev]);
}
spline.addPoint(end);
// Loop through all chain elements
for (size_t j = 0; j < _maxChainElements; ++j)
{
PUBillboardChain::Element element = _billboardChain->getChainElement(visualData->chainIndex, j);
// 1. Set the width of the chain if required
if (particle->ownDimensions)
{
element.width = _rendererScale.x * particle->width;
}
// 2. Set positions of the elements
element.position = spline.interpolate((float)j / (float)_maxChainElements);
// 3. Set the colour
element.color = particle->color;
// 4. Update
_billboardChain->updateChainElement(visualData->chainIndex, j, element);
}
visualData->setVisible(true);
}
}
_billboardChain->render(renderer, transform, particleSystem);
}
2015-03-05 13:24:06 +08:00
PUBeamRender::PUBeamRender() :
2015-03-02 13:07:32 +08:00
_billboardChain(0),
_quota(0),
_useVertexColours(DEFAULT_USE_VERTEX_COLOURS),
_maxChainElements(DEFAULT_MAX_ELEMENTS),
_updateInterval(DEFAULT_UPDATE_INTERVAL),
_deviation(DEFAULT_DEVIATION),
_numberOfSegments(DEFAULT_NUMBER_OF_SEGMENTS),
_jump(false),
_texCoordDirection(DEFAULT_TEXTURE_DIRECTION)
{
autoRotate = true;
}
2015-03-05 13:24:06 +08:00
PUBeamRender::~PUBeamRender()
2015-03-02 13:07:32 +08:00
{
if (!_particleSystem)
return;
destroyAll();
}
2015-03-05 13:24:06 +08:00
void PUBeamRender::particleEmitted( PUParticleSystem3D* particleSystem, PUParticle3D* particle )
2015-03-02 13:07:32 +08:00
{
if (!particle->visualData && !_visualData.empty() && particle->particleType == PUParticle3D::PT_VISUAL)
{
particle->visualData = _visualData.back();
PUParticle3DBeamVisualData* beamRendererVisualData = static_cast<PUParticle3DBeamVisualData*>(particle->visualData);
beamRendererVisualData->setVisible(true, _rendererScale.x * particleSystem->getDefaultWidth()); // PU 1.4
_visualData.pop_back();
}
}
2015-03-05 13:24:06 +08:00
void PUBeamRender::particleExpired( PUParticleSystem3D* particleSystem, PUParticle3D* particle )
2015-03-02 13:07:32 +08:00
{
if (particle->visualData)
{
PUParticle3DBeamVisualData* beamRendererVisualData = static_cast<PUParticle3DBeamVisualData*>(particle->visualData);
beamRendererVisualData->setVisible(false, 0); // PU 1.4
_visualData.push_back(beamRendererVisualData);
particle->visualData = nullptr;
}
}
//-----------------------------------------------------------------------
2015-03-05 13:24:06 +08:00
bool PUBeamRender::isUseVertexColours(void) const
2015-03-02 13:07:32 +08:00
{
return _useVertexColours;
}
//-----------------------------------------------------------------------
2015-03-05 13:24:06 +08:00
void PUBeamRender::setUseVertexColours(bool useVertexColours)
2015-03-02 13:07:32 +08:00
{
_useVertexColours = useVertexColours;
if (!_billboardChain)
return;
_billboardChain->setUseVertexColours(_useVertexColours);
_billboardChain->setUseTextureCoords(!_useVertexColours);
}
//-----------------------------------------------------------------------
2015-03-05 13:24:06 +08:00
size_t PUBeamRender::getMaxChainElements(void) const
2015-03-02 13:07:32 +08:00
{
return _maxChainElements;
}
//-----------------------------------------------------------------------
2015-03-05 13:24:06 +08:00
void PUBeamRender::setMaxChainElements(size_t maxChainElements)
2015-03-02 13:07:32 +08:00
{
_maxChainElements = maxChainElements;
}
//-----------------------------------------------------------------------
2015-03-05 13:24:06 +08:00
float PUBeamRender::getUpdateInterval(void) const
2015-03-02 13:07:32 +08:00
{
return _updateInterval;
}
//-----------------------------------------------------------------------
2015-03-05 13:24:06 +08:00
void PUBeamRender::setUpdateInterval(float updateInterval)
2015-03-02 13:07:32 +08:00
{
_updateInterval = updateInterval;
}
//-----------------------------------------------------------------------
2015-03-05 13:24:06 +08:00
float PUBeamRender::getDeviation(void) const
2015-03-02 13:07:32 +08:00
{
return _deviation;
}
//-----------------------------------------------------------------------
2015-03-05 13:24:06 +08:00
void PUBeamRender::setDeviation(float deviation)
2015-03-02 13:07:32 +08:00
{
_deviation = deviation;
}
//-----------------------------------------------------------------------
2015-03-05 13:24:06 +08:00
size_t PUBeamRender::getNumberOfSegments(void) const
2015-03-02 13:07:32 +08:00
{
return _numberOfSegments;
}
//-----------------------------------------------------------------------
2015-03-05 13:24:06 +08:00
void PUBeamRender::setNumberOfSegments(size_t numberOfSegments)
2015-03-02 13:07:32 +08:00
{
_numberOfSegments = numberOfSegments;
}
//-----------------------------------------------------------------------
2015-03-05 13:24:06 +08:00
bool PUBeamRender::isJump(void) const
2015-03-02 13:07:32 +08:00
{
return _jump;
}
//-----------------------------------------------------------------------
2015-03-05 13:24:06 +08:00
void PUBeamRender::setJump(bool jump)
2015-03-02 13:07:32 +08:00
{
_jump = jump;
}
//-----------------------------------------------------------------------
2015-03-05 13:24:06 +08:00
PUBillboardChain::TexCoordDirection PUBeamRender::getTexCoordDirection(void) const
2015-03-02 13:07:32 +08:00
{
return _texCoordDirection;
}
//-----------------------------------------------------------------------
2015-03-05 13:24:06 +08:00
void PUBeamRender::setTexCoordDirection(PUBillboardChain::TexCoordDirection texCoordDirection)
2015-03-02 13:07:32 +08:00
{
_texCoordDirection = texCoordDirection;
}
//-----------------------------------------------------------------------
2015-03-05 13:24:06 +08:00
void PUBeamRender::prepare()
2015-03-02 13:07:32 +08:00
{
if (!_particleSystem)
return;
// Register itself to the technique
if (_particleSystem)
{
// 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);
}
_quota = _particleSystem->getParticleQuota();
// Create BillboardChain
std::stringstream ss;
ss << this;
_billboardChainName = "Beam" + ss.str();
2015-03-02 16:05:26 +08:00
_billboardChain = new (std::nothrow) PUBillboardChain(_billboardChainName, _texFile);
2015-03-02 13:07:32 +08:00
_billboardChain->setDynamic(true);
_billboardChain->setNumberOfChains(_quota);
_billboardChain->setMaxChainElements(_maxChainElements);
_billboardChain->setTextureCoordDirection(_texCoordDirection);
setUseVertexColours(_useVertexColours);
_billboardChain->setOtherTextureCoordRange(0.0f, 1.0f);
_billboardChain->setDepthTest(_depthTest);
_billboardChain->setDepthWrite(_depthWrite);
// Create number of VisualData objects
for (size_t i = 0; i < _quota; i++)
{
for (size_t j = 0; j < _maxChainElements; j++)
{
PUBillboardChain::Element element;
element = PUBillboardChain::Element(Vec3::ZERO, _rendererScale.x * static_cast<PUParticleSystem3D *>(_particleSystem)->getDefaultWidth(), 0.0f, Vec4::ONE, Quaternion::identity()); // V1.51
_billboardChain->addChainElement(i, element);
}
2015-03-02 16:05:26 +08:00
PUParticle3DBeamVisualData* visualData = new (std::nothrow) PUParticle3DBeamVisualData(i, _billboardChain);
2015-03-02 13:07:32 +08:00
for (size_t numDev = 0; numDev < _numberOfSegments; ++numDev)
{
// Initialise the positions
visualData->half[numDev].setZero();
visualData->destinationHalf[numDev].setZero();
2015-03-02 13:07:32 +08:00
}
_allVisualData.push_back(visualData); // Managed by this renderer
_visualData.push_back(visualData); // Used to assign to a particle
}
}
2015-03-05 13:24:06 +08:00
void PUBeamRender::unPrepare()
2015-03-02 13:07:32 +08:00
{
destroyAll();
}
2015-03-05 13:24:06 +08:00
void PUBeamRender::updateRender( PUParticle3D *particle, float deltaTime, bool firstParticle )
2015-03-02 13:07:32 +08:00
{
if (!particle->visualData)
return;
PUParticle3DBeamVisualData* beamRendererVisualData = static_cast<PUParticle3DBeamVisualData*>(particle->visualData);
beamRendererVisualData->timeSinceLastUpdate -= deltaTime;
if (beamRendererVisualData->timeSinceLastUpdate < 0)
{
Vec3 end = particle->position - static_cast<PUParticleSystem3D *>(_particleSystem)->getDerivedPosition();
Vec3 perpendicular;
float divide = (float)_numberOfSegments + 1.0f;
for (size_t numDev = 0; numDev < _numberOfSegments; ++numDev)
{
Vec3::cross(end, Vec3(CCRANDOM_MINUS1_1(), CCRANDOM_MINUS1_1(), CCRANDOM_MINUS1_1()), &perpendicular);
perpendicular.normalize();
beamRendererVisualData->destinationHalf[numDev] = (((float)numDev + 1.0f) / divide) * end
+ Vec3(_rendererScale.x * _deviation * perpendicular.x
, _rendererScale.y * _deviation * perpendicular.y
, _rendererScale.z * _deviation * perpendicular.z);
}
beamRendererVisualData->timeSinceLastUpdate += _updateInterval;
}
Vec3 diff;
for (size_t numDev = 0; numDev < _numberOfSegments; ++numDev)
{
if (_jump)
{
beamRendererVisualData->half[numDev] = beamRendererVisualData->destinationHalf[numDev];
}
else
{
diff = beamRendererVisualData->destinationHalf[numDev] - beamRendererVisualData->half[numDev];
beamRendererVisualData->half[numDev] = beamRendererVisualData->half[numDev] + deltaTime * diff;
}
}
}
//-----------------------------------------------------------------------
2015-03-05 13:24:06 +08:00
void PUBeamRender::destroyAll(void)
2015-03-02 13:07:32 +08:00
{
if (!_particleSystem || !_billboardChain)
return;
// Remove the listener
static_cast<PUParticleSystem3D *>(_particleSystem)->removeListener(this);
// Delete the BillboardChain
CC_SAFE_DELETE(_billboardChain);
// Delete the visual data
std::vector<PUParticle3DBeamVisualData*>::const_iterator it;
std::vector<PUParticle3DBeamVisualData*>::const_iterator itEnd = _allVisualData.end();
for (it = _allVisualData.begin(); it != itEnd; ++it)
{
delete *it;
}
_allVisualData.clear();
_visualData.clear();
}
2015-03-05 13:24:06 +08:00
PUBeamRender* PUBeamRender::clone()
2015-03-02 13:07:32 +08:00
{
2015-03-05 13:24:06 +08:00
auto br = PUBeamRender::create(_texFile);
2015-03-02 13:07:32 +08:00
copyAttributesTo(br);
return br;
}
void PUBeamRender::copyAttributesTo(PUBeamRender *beamRender)
2015-03-02 13:07:32 +08:00
{
PURender::copyAttributesTo(beamRender);
2015-03-02 13:07:32 +08:00
beamRender->setUseVertexColours(_useVertexColours);
beamRender->setMaxChainElements(_maxChainElements);
beamRender->setUpdateInterval(_updateInterval);
beamRender->setDeviation(_deviation);
beamRender->setNumberOfSegments(_numberOfSegments);
beamRender->setJump(_jump);
beamRender->setTexCoordDirection(_texCoordDirection);
}
NS_CC_END