CCPUEmitter.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  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. #ifndef __CC_PU_PARTICLE_3D_EMITTER_H__
  23. #define __CC_PU_PARTICLE_3D_EMITTER_H__
  24. #include "base/CCRef.h"
  25. #include "math/CCMath.h"
  26. #include "extensions/Particle3D/CCParticle3DEmitter.h"
  27. #include "extensions/Particle3D/PU/CCPUDynamicAttribute.h"
  28. #include "extensions/Particle3D/PU/CCPUParticleSystem3D.h"
  29. #include <vector>
  30. #include <string>
  31. NS_CC_BEGIN
  32. struct PUParticle3D;
  33. class PUParticleSystem3D;
  34. /**
  35. * 3d particle emitter
  36. */
  37. class CC_DLL PUEmitter : public Particle3DEmitter
  38. {
  39. friend class PUParticleSystem3D;
  40. public:
  41. // Default values
  42. static const bool DEFAULT_ENABLED;
  43. static const Vec3 DEFAULT_POSITION;
  44. static const bool DEFAULT_KEEP_LOCAL;
  45. static const Vec3 DEFAULT_DIRECTION;
  46. static const Quaternion DEFAULT_ORIENTATION;
  47. static const Quaternion DEFAULT_ORIENTATION_RANGE_START;
  48. static const Quaternion DEFAULT_ORIENTATION_RANGE_END;
  49. //static const Particle::ParticleType DEFAULT_EMITS;
  50. static const unsigned short DEFAULT_START_TEXTURE_COORDS;
  51. static const unsigned short DEFAULT_END_TEXTURE_COORDS;
  52. static const unsigned short DEFAULT_TEXTURE_COORDS;
  53. static const Vec4 DEFAULT_START_COLOUR_RANGE;
  54. static const Vec4 DEFAULT_END_COLOUR_RANGE;
  55. static const Vec4 DEFAULT_COLOUR;
  56. static const bool DEFAULT_AUTO_DIRECTION;
  57. static const bool DEFAULT_FORCE_EMISSION;
  58. static const float DEFAULT_EMISSION_RATE;
  59. static const float DEFAULT_TIME_TO_LIVE;
  60. static const float DEFAULT_MASS;
  61. static const float DEFAULT_VELOCITY;
  62. static const float DEFAULT_DURATION;
  63. static const float DEFAULT_REPEAT_DELAY;
  64. static const float DEFAULT_ANGLE;
  65. static const float DEFAULT_DIMENSIONS;
  66. static const float DEFAULT_WIDTH;
  67. static const float DEFAULT_HEIGHT;
  68. static const float DEFAULT_DEPTH;
  69. PUEmitter();
  70. virtual ~PUEmitter();
  71. virtual void notifyStart();
  72. virtual void notifyStop();
  73. virtual void notifyPause();
  74. virtual void notifyResume();
  75. virtual void notifyRescaled(const Vec3& scale);
  76. virtual void prepare();
  77. virtual void unPrepare();
  78. virtual void preUpdateEmitter(float deltaTime);
  79. virtual void updateEmitter(Particle3D *particle, float deltaTime) override;
  80. virtual void postUpdateEmitter(float deltaTime);
  81. virtual unsigned short calculateRequestedParticles(float timeElapsed);
  82. virtual void emit(int count) override;
  83. void setLocalPosition(const Vec3 &pos) { _position = pos; };
  84. const Vec3 getLocalPosition() const { return _position; };
  85. /** Calculate the derived position of the affector.
  86. @remarks
  87. Note, that in script, the position is set as localspace, while if the affector is
  88. emitted, its position is automatically transformed. This function always returns
  89. the derived position.
  90. */
  91. const Vec3& getDerivedPosition();
  92. /** Enables or disables the emitter.
  93. */
  94. void setEnabled (bool enabled);
  95. bool isEnabled(void) const;
  96. bool isEmitterDone() const;
  97. /** Todo
  98. */
  99. const std::string& getEmitterType() const { return _emitterType; }
  100. void setEmitterType(const std::string& emitterType) {_emitterType = emitterType;};
  101. /** Todo
  102. */
  103. const std::string& getName() const { return _name; }
  104. void setName(const std::string& name) {_name = name;};
  105. /** Todo
  106. */
  107. PUDynamicAttribute* getDynAngle() const { return _dynAngle; }
  108. void setDynAngle(PUDynamicAttribute* dynAngle);
  109. /** Todo
  110. */
  111. PUDynamicAttribute* getDynEmissionRate() const { return _dynEmissionRate; }
  112. void setDynEmissionRate(PUDynamicAttribute* dynEmissionRate);
  113. /** Todo
  114. */
  115. PUDynamicAttribute* getDynTotalTimeToLive() const { return _dynTotalTimeToLive; }
  116. void setDynTotalTimeToLive(PUDynamicAttribute* dynTotalTimeToLive);
  117. /** Todo
  118. */
  119. PUDynamicAttribute* getDynParticleMass() const { return _dynParticleMass; }
  120. void setDynParticleMass(PUDynamicAttribute* dynParticleMass);
  121. /** Todo
  122. */
  123. PUDynamicAttribute* getDynVelocity() const { return _dynVelocity; }
  124. void setDynVelocity(PUDynamicAttribute* dynVelocity);
  125. /** Todo
  126. */
  127. PUDynamicAttribute* getDynDuration() const { return _dynDuration; }
  128. void setDynDuration(PUDynamicAttribute* dynDuration);
  129. void setDynDurationSet(bool durationSet);
  130. /** Todo
  131. */
  132. PUDynamicAttribute* getDynRepeatDelay() const { return _dynRepeatDelay; }
  133. void setDynRepeatDelay(PUDynamicAttribute* dynRepeatDelay);
  134. void setDynRepeatDelaySet(bool repeatDelaySet);
  135. /** Todo
  136. */
  137. PUDynamicAttribute* getDynParticleAllDimensions() const { return _dynParticleAllDimensions; }
  138. void setDynParticleAllDimensions(PUDynamicAttribute* dynParticleAllDimensions);
  139. void setDynParticleAllDimensionsSet(bool particleAllDimensionsSet);
  140. /** Todo
  141. */
  142. PUDynamicAttribute* getDynParticleWidth() const { return _dynParticleWidth; }
  143. void setDynParticleWidth(PUDynamicAttribute* dynParticleWidth);
  144. void setDynParticleWidthSet(bool particleWidthSet);
  145. /** Todo
  146. */
  147. PUDynamicAttribute* getDynParticleHeight() const { return _dynParticleHeight; }
  148. void setDynParticleHeight(PUDynamicAttribute* dynParticleHeight);
  149. void setDynParticleHeightSet(bool particleHeightSet);
  150. /** Todo
  151. */
  152. PUDynamicAttribute* getDynParticleDepth() const { return _dynParticleDepth; }
  153. void setDynParticleDepth(PUDynamicAttribute* dynParticleDepth);
  154. void setDynParticleDepthSet(bool particleDepthSet);
  155. ///** Todo
  156. //*/
  157. //ParticleType getEmitsType() const { return mEmitsType; }
  158. //void setEmitsType(ParticleType emitsType) {mEmitsType = emitsType;};
  159. /** Todo
  160. */
  161. const std::string& getEmitsName() const { return _emitsName; }
  162. void setEmitsName(const std::string& emitsName);
  163. PUParticle3D::ParticleType getEmitsType() const { return _emitsType; }
  164. void setEmitsType(PUParticle3D::ParticleType type) {_emitsType = type;};
  165. Ref* getEmitsEntityPtr() const;
  166. bool isMarkedForEmission() const {return _isMarkedForEmission;};
  167. void setMarkedForEmission(bool isMarked) {_isMarkedForEmission = isMarked;};
  168. /** Returns the base direction of the particle that is going to be emitted.
  169. */
  170. const Vec3& getParticleDirection(void);
  171. /** Returns the originally set particle direction. This value is not affected by affectors, angle, etc.
  172. */
  173. const Vec3& getOriginalParticleDirection(void) const;
  174. /** Returns the base orientation of the particle that is going to be emitted.
  175. */
  176. const Quaternion& getParticleOrientation(void) const;
  177. /** Set the orientation of the particle.
  178. */
  179. void setParticleOrientation(const Quaternion& orientation);
  180. /** Returns the start orientation of the particle that is going to be emitted.
  181. @remarks
  182. The orientation is generated random between mParticleOrientationRangeStart and mParticleOrientationRangeEnd.
  183. */
  184. const Quaternion& getParticleOrientationRangeStart(void) const;
  185. /** Set start orientation of the particle that is going to be emitted.
  186. @remarks
  187. The orientation is generated random between mParticleOrientationRangeStart and mParticleOrientationRangeEnd.
  188. */
  189. void setParticleOrientationRangeStart(const Quaternion& orientationRangeStart);
  190. /** Returns the end orientation of the particle that is going to be emitted.
  191. @remarks
  192. The orientation is generated random between mParticleOrientationRangeStart and mParticleOrientationRangeEnd.
  193. */
  194. const Quaternion& getParticleOrientationRangeEnd(void) const;
  195. /** Set end orientation of the particle that is going to be emitted.
  196. @remarks
  197. The orientation is generated random between mParticleOrientationRangeStart and mParticleOrientationRangeEnd.
  198. */
  199. void setParticleOrientationRangeEnd(const Quaternion& orientationRangeEnd);
  200. /** Sets the direction of the particle that the emitter is emitting.
  201. @remarks
  202. Don't confuse this with the emitters own direction.
  203. @param direction The base direction of emitted particles.
  204. */
  205. void setParticleDirection(const Vec3& direction);
  206. /** Todo
  207. */
  208. bool isAutoDirection(void) const;
  209. /** Todo
  210. */
  211. void setAutoDirection(bool autoDirection);
  212. /** Todo
  213. */
  214. bool isForceEmission(void) const;
  215. /** Todo
  216. */
  217. void setForceEmission(bool forceEmission);
  218. /** Get the colour of a particle that will be emitted.
  219. */
  220. const Vec4& getParticleColor(void) const;
  221. /** Set the colour of an emitted particle.
  222. */
  223. void setParticleColor(const Vec4& particleColour);
  224. /** Get the colour range start of an emitted particle.
  225. */
  226. const Vec4& getParticleColorRangeStart(void) const;
  227. /** Set the colour range start of an emitted particle. This is the lower value used to generate a random colour.
  228. */
  229. void setParticleColorRangeStart(const Vec4& particleColourRangeStart);
  230. /** Get the colour range end of an emitted particle.
  231. */
  232. const Vec4& getParticleColorRangeEnd(void) const;
  233. /** Set the colour range end of an emitted particle. This is the upper value used to generate a random colour.
  234. */
  235. void setParticleColorRangeEnd(const Vec4& particleColourRangeEnd);
  236. /** Get the texture coords of an emitted particle.
  237. */
  238. const unsigned short& getParticleTextureCoords(void) const;
  239. /** Set the texture coords of an emitted particle.
  240. */
  241. void setParticleTextureCoords(const unsigned short& particleTextureCoords);
  242. /** Get the texture coords range start of an emitted particle.
  243. */
  244. const unsigned short& getParticleTextureCoordsRangeStart(void) const;
  245. /** Set the texture coords range start of an emitted particle. This is the lower value used to set a random texture coords.
  246. */
  247. void setParticleTextureCoordsRangeStart(const unsigned short& particleTextureCoordsRangeStart);
  248. /** Get the texture coords range end of an emitted particle.
  249. */
  250. const unsigned short& getParticleTextureCoordsRangeEnd(void) const;
  251. /** Set the texture coords range end of an emitted particle. This is the upper value used to set a random texture coords.
  252. */
  253. void setParticleTextureCoordsRangeEnd(const unsigned short& particleTextureCoordsRangeEnd);
  254. /** Todo
  255. */
  256. bool isKeepLocal(void) const;
  257. /** If this attribute is set to 'true', the particles are emitted relative to the emitter
  258. */
  259. void setKeepLocal(bool keepLocal);
  260. /** Transforms the particle position in a local position relative to the emitter
  261. */
  262. bool makeParticleLocal(PUParticle3D* particle);
  263. virtual PUEmitter* clone() = 0;
  264. virtual void copyAttributesTo (PUEmitter* emitter);
  265. protected:
  266. /** Todo
  267. */
  268. virtual void initParticlePosition(PUParticle3D* particle);
  269. /** Internal method for generating the particle direction.
  270. */
  271. virtual void initParticleDirection(PUParticle3D* particle);
  272. /** Internal method for generating the particle orientation.
  273. */
  274. virtual void initParticleOrientation(PUParticle3D* particle);
  275. virtual void initParticleVelocity(PUParticle3D* particle);
  276. virtual void initParticleMass(PUParticle3D* particle);
  277. virtual void initParticleColor(PUParticle3D* particle);
  278. virtual void initParticleTextureCoords(PUParticle3D* particle);
  279. virtual float initParticleTimeToLive();
  280. virtual void initParticleDimensions(PUParticle3D* particle);
  281. virtual void initParticleForEmission(PUParticle3D* particle);
  282. /** Initialise some attributes that are time-based.
  283. */
  284. inline void initTimeBased(void);
  285. /** Internal method for generating the angle.
  286. */
  287. void generateAngle(float &angle);
  288. protected:
  289. Vec3 _position;
  290. Vec3 _latestPosition;
  291. Vec3 _latestPositionDiff;
  292. Vec3 _derivedPosition;
  293. /** Although the scale is on a Particle System level, the emitter can also be scaled.
  294. */
  295. Vec3 _emitterScale;
  296. // Type of the emitter
  297. std::string _emitterType;
  298. // Name of the emitter (optional)
  299. std::string _name;
  300. /** Direction (and speed) of the emitted particle
  301. @remarks
  302. Don't confuse the particle direction with the direction of the emitter itself.
  303. The particleDirection is the direction of an emitted particle.
  304. */
  305. Vec3 _particleDirection;
  306. /** The original direction of the emitted particle
  307. @remarks
  308. Don't confuse this with the particles' originalDirection.
  309. */
  310. Vec3 _originalParticleDirection;
  311. /** Orientation of the particle as soon as it is emitted.
  312. @remarks
  313. This is only visible if a renderer is used that renders 3D particles.
  314. */
  315. Quaternion _particleOrientation;
  316. /** If set, the range generates a random orientation between start and end.
  317. */
  318. Quaternion _particleOrientationRangeStart;
  319. Quaternion _particleOrientationRangeEnd;
  320. bool _particleOrientationRangeSet;
  321. // Angle around direction which particles may be emitted
  322. PUDynamicAttribute* _dynAngle;
  323. // Rate of particle emission.
  324. PUDynamicAttribute* _dynEmissionRate;
  325. //// Identifies the type of particle this emitter emits (default is visual particle).
  326. //ParticleType mEmitsType;
  327. // Identifies the name of particle this emitter emits (default is visual particle, so there is no name)
  328. std::string _emitsName;
  329. /** Dynamic attribute used to generate the total time to live.
  330. */
  331. PUDynamicAttribute* _dynTotalTimeToLive;
  332. /** Dynamic attribute used to generate the mass of a particle.
  333. */
  334. PUDynamicAttribute* _dynParticleMass;
  335. /** Dynamic attribute used to generate the velocity of a particle.
  336. */
  337. PUDynamicAttribute* _dynVelocity;
  338. /** Dynamic attribute used to define the duration of the emitter.
  339. */
  340. PUDynamicAttribute* _dynDuration;
  341. /** Dynamic attribute used to define the repeat/delay of the emitter.
  342. */
  343. PUDynamicAttribute* _dynRepeatDelay;
  344. /** Dynamic attribute used to define the (own) dimensions of a particle.
  345. @remarks
  346. In the Particle Technique it is possible to set the default dimensions for all particles, but it
  347. is also possible to set the dimensions per particle. Note, that this only applies to visual particles,
  348. of course. This attribute is used to x, y and z dimensions with the value.
  349. */
  350. PUDynamicAttribute* _dynParticleAllDimensions;
  351. bool _dynParticleAllDimensionsSet;
  352. /** Dynamic attribute used to define the (own) width of a particle.
  353. @remarks
  354. In the Particle Technique it is possible to set the default width for all particles, but it
  355. is also possible to set the width per particle. Note, that this only applies to visual particles,
  356. of course.
  357. */
  358. PUDynamicAttribute* _dynParticleWidth;
  359. bool _dynParticleWidthSet;
  360. /** Dynamic attribute used to define the (own) height of a particle.
  361. @remarks
  362. In the Particle Technique it is possible to set the default height for all particles, but it
  363. is also possible to set the height per particle. Note, that this only applies to visual particles,
  364. of course.
  365. */
  366. PUDynamicAttribute* _dynParticleHeight;
  367. bool _dynParticleHeightSet;
  368. /** Dynamic attribute used to define the (own) depth of a particle.
  369. @remarks
  370. In the Particle Technique it is possible to set the default depth for all particles, but it
  371. is also possible to set the depth per particle. Note, that this only applies to visual particles,
  372. of course.
  373. */
  374. PUDynamicAttribute* _dynParticleDepth;
  375. bool _dynParticleDepthSet;
  376. /** Notional up vector, just used to speed up generation of variant directions.
  377. */
  378. Vec3 _upVector;
  379. /** Particles that are left to be emitted from the previous time.
  380. */
  381. float _remainder;
  382. ///** Helper factory
  383. //*/
  384. //DynamicAttributeFactory mDynamicAttributeFactory;
  385. /** Helper class
  386. */
  387. PUDynamicAttributeHelper _dynamicAttributeHelper;
  388. /** Duration remainder.
  389. */
  390. float _durationRemain;
  391. /** Indication to determine whether duration is set.
  392. */
  393. bool _dynDurationSet;
  394. /** Repeat/delay remainder.
  395. */
  396. float _repeatDelayRemain;
  397. /** Indication to determine whether repeat/delay is set.
  398. */
  399. bool _dynRepeatDelaySet;
  400. /** Auto direction means that the direction of a particle is not determined by the
  401. direction vector that has been set, but the direction vector is generated based on
  402. the shape of the emitter. In most cases this means that the particle direction is
  403. perpendicular on the shape of the emitter.
  404. @remarks
  405. This attribute has only a meaning for certain emitters.
  406. */
  407. bool _autoDirection;
  408. /** In normal cases, emission is regulated by time. This prevents that too much particles are
  409. emitted at once, which causes a drop in the framerate. In some cases you just want to emit
  410. all particles immediately without any delay. Setting the 'mForceEmission' to true enables this.
  411. In that case, the emission rate defines the number of particles that are emitted immediately.
  412. If the emission rate is 100, the emitter emits 100 particles at once and then disables.
  413. */
  414. bool _forceEmission;
  415. /** Original value, used to set it back.
  416. */
  417. bool _originalForceEmission;
  418. /** If mForceEmission has been set to true and if all particles are emitted, the mForceEmissionExecuted
  419. is also set to true, preventing any further 'forced emission'.
  420. */
  421. bool _forceEmissionExecuted;
  422. /** Original value, used to set it back.
  423. */
  424. bool _originalForceEmissionExecuted;
  425. /** Colour that is assigned to an emitted particle.
  426. */
  427. Vec4 _particleColor;
  428. /** Used to randomize the colour of an emitted particle.
  429. */
  430. Vec4 _particleColorRangeStart;
  431. /** Used to randomize the colour of an emitted particle.
  432. */
  433. Vec4 _particleColorRangeEnd;
  434. /** Used to determine whether the colour range has been set.
  435. */
  436. bool _particleColorRangeSet;
  437. /** Determines whether particle positions should be kept local in relation to the emitter.
  438. */
  439. bool _keepLocal;
  440. /** Texture coords that is assigned to an emitted particle (only works if the renderer uses it)
  441. */
  442. unsigned short _particleTextureCoords;
  443. /** Used to randomize the texture coords of an emitted particle.
  444. */
  445. unsigned short _particleTextureCoordsRangeStart;
  446. /** Used to randomize the texture coords of an emitted particle.
  447. */
  448. unsigned short _particleTextureCoordsRangeEnd;
  449. /** Used to determine whether the texture coords range has been set.
  450. */
  451. bool _particleTextureCoordsRangeSet;
  452. bool _originEnabled;
  453. bool _originEnabledSet;
  454. PUParticle3D::ParticleType _emitsType;
  455. Ref *_emitsEntity;
  456. bool _isMarkedForEmission;
  457. };
  458. NS_CC_END
  459. #endif