particleEmitter.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. #ifndef _H_PARTICLE_EMITTER
  23. #define _H_PARTICLE_EMITTER
  24. #ifndef _GAMEBASE_H_
  25. #include "T3D/gameBase/gameBase.h"
  26. #endif
  27. #ifndef _COLOR_H_
  28. #include "core/color.h"
  29. #endif
  30. #ifndef _GFXPRIMITIVEBUFFER_H_
  31. #include "gfx/gfxPrimitiveBuffer.h"
  32. #endif
  33. #ifndef _GFXVERTEXBUFFER_H_
  34. #include "gfx/gfxVertexBuffer.h"
  35. #endif
  36. #ifndef _PARTICLE_H_
  37. #include "T3D/fx/particle.h"
  38. #endif
  39. #if defined(TORQUE_OS_XENON)
  40. #include "gfx/D3D9/360/gfx360MemVertexBuffer.h"
  41. #endif
  42. class RenderPassManager;
  43. class ParticleData;
  44. //*****************************************************************************
  45. // Particle Emitter Data
  46. //*****************************************************************************
  47. class ParticleEmitterData : public GameBaseData
  48. {
  49. typedef GameBaseData Parent;
  50. static bool _setAlignDirection( void *object, const char *index, const char *data );
  51. public:
  52. ParticleEmitterData();
  53. DECLARE_CONOBJECT(ParticleEmitterData);
  54. static void initPersistFields();
  55. void packData(BitStream* stream);
  56. void unpackData(BitStream* stream);
  57. bool preload(bool server, String &errorStr);
  58. bool onAdd();
  59. void allocPrimBuffer( S32 overrideSize = -1 );
  60. public:
  61. S32 ejectionPeriodMS; ///< Time, in Milliseconds, between particle ejection
  62. S32 periodVarianceMS; ///< Varience in ejection peroid between 0 and n
  63. F32 ejectionVelocity; ///< Ejection velocity
  64. F32 velocityVariance; ///< Variance for velocity between 0 and n
  65. F32 ejectionOffset; ///< Z offset from emitter point to eject from
  66. F32 ejectionOffsetVariance; ///< Z offset Variance from emitter point to eject
  67. F32 thetaMin; ///< Minimum angle, from the horizontal plane, to eject from
  68. F32 thetaMax; ///< Maximum angle, from the horizontal plane, to eject from
  69. F32 phiReferenceVel; ///< Reference angle, from the verticle plane, to eject from
  70. F32 phiVariance; ///< Varience from the reference angle, from 0 to n
  71. F32 softnessDistance; ///< For soft particles, the distance (in meters) where particles will be faded
  72. ///< based on the difference in depth between the particle and the scene geometry.
  73. /// A scalar value used to influence the effect
  74. /// of the ambient color on the particle.
  75. F32 ambientFactor;
  76. S32 lifetimeMS; ///< Lifetime of particles
  77. U32 lifetimeVarianceMS; ///< Varience in lifetime from 0 to n
  78. bool overrideAdvance; ///<
  79. bool orientParticles; ///< Particles always face the screen
  80. bool orientOnVelocity; ///< Particles face the screen at the start
  81. bool useEmitterSizes; ///< Use emitter specified sizes instead of datablock sizes
  82. bool useEmitterColors; ///< Use emitter specified colors instead of datablock colors
  83. bool alignParticles; ///< Particles always face along a particular axis
  84. Point3F alignDirection; ///< The direction aligned particles should face
  85. StringTableEntry particleString; ///< Used to load particle data directly from a string
  86. Vector<ParticleData*> particleDataBlocks; ///< Particle Datablocks
  87. Vector<U32> dataBlockIds; ///< Datablock IDs (parellel array to particleDataBlocks)
  88. U32 partListInitSize; /// initial size of particle list calc'd from datablock info
  89. GFXPrimitiveBufferHandle primBuff;
  90. S32 blendStyle; ///< Pre-define blend factor setting
  91. bool sortParticles; ///< Particles are sorted back-to-front
  92. bool reverseOrder; ///< reverses draw order
  93. StringTableEntry textureName; ///< Emitter texture file to override particle textures
  94. GFXTexHandle textureHandle; ///< Emitter texture handle from txrName
  95. bool highResOnly; ///< This particle system should not use the mixed-resolution particle rendering
  96. bool renderReflection; ///< Enables this emitter to render into reflection passes.
  97. bool glow; ///< Renders this emitter into the glow buffer.
  98. bool reload();
  99. };
  100. //*****************************************************************************
  101. // Particle Emitter
  102. //*****************************************************************************
  103. class ParticleEmitter : public GameBase
  104. {
  105. typedef GameBase Parent;
  106. public:
  107. #if defined(TORQUE_OS_XENON)
  108. typedef GFXVertexPCTT ParticleVertexType;
  109. #else
  110. typedef GFXVertexPCT ParticleVertexType;
  111. #endif
  112. ParticleEmitter();
  113. ~ParticleEmitter();
  114. DECLARE_CONOBJECT(ParticleEmitter);
  115. static Point3F mWindVelocity;
  116. static void setWindVelocity( const Point3F &vel ){ mWindVelocity = vel; }
  117. ColorF getCollectiveColor();
  118. /// Sets sizes of particles based on sizelist provided
  119. /// @param sizeList List of sizes
  120. void setSizes( F32 *sizeList );
  121. /// Sets colors for particles based on color list provided
  122. /// @param colorList List of colors
  123. void setColors( ColorF *colorList );
  124. ParticleEmitterData *getDataBlock(){ return mDataBlock; }
  125. bool onNewDataBlock( GameBaseData *dptr, bool reload );
  126. /// By default, a particle renderer will wait for it's owner to delete it. When this
  127. /// is turned on, it will delete itself as soon as it's particle count drops to zero.
  128. void deleteWhenEmpty();
  129. /// @name Particle Emission
  130. /// Main interface for creating particles. The emitter does _not_ track changes
  131. /// in axis or velocity over the course of a single update, so this should be called
  132. /// at a fairly fine grain. The emitter will potentially track the last particle
  133. /// to be created into the next call to this function in order to create a uniformly
  134. /// random time distribution of the particles. If the object to which the emitter is
  135. /// attached is in motion, it should try to ensure that for call (n+1) to this
  136. /// function, start is equal to the end from call (n). This will ensure a uniform
  137. /// spatial distribution.
  138. /// @{
  139. void emitParticles(const Point3F& start,
  140. const Point3F& end,
  141. const Point3F& axis,
  142. const Point3F& velocity,
  143. const U32 numMilliseconds);
  144. void emitParticles(const Point3F& point,
  145. const bool useLastPosition,
  146. const Point3F& axis,
  147. const Point3F& velocity,
  148. const U32 numMilliseconds);
  149. void emitParticles(const Point3F& rCenter,
  150. const Point3F& rNormal,
  151. const F32 radius,
  152. const Point3F& velocity,
  153. S32 count);
  154. /// @}
  155. bool mDead;
  156. protected:
  157. /// @name Internal interface
  158. /// @{
  159. /// Adds a particle
  160. /// @param pos Initial position of particle
  161. /// @param axis
  162. /// @param vel Initial velocity
  163. /// @param axisx
  164. void addParticle(const Point3F &pos, const Point3F &axis, const Point3F &vel, const Point3F &axisx);
  165. inline void setupBillboard( Particle *part,
  166. Point3F *basePts,
  167. const MatrixF &camView,
  168. const ColorF &ambientColor,
  169. ParticleVertexType *lVerts );
  170. inline void setupOriented( Particle *part,
  171. const Point3F &camPos,
  172. const ColorF &ambientColor,
  173. ParticleVertexType *lVerts );
  174. inline void setupAligned( const Particle *part,
  175. const ColorF &ambientColor,
  176. ParticleVertexType *lVerts );
  177. /// Updates the bounding box for the particle system
  178. void updateBBox();
  179. /// @}
  180. protected:
  181. bool onAdd();
  182. void onRemove();
  183. void processTick(const Move *move);
  184. void advanceTime(F32 dt);
  185. // Rendering
  186. protected:
  187. void prepRenderImage( SceneRenderState *state );
  188. void copyToVB( const Point3F &camPos, const ColorF &ambientColor );
  189. // PEngine interface
  190. private:
  191. void update( U32 ms );
  192. inline void updateKeyData( Particle *part );
  193. private:
  194. /// Constant used to calculate particle
  195. /// rotation from spin and age.
  196. static const F32 AgedSpinToRadians;
  197. ParticleEmitterData* mDataBlock;
  198. U32 mInternalClock;
  199. U32 mNextParticleTime;
  200. Point3F mLastPosition;
  201. bool mHasLastPosition;
  202. MatrixF mBBObjToWorld;
  203. bool mDeleteWhenEmpty;
  204. bool mDeleteOnTick;
  205. S32 mLifetimeMS;
  206. S32 mElapsedTimeMS;
  207. F32 sizes[ ParticleData::PDC_NUM_KEYS ];
  208. ColorF colors[ ParticleData::PDC_NUM_KEYS ];
  209. #if defined(TORQUE_OS_XENON)
  210. GFX360MemVertexBufferHandle<ParticleVertexType> mVertBuff;
  211. #else
  212. GFXVertexBufferHandle<ParticleVertexType> mVertBuff;
  213. #endif
  214. // These members are for implementing a link-list of the active emitter
  215. // particles. Member part_store contains blocks of particles that can be
  216. // chained in a link-list. Usually the first part_store block is large
  217. // enough to contain all the particles but it can be expanded in emergency
  218. // circumstances.
  219. Vector <Particle*> part_store;
  220. Particle* part_freelist;
  221. Particle part_list_head;
  222. S32 n_part_capacity;
  223. S32 n_parts;
  224. S32 mCurBuffSize;
  225. };
  226. #endif // _H_PARTICLE_EMITTER