particleEmitter.h 13 KB

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