particleEmitter.h 13 KB

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