particleEmitter.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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);
  64. void unpackData(BitStream* stream);
  65. bool preload(bool server, String &errorStr);
  66. bool onAdd();
  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. U32 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. virtual bool allowSubstitutions() const { 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. static Point3F mWindVelocity;
  145. static void setWindVelocity( const Point3F &vel ){ mWindVelocity = vel; }
  146. LinearColorF getCollectiveColor();
  147. /// Sets sizes of particles based on sizelist provided
  148. /// @param sizeList List of sizes
  149. void setSizes( F32 *sizeList );
  150. /// Sets colors for particles based on color list provided
  151. /// @param colorList List of colors
  152. void setColors( LinearColorF *colorList );
  153. ParticleEmitterData *getDataBlock(){ return mDataBlock; }
  154. bool onNewDataBlock( GameBaseData *dptr, bool reload );
  155. /// By default, a particle renderer will wait for it's owner to delete it. When this
  156. /// is turned on, it will delete itself as soon as it's particle count drops to zero.
  157. void deleteWhenEmpty();
  158. /// @name Particle Emission
  159. /// Main interface for creating particles. The emitter does _not_ track changes
  160. /// in axis or velocity over the course of a single update, so this should be called
  161. /// at a fairly fine grain. The emitter will potentially track the last particle
  162. /// to be created into the next call to this function in order to create a uniformly
  163. /// random time distribution of the particles. If the object to which the emitter is
  164. /// attached is in motion, it should try to ensure that for call (n+1) to this
  165. /// function, start is equal to the end from call (n). This will ensure a uniform
  166. /// spatial distribution.
  167. /// @{
  168. void emitParticles(const Point3F& start,
  169. const Point3F& end,
  170. const Point3F& axis,
  171. const Point3F& velocity,
  172. const U32 numMilliseconds);
  173. void emitParticles(const Point3F& point,
  174. const bool useLastPosition,
  175. const Point3F& axis,
  176. const Point3F& velocity,
  177. const U32 numMilliseconds);
  178. void emitParticles(const Point3F& rCenter,
  179. const Point3F& rNormal,
  180. const F32 radius,
  181. const Point3F& velocity,
  182. S32 count);
  183. /// @}
  184. bool mDead;
  185. protected:
  186. /// @name Internal interface
  187. /// @{
  188. /// Adds a particle
  189. /// @param pos Initial position of particle
  190. /// @param axis
  191. /// @param vel Initial velocity
  192. /// @param axisx
  193. void addParticle(const Point3F &pos, const Point3F &axis, const Point3F &vel, const Point3F &axisx, const U32 age_offset);
  194. inline void setupBillboard( Particle *part,
  195. Point3F *basePts,
  196. const MatrixF &camView,
  197. const LinearColorF &ambientColor,
  198. ParticleVertexType *lVerts );
  199. inline void setupOriented( Particle *part,
  200. const Point3F &camPos,
  201. const LinearColorF &ambientColor,
  202. ParticleVertexType *lVerts );
  203. inline void setupAligned( const Particle *part,
  204. const LinearColorF &ambientColor,
  205. ParticleVertexType *lVerts );
  206. inline void setupRibbon( Particle *part,
  207. Particle *next,
  208. Particle *prev,
  209. const Point3F &camPos,
  210. const LinearColorF &ambientColor,
  211. ParticleVertexType *lVerts);
  212. /// Updates the bounding box for the particle system
  213. void updateBBox();
  214. /// @}
  215. protected:
  216. bool onAdd();
  217. void onRemove();
  218. void processTick(const Move *move);
  219. void advanceTime(F32 dt);
  220. // Rendering
  221. protected:
  222. void prepRenderImage( SceneRenderState *state );
  223. void copyToVB( const Point3F &camPos, const LinearColorF &ambientColor );
  224. // PEngine interface
  225. private:
  226. // AFX subclasses to ParticleEmitter require access to some members and methods of
  227. // ParticleEmitter which are normally declared with private scope. In this section,
  228. // protected and private scope statements have been inserted inline with the original
  229. // code to expose the necessary members and methods.
  230. void update( U32 ms );
  231. protected:
  232. void updateKeyData( Particle *part );
  233. private:
  234. /// Constant used to calculate particle
  235. /// rotation from spin and age.
  236. static const F32 AgedSpinToRadians;
  237. ParticleEmitterData* mDataBlock;
  238. protected:
  239. U32 mInternalClock;
  240. U32 mNextParticleTime;
  241. F32 mThetaOld;
  242. F32 mPhiOld;
  243. Point3F mLastPosition;
  244. bool mHasLastPosition;
  245. private:
  246. MatrixF mBBObjToWorld;
  247. bool mDeleteWhenEmpty;
  248. bool mDeleteOnTick;
  249. protected:
  250. S32 mLifetimeMS;
  251. S32 mElapsedTimeMS;
  252. private:
  253. F32 sizes[ ParticleData::PDC_NUM_KEYS ];
  254. LinearColorF colors[ ParticleData::PDC_NUM_KEYS ];
  255. GFXVertexBufferHandle<ParticleVertexType> mVertBuff;
  256. protected:
  257. // These members are for implementing a link-list of the active emitter
  258. // particles. Member part_store contains blocks of particles that can be
  259. // chained in a link-list. Usually the first part_store block is large
  260. // enough to contain all the particles but it can be expanded in emergency
  261. // circumstances.
  262. Vector <Particle*> part_store;
  263. Particle* part_freelist;
  264. Particle part_list_head;
  265. S32 n_part_capacity;
  266. S32 n_parts;
  267. private:
  268. S32 mCurBuffSize;
  269. protected:
  270. F32 fade_amt;
  271. bool forced_bbox;
  272. bool db_temp_clone;
  273. Point3F pos_pe;
  274. S8 sort_priority;
  275. virtual void sub_particleUpdate(Particle*) { }
  276. public:
  277. virtual void emitParticlesExt(const MatrixF& xfm, const Point3F& point, const Point3F& velocity, const U32 numMilliseconds);
  278. void setFadeAmount(F32 amt) { fade_amt = amt; }
  279. void setForcedObjBox(Box3F& box);
  280. void setSortPriority(S8 priority);
  281. #if defined(AFX_CAP_PARTICLE_POOLS)
  282. protected:
  283. afxParticlePool* pool;
  284. public:
  285. void clearPool() { pool = 0; }
  286. void setPool(afxParticlePool* p) { pool = p; }
  287. #endif
  288. };
  289. #endif // _H_PARTICLE_EMITTER