axmol/extensions/Particle3D/PU/CCPURender.cpp

1067 lines
38 KiB
C++
Raw Normal View History

2015-02-11 18:14:22 +08:00
/****************************************************************************
2015-03-02 16:05:26 +08:00
Copyright (C) 2013 Henry van Merode. All rights reserved.
Copyright (c) 2015-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
2021-12-25 10:04:45 +08:00
2015-02-11 18:14:22 +08:00
http://www.cocos2d-x.org
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-05 13:24:06 +08:00
#include "extensions/Particle3D/PU/CCPURender.h"
#include "extensions/Particle3D/PU/CCPUParticleSystem3D.h"
#include "extensions/Particle3D/PU/CCPUUtil.h"
2015-02-11 18:14:22 +08:00
#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"
2015-02-11 18:14:22 +08:00
#include "base/CCDirector.h"
#include "3d/CCSprite3D.h"
#include "3d/CCMesh.h"
#include "2d/CCCamera.h"
NS_CC_BEGIN
2021-12-25 10:04:45 +08:00
void PURender::updateRender(PUParticle3D* /*particle*/, float /*deltaTime*/, bool /*firstParticle*/) {}
2021-12-25 10:04:45 +08:00
void PURender::copyAttributesTo(PURender* render)
2015-03-02 13:07:32 +08:00
{
Particle3DRender::copyAttributesTo(render);
2015-03-02 13:07:32 +08:00
render->_renderType = _renderType;
}
2021-12-25 10:04:45 +08:00
// static bool compareParticle3D(PUParticle3D* left, PUParticle3D* right)
//{
2021-12-25 10:04:45 +08:00
// return left->depthInView > right->depthInView;
// }
2015-02-11 18:14:22 +08:00
PUParticle3DQuadRender* PUParticle3DQuadRender::create(const std::string& texFile)
{
2021-12-08 00:11:53 +08:00
auto ret = new PUParticle3DQuadRender();
if (ret->initRender(texFile))
2015-03-02 17:47:26 +08:00
{
2015-11-27 19:01:30 +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 PUParticle3DQuadRender::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(VertexInfo);
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-03 11:42:15 +08:00
if (_vertexBuffer == nullptr)
{
CCLOG("PUParticle3DQuadRender::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(6 * particleSystem->getParticleQuota() * sizeof(uint16_t),
backend::BufferType::INDEX, backend::BufferUsage::DYNAMIC);
2015-03-03 11:42:15 +08:00
if (_indexBuffer == nullptr)
{
CCLOG("PUParticle3DQuadRender::render create index buffer failed");
return;
}
2015-02-11 18:14:22 +08:00
}
2021-12-25 10:04:45 +08:00
const ParticlePool::PoolList& activeParticleList = particlePool.getActiveDataList();
2015-03-02 13:07:32 +08:00
if (_vertices.size() < activeParticleList.size() * 4)
2015-02-11 18:14:22 +08:00
{
2015-03-02 13:07:32 +08:00
_vertices.resize(activeParticleList.size() * 4);
_indices.resize(activeParticleList.size() * 6);
2015-02-11 18:14:22 +08:00
}
2021-12-25 10:04:45 +08:00
auto camera = Camera::getVisitingCamera();
2015-02-11 18:14:22 +08:00
auto cameraMat = camera->getNodeToWorldTransform();
2021-12-25 10:04:45 +08:00
// for (auto iter : activeParticleList){
// iter->depthInView = -(viewMat.m[2] * iter->positionInWorld.x + viewMat.m[6] * iter->positionInWorld.y +
// viewMat.m[10] * iter->positionInWorld.z + viewMat.m[14]);
// }
2015-02-11 18:14:22 +08:00
2021-12-25 10:04:45 +08:00
// std::sort(activeParticleList.begin(), activeParticleList.end(), compareParticle3D);
2015-02-11 18:14:22 +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
2015-02-11 18:14:22 +08:00
Mat4 pRotMat;
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;
int offsetX, offsetY;
2015-02-11 18:14:22 +08:00
getOriginOffset(offsetX, offsetY);
2021-12-25 10:04:45 +08:00
if (_type == PERPENDICULAR_COMMON)
{
2015-02-11 18:14:22 +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)
{
2015-02-11 18:14:22 +08:00
up = _commonDir;
up.normalize();
Vec3::cross(up, backward, &right);
right.normalize();
}
for (auto iter : activeParticleList)
{
2021-12-25 10:04:45 +08:00
auto particle = static_cast<PUParticle3D*>(iter);
2015-02-11 18:14:22 +08:00
determineUVCoords(particle);
2021-12-25 10:04:45 +08:00
if (_type == ORIENTED_SELF)
{
2015-04-10 15:58:16 +08:00
Vec3 direction = particle->direction;
2021-12-25 10:04:45 +08:00
// transform.transformVector(particle->direction, &direction);
2015-02-11 18:14:22 +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)
{
2015-04-10 15:58:16 +08:00
Vec3 direction = particle->direction;
2021-12-25 10:04:45 +08:00
// transform.transformVector(particle->direction, &direction);
2015-02-11 18:14:22 +08:00
direction.normalize();
2021-12-25 10:04:45 +08:00
// up = PUUtil::perpendicular(direction);
// up.normalize();
2015-02-11 18:14:22 +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)
{
up.set(particle->orientation.x, particle->orientation.y, particle->orientation.z);
2015-02-11 18:14:22 +08:00
up.normalize();
Vec3::cross(up, backward, &right);
right.normalize();
}
2021-12-25 10:04:45 +08:00
Vec3 halfwidth = particle->width * 0.5f * right;
2015-03-02 13:07:32 +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);
2015-03-02 13:07:32 +08:00
position = particle->position;
2015-02-11 18:14:22 +08:00
2021-12-25 10:04:45 +08:00
if (_rotateType == TEXTURE_COORDS)
{
2015-02-11 18:14:22 +08:00
float costheta = cosf(-particle->zRotation);
float sintheta = sinf(-particle->zRotation);
Vec2 texOffset = 0.5f * (particle->lb_uv + particle->rt_uv);
2015-02-11 18:14:22 +08:00
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);
2015-02-11 18:14:22 +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);
2021-12-25 10:04:45 +08:00
fillVertex(vertexindex + 1, (position + (halfwidth - halfheight + offset)), particle->color,
val + texOffset);
2015-02-11 18:14:22 +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);
2015-02-11 18:14:22 +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
{
2015-02-11 18:14:22 +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);
2015-02-11 18:14:22 +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);
2015-02-11 18:14:22 +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);
2015-02-11 18:14:22 +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);
2015-02-11 18:14:22 +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);
2015-02-11 18:14:22 +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
2015-02-11 18:14:22 +08:00
index += 6;
vertexindex += 4;
}
2021-12-25 10:04:45 +08:00
2015-03-02 13:07:32 +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())
{
_vertexBuffer->updateData(&_vertices[0], vertexindex * sizeof(_vertices[0]));
_indexBuffer->updateData(&_indices[0], index * sizeof(_indices[0]));
2015-03-02 13:07:32 +08:00
_stateBlock.setBlendFunc(particleSystem->getBlendFunc());
2021-12-25 10:04:45 +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);
_programState->setUniform(_locPMatrix, &projectionMatrix.m, sizeof(projectionMatrix.m));
renderer->addCommand(&_meshCommand);
2015-03-02 13:07:32 +08:00
}
2015-02-11 18:14:22 +08:00
}
void PUParticle3DEntityRender::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 PUParticle3DEntityRender::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);
}
2015-02-11 18:14:22 +08:00
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)
{
2015-03-02 13:07:32 +08:00
autoRotate = false;
2015-02-11 18:14:22 +08:00
}
2021-12-25 10:04:45 +08:00
PUParticle3DQuadRender::~PUParticle3DQuadRender() {}
2015-02-11 18:14:22 +08:00
2021-12-25 10:04:45 +08:00
void PUParticle3DQuadRender::getOriginOffset(int& offsetX, int& offsetY)
2015-02-11 18:14:22 +08:00
{
switch (_origin)
{
case TOP_LEFT:
2021-12-25 10:04:45 +08:00
{
offsetX = 1;
offsetY = -1;
}
break;
2015-02-11 18:14:22 +08:00
case TOP_CENTER:
2021-12-25 10:04:45 +08:00
{
offsetX = 0;
offsetY = -1;
}
break;
2015-02-11 18:14:22 +08:00
case TOP_RIGHT:
2021-12-25 10:04:45 +08:00
{
offsetX = -1;
offsetY = -1;
}
break;
2015-02-11 18:14:22 +08:00
case CENTER_LEFT:
2021-12-25 10:04:45 +08:00
{
offsetX = 1;
offsetY = 0;
}
break;
2015-02-11 18:14:22 +08:00
case CENTER:
2021-12-25 10:04:45 +08:00
{
offsetX = 0;
offsetY = 0;
}
break;
2015-02-11 18:14:22 +08:00
case CENTER_RIGHT:
2021-12-25 10:04:45 +08:00
{
offsetX = -1;
offsetY = 0;
}
break;
2015-02-11 18:14:22 +08:00
case BOTTOM_LEFT:
2021-12-25 10:04:45 +08:00
{
offsetX = 1;
offsetY = 1;
}
break;
2015-02-11 18:14:22 +08:00
case BOTTOM_CENTER:
2021-12-25 10:04:45 +08:00
{
offsetX = 0;
offsetY = 1;
}
break;
2015-02-11 18:14:22 +08:00
case BOTTOM_RIGHT:
2021-12-25 10:04:45 +08:00
{
offsetX = -1;
offsetY = 1;
}
break;
2015-02-11 18:14:22 +08:00
}
}
unsigned short PUParticle3DQuadRender::getTextureCoordsRows() const
{
return _textureCoordsRows;
}
2021-12-25 10:04:45 +08:00
void PUParticle3DQuadRender::setTextureCoordsRows(unsigned short textureCoordsRows)
2015-02-11 18:14:22 +08:00
{
2021-12-25 10:04:45 +08:00
_textureCoordsRows = textureCoordsRows;
2015-02-11 18:14:22 +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)
2015-02-11 18:14:22 +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)
2015-02-11 18:14:22 +08:00
{
2021-12-25 10:04:45 +08:00
if (_textureCoordsColumns == 1 && _textureCoordsRows == 1)
return;
2015-02-11 18:14:22 +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;
2015-02-11 18:14:22 +08:00
particle->lb_uv.set(_textureCoordsColStep * currentCol, _textureCoordsRowStep * currentRow);
2015-02-11 18:14:22 +08:00
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)
2015-02-11 18:14:22 +08:00
{
2015-03-02 13:07:32 +08:00
_vertices[index].position = pos;
2021-12-25 10:04:45 +08:00
_vertices[index].color = color;
_vertices[index].uv = uv;
2015-02-11 18:14:22 +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)
2015-02-11 18:14:22 +08:00
{
2021-12-25 10:04:45 +08:00
_indices[index] = v0;
2015-03-02 13:07:32 +08:00
_indices[index + 1] = v1;
_indices[index + 2] = v2;
2015-02-11 18:14:22 +08:00
}
2021-12-25 10:04:45 +08:00
void PUParticle3DQuadRender::setType(Type type)
2015-02-11 18:14:22 +08:00
{
_type = type;
2021-12-25 10:04:45 +08:00
if (_type == PERPENDICULAR_COMMON || _type == PERPENDICULAR_SELF)
{
_stateBlock.setCullFace(false);
2021-12-25 10:04:45 +08:00
}
else
{
_stateBlock.setCullFace(true);
2015-02-11 18:14:22 +08:00
}
}
2021-12-25 10:04:45 +08:00
void PUParticle3DQuadRender::copyAttributesTo(PUParticle3DQuadRender* quadRender)
2015-03-02 13:07:32 +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;
2015-03-02 13:07:32 +08:00
quadRender->_textureCoordsColumns = _textureCoordsColumns;
quadRender->_textureCoordsRowStep = _textureCoordsRowStep;
quadRender->_textureCoordsColStep = _textureCoordsColStep;
}
PUParticle3DQuadRender* PUParticle3DQuadRender::clone()
{
auto render = PUParticle3DQuadRender::create(_texFile);
copyAttributesTo(render);
return render;
}
2015-02-11 18:14:22 +08:00
2021-12-25 10:04:45 +08:00
PUParticle3DModelRender* PUParticle3DModelRender::create(const std::string& modelFile,
const std::string& texFile /*= ""*/)
2015-02-11 18:14:22 +08:00
{
2021-12-25 10:04:45 +08:00
auto ret = new PUParticle3DModelRender();
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 PUParticle3DModelRender::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 (_spriteList.empty())
{
for (unsigned int i = 0; i < particleSystem->getParticleQuota(); ++i)
{
Sprite3D* sprite = Sprite3D::create(_modelFile);
if (sprite == nullptr)
{
CCLOG("failed to load file %s", _modelFile.c_str());
continue;
}
2015-02-11 18:14:22 +08:00
sprite->setTexture(_texFile);
sprite->setBlendFunc(particleSystem->getBlendFunc());
sprite->setCullFaceEnabled(false);
2015-02-11 18:14:22 +08:00
sprite->retain();
_spriteList.push_back(sprite);
}
2021-12-25 10:04:45 +08:00
if (!_spriteList.empty())
{
const AABB& aabb = _spriteList[0]->getAABB();
2015-02-11 18:14:22 +08:00
Vec3 corners[8];
aabb.getCorners(corners);
_spriteSize = corners[3] - corners[6];
2021-12-25 10:04:45 +08:00
}
else
{
2015-03-04 13:19:42 +08:00
_isVisible = false;
return;
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
{
2021-12-25 10:04:45 +08:00
auto particle = static_cast<PUParticle3D*>(iter);
2015-04-03 12:43:14 +08:00
Mat4::createRotation(q * particle->orientation, &rotMat);
2021-12-25 10:04:45 +08:00
sclMat.m[0] = particle->width / _spriteSize.x;
sclMat.m[5] = particle->height / _spriteSize.y;
2015-03-02 13:07:32 +08:00
sclMat.m[10] = particle->depth / _spriteSize.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 (_spriteList[index]->getCameraMask() != particleSystem->getCameraMask())
_spriteList[index]->setCameraMask(particleSystem->getCameraMask());
2021-12-25 10:04:45 +08:00
_spriteList[index]->setColor(
Color3B(particle->color.x * 255, particle->color.y * 255, particle->color.z * 255));
2015-03-02 13:07:32 +08:00
_spriteList[index]->setOpacity(particle->color.w * 255);
_spriteList[index]->visit(renderer, mat, Node::FLAGS_DIRTY_MASK);
2015-03-02 13:07:32 +08:00
++index;
2015-02-11 18:14:22 +08:00
}
}
PUParticle3DModelRender::PUParticle3DModelRender()
{
2015-03-02 13:07:32 +08:00
autoRotate = true;
2015-02-11 18:14:22 +08:00
}
PUParticle3DModelRender::~PUParticle3DModelRender()
{
2021-12-25 10:04:45 +08:00
for (auto iter : _spriteList)
{
iter->release();
}
2015-03-02 13:07:32 +08:00
}
2021-12-25 10:04:45 +08:00
void PUParticle3DModelRender::copyAttributesTo(PUParticle3DModelRender* render)
2015-03-02 13:07:32 +08:00
{
2015-03-05 13:24:06 +08:00
PURender::copyAttributesTo(render);
2015-03-02 13:07:32 +08:00
}
PUParticle3DModelRender* PUParticle3DModelRender::clone()
{
auto mr = PUParticle3DModelRender::create(_modelFile, _texFile);
copyAttributesTo(mr);
return mr;
}
void PUParticle3DModelRender::reset()
{
2021-12-25 10:04:45 +08:00
for (auto iter : _spriteList)
{
iter->release();
}
_spriteList.clear();
}
2015-03-02 13:07:32 +08:00
PUParticle3DEntityRender::PUParticle3DEntityRender()
2021-12-25 10:04:45 +08:00
: _texture(nullptr), _programState(nullptr), _indexBuffer(nullptr), _vertexBuffer(nullptr)
2015-03-02 13:07:32 +08:00
{
_stateBlock.setCullFace(false);
_stateBlock.setCullFaceSide(backend::CullMode::BACK);
_stateBlock.setDepthTest(false);
_stateBlock.setDepthWrite(false);
_stateBlock.setBlend(true);
2015-03-02 13:07:32 +08:00
}
PUParticle3DEntityRender::~PUParticle3DEntityRender()
2021-12-25 10:04:45 +08:00
{
;
// CC_SAFE_RELEASE(_texture);
CC_SAFE_RELEASE(_programState);
2015-03-02 13:07:32 +08:00
CC_SAFE_RELEASE(_vertexBuffer);
CC_SAFE_RELEASE(_indexBuffer);
}
2021-12-25 10:04:45 +08:00
bool PUParticle3DEntityRender::initRender(const std::string& texFile)
2015-03-02 13:07:32 +08:00
{
CC_SAFE_RELEASE_NULL(_programState);
2015-03-02 13:07:32 +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-03-02 13:07:32 +08:00
}
}
2015-03-02 13:07:32 +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(VertexInfo, 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(VertexInfo, 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(VertexInfo, color), false);
2019-03-13 15:12:36 +08:00
}
layout->setLayout(sizeof(VertexInfo));
2021-12-25 10:04:45 +08:00
_locColor = _programState->getUniformLocation("u_color");
_locPMatrix = _programState->getUniformLocation("u_PMatrix");
_locTexture = _programState->getUniformLocation("u_texture");
2015-03-02 13:07:32 +08:00
_meshCommand.setTransparent(true);
_meshCommand.setSkipBatching(true);
2015-03-02 13:07:32 +08:00
_stateBlock.setDepthTest(true);
_stateBlock.setDepthWrite(false);
_stateBlock.setCullFaceSide(backend::CullMode::BACK);
_stateBlock.setCullFace(true);
2015-03-02 13:07:32 +08:00
_meshCommand.setBeforeCallback(CC_CALLBACK_0(PUParticle3DEntityRender::onBeforeDraw, this));
_meshCommand.setAfterCallback(CC_CALLBACK_0(PUParticle3DEntityRender::onAfterDraw, this));
2015-03-02 13:07:32 +08:00
return true;
2015-03-02 13:07:32 +08:00
}
2021-12-25 10:04:45 +08:00
void PUParticle3DEntityRender::copyAttributesTo(PUParticle3DEntityRender* render)
2015-11-27 19:01:30 +08:00
{
PURender::copyAttributesTo(render);
2015-11-27 19:01:30 +08:00
}
void PUParticle3DEntityRender::reset()
2015-03-02 13:07:32 +08:00
{
this->initRender(_texFile);
2015-03-02 13:07:32 +08:00
}
PUParticle3DBoxRender::PUParticle3DBoxRender()
{
autoRotate = false;
}
2021-12-25 10:04:45 +08:00
PUParticle3DBoxRender::~PUParticle3DBoxRender() {}
2015-03-02 13:07:32 +08:00
2021-12-25 10:04:45 +08:00
PUParticle3DBoxRender* PUParticle3DBoxRender::create(const std::string& texFile)
2015-03-02 13:07:32 +08:00
{
2021-12-08 00:11:53 +08:00
auto ret = new PUParticle3DBoxRender();
if (ret->initRender(texFile))
2015-03-02 17:47:26 +08:00
{
ret->autorelease();
}
else
{
CC_SAFE_DELETE(ret);
}
return ret;
2015-03-02 13:07:32 +08:00
}
2021-12-25 10:04:45 +08:00
void PUParticle3DBoxRender::render(Renderer* renderer, const Mat4& transform, ParticleSystem3D* particleSystem)
2015-03-02 13:07:32 +08:00
{
2021-12-25 10:04:45 +08:00
// batch and generate draw
const ParticlePool& particlePool = particleSystem->getParticlePool();
2015-03-02 13:07:32 +08:00
if (!_isVisible || particlePool.empty())
return;
2021-12-25 10:04:45 +08:00
auto camera = Camera::getVisitingCamera();
2015-03-02 13:07:32 +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-06-05 17:58:33 +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);
2015-03-03 11:42:15 +08:00
if (_vertexBuffer == nullptr)
{
CCLOG("PUParticle3DBoxRender::render create vertex buffer failed");
return;
}
2015-03-02 13:07:32 +08:00
_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);
2015-03-03 11:42:15 +08:00
if (_indexBuffer == nullptr)
{
CCLOG("PUParticle3DBoxRender::render create index buffer failed");
return;
}
2015-03-02 13:07:32 +08:00
_indices.resize(36 * particleSystem->getParticleQuota());
reBuildIndices(particleSystem->getParticleQuota());
}
unsigned int vertexindex = 0;
2021-12-25 10:04:45 +08:00
unsigned int index = 0;
2015-03-02 13:07:32 +08:00
Mat4 texRot;
Vec3 val;
for (auto iter : particlePool.getActiveDataList())
{
2021-12-25 10:04:45 +08:00
auto particle = static_cast<PUParticle3D*>(iter);
2015-03-02 13:07:32 +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;
2015-03-02 13:07:32 +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);
2015-03-02 13:07:32 +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);
2015-03-02 13:07:32 +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);
2015-03-02 13:07:32 +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);
2015-03-02 13:07:32 +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;
2015-03-02 13:07:32 +08:00
2021-12-25 10:04:45 +08:00
val = texRot * Vec3(0.0f, 0.0f, 0.0);
2015-03-02 13:07:32 +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);
2015-03-02 13:07:32 +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);
2015-03-02 13:07:32 +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);
2015-03-02 13:07:32 +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;
2015-03-02 13:07:32 +08:00
vertexindex += 8;
index += 36;
}
2021-12-25 10:04:45 +08:00
if (!_vertices.empty() && !_indices.empty())
{
_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
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);
_programState->setUniform(_locPMatrix, &projectionMatrix.m, sizeof(projectionMatrix.m));
_meshCommand.setIndexDrawInfo(0, _indices.size());
renderer->addCommand(&_meshCommand);
2015-03-02 13:07:32 +08:00
}
}
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
2015-03-02 13:07:32 +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
2015-03-02 13:07:32 +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
2015-03-02 13:07:32 +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
2015-03-02 13:07:32 +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
2015-03-02 13:07:32 +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
2015-03-02 13:07:32 +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;
}
2021-12-25 10:04:45 +08:00
PUSphereRender* PUSphereRender::create(const std::string& texFile)
2015-03-02 13:07:32 +08:00
{
2021-12-08 00:11:53 +08:00
auto ret = new PUSphereRender();
if (ret->initRender(texFile))
2015-03-02 17:47:26 +08:00
{
ret->autorelease();
}
else
{
CC_SAFE_DELETE(ret);
}
return ret;
2015-03-02 13:07:32 +08:00
}
2021-12-25 10:04:45 +08:00
void PUSphereRender::render(Renderer* renderer, const Mat4& transform, ParticleSystem3D* particleSystem)
2015-03-02 13:07:32 +08:00
{
2021-12-25 10:04:45 +08:00
// batch and generate draw
const ParticlePool& particlePool = particleSystem->getParticlePool();
2015-03-02 13:07:32 +08:00
if (!_isVisible || particlePool.empty())
return;
2021-12-25 10:04:45 +08:00
auto camera = Camera::getVisitingCamera();
2015-03-02 13:07:32 +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-06-05 17:58:33 +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);
2015-03-03 11:42:15 +08:00
if (_vertexBuffer == nullptr)
{
2015-03-05 13:24:06 +08:00
CCLOG("PUSphereRender::render create vertex buffer failed");
2015-03-03 11:42:15 +08:00
return;
}
2015-03-02 13:07:32 +08:00
_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);
2015-03-03 11:42:15 +08:00
if (_indexBuffer == nullptr)
{
2015-03-05 13:24:06 +08:00
CCLOG("PUSphereRender::render create index buffer failed");
2015-03-03 11:42:15 +08:00
return;
}
2015-03-02 13:07:32 +08:00
_indices.resize(indexCount * particleSystem->getParticleQuota());
buildBuffers(particleSystem->getParticleQuota());
}
unsigned int vertexindex = 0;
2021-12-25 10:04:45 +08:00
unsigned int index = 0;
2015-03-02 13:07:32 +08:00
Mat4 mat;
Mat4 rotMat;
Mat4 sclMat;
Mat4 texRot;
Vec3 val;
for (auto iter : particlePool.getActiveDataList())
{
2021-12-25 10:04:45 +08:00
auto particle = static_cast<PUParticle3D*>(iter);
float radius = particle->width * 0.5f;
2015-03-02 13:07:32 +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;
2015-03-02 13:07:32 +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)
{
2015-03-02 13:07:32 +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;
2015-03-02 13:07:32 +08:00
}
vertexindex += vertexCount;
index += indexCount;
}
2021-12-25 10:04:45 +08:00
if (!_vertices.empty() && !_indices.empty())
{
_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);
_programState->setUniform(_locPMatrix, &projectionMatrix.m, sizeof(projectionMatrix.m));
renderer->addCommand(&_meshCommand);
2015-03-02 13:07:32 +08:00
}
}
2021-12-25 10:04:45 +08:00
void PUSphereRender::buildBuffers(unsigned short count)
2015-03-02 13:07:32 +08:00
{
2021-12-25 10:04:45 +08:00
float stepRingAngle = (M_PI / _numberOfRings);
2015-03-02 13:07:32 +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;
2015-03-02 13:07:32 +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++)
2015-03-02 13:07:32 +08:00
{
2021-12-25 10:04:45 +08:00
float r0 = sinf(ring * stepRingAngle);
float y0 = cosf(ring * stepRingAngle);
2015-03-02 13:07:32 +08:00
2021-12-25 10:04:45 +08:00
for (unsigned int segment = 0; segment <= _numberOfSegments; segment++)
2015-03-02 13:07:32 +08:00
{
VertexInfo vi;
float x0 = r0 * sinf(segment * stepSegmentAngle);
float z0 = r0 * cosf(segment * stepSegmentAngle);
// Vertex
vi.position.set(x0, y0, z0);
2015-03-02 13:07:32 +08:00
// 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;
2015-03-02 13:07:32 +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)
2015-03-02 13:07:32 +08:00
{
autoRotate = false;
}
2021-12-25 10:04:45 +08:00
PUSphereRender::~PUSphereRender() {}
2015-03-02 13:07:32 +08:00
2021-12-25 10:04:45 +08:00
void PUSphereRender::copyAttributesTo(PUSphereRender* sphereRender)
2015-03-02 13:07:32 +08:00
{
PURender::copyAttributesTo(sphereRender);
2021-12-25 10:04:45 +08:00
sphereRender->_numberOfRings = _numberOfRings;
2015-03-02 13:07:32 +08:00
sphereRender->_numberOfSegments = _numberOfSegments;
}
2015-03-05 13:24:06 +08:00
PUSphereRender* PUSphereRender::clone()
2015-03-02 13:07:32 +08:00
{
2015-03-05 13:24:06 +08:00
auto render = PUSphereRender::create(_texFile);
2015-03-02 13:07:32 +08:00
copyAttributesTo(render);
return render;
2015-02-11 18:14:22 +08:00
}
NS_CC_END