precipitation.h 12 KB

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