2019-11-23 20:27:39 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2009 Matt Oswald
|
|
|
|
* Copyright (c) 2009-2010 Ricardo Quesada
|
|
|
|
* Copyright (c) 2010-2012 cocos2d-x.org
|
|
|
|
* Copyright (c) 2011 Zynga Inc.
|
|
|
|
* Copyright (c) 2011 Marco Tillemans
|
|
|
|
* Copyright (c) 2013-2016 Chukong Technologies Inc.
|
|
|
|
* Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2022-04-25 19:15:46 +08:00
|
|
|
* Copyright (c) 2020-2021 C4games Ltd.
|
2021-10-09 13:48:56 +08:00
|
|
|
* Copyright (c) 2021 Bytedance Inc.
|
2021-12-25 10:04:45 +08:00
|
|
|
*
|
2022-08-08 18:02:17 +08:00
|
|
|
* https://axys1.github.io/
|
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:
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#include "2d/CCParticleBatchNode.h"
|
2021-12-25 10:04:45 +08:00
|
|
|
#include <stddef.h> // offsetof
|
2019-11-23 20:27:39 +08:00
|
|
|
#include "base/ccTypes.h"
|
|
|
|
#include "2d/CCGrid.h"
|
|
|
|
#include "2d/CCParticleSystem.h"
|
|
|
|
#include "renderer/CCTextureCache.h"
|
|
|
|
#include "renderer/CCQuadCommand.h"
|
|
|
|
#include "renderer/CCRenderer.h"
|
|
|
|
#include "renderer/CCTextureAtlas.h"
|
|
|
|
#include "base/CCProfiling.h"
|
|
|
|
#include "base/ccUTF8.h"
|
|
|
|
#include "base/ccUtils.h"
|
|
|
|
#include "renderer/ccShaders.h"
|
|
|
|
#include "renderer/backend/ProgramState.h"
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BEGIN
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
ParticleBatchNode::ParticleBatchNode()
|
|
|
|
{
|
2020-10-26 14:49:14 +08:00
|
|
|
auto& pipelinePS = _customCommand.getPipelineDescriptor().programState;
|
2021-12-25 10:04:45 +08:00
|
|
|
auto* program = backend::Program::getBuiltinProgram(backend::ProgramType::POSITION_TEXTURE_COLOR);
|
2020-10-26 14:49:14 +08:00
|
|
|
//!!! ParticleBatchNode private programState don't want affect by Node::_programState, so store at _customCommand
|
|
|
|
//!!! support etc1 with alpha?
|
2021-12-08 00:11:53 +08:00
|
|
|
pipelinePS = new backend::ProgramState(program);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2020-10-26 14:49:14 +08:00
|
|
|
_mvpMatrixLocaiton = pipelinePS->getUniformLocation("u_MVPMatrix");
|
2022-07-05 00:39:20 +08:00
|
|
|
_textureLocation = pipelinePS->getUniformLocation("u_tex0");
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
auto layout = pipelinePS->getVertexLayout();
|
2020-10-26 14:49:14 +08:00
|
|
|
const auto& attributeInfo = pipelinePS->getProgram()->getActiveAttributes();
|
2021-12-25 10:04:45 +08:00
|
|
|
auto iter = attributeInfo.find("a_position");
|
|
|
|
if (iter != attributeInfo.end())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
layout->setAttribute("a_position", iter->second.location, backend::VertexFormat::FLOAT3, 0, false);
|
|
|
|
}
|
|
|
|
iter = attributeInfo.find("a_texCoord");
|
2021-12-25 10:04:45 +08:00
|
|
|
if (iter != attributeInfo.end())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
layout->setAttribute("a_texCoord", iter->second.location, backend::VertexFormat::FLOAT2,
|
|
|
|
offsetof(V3F_C4B_T2F, texCoords), false);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
iter = attributeInfo.find("a_color");
|
2021-12-25 10:04:45 +08:00
|
|
|
if (iter != attributeInfo.end())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
layout->setAttribute("a_color", iter->second.location, backend::VertexFormat::UBYTE4,
|
|
|
|
offsetof(V3F_C4B_T2F, colors), true);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
layout->setLayout(sizeof(V3F_C4B_T2F));
|
|
|
|
|
|
|
|
_customCommand.setDrawType(CustomCommand::DrawType::ELEMENT);
|
|
|
|
_customCommand.setPrimitiveType(CustomCommand::PrimitiveType::TRIANGLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
ParticleBatchNode::~ParticleBatchNode()
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_RELEASE(_textureAtlas);
|
|
|
|
AX_SAFE_RELEASE(_customCommand.getPipelineDescriptor().programState);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* creation with Texture2D
|
|
|
|
*/
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
ParticleBatchNode* ParticleBatchNode::createWithTexture(Texture2D* tex, int capacity /* = kParticleDefaultCapacity*/)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
ParticleBatchNode* p = new ParticleBatchNode();
|
|
|
|
if (p->initWithTexture(tex, capacity))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
p->autorelease();
|
|
|
|
return p;
|
|
|
|
}
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_DELETE(p);
|
2019-11-23 20:27:39 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* creation with File Image
|
|
|
|
*/
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
ParticleBatchNode* ParticleBatchNode::create(std::string_view imageFile, int capacity /* = kParticleDefaultCapacity*/)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
ParticleBatchNode* p = new ParticleBatchNode();
|
|
|
|
if (p->initWithFile(imageFile, capacity))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
p->autorelease();
|
|
|
|
return p;
|
|
|
|
}
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_DELETE(p);
|
2019-11-23 20:27:39 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* init with Texture2D
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
bool ParticleBatchNode::initWithTexture(Texture2D* tex, int capacity)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
_textureAtlas = new TextureAtlas();
|
2019-11-23 20:27:39 +08:00
|
|
|
_textureAtlas->initWithTexture(tex, capacity);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2021-04-22 22:01:47 +08:00
|
|
|
updateProgramStateTexture();
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
_children.reserve(capacity);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
_blendFunc = BlendFunc::ALPHA_PREMULTIPLIED;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* init with FileImage
|
|
|
|
*/
|
2021-12-31 12:12:40 +08:00
|
|
|
bool ParticleBatchNode::initWithFile(std::string_view fileImage, int capacity)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
Texture2D* tex = _director->getTextureCache()->addImage(fileImage);
|
2019-11-23 20:27:39 +08:00
|
|
|
return initWithTexture(tex, capacity);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParticleBatchNode - composition
|
|
|
|
|
|
|
|
// override visit.
|
|
|
|
// Don't call visit on it's children
|
2021-12-25 10:04:45 +08:00
|
|
|
void ParticleBatchNode::visit(Renderer* renderer, const Mat4& parentTransform, uint32_t parentFlags)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
// CAREFUL:
|
|
|
|
// This visit is almost identical to Node#visit
|
|
|
|
// with the exception that it doesn't call visit on it's children
|
|
|
|
//
|
|
|
|
// The alternative is to have a void Sprite#visit, but
|
|
|
|
// although this is less maintainable, is faster
|
|
|
|
//
|
|
|
|
if (!_visible)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t flags = processParentFlags(parentTransform, parentFlags);
|
|
|
|
|
|
|
|
if (isVisitableByVisitingCamera())
|
|
|
|
{
|
|
|
|
// IMPORTANT:d
|
|
|
|
// To ease the migration to v3.0, we still support the Mat4 stack,
|
|
|
|
// but it is deprecated and your code should not rely on it
|
2021-04-22 22:01:47 +08:00
|
|
|
_director->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
|
|
|
|
_director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW, _modelViewTransform);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
draw(renderer, _modelViewTransform, flags);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2021-04-22 22:01:47 +08:00
|
|
|
_director->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// override addChild:
|
2021-12-25 10:04:45 +08:00
|
|
|
void ParticleBatchNode::addChild(Node* aChild, int zOrder, int tag)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(aChild != nullptr, "Argument must be non-nullptr");
|
|
|
|
AXASSERT(dynamic_cast<ParticleSystem*>(aChild) != nullptr,
|
2021-12-25 10:04:45 +08:00
|
|
|
"CCParticleBatchNode only supports QuadParticleSystems as children");
|
2019-11-23 20:27:39 +08:00
|
|
|
ParticleSystem* child = static_cast<ParticleSystem*>(aChild);
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(child->getTexture()->getBackendTexture() == _textureAtlas->getTexture()->getBackendTexture(),
|
2021-12-25 10:04:45 +08:00
|
|
|
"CCParticleSystem is not using the same texture id");
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
addChildByTagOrName(child, zOrder, tag, "", true);
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void ParticleBatchNode::addChild(Node* aChild, int zOrder, std::string_view name)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(aChild != nullptr, "Argument must be non-nullptr");
|
|
|
|
AXASSERT(dynamic_cast<ParticleSystem*>(aChild) != nullptr,
|
2021-12-25 10:04:45 +08:00
|
|
|
"CCParticleBatchNode only supports QuadParticleSystems as children");
|
2019-11-23 20:27:39 +08:00
|
|
|
ParticleSystem* child = static_cast<ParticleSystem*>(aChild);
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(child->getTexture()->getBackendTexture() == _textureAtlas->getTexture()->getBackendTexture(),
|
2021-12-25 10:04:45 +08:00
|
|
|
"CCParticleSystem is not using the same texture id");
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
addChildByTagOrName(child, zOrder, 0, name, false);
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void ParticleBatchNode::addChildByTagOrName(ParticleSystem* child,
|
|
|
|
int zOrder,
|
|
|
|
int tag,
|
2021-12-31 12:12:40 +08:00
|
|
|
std::string_view name,
|
2021-12-25 10:04:45 +08:00
|
|
|
bool setTag)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
// If this is the 1st children, then copy blending function
|
|
|
|
if (_children.empty())
|
|
|
|
{
|
|
|
|
setBlendFunc(child->getBlendFunc());
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(_blendFunc.src == child->getBlendFunc().src && _blendFunc.dst == child->getBlendFunc().dst,
|
2021-12-25 10:04:45 +08:00
|
|
|
"Can't add a ParticleSystem that uses a different blending function");
|
|
|
|
|
|
|
|
// no lazy sorting, so don't call super addChild, call helper instead
|
2019-11-23 20:27:39 +08:00
|
|
|
int pos = 0;
|
|
|
|
if (setTag)
|
|
|
|
pos = addChildHelper(child, zOrder, tag, "", true);
|
|
|
|
else
|
|
|
|
pos = addChildHelper(child, zOrder, 0, name, false);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
// get new atlasIndex
|
2019-11-23 20:27:39 +08:00
|
|
|
int atlasIndex = 0;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
if (pos != 0)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
ParticleSystem* p = static_cast<ParticleSystem*>(_children.at(pos - 1));
|
|
|
|
atlasIndex = p->getAtlasIndex() + p->getTotalParticles();
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
atlasIndex = 0;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
insertChild(child, atlasIndex);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// update quad info
|
|
|
|
child->setBatchNode(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
// don't use lazy sorting, reordering the particle systems quads afterwards would be too complex
|
2021-12-25 10:04:45 +08:00
|
|
|
// FIXME: research whether lazy sorting + freeing current quads and calloc a new block with size of capacity would be
|
|
|
|
// faster
|
2019-11-23 20:27:39 +08:00
|
|
|
// FIXME: or possibly using vertexZ for reordering, that would be fastest
|
|
|
|
// this helper is almost equivalent to Node's addChild, but doesn't make use of the lazy sorting
|
2021-12-31 12:12:40 +08:00
|
|
|
int ParticleBatchNode::addChildHelper(ParticleSystem* child, int z, int aTag, std::string_view name, bool setTag)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(child != nullptr, "Argument must be non-nil");
|
|
|
|
AXASSERT(child->getParent() == nullptr, "child already added. It can't be added again");
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
_children.reserve(4);
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
// don't use a lazy insert
|
2019-11-23 20:27:39 +08:00
|
|
|
auto pos = searchNewPositionInChildrenForZ(z);
|
|
|
|
|
|
|
|
_children.insert(pos, child);
|
|
|
|
|
|
|
|
if (setTag)
|
|
|
|
child->setTag(aTag);
|
|
|
|
else
|
|
|
|
child->setName(name);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
child->setLocalZOrder(z);
|
|
|
|
|
|
|
|
child->setParent(this);
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_running)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
child->onEnter();
|
|
|
|
child->onEnterTransitionDidFinish();
|
|
|
|
}
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reorder will be done in this function, no "lazy" reorder to particles
|
2021-12-25 10:04:45 +08:00
|
|
|
void ParticleBatchNode::reorderChild(Node* aChild, int zOrder)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(aChild != nullptr, "Child must be non-nullptr");
|
|
|
|
AXASSERT(dynamic_cast<ParticleSystem*>(aChild) != nullptr,
|
2021-12-25 10:04:45 +08:00
|
|
|
"CCParticleBatchNode only supports QuadParticleSystems as children");
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(_children.contains(aChild), "Child doesn't belong to batch");
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
ParticleSystem* child = static_cast<ParticleSystem*>(aChild);
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (zOrder == child->getLocalZOrder())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// no reordering if only 1 child
|
|
|
|
if (!_children.empty())
|
|
|
|
{
|
|
|
|
int newIndex = 0, oldIndex = 0;
|
|
|
|
|
|
|
|
getCurrentIndex(&oldIndex, &newIndex, child, zOrder);
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (oldIndex != newIndex)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
// reorder _children->array
|
|
|
|
child->retain();
|
|
|
|
_children.erase(oldIndex);
|
|
|
|
_children.insert(newIndex, child);
|
|
|
|
child->release();
|
|
|
|
|
|
|
|
// save old altasIndex
|
|
|
|
int oldAtlasIndex = child->getAtlasIndex();
|
|
|
|
|
|
|
|
// update atlas index
|
|
|
|
updateAllAtlasIndexes();
|
|
|
|
|
|
|
|
// Find new AtlasIndex
|
|
|
|
int newAtlasIndex = 0;
|
|
|
|
for (const auto& iter : _children)
|
|
|
|
{
|
|
|
|
auto node = static_cast<ParticleSystem*>(iter);
|
2021-12-25 10:04:45 +08:00
|
|
|
if (node == child)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
newAtlasIndex = child->getAtlasIndex();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// reorder textureAtlas quads
|
|
|
|
_textureAtlas->moveQuadsFromIndex(oldAtlasIndex, child->getTotalParticles(), newAtlasIndex);
|
|
|
|
|
|
|
|
child->updateWithNoTime();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
child->setLocalZOrder(zOrder);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ParticleBatchNode::getCurrentIndex(int* oldIndex, int* newIndex, Node* child, int z)
|
|
|
|
{
|
|
|
|
bool foundCurrentIdx = false;
|
2021-12-25 10:04:45 +08:00
|
|
|
bool foundNewIdx = false;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
int minusOne = 0;
|
|
|
|
auto count = _children.size();
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
for (int i = 0; i < count; i++)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
Node* pNode = _children.at(i);
|
|
|
|
|
|
|
|
// new index
|
2021-12-25 10:04:45 +08:00
|
|
|
if (pNode->getLocalZOrder() > z && !foundNewIdx)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
*newIndex = i;
|
2019-11-23 20:27:39 +08:00
|
|
|
foundNewIdx = true;
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (foundCurrentIdx && foundNewIdx)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// current index
|
2021-12-25 10:04:45 +08:00
|
|
|
if (child == pNode)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
*oldIndex = i;
|
2019-11-23 20:27:39 +08:00
|
|
|
foundCurrentIdx = true;
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!foundNewIdx)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
minusOne = -1;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (foundCurrentIdx && foundNewIdx)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!foundNewIdx)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
*newIndex = static_cast<int>(count);
|
|
|
|
}
|
|
|
|
|
|
|
|
*newIndex += minusOne;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ParticleBatchNode::searchNewPositionInChildrenForZ(int z)
|
|
|
|
{
|
|
|
|
auto count = _children.size();
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
for (int i = 0; i < count; i++)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
Node* child = _children.at(i);
|
2019-11-23 20:27:39 +08:00
|
|
|
if (child->getLocalZOrder() > z)
|
|
|
|
{
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return static_cast<int>(count);
|
|
|
|
}
|
|
|
|
|
|
|
|
// override removeChild:
|
2021-12-25 10:04:45 +08:00
|
|
|
void ParticleBatchNode::removeChild(Node* aChild, bool cleanup)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
// explicit nil handling
|
|
|
|
if (aChild == nullptr)
|
|
|
|
return;
|
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(dynamic_cast<ParticleSystem*>(aChild) != nullptr,
|
2021-12-25 10:04:45 +08:00
|
|
|
"CCParticleBatchNode only supports QuadParticleSystems as children");
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(_children.contains(aChild), "CCParticleBatchNode doesn't contain the sprite. Can't remove it");
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
ParticleSystem* child = static_cast<ParticleSystem*>(aChild);
|
|
|
|
|
|
|
|
// remove child helper
|
|
|
|
_textureAtlas->removeQuadsAtIndex(child->getAtlasIndex(), child->getTotalParticles());
|
|
|
|
|
|
|
|
// after memmove of data, empty the quads at the end of array
|
|
|
|
_textureAtlas->fillWithEmptyQuadsFromIndex(_textureAtlas->getTotalQuads(), child->getTotalParticles());
|
|
|
|
|
|
|
|
// particle could be reused for self rendering
|
|
|
|
child->setBatchNode(nullptr);
|
|
|
|
Node::removeChild(child, cleanup);
|
|
|
|
|
|
|
|
updateAllAtlasIndexes();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ParticleBatchNode::removeChildAtIndex(int index, bool doCleanup)
|
|
|
|
{
|
|
|
|
removeChild(_children.at(index), doCleanup);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ParticleBatchNode::removeAllChildrenWithCleanup(bool doCleanup)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
for (const auto& child : _children)
|
2019-11-23 20:27:39 +08:00
|
|
|
static_cast<ParticleSystem*>(child)->setBatchNode(nullptr);
|
|
|
|
|
|
|
|
Node::removeAllChildrenWithCleanup(doCleanup);
|
|
|
|
|
|
|
|
_textureAtlas->removeAllQuads();
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void ParticleBatchNode::draw(Renderer* renderer, const Mat4& transform, uint32_t flags)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_PROFILER_START("CCParticleBatchNode - draw");
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_textureAtlas->getTotalQuads() == 0)
|
2019-11-23 20:27:39 +08:00
|
|
|
return;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
_customCommand.init(_globalZOrder, _blendFunc);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// Texture is set in TextureAtlas.
|
2022-08-08 18:02:17 +08:00
|
|
|
const ax::Mat4& projectionMat = _director->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
|
2021-12-25 10:04:45 +08:00
|
|
|
Mat4 finalMat = projectionMat * transform;
|
|
|
|
auto programState = _customCommand.getPipelineDescriptor().programState;
|
2019-11-23 20:27:39 +08:00
|
|
|
programState->setUniform(_mvpMatrixLocaiton, finalMat.m, sizeof(finalMat.m));
|
|
|
|
if (_textureAtlas->isDirty())
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
const auto& quads = _textureAtlas->getQuads();
|
2019-11-23 20:27:39 +08:00
|
|
|
unsigned int capacity = (unsigned int)_textureAtlas->getCapacity();
|
2021-12-25 10:04:45 +08:00
|
|
|
const auto& indices = _textureAtlas->getIndices();
|
|
|
|
|
|
|
|
_customCommand.createVertexBuffer((unsigned int)(sizeof(quads[0])), capacity,
|
|
|
|
CustomCommand::BufferUsage::STATIC);
|
2019-11-23 20:27:39 +08:00
|
|
|
_customCommand.updateVertexBuffer(quads, sizeof(quads[0]) * capacity);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
_customCommand.createIndexBuffer(CustomCommand::IndexFormat::U_SHORT, capacity * 6,
|
|
|
|
CustomCommand::BufferUsage::STATIC);
|
2019-11-23 20:27:39 +08:00
|
|
|
_customCommand.updateIndexBuffer(indices, sizeof(indices[0]) * capacity * 6);
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
renderer->addCommand(&_customCommand);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_PROFILER_STOP("CCParticleBatchNode - draw");
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ParticleBatchNode::increaseAtlasCapacityTo(ssize_t quantity)
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOG("cocos2d: ParticleBatchNode: resizing TextureAtlas capacity from [%d] to [%d].",
|
2021-12-25 10:04:45 +08:00
|
|
|
(int)_textureAtlas->getCapacity(), (int)quantity);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!_textureAtlas->resizeCapacity(quantity))
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
// serious problems
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOGWARN("cocos2d: WARNING: Not enough memory to resize the atlas");
|
|
|
|
AXASSERT(false, "XXX: ParticleBatchNode #increaseAtlasCapacity SHALL handle this assert");
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
// sets a 0'd quad into the quads array
|
2019-11-23 20:27:39 +08:00
|
|
|
void ParticleBatchNode::disableParticle(int particleIndex)
|
|
|
|
{
|
|
|
|
V3F_C4B_T2F_Quad* quad = &((_textureAtlas->getQuads())[particleIndex]);
|
2021-12-25 10:04:45 +08:00
|
|
|
quad->br.vertices.x = quad->br.vertices.y = quad->tr.vertices.x = quad->tr.vertices.y = quad->tl.vertices.x =
|
|
|
|
quad->tl.vertices.y = quad->bl.vertices.x = quad->bl.vertices.y = 0.0f;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// ParticleBatchNode - add / remove / reorder helper methods
|
|
|
|
|
|
|
|
// add child helper
|
|
|
|
void ParticleBatchNode::insertChild(ParticleSystem* system, int index)
|
|
|
|
{
|
|
|
|
system->setAtlasIndex(index);
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_textureAtlas->getTotalQuads() + system->getTotalParticles() > _textureAtlas->getCapacity())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
increaseAtlasCapacityTo(_textureAtlas->getTotalQuads() + system->getTotalParticles());
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
// after a realloc empty quads of textureAtlas can be filled with gibberish (realloc doesn't perform calloc),
|
|
|
|
// insert empty quads to prevent it
|
|
|
|
_textureAtlas->fillWithEmptyQuadsFromIndex(_textureAtlas->getCapacity() - system->getTotalParticles(),
|
|
|
|
system->getTotalParticles());
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// make room for quads, not necessary for last child
|
|
|
|
if (system->getAtlasIndex() + system->getTotalParticles() != _textureAtlas->getTotalQuads())
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_textureAtlas->moveQuadsFromIndex(index, index + system->getTotalParticles());
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// increase totalParticles here for new particles, update method of particle-system will fill the quads
|
|
|
|
_textureAtlas->increaseTotalQuadsWith(system->getTotalParticles());
|
|
|
|
|
|
|
|
updateAllAtlasIndexes();
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
// rebuild atlas indexes
|
2019-11-23 20:27:39 +08:00
|
|
|
void ParticleBatchNode::updateAllAtlasIndexes()
|
|
|
|
{
|
|
|
|
int index = 0;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
for (const auto& child : _children)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
ParticleSystem* partiSys = static_cast<ParticleSystem*>(child);
|
|
|
|
partiSys->setAtlasIndex(index);
|
|
|
|
index += partiSys->getTotalParticles();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParticleBatchNode - CocosNodeTexture protocol
|
|
|
|
|
|
|
|
void ParticleBatchNode::updateBlendFunc()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!_textureAtlas->getTexture()->hasPremultipliedAlpha())
|
2019-11-23 20:27:39 +08:00
|
|
|
_blendFunc = BlendFunc::ALPHA_NON_PREMULTIPLIED;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ParticleBatchNode::setTexture(Texture2D* texture)
|
|
|
|
{
|
|
|
|
_textureAtlas->setTexture(texture);
|
2021-04-22 22:01:47 +08:00
|
|
|
updateProgramStateTexture();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ParticleBatchNode::updateProgramStateTexture()
|
|
|
|
{
|
|
|
|
auto texture = _textureAtlas->getTexture();
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!texture)
|
2021-04-22 22:01:47 +08:00
|
|
|
return;
|
|
|
|
auto programState = _customCommand.getPipelineDescriptor().programState;
|
|
|
|
programState->setTexture(texture->getBackendTexture());
|
|
|
|
// If the new texture has No premultiplied alpha, AND the blendFunc hasn't been changed, then update it
|
2022-07-16 10:43:05 +08:00
|
|
|
if (!texture->hasPremultipliedAlpha() && (_blendFunc.src == AX_BLEND_SRC && _blendFunc.dst == AX_BLEND_DST))
|
2021-04-22 22:01:47 +08:00
|
|
|
_blendFunc = BlendFunc::ALPHA_NON_PREMULTIPLIED;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Texture2D* ParticleBatchNode::getTexture() const
|
|
|
|
{
|
|
|
|
return _textureAtlas->getTexture();
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void ParticleBatchNode::setBlendFunc(const BlendFunc& blendFunc)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_blendFunc = blendFunc;
|
|
|
|
}
|
|
|
|
// returns the blending function used for the texture
|
|
|
|
const BlendFunc& ParticleBatchNode::getBlendFunc() const
|
|
|
|
{
|
|
|
|
return _blendFunc;
|
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_END
|