axmol/extensions/Particle3D/CCParticle3DRender.cpp

416 lines
14 KiB
C++
Raw Normal View History

2015-02-11 18:14:22 +08:00
/****************************************************************************
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
2015-02-11 18:14:22 +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-02-11 18:14:22 +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-02-11 18:14:22 +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"
2021-12-25 10:04:45 +08:00
#include <stddef.h> // offsetof
#include "base/ccTypes.h"
2015-03-02 16:05:26 +08:00
#include "extensions/Particle3D/CCParticle3DRender.h"
2015-02-11 18:14:22 +08:00
#include "renderer/CCMeshCommand.h"
#include "renderer/CCRenderer.h"
#include "renderer/CCTextureCache.h"
#include "renderer/backend/ProgramState.h"
#include "renderer/backend/Buffer.h"
#include "renderer/backend/Device.h"
#include "renderer/ccShaders.h"
2015-02-11 18:14:22 +08:00
#include "base/CCDirector.h"
#include "3d/CCMeshRenderer.h"
2015-02-11 18:14:22 +08:00
#include "2d/CCCamera.h"
NS_CC_BEGIN
Particle3DQuadRender::Particle3DQuadRender()
2021-12-25 10:04:45 +08:00
: _texture(nullptr), _programState(nullptr), _indexBuffer(nullptr), _vertexBuffer(nullptr), _texFile("")
{}
2015-02-11 18:14:22 +08:00
Particle3DQuadRender::~Particle3DQuadRender()
{
2021-12-25 10:04:45 +08:00
// CC_SAFE_RELEASE(_texture);
CC_SAFE_RELEASE(_programState);
2015-02-11 18:14:22 +08:00
CC_SAFE_RELEASE(_vertexBuffer);
CC_SAFE_RELEASE(_indexBuffer);
}
2021-12-26 23:26:34 +08:00
Particle3DQuadRender* Particle3DQuadRender::create(std::string_view texFile)
2015-02-11 18:14:22 +08:00
{
2021-12-08 00:11:53 +08:00
auto ret = new Particle3DQuadRender();
if (ret->initQuadRender(texFile))
2015-03-02 17:47:26 +08:00
{
ret->_texFile = texFile;
2015-03-02 17:47:26 +08:00
ret->autorelease();
}
else
{
CC_SAFE_DELETE(ret);
}
2015-02-11 18:14:22 +08:00
return ret;
}
2021-12-25 10:04:45 +08:00
void Particle3DQuadRender::render(Renderer* renderer, const Mat4& transform, ParticleSystem3D* particleSystem)
2015-02-11 18:14:22 +08:00
{
2021-12-25 10:04:45 +08:00
// batch and generate draw
const ParticlePool& particlePool = particleSystem->getParticlePool();
2015-02-11 18:14:22 +08:00
if (!_isVisible || particlePool.empty())
return;
2021-12-25 10:04:45 +08:00
if (_vertexBuffer == nullptr)
{
2019-06-05 17:58:33 +08:00
size_t stride = sizeof(Particle3DQuadRender::posuvcolor);
2021-12-25 10:04:45 +08:00
_vertexBuffer =
backend::Device::getInstance()->newBuffer(stride * 4 * particleSystem->getParticleQuota(),
backend::BufferType::VERTEX, backend::BufferUsage::DYNAMIC);
2015-03-02 17:47:26 +08:00
if (_vertexBuffer == nullptr)
{
CCLOG("Particle3DQuadRender::render create vertex buffer failed");
return;
}
2015-02-11 18:14:22 +08:00
}
2021-12-25 10:04:45 +08:00
if (_indexBuffer == nullptr)
{
_indexBuffer =
backend::Device::getInstance()->newBuffer(sizeof(uint16_t) * 6 * particleSystem->getParticleQuota(),
backend::BufferType::INDEX, backend::BufferUsage::DYNAMIC);
2015-03-02 17:47:26 +08:00
if (_indexBuffer == nullptr)
{
CCLOG("Particle3DQuadRender::render create index buffer failed");
return;
}
2015-02-11 18:14:22 +08:00
}
2015-03-02 13:07:32 +08:00
ParticlePool::PoolList activeParticleList = particlePool.getActiveDataList();
2015-02-11 18:14:22 +08:00
if (_posuvcolors.size() < activeParticleList.size() * 4)
{
_posuvcolors.resize(activeParticleList.size() * 4);
_indexData.resize(activeParticleList.size() * 6);
}
2021-12-25 10:04:45 +08:00
auto camera = Camera::getVisitingCamera();
auto cameraMat = camera->getNodeToWorldTransform();
const Mat4& viewMat = cameraMat.getInversed();
2015-02-11 18:14:22 +08:00
Mat4 pRotMat;
Vec3 right(cameraMat.m[0], cameraMat.m[1], cameraMat.m[2]);
Vec3 up(cameraMat.m[4], cameraMat.m[5], cameraMat.m[6]);
Vec3 backward(cameraMat.m[8], cameraMat.m[9], cameraMat.m[10]);
2021-12-25 10:04:45 +08:00
Vec3 position; // particle position
2015-02-11 18:14:22 +08:00
int vertexindex = 0;
2021-12-25 10:04:45 +08:00
int index = 0;
2015-02-11 18:14:22 +08:00
for (auto iter : activeParticleList)
{
2021-12-25 10:04:45 +08:00
auto particle = iter;
Vec3 halfwidth = particle->width * 0.5f * right;
2015-02-11 18:14:22 +08:00
Vec3 halfheight = particle->height * 0.5f * up;
2021-12-25 10:04:45 +08:00
// transform.transformPoint(particle->position, &position);
position = particle->position;
_posuvcolors[vertexindex].position = (position + (-halfwidth - halfheight));
_posuvcolors[vertexindex].color = particle->color;
_posuvcolors[vertexindex].uv.set(particle->lb_uv);
2015-02-11 18:14:22 +08:00
_posuvcolors[vertexindex + 1].position = (position + (halfwidth - halfheight));
2021-12-25 10:04:45 +08:00
_posuvcolors[vertexindex + 1].color = particle->color;
_posuvcolors[vertexindex + 1].uv.set(particle->rt_uv.x, particle->lb_uv.y);
2021-12-25 10:04:45 +08:00
_posuvcolors[vertexindex + 2].position = (position + (-halfwidth + halfheight));
_posuvcolors[vertexindex + 2].color = particle->color;
_posuvcolors[vertexindex + 2].uv.set(particle->lb_uv.x, particle->rt_uv.y);
2021-12-25 10:04:45 +08:00
2015-02-11 18:14:22 +08:00
_posuvcolors[vertexindex + 3].position = (position + (halfwidth + halfheight));
2021-12-25 10:04:45 +08:00
_posuvcolors[vertexindex + 3].color = particle->color;
_posuvcolors[vertexindex + 3].uv.set(particle->rt_uv);
2021-12-25 10:04:45 +08:00
_indexData[index] = vertexindex;
2015-02-11 18:14:22 +08:00
_indexData[index + 1] = vertexindex + 1;
_indexData[index + 2] = vertexindex + 3;
_indexData[index + 3] = vertexindex;
_indexData[index + 4] = vertexindex + 3;
_indexData[index + 5] = vertexindex + 2;
2021-12-25 10:04:45 +08:00
2015-02-11 18:14:22 +08:00
index += 6;
vertexindex += 4;
}
2021-12-25 10:04:45 +08:00
2015-02-11 18:14:22 +08:00
_posuvcolors.erase(_posuvcolors.begin() + vertexindex, _posuvcolors.end());
_indexData.erase(_indexData.begin() + index, _indexData.end());
2021-12-25 10:04:45 +08:00
_vertexBuffer->updateData(&_posuvcolors[0], vertexindex * sizeof(_posuvcolors[0]));
_indexBuffer->updateData(&_indexData[0], index * sizeof(_indexData[0]));
2021-12-25 10:04:45 +08:00
float depthZ = -(viewMat.m[2] * transform.m[12] + viewMat.m[6] * transform.m[13] + viewMat.m[10] * transform.m[14] +
viewMat.m[14]);
2022-06-24 14:18:48 +08:00
auto beforeCommand = renderer->nextCallbackCommand();
beforeCommand->init(depthZ);
2019-03-07 17:30:11 +08:00
_meshCommand.init(depthZ);
2022-06-24 14:18:48 +08:00
auto afterCommand = renderer->nextCallbackCommand();
afterCommand->init(depthZ);
beforeCommand->func = [=]() { onBeforeDraw(); }; // CC_CALLBACK_0(Particle3DQuadRender::onBeforeDraw, this);
afterCommand->func = [=]() { onAfterDraw(); }; // CC_CALLBACK_0(Particle3DQuadRender::onAfterDraw, this);
2019-03-07 17:30:11 +08:00
_meshCommand.setVertexBuffer(_vertexBuffer);
_meshCommand.setIndexBuffer(_indexBuffer, MeshCommand::IndexFormat::U_SHORT);
2021-12-25 10:04:45 +08:00
auto& projectionMatrix = Director::getInstance()->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
_programState->setUniform(_locPMatrix, &projectionMatrix.m, sizeof(projectionMatrix.m));
if (_texture)
{
_programState->setTexture(_locTexture, 0, _texture->getBackendTexture());
}
_stateBlock.setBlendFunc(particleSystem->getBlendFunc());
auto uColor = Vec4(1, 1, 1, 1);
_programState->setUniform(_locColor, &uColor, sizeof(uColor));
2019-03-07 17:30:11 +08:00
_meshCommand.setIndexDrawInfo(0, index);
2021-12-25 10:04:45 +08:00
2022-06-24 14:18:48 +08:00
renderer->addCommand(beforeCommand);
2019-03-07 17:30:11 +08:00
renderer->addCommand(&_meshCommand);
2022-06-24 14:18:48 +08:00
renderer->addCommand(afterCommand);
2015-02-11 18:14:22 +08:00
}
2021-12-26 23:26:34 +08:00
bool Particle3DQuadRender::initQuadRender(std::string_view texFile)
2015-02-11 18:14:22 +08:00
{
CC_SAFE_RELEASE_NULL(_programState);
2015-02-11 18:14:22 +08:00
if (!texFile.empty())
{
auto tex = Director::getInstance()->getTextureCache()->addImage(texFile);
if (tex)
{
2021-12-25 10:04:45 +08:00
_texture = tex;
auto* program = backend::Program::getBuiltinProgram(backend::ProgramType::PARTICLE_TEXTURE_3D);
_programState = new backend::ProgramState(program);
2015-02-11 18:14:22 +08:00
}
}
if (!_programState)
{
auto* program = backend::Program::getBuiltinProgram(backend::ProgramType::PARTICLE_COLOR_3D);
_programState = new backend::ProgramState(program);
}
2021-12-25 10:04:45 +08:00
auto& pipelineDescriptor = _meshCommand.getPipelineDescriptor();
pipelineDescriptor.programState = _programState;
2021-12-25 10:04:45 +08:00
auto layout = _programState->getVertexLayout();
const auto& attributeInfo = _programState->getProgram()->getActiveAttributes();
auto iter = attributeInfo.find("a_position");
if (iter != attributeInfo.end())
2019-03-13 15:12:36 +08:00
{
2021-12-25 10:04:45 +08:00
layout->setAttribute("a_position", iter->second.location, backend::VertexFormat::FLOAT3,
offsetof(posuvcolor, position), false);
2019-03-13 15:12:36 +08:00
}
iter = attributeInfo.find("a_texCoord");
2021-12-25 10:04:45 +08:00
if (iter != attributeInfo.end())
2019-03-13 15:12:36 +08:00
{
2021-12-25 10:04:45 +08:00
layout->setAttribute("a_texCoord", iter->second.location, backend::VertexFormat::FLOAT2,
offsetof(posuvcolor, uv), false);
2019-03-13 15:12:36 +08:00
}
iter = attributeInfo.find("a_color");
2021-12-25 10:04:45 +08:00
if (iter != attributeInfo.end())
2019-03-13 15:12:36 +08:00
{
2021-12-25 10:04:45 +08:00
layout->setAttribute("a_color", iter->second.location, backend::VertexFormat::FLOAT4,
offsetof(posuvcolor, color), false);
2019-03-13 15:12:36 +08:00
}
layout->setLayout(sizeof(posuvcolor));
2021-12-25 10:04:45 +08:00
_locColor = _programState->getUniformLocation("u_color");
_locPMatrix = _programState->getUniformLocation("u_PMatrix");
2022-07-04 21:26:48 +08:00
_locTexture = _programState->getUniformLocation("u_tex0");
2019-03-07 17:30:11 +08:00
_meshCommand.setTransparent(true);
_meshCommand.setSkipBatching(true);
_stateBlock.setDepthTest(true);
_stateBlock.setDepthWrite(false);
_stateBlock.setCullFaceSide(backend::CullMode::BACK);
_stateBlock.setCullFace(true);
2015-03-02 17:47:26 +08:00
return true;
2015-02-11 18:14:22 +08:00
}
void Particle3DQuadRender::onBeforeDraw()
{
2021-12-25 10:04:45 +08:00
auto* renderer = Director::getInstance()->getRenderer();
auto& pipelineDescriptor = _meshCommand.getPipelineDescriptor();
_rendererDepthTestEnabled = renderer->getDepthTest();
2021-12-25 10:04:45 +08:00
_rendererDepthCmpFunc = renderer->getDepthCompareFunction();
_rendererCullMode = renderer->getCullMode();
_rendererDepthWrite = renderer->getDepthWrite();
_rendererWinding = renderer->getWinding();
_stateBlock.bind(&pipelineDescriptor);
renderer->setDepthTest(true);
}
void Particle3DQuadRender::onAfterDraw()
{
2021-12-25 10:04:45 +08:00
auto* renderer = Director::getInstance()->getRenderer();
renderer->setDepthTest(_rendererDepthTestEnabled);
renderer->setDepthCompareFunction(_rendererDepthCmpFunc);
renderer->setCullMode(_rendererCullMode);
renderer->setDepthWrite(_rendererDepthWrite);
renderer->setWinding(_rendererWinding);
}
void Particle3DQuadRender::reset()
{
this->initQuadRender(_texFile);
}
2015-02-11 18:14:22 +08:00
//////////////////////////////////////////////////////////////////////////////
Particle3DModelRender::Particle3DModelRender() : _meshSize(Vec3::ONE) {}
2015-02-11 18:14:22 +08:00
Particle3DModelRender::~Particle3DModelRender()
{
for (auto iter : _meshList)
2021-12-25 10:04:45 +08:00
{
2015-02-11 18:14:22 +08:00
iter->release();
}
}
2021-12-26 23:26:34 +08:00
Particle3DModelRender* Particle3DModelRender::create(std::string_view modelFile, std::string_view texFile)
2015-02-11 18:14:22 +08:00
{
2021-12-25 10:04:45 +08:00
auto ret = new Particle3DModelRender();
2015-02-11 18:14:22 +08:00
ret->_modelFile = modelFile;
2021-12-25 10:04:45 +08:00
ret->_texFile = texFile;
2015-02-11 18:14:22 +08:00
return ret;
}
2021-12-25 10:04:45 +08:00
void Particle3DModelRender::render(Renderer* renderer, const Mat4& transform, ParticleSystem3D* particleSystem)
2015-02-11 18:14:22 +08:00
{
if (!_isVisible)
return;
2021-12-25 10:04:45 +08:00
if (_meshList.empty())
2021-12-25 10:04:45 +08:00
{
for (unsigned int i = 0; i < particleSystem->getParticleQuota(); ++i)
{
MeshRenderer* mesh = MeshRenderer::create(_modelFile);
if (mesh == nullptr)
{
CCLOG("failed to load file %s", _modelFile.c_str());
continue;
}
mesh->setTexture(_texFile);
mesh->retain();
_meshList.push_back(mesh);
2015-02-11 18:14:22 +08:00
}
if (!_meshList.empty())
2021-12-25 10:04:45 +08:00
{
const AABB& aabb = _meshList[0]->getAABB();
2015-02-11 18:14:22 +08:00
Vec3 corners[8];
aabb.getCorners(corners);
_meshSize = corners[3] - corners[6];
2015-02-11 18:14:22 +08:00
}
}
2021-12-25 10:04:45 +08:00
const ParticlePool& particlePool = particleSystem->getParticlePool();
2015-03-02 13:07:32 +08:00
ParticlePool::PoolList activeParticleList = particlePool.getActiveDataList();
2015-02-11 18:14:22 +08:00
Mat4 mat;
Mat4 rotMat;
Mat4 sclMat;
Quaternion q;
transform.decompose(nullptr, &q, nullptr);
2015-03-02 13:07:32 +08:00
unsigned int index = 0;
for (auto iter : activeParticleList)
2015-02-11 18:14:22 +08:00
{
2015-03-02 13:07:32 +08:00
auto particle = iter;
2015-04-03 12:43:14 +08:00
Mat4::createRotation(q * particle->orientation, &rotMat);
sclMat.m[0] = particle->width / _meshSize.x;
sclMat.m[5] = particle->height / _meshSize.y;
sclMat.m[10] = particle->depth / _meshSize.z;
2021-12-25 10:04:45 +08:00
mat = rotMat * sclMat;
mat.m[12] = particle->position.x;
mat.m[13] = particle->position.y;
mat.m[14] = particle->position.z;
_meshList[index++]->draw(renderer, mat, 0);
2015-02-11 18:14:22 +08:00
}
}
void Particle3DModelRender::reset()
{
for (auto iter : _meshList)
2021-12-25 10:04:45 +08:00
{
iter->release();
}
_meshList.clear();
}
// MARK: Particle3DRender
Particle3DRender::Particle3DRender()
2021-12-25 10:04:45 +08:00
: _particleSystem(nullptr), _isVisible(true), _rendererScale(Vec3::ONE), _depthTest(true), _depthWrite(false)
{
_stateBlock.setCullFace(false);
_stateBlock.setCullFaceSide(backend::CullMode::BACK);
_stateBlock.setDepthTest(false);
_stateBlock.setDepthWrite(false);
_stateBlock.setBlend(true);
};
2021-12-25 10:04:45 +08:00
Particle3DRender::~Particle3DRender() {}
2021-12-25 10:04:45 +08:00
void Particle3DRender::copyAttributesTo(Particle3DRender* render)
{
2021-12-25 10:04:45 +08:00
render->_stateBlock = _stateBlock;
render->_isVisible = _isVisible;
render->_rendererScale = _rendererScale;
2021-12-25 10:04:45 +08:00
render->_depthTest = _depthTest;
render->_depthWrite = _depthWrite;
}
2015-02-11 18:14:22 +08:00
2015-03-02 17:47:26 +08:00
void Particle3DRender::notifyStart()
2015-02-11 18:14:22 +08:00
{
setVisible(true);
}
2015-03-02 17:47:26 +08:00
void Particle3DRender::notifyStop()
2015-02-11 18:14:22 +08:00
{
setVisible(false);
}
2021-12-25 10:04:45 +08:00
void Particle3DRender::notifyRescaled(const Vec3& scale)
2015-02-11 18:14:22 +08:00
{
_rendererScale = scale;
}
2021-12-25 10:04:45 +08:00
void Particle3DRender::setDepthTest(bool isDepthTest)
{
_depthTest = isDepthTest;
_stateBlock.setDepthTest(_depthTest);
}
2021-12-25 10:04:45 +08:00
void Particle3DRender::setDepthWrite(bool isDepthWrite)
{
_depthWrite = isDepthWrite;
_stateBlock.setDepthWrite(_depthWrite);
}
2021-12-25 10:04:45 +08:00
void Particle3DRender::setBlendFunc(const BlendFunc& blendFunc)
{
_stateBlock.setBlendFunc(blendFunc);
}
2015-02-11 18:14:22 +08:00
NS_CC_END