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