CCPUSphereSurfaceEmitter.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 "CCPUSphereSurfaceEmitter.h"
  23. #include "extensions/Particle3D/PU/CCPUParticleSystem3D.h"
  24. #include "extensions/Particle3D/PU/CCPUUtil.h"
  25. NS_CC_BEGIN
  26. // Constants
  27. const float PUSphereSurfaceEmitter::DEFAULT_RADIUS = 10.0f;
  28. //-----------------------------------------------------------------------
  29. PUSphereSurfaceEmitter::PUSphereSurfaceEmitter(void) :
  30. PUEmitter(),
  31. _radius(DEFAULT_RADIUS)
  32. {
  33. }
  34. //-----------------------------------------------------------------------
  35. float PUSphereSurfaceEmitter::getRadius() const
  36. {
  37. return _radius;
  38. }
  39. //-----------------------------------------------------------------------
  40. void PUSphereSurfaceEmitter::setRadius(const float radius)
  41. {
  42. _radius = radius;
  43. }
  44. //-----------------------------------------------------------------------
  45. void PUSphereSurfaceEmitter::initParticlePosition(PUParticle3D* particle)
  46. {
  47. // Generate a random unit vector to calculate a point on the sphere. This unit vector is
  48. // also used as direction vector if mAutoDirection has been set.
  49. _randomVector.set(CCRANDOM_MINUS1_1(), CCRANDOM_MINUS1_1(), CCRANDOM_MINUS1_1());
  50. _randomVector.normalize();
  51. //ParticleSystem* sys = mParentTechnique->getParentSystem();
  52. //if (sys)
  53. {
  54. Mat4 rotMat;
  55. Mat4::createRotation(static_cast<PUParticleSystem3D *>(_particleSystem)->getDerivedOrientation(), &rotMat);
  56. particle->position = getDerivedPosition() + rotMat * (Vec3(_emitterScale.x * _randomVector.x, _emitterScale.y * _randomVector.y, _emitterScale.z * _randomVector.z) * _radius);
  57. }
  58. //else
  59. //{
  60. // particle->position = getDerivedPosition() + (_mEmitterScale * _randomVector * _radius);
  61. //}
  62. particle->originalPosition = particle->position;
  63. }
  64. //-----------------------------------------------------------------------
  65. void PUSphereSurfaceEmitter::initParticleDirection(PUParticle3D* particle)
  66. {
  67. if (_autoDirection)
  68. {
  69. // The value of the direction vector that has been set does not have a meaning for
  70. // the sphere surface emitter.
  71. float angle = 0.0f;
  72. generateAngle(angle);
  73. if (angle != 0.0f)
  74. {
  75. //particle->direction = _randomVector.randomDeviant(angle, mUpVector);
  76. particle->direction = PUUtil::randomDeviant(_randomVector, angle, _upVector);
  77. particle->originalDirection = particle->direction;
  78. }
  79. else
  80. {
  81. particle->direction = _randomVector;
  82. particle->originalDirection = particle->direction;
  83. }
  84. }
  85. else
  86. {
  87. // Use the standard way
  88. PUEmitter::initParticleDirection(particle);
  89. }
  90. }
  91. PUSphereSurfaceEmitter* PUSphereSurfaceEmitter::create()
  92. {
  93. auto pe = new (std::nothrow) PUSphereSurfaceEmitter();
  94. pe->autorelease();
  95. return pe;
  96. }
  97. void PUSphereSurfaceEmitter::copyAttributesTo( PUEmitter* emitter )
  98. {
  99. PUEmitter::copyAttributesTo(emitter);
  100. PUSphereSurfaceEmitter* sphereSurfaceEmitter = static_cast<PUSphereSurfaceEmitter*>(emitter);
  101. sphereSurfaceEmitter->_radius = _radius;
  102. }
  103. PUSphereSurfaceEmitter* PUSphereSurfaceEmitter::clone()
  104. {
  105. auto be = PUSphereSurfaceEmitter::create();
  106. copyAttributesTo(be);
  107. return be;
  108. }
  109. NS_CC_END