particleEmitter.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. U32 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 reload();
  98. };
  99. //*****************************************************************************
  100. // Particle Emitter
  101. //*****************************************************************************
  102. class ParticleEmitter : public GameBase
  103. {
  104. typedef GameBase Parent;
  105. public:
  106. #if defined(TORQUE_OS_XENON)
  107. typedef GFXVertexPCTT ParticleVertexType;
  108. #else
  109. typedef GFXVertexPCT ParticleVertexType;
  110. #endif
  111. ParticleEmitter();
  112. ~ParticleEmitter();
  113. DECLARE_CONOBJECT(ParticleEmitter);
  114. static Point3F mWindVelocity;
  115. static void setWindVelocity( const Point3F &vel ){ mWindVelocity = vel; }
  116. ColorF getCollectiveColor();
  117. /// Sets sizes of particles based on sizelist provided
  118. /// @param sizeList List of sizes
  119. void setSizes( F32 *sizeList );
  120. /// Sets colors for particles based on color list provided
  121. /// @param colorList List of colors
  122. void setColors( ColorF *colorList );
  123. ParticleEmitterData *getDataBlock(){ return mDataBlock; }
  124. bool onNewDataBlock( GameBaseData *dptr, bool reload );
  125. /// By default, a particle renderer will wait for it's owner to delete it. When this
  126. /// is turned on, it will delete itself as soon as it's particle count drops to zero.
  127. void deleteWhenEmpty();
  128. /// @name Particle Emission
  129. /// Main interface for creating particles. The emitter does _not_ track changes
  130. /// in axis or velocity over the course of a single update, so this should be called
  131. /// at a fairly fine grain. The emitter will potentially track the last particle
  132. /// to be created into the next call to this function in order to create a uniformly
  133. /// random time distribution of the particles. If the object to which the emitter is
  134. /// attached is in motion, it should try to ensure that for call (n+1) to this
  135. /// function, start is equal to the end from call (n). This will ensure a uniform
  136. /// spatial distribution.
  137. /// @{
  138. void emitParticles(const Point3F& start,
  139. const Point3F& end,
  140. const Point3F& axis,
  141. const Point3F& velocity,
  142. const U32 numMilliseconds);
  143. void emitParticles(const Point3F& point,
  144. const bool useLastPosition,
  145. const Point3F& axis,
  146. const Point3F& velocity,
  147. const U32 numMilliseconds);
  148. void emitParticles(const Point3F& rCenter,
  149. const Point3F& rNormal,
  150. const F32 radius,
  151. const Point3F& velocity,
  152. S32 count);
  153. /// @}
  154. bool mDead;
  155. protected:
  156. /// @name Internal interface
  157. /// @{
  158. /// Adds a particle
  159. /// @param pos Initial position of particle
  160. /// @param axis
  161. /// @param vel Initial velocity
  162. /// @param axisx
  163. void addParticle(const Point3F &pos, const Point3F &axis, const Point3F &vel, const Point3F &axisx);
  164. inline void setupBillboard( Particle *part,
  165. Point3F *basePts,
  166. const MatrixF &camView,
  167. const ColorF &ambientColor,
  168. ParticleVertexType *lVerts );
  169. inline void setupOriented( Particle *part,
  170. const Point3F &camPos,
  171. const ColorF &ambientColor,
  172. ParticleVertexType *lVerts );
  173. inline void setupAligned( const Particle *part,
  174. const ColorF &ambientColor,
  175. ParticleVertexType *lVerts );
  176. /// Updates the bounding box for the particle system
  177. void updateBBox();
  178. /// @}
  179. protected:
  180. bool onAdd();
  181. void onRemove();
  182. void processTick(const Move *move);
  183. void advanceTime(F32 dt);
  184. // Rendering
  185. protected:
  186. void prepRenderImage( SceneRenderState *state );
  187. void copyToVB( const Point3F &camPos, const ColorF &ambientColor );
  188. // PEngine interface
  189. private:
  190. void update( U32 ms );
  191. inline void updateKeyData( Particle *part );
  192. private:
  193. /// Constant used to calculate particle
  194. /// rotation from spin and age.
  195. static const F32 AgedSpinToRadians;
  196. ParticleEmitterData* mDataBlock;
  197. U32 mInternalClock;
  198. U32 mNextParticleTime;
  199. Point3F mLastPosition;
  200. bool mHasLastPosition;
  201. MatrixF mBBObjToWorld;
  202. bool mDeleteWhenEmpty;
  203. bool mDeleteOnTick;
  204. S32 mLifetimeMS;
  205. S32 mElapsedTimeMS;
  206. F32 sizes[ ParticleData::PDC_NUM_KEYS ];
  207. ColorF colors[ ParticleData::PDC_NUM_KEYS ];
  208. #if defined(TORQUE_OS_XENON)
  209. GFX360MemVertexBufferHandle<ParticleVertexType> mVertBuff;
  210. #else
  211. GFXVertexBufferHandle<ParticleVertexType> mVertBuff;
  212. #endif
  213. // These members are for implementing a link-list of the active emitter
  214. // particles. Member part_store contains blocks of particles that can be
  215. // chained in a link-list. Usually the first part_store block is large
  216. // enough to contain all the particles but it can be expanded in emergency
  217. // circumstances.
  218. Vector <Particle*> part_store;
  219. Particle* part_freelist;
  220. Particle part_list_head;
  221. S32 n_part_capacity;
  222. S32 n_parts;
  223. S32 mCurBuffSize;
  224. };
  225. #endif // _H_PARTICLE_EMITTER