CCPUMeshSurfaceEmitter.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. /****************************************************************************
  2. Copyright (C) 2013 Henry van Merode. All rights reserved.
  3. Copyright (c) 2015-2016 Chukong Technologies Inc.
  4. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  5. http://www.cocos2d-x.org
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22. #include "CCPUMeshSurfaceEmitter.h"
  23. #include "extensions/Particle3D/PU/CCPUParticleSystem3D.h"
  24. #include "extensions/Particle3D/PU/CCPUUtil.h"
  25. NS_CC_BEGIN
  26. // Constants
  27. const Vec3 PUMeshSurfaceEmitter::DEFAULT_SCALE(1, 1, 1);
  28. const MeshInfo::MeshSurfaceDistribution PUMeshSurfaceEmitter::DEFAULT_DISTRIBUTION = MeshInfo::MSD_HOMOGENEOUS;
  29. //-----------------------------------------------------------------------
  30. inline void PUTriangle::calculateSquareSurface (void)
  31. {
  32. /* Calculating the surface of a triangle with the following algorithm:
  33. v1 = Vector3(x1, y1, z1)
  34. v2 = Vector3(x2, y2, z2)
  35. v3 = Vector3(x3, y3, z3)
  36. a = sqrt ((x1-x3)*(x1-x3) + (y1-y3)*(y1-y3) + (z1-z3)*(z1-z3))
  37. b = sqrt ((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) + (z2-z1)*(z2-z1))
  38. c = sqrt ((x3-x2)*(x3-x2) + (y3-y2)*(y3-y2) + (z3-z2)*(z3-z2))
  39. p = 0.5 * (a + b + c)
  40. surface = sqrt (p * (p-a) * (p-b) * (p-c));
  41. */
  42. // a, b and c are the length of each triangle
  43. float a = sqrt ( (v1.x - v3.x) * (v1.x - v3.x) +
  44. (v1.y - v3.y) * (v1.y - v3.y) +
  45. (v1.z - v3.z) * (v1.z - v3.z));
  46. float b = sqrt ( (v2.x - v1.x) * (v2.x - v1.x) +
  47. (v2.y - v1.y) * (v2.y - v1.y) +
  48. (v2.z - v1.z) * (v2.z - v1.z));
  49. float c = sqrt ( (v3.x - v2.x) * (v3.x - v2.x) +
  50. (v3.y - v2.y) * (v3.y - v2.y) +
  51. (v3.z - v2.z) * (v3.z - v2.z));
  52. float p = 0.5f * (a + b + c);
  53. // Assign square surface of the triangle
  54. squareSurface = p * (p-a) * (p-b) * (p-c);
  55. }
  56. //-----------------------------------------------------------------------
  57. inline void PUTriangle::calculateSurfaceNormal (void)
  58. {
  59. /* Calculating the surface normal of a triangle with the following algorithm:
  60. v1 = Vector3(x1, y1, z1)
  61. v2 = Vector3(x2, y2, z2)
  62. v3 = Vector3(x3, y3, z3)
  63. n = (v2-v1)x(v3-v1), where the 'x' is the cross product
  64. */
  65. Vec3::cross(v2-v1, v3-v1, &surfaceNormal);
  66. surfaceNormal.normalize();
  67. }
  68. //-----------------------------------------------------------------------
  69. inline void PUTriangle::calculateEdgeNormals (void)
  70. {
  71. Vec3::cross(v1, v2, &en1);
  72. en1.normalize();
  73. Vec3::cross(v2, v3, &en2);
  74. en2.normalize();
  75. Vec3::cross(v3, v1, &en3);
  76. en3.normalize();
  77. }
  78. //-----------------------------------------------------------------------
  79. const Vec3 PUTriangle::getRandomTrianglePosition (void)
  80. {
  81. // Use barycentric coordinates. Let A, B, C be the three vertices of the triangle. Any point P inside can
  82. // be expressed uniquely as P = aA + bB + cC, where a+b+c=1 and a,b,c are each >= 0.
  83. // Knowing a and b permits you to calculate c=1-a-b.
  84. // So if you can generate two random numbers a and b, each in [0,1], such that their sum <=1, you've got a
  85. // random point in your triangle. Generate random a and b independently and uniformly in [0,1]
  86. // 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
  87. // in triangle ABC: the reflection step a=1-a; b=1-b gives a point (a,b) uniformly distributed in the
  88. // triangle (0,0)(1,0)(0,1), which is then mapped affinely to ABC. Now you have barycentric coordinates
  89. // a,b,c. Compute your point P = aA + bB + cC.
  90. float a = CCRANDOM_0_1();
  91. float b = CCRANDOM_0_1();
  92. if (a + b > 1)
  93. {
  94. a = 1 - a;
  95. b = 1 - b;
  96. }
  97. float c = 1 - a - b;
  98. return a * v1 + b * v2 + c * v3;
  99. }
  100. //-----------------------------------------------------------------------
  101. const PUTriangle::PositionAndNormal PUTriangle::getRandomEdgePositionAndNormal (void)
  102. {
  103. float mult = CCRANDOM_0_1();
  104. float randomVal = CCRANDOM_0_1() * 3.0f;
  105. PositionAndNormal pAndN;
  106. pAndN.position.setZero();
  107. pAndN.normal.setZero();
  108. if (randomVal < 1)
  109. {
  110. pAndN.position.set(v2.x + mult*(v1.x - v2.x), v2.y + mult*(v1.y - v2.y), v2.z + mult*(v1.z - v2.z));
  111. pAndN.normal = en1;
  112. }
  113. else
  114. {
  115. if (randomVal < 2)
  116. {
  117. pAndN.position.set(v3.x + mult*(v2.x - v3.x), v3.y + mult*(v2.y - v3.y), v3.z + mult*(v2.z - v3.z));
  118. pAndN.normal = en2;
  119. }
  120. else
  121. {
  122. pAndN.position.set(v1.x + mult*(v3.x - v1.x), v1.y + mult*(v3.y - v1.y), v1.z + mult*(v3.z - v1.z));
  123. pAndN.normal = en3;
  124. }
  125. }
  126. return pAndN;
  127. }
  128. //-----------------------------------------------------------------------
  129. const PUTriangle::PositionAndNormal PUTriangle::getRandomVertexAndNormal (void)
  130. {
  131. float randomVal = CCRANDOM_0_1() * 3.0f;
  132. PositionAndNormal pAndN;
  133. pAndN.position.setZero();
  134. pAndN.normal.setZero();
  135. if (randomVal < 1)
  136. {
  137. pAndN.position = v1;
  138. pAndN.normal = vn1;
  139. }
  140. else
  141. {
  142. if (randomVal < 2)
  143. {
  144. pAndN.position = v2;
  145. pAndN.normal = vn2;
  146. }
  147. else
  148. {
  149. pAndN.position = v3;
  150. pAndN.normal = vn3;
  151. }
  152. }
  153. return pAndN;
  154. }
  155. //-----------------------------------------------------------------------
  156. //-----------------------------------------------------------------------
  157. //-----------------------------------------------------------------------
  158. MeshInfo::MeshInfo (const std::string& /*meshName*/,
  159. MeshSurfaceDistribution distribution,
  160. const Quaternion& /*orientation*/,
  161. const Vec3& /*scale*/) :
  162. mDistribution(distribution)
  163. {
  164. //Ogre::MeshPtr mesh = Ogre::MeshManager::getSingleton().load(meshName, Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
  165. //getMeshInformation(mesh, Vec3::ZERO, orientation, scale);
  166. }
  167. //-----------------------------------------------------------------------
  168. MeshInfo::~MeshInfo (void)
  169. {
  170. _triangles.clear();
  171. }
  172. //-----------------------------------------------------------------------
  173. inline float MeshInfo::getGaussianRandom (float high, float cutoff)
  174. {
  175. float x1 = 0;
  176. float x2 = 0;
  177. float w = 0;
  178. float y1 = 0;
  179. unsigned int max = 0;
  180. do
  181. {
  182. x1 = CCRANDOM_0_1();
  183. x2 = CCRANDOM_0_1();
  184. w = x1 * x1 + x2 * x2;
  185. // Prevent infinite loop
  186. if (w >= 1.0f && max > 4 )
  187. w = x1;
  188. } while (w >= 1.0f);
  189. w = sqrt((-2.0f * ::log(w)) / w);
  190. y1 = std::abs(x1 * w);
  191. y1 = y1 > cutoff ? cutoff : y1;
  192. y1 *= high / cutoff;
  193. return y1;
  194. }
  195. //-----------------------------------------------------------------------
  196. const PUTriangle& MeshInfo::getTriangle (size_t triangleIndex)
  197. {
  198. return _triangles[triangleIndex];
  199. }
  200. //-----------------------------------------------------------------------
  201. size_t MeshInfo::getRandomTriangleIndex()
  202. {
  203. size_t index;
  204. if (mDistribution == MSD_HOMOGENEOUS || mDistribution == MSD_HETEROGENEOUS_1)
  205. {
  206. index = (size_t)getGaussianRandom((float)_triangles.size() - 1);
  207. }
  208. else
  209. index = (size_t)(CCRANDOM_0_1() * (float)(_triangles.size() - 1));
  210. return index;
  211. }
  212. //-----------------------------------------------------------------------
  213. const PUTriangle::PositionAndNormal MeshInfo::getRandomPositionAndNormal (const size_t triangleIndex)
  214. {
  215. PUTriangle triangle = getTriangle(triangleIndex);
  216. PUTriangle::PositionAndNormal pAndN;
  217. pAndN.position.setZero();
  218. pAndN.normal.setZero();
  219. if (mDistribution == MSD_HOMOGENEOUS || mDistribution == MSD_HETEROGENEOUS_1 || mDistribution == MSD_HETEROGENEOUS_2)
  220. {
  221. pAndN.position = triangle.getRandomTrianglePosition();
  222. pAndN.normal = triangle.surfaceNormal;
  223. }
  224. else
  225. {
  226. if (mDistribution == MSD_VERTEX)
  227. {
  228. pAndN = triangle.getRandomVertexAndNormal();
  229. }
  230. else
  231. {
  232. if (mDistribution == MSD_EDGE)
  233. {
  234. pAndN = triangle.getRandomEdgePositionAndNormal();
  235. }
  236. }
  237. }
  238. return pAndN;
  239. }
  240. //-----------------------------------------------------------------------
  241. //void MeshInfo::getMeshInformation( Ogre::MeshPtr mesh,
  242. // const Vec3 &position,
  243. // const Quaternion &orient,
  244. // const Vec3 &scale)
  245. //{
  246. // size_t vertexCount = 0;
  247. // size_t indexCount = 0;
  248. // Vec3* vertices = 0;
  249. // Vec3* normals;
  250. // unsigned long* indices = 0;
  251. //
  252. // bool added_shared = false;
  253. // size_t current_offset = 0;
  254. // size_t shared_offset = 0;
  255. // size_t next_offset = 0;
  256. // size_t index_offset = 0;
  257. //
  258. // // Calculate how many vertices and indices we're going to need
  259. // for ( unsigned short i = 0; i < mesh->getNumSubMeshes(); ++i)
  260. // {
  261. // Ogre::SubMesh* submesh = mesh->getSubMesh( i );
  262. //
  263. // // We only need to add the shared vertices once
  264. // if(submesh->useSharedVertices)
  265. // {
  266. // if( !added_shared )
  267. // {
  268. // vertexCount += mesh->sharedVertexData->vertexCount;
  269. // added_shared = true;
  270. // }
  271. // }
  272. // else
  273. // {
  274. // vertexCount += submesh->vertexData->vertexCount;
  275. // }
  276. //
  277. // // Add the indices
  278. // indexCount += submesh->indexData->indexCount;
  279. // }
  280. //
  281. // // Allocate space for the vertices and indices
  282. // vertices = new (std::nothrow) Vec3[vertexCount];
  283. // normals = new (std::nothrow) Vec3[vertexCount];
  284. // indices = new (std::nothrow) unsigned long[indexCount];
  285. // added_shared = false;
  286. //
  287. // // Run through the submeshes again, adding the data into the arrays
  288. // for ( unsigned short i = 0; i < mesh->getNumSubMeshes(); ++i)
  289. // {
  290. // Ogre::SubMesh* submesh = mesh->getSubMesh(i);
  291. // Ogre::VertexData* vertex_data = submesh->useSharedVertices ? mesh->sharedVertexData : submesh->vertexData;
  292. //
  293. // if((!submesh->useSharedVertices)||(submesh->useSharedVertices && !added_shared))
  294. // {
  295. // if(submesh->useSharedVertices)
  296. // {
  297. // added_shared = true;
  298. // shared_offset = current_offset;
  299. // }
  300. //
  301. // const Ogre::VertexElement* posElem = vertex_data->vertexDeclaration->findElementBySemantic(Ogre::VES_POSITION);
  302. // const Ogre::VertexElement* normalElem = vertex_data->vertexDeclaration->findElementBySemantic(Ogre::VES_NORMAL);
  303. // Ogre::HardwareVertexBufferSharedPtr vbuf = vertex_data->vertexBufferBinding->getBuffer(posElem->getSource());
  304. // unsigned char* vertex = static_cast<unsigned char*>(vbuf->lock(Ogre::HardwareBuffer::HBL_READ_ONLY));
  305. // float* pReal;
  306. //
  307. // for( size_t j = 0; j < vertex_data->vertexCount; ++j, vertex += vbuf->getVertexSize())
  308. // {
  309. // posElem->baseVertexPointerToElement(vertex, &pReal);
  310. // Vec3 pt(pReal[0], pReal[1], pReal[2]);
  311. // vertices[current_offset + j] = (orient * (pt * scale)) + position;
  312. //
  313. // normalElem->baseVertexPointerToElement(vertex, &pReal);
  314. // Vec3 nt(pReal[0], pReal[1], pReal[2]);
  315. // normals[current_offset + j] = nt;
  316. // }
  317. //
  318. // vbuf->unlock();
  319. // next_offset += vertex_data->vertexCount;
  320. // }
  321. //
  322. // Ogre::IndexData* index_data = submesh->indexData;
  323. // size_t numTris = index_data->indexCount / 3;
  324. // Ogre::HardwareIndexBufferSharedPtr ibuf = index_data->indexBuffer;
  325. // bool use32bitindexes = (ibuf->getType() == Ogre::HardwareIndexBuffer::IT_32BIT);
  326. // unsigned long* pLong = static_cast<unsigned long*>(ibuf->lock(Ogre::HardwareBuffer::HBL_READ_ONLY));
  327. // unsigned short* pShort = reinterpret_cast<unsigned short*>(pLong);
  328. // size_t offset = (submesh->useSharedVertices)? shared_offset : current_offset;
  329. //
  330. // size_t numTrisMultThree = numTris*3;
  331. // if ( use32bitindexes )
  332. // {
  333. // for ( size_t k = 0; k < numTrisMultThree; ++k)
  334. // {
  335. // indices[index_offset++] = pLong[k] + static_cast<unsigned long>(offset);
  336. // }
  337. // }
  338. // else
  339. // {
  340. // for ( size_t k = 0; k < numTrisMultThree; ++k)
  341. // {
  342. // indices[index_offset++] = static_cast<unsigned long>(pShort[k]) + static_cast<unsigned long>(offset);
  343. // }
  344. // }
  345. //
  346. // ibuf->unlock();
  347. // current_offset = next_offset;
  348. // }
  349. //
  350. // // Create triangles from the retrieved data
  351. // for (size_t k = 0; k < indexCount-1; k+=3)
  352. // {
  353. // Triangle t;
  354. // t.v1 = vertices [indices[k]];
  355. // t.vn1 = normals [indices[k]];
  356. //
  357. // t.v2 = vertices [indices[k+1]];
  358. // t.vn2 = normals [indices[k+1]];
  359. //
  360. // t.v3 = vertices [indices[k+2]];
  361. // t.vn3 = normals [indices[k+2]];
  362. //
  363. // t.calculateSquareSurface();
  364. // t.calculateSurfaceNormal();
  365. // t.calculateEdgeNormals();
  366. // _triangles.push_back(t);
  367. // }
  368. //
  369. // // Delete intermediate arrays
  370. // delete [] indices;
  371. // delete [] normals;
  372. // delete [] vertices;
  373. //
  374. // // Sort the triangle on their size, if needed (only if a gaussian random number generator
  375. // // function is used to perform a random lookup of a triangle)
  376. // if (mDistribution == MSD_HOMOGENEOUS)
  377. // sort(_triangles.begin(), _triangles.end(), SortDescending());
  378. // else
  379. // if (mDistribution == MSD_HETEROGENEOUS_1)
  380. // sort(_triangles.begin(), _triangles.end(), SortAscending());
  381. //}
  382. //-----------------------------------------------------------------------
  383. //-----------------------------------------------------------------------
  384. //-----------------------------------------------------------------------
  385. PUMeshSurfaceEmitter::PUMeshSurfaceEmitter(void) :
  386. PUEmitter(),
  387. _meshName(),
  388. _orientation(),
  389. _scale(DEFAULT_SCALE),
  390. _distribution(DEFAULT_DISTRIBUTION),
  391. _meshInfo(0),
  392. _triangleIndex(0),
  393. _directionSet(false)
  394. {
  395. }
  396. //-----------------------------------------------------------------------
  397. PUMeshSurfaceEmitter::~PUMeshSurfaceEmitter(void)
  398. {
  399. if (_meshInfo)
  400. {
  401. CC_SAFE_DELETE(_meshInfo);
  402. }
  403. }
  404. //-----------------------------------------------------------------------
  405. void PUMeshSurfaceEmitter::prepare()
  406. {
  407. PUEmitter::prepare();
  408. // Build the data
  409. if (!_meshName.empty())
  410. {
  411. build();
  412. }
  413. }
  414. //-----------------------------------------------------------------------
  415. void PUMeshSurfaceEmitter::unPrepare()
  416. {
  417. // Todo
  418. PUEmitter::unPrepare();
  419. }
  420. //-----------------------------------------------------------------------
  421. void PUMeshSurfaceEmitter::initParticlePosition(PUParticle3D* particle)
  422. {
  423. PUTriangle::PositionAndNormal pAndN;
  424. pAndN.position.setZero();
  425. pAndN.normal.setZero();
  426. _directionSet = false;
  427. if (_meshInfo && _meshInfo->getTriangleCount())
  428. {
  429. getDerivedPosition();
  430. _triangleIndex = _meshInfo->getRandomTriangleIndex (); // Get a random triangle index
  431. pAndN = _meshInfo->getRandomPositionAndNormal(_triangleIndex); // Determine position and normal
  432. //ParticleSystem* sys = mParentTechnique->getParentSystem();
  433. if (_autoDirection)
  434. {
  435. if (pAndN.normal != Vec3::ZERO)
  436. {
  437. // Set position and direction of the particle
  438. //if (sys)
  439. {
  440. Mat4 rotMat;
  441. Mat4::createRotation(static_cast<PUParticleSystem3D *>(_particleSystem)->getDerivedOrientation(), &rotMat);
  442. particle->position = _derivedPosition + rotMat * Vec3(_emitterScale.x * pAndN.position.x, _emitterScale.y * pAndN.position.y, _emitterScale.z * pAndN.position.z);
  443. }
  444. //else
  445. //{
  446. // particle->position = _derivedPosition + _emitterScale * pAndN.position;
  447. //}
  448. // The value of the direction vector that has been set does not have a meaning
  449. float angle = (_dynamicAttributeHelper.calculate(_dynAngle, (static_cast<PUParticleSystem3D *>(_particleSystem))->getTimeElapsedSinceStart()));
  450. if (angle != 0.0f)
  451. {
  452. //particle->direction = (pAndN.normal).randomDeviant(angle, mUpVector);
  453. particle->direction = PUUtil::randomDeviant(pAndN.normal, angle, _upVector);
  454. }
  455. else
  456. {
  457. particle->direction = pAndN.normal;
  458. }
  459. particle->originalDirection = particle->direction;
  460. _directionSet = true;
  461. }
  462. }
  463. else
  464. {
  465. // Set position of the particle
  466. //if (sys)
  467. {
  468. Mat4 rotMat;
  469. Mat4::createRotation(static_cast<PUParticleSystem3D *>(_particleSystem)->getDerivedOrientation(), &rotMat);
  470. particle->position = _derivedPosition + rotMat * Vec3(_emitterScale.x * pAndN.position.x, _emitterScale.y * pAndN.position.y, _emitterScale.z * pAndN.position.z);
  471. }
  472. //else
  473. //{
  474. // particle->position = mDerivedPosition + _mEmitterScale * pAndN.position;
  475. //}
  476. }
  477. particle->originalPosition = particle->position;
  478. }
  479. }
  480. //-----------------------------------------------------------------------
  481. unsigned short PUMeshSurfaceEmitter::calculateRequestedParticles(float timeElapsed)
  482. {
  483. if (_meshInfo)
  484. {
  485. return PUEmitter::calculateRequestedParticles(timeElapsed);
  486. }
  487. return 0;
  488. }
  489. //-----------------------------------------------------------------------
  490. void PUMeshSurfaceEmitter::initParticleDirection(PUParticle3D* particle)
  491. {
  492. // Only determine direction if it hasn't been calculated yet
  493. if (!_directionSet)
  494. {
  495. PUEmitter::initParticleDirection(particle);
  496. }
  497. }
  498. //-----------------------------------------------------------------------
  499. const std::string& PUMeshSurfaceEmitter::getMeshName(void) const
  500. {
  501. return _meshName;
  502. }
  503. //-----------------------------------------------------------------------
  504. void PUMeshSurfaceEmitter::setMeshName(const std::string& meshName, bool doBuild)
  505. {
  506. _meshName = meshName;
  507. // If needed, build the data needed for emitting particles
  508. if (doBuild)
  509. {
  510. build();
  511. }
  512. }
  513. //-----------------------------------------------------------------------
  514. MeshInfo::MeshSurfaceDistribution PUMeshSurfaceEmitter::getDistribution() const
  515. {
  516. return _distribution;
  517. }
  518. //-----------------------------------------------------------------------
  519. void PUMeshSurfaceEmitter::setDistribution(MeshInfo::MeshSurfaceDistribution distribution)
  520. {
  521. _distribution = distribution;
  522. }
  523. //-----------------------------------------------------------------------
  524. const Vec3& PUMeshSurfaceEmitter::getScale (void) const
  525. {
  526. return _scale;
  527. }
  528. //-----------------------------------------------------------------------
  529. void PUMeshSurfaceEmitter::setScale (const Vec3& scale)
  530. {
  531. _scale = scale;
  532. }
  533. //-----------------------------------------------------------------------
  534. void PUMeshSurfaceEmitter::build(void)
  535. {
  536. // Delete the mesh info if already existing
  537. if (_meshInfo)
  538. {
  539. CC_SAFE_DELETE(_meshInfo);
  540. }
  541. // Generate meshinfo.
  542. _meshInfo = new (std::nothrow) MeshInfo(_meshName, _distribution, _orientation, _scale);
  543. }
  544. PUMeshSurfaceEmitter* PUMeshSurfaceEmitter::create()
  545. {
  546. auto pe = new (std::nothrow) PUMeshSurfaceEmitter();
  547. pe->autorelease();
  548. return pe;
  549. }
  550. void PUMeshSurfaceEmitter::copyAttributesTo( PUEmitter* emitter )
  551. {
  552. PUEmitter::copyAttributesTo(emitter);
  553. PUMeshSurfaceEmitter* meshSurfaceEmitter = static_cast<PUMeshSurfaceEmitter*>(emitter);
  554. meshSurfaceEmitter->_meshName = _meshName;
  555. meshSurfaceEmitter->_distribution = _distribution;
  556. meshSurfaceEmitter->_orientation = _orientation;
  557. meshSurfaceEmitter->_scale = _scale;
  558. }
  559. PUMeshSurfaceEmitter* PUMeshSurfaceEmitter::clone()
  560. {
  561. auto be = PUMeshSurfaceEmitter::create();
  562. copyAttributesTo(be);
  563. return be;
  564. }
  565. NS_CC_END