axmol/extensions/Particle3D/PU/CCPURender.cpp

1066 lines
38 KiB
C++
Raw Normal View History

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
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 "extensions/Particle3D/CCParticleSystem3D.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 "extensions/Particle3D/PU/CCPURender.h"
#include "extensions/Particle3D/PU/CCPUParticleSystem3D.h"
#include "extensions/Particle3D/PU/CCPUUtil.h"
#include "renderer/CCMeshCommand.h"
#include "renderer/CCRenderer.h"
#include "renderer/CCTextureCache.h"
#include "renderer/ccShaders.h"
#include "renderer/backend/Device.h"
#include "renderer/backend/Buffer.h"
#include "base/CCDirector.h"
#include "3d/CCMeshRenderer.h"
2019-11-23 20:27:39 +08:00
#include "3d/CCMesh.h"
#include "2d/CCCamera.h"
NS_AX_BEGIN
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
void PURender::updateRender(PUParticle3D* /*particle*/, float /*deltaTime*/, bool /*firstParticle*/) {}
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
void PURender::copyAttributesTo(PURender* render)
2019-11-23 20:27:39 +08:00
{
Particle3DRender::copyAttributesTo(render);
render->_renderType = _renderType;
}
2021-12-25 10:04:45 +08:00
// static bool compareParticle3D(PUParticle3D* left, PUParticle3D* right)
2019-11-23 20:27:39 +08:00
//{
2021-12-25 10:04:45 +08:00
// return left->depthInView > right->depthInView;
// }
2019-11-23 20:27:39 +08:00
PUParticle3DQuadRender* PUParticle3DQuadRender::create(std::string_view texFile)
2019-11-23 20:27:39 +08:00
{
2021-12-08 00:11:53 +08:00
auto ret = new PUParticle3DQuadRender();
if (ret->initRender(texFile))
2019-11-23 20:27:39 +08:00
{
ret->_texFile = texFile;
ret->autorelease();
}
else
{
2022-07-16 10:43:05 +08:00
AX_SAFE_DELETE(ret);
2019-11-23 20:27:39 +08:00
}
return ret;
}
2021-12-25 10:04:45 +08:00
void PUParticle3DQuadRender::render(Renderer* renderer, const Mat4& transform, ParticleSystem3D* particleSystem)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
// batch and generate draw
const ParticlePool& particlePool = particleSystem->getParticlePool();
2019-11-23 20:27:39 +08:00
if (!_isVisible || particlePool.empty())
return;
2021-12-25 10:04:45 +08:00
if (_vertexBuffer == nullptr)
{
2019-11-23 20:27:39 +08:00
size_t stride = sizeof(VertexInfo);
2021-12-25 10:04:45 +08:00
_vertexBuffer =
backend::Device::getInstance()->newBuffer(stride * 4 * particleSystem->getParticleQuota(),
backend::BufferType::VERTEX, backend::BufferUsage::DYNAMIC);
2019-11-23 20:27:39 +08:00
if (_vertexBuffer == nullptr)
{
2022-07-16 10:43:05 +08:00
AXLOG("PUParticle3DQuadRender::render create vertex buffer failed");
2019-11-23 20:27:39 +08:00
return;
}
}
2021-12-25 10:04:45 +08:00
if (_indexBuffer == nullptr)
{
_indexBuffer =
backend::Device::getInstance()->newBuffer(6 * particleSystem->getParticleQuota() * sizeof(uint16_t),
backend::BufferType::INDEX, backend::BufferUsage::DYNAMIC);
2019-11-23 20:27:39 +08:00
if (_indexBuffer == nullptr)
{
2022-07-16 10:43:05 +08:00
AXLOG("PUParticle3DQuadRender::render create index buffer failed");
2019-11-23 20:27:39 +08:00
return;
}
}
2021-12-25 10:04:45 +08:00
const ParticlePool::PoolList& activeParticleList = particlePool.getActiveDataList();
2019-11-23 20:27:39 +08:00
if (_vertices.size() < activeParticleList.size() * 4)
{
_vertices.resize(activeParticleList.size() * 4);
_indices.resize(activeParticleList.size() * 6);
}
2021-12-25 10:04:45 +08:00
auto camera = Camera::getVisitingCamera();
2019-11-23 20:27:39 +08:00
auto cameraMat = camera->getNodeToWorldTransform();
// for (auto&& iter : activeParticleList){
2021-12-25 10:04:45 +08:00
// iter->depthInView = -(viewMat.m[2] * iter->positionInWorld.x + viewMat.m[6] * iter->positionInWorld.y +
// viewMat.m[10] * iter->positionInWorld.z + viewMat.m[14]);
// }
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
// std::sort(activeParticleList.begin(), activeParticleList.end(), compareParticle3D);
2019-11-23 20:27:39 +08:00
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
2019-11-23 20:27:39 +08:00
Mat4 pRotMat;
2021-12-25 10:04:45 +08:00
Vec3 position; // particle position
2019-11-23 20:27:39 +08:00
int vertexindex = 0;
2021-12-25 10:04:45 +08:00
int index = 0;
int offsetX, offsetY;
2019-11-23 20:27:39 +08:00
getOriginOffset(offsetX, offsetY);
2021-12-25 10:04:45 +08:00
if (_type == PERPENDICULAR_COMMON)
{
2019-11-23 20:27:39 +08:00
up = _commonUp;
up.normalize();
Vec3::cross(up, _commonDir, &right);
right.normalize();
backward = _commonDir;
2021-12-25 10:04:45 +08:00
}
else if (_type == ORIENTED_COMMON)
{
2019-11-23 20:27:39 +08:00
up = _commonDir;
up.normalize();
Vec3::cross(up, backward, &right);
right.normalize();
}
for (auto&& iter : activeParticleList)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
auto particle = static_cast<PUParticle3D*>(iter);
2019-11-23 20:27:39 +08:00
determineUVCoords(particle);
2021-12-25 10:04:45 +08:00
if (_type == ORIENTED_SELF)
{
2019-11-23 20:27:39 +08:00
Vec3 direction = particle->direction;
2021-12-25 10:04:45 +08:00
// transform.transformVector(particle->direction, &direction);
2019-11-23 20:27:39 +08:00
up = direction;
up.normalize();
Vec3::cross(direction, backward, &right);
right.normalize();
2021-12-25 10:04:45 +08:00
}
else if (_type == PERPENDICULAR_SELF)
{
2019-11-23 20:27:39 +08:00
Vec3 direction = particle->direction;
2021-12-25 10:04:45 +08:00
// transform.transformVector(particle->direction, &direction);
2019-11-23 20:27:39 +08:00
direction.normalize();
2021-12-25 10:04:45 +08:00
// up = PUUtil::perpendicular(direction);
// up.normalize();
2019-11-23 20:27:39 +08:00
Vec3::cross(_commonUp, direction, &right);
right.normalize();
Vec3::cross(direction, right, &up);
up.normalize();
backward = direction;
2021-12-25 10:04:45 +08:00
}
else if (_type == ORIENTED_SHAPE)
{
2019-11-23 20:27:39 +08:00
up.set(particle->orientation.x, particle->orientation.y, particle->orientation.z);
up.normalize();
Vec3::cross(up, backward, &right);
right.normalize();
}
2021-12-25 10:04:45 +08:00
Vec3 halfwidth = particle->width * 0.5f * right;
2019-11-23 20:27:39 +08:00
Vec3 halfheight = particle->height * 0.5f * up;
2021-12-25 10:04:45 +08:00
Vec3 offset = halfwidth * offsetX + halfheight * offsetY;
// transform.transformPoint(particle->position, &position);
2019-11-23 20:27:39 +08:00
position = particle->position;
2021-12-25 10:04:45 +08:00
if (_rotateType == TEXTURE_COORDS)
{
2019-11-23 20:27:39 +08:00
float costheta = cosf(-particle->zRotation);
float sintheta = sinf(-particle->zRotation);
Vec2 texOffset = 0.5f * (particle->lb_uv + particle->rt_uv);
Vec2 val;
val.set((particle->lb_uv.x - texOffset.x), (particle->lb_uv.y - texOffset.y));
val.set(val.x * costheta - val.y * sintheta, val.x * sintheta + val.y * costheta);
fillVertex(vertexindex, (position + (-halfwidth - halfheight + offset)), particle->color, val + texOffset);
val.set(particle->rt_uv.x - texOffset.x, particle->lb_uv.y - texOffset.y);
val.set(val.x * costheta - val.y * sintheta, val.x * sintheta + val.y * costheta);
2021-12-25 10:04:45 +08:00
fillVertex(vertexindex + 1, (position + (halfwidth - halfheight + offset)), particle->color,
val + texOffset);
2019-11-23 20:27:39 +08:00
val.set(particle->lb_uv.x - texOffset.x, particle->rt_uv.y - texOffset.y);
val.set(val.x * costheta - val.y * sintheta, val.x * sintheta + val.y * costheta);
2021-12-25 10:04:45 +08:00
fillVertex(vertexindex + 2, (position + (-halfwidth + halfheight + offset)), particle->color,
val + texOffset);
2019-11-23 20:27:39 +08:00
val.set(particle->rt_uv.x - texOffset.x, particle->rt_uv.y - texOffset.y);
val.set(val.x * costheta - val.y * sintheta, val.x * sintheta + val.y * costheta);
2021-12-25 10:04:45 +08:00
fillVertex(vertexindex + 3, (position + (halfwidth + halfheight + offset)), particle->color,
val + texOffset);
}
else
{
2019-11-23 20:27:39 +08:00
Mat4::createRotation(backward, -particle->zRotation, &pRotMat);
2021-12-25 10:04:45 +08:00
fillVertex(vertexindex, (position + pRotMat * (-halfwidth - halfheight + offset)), particle->color,
particle->lb_uv);
fillVertex(vertexindex + 1, (position + pRotMat * (halfwidth - halfheight + offset)), particle->color,
Vec2(particle->rt_uv.x, particle->lb_uv.y));
fillVertex(vertexindex + 2, (position + pRotMat * (-halfwidth + halfheight + offset)), particle->color,
Vec2(particle->lb_uv.x, particle->rt_uv.y));
fillVertex(vertexindex + 3, (position + pRotMat * (halfwidth + halfheight + offset)), particle->color,
particle->rt_uv);
2019-11-23 20:27:39 +08:00
}
fillTriangle(index, vertexindex, vertexindex + 1, vertexindex + 3);
fillTriangle(index + 3, vertexindex, vertexindex + 3, vertexindex + 2);
2021-12-25 10:04:45 +08:00
//_posuvcolors[vertexindex].position = (position + (- halfwidth - halfheight + halfwidth * offsetX + halfheight
//* offsetY)); _posuvcolors[vertexindex].color = particle->color; _posuvcolors[vertexindex].uv.set(val.x +
// texOffset.x, val.y + texOffset.y);
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
// val.set(particle->rt_uv.x - texOffset.x, particle->lb_uv.y - texOffset.y);
// val.set(val.x * costheta - val.y * sintheta, val.x * sintheta + val.y * costheta);
//_posuvcolors[vertexindex + 1].position = (position + (halfwidth - halfheight + halfwidth * offsetX +
// halfheight * offsetY)); _posuvcolors[vertexindex + 1].color = particle->color; _posuvcolors[vertexindex +
// 1].uv.set(val.x + texOffset.x, val.y + texOffset.y);
2019-11-23 20:27:39 +08:00
//
2021-12-25 10:04:45 +08:00
// val.set(particle->lb_uv.x - texOffset.x, particle->rt_uv.y - texOffset.y);
// val.set(val.x * costheta - val.y * sintheta, val.x * sintheta + val.y * costheta);
//_posuvcolors[vertexindex + 2].position = (position + (- halfwidth + halfheight + halfwidth * offsetX +
// halfheight * offsetY)); _posuvcolors[vertexindex + 2].color = particle->color; _posuvcolors[vertexindex +
// 2].uv.set(val.x + texOffset.x, val.y + texOffset.y);
2019-11-23 20:27:39 +08:00
//
2021-12-25 10:04:45 +08:00
// val.set(particle->rt_uv.x - texOffset.x, particle->rt_uv.y - texOffset.y);
// val.set(val.x * costheta - val.y * sintheta, val.x * sintheta + val.y * costheta);
//_posuvcolors[vertexindex + 3].position = (position + (halfwidth + halfheight + halfwidth * offsetX +
// halfheight * offsetY)); _posuvcolors[vertexindex + 3].color = particle->color; _posuvcolors[vertexindex +
// 3].uv.set(val.x + texOffset.x, val.y + texOffset.y);
2019-11-23 20:27:39 +08:00
//
//
//_indexData[index] = vertexindex;
//_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
2019-11-23 20:27:39 +08:00
index += 6;
vertexindex += 4;
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
_vertices.erase(_vertices.begin() + vertexindex, _vertices.end());
_indices.erase(_indices.begin() + index, _indices.end());
2021-12-25 10:04:45 +08:00
if (!_vertices.empty() && !_indices.empty())
{
2019-11-23 20:27:39 +08:00
_vertexBuffer->updateData(&_vertices[0], vertexindex * sizeof(_vertices[0]));
_indexBuffer->updateData(&_indices[0], index * sizeof(_indices[0]));
_stateBlock.setBlendFunc(particleSystem->getBlendFunc());
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
_meshCommand.init(0.0);
_meshCommand.setSkipBatching(true);
_meshCommand.setTransparent(true);
_meshCommand.setVertexBuffer(_vertexBuffer);
_meshCommand.setIndexBuffer(_indexBuffer, MeshCommand::IndexFormat::U_SHORT);
_meshCommand.setIndexDrawInfo(0, index);
if (_texture)
{
_programState->setTexture(_locTexture, 0, _texture->getBackendTexture());
}
auto uColor = Vec4(1, 1, 1, 1);
_programState->setUniform(_locColor, &uColor, sizeof(uColor));
2021-12-25 10:04:45 +08:00
auto& projectionMatrix = Director::getInstance()->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
2019-11-23 20:27:39 +08:00
_programState->setUniform(_locPMatrix, &projectionMatrix.m, sizeof(projectionMatrix.m));
renderer->addCommand(&_meshCommand);
}
}
void PUParticle3DEntityRender::onBeforeDraw()
{
2021-12-25 10:04:45 +08:00
auto* renderer = Director::getInstance()->getRenderer();
auto& pipelineDescriptor = _meshCommand.getPipelineDescriptor();
2019-11-23 20:27:39 +08:00
_rendererDepthTestEnabled = renderer->getDepthTest();
2021-12-25 10:04:45 +08:00
_rendererDepthCmpFunc = renderer->getDepthCompareFunction();
_rendererCullMode = renderer->getCullMode();
_rendererDepthWrite = renderer->getDepthWrite();
_rendererWinding = renderer->getWinding();
2019-11-23 20:27:39 +08:00
_stateBlock.bind(&pipelineDescriptor);
renderer->setDepthTest(true);
}
void PUParticle3DEntityRender::onAfterDraw()
{
2021-12-25 10:04:45 +08:00
auto* renderer = Director::getInstance()->getRenderer();
2019-11-23 20:27:39 +08:00
renderer->setDepthTest(_rendererDepthTestEnabled);
renderer->setDepthCompareFunction(_rendererDepthCmpFunc);
renderer->setCullMode(_rendererCullMode);
renderer->setDepthWrite(_rendererDepthWrite);
renderer->setWinding(_rendererWinding);
}
PUParticle3DQuadRender::PUParticle3DQuadRender()
: _type(POINT)
, _origin(CENTER)
, _rotateType(TEXTURE_COORDS)
, _commonDir(Vec3(0.0f, 0.0f, 1.0f))
, _commonUp(Vec3(0.0f, 1.0f, 0.0f))
, _textureCoordsRows(1)
, _textureCoordsColumns(1)
, _textureCoordsRowStep(1.0f)
, _textureCoordsColStep(1.0f)
{
autoRotate = false;
}
2021-12-25 10:04:45 +08:00
PUParticle3DQuadRender::~PUParticle3DQuadRender() {}
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
void PUParticle3DQuadRender::getOriginOffset(int& offsetX, int& offsetY)
2019-11-23 20:27:39 +08:00
{
switch (_origin)
{
case TOP_LEFT:
2021-12-25 10:04:45 +08:00
{
offsetX = 1;
offsetY = -1;
}
break;
2019-11-23 20:27:39 +08:00
case TOP_CENTER:
2021-12-25 10:04:45 +08:00
{
offsetX = 0;
offsetY = -1;
}
break;
2019-11-23 20:27:39 +08:00
case TOP_RIGHT:
2021-12-25 10:04:45 +08:00
{
offsetX = -1;
offsetY = -1;
}
break;
2019-11-23 20:27:39 +08:00
case CENTER_LEFT:
2021-12-25 10:04:45 +08:00
{
offsetX = 1;
offsetY = 0;
}
break;
2019-11-23 20:27:39 +08:00
case CENTER:
2021-12-25 10:04:45 +08:00
{
offsetX = 0;
offsetY = 0;
}
break;
2019-11-23 20:27:39 +08:00
case CENTER_RIGHT:
2021-12-25 10:04:45 +08:00
{
offsetX = -1;
offsetY = 0;
}
break;
2019-11-23 20:27:39 +08:00
case BOTTOM_LEFT:
2021-12-25 10:04:45 +08:00
{
offsetX = 1;
offsetY = 1;
}
break;
2019-11-23 20:27:39 +08:00
case BOTTOM_CENTER:
2021-12-25 10:04:45 +08:00
{
offsetX = 0;
offsetY = 1;
}
break;
2019-11-23 20:27:39 +08:00
case BOTTOM_RIGHT:
2021-12-25 10:04:45 +08:00
{
offsetX = -1;
offsetY = 1;
}
break;
2019-11-23 20:27:39 +08:00
}
}
unsigned short PUParticle3DQuadRender::getTextureCoordsRows() const
{
return _textureCoordsRows;
}
2021-12-25 10:04:45 +08:00
void PUParticle3DQuadRender::setTextureCoordsRows(unsigned short textureCoordsRows)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
_textureCoordsRows = textureCoordsRows;
2019-11-23 20:27:39 +08:00
_textureCoordsRowStep = 1.0f / (float)_textureCoordsRows;
}
unsigned short PUParticle3DQuadRender::getTextureCoordsColumns() const
{
return _textureCoordsColumns;
}
2021-12-25 10:04:45 +08:00
void PUParticle3DQuadRender::setTextureCoordsColumns(unsigned short textureCoordsColumns)
2019-11-23 20:27:39 +08:00
{
_textureCoordsColumns = textureCoordsColumns;
_textureCoordsColStep = 1.0f / (float)_textureCoordsColumns;
}
unsigned int PUParticle3DQuadRender::getNumTextureCoords()
{
return _textureCoordsColumns * _textureCoordsRows;
}
2021-12-25 10:04:45 +08:00
void PUParticle3DQuadRender::determineUVCoords(PUParticle3D* particle)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
if (_textureCoordsColumns == 1 && _textureCoordsRows == 1)
return;
2019-11-23 20:27:39 +08:00
unsigned short currentRow = particle->textureCoordsCurrent / _textureCoordsColumns;
unsigned short currentCol = particle->textureCoordsCurrent - _textureCoordsColumns * currentRow;
2021-12-25 10:04:45 +08:00
currentRow = _textureCoordsRows - currentRow - 1;
2019-11-23 20:27:39 +08:00
particle->lb_uv.set(_textureCoordsColStep * currentCol, _textureCoordsRowStep * currentRow);
particle->rt_uv = particle->lb_uv + Vec2(_textureCoordsColStep, _textureCoordsRowStep);
}
2021-12-25 10:04:45 +08:00
void PUParticle3DQuadRender::fillVertex(unsigned short index, const Vec3& pos, const Vec4& color, const Vec2& uv)
2019-11-23 20:27:39 +08:00
{
_vertices[index].position = pos;
2021-12-25 10:04:45 +08:00
_vertices[index].color = color;
_vertices[index].uv = uv;
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
void PUParticle3DQuadRender::fillTriangle(unsigned short index, unsigned short v0, unsigned short v1, unsigned short v2)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
_indices[index] = v0;
2019-11-23 20:27:39 +08:00
_indices[index + 1] = v1;
_indices[index + 2] = v2;
}
2021-12-25 10:04:45 +08:00
void PUParticle3DQuadRender::setType(Type type)
2019-11-23 20:27:39 +08:00
{
_type = type;
2021-12-25 10:04:45 +08:00
if (_type == PERPENDICULAR_COMMON || _type == PERPENDICULAR_SELF)
{
2019-11-23 20:27:39 +08:00
_stateBlock.setCullFace(false);
2021-12-25 10:04:45 +08:00
}
else
{
2019-11-23 20:27:39 +08:00
_stateBlock.setCullFace(true);
}
}
2021-12-25 10:04:45 +08:00
void PUParticle3DQuadRender::copyAttributesTo(PUParticle3DQuadRender* quadRender)
2019-11-23 20:27:39 +08:00
{
PURender::copyAttributesTo(quadRender);
2021-12-25 10:04:45 +08:00
quadRender->_type = _type;
quadRender->_origin = _origin;
quadRender->_rotateType = _rotateType;
quadRender->_commonDir = _commonDir;
quadRender->_commonUp = _commonUp;
quadRender->_textureCoordsRows = _textureCoordsRows;
2019-11-23 20:27:39 +08:00
quadRender->_textureCoordsColumns = _textureCoordsColumns;
quadRender->_textureCoordsRowStep = _textureCoordsRowStep;
quadRender->_textureCoordsColStep = _textureCoordsColStep;
}
PUParticle3DQuadRender* PUParticle3DQuadRender::clone()
{
auto render = PUParticle3DQuadRender::create(_texFile);
copyAttributesTo(render);
return render;
}
PUParticle3DModelRender* PUParticle3DModelRender::create(std::string_view modelFile, std::string_view texFile /*= ""*/)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
auto ret = new PUParticle3DModelRender();
2019-11-23 20:27:39 +08:00
ret->_modelFile = modelFile;
2021-12-25 10:04:45 +08:00
ret->_texFile = texFile;
2019-11-23 20:27:39 +08:00
return ret;
}
2021-12-25 10:04:45 +08:00
void PUParticle3DModelRender::render(Renderer* renderer, const Mat4& transform, ParticleSystem3D* particleSystem)
2019-11-23 20:27:39 +08:00
{
if (!_isVisible)
return;
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)
2019-11-23 20:27:39 +08:00
{
2022-07-16 10:43:05 +08:00
AXLOG("failed to load file %s", _modelFile.c_str());
2019-11-23 20:27:39 +08:00
continue;
}
mesh->setTexture(_texFile);
mesh->setBlendFunc(particleSystem->getBlendFunc());
mesh->setCullFaceEnabled(false);
mesh->retain();
_meshList.push_back(mesh);
2019-11-23 20:27:39 +08:00
}
if (!_meshList.empty())
2021-12-25 10:04:45 +08:00
{
const AABB& aabb = _meshList[0]->getAABB();
2019-11-23 20:27:39 +08:00
Vec3 corners[8];
aabb.getCorners(corners);
_meshSize = corners[3] - corners[6];
2021-12-25 10:04:45 +08:00
}
else
{
2019-11-23 20:27:39 +08:00
_isVisible = false;
return;
}
}
2021-12-25 10:04:45 +08:00
const ParticlePool& particlePool = particleSystem->getParticlePool();
2019-11-23 20:27:39 +08:00
ParticlePool::PoolList activeParticleList = particlePool.getActiveDataList();
Mat4 mat;
Mat4 rotMat;
Mat4 sclMat;
Quaternion q;
transform.decompose(nullptr, &q, nullptr);
unsigned int index = 0;
for (auto&& iter : activeParticleList)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
auto particle = static_cast<PUParticle3D*>(iter);
2019-11-23 20:27:39 +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;
if (_meshList[index]->getCameraMask() != particleSystem->getCameraMask())
_meshList[index]->setCameraMask(particleSystem->getCameraMask());
_meshList[index]->setColor(
2021-12-25 10:04:45 +08:00
Color3B(particle->color.x * 255, particle->color.y * 255, particle->color.z * 255));
_meshList[index]->setOpacity(particle->color.w * 255);
_meshList[index]->visit(renderer, mat, Node::FLAGS_DIRTY_MASK);
2019-11-23 20:27:39 +08:00
++index;
}
}
PUParticle3DModelRender::PUParticle3DModelRender()
{
autoRotate = true;
}
PUParticle3DModelRender::~PUParticle3DModelRender()
{
for (auto&& iter : _meshList)
2021-12-25 10:04:45 +08:00
{
2019-11-23 20:27:39 +08:00
iter->release();
}
}
2021-12-25 10:04:45 +08:00
void PUParticle3DModelRender::copyAttributesTo(PUParticle3DModelRender* render)
2019-11-23 20:27:39 +08:00
{
PURender::copyAttributesTo(render);
}
PUParticle3DModelRender* PUParticle3DModelRender::clone()
{
auto mr = PUParticle3DModelRender::create(_modelFile, _texFile);
copyAttributesTo(mr);
return mr;
}
void PUParticle3DModelRender::reset()
{
for (auto&& iter : _meshList)
2021-12-25 10:04:45 +08:00
{
2019-11-23 20:27:39 +08:00
iter->release();
}
_meshList.clear();
2019-11-23 20:27:39 +08:00
}
PUParticle3DEntityRender::PUParticle3DEntityRender()
2021-12-25 10:04:45 +08:00
: _texture(nullptr), _programState(nullptr), _indexBuffer(nullptr), _vertexBuffer(nullptr)
2019-11-23 20:27:39 +08:00
{
_stateBlock.setCullFace(false);
_stateBlock.setCullFaceSide(backend::CullMode::BACK);
_stateBlock.setDepthTest(false);
_stateBlock.setDepthWrite(false);
_stateBlock.setBlend(true);
}
PUParticle3DEntityRender::~PUParticle3DEntityRender()
2021-12-25 10:04:45 +08:00
{
;
2022-07-16 10:43:05 +08:00
// AX_SAFE_RELEASE(_texture);
AX_SAFE_RELEASE(_programState);
AX_SAFE_RELEASE(_vertexBuffer);
AX_SAFE_RELEASE(_indexBuffer);
2019-11-23 20:27:39 +08:00
}
bool PUParticle3DEntityRender::initRender(std::string_view texFile)
2019-11-23 20:27:39 +08:00
{
2022-07-16 10:43:05 +08:00
AX_SAFE_RELEASE_NULL(_programState);
2019-11-23 20:27:39 +08:00
if (!texFile.empty())
{
auto tex = Director::getInstance()->getTextureCache()->addImage(texFile);
if (tex)
{
2021-12-25 10:04:45 +08:00
_texture = tex;
2019-11-23 20:27:39 +08:00
auto* program = backend::Program::getBuiltinProgram(backend::ProgramType::PARTICLE_TEXTURE_3D);
_programState = new backend::ProgramState(program);
}
}
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();
2019-11-23 20:27:39 +08:00
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-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
layout->setAttribute("a_position", iter->second.location, backend::VertexFormat::FLOAT3,
offsetof(VertexInfo, position), false);
2019-11-23 20:27:39 +08:00
}
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(VertexInfo, uv), 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::FLOAT4,
offsetof(VertexInfo, color), false);
2019-11-23 20:27:39 +08:00
}
layout->setLayout(sizeof(VertexInfo));
2021-12-25 10:04:45 +08:00
_locColor = _programState->getUniformLocation("u_color");
2019-11-23 20:27:39 +08:00
_locPMatrix = _programState->getUniformLocation("u_PMatrix");
_locTexture = _programState->getUniformLocation("u_tex0");
2019-11-23 20:27:39 +08:00
_meshCommand.setTransparent(true);
_meshCommand.setSkipBatching(true);
_stateBlock.setDepthTest(true);
_stateBlock.setDepthWrite(false);
_stateBlock.setCullFaceSide(backend::CullMode::BACK);
_stateBlock.setCullFace(true);
2022-07-16 10:43:05 +08:00
_meshCommand.setBeforeCallback(AX_CALLBACK_0(PUParticle3DEntityRender::onBeforeDraw, this));
_meshCommand.setAfterCallback(AX_CALLBACK_0(PUParticle3DEntityRender::onAfterDraw, this));
2019-11-23 20:27:39 +08:00
return true;
}
2021-12-25 10:04:45 +08:00
void PUParticle3DEntityRender::copyAttributesTo(PUParticle3DEntityRender* render)
2019-11-23 20:27:39 +08:00
{
PURender::copyAttributesTo(render);
}
void PUParticle3DEntityRender::reset()
{
this->initRender(_texFile);
}
PUParticle3DBoxRender::PUParticle3DBoxRender()
{
autoRotate = false;
}
2021-12-25 10:04:45 +08:00
PUParticle3DBoxRender::~PUParticle3DBoxRender() {}
2019-11-23 20:27:39 +08:00
PUParticle3DBoxRender* PUParticle3DBoxRender::create(std::string_view texFile)
2019-11-23 20:27:39 +08:00
{
2021-12-08 00:11:53 +08:00
auto ret = new PUParticle3DBoxRender();
if (ret->initRender(texFile))
2019-11-23 20:27:39 +08:00
{
ret->autorelease();
}
else
{
2022-07-16 10:43:05 +08:00
AX_SAFE_DELETE(ret);
2019-11-23 20:27:39 +08:00
}
return ret;
}
2021-12-25 10:04:45 +08:00
void PUParticle3DBoxRender::render(Renderer* renderer, const Mat4& transform, ParticleSystem3D* particleSystem)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
// batch and generate draw
const ParticlePool& particlePool = particleSystem->getParticlePool();
2019-11-23 20:27:39 +08:00
if (!_isVisible || particlePool.empty())
return;
2021-12-25 10:04:45 +08:00
auto camera = Camera::getVisitingCamera();
2019-11-23 20:27:39 +08:00
auto cameraMat = camera->getNodeToWorldTransform();
Vec3 backward(cameraMat.m[8], cameraMat.m[9], cameraMat.m[10]);
2021-12-25 10:04:45 +08:00
if (_vertexBuffer == nullptr && _indexBuffer == nullptr)
{
2019-11-23 20:27:39 +08:00
size_t stride = sizeof(VertexInfo);
2021-12-25 10:04:45 +08:00
_vertexBuffer =
backend::Device::getInstance()->newBuffer(stride * 8 * particleSystem->getParticleQuota(),
backend::BufferType::VERTEX, backend::BufferUsage::DYNAMIC);
2019-11-23 20:27:39 +08:00
if (_vertexBuffer == nullptr)
{
2022-07-16 10:43:05 +08:00
AXLOG("PUParticle3DBoxRender::render create vertex buffer failed");
2019-11-23 20:27:39 +08:00
return;
}
_vertices.resize(8 * particleSystem->getParticleQuota());
2021-12-25 10:04:45 +08:00
_indexBuffer =
backend::Device::getInstance()->newBuffer(sizeof(uint16_t) * 36 * particleSystem->getParticleQuota(),
backend::BufferType::INDEX, backend::BufferUsage::DYNAMIC);
2019-11-23 20:27:39 +08:00
if (_indexBuffer == nullptr)
{
2022-07-16 10:43:05 +08:00
AXLOG("PUParticle3DBoxRender::render create index buffer failed");
2019-11-23 20:27:39 +08:00
return;
}
_indices.resize(36 * particleSystem->getParticleQuota());
reBuildIndices(particleSystem->getParticleQuota());
}
unsigned int vertexindex = 0;
2021-12-25 10:04:45 +08:00
unsigned int index = 0;
2019-11-23 20:27:39 +08:00
Mat4 texRot;
Vec3 val;
for (auto&& iter : particlePool.getActiveDataList())
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
auto particle = static_cast<PUParticle3D*>(iter);
2019-11-23 20:27:39 +08:00
float halfHeight = particle->height * 0.5f;
2021-12-25 10:04:45 +08:00
float halfWidth = particle->width * 0.5f;
float halfDepth = particle->depth * 0.5f;
2019-11-23 20:27:39 +08:00
Mat4::createRotation(backward, particle->zRotation, &texRot);
2021-12-25 10:04:45 +08:00
val = texRot * Vec3(0.0f, 0.75f, 0.0);
2019-11-23 20:27:39 +08:00
_vertices[vertexindex + 0].position = particle->position + Vec3(-halfWidth, -halfHeight, halfDepth);
2021-12-25 10:04:45 +08:00
_vertices[vertexindex + 0].color = particle->color;
_vertices[vertexindex + 0].uv.x = val.x;
_vertices[vertexindex + 0].uv.y = val.y;
val = texRot * Vec3(0.0f, 0.25f, 0.0);
2019-11-23 20:27:39 +08:00
_vertices[vertexindex + 1].position = particle->position + Vec3(halfWidth, -halfHeight, halfDepth);
2021-12-25 10:04:45 +08:00
_vertices[vertexindex + 1].color = particle->color;
_vertices[vertexindex + 1].uv.x = val.x;
_vertices[vertexindex + 1].uv.y = val.y;
val = texRot * Vec3(0.5f, 0.25f, 0.0);
2019-11-23 20:27:39 +08:00
_vertices[vertexindex + 2].position = particle->position + Vec3(halfWidth, halfHeight, halfDepth);
2021-12-25 10:04:45 +08:00
_vertices[vertexindex + 2].color = particle->color;
_vertices[vertexindex + 2].uv.x = val.x;
_vertices[vertexindex + 2].uv.y = val.y;
val = texRot * Vec3(0.5f, 0.75f, 0.0);
2019-11-23 20:27:39 +08:00
_vertices[vertexindex + 3].position = particle->position + Vec3(-halfWidth, halfHeight, halfDepth);
2021-12-25 10:04:45 +08:00
_vertices[vertexindex + 3].color = particle->color;
_vertices[vertexindex + 3].uv.x = val.x;
_vertices[vertexindex + 3].uv.y = val.y;
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
val = texRot * Vec3(0.0f, 0.0f, 0.0);
2019-11-23 20:27:39 +08:00
_vertices[vertexindex + 4].position = particle->position + Vec3(halfWidth, -halfHeight, -halfDepth);
2021-12-25 10:04:45 +08:00
_vertices[vertexindex + 4].color = particle->color;
_vertices[vertexindex + 4].uv.x = val.x;
_vertices[vertexindex + 4].uv.y = val.y;
val = texRot * Vec3(0.0f, 1.0f, 0.0);
2019-11-23 20:27:39 +08:00
_vertices[vertexindex + 5].position = particle->position + Vec3(-halfWidth, -halfHeight, -halfDepth);
2021-12-25 10:04:45 +08:00
_vertices[vertexindex + 5].color = particle->color;
_vertices[vertexindex + 5].uv.x = val.x;
_vertices[vertexindex + 5].uv.y = val.y;
val = texRot * Vec3(0.5f, 1.0f, 0.0);
2019-11-23 20:27:39 +08:00
_vertices[vertexindex + 6].position = particle->position + Vec3(-halfWidth, halfHeight, -halfDepth);
2021-12-25 10:04:45 +08:00
_vertices[vertexindex + 6].color = particle->color;
_vertices[vertexindex + 6].uv.x = val.x;
_vertices[vertexindex + 6].uv.y = val.y;
val = texRot * Vec3(0.5f, 0.0f, 0.0);
2019-11-23 20:27:39 +08:00
_vertices[vertexindex + 7].position = particle->position + Vec3(halfWidth, halfHeight, -halfDepth);
2021-12-25 10:04:45 +08:00
_vertices[vertexindex + 7].color = particle->color;
_vertices[vertexindex + 7].uv.x = val.x;
_vertices[vertexindex + 7].uv.y = val.y;
2019-11-23 20:27:39 +08:00
vertexindex += 8;
index += 36;
}
2021-12-25 10:04:45 +08:00
if (!_vertices.empty() && !_indices.empty())
{
2019-11-23 20:27:39 +08:00
_vertexBuffer->updateData(&_vertices[0], vertexindex * sizeof(_vertices[0]));
_indexBuffer->updateData(&_indices[0], sizeof(_indices[0]) * sizeof(uint16_t));
_stateBlock.setBlendFunc(_particleSystem->getBlendFunc());
auto uColor = Vec4(1, 1, 1, 1);
_programState->setUniform(_locColor, &uColor, sizeof(uColor));
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
if (_texture)
{
_programState->setTexture(_locTexture, 0, _texture->getBackendTexture());
}
2021-12-25 10:04:45 +08:00
auto& projectionMatrix = Director::getInstance()->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
2019-11-23 20:27:39 +08:00
_programState->setUniform(_locPMatrix, &projectionMatrix.m, sizeof(projectionMatrix.m));
_meshCommand.setIndexDrawInfo(0, _indices.size());
renderer->addCommand(&_meshCommand);
}
}
void PUParticle3DBoxRender::reBuildIndices(unsigned short count)
{
unsigned short vertexIndex = 0;
2021-12-25 10:04:45 +08:00
for (unsigned short i = 0; i < 36 * count;)
{
// front
2019-11-23 20:27:39 +08:00
_indices[i++] = vertexIndex + 0;
_indices[i++] = vertexIndex + 2;
_indices[i++] = vertexIndex + 3;
_indices[i++] = vertexIndex + 0;
_indices[i++] = vertexIndex + 1;
_indices[i++] = vertexIndex + 2;
2021-12-25 10:04:45 +08:00
// right
2019-11-23 20:27:39 +08:00
_indices[i++] = vertexIndex + 1;
_indices[i++] = vertexIndex + 7;
_indices[i++] = vertexIndex + 2;
_indices[i++] = vertexIndex + 1;
_indices[i++] = vertexIndex + 4;
_indices[i++] = vertexIndex + 7;
2021-12-25 10:04:45 +08:00
// back
2019-11-23 20:27:39 +08:00
_indices[i++] = vertexIndex + 4;
_indices[i++] = vertexIndex + 6;
_indices[i++] = vertexIndex + 7;
_indices[i++] = vertexIndex + 4;
_indices[i++] = vertexIndex + 5;
_indices[i++] = vertexIndex + 6;
2021-12-25 10:04:45 +08:00
// left
2019-11-23 20:27:39 +08:00
_indices[i++] = vertexIndex + 5;
_indices[i++] = vertexIndex + 3;
_indices[i++] = vertexIndex + 6;
_indices[i++] = vertexIndex + 5;
_indices[i++] = vertexIndex + 0;
_indices[i++] = vertexIndex + 3;
2021-12-25 10:04:45 +08:00
// top
2019-11-23 20:27:39 +08:00
_indices[i++] = vertexIndex + 3;
_indices[i++] = vertexIndex + 7;
_indices[i++] = vertexIndex + 6;
_indices[i++] = vertexIndex + 3;
_indices[i++] = vertexIndex + 2;
_indices[i++] = vertexIndex + 7;
2021-12-25 10:04:45 +08:00
// bottom
2019-11-23 20:27:39 +08:00
_indices[i++] = vertexIndex + 5;
_indices[i++] = vertexIndex + 1;
_indices[i++] = vertexIndex + 0;
_indices[i++] = vertexIndex + 5;
_indices[i++] = vertexIndex + 4;
_indices[i++] = vertexIndex + 1;
vertexIndex += 8;
}
}
PUParticle3DBoxRender* PUParticle3DBoxRender::clone()
{
auto render = PUParticle3DBoxRender::create(_texFile);
copyAttributesTo(render);
return render;
}
PUSphereRender* PUSphereRender::create(std::string_view texFile)
2019-11-23 20:27:39 +08:00
{
2021-12-08 00:11:53 +08:00
auto ret = new PUSphereRender();
if (ret->initRender(texFile))
2019-11-23 20:27:39 +08:00
{
ret->autorelease();
}
else
{
2022-07-16 10:43:05 +08:00
AX_SAFE_DELETE(ret);
2019-11-23 20:27:39 +08:00
}
return ret;
}
2021-12-25 10:04:45 +08:00
void PUSphereRender::render(Renderer* renderer, const Mat4& transform, ParticleSystem3D* particleSystem)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
// batch and generate draw
const ParticlePool& particlePool = particleSystem->getParticlePool();
2019-11-23 20:27:39 +08:00
if (!_isVisible || particlePool.empty())
return;
2021-12-25 10:04:45 +08:00
auto camera = Camera::getVisitingCamera();
2019-11-23 20:27:39 +08:00
auto cameraMat = camera->getNodeToWorldTransform();
Vec3 backward(cameraMat.m[8], cameraMat.m[9], cameraMat.m[10]);
unsigned int vertexCount = (_numberOfRings + 1) * (_numberOfSegments + 1);
2021-12-25 10:04:45 +08:00
unsigned int indexCount = 6 * _numberOfRings * (_numberOfSegments + 1);
if (_vertexBuffer == nullptr && _indexBuffer == nullptr)
{
2019-11-23 20:27:39 +08:00
size_t stride = sizeof(VertexInfo);
2021-12-25 10:04:45 +08:00
_vertexBuffer =
backend::Device::getInstance()->newBuffer(stride * vertexCount * particleSystem->getParticleQuota(),
backend::BufferType::VERTEX, backend::BufferUsage::DYNAMIC);
2019-11-23 20:27:39 +08:00
if (_vertexBuffer == nullptr)
{
2022-07-16 10:43:05 +08:00
AXLOG("PUSphereRender::render create vertex buffer failed");
2019-11-23 20:27:39 +08:00
return;
}
_vertices.resize(vertexCount * particleSystem->getParticleQuota());
2021-12-25 10:04:45 +08:00
_indexBuffer = backend::Device::getInstance()->newBuffer(
sizeof(uint16_t) * indexCount * particleSystem->getParticleQuota(), backend::BufferType::INDEX,
backend::BufferUsage::DYNAMIC);
2019-11-23 20:27:39 +08:00
if (_indexBuffer == nullptr)
{
2022-07-16 10:43:05 +08:00
AXLOG("PUSphereRender::render create index buffer failed");
2019-11-23 20:27:39 +08:00
return;
}
_indices.resize(indexCount * particleSystem->getParticleQuota());
buildBuffers(particleSystem->getParticleQuota());
}
unsigned int vertexindex = 0;
2021-12-25 10:04:45 +08:00
unsigned int index = 0;
2019-11-23 20:27:39 +08:00
Mat4 mat;
Mat4 rotMat;
Mat4 sclMat;
Mat4 texRot;
Vec3 val;
for (auto&& iter : particlePool.getActiveDataList())
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
auto particle = static_cast<PUParticle3D*>(iter);
float radius = particle->width * 0.5f;
2019-11-23 20:27:39 +08:00
Mat4::createRotation(particle->orientation, &rotMat);
Mat4::createScale(radius, radius, radius, &sclMat);
Mat4::createRotation(backward, particle->zRotation, &texRot);
2021-12-25 10:04:45 +08:00
mat = rotMat * sclMat;
2019-11-23 20:27:39 +08:00
mat.m[12] = particle->position.x;
mat.m[13] = particle->position.y;
mat.m[14] = particle->position.z;
2021-12-25 10:04:45 +08:00
for (unsigned int i = 0; i < vertexCount; ++i)
{
2019-11-23 20:27:39 +08:00
val = texRot * Vec3(_vertexTemplate[vertexindex + i].uv.x, _vertexTemplate[vertexindex + i].uv.y, 0.0f);
mat.transformPoint(_vertexTemplate[vertexindex + i].position, &_vertices[vertexindex + i].position);
_vertices[vertexindex + i].color = particle->color;
2021-12-25 10:04:45 +08:00
_vertices[vertexindex + i].uv.x = val.x;
_vertices[vertexindex + i].uv.y = val.y;
2019-11-23 20:27:39 +08:00
}
vertexindex += vertexCount;
index += indexCount;
}
2021-12-25 10:04:45 +08:00
if (!_vertices.empty() && !_indices.empty())
{
2019-11-23 20:27:39 +08:00
_meshCommand.init(0.0f);
_vertexBuffer->updateData(&_vertices[0], vertexindex * sizeof(_vertices[0]));
_indexBuffer->updateData(&_indices[0], index * sizeof(_indices[0]));
_meshCommand.setVertexBuffer(_vertexBuffer);
_meshCommand.setIndexBuffer(_indexBuffer, MeshCommand::IndexFormat::U_SHORT);
_meshCommand.setIndexDrawInfo(0, index);
auto uColor = Vec4(1, 1, 1, 1);
_programState->setUniform(_locColor, &uColor, sizeof(uColor));
if (_texture)
{
_programState->setTexture(_locTexture, 0, _texture->getBackendTexture());
}
2021-12-25 10:04:45 +08:00
auto& projectionMatrix = Director::getInstance()->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
2019-11-23 20:27:39 +08:00
_programState->setUniform(_locPMatrix, &projectionMatrix.m, sizeof(projectionMatrix.m));
renderer->addCommand(&_meshCommand);
}
}
2021-12-25 10:04:45 +08:00
void PUSphereRender::buildBuffers(unsigned short count)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
float stepRingAngle = (M_PI / _numberOfRings);
2019-11-23 20:27:39 +08:00
float stepSegmentAngle = (2.0 * M_PI / _numberOfSegments);
unsigned short vertexIndex = 0;
2021-12-25 10:04:45 +08:00
unsigned short index = 0;
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
for (unsigned short i = 0; i < count; ++i)
{
for (unsigned int ring = 0; ring <= _numberOfRings; ring++)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
float r0 = sinf(ring * stepRingAngle);
float y0 = cosf(ring * stepRingAngle);
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
for (unsigned int segment = 0; segment <= _numberOfSegments; segment++)
2019-11-23 20:27:39 +08:00
{
VertexInfo vi;
float x0 = r0 * sinf(segment * stepSegmentAngle);
float z0 = r0 * cosf(segment * stepSegmentAngle);
// Vertex
vi.position.set(x0, y0, z0);
// Colour
vi.color = Vec4::ONE;
// Texture Coordinates
2021-12-25 10:04:45 +08:00
vi.uv.x = (float)segment / (float)_numberOfSegments;
vi.uv.y = 1.0f - (float)ring / (float)_numberOfRings;
2019-11-23 20:27:39 +08:00
if (ring != _numberOfRings)
{
// each vertex (except the last) has six indices pointing to it
_indices[index++] = vertexIndex + _numberOfSegments + 1;
_indices[index++] = vertexIndex;
_indices[index++] = vertexIndex + _numberOfSegments;
_indices[index++] = vertexIndex + _numberOfSegments + 1;
_indices[index++] = vertexIndex + 1;
_indices[index++] = vertexIndex;
}
++vertexIndex;
_vertexTemplate.push_back(vi);
}
}
}
}
2021-12-25 10:04:45 +08:00
PUSphereRender::PUSphereRender() : _numberOfRings(16), _numberOfSegments(16)
2019-11-23 20:27:39 +08:00
{
autoRotate = false;
}
2021-12-25 10:04:45 +08:00
PUSphereRender::~PUSphereRender() {}
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
void PUSphereRender::copyAttributesTo(PUSphereRender* sphereRender)
2019-11-23 20:27:39 +08:00
{
PURender::copyAttributesTo(sphereRender);
2021-12-25 10:04:45 +08:00
sphereRender->_numberOfRings = _numberOfRings;
2019-11-23 20:27:39 +08:00
sphereRender->_numberOfSegments = _numberOfSegments;
}
PUSphereRender* PUSphereRender::clone()
{
auto render = PUSphereRender::create(_texFile);
copyAttributesTo(render);
return render;
}
NS_AX_END