particleEmitter.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  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
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  23. // Arcane-FX for MIT Licensed Open Source version of Torque 3D from GarageGames
  24. // Copyright (C) 2015 Faust Logic, Inc.
  25. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  26. #ifndef _H_PARTICLE_EMITTER
  27. #define _H_PARTICLE_EMITTER
  28. #ifndef _GAMEBASE_H_
  29. #include "T3D/gameBase/gameBase.h"
  30. #endif
  31. #ifndef _COLOR_H_
  32. #include "core/color.h"
  33. #endif
  34. #ifndef _GFXPRIMITIVEBUFFER_H_
  35. #include "gfx/gfxPrimitiveBuffer.h"
  36. #endif
  37. #ifndef _GFXVERTEXBUFFER_H_
  38. #include "gfx/gfxVertexBuffer.h"
  39. #endif
  40. #ifndef _PARTICLE_H_
  41. #include "T3D/fx/particle.h"
  42. #endif
  43. class RenderPassManager;
  44. class ParticleData;
  45. #ifdef TORQUE_AFX_ENABLED
  46. #define AFX_CAP_PARTICLE_POOLS
  47. #if defined(AFX_CAP_PARTICLE_POOLS)
  48. class afxParticlePoolData;
  49. class afxParticlePool;
  50. #endif
  51. #endif
  52. //*****************************************************************************
  53. // Particle Emitter Data
  54. //*****************************************************************************
  55. class ParticleEmitterData : public GameBaseData
  56. {
  57. typedef GameBaseData Parent;
  58. static bool _setAlignDirection( void *object, const char *index, const char *data );
  59. public:
  60. ParticleEmitterData();
  61. DECLARE_CONOBJECT(ParticleEmitterData);
  62. static void initPersistFields();
  63. void packData(BitStream* stream) override;
  64. void unpackData(BitStream* stream) override;
  65. bool preload(bool server, String &errorStr) override;
  66. bool onAdd() override;
  67. void allocPrimBuffer( S32 overrideSize = -1 );
  68. public:
  69. S32 ejectionPeriodMS; ///< Time, in Milliseconds, between particle ejection
  70. S32 periodVarianceMS; ///< Varience in ejection peroid between 0 and n
  71. F32 ejectionVelocity; ///< Ejection velocity
  72. F32 velocityVariance; ///< Variance for velocity between 0 and n
  73. F32 ejectionOffset; ///< Z offset from emitter point to eject from
  74. F32 ejectionOffsetVariance; ///< Z offset Variance from emitter point to eject
  75. F32 thetaMin; ///< Minimum angle, from the horizontal plane, to eject from
  76. F32 thetaMax; ///< Maximum angle, from the horizontal plane, to eject from
  77. F32 thetaVariance; ///< Angle, from the previous particle, to eject from
  78. F32 phiReferenceVel; ///< Reference angle, from the verticle plane, to eject from
  79. F32 phiVariance; ///< Varience from the reference angle, from 0 to n
  80. F32 softnessDistance; ///< For soft particles, the distance (in meters) where particles will be faded
  81. ///< based on the difference in depth between the particle and the scene geometry.
  82. /// A scalar value used to influence the effect
  83. /// of the ambient color on the particle.
  84. F32 ambientFactor;
  85. S32 lifetimeMS; ///< Lifetime of particles
  86. S32 lifetimeVarianceMS; ///< Varience in lifetime from 0 to n
  87. bool overrideAdvance; ///<
  88. bool orientParticles; ///< Particles always face the screen
  89. bool orientOnVelocity; ///< Particles face the screen at the start
  90. bool ribbonParticles; ///< Particles are rendered as a continous ribbon
  91. bool useEmitterSizes; ///< Use emitter specified sizes instead of datablock sizes
  92. bool useEmitterColors; ///< Use emitter specified colors instead of datablock colors
  93. bool alignParticles; ///< Particles always face along a particular axis
  94. Point3F alignDirection; ///< The direction aligned particles should face
  95. StringTableEntry particleString; ///< Used to load particle data directly from a string
  96. Vector<ParticleData*> particleDataBlocks; ///< Particle Datablocks
  97. Vector<U32> dataBlockIds; ///< Datablock IDs (parellel array to particleDataBlocks)
  98. U32 partListInitSize; /// initial size of particle list calc'd from datablock info
  99. GFXPrimitiveBufferHandle primBuff;
  100. S32 blendStyle; ///< Pre-define blend factor setting
  101. bool sortParticles; ///< Particles are sorted back-to-front
  102. bool reverseOrder; ///< reverses draw order
  103. StringTableEntry textureName; ///< Emitter texture file to override particle textures
  104. GFXTexHandle textureHandle; ///< Emitter texture handle from txrName
  105. bool highResOnly; ///< This particle system should not use the mixed-resolution particle rendering
  106. bool renderReflection; ///< Enables this emitter to render into reflection passes.
  107. bool glow; ///< Renders this emitter into the glow buffer.
  108. bool reload();
  109. public:
  110. bool fade_color;
  111. bool fade_size;
  112. bool fade_alpha;
  113. bool ejectionInvert;
  114. U8 parts_per_eject;
  115. bool use_emitter_xfm;
  116. #if defined(AFX_CAP_PARTICLE_POOLS)
  117. public:
  118. afxParticlePoolData* pool_datablock;
  119. U32 pool_index;
  120. bool pool_depth_fade;
  121. bool pool_radial_fade;
  122. bool do_pool_id_convert;
  123. #endif
  124. public:
  125. /*C*/ ParticleEmitterData(const ParticleEmitterData&, bool = false);
  126. /*D*/ ~ParticleEmitterData();
  127. virtual ParticleEmitterData* cloneAndPerformSubstitutions(const SimObject*, S32 index=0);
  128. bool allowSubstitutions() const override { return true; }
  129. };
  130. //*****************************************************************************
  131. // Particle Emitter
  132. //*****************************************************************************
  133. class ParticleEmitter : public GameBase
  134. {
  135. typedef GameBase Parent;
  136. #if defined(AFX_CAP_PARTICLE_POOLS)
  137. friend class afxParticlePool;
  138. #endif
  139. public:
  140. typedef GFXVertexPCT ParticleVertexType;
  141. ParticleEmitter();
  142. ~ParticleEmitter();
  143. DECLARE_CONOBJECT(ParticleEmitter);
  144. DECLARE_CATEGORY("UNLISTED");
  145. static Point3F mWindVelocity;
  146. static void setWindVelocity( const Point3F &vel ){ mWindVelocity = vel; }
  147. LinearColorF getCollectiveColor();
  148. /// Sets sizes of particles based on sizelist provided
  149. /// @param sizeList List of sizes
  150. void setSizes( F32 *sizeList );
  151. /// Sets colors for particles based on color list provided
  152. /// @param colorList List of colors
  153. void setColors( LinearColorF *colorList );
  154. ParticleEmitterData *getDataBlock(){ return mDataBlock; }
  155. bool onNewDataBlock( GameBaseData *dptr, bool reload ) override;
  156. /// By default, a particle renderer will wait for it's owner to delete it. When this
  157. /// is turned on, it will delete itself as soon as it's particle count drops to zero.
  158. void deleteWhenEmpty();
  159. /// @name Particle Emission
  160. /// Main interface for creating particles. The emitter does _not_ track changes
  161. /// in axis or velocity over the course of a single update, so this should be called
  162. /// at a fairly fine grain. The emitter will potentially track the last particle
  163. /// to be created into the next call to this function in order to create a uniformly
  164. /// random time distribution of the particles. If the object to which the emitter is
  165. /// attached is in motion, it should try to ensure that for call (n+1) to this
  166. /// function, start is equal to the end from call (n). This will ensure a uniform
  167. /// spatial distribution.
  168. /// @{
  169. void emitParticles(const Point3F& start,
  170. const Point3F& end,
  171. const Point3F& axis,
  172. const Point3F& velocity,
  173. const U32 numMilliseconds);
  174. void emitParticles(const Point3F& point,
  175. const bool useLastPosition,
  176. const Point3F& axis,
  177. const Point3F& velocity,
  178. const U32 numMilliseconds);
  179. void emitParticles(const Point3F& rCenter,
  180. const Point3F& rNormal,
  181. const F32 radius,
  182. const Point3F& velocity,
  183. S32 count);
  184. /// @}
  185. bool mDead;
  186. protected:
  187. /// @name Internal interface
  188. /// @{
  189. /// Adds a particle
  190. /// @param pos Initial position of particle
  191. /// @param axis
  192. /// @param vel Initial velocity
  193. /// @param axisx
  194. void addParticle(const Point3F &pos, const Point3F &axis, const Point3F &vel, const Point3F &axisx, const U32 age_offset);
  195. void setupBillboard( Particle *part,
  196. Point3F *basePts,
  197. const MatrixF &camView,
  198. const LinearColorF &ambientColor,
  199. ParticleVertexType *lVerts );
  200. void setupOriented( Particle *part,
  201. const Point3F &camPos,
  202. const LinearColorF &ambientColor,
  203. ParticleVertexType *lVerts );
  204. inline void setupAligned( const Particle *part,
  205. const LinearColorF &ambientColor,
  206. ParticleVertexType *lVerts );
  207. inline void setupRibbon( Particle *part,
  208. Particle *next,
  209. Particle *prev,
  210. const Point3F &camPos,
  211. const LinearColorF &ambientColor,
  212. ParticleVertexType *lVerts);
  213. /// Updates the bounding box for the particle system
  214. void updateBBox();
  215. /// @}
  216. protected:
  217. bool onAdd() override;
  218. void onRemove() override;
  219. void processTick(const Move *move) override;
  220. void advanceTime(F32 dt) override;
  221. // Rendering
  222. protected:
  223. void prepRenderImage( SceneRenderState *state ) override;
  224. void copyToVB( const Point3F &camPos, const LinearColorF &ambientColor );
  225. // PEngine interface
  226. private:
  227. // AFX subclasses to ParticleEmitter require access to some members and methods of
  228. // ParticleEmitter which are normally declared with private scope. In this section,
  229. // protected and private scope statements have been inserted inline with the original
  230. // code to expose the necessary members and methods.
  231. void update( U32 ms );
  232. protected:
  233. void updateKeyData( Particle *part );
  234. private:
  235. /// Constant used to calculate particle
  236. /// rotation from spin and age.
  237. static const F32 AgedSpinToRadians;
  238. ParticleEmitterData* mDataBlock;
  239. protected:
  240. U32 mInternalClock;
  241. U32 mNextParticleTime;
  242. F32 mThetaOld;
  243. F32 mPhiOld;
  244. Point3F mLastPosition;
  245. bool mHasLastPosition;
  246. private:
  247. MatrixF mBBObjToWorld;
  248. bool mDeleteWhenEmpty;
  249. bool mDeleteOnTick;
  250. protected:
  251. S32 mLifetimeMS;
  252. S32 mElapsedTimeMS;
  253. private:
  254. F32 sizes[ ParticleData::PDC_NUM_KEYS ];
  255. LinearColorF colors[ ParticleData::PDC_NUM_KEYS ];
  256. GFXVertexBufferHandle<ParticleVertexType> mVertBuff;
  257. protected:
  258. // These members are for implementing a link-list of the active emitter
  259. // particles. Member part_store contains blocks of particles that can be
  260. // chained in a link-list. Usually the first part_store block is large
  261. // enough to contain all the particles but it can be expanded in emergency
  262. // circumstances.
  263. Vector <Particle*> part_store;
  264. Particle* part_freelist;
  265. Particle part_list_head;
  266. S32 n_part_capacity;
  267. S32 n_parts;
  268. private:
  269. S32 mCurBuffSize;
  270. protected:
  271. F32 fade_amt;
  272. bool forced_bbox;
  273. bool db_temp_clone;
  274. Point3F pos_pe;
  275. S8 sort_priority;
  276. virtual void sub_particleUpdate(Particle*) { }
  277. public:
  278. virtual void emitParticlesExt(const MatrixF& xfm, const Point3F& point, const Point3F& velocity, const U32 numMilliseconds);
  279. void setFadeAmount(F32 amt) { fade_amt = amt; }
  280. void setForcedObjBox(Box3F& box);
  281. void setSortPriority(S8 priority);
  282. #if defined(AFX_CAP_PARTICLE_POOLS)
  283. protected:
  284. afxParticlePool* pool;
  285. public:
  286. void clearPool() { pool = 0; }
  287. void setPool(afxParticlePool* p) { pool = p; }
  288. #endif
  289. };
  290. #endif // _H_PARTICLE_EMITTER