Torque3D Documentation / _generateds / precipitation.h

precipitation.h

Engine/source/T3D/fx/precipitation.h

More...

Classes:

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 _PRECIPITATION_H_
 25#define _PRECIPITATION_H_
 26
 27#include "gfx/gfxDevice.h"
 28#include "T3D/gameBase/gameBase.h"
 29
 30#ifndef _GFXPRIMITIVEBUFFER_H_
 31#include "gfx/gfxPrimitiveBuffer.h"
 32#endif
 33#ifndef _RENDERPASSMANAGER_H_
 34#include "renderInstance/renderPassManager.h"
 35#endif
 36
 37class SFXTrack;
 38class SFXSource;
 39
 40//--------------------------------------------------------------------------
 41/// Precipitation datablock.
 42class PrecipitationData : public GameBaseData 
 43{
 44   typedef GameBaseData Parent;
 45
 46  public:
 47   SFXTrack*     soundProfile;
 48
 49   StringTableEntry mDropName;         ///< Texture filename for drop particles
 50   StringTableEntry mDropShaderName;   ///< The name of the shader used for raindrops
 51   StringTableEntry mSplashName;       ///< Texture filename for splash particles
 52   StringTableEntry mSplashShaderName; ///< The name of the shader used for raindrops
 53
 54   S32  mDropsPerSide;     ///< How many drops are on a side of the raindrop texture.
 55   S32  mSplashesPerSide;  ///< How many splash are on a side of the splash texture.
 56   
 57   PrecipitationData();
 58   DECLARE_CONOBJECT(PrecipitationData);
 59   bool preload( bool server, String& errorStr );
 60   static void  initPersistFields();
 61   virtual void packData(BitStream* stream);
 62   virtual void unpackData(BitStream* stream);
 63};
 64
 65struct Raindrop
 66{
 67   F32 velocity;           ///< How fast the drop is falling downwards
 68   Point3F position;       ///< Position of the drop
 69   Point3F renderPosition; ///< Interpolated render-position of the drop
 70   F32 time;               ///< Time into the turbulence function
 71   F32 mass;               ///< Mass of drop used for how much turbulence/wind effects the drop
 72
 73   U32 texCoordIndex;      ///< Which piece of the material will be used
 74
 75   bool toRender;          ///< Don't want to render all drops, just the ones that pass a few tests
 76   bool valid;             ///< Drop becomes invalid after hitting something.  Just keep updating
 77                           ///<   the position of it, but don't render until it hits the bottom
 78                           ///<   of the renderbox and respawns
 79
 80   Point3F  hitPos;        ///< Point at which the drop will collide with something
 81   U32      hitType;       ///< What kind of object the drop will hit
 82
 83   Raindrop *nextSplashDrop;  ///< Linked list cruft for easily adding/removing stuff from the splash list
 84   Raindrop *prevSplashDrop;  ///< Same as next but previous!
 85
 86   SimTime animStartTime;     ///< Animation time tracker
 87
 88   Raindrop* next;         ///< linked list cruft
 89
 90   Raindrop()
 91   {
 92      velocity = 0;
 93      time = 0;
 94      mass = 1;
 95      texCoordIndex = 0;
 96      next = NULL;
 97      toRender = false;
 98      valid = true;
 99      nextSplashDrop = NULL;
100      prevSplashDrop = NULL;
101      animStartTime = 0;
102      hitType = 0;
103      hitPos = Point3F(0,0,0);
104   }
105};
106
107//--------------------------------------------------------------------------
108class Precipitation : public GameBase
109{
110  protected:
111
112   typedef GameBase Parent;
113   PrecipitationData*   mDataBlock;
114
115   Raindrop *mDropHead;    ///< Drop linked list head
116   Raindrop *mSplashHead;  ///< Splash linked list head
117
118   Point2F* mTexCoords;     ///< texture coords for rain texture
119   Point2F* mSplashCoords;  ///< texture coordinates for splash texture
120
121   SFXSource* mAmbientSound;        ///< Ambient sound
122
123   GFXShaderRef mDropShader;     ///< The shader used for raindrops
124   GFXTexHandle mDropHandle;     ///< Texture handle for raindrop
125   GFXShaderRef mSplashShader;   ///< The shader used for splashes
126   GFXTexHandle mSplashHandle;   ///< Texture handle for splash
127
128   U32 mLastRenderFrame;         ///< Used to skip processTick when we haven't been visible.
129
130   U32 mDropHitMask;             ///< Stores the current drop hit mask.
131
132   //console exposed variables
133   bool mFollowCam;                 ///< Does the system follow the camera or stay where it's placed.
134
135   F32  mDropSize;                  ///< Droplet billboard size
136   F32  mSplashSize;                ///< Splash billboard size
137   bool mUseTrueBillboards;         ///< True to use true billboards, false for axis-aligned billboards
138   S32  mSplashMS;                  ///< How long in milliseconds a splash will last
139   bool mAnimateSplashes;           ///< Animate the splashes using the frames in the texture.
140
141   S32 mDropAnimateMS;           ///< If greater than zero, will animate the drops from
142                                 ///< the frames in the texture
143
144   S32 mNumDrops;                ///< Number of drops in the scene
145   F32 mPercentage;              ///< Server-side set var (NOT exposed to console)
146                                 ///< which controls how many drops are present [0,1]
147
148   F32 mMinSpeed;                ///< Minimum downward speed of drops
149   F32 mMaxSpeed;                ///< Maximum downward speed of drops
150
151   F32 mMinMass;                 ///< Minimum mass of drops
152   F32 mMaxMass;                 ///< Maximum mass of drops
153
154   F32 mBoxWidth;                ///< How far away in the x and y directions drops will render
155   F32 mBoxHeight;               ///< How high drops will render
156
157   F32 mMaxTurbulence;           ///< Coefficient to sin/cos for adding turbulence
158   F32 mTurbulenceSpeed;         ///< How fast the turbulence wraps in a circle
159   bool mUseTurbulence;          ///< Whether to use turbulence or not (MAY EFFECT PERFORMANCE)
160
161   bool mUseLighting;            ///< This enables shading of the drops and splashes
162                                 ///< by the sun color.
163
164   LinearColorF mGlowIntensity;        ///< Set it to 0 to disable the glow or use it to control 
165                                 ///< the intensity of each channel.
166
167   bool mReflect;                ///< This enables the precipitation to be rendered
168                                 ///< during reflection passes.  This is expensive.
169
170   bool mUseWind;                ///< This enables the wind from the sky SceneObject
171                                 ///< to effect the velocitiy of the drops.
172
173   bool mRotateWithCamVel;       ///< Rotate the drops relative to the camera velocity
174                                 ///< This is useful for "streak" type drops
175
176   bool mDoCollision;            ///< Whether or not to do collision
177   bool mDropHitPlayers;         ///< Should drops collide with players
178   bool mDropHitVehicles;        ///< Should drops collide with vehicles
179
180   F32 mFadeDistance;            ///< The distance at which fading of the particles begins.
181   F32 mFadeDistanceEnd;         ///< The distance at which fading of the particles ends.
182
183   U32 mMaxVBDrops;              ///< The maximum drops allowed in one render batch.
184
185   GFXStateBlockRef mDefaultSB;
186   GFXStateBlockRef mDistantSB;
187
188   GFXShaderConstBufferRef mDropShaderConsts;
189   
190   GFXShaderConstHandle* mDropShaderModelViewSC;
191   GFXShaderConstHandle* mDropShaderFadeStartEndSC;
192   GFXShaderConstHandle* mDropShaderCameraPosSC;
193   GFXShaderConstHandle* mDropShaderAmbientSC;
194
195   GFXShaderConstBufferRef mSplashShaderConsts;
196   
197   GFXShaderConstHandle* mSplashShaderModelViewSC;
198   GFXShaderConstHandle* mSplashShaderFadeStartEndSC;
199   GFXShaderConstHandle* mSplashShaderCameraPosSC;
200   GFXShaderConstHandle* mSplashShaderAmbientSC;
201
202   struct
203   {
204      bool valid;
205      U32 startTime;
206      U32 totalTime;
207      F32 startPct;
208      F32 endPct;
209
210   } mStormData;
211   
212   struct
213   {
214      bool valid;
215      U32 startTime;
216      U32 totalTime;
217      F32 startMax;
218      F32 startSpeed;
219      F32 endMax;
220      F32 endSpeed;
221
222   } mTurbulenceData;
223
224   //other functions...
225   void processTick(const Move*);
226   void interpolateTick(F32 delta);
227
228   VectorF getWindVelocity();
229   void fillDropList();                       ///< Adds/removes drops from the list to have the right # of drops
230   void killDropList();                       ///< Deletes the entire drop list
231   void initRenderObjects();                  ///< Re-inits the texture coord lookup tables
232   void initMaterials();                      ///< Re-inits the textures and shaders
233   void spawnDrop(Raindrop *drop);            ///< Fills drop info with random velocity, x/y positions, and mass
234   void spawnNewDrop(Raindrop *drop);         ///< Same as spawnDrop except also does z position
235   
236   void findDropCutoff(Raindrop *drop, const Box3F &box, const VectorF &windVel);   ///< Casts a ray to see if/when a drop will collide
237   void wrapDrop(Raindrop *drop, const Box3F &box, const U32 currTime, const VectorF &windVel);         ///< Wraps a drop within the specified box
238   
239   void createSplash(Raindrop *drop);        ///< Adds a drop to the splash list
240   void destroySplash(Raindrop *drop);       ///< Removes a drop from the splash list
241
242   GFXPrimitiveBufferHandle mRainIB;
243   GFXVertexBufferHandle<GFXVertexPCT> mRainVB;
244
245   bool onAdd();
246   void onRemove();
247
248   // Rendering
249   void prepRenderImage( SceneRenderState* state );
250   void renderObject(ObjectRenderInst *ri, SceneRenderState *state, BaseMatInstance* );
251
252   void setTransform(const MatrixF &mat);
253
254  public:
255
256   Precipitation();
257   ~Precipitation();
258   void inspectPostApply();
259
260   enum
261   {
262      DataMask       = Parent::NextFreeMask << 0,
263      PercentageMask = Parent::NextFreeMask << 1,
264      StormMask      = Parent::NextFreeMask << 2,
265      TransformMask  = Parent::NextFreeMask << 3,
266      TurbulenceMask = Parent::NextFreeMask << 4,
267      NextFreeMask   = Parent::NextFreeMask << 5
268   };
269
270   bool onNewDataBlock( GameBaseData *dptr, bool reload );
271   DECLARE_CONOBJECT(Precipitation);
272   static void initPersistFields();
273   
274   U32  packUpdate(NetConnection*, U32 mask, BitStream* stream);
275   void unpackUpdate(NetConnection*, BitStream* stream);
276
277   void setPercentage(F32 pct);
278   void modifyStorm(F32 pct, U32 ms);
279
280   /// This is used to smoothly change the turbulence
281   /// over a desired time period.  Setting ms to zero
282   /// will cause the change to be instantaneous.  Setting
283   /// max zero will disable turbulence.
284   void setTurbulence(F32 max, F32 speed, U32 ms);
285};
286
287#endif // PRECIPITATION_H_
288
289