particleEmitter.h
Engine/source/T3D/fx/particleEmitter.h
Classes:
class
class
Detailed Description
1 2//----------------------------------------------------------------------------- 3// Copyright (c) 2012 GarageGames, LLC 4// 5// Permission is hereby granted, free of charge, to any person obtaining a copy 6// of this software and associated documentation files (the "Software"), to 7// deal in the Software without restriction, including without limitation the 8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 9// sell copies of the Software, and to permit persons to whom the Software is 10// furnished to do so, subject to the following conditions: 11// 12// The above copyright notice and this permission notice shall be included in 13// all copies or substantial portions of the Software. 14// 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21// IN THE SOFTWARE. 22//----------------------------------------------------------------------------- 23 24#ifndef _H_PARTICLE_EMITTER 25#define _H_PARTICLE_EMITTER 26 27#ifndef _GAMEBASE_H_ 28#include "T3D/gameBase/gameBase.h" 29#endif 30#ifndef _COLOR_H_ 31#include "core/color.h" 32#endif 33#ifndef _GFXPRIMITIVEBUFFER_H_ 34#include "gfx/gfxPrimitiveBuffer.h" 35#endif 36#ifndef _GFXVERTEXBUFFER_H_ 37#include "gfx/gfxVertexBuffer.h" 38#endif 39#ifndef _PARTICLE_H_ 40#include "T3D/fx/particle.h" 41#endif 42 43#if defined(TORQUE_OS_XENON) 44#include "gfx/D3D9/360/gfx360MemVertexBuffer.h" 45#endif 46 47class RenderPassManager; 48class ParticleData; 49 50//***************************************************************************** 51// Particle Emitter Data 52//***************************************************************************** 53class ParticleEmitterData : public GameBaseData 54{ 55 typedef GameBaseData Parent; 56 57 static bool _setAlignDirection( void *object, const char *index, const char *data ); 58 59 public: 60 61 ParticleEmitterData(); 62 DECLARE_CONOBJECT(ParticleEmitterData); 63 static void initPersistFields(); 64 void packData(BitStream* stream); 65 void unpackData(BitStream* stream); 66 bool preload(bool server, String &errorStr); 67 bool onAdd(); 68 void allocPrimBuffer( S32 overrideSize = -1 ); 69 70 public: 71 S32 ejectionPeriodMS; ///< Time, in Milliseconds, between particle ejection 72 S32 periodVarianceMS; ///< Varience in ejection peroid between 0 and n 73 74 F32 ejectionVelocity; ///< Ejection velocity 75 F32 velocityVariance; ///< Variance for velocity between 0 and n 76 F32 ejectionOffset; ///< Z offset from emitter point to eject from 77 F32 ejectionOffsetVariance; ///< Z offset Variance from emitter point to eject 78 F32 thetaMin; ///< Minimum angle, from the horizontal plane, to eject from 79 F32 thetaMax; ///< Maximum angle, from the horizontal plane, to eject from 80 81 F32 phiReferenceVel; ///< Reference angle, from the verticle plane, to eject from 82 F32 phiVariance; ///< Varience from the reference angle, from 0 to n 83 84 F32 softnessDistance; ///< For soft particles, the distance (in meters) where particles will be faded 85 ///< based on the difference in depth between the particle and the scene geometry. 86 87 /// A scalar value used to influence the effect 88 /// of the ambient color on the particle. 89 F32 ambientFactor; 90 91 S32 lifetimeMS; ///< Lifetime of particles 92 U32 lifetimeVarianceMS; ///< Varience in lifetime from 0 to n 93 94 bool overrideAdvance; ///< 95 bool orientParticles; ///< Particles always face the screen 96 bool orientOnVelocity; ///< Particles face the screen at the start 97 bool useEmitterSizes; ///< Use emitter specified sizes instead of datablock sizes 98 bool useEmitterColors; ///< Use emitter specified colors instead of datablock colors 99 bool alignParticles; ///< Particles always face along a particular axis 100 Point3F alignDirection; ///< The direction aligned particles should face 101 102 StringTableEntry particleString; ///< Used to load particle data directly from a string 103 104 Vector<ParticleData*> particleDataBlocks; ///< Particle Datablocks 105 Vector<U32> dataBlockIds; ///< Datablock IDs (parellel array to particleDataBlocks) 106 107 U32 partListInitSize; /// initial size of particle list calc'd from datablock info 108 109 GFXPrimitiveBufferHandle primBuff; 110 111 S32 blendStyle; ///< Pre-define blend factor setting 112 bool sortParticles; ///< Particles are sorted back-to-front 113 bool reverseOrder; ///< reverses draw order 114 StringTableEntry textureName; ///< Emitter texture file to override particle textures 115 GFXTexHandle textureHandle; ///< Emitter texture handle from txrName 116 bool highResOnly; ///< This particle system should not use the mixed-resolution particle rendering 117 bool renderReflection; ///< Enables this emitter to render into reflection passes. 118 bool glow; ///< Renders this emitter into the glow buffer. 119 120 bool reload(); 121}; 122 123//***************************************************************************** 124// Particle Emitter 125//***************************************************************************** 126class ParticleEmitter : public GameBase 127{ 128 typedef GameBase Parent; 129 130 public: 131 132#if defined(TORQUE_OS_XENON) 133 typedef GFXVertexPCTT ParticleVertexType; 134#else 135 typedef GFXVertexPCT ParticleVertexType; 136#endif 137 138 ParticleEmitter(); 139 ~ParticleEmitter(); 140 141 DECLARE_CONOBJECT(ParticleEmitter); 142 143 static Point3F mWindVelocity; 144 static void setWindVelocity( const Point3F &vel ){ mWindVelocity = vel; } 145 146 ColorF getCollectiveColor(); 147 148 /// Sets sizes of particles based on sizelist provided 149 /// @param sizeList List of sizes 150 void setSizes( F32 *sizeList ); 151 152 /// Sets colors for particles based on color list provided 153 /// @param colorList List of colors 154 void setColors( ColorF *colorList ); 155 156 ParticleEmitterData *getDataBlock(){ return mDataBlock; } 157 bool onNewDataBlock( GameBaseData *dptr, bool reload ); 158 159 /// By default, a particle renderer will wait for it's owner to delete it. When this 160 /// is turned on, it will delete itself as soon as it's particle count drops to zero. 161 void deleteWhenEmpty(); 162 163 /// @name Particle Emission 164 /// Main interface for creating particles. The emitter does _not_ track changes 165 /// in axis or velocity over the course of a single update, so this should be called 166 /// at a fairly fine grain. The emitter will potentially track the last particle 167 /// to be created into the next call to this function in order to create a uniformly 168 /// random time distribution of the particles. If the object to which the emitter is 169 /// attached is in motion, it should try to ensure that for call (n+1) to this 170 /// function, start is equal to the end from call (n). This will ensure a uniform 171 /// spatial distribution. 172 /// @{ 173 174 void emitParticles(const Point3F& start, 175 const Point3F& end, 176 const Point3F& axis, 177 const Point3F& velocity, 178 const U32 numMilliseconds); 179 void emitParticles(const Point3F& point, 180 const bool useLastPosition, 181 const Point3F& axis, 182 const Point3F& velocity, 183 const U32 numMilliseconds); 184 void emitParticles(const Point3F& rCenter, 185 const Point3F& rNormal, 186 const F32 radius, 187 const Point3F& velocity, 188 S32 count); 189 /// @} 190 191 bool mDead; 192 193 protected: 194 /// @name Internal interface 195 /// @{ 196 197 /// Adds a particle 198 /// @param pos Initial position of particle 199 /// @param axis 200 /// @param vel Initial velocity 201 /// @param axisx 202 void addParticle(const Point3F &pos, const Point3F &axis, const Point3F &vel, const Point3F &axisx); 203 204 205 inline void setupBillboard( Particle *part, 206 Point3F *basePts, 207 const MatrixF &camView, 208 const ColorF &ambientColor, 209 ParticleVertexType *lVerts ); 210 211 inline void setupOriented( Particle *part, 212 const Point3F &camPos, 213 const ColorF &ambientColor, 214 ParticleVertexType *lVerts ); 215 216 inline void setupAligned( const Particle *part, 217 const ColorF &ambientColor, 218 ParticleVertexType *lVerts ); 219 220 /// Updates the bounding box for the particle system 221 void updateBBox(); 222 223 /// @} 224 protected: 225 bool onAdd(); 226 void onRemove(); 227 228 void processTick(const Move *move); 229 void advanceTime(F32 dt); 230 231 // Rendering 232 protected: 233 void prepRenderImage( SceneRenderState *state ); 234 void copyToVB( const Point3F &camPos, const ColorF &ambientColor ); 235 236 // PEngine interface 237 private: 238 239 void update( U32 ms ); 240 inline void updateKeyData( Particle *part ); 241 242 243 private: 244 245 /// Constant used to calculate particle 246 /// rotation from spin and age. 247 static const F32 AgedSpinToRadians; 248 249 ParticleEmitterData* mDataBlock; 250 251 U32 mInternalClock; 252 253 U32 mNextParticleTime; 254 255 Point3F mLastPosition; 256 bool mHasLastPosition; 257 MatrixF mBBObjToWorld; 258 259 bool mDeleteWhenEmpty; 260 bool mDeleteOnTick; 261 262 S32 mLifetimeMS; 263 S32 mElapsedTimeMS; 264 265 F32 sizes[ ParticleData::PDC_NUM_KEYS ]; 266 ColorF colors[ ParticleData::PDC_NUM_KEYS ]; 267 268#if defined(TORQUE_OS_XENON) 269 GFX360MemVertexBufferHandle<ParticleVertexType> mVertBuff; 270#else 271 GFXVertexBufferHandle<ParticleVertexType> mVertBuff; 272#endif 273 274 // These members are for implementing a link-list of the active emitter 275 // particles. Member part_store contains blocks of particles that can be 276 // chained in a link-list. Usually the first part_store block is large 277 // enough to contain all the particles but it can be expanded in emergency 278 // circumstances. 279 Vector <Particle*> part_store; 280 Particle* part_freelist; 281 Particle part_list_head; 282 S32 n_part_capacity; 283 S32 n_parts; 284 S32 mCurBuffSize; 285 286}; 287 288#endif // _H_PARTICLE_EMITTER 289 290
