axmol/extensions/Particle3D/PU/CCPUMeshSurfaceEmitter.cpp

604 lines
20 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
2022-08-08 18:02:17 +08:00
https://axys1.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 "CCPUMeshSurfaceEmitter.h"
#include "extensions/Particle3D/PU/CCPUParticleSystem3D.h"
#include "extensions/Particle3D/PU/CCPUUtil.h"
NS_AX_BEGIN
2019-11-23 20:27:39 +08:00
// Constants
const Vec3 PUMeshSurfaceEmitter::DEFAULT_SCALE(1, 1, 1);
const MeshInfo::MeshSurfaceDistribution PUMeshSurfaceEmitter::DEFAULT_DISTRIBUTION = MeshInfo::MSD_HOMOGENEOUS;
//-----------------------------------------------------------------------
2021-12-25 10:04:45 +08:00
inline void PUTriangle::calculateSquareSurface()
2019-11-23 20:27:39 +08:00
{
/* Calculating the surface of a triangle with the following algorithm:
v1 = Vector3(x1, y1, z1)
v2 = Vector3(x2, y2, z2)
v3 = Vector3(x3, y3, z3)
a = sqrt ((x1-x3)*(x1-x3) + (y1-y3)*(y1-y3) + (z1-z3)*(z1-z3))
b = sqrt ((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) + (z2-z1)*(z2-z1))
c = sqrt ((x3-x2)*(x3-x2) + (y3-y2)*(y3-y2) + (z3-z2)*(z3-z2))
p = 0.5 * (a + b + c)
surface = sqrt (p * (p-a) * (p-b) * (p-c));
*/
// a, b and c are the length of each triangle
2021-12-25 10:04:45 +08:00
float a = sqrt((v1.x - v3.x) * (v1.x - v3.x) + (v1.y - v3.y) * (v1.y - v3.y) + (v1.z - v3.z) * (v1.z - v3.z));
float b = sqrt((v2.x - v1.x) * (v2.x - v1.x) + (v2.y - v1.y) * (v2.y - v1.y) + (v2.z - v1.z) * (v2.z - v1.z));
float c = sqrt((v3.x - v2.x) * (v3.x - v2.x) + (v3.y - v2.y) * (v3.y - v2.y) + (v3.z - v2.z) * (v3.z - v2.z));
2019-11-23 20:27:39 +08:00
float p = 0.5f * (a + b + c);
// Assign square surface of the triangle
2021-12-25 10:04:45 +08:00
squareSurface = p * (p - a) * (p - b) * (p - c);
2019-11-23 20:27:39 +08:00
}
//-----------------------------------------------------------------------
2021-12-25 10:04:45 +08:00
inline void PUTriangle::calculateSurfaceNormal()
2019-11-23 20:27:39 +08:00
{
/* Calculating the surface normal of a triangle with the following algorithm:
v1 = Vector3(x1, y1, z1)
v2 = Vector3(x2, y2, z2)
v3 = Vector3(x3, y3, z3)
n = (v2-v1)x(v3-v1), where the 'x' is the cross product
*/
2021-12-25 10:04:45 +08:00
Vec3::cross(v2 - v1, v3 - v1, &surfaceNormal);
2019-11-23 20:27:39 +08:00
surfaceNormal.normalize();
}
//-----------------------------------------------------------------------
2021-12-25 10:04:45 +08:00
inline void PUTriangle::calculateEdgeNormals()
2019-11-23 20:27:39 +08:00
{
Vec3::cross(v1, v2, &en1);
en1.normalize();
Vec3::cross(v2, v3, &en2);
en2.normalize();
Vec3::cross(v3, v1, &en3);
en3.normalize();
}
//-----------------------------------------------------------------------
2021-12-25 10:04:45 +08:00
const Vec3 PUTriangle::getRandomTrianglePosition()
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
// Use barycentric coordinates. Let A, B, C be the three vertices of the triangle. Any point P inside can
2019-11-23 20:27:39 +08:00
// be expressed uniquely as P = aA + bB + cC, where a+b+c=1 and a,b,c are each >= 0.
// Knowing a and b permits you to calculate c=1-a-b.
2021-12-25 10:04:45 +08:00
// So if you can generate two random numbers a and b, each in [0,1], such that their sum <=1, you've got a
// random point in your triangle. Generate random a and b independently and uniformly in [0,1]
// If a+b>1, replace a by 1-a, b by 1-b. Let c=1-a-b. Then aA + bB + cC is uniformly distributed
// in triangle ABC: the reflection step a=1-a; b=1-b gives a point (a,b) uniformly distributed in the
// triangle (0,0)(1,0)(0,1), which is then mapped affinely to ABC. Now you have barycentric coordinates
2019-11-23 20:27:39 +08:00
// a,b,c. Compute your point P = aA + bB + cC.
2022-07-16 10:43:05 +08:00
float a = AXRANDOM_0_1();
float b = AXRANDOM_0_1();
2019-11-23 20:27:39 +08:00
if (a + b > 1)
{
a = 1 - a;
b = 1 - b;
}
float c = 1 - a - b;
return a * v1 + b * v2 + c * v3;
}
//-----------------------------------------------------------------------
2021-12-25 10:04:45 +08:00
const PUTriangle::PositionAndNormal PUTriangle::getRandomEdgePositionAndNormal()
2019-11-23 20:27:39 +08:00
{
2022-07-16 10:43:05 +08:00
float mult = AXRANDOM_0_1();
float randomVal = AXRANDOM_0_1() * 3.0f;
2019-11-23 20:27:39 +08:00
PositionAndNormal pAndN;
pAndN.position.setZero();
pAndN.normal.setZero();
if (randomVal < 1)
{
2021-12-25 10:04:45 +08:00
pAndN.position.set(v2.x + mult * (v1.x - v2.x), v2.y + mult * (v1.y - v2.y), v2.z + mult * (v1.z - v2.z));
2019-11-23 20:27:39 +08:00
pAndN.normal = en1;
}
else
{
if (randomVal < 2)
{
2021-12-25 10:04:45 +08:00
pAndN.position.set(v3.x + mult * (v2.x - v3.x), v3.y + mult * (v2.y - v3.y), v3.z + mult * (v2.z - v3.z));
2019-11-23 20:27:39 +08:00
pAndN.normal = en2;
}
else
{
2021-12-25 10:04:45 +08:00
pAndN.position.set(v1.x + mult * (v3.x - v1.x), v1.y + mult * (v3.y - v1.y), v1.z + mult * (v3.z - v1.z));
2019-11-23 20:27:39 +08:00
pAndN.normal = en3;
}
}
return pAndN;
}
//-----------------------------------------------------------------------
2021-12-25 10:04:45 +08:00
const PUTriangle::PositionAndNormal PUTriangle::getRandomVertexAndNormal()
2019-11-23 20:27:39 +08:00
{
2022-07-16 10:43:05 +08:00
float randomVal = AXRANDOM_0_1() * 3.0f;
2019-11-23 20:27:39 +08:00
PositionAndNormal pAndN;
pAndN.position.setZero();
pAndN.normal.setZero();
if (randomVal < 1)
{
pAndN.position = v1;
2021-12-25 10:04:45 +08:00
pAndN.normal = vn1;
2019-11-23 20:27:39 +08:00
}
else
{
if (randomVal < 2)
{
pAndN.position = v2;
2021-12-25 10:04:45 +08:00
pAndN.normal = vn2;
2019-11-23 20:27:39 +08:00
}
else
{
pAndN.position = v3;
2021-12-25 10:04:45 +08:00
pAndN.normal = vn3;
2019-11-23 20:27:39 +08:00
}
}
return pAndN;
}
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
MeshInfo::MeshInfo(std::string_view /*meshName*/,
2021-12-25 10:04:45 +08:00
MeshSurfaceDistribution distribution,
const Quaternion& /*orientation*/,
const Vec3& /*scale*/)
: mDistribution(distribution)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
// Ogre::MeshPtr mesh = Ogre::MeshManager::getSingleton().load(meshName,
// Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME); getMeshInformation(mesh, Vec3::ZERO, orientation,
// scale);
2019-11-23 20:27:39 +08:00
}
//-----------------------------------------------------------------------
2021-12-25 10:04:45 +08:00
MeshInfo::~MeshInfo()
2019-11-23 20:27:39 +08:00
{
_triangles.clear();
}
//-----------------------------------------------------------------------
2021-12-25 10:04:45 +08:00
inline float MeshInfo::getGaussianRandom(float high, float cutoff)
2019-11-23 20:27:39 +08:00
{
float x1 = 0;
float x2 = 0;
2021-12-25 10:04:45 +08:00
float w = 0;
2019-11-23 20:27:39 +08:00
float y1 = 0;
unsigned int max = 0;
do
{
2022-07-16 10:43:05 +08:00
x1 = AXRANDOM_0_1();
x2 = AXRANDOM_0_1();
2021-12-25 10:04:45 +08:00
w = x1 * x1 + x2 * x2;
2019-11-23 20:27:39 +08:00
// Prevent infinite loop
2021-12-25 10:04:45 +08:00
if (w >= 1.0f && max > 4)
2019-11-23 20:27:39 +08:00
w = x1;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
} while (w >= 1.0f);
2021-12-25 10:04:45 +08:00
w = sqrt((-2.0f * ::log(w)) / w);
2019-11-23 20:27:39 +08:00
y1 = std::abs(x1 * w);
y1 = y1 > cutoff ? cutoff : y1;
y1 *= high / cutoff;
return y1;
}
//-----------------------------------------------------------------------
2021-12-25 10:04:45 +08:00
const PUTriangle& MeshInfo::getTriangle(size_t triangleIndex)
2019-11-23 20:27:39 +08:00
{
return _triangles[triangleIndex];
}
//-----------------------------------------------------------------------
size_t MeshInfo::getRandomTriangleIndex()
{
size_t index;
if (mDistribution == MSD_HOMOGENEOUS || mDistribution == MSD_HETEROGENEOUS_1)
{
index = (size_t)getGaussianRandom((float)_triangles.size() - 1);
}
else
2022-07-16 10:43:05 +08:00
index = (size_t)(AXRANDOM_0_1() * (float)(_triangles.size() - 1));
2019-11-23 20:27:39 +08:00
return index;
}
//-----------------------------------------------------------------------
2021-12-25 10:04:45 +08:00
const PUTriangle::PositionAndNormal MeshInfo::getRandomPositionAndNormal(const size_t triangleIndex)
2019-11-23 20:27:39 +08:00
{
PUTriangle triangle = getTriangle(triangleIndex);
PUTriangle::PositionAndNormal pAndN;
pAndN.position.setZero();
pAndN.normal.setZero();
2021-12-25 10:04:45 +08:00
if (mDistribution == MSD_HOMOGENEOUS || mDistribution == MSD_HETEROGENEOUS_1 ||
mDistribution == MSD_HETEROGENEOUS_2)
2019-11-23 20:27:39 +08:00
{
pAndN.position = triangle.getRandomTrianglePosition();
2021-12-25 10:04:45 +08:00
pAndN.normal = triangle.surfaceNormal;
2019-11-23 20:27:39 +08:00
}
else
{
if (mDistribution == MSD_VERTEX)
{
pAndN = triangle.getRandomVertexAndNormal();
}
else
{
if (mDistribution == MSD_EDGE)
{
pAndN = triangle.getRandomEdgePositionAndNormal();
}
}
}
return pAndN;
}
//-----------------------------------------------------------------------
2021-12-25 10:04:45 +08:00
// void MeshInfo::getMeshInformation( Ogre::MeshPtr mesh,
2019-11-23 20:27:39 +08:00
// const Vec3 &position,
// const Quaternion &orient,
// const Vec3 &scale)
//{
// size_t vertexCount = 0;
// size_t indexCount = 0;
// Vec3* vertices = 0;
// Vec3* normals;
// unsigned long* indices = 0;
//
// bool added_shared = false;
// size_t current_offset = 0;
// size_t shared_offset = 0;
// size_t next_offset = 0;
// size_t index_offset = 0;
//
// // Calculate how many vertices and indices we're going to need
// for ( unsigned short i = 0; i < mesh->getNumSubMeshes(); ++i)
// {
// Ogre::SubMesh* submesh = mesh->getSubMesh( i );
//
// // We only need to add the shared vertices once
// if(submesh->useSharedVertices)
// {
// if( !added_shared )
// {
// vertexCount += mesh->sharedVertexData->vertexCount;
// added_shared = true;
// }
// }
// else
// {
// vertexCount += submesh->vertexData->vertexCount;
// }
//
// // Add the indices
// indexCount += submesh->indexData->indexCount;
// }
//
// // Allocate space for the vertices and indices
2021-12-08 00:11:53 +08:00
// vertices = new Vec3[vertexCount];
// normals = new Vec3[vertexCount];
// indices = new unsigned long[indexCount];
2019-11-23 20:27:39 +08:00
// added_shared = false;
//
// // Run through the submeshes again, adding the data into the arrays
// for ( unsigned short i = 0; i < mesh->getNumSubMeshes(); ++i)
// {
// Ogre::SubMesh* submesh = mesh->getSubMesh(i);
2021-12-25 10:04:45 +08:00
// Ogre::VertexData* vertex_data = submesh->useSharedVertices ? mesh->sharedVertexData :
// submesh->vertexData;
2019-11-23 20:27:39 +08:00
//
// if((!submesh->useSharedVertices)||(submesh->useSharedVertices && !added_shared))
// {
// if(submesh->useSharedVertices)
// {
// added_shared = true;
// shared_offset = current_offset;
// }
//
2021-12-25 10:04:45 +08:00
// const Ogre::VertexElement* posElem =
// vertex_data->vertexDeclaration->findElementBySemantic(Ogre::VES_POSITION); const
// Ogre::VertexElement* normalElem = vertex_data->vertexDeclaration->findElementBySemantic(Ogre::VES_NORMAL);
// Ogre::HardwareVertexBufferSharedPtr vbuf = vertex_data->vertexBufferBinding->getBuffer(posElem->getSource());
// unsigned char* vertex = static_cast<unsigned char*>(vbuf->lock(Ogre::HardwareBuffer::HBL_READ_ONLY));
// float* pReal;
2019-11-23 20:27:39 +08:00
//
// for( size_t j = 0; j < vertex_data->vertexCount; ++j, vertex += vbuf->getVertexSize())
// {
// posElem->baseVertexPointerToElement(vertex, &pReal);
// Vec3 pt(pReal[0], pReal[1], pReal[2]);
// vertices[current_offset + j] = (orient * (pt * scale)) + position;
//
// normalElem->baseVertexPointerToElement(vertex, &pReal);
// Vec3 nt(pReal[0], pReal[1], pReal[2]);
// normals[current_offset + j] = nt;
// }
2021-12-25 10:04:45 +08:00
//
2019-11-23 20:27:39 +08:00
// vbuf->unlock();
// next_offset += vertex_data->vertexCount;
// }
//
// Ogre::IndexData* index_data = submesh->indexData;
// size_t numTris = index_data->indexCount / 3;
// Ogre::HardwareIndexBufferSharedPtr ibuf = index_data->indexBuffer;
// bool use32bitindexes = (ibuf->getType() == Ogre::HardwareIndexBuffer::IT_32BIT);
// unsigned long* pLong = static_cast<unsigned long*>(ibuf->lock(Ogre::HardwareBuffer::HBL_READ_ONLY));
// unsigned short* pShort = reinterpret_cast<unsigned short*>(pLong);
// size_t offset = (submesh->useSharedVertices)? shared_offset : current_offset;
//
// size_t numTrisMultThree = numTris*3;
// if ( use32bitindexes )
// {
// for ( size_t k = 0; k < numTrisMultThree; ++k)
// {
// indices[index_offset++] = pLong[k] + static_cast<unsigned long>(offset);
// }
// }
// else
// {
// for ( size_t k = 0; k < numTrisMultThree; ++k)
// {
2021-12-25 10:04:45 +08:00
// indices[index_offset++] = static_cast<unsigned long>(pShort[k]) + static_cast<unsigned
// long>(offset);
2019-11-23 20:27:39 +08:00
// }
// }
//
// ibuf->unlock();
// current_offset = next_offset;
// }
//
// // Create triangles from the retrieved data
// for (size_t k = 0; k < indexCount-1; k+=3)
// {
// Triangle t;
// t.v1 = vertices [indices[k]];
// t.vn1 = normals [indices[k]];
//
// t.v2 = vertices [indices[k+1]];
// t.vn2 = normals [indices[k+1]];
//
// t.v3 = vertices [indices[k+2]];
// t.vn3 = normals [indices[k+2]];
//
// t.calculateSquareSurface();
// t.calculateSurfaceNormal();
// t.calculateEdgeNormals();
// _triangles.emplace_back(t);
2019-11-23 20:27:39 +08:00
// }
//
// // Delete intermediate arrays
// delete [] indices;
// delete [] normals;
// delete [] vertices;
//
// // Sort the triangle on their size, if needed (only if a gaussian random number generator
// // function is used to perform a random lookup of a triangle)
// if (mDistribution == MSD_HOMOGENEOUS)
// sort(_triangles.begin(), _triangles.end(), SortDescending());
// else
// if (mDistribution == MSD_HETEROGENEOUS_1)
// sort(_triangles.begin(), _triangles.end(), SortAscending());
//}
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
2021-12-25 10:04:45 +08:00
PUMeshSurfaceEmitter::PUMeshSurfaceEmitter()
: PUEmitter()
, _meshName()
, _orientation()
, _scale(DEFAULT_SCALE)
, _distribution(DEFAULT_DISTRIBUTION)
, _meshInfo(0)
, _triangleIndex(0)
, _directionSet(false)
{}
2019-11-23 20:27:39 +08:00
//-----------------------------------------------------------------------
PUMeshSurfaceEmitter::~PUMeshSurfaceEmitter()
{
if (_meshInfo)
{
2022-07-16 10:43:05 +08:00
AX_SAFE_DELETE(_meshInfo);
2019-11-23 20:27:39 +08:00
}
}
//-----------------------------------------------------------------------
void PUMeshSurfaceEmitter::prepare()
{
PUEmitter::prepare();
// Build the data
if (!_meshName.empty())
{
build();
}
}
//-----------------------------------------------------------------------
void PUMeshSurfaceEmitter::unPrepare()
{
// Todo
PUEmitter::unPrepare();
}
//-----------------------------------------------------------------------
void PUMeshSurfaceEmitter::initParticlePosition(PUParticle3D* particle)
{
PUTriangle::PositionAndNormal pAndN;
pAndN.position.setZero();
pAndN.normal.setZero();
_directionSet = false;
if (_meshInfo && _meshInfo->getTriangleCount())
{
getDerivedPosition();
2021-12-25 10:04:45 +08:00
_triangleIndex = _meshInfo->getRandomTriangleIndex(); // Get a random triangle index
pAndN = _meshInfo->getRandomPositionAndNormal(_triangleIndex); // Determine position and normal
// ParticleSystem* sys = mParentTechnique->getParentSystem();
2019-11-23 20:27:39 +08:00
if (_autoDirection)
{
if (pAndN.normal != Vec3::ZERO)
{
// Set position and direction of the particle
2021-12-25 10:04:45 +08:00
// if (sys)
2019-11-23 20:27:39 +08:00
{
Mat4 rotMat;
2021-12-25 10:04:45 +08:00
Mat4::createRotation(static_cast<PUParticleSystem3D*>(_particleSystem)->getDerivedOrientation(),
&rotMat);
particle->position = _derivedPosition + rotMat * Vec3(_emitterScale.x * pAndN.position.x,
_emitterScale.y * pAndN.position.y,
_emitterScale.z * pAndN.position.z);
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
// else
2019-11-23 20:27:39 +08:00
//{
// particle->position = _derivedPosition + _emitterScale * pAndN.position;
2021-12-25 10:04:45 +08:00
// }
2019-11-23 20:27:39 +08:00
// The value of the direction vector that has been set does not have a meaning
2021-12-25 10:04:45 +08:00
float angle = (_dynamicAttributeHelper.calculate(
_dynAngle, (static_cast<PUParticleSystem3D*>(_particleSystem))->getTimeElapsedSinceStart()));
2019-11-23 20:27:39 +08:00
if (angle != 0.0f)
{
2021-12-25 10:04:45 +08:00
// particle->direction = (pAndN.normal).randomDeviant(angle, mUpVector);
2019-11-23 20:27:39 +08:00
particle->direction = PUUtil::randomDeviant(pAndN.normal, angle, _upVector);
}
else
{
particle->direction = pAndN.normal;
}
particle->originalDirection = particle->direction;
2021-12-25 10:04:45 +08:00
_directionSet = true;
2019-11-23 20:27:39 +08:00
}
}
else
{
// Set position of the particle
2021-12-25 10:04:45 +08:00
// if (sys)
2019-11-23 20:27:39 +08:00
{
Mat4 rotMat;
2021-12-25 10:04:45 +08:00
Mat4::createRotation(static_cast<PUParticleSystem3D*>(_particleSystem)->getDerivedOrientation(),
&rotMat);
particle->position = _derivedPosition + rotMat * Vec3(_emitterScale.x * pAndN.position.x,
_emitterScale.y * pAndN.position.y,
_emitterScale.z * pAndN.position.z);
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
// else
2019-11-23 20:27:39 +08:00
//{
// particle->position = mDerivedPosition + _mEmitterScale * pAndN.position;
2021-12-25 10:04:45 +08:00
// }
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
particle->originalPosition = particle->position;
}
}
//-----------------------------------------------------------------------
unsigned short PUMeshSurfaceEmitter::calculateRequestedParticles(float timeElapsed)
{
if (_meshInfo)
{
return PUEmitter::calculateRequestedParticles(timeElapsed);
}
return 0;
}
//-----------------------------------------------------------------------
void PUMeshSurfaceEmitter::initParticleDirection(PUParticle3D* particle)
{
// Only determine direction if it hasn't been calculated yet
if (!_directionSet)
{
PUEmitter::initParticleDirection(particle);
}
}
//-----------------------------------------------------------------------
std::string_view PUMeshSurfaceEmitter::getMeshName() const
2019-11-23 20:27:39 +08:00
{
return _meshName;
}
//-----------------------------------------------------------------------
void PUMeshSurfaceEmitter::setMeshName(std::string_view meshName, bool doBuild)
2019-11-23 20:27:39 +08:00
{
_meshName = meshName;
// If needed, build the data needed for emitting particles
if (doBuild)
{
build();
}
}
//-----------------------------------------------------------------------
MeshInfo::MeshSurfaceDistribution PUMeshSurfaceEmitter::getDistribution() const
{
return _distribution;
}
//-----------------------------------------------------------------------
void PUMeshSurfaceEmitter::setDistribution(MeshInfo::MeshSurfaceDistribution distribution)
{
_distribution = distribution;
}
//-----------------------------------------------------------------------
2021-12-25 10:04:45 +08:00
const Vec3& PUMeshSurfaceEmitter::getScale() const
2019-11-23 20:27:39 +08:00
{
return _scale;
}
//-----------------------------------------------------------------------
2021-12-25 10:04:45 +08:00
void PUMeshSurfaceEmitter::setScale(const Vec3& scale)
2019-11-23 20:27:39 +08:00
{
_scale = scale;
}
//-----------------------------------------------------------------------
void PUMeshSurfaceEmitter::build()
{
// Delete the mesh info if already existing
if (_meshInfo)
{
2022-07-16 10:43:05 +08:00
AX_SAFE_DELETE(_meshInfo);
2019-11-23 20:27:39 +08:00
}
// Generate meshinfo.
2021-12-08 00:11:53 +08:00
_meshInfo = new MeshInfo(_meshName, _distribution, _orientation, _scale);
2019-11-23 20:27:39 +08:00
}
PUMeshSurfaceEmitter* PUMeshSurfaceEmitter::create()
{
2021-12-08 00:11:53 +08:00
auto pe = new PUMeshSurfaceEmitter();
2019-11-23 20:27:39 +08:00
pe->autorelease();
return pe;
}
2021-12-25 10:04:45 +08:00
void PUMeshSurfaceEmitter::copyAttributesTo(PUEmitter* emitter)
2019-11-23 20:27:39 +08:00
{
PUEmitter::copyAttributesTo(emitter);
PUMeshSurfaceEmitter* meshSurfaceEmitter = static_cast<PUMeshSurfaceEmitter*>(emitter);
2021-12-25 10:04:45 +08:00
meshSurfaceEmitter->_meshName = _meshName;
meshSurfaceEmitter->_distribution = _distribution;
meshSurfaceEmitter->_orientation = _orientation;
meshSurfaceEmitter->_scale = _scale;
2019-11-23 20:27:39 +08:00
}
PUMeshSurfaceEmitter* PUMeshSurfaceEmitter::clone()
{
auto be = PUMeshSurfaceEmitter::create();
copyAttributesTo(be);
return be;
}
NS_AX_END