precipitation.cpp 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878
  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. #include "platform/platform.h"
  23. #include "T3D/fx/precipitation.h"
  24. #include "math/mathIO.h"
  25. #include "console/consoleTypes.h"
  26. #include "console/typeValidators.h"
  27. #include "scene/sceneManager.h"
  28. #include "scene/sceneRenderState.h"
  29. #include "lighting/lightInfo.h"
  30. #include "lighting/lightManager.h"
  31. #include "materials/shaderData.h"
  32. #include "T3D/gameBase/gameConnection.h"
  33. #include "T3D/player.h"
  34. #include "core/stream/bitStream.h"
  35. #include "platform/profiler.h"
  36. #include "renderInstance/renderPassManager.h"
  37. #include "sfx/sfxSystem.h"
  38. #include "sfx/sfxTrack.h"
  39. #include "sfx/sfxSource.h"
  40. #include "sfx/sfxTypes.h"
  41. #include "console/engineAPI.h"
  42. #include "particleEmitter.h"
  43. static const U32 dropHitMask =
  44. TerrainObjectType |
  45. WaterObjectType |
  46. StaticShapeObjectType;
  47. IMPLEMENT_CO_NETOBJECT_V1(Precipitation);
  48. IMPLEMENT_CO_DATABLOCK_V1(PrecipitationData);
  49. ConsoleDocClass( Precipitation,
  50. "@brief Defines a precipitation based storm (rain, snow, etc).\n\n"
  51. "The Precipitation effect works by creating many 'drops' within a fixed size "
  52. "box. This box can be configured to move around with the camera (to simulate "
  53. "level-wide precipitation), or to remain in a fixed position (to simulate "
  54. "localized precipitation). When #followCam is true, the box containing the "
  55. "droplets can be thought of as centered on the camera then pushed slightly "
  56. "forward in the direction the camera is facing so most of the box is in "
  57. "front of the camera (allowing more drops to be visible on screen at once).\n\n"
  58. "The effect can also be configured to create a small 'splash' whenever a drop "
  59. "hits another world object.\n\n"
  60. "@tsexample\n"
  61. "// The following is added to a level file (.mis) by the World Editor\n"
  62. "new Precipitation( TheRain )\n"
  63. "{\n"
  64. " dropSize = \"0.5\";\n"
  65. " splashSize = \"0.5\";\n"
  66. " splashMS = \"250\";\n"
  67. " animateSplashes = \"1\";\n"
  68. " dropAnimateMS = \"0\";\n"
  69. " fadeDist = \"0\";\n"
  70. " fadeDistEnd = \"0\";\n"
  71. " useTrueBillboards = \"0\";\n"
  72. " useLighting = \"0\";\n"
  73. " glowIntensity = \"0 0 0 0\";\n"
  74. " reflect = \"0\";\n"
  75. " rotateWithCamVel = \"1\";\n"
  76. " doCollision = \"1\";\n"
  77. " hitPlayers = \"0\";\n"
  78. " hitVehicles = \"0\";\n"
  79. " followCam = \"1\";\n"
  80. " useWind = \"0\";\n"
  81. " minSpeed = \"1.5\";\n"
  82. " maxSpeed = \"2\";\n"
  83. " minMass = \"0.75\";\n"
  84. " maxMass = \"0.85\";\n"
  85. " useTurbulence = \"0\";\n"
  86. " maxTurbulence = \"0.1\";\n"
  87. " turbulenceSpeed = \"0.2\";\n"
  88. " numDrops = \"1024\";\n"
  89. " boxWidth = \"200\";\n"
  90. " boxHeight = \"100\";\n"
  91. " dataBlock = \"HeavyRain\";\n"
  92. "};\n"
  93. "@endtsexample\n"
  94. "@ingroup FX\n"
  95. "@ingroup Atmosphere\n"
  96. "@see PrecipitationData\n"
  97. );
  98. ConsoleDocClass( PrecipitationData,
  99. "@brief Defines the droplets used in a storm (raindrops, snowflakes, etc).\n\n"
  100. "@tsexample\n"
  101. "datablock PrecipitationData( HeavyRain )\n"
  102. "{\n"
  103. " soundProfile = \"HeavyRainSound\";\n"
  104. " dropTexture = \"art/environment/precipitation/rain\";\n"
  105. " splashTexture = \"art/environment/precipitation/water_splash\";\n"
  106. " dropsPerSide = 4;\n"
  107. " splashesPerSide = 2;\n"
  108. "};\n"
  109. "@endtsexample\n"
  110. "@ingroup FX\n"
  111. "@ingroup Atmosphere\n"
  112. "@see Precipitation\n"
  113. );
  114. //----------------------------------------------------------
  115. // PrecipitationData
  116. //----------------------------------------------------------
  117. PrecipitationData::PrecipitationData()
  118. {
  119. soundProfile = NULL;
  120. INIT_IMAGEASSET(Drop);
  121. mDropShaderName = StringTable->EmptyString();
  122. INIT_IMAGEASSET(Splash);
  123. mSplashShaderName = StringTable->EmptyString();
  124. mDropsPerSide = 4;
  125. mSplashesPerSide = 2;
  126. }
  127. void PrecipitationData::initPersistFields()
  128. {
  129. addField( "soundProfile", TYPEID< SFXTrack >(), Offset(soundProfile, PrecipitationData),
  130. "Looping SFXProfile effect to play while Precipitation is active." );
  131. addProtectedField( "dropTexture", TypeFilename, Offset(mDropName, PrecipitationData), &_setDropData, &defaultProtectedGetFn,
  132. "@brief Texture filename for drop particles.\n\n"
  133. "The drop texture can contain several different drop sub-textures "
  134. "arranged in a grid. There must be the same number of rows as columns. A "
  135. "random frame will be chosen for each drop.", AbstractClassRep::FIELD_HideInInspectors );
  136. INITPERSISTFIELD_IMAGEASSET(Drop, PrecipitationData, "@brief Texture for drop particles.\n\n"
  137. "The drop texture can contain several different drop sub-textures "
  138. "arranged in a grid. There must be the same number of rows as columns. A "
  139. "random frame will be chosen for each drop.");
  140. addField( "dropShader", TypeString, Offset(mDropShaderName, PrecipitationData),
  141. "The name of the shader used for raindrops." );
  142. addProtectedField("splashTexture", TypeFilename, Offset(mSplashName, PrecipitationData), &_setSplashData, &defaultProtectedGetFn,
  143. "@brief Texture filename for splash particles.\n\n"
  144. "The splash texture can contain several different splash sub-textures "
  145. "arranged in a grid. There must be the same number of rows as columns. A "
  146. "random frame will be chosen for each splash.", AbstractClassRep::FIELD_HideInInspectors);
  147. INITPERSISTFIELD_IMAGEASSET(Splash, PrecipitationData, "@brief Texture for splash particles.\n\n"
  148. "The splash texture can contain several different splash sub-textures "
  149. "arranged in a grid. There must be the same number of rows as columns. A "
  150. "random frame will be chosen for each splash.");
  151. addField( "splashShader", TypeString, Offset(mSplashShaderName, PrecipitationData),
  152. "The name of the shader used for splashes." );
  153. addField( "dropsPerSide", TypeS32, Offset(mDropsPerSide, PrecipitationData),
  154. "@brief How many rows and columns are in the raindrop texture.\n\n"
  155. "For example, if the texture has 16 raindrops arranged in a grid, this "
  156. "field should be set to 4." );
  157. addField( "splashesPerSide", TypeS32, Offset(mSplashesPerSide, PrecipitationData),
  158. "@brief How many rows and columns are in the splash texture.\n\n"
  159. "For example, if the texture has 9 splashes arranged in a grid, this "
  160. "field should be set to 3." );
  161. Parent::initPersistFields();
  162. }
  163. bool PrecipitationData::preload( bool server, String &errorStr )
  164. {
  165. if( Parent::preload( server, errorStr) == false)
  166. return false;
  167. if( !server && !sfxResolve( &soundProfile, errorStr ) )
  168. return false;
  169. return true;
  170. }
  171. void PrecipitationData::packData(BitStream* stream)
  172. {
  173. Parent::packData(stream);
  174. sfxWrite( stream, soundProfile );
  175. PACKDATA_IMAGEASSET(Drop);
  176. stream->writeString(mDropShaderName);
  177. PACKDATA_IMAGEASSET(Splash);
  178. stream->writeString(mSplashShaderName);
  179. stream->write(mDropsPerSide);
  180. stream->write(mSplashesPerSide);
  181. }
  182. void PrecipitationData::unpackData(BitStream* stream)
  183. {
  184. Parent::unpackData(stream);
  185. sfxRead( stream, &soundProfile );
  186. UNPACKDATA_IMAGEASSET(Drop);
  187. mDropShaderName = stream->readSTString();
  188. UNPACKDATA_IMAGEASSET(Splash);
  189. mSplashShaderName = stream->readSTString();
  190. stream->read(&mDropsPerSide);
  191. stream->read(&mSplashesPerSide);
  192. }
  193. //----------------------------------------------------------
  194. // Precipitation!
  195. //----------------------------------------------------------
  196. Precipitation::Precipitation()
  197. {
  198. mTypeMask |= ProjectileObjectType;
  199. mDataBlock = NULL;
  200. mTexCoords = NULL;
  201. mSplashCoords = NULL;
  202. mDropShader = NULL;
  203. mDropHandle = NULL;
  204. mSplashShader = NULL;
  205. mSplashHandle = NULL;
  206. mDropHead = NULL;
  207. mSplashHead = NULL;
  208. mNumDrops = 1024;
  209. mPercentage = 1.0;
  210. mMinSpeed = 1.5;
  211. mMaxSpeed = 2.0;
  212. mFollowCam = true;
  213. mLastRenderFrame = 0;
  214. mDropHitMask = 0;
  215. mDropSize = 0.5;
  216. mSplashSize = 0.5;
  217. mUseTrueBillboards = false;
  218. mSplashMS = 250;
  219. mAnimateSplashes = true;
  220. mDropAnimateMS = 0;
  221. mUseLighting = false;
  222. mGlowIntensity = LinearColorF( 0,0,0,0 );
  223. mReflect = false;
  224. mUseWind = false;
  225. mBoxWidth = 200;
  226. mBoxHeight = 100;
  227. mFadeDistance = 0;
  228. mFadeDistanceEnd = 0;
  229. mMinMass = 0.75f;
  230. mMaxMass = 0.85f;
  231. mMaxTurbulence = 0.1f;
  232. mTurbulenceSpeed = 0.2f;
  233. mUseTurbulence = false;
  234. mRotateWithCamVel = true;
  235. mDoCollision = true;
  236. mDropHitPlayers = false;
  237. mDropHitVehicles = false;
  238. mStormData.valid = false;
  239. mStormData.startPct = 0;
  240. mStormData.endPct = 0;
  241. mStormData.startTime = 0;
  242. mStormData.totalTime = 0;
  243. mTurbulenceData.valid = false;
  244. mTurbulenceData.startTime = 0;
  245. mTurbulenceData.totalTime = 0;
  246. mTurbulenceData.startMax = 0;
  247. mTurbulenceData.startSpeed = 0;
  248. mTurbulenceData.endMax = 0;
  249. mTurbulenceData.endSpeed = 0;
  250. mAmbientSound = NULL;
  251. mDropShaderModelViewSC = NULL;
  252. mDropShaderFadeStartEndSC = NULL;
  253. mDropShaderCameraPosSC = NULL;
  254. mDropShaderAmbientSC = NULL;
  255. mSplashShaderModelViewSC = NULL;
  256. mSplashShaderFadeStartEndSC = NULL;
  257. mSplashShaderCameraPosSC = NULL;
  258. mSplashShaderAmbientSC = NULL;
  259. mMaxVBDrops = 5000;
  260. }
  261. Precipitation::~Precipitation()
  262. {
  263. SAFE_DELETE_ARRAY(mTexCoords);
  264. SAFE_DELETE_ARRAY(mSplashCoords);
  265. }
  266. void Precipitation::inspectPostApply()
  267. {
  268. if (mFollowCam)
  269. {
  270. setGlobalBounds();
  271. }
  272. else
  273. {
  274. mObjBox.minExtents = -Point3F(mBoxWidth/2, mBoxWidth/2, mBoxHeight/2);
  275. mObjBox.maxExtents = Point3F(mBoxWidth/2, mBoxWidth/2, mBoxHeight/2);
  276. }
  277. resetWorldBox();
  278. setMaskBits(DataMask);
  279. }
  280. void Precipitation::setTransform(const MatrixF & mat)
  281. {
  282. Parent::setTransform(mat);
  283. setMaskBits(TransformMask);
  284. }
  285. //--------------------------------------------------------------------------
  286. // Console stuff...
  287. //--------------------------------------------------------------------------
  288. IRangeValidator ValidNumDropsRange(1, 100000);
  289. void Precipitation::initPersistFields()
  290. {
  291. addGroup("Precipitation");
  292. addFieldV( "numDrops", TypeS32, Offset(mNumDrops, Precipitation), &ValidNumDropsRange,
  293. "@brief Maximum number of drops allowed to exist in the precipitation "
  294. "box at any one time.\n\n"
  295. "The actual number of drops in the effect depends on the current "
  296. "percentage, which can change over time using modifyStorm()." );
  297. addField( "boxWidth", TypeF32, Offset(mBoxWidth, Precipitation),
  298. "Width and depth (horizontal dimensions) of the precipitation box." );
  299. addField( "boxHeight", TypeF32, Offset(mBoxHeight, Precipitation),
  300. "Height (vertical dimension) of the precipitation box." );
  301. endGroup("Precipitation");
  302. addGroup("Rendering");
  303. addField( "dropSize", TypeF32, Offset(mDropSize, Precipitation),
  304. "Size of each drop of precipitation. This will scale the texture." );
  305. addField( "splashSize", TypeF32, Offset(mSplashSize, Precipitation),
  306. "Size of each splash animation when a drop collides with another surface." );
  307. addField( "splashMS", TypeS32, Offset(mSplashMS, Precipitation),
  308. "Lifetime of splashes in milliseconds." );
  309. addField( "animateSplashes", TypeBool, Offset(mAnimateSplashes, Precipitation),
  310. "Set to true to enable splash animations when drops collide with other surfaces." );
  311. addField( "dropAnimateMS", TypeS32, Offset(mDropAnimateMS, Precipitation),
  312. "@brief Length (in milliseconds) to display each drop frame.\n\n"
  313. "If #dropAnimateMS <= 0, drops select a single random frame at creation "
  314. "that does not change throughout the drop's lifetime. If #dropAnimateMS "
  315. "> 0, each drop cycles through the the available frames in the drop "
  316. "texture at the given rate." );
  317. addField( "fadeDist", TypeF32, Offset(mFadeDistance, Precipitation),
  318. "The distance at which drops begin to fade out." );
  319. addField( "fadeDistEnd", TypeF32, Offset(mFadeDistanceEnd, Precipitation),
  320. "The distance at which drops are completely faded out." );
  321. addField( "useTrueBillboards", TypeBool, Offset(mUseTrueBillboards, Precipitation),
  322. "Set to true to make drops true (non axis-aligned) billboards." );
  323. addField( "useLighting", TypeBool, Offset(mUseLighting, Precipitation),
  324. "Set to true to enable shading of the drops and splashes by the sun color." );
  325. addField( "glowIntensity", TypeColorF, Offset(mGlowIntensity, Precipitation),
  326. "Set to 0 to disable the glow or or use it to control the intensity of each channel." );
  327. addField( "reflect", TypeBool, Offset(mReflect, Precipitation),
  328. "@brief This enables precipitation rendering during reflection passes.\n\n"
  329. "@note This is expensive." );
  330. addField( "rotateWithCamVel", TypeBool, Offset(mRotateWithCamVel, Precipitation),
  331. "Set to true to include the camera velocity when calculating drop "
  332. "rotation speed." );
  333. endGroup("Rendering");
  334. addGroup("Collision");
  335. addField( "doCollision", TypeBool, Offset(mDoCollision, Precipitation),
  336. "@brief Allow drops to collide with world objects.\n\n"
  337. "If #animateSplashes is true, drops that collide with another object "
  338. "will produce a simple splash animation.\n"
  339. "@note This can be expensive as each drop will perform a raycast when "
  340. "it is created to determine where it will hit." );
  341. addField( "hitPlayers", TypeBool, Offset(mDropHitPlayers, Precipitation),
  342. "Allow drops to collide with Player objects; only valid if #doCollision is true." );
  343. addField( "hitVehicles", TypeBool, Offset(mDropHitVehicles, Precipitation),
  344. "Allow drops to collide with Vehicle objects; only valid if #doCollision is true." );
  345. endGroup("Collision");
  346. addGroup("Movement");
  347. addField( "followCam", TypeBool, Offset(mFollowCam, Precipitation),
  348. "@brief Controls whether the Precipitation system follows the camera "
  349. "or remains where it is first placed in the scene.\n\n"
  350. "Set to true to make it seem like it is raining everywhere in the "
  351. "level (ie. the Player will always be in the rain). Set to false "
  352. "to have a single area affected by rain (ie. the Player can move in "
  353. "and out of the rainy area)." );
  354. addField( "useWind", TypeBool, Offset(mUseWind, Precipitation),
  355. "Controls whether drops are affected by wind.\n"
  356. "@see ForestWindEmitter" );
  357. addField( "minSpeed", TypeF32, Offset(mMinSpeed, Precipitation),
  358. "@brief Minimum speed at which a drop will fall.\n\n"
  359. "On creation, the drop will be assigned a random speed between #minSpeed "
  360. "and #maxSpeed." );
  361. addField( "maxSpeed", TypeF32, Offset(mMaxSpeed, Precipitation),
  362. "@brief Maximum speed at which a drop will fall.\n\n"
  363. "On creation, the drop will be assigned a random speed between #minSpeed "
  364. "and #maxSpeed." );
  365. addField( "minMass", TypeF32, Offset(mMinMass, Precipitation),
  366. "@brief Minimum mass of a drop.\n\n"
  367. "Drop mass determines how strongly the drop is affected by wind and "
  368. "turbulence. On creation, the drop will be assigned a random speed "
  369. "between #minMass and #minMass." );
  370. addField( "maxMass", TypeF32, Offset(mMaxMass, Precipitation),
  371. "@brief Maximum mass of a drop.\n\n"
  372. "Drop mass determines how strongly the drop is affected by wind and "
  373. "turbulence. On creation, the drop will be assigned a random speed "
  374. "between #minMass and #minMass." );
  375. endGroup("Movement");
  376. addGroup("Turbulence");
  377. addField( "useTurbulence", TypeBool, Offset(mUseTurbulence, Precipitation),
  378. "Check to enable turbulence. This causes precipitation drops to spiral "
  379. "while falling." );
  380. addField( "maxTurbulence", TypeF32, Offset(mMaxTurbulence, Precipitation),
  381. "Radius at which precipitation drops spiral when turbulence is enabled." );
  382. addField( "turbulenceSpeed", TypeF32, Offset(mTurbulenceSpeed, Precipitation),
  383. "Speed at which precipitation drops spiral when turbulence is enabled." );
  384. endGroup("Turbulence");
  385. Parent::initPersistFields();
  386. }
  387. //-----------------------------------
  388. // Console methods...
  389. DefineEngineMethod(Precipitation, setPercentage, void, (F32 percentage), (1.0f),
  390. "Sets the maximum number of drops in the effect, as a percentage of #numDrops.\n"
  391. "The change occurs instantly (use modifyStorm() to change the number of drops "
  392. "over a period of time.\n"
  393. "@param percentage New maximum number of drops value (as a percentage of "
  394. "#numDrops). Valid range is 0-1.\n"
  395. "@tsexample\n"
  396. "%percentage = 0.5; // The percentage, from 0 to 1, of the maximum drops to display\n"
  397. "%precipitation.setPercentage( %percentage );\n"
  398. "@endtsexample\n"
  399. "@see modifyStorm\n" )
  400. {
  401. object->setPercentage(percentage);
  402. }
  403. DefineEngineMethod(Precipitation, modifyStorm, void, (F32 percentage, F32 seconds), (1.0f, 5.0f),
  404. "Smoothly change the maximum number of drops in the effect (from current "
  405. "value to #numDrops * @a percentage).\n"
  406. "This method can be used to simulate a storm building or fading in intensity "
  407. "as the number of drops in the Precipitation box changes.\n"
  408. "@param percentage New maximum number of drops value (as a percentage of "
  409. "#numDrops). Valid range is 0-1.\n"
  410. "@param seconds Length of time (in seconds) over which to increase the drops "
  411. "percentage value. Set to 0 to change instantly.\n"
  412. "@tsexample\n"
  413. "%percentage = 0.5; // The percentage, from 0 to 1, of the maximum drops to display\n"
  414. "%seconds = 5.0; // The length of time over which to make the change.\n"
  415. "%precipitation.modifyStorm( %percentage, %seconds );\n"
  416. "@endtsexample\n" )
  417. {
  418. object->modifyStorm(percentage, S32(seconds * 1000.0f));
  419. }
  420. DefineEngineMethod(Precipitation, setTurbulence, void, (F32 max, F32 speed, F32 seconds), (1.0f, 5.0f, 5.0f),
  421. "Smoothly change the turbulence parameters over a period of time.\n"
  422. "@param max New #maxTurbulence value. Set to 0 to disable turbulence.\n"
  423. "@param speed New #turbulenceSpeed value.\n"
  424. "@param seconds Length of time (in seconds) over which to interpolate the "
  425. "turbulence settings. Set to 0 to change instantly.\n"
  426. "@tsexample\n"
  427. "%turbulence = 0.5; // Set the new turbulence value. Set to 0 to disable turbulence.\n"
  428. "%speed = 5.0; // The new speed of the turbulance effect.\n"
  429. "%seconds = 5.0; // The length of time over which to make the change.\n"
  430. "%precipitation.setTurbulence( %turbulence, %speed, %seconds );\n"
  431. "@endtsexample\n" )
  432. {
  433. object->setTurbulence( max, speed, S32(seconds * 1000.0f));
  434. }
  435. //--------------------------------------------------------------------------
  436. // Backend
  437. //--------------------------------------------------------------------------
  438. bool Precipitation::onAdd()
  439. {
  440. if(!Parent::onAdd())
  441. return false;
  442. if (mFollowCam)
  443. {
  444. setGlobalBounds();
  445. }
  446. else
  447. {
  448. mObjBox.minExtents = -Point3F(mBoxWidth/2, mBoxWidth/2, mBoxHeight/2);
  449. mObjBox.maxExtents = Point3F(mBoxWidth/2, mBoxWidth/2, mBoxHeight/2);
  450. }
  451. resetWorldBox();
  452. if (isClientObject())
  453. {
  454. fillDropList();
  455. initRenderObjects();
  456. initMaterials();
  457. }
  458. addToScene();
  459. return true;
  460. }
  461. void Precipitation::onRemove()
  462. {
  463. removeFromScene();
  464. Parent::onRemove();
  465. SFX_DELETE( mAmbientSound );
  466. if (isClientObject())
  467. killDropList();
  468. }
  469. bool Precipitation::onNewDataBlock( GameBaseData *dptr, bool reload )
  470. {
  471. mDataBlock = dynamic_cast<PrecipitationData*>( dptr );
  472. if ( !mDataBlock || !Parent::onNewDataBlock( dptr, reload ) )
  473. return false;
  474. if (isClientObject())
  475. {
  476. SFX_DELETE( mAmbientSound );
  477. if ( mDataBlock->soundProfile )
  478. {
  479. mAmbientSound = SFX->createSource( mDataBlock->soundProfile, &getTransform() );
  480. if ( mAmbientSound )
  481. mAmbientSound->play();
  482. }
  483. initRenderObjects();
  484. initMaterials();
  485. }
  486. scriptOnNewDataBlock();
  487. return true;
  488. }
  489. void Precipitation::initMaterials()
  490. {
  491. AssertFatal(isClientObject(), "Precipitation is setting materials on the server - BAD!");
  492. if(!mDataBlock)
  493. return;
  494. PrecipitationData *pd = (PrecipitationData*)mDataBlock;
  495. mDropHandle = NULL;
  496. mSplashHandle = NULL;
  497. mDropShader = NULL;
  498. mSplashShader = NULL;
  499. if(pd->mDrop.isNull())
  500. Con::warnf("Precipitation::initMaterials - failed to locate texture '%s'!", pd->getDrop());
  501. if ( dStrlen(pd->mDropShaderName) > 0 )
  502. {
  503. ShaderData *shaderData;
  504. if ( Sim::findObject( pd->mDropShaderName, shaderData ) )
  505. mDropShader = shaderData->getShader();
  506. if( !mDropShader )
  507. Con::warnf( "Precipitation::initMaterials - could not find shader '%s'!", pd->mDropShaderName );
  508. else
  509. {
  510. mDropShaderConsts = mDropShader->allocConstBuffer();
  511. mDropShaderModelViewSC = mDropShader->getShaderConstHandle("$modelView");
  512. mDropShaderFadeStartEndSC = mDropShader->getShaderConstHandle("$fadeStartEnd");
  513. mDropShaderCameraPosSC = mDropShader->getShaderConstHandle("$cameraPos");
  514. mDropShaderAmbientSC = mDropShader->getShaderConstHandle("$ambient");
  515. }
  516. }
  517. if (pd->mSplash.isNull())
  518. Con::warnf("Precipitation::initMaterials - failed to locate texture '%s'!", pd->getSplash());
  519. if ( dStrlen(pd->mSplashShaderName) > 0 )
  520. {
  521. ShaderData *shaderData;
  522. if ( Sim::findObject( pd->mSplashShaderName, shaderData ) )
  523. mSplashShader = shaderData->getShader();
  524. if( !mSplashShader )
  525. Con::warnf( "Precipitation::initMaterials - could not find shader '%s'!", pd->mSplashShaderName );
  526. else
  527. {
  528. mSplashShaderConsts = mSplashShader->allocConstBuffer();
  529. mSplashShaderModelViewSC = mSplashShader->getShaderConstHandle("$modelView");
  530. mSplashShaderFadeStartEndSC = mSplashShader->getShaderConstHandle("$fadeStartEnd");
  531. mSplashShaderCameraPosSC = mSplashShader->getShaderConstHandle("$cameraPos");
  532. mSplashShaderAmbientSC = mSplashShader->getShaderConstHandle("$ambient");
  533. }
  534. }
  535. }
  536. U32 Precipitation::packUpdate(NetConnection* con, U32 mask, BitStream* stream)
  537. {
  538. Parent::packUpdate(con, mask, stream);
  539. if (stream->writeFlag( !mFollowCam && mask & TransformMask))
  540. stream->writeAffineTransform(mObjToWorld);
  541. if (stream->writeFlag(mask & DataMask))
  542. {
  543. stream->write(mDropSize);
  544. stream->write(mSplashSize);
  545. stream->write(mSplashMS);
  546. stream->write(mDropAnimateMS);
  547. stream->write(mNumDrops);
  548. stream->write(mMinSpeed);
  549. stream->write(mMaxSpeed);
  550. stream->write(mBoxWidth);
  551. stream->write(mBoxHeight);
  552. stream->write(mMinMass);
  553. stream->write(mMaxMass);
  554. stream->write(mMaxTurbulence);
  555. stream->write(mTurbulenceSpeed);
  556. stream->write(mFadeDistance);
  557. stream->write(mFadeDistanceEnd);
  558. stream->write(mGlowIntensity.red);
  559. stream->write(mGlowIntensity.green);
  560. stream->write(mGlowIntensity.blue);
  561. stream->write(mGlowIntensity.alpha);
  562. stream->writeFlag(mReflect);
  563. stream->writeFlag(mRotateWithCamVel);
  564. stream->writeFlag(mDoCollision);
  565. stream->writeFlag(mDropHitPlayers);
  566. stream->writeFlag(mDropHitVehicles);
  567. stream->writeFlag(mUseTrueBillboards);
  568. stream->writeFlag(mUseTurbulence);
  569. stream->writeFlag(mUseLighting);
  570. stream->writeFlag(mUseWind);
  571. stream->writeFlag(mFollowCam);
  572. stream->writeFlag(mAnimateSplashes);
  573. }
  574. if (stream->writeFlag(!(mask & DataMask) && (mask & TurbulenceMask)))
  575. {
  576. stream->write(mTurbulenceData.endMax);
  577. stream->write(mTurbulenceData.endSpeed);
  578. stream->write(mTurbulenceData.totalTime);
  579. }
  580. if (stream->writeFlag(mask & PercentageMask))
  581. {
  582. stream->write(mPercentage);
  583. }
  584. if (stream->writeFlag(!(mask & ~(DataMask | PercentageMask | StormMask)) && (mask & StormMask)))
  585. {
  586. stream->write(mStormData.endPct);
  587. stream->write(mStormData.totalTime);
  588. }
  589. return 0;
  590. }
  591. void Precipitation::unpackUpdate(NetConnection* con, BitStream* stream)
  592. {
  593. Parent::unpackUpdate(con, stream);
  594. if (stream->readFlag())
  595. {
  596. MatrixF mat;
  597. stream->readAffineTransform(&mat);
  598. Parent::setTransform(mat);
  599. }
  600. U32 oldDrops = U32(mNumDrops * mPercentage);
  601. if (stream->readFlag())
  602. {
  603. stream->read(&mDropSize);
  604. stream->read(&mSplashSize);
  605. stream->read(&mSplashMS);
  606. stream->read(&mDropAnimateMS);
  607. stream->read(&mNumDrops);
  608. stream->read(&mMinSpeed);
  609. stream->read(&mMaxSpeed);
  610. stream->read(&mBoxWidth);
  611. stream->read(&mBoxHeight);
  612. stream->read(&mMinMass);
  613. stream->read(&mMaxMass);
  614. stream->read(&mMaxTurbulence);
  615. stream->read(&mTurbulenceSpeed);
  616. stream->read(&mFadeDistance);
  617. stream->read(&mFadeDistanceEnd);
  618. stream->read(&mGlowIntensity.red);
  619. stream->read(&mGlowIntensity.green);
  620. stream->read(&mGlowIntensity.blue);
  621. stream->read(&mGlowIntensity.alpha);
  622. mReflect = stream->readFlag();
  623. mRotateWithCamVel = stream->readFlag();
  624. mDoCollision = stream->readFlag();
  625. mDropHitPlayers = stream->readFlag();
  626. mDropHitVehicles = stream->readFlag();
  627. mUseTrueBillboards = stream->readFlag();
  628. mUseTurbulence = stream->readFlag();
  629. mUseLighting = stream->readFlag();
  630. mUseWind = stream->readFlag();
  631. mFollowCam = stream->readFlag();
  632. mAnimateSplashes = stream->readFlag();
  633. mDropHitMask = dropHitMask |
  634. ( mDropHitPlayers ? PlayerObjectType : 0 ) |
  635. ( mDropHitVehicles ? VehicleObjectType : 0 );
  636. mTurbulenceData.valid = false;
  637. }
  638. if (stream->readFlag())
  639. {
  640. F32 max, speed;
  641. U32 ms;
  642. stream->read(&max);
  643. stream->read(&speed);
  644. stream->read(&ms);
  645. setTurbulence( max, speed, ms );
  646. }
  647. if (stream->readFlag())
  648. {
  649. F32 pct;
  650. stream->read(&pct);
  651. setPercentage(pct);
  652. }
  653. if (stream->readFlag())
  654. {
  655. F32 pct;
  656. U32 time;
  657. stream->read(&pct);
  658. stream->read(&time);
  659. modifyStorm(pct, time);
  660. }
  661. AssertFatal(isClientObject(), "Precipitation::unpackUpdate() should only be called on the client!");
  662. U32 newDrops = U32(mNumDrops * mPercentage);
  663. if (oldDrops != newDrops)
  664. {
  665. fillDropList();
  666. initRenderObjects();
  667. }
  668. if (mFollowCam)
  669. {
  670. setGlobalBounds();
  671. }
  672. else
  673. {
  674. mObjBox.minExtents = -Point3F(mBoxWidth/2, mBoxWidth/2, mBoxHeight/2);
  675. mObjBox.maxExtents = Point3F(mBoxWidth/2, mBoxWidth/2, mBoxHeight/2);
  676. }
  677. resetWorldBox();
  678. }
  679. //--------------------------------------------------------------------------
  680. // Support functions
  681. //--------------------------------------------------------------------------
  682. VectorF Precipitation::getWindVelocity()
  683. {
  684. // The WindManager happens to set global-wind velocity here, it is not just for particles.
  685. return mUseWind ? ParticleEmitter::mWindVelocity : Point3F::Zero;
  686. }
  687. void Precipitation::fillDropList()
  688. {
  689. AssertFatal(isClientObject(), "Precipitation is doing stuff on the server - BAD!");
  690. F32 density = Con::getFloatVariable("$pref::precipitationDensity", 1.0f);
  691. U32 newDropCount = (U32)(mNumDrops * mPercentage * density);
  692. U32 dropCount = 0;
  693. if (newDropCount == 0)
  694. killDropList();
  695. if (mDropHead)
  696. {
  697. Raindrop* curr = mDropHead;
  698. while (curr)
  699. {
  700. dropCount++;
  701. curr = curr->next;
  702. if (dropCount == newDropCount && curr)
  703. {
  704. //delete the remaining drops
  705. Raindrop* next = curr->next;
  706. curr->next = NULL;
  707. while (next)
  708. {
  709. Raindrop* last = next;
  710. next = next->next;
  711. last->next = NULL;
  712. destroySplash(last);
  713. delete last;
  714. }
  715. break;
  716. }
  717. }
  718. }
  719. if (dropCount < newDropCount)
  720. {
  721. //move to the end
  722. Raindrop* curr = mDropHead;
  723. if (curr)
  724. {
  725. while (curr->next)
  726. curr = curr->next;
  727. }
  728. else
  729. {
  730. mDropHead = curr = new Raindrop;
  731. spawnNewDrop(curr);
  732. dropCount++;
  733. }
  734. //and add onto it
  735. while (dropCount < newDropCount)
  736. {
  737. curr->next = new Raindrop;
  738. curr = curr->next;
  739. spawnNewDrop(curr);
  740. dropCount++;
  741. }
  742. }
  743. }
  744. void Precipitation::initRenderObjects()
  745. {
  746. AssertFatal(isClientObject(), "Precipitation is doing stuff on the server - BAD!");
  747. SAFE_DELETE_ARRAY(mTexCoords);
  748. SAFE_DELETE_ARRAY(mSplashCoords);
  749. if (!mDataBlock)
  750. return;
  751. mTexCoords = new Point2F[4*mDataBlock->mDropsPerSide*mDataBlock->mDropsPerSide];
  752. // Setup the texcoords for the drop texture.
  753. // The order of the coords when animating is...
  754. //
  755. // +---+---+---+
  756. // | 1 | 2 | 3 |
  757. // |---|---|---+
  758. // | 4 | 5 | 6 |
  759. // +---+---+---+
  760. // | 7 | etc...
  761. // +---+
  762. //
  763. U32 count = 0;
  764. for (U32 v = 0; v < mDataBlock->mDropsPerSide; v++)
  765. {
  766. F32 y1 = (F32) v / mDataBlock->mDropsPerSide;
  767. F32 y2 = (F32)(v+1) / mDataBlock->mDropsPerSide;
  768. for (U32 u = 0; u < mDataBlock->mDropsPerSide; u++)
  769. {
  770. F32 x1 = (F32) u / mDataBlock->mDropsPerSide;
  771. F32 x2 = (F32)(u+1) / mDataBlock->mDropsPerSide;
  772. mTexCoords[4*count+0].x = x1;
  773. mTexCoords[4*count+0].y = y1;
  774. mTexCoords[4*count+1].x = x2;
  775. mTexCoords[4*count+1].y = y1;
  776. mTexCoords[4*count+2].x = x2;
  777. mTexCoords[4*count+2].y = y2;
  778. mTexCoords[4*count+3].x = x1;
  779. mTexCoords[4*count+3].y = y2;
  780. count++;
  781. }
  782. }
  783. count = 0;
  784. mSplashCoords = new Point2F[4*mDataBlock->mSplashesPerSide*mDataBlock->mSplashesPerSide];
  785. for (U32 v = 0; v < mDataBlock->mSplashesPerSide; v++)
  786. {
  787. F32 y1 = (F32) v / mDataBlock->mSplashesPerSide;
  788. F32 y2 = (F32)(v+1) / mDataBlock->mSplashesPerSide;
  789. for (U32 u = 0; u < mDataBlock->mSplashesPerSide; u++)
  790. {
  791. F32 x1 = (F32) u / mDataBlock->mSplashesPerSide;
  792. F32 x2 = (F32)(u+1) / mDataBlock->mSplashesPerSide;
  793. mSplashCoords[4*count+0].x = x1;
  794. mSplashCoords[4*count+0].y = y1;
  795. mSplashCoords[4*count+1].x = x2;
  796. mSplashCoords[4*count+1].y = y1;
  797. mSplashCoords[4*count+2].x = x2;
  798. mSplashCoords[4*count+2].y = y2;
  799. mSplashCoords[4*count+3].x = x1;
  800. mSplashCoords[4*count+3].y = y2;
  801. count++;
  802. }
  803. }
  804. // Cap the number of precipitation drops so that we don't blow out the max verts
  805. mMaxVBDrops = getMin( (U32)mNumDrops, ( GFX->getMaxDynamicVerts() / 4 ) - 1 );
  806. // If we have no drops then skip allocating anything!
  807. if ( mMaxVBDrops == 0 )
  808. return;
  809. // Create a volitile vertex buffer which
  810. // we'll lock and fill every frame.
  811. mRainVB.set(GFX, mMaxVBDrops * 4, GFXBufferTypeDynamic);
  812. // Init the index buffer for rendering the
  813. // entire or a partially filled vb.
  814. mRainIB.set(GFX, mMaxVBDrops * 6, 0, GFXBufferTypeStatic);
  815. U16 *idxBuff;
  816. mRainIB.lock(&idxBuff, NULL, NULL, NULL);
  817. for( U32 i=0; i < mMaxVBDrops; i++ )
  818. {
  819. //
  820. // The vertex pattern in the VB for each
  821. // particle is as follows...
  822. //
  823. // 0----1
  824. // |\ |
  825. // | \ |
  826. // | \ |
  827. // | \|
  828. // 3----2
  829. //
  830. // We setup the index order below to ensure
  831. // sequential, cache friendly, access.
  832. //
  833. U32 offset = i * 4;
  834. idxBuff[i*6+0] = 0 + offset;
  835. idxBuff[i*6+1] = 1 + offset;
  836. idxBuff[i*6+2] = 2 + offset;
  837. idxBuff[i*6+3] = 2 + offset;
  838. idxBuff[i*6+4] = 3 + offset;
  839. idxBuff[i*6+5] = 0 + offset;
  840. }
  841. mRainIB.unlock();
  842. }
  843. void Precipitation::killDropList()
  844. {
  845. AssertFatal(isClientObject(), "Precipitation is doing stuff on the server - BAD!");
  846. Raindrop* curr = mDropHead;
  847. while (curr)
  848. {
  849. Raindrop* next = curr->next;
  850. delete curr;
  851. curr = next;
  852. }
  853. mDropHead = NULL;
  854. mSplashHead = NULL;
  855. }
  856. void Precipitation::spawnDrop(Raindrop *drop)
  857. {
  858. PROFILE_START(PrecipSpawnDrop);
  859. AssertFatal(isClientObject(), "Precipitation is doing stuff on the server - BAD!");
  860. drop->velocity = Platform::getRandom() * (mMaxSpeed - mMinSpeed) + mMinSpeed;
  861. drop->position.x = Platform::getRandom() * mBoxWidth;
  862. drop->position.y = Platform::getRandom() * mBoxWidth;
  863. // The start time should be randomized so that
  864. // all the drops are not animating at the same time.
  865. drop->animStartTime = (SimTime)(Platform::getVirtualMilliseconds() * Platform::getRandom());
  866. if (mDropAnimateMS <= 0 && mDataBlock)
  867. drop->texCoordIndex = (U32)(Platform::getRandom() * ((F32)mDataBlock->mDropsPerSide*mDataBlock->mDropsPerSide - 0.5));
  868. drop->valid = true;
  869. drop->time = Platform::getRandom() * M_2PI;
  870. drop->mass = Platform::getRandom() * (mMaxMass - mMinMass) + mMinMass;
  871. PROFILE_END();
  872. }
  873. void Precipitation::spawnNewDrop(Raindrop *drop)
  874. {
  875. AssertFatal(isClientObject(), "Precipitation is doing stuff on the server - BAD!");
  876. spawnDrop(drop);
  877. drop->position.z = Platform::getRandom() * mBoxHeight - (mBoxHeight / 2);
  878. }
  879. void Precipitation::wrapDrop(Raindrop *drop, const Box3F &box, const U32 currTime, const VectorF &windVel)
  880. {
  881. //could probably be slightly optimized to get rid of the while loops
  882. if (drop->position.z < box.minExtents.z)
  883. {
  884. spawnDrop(drop);
  885. drop->position.x += box.minExtents.x;
  886. drop->position.y += box.minExtents.y;
  887. while (drop->position.z < box.minExtents.z)
  888. drop->position.z += mBoxHeight;
  889. findDropCutoff(drop, box, windVel);
  890. }
  891. else if (drop->position.z > box.maxExtents.z)
  892. {
  893. while (drop->position.z > box.maxExtents.z)
  894. drop->position.z -= mBoxHeight;
  895. findDropCutoff(drop, box, windVel);
  896. }
  897. else if (drop->position.x < box.minExtents.x)
  898. {
  899. while (drop->position.x < box.minExtents.x)
  900. drop->position.x += mBoxWidth;
  901. findDropCutoff(drop, box, windVel);
  902. }
  903. else if (drop->position.x > box.maxExtents.x)
  904. {
  905. while (drop->position.x > box.maxExtents.x)
  906. drop->position.x -= mBoxWidth;
  907. findDropCutoff(drop, box, windVel);
  908. }
  909. else if (drop->position.y < box.minExtents.y)
  910. {
  911. while (drop->position.y < box.minExtents.y)
  912. drop->position.y += mBoxWidth;
  913. findDropCutoff(drop, box, windVel);
  914. }
  915. else if (drop->position.y > box.maxExtents.y)
  916. {
  917. while (drop->position.y > box.maxExtents.y)
  918. drop->position.y -= mBoxWidth;
  919. findDropCutoff(drop, box, windVel);
  920. }
  921. }
  922. void Precipitation::findDropCutoff(Raindrop *drop, const Box3F &box, const VectorF &windVel)
  923. {
  924. PROFILE_START(PrecipFindDropCutoff);
  925. AssertFatal(isClientObject(), "Precipitation is doing stuff on the server - BAD!");
  926. if (mDoCollision)
  927. {
  928. VectorF velocity = windVel / drop->mass - VectorF(0, 0, drop->velocity);
  929. velocity.normalize();
  930. Point3F end = drop->position + 100 * velocity;
  931. Point3F start = drop->position - (mFollowCam ? 500.0f : 0.0f) * velocity;
  932. if (!mFollowCam)
  933. {
  934. mObjToWorld.mulP(start);
  935. mObjToWorld.mulP(end);
  936. }
  937. // Look for a collision... make sure we don't
  938. // collide with backfaces.
  939. RayInfo rInfo;
  940. if (getContainer()->castRay(start, end, mDropHitMask, &rInfo))
  941. {
  942. // TODO: Add check to filter out hits on backfaces.
  943. if (!mFollowCam)
  944. mWorldToObj.mulP(rInfo.point);
  945. drop->hitPos = rInfo.point;
  946. drop->hitType = rInfo.object->getTypeMask();
  947. }
  948. else
  949. drop->hitPos = Point3F(0,0,-1000);
  950. drop->valid = drop->position.z > drop->hitPos.z;
  951. }
  952. else
  953. {
  954. drop->hitPos = Point3F(0,0,-1000);
  955. drop->valid = true;
  956. }
  957. PROFILE_END();
  958. }
  959. void Precipitation::createSplash(Raindrop *drop)
  960. {
  961. if (!mDataBlock)
  962. return;
  963. PROFILE_START(PrecipCreateSplash);
  964. if (drop != mSplashHead && !(drop->nextSplashDrop || drop->prevSplashDrop))
  965. {
  966. if (!mSplashHead)
  967. {
  968. mSplashHead = drop;
  969. drop->prevSplashDrop = NULL;
  970. drop->nextSplashDrop = NULL;
  971. }
  972. else
  973. {
  974. mSplashHead->prevSplashDrop = drop;
  975. drop->nextSplashDrop = mSplashHead;
  976. drop->prevSplashDrop = NULL;
  977. mSplashHead = drop;
  978. }
  979. }
  980. drop->animStartTime = Platform::getVirtualMilliseconds();
  981. if (!mAnimateSplashes)
  982. drop->texCoordIndex = (U32)(Platform::getRandom() * ((F32)mDataBlock->mSplashesPerSide*mDataBlock->mSplashesPerSide - 0.5));
  983. PROFILE_END();
  984. }
  985. void Precipitation::destroySplash(Raindrop *drop)
  986. {
  987. PROFILE_START(PrecipDestroySplash);
  988. if (drop == mSplashHead)
  989. {
  990. mSplashHead = mSplashHead->nextSplashDrop;
  991. }
  992. if (drop->nextSplashDrop)
  993. drop->nextSplashDrop->prevSplashDrop = drop->prevSplashDrop;
  994. if (drop->prevSplashDrop)
  995. drop->prevSplashDrop->nextSplashDrop = drop->nextSplashDrop;
  996. drop->nextSplashDrop = NULL;
  997. drop->prevSplashDrop = NULL;
  998. PROFILE_END();
  999. }
  1000. //--------------------------------------------------------------------------
  1001. // Processing
  1002. //--------------------------------------------------------------------------
  1003. void Precipitation::setPercentage(F32 pct)
  1004. {
  1005. mPercentage = mClampF(pct, 0, 1);
  1006. mStormData.valid = false;
  1007. if (isServerObject())
  1008. {
  1009. setMaskBits(PercentageMask);
  1010. }
  1011. }
  1012. void Precipitation::modifyStorm(F32 pct, U32 ms)
  1013. {
  1014. if ( ms == 0 )
  1015. {
  1016. setPercentage( pct );
  1017. return;
  1018. }
  1019. pct = mClampF(pct, 0, 1);
  1020. mStormData.endPct = pct;
  1021. mStormData.totalTime = ms;
  1022. if (isServerObject())
  1023. {
  1024. setMaskBits(StormMask);
  1025. return;
  1026. }
  1027. mStormData.startTime = Platform::getVirtualMilliseconds();
  1028. mStormData.startPct = mPercentage;
  1029. mStormData.valid = true;
  1030. }
  1031. void Precipitation::setTurbulence(F32 max, F32 speed, U32 ms)
  1032. {
  1033. if ( ms == 0 && !isServerObject() )
  1034. {
  1035. mUseTurbulence = max > 0;
  1036. mMaxTurbulence = max;
  1037. mTurbulenceSpeed = speed;
  1038. return;
  1039. }
  1040. mTurbulenceData.endMax = max;
  1041. mTurbulenceData.endSpeed = speed;
  1042. mTurbulenceData.totalTime = ms;
  1043. if (isServerObject())
  1044. {
  1045. setMaskBits(TurbulenceMask);
  1046. return;
  1047. }
  1048. mTurbulenceData.startTime = Platform::getVirtualMilliseconds();
  1049. mTurbulenceData.startMax = mMaxTurbulence;
  1050. mTurbulenceData.startSpeed = mTurbulenceSpeed;
  1051. mTurbulenceData.valid = true;
  1052. }
  1053. void Precipitation::interpolateTick(F32 delta)
  1054. {
  1055. AssertFatal(isClientObject(), "Precipitation is doing stuff on the server - BAD!");
  1056. // If we're not being seen then the simulation
  1057. // is paused and we don't need any interpolation.
  1058. if (mLastRenderFrame != ShapeBase::sLastRenderFrame)
  1059. return;
  1060. PROFILE_START(PrecipInterpolate);
  1061. const F32 dt = 1-delta;
  1062. const VectorF windVel = dt * getWindVelocity();
  1063. const F32 turbSpeed = dt * mTurbulenceSpeed;
  1064. Raindrop* curr = mDropHead;
  1065. VectorF turbulence;
  1066. F32 renderTime;
  1067. while (curr)
  1068. {
  1069. if (!curr->valid || !curr->toRender)
  1070. {
  1071. curr = curr->next;
  1072. continue;
  1073. }
  1074. if (mUseTurbulence)
  1075. {
  1076. renderTime = curr->time + turbSpeed;
  1077. turbulence.x = windVel.x + ( mSin(renderTime) * mMaxTurbulence );
  1078. turbulence.y = windVel.y + ( mCos(renderTime) * mMaxTurbulence );
  1079. turbulence.z = windVel.z;
  1080. curr->renderPosition = curr->position + turbulence / curr->mass;
  1081. }
  1082. else
  1083. curr->renderPosition = curr->position + windVel / curr->mass;
  1084. curr->renderPosition.z -= dt * curr->velocity;
  1085. curr = curr->next;
  1086. }
  1087. PROFILE_END();
  1088. }
  1089. void Precipitation::processTick(const Move *)
  1090. {
  1091. //nothing to do on the server
  1092. if (isServerObject() || mDataBlock == NULL || isHidden())
  1093. return;
  1094. const U32 currTime = Platform::getVirtualMilliseconds();
  1095. // Update the storm if necessary
  1096. if (mStormData.valid)
  1097. {
  1098. F32 t = (currTime - mStormData.startTime) / (F32)mStormData.totalTime;
  1099. if (t >= 1)
  1100. {
  1101. mPercentage = mStormData.endPct;
  1102. mStormData.valid = false;
  1103. }
  1104. else
  1105. mPercentage = mStormData.startPct * (1-t) + mStormData.endPct * t;
  1106. fillDropList();
  1107. }
  1108. // Do we need to update the turbulence?
  1109. if ( mTurbulenceData.valid )
  1110. {
  1111. F32 t = (currTime - mTurbulenceData.startTime) / (F32)mTurbulenceData.totalTime;
  1112. if (t >= 1)
  1113. {
  1114. mMaxTurbulence = mTurbulenceData.endMax;
  1115. mTurbulenceSpeed = mTurbulenceData.endSpeed;
  1116. mTurbulenceData.valid = false;
  1117. }
  1118. else
  1119. {
  1120. mMaxTurbulence = mTurbulenceData.startMax * (1-t) + mTurbulenceData.endMax * t;
  1121. mTurbulenceSpeed = mTurbulenceData.startSpeed * (1-t) + mTurbulenceData.endSpeed * t;
  1122. }
  1123. mUseTurbulence = mMaxTurbulence > 0;
  1124. }
  1125. // If we're not being seen then pause the
  1126. // simulation. Precip is generally noisy
  1127. // enough that no one should notice.
  1128. if (mLastRenderFrame != ShapeBase::sLastRenderFrame)
  1129. return;
  1130. //we need to update positions and do some collision here
  1131. GameConnection* conn = GameConnection::getConnectionToServer();
  1132. if (!conn)
  1133. return; //need connection to server
  1134. ShapeBase* camObj = dynamic_cast<ShapeBase*>(conn->getCameraObject());
  1135. if (!camObj)
  1136. return;
  1137. PROFILE_START(PrecipProcess);
  1138. MatrixF camMat;
  1139. camObj->getEyeTransform(&camMat);
  1140. const F32 camFov = camObj->getCameraFov();
  1141. Point3F camPos, camDir;
  1142. Box3F box;
  1143. if (mFollowCam)
  1144. {
  1145. camMat.getColumn(3, &camPos);
  1146. box = Box3F(camPos.x - mBoxWidth / 2, camPos.y - mBoxWidth / 2, camPos.z - mBoxHeight / 2,
  1147. camPos.x + mBoxWidth / 2, camPos.y + mBoxWidth / 2, camPos.z + mBoxHeight / 2);
  1148. camMat.getColumn(1, &camDir);
  1149. camDir.normalize();
  1150. }
  1151. else
  1152. {
  1153. box = mObjBox;
  1154. camMat.getColumn(3, &camPos);
  1155. mWorldToObj.mulP(camPos);
  1156. camMat.getColumn(1, &camDir);
  1157. camDir.normalize();
  1158. mWorldToObj.mulV(camDir);
  1159. }
  1160. const VectorF windVel = getWindVelocity();
  1161. const F32 fovDot = camFov / 180;
  1162. Raindrop* curr = mDropHead;
  1163. //offset the renderbox in the direction of the camera direction
  1164. //in order to have more of the drops actually rendered
  1165. if (mFollowCam)
  1166. {
  1167. box.minExtents.x += camDir.x * mBoxWidth / 4;
  1168. box.maxExtents.x += camDir.x * mBoxWidth / 4;
  1169. box.minExtents.y += camDir.y * mBoxWidth / 4;
  1170. box.maxExtents.y += camDir.y * mBoxWidth / 4;
  1171. box.minExtents.z += camDir.z * mBoxHeight / 4;
  1172. box.maxExtents.z += camDir.z * mBoxHeight / 4;
  1173. }
  1174. VectorF lookVec;
  1175. F32 pct;
  1176. const S32 dropCount = mDataBlock->mDropsPerSide*mDataBlock->mDropsPerSide;
  1177. while (curr)
  1178. {
  1179. // Update the position. This happens even if this
  1180. // is a splash so that the drop respawns when it wraps
  1181. // around to the top again.
  1182. if (mUseTurbulence)
  1183. curr->time += mTurbulenceSpeed;
  1184. curr->position += windVel / curr->mass;
  1185. curr->position.z -= curr->velocity;
  1186. // Wrap the drop if it reaches an edge of the box.
  1187. wrapDrop(curr, box, currTime, windVel);
  1188. // Did the drop pass below the hit position?
  1189. if (curr->valid && curr->position.z < curr->hitPos.z)
  1190. {
  1191. // If this drop was to hit a player or vehicle double
  1192. // check to see if the object has moved out of the way.
  1193. // This keeps us from leaving phantom trails of splashes
  1194. // behind a moving player/vehicle.
  1195. if (curr->hitType & (PlayerObjectType | VehicleObjectType))
  1196. {
  1197. findDropCutoff(curr, box, windVel);
  1198. if (curr->position.z > curr->hitPos.z)
  1199. goto NO_SPLASH; // Ugly, yet simple.
  1200. }
  1201. // The drop is dead.
  1202. curr->valid = false;
  1203. // Convert the drop into a splash or let it
  1204. // wrap around and respawn in wrapDrop().
  1205. if (mSplashMS > 0)
  1206. createSplash(curr);
  1207. // So ugly... yet simple.
  1208. NO_SPLASH:;
  1209. }
  1210. // We do not do cull individual drops when we're not
  1211. // following as it is usually a tight box and all of
  1212. // the particles are in view.
  1213. if (!mFollowCam)
  1214. curr->toRender = true;
  1215. else
  1216. {
  1217. lookVec = curr->position - camPos;
  1218. curr->toRender = mDot(lookVec, camDir) > fovDot;
  1219. }
  1220. // Do we need to animate the drop?
  1221. if (curr->valid && mDropAnimateMS > 0 && curr->toRender)
  1222. {
  1223. pct = (F32)(currTime - curr->animStartTime) / mDropAnimateMS;
  1224. pct = mFmod(pct, 1);
  1225. curr->texCoordIndex = (U32)(dropCount * pct);
  1226. }
  1227. curr = curr->next;
  1228. }
  1229. //update splashes
  1230. curr = mSplashHead;
  1231. Raindrop *next;
  1232. const S32 splashCount = mDataBlock->mSplashesPerSide * mDataBlock->mSplashesPerSide;
  1233. while (curr)
  1234. {
  1235. pct = (F32)(currTime - curr->animStartTime) / mSplashMS;
  1236. if (pct >= 1.0f)
  1237. {
  1238. next = curr->nextSplashDrop;
  1239. destroySplash(curr);
  1240. curr = next;
  1241. continue;
  1242. }
  1243. if (mAnimateSplashes)
  1244. curr->texCoordIndex = (U32)(splashCount * pct);
  1245. curr = curr->nextSplashDrop;
  1246. }
  1247. PROFILE_END_NAMED(PrecipProcess);
  1248. }
  1249. //--------------------------------------------------------------------------
  1250. // Rendering
  1251. //--------------------------------------------------------------------------
  1252. void Precipitation::prepRenderImage(SceneRenderState* state)
  1253. {
  1254. PROFILE_SCOPE(Precipitation_prepRenderImage);
  1255. // We we have no drops then skip rendering
  1256. // and don't bother with the sound.
  1257. if (mMaxVBDrops == 0)
  1258. return;
  1259. // We do nothing if we're not supposed to be reflected.
  1260. if ( state->isReflectPass() && !mReflect )
  1261. return;
  1262. // This should be sufficient for most objects that don't manage zones, and
  1263. // don't need to return a specialized RenderImage...
  1264. ObjectRenderInst *ri = state->getRenderPass()->allocInst<ObjectRenderInst>();
  1265. ri->renderDelegate.bind(this, &Precipitation::renderObject);
  1266. ri->type = RenderPassManager::RIT_Foliage;
  1267. state->getRenderPass()->addInst( ri );
  1268. }
  1269. void Precipitation::renderObject(ObjectRenderInst *ri, SceneRenderState *state, BaseMatInstance* overrideMat)
  1270. {
  1271. if (overrideMat)
  1272. return;
  1273. GameConnection* conn = GameConnection::getConnectionToServer();
  1274. if (!conn)
  1275. return; //need connection to server
  1276. ShapeBase* camObj = dynamic_cast<ShapeBase*>(conn->getCameraObject());
  1277. if (!camObj)
  1278. return; // need camera object
  1279. PROFILE_START(PrecipRender);
  1280. GFX->pushWorldMatrix();
  1281. MatrixF world = GFX->getWorldMatrix();
  1282. MatrixF proj = GFX->getProjectionMatrix();
  1283. if (!mFollowCam)
  1284. {
  1285. world.mul( getRenderTransform() );
  1286. world.scale( getScale() );
  1287. GFX->setWorldMatrix( world );
  1288. }
  1289. proj.mul(world);
  1290. //GFX2 doesn't require transpose?
  1291. //proj.transpose();
  1292. Point3F camPos = state->getCameraPosition();
  1293. VectorF camVel = camObj->getVelocity();
  1294. if (!mFollowCam)
  1295. {
  1296. getRenderWorldTransform().mulP(camPos);
  1297. getRenderWorldTransform().mulV(camVel);
  1298. }
  1299. const VectorF windVel = getWindVelocity();
  1300. const bool useBillboards = mUseTrueBillboards;
  1301. const F32 dropSize = mDropSize;
  1302. Point3F pos;
  1303. VectorF orthoDir, velocity, right, up, rightUp(0.0f, 0.0f, 0.0f), leftUp(0.0f, 0.0f, 0.0f);
  1304. F32 distance = 0;
  1305. GFXVertexPCT* vertPtr = NULL;
  1306. const Point2F *tc;
  1307. // Do this here and we won't have to in the loop!
  1308. if (useBillboards)
  1309. {
  1310. MatrixF camMat = state->getCameraTransform();
  1311. camMat.inverse();
  1312. camMat.getRow(0,&right);
  1313. camMat.getRow(2,&up);
  1314. if (!mFollowCam)
  1315. {
  1316. mWorldToObj.mulV(right);
  1317. mWorldToObj.mulV(up);
  1318. }
  1319. right.normalize();
  1320. up.normalize();
  1321. right *= mDropSize;
  1322. up *= mDropSize;
  1323. rightUp = right + up;
  1324. leftUp = -right + up;
  1325. }
  1326. // We pass the sunlight as a constant to the
  1327. // shader. Once the lighting and shadow systems
  1328. // are added into TSE we can expand this to include
  1329. // the N nearest lights to the camera + the ambient.
  1330. LinearColorF ambient( 1, 1, 1 );
  1331. if ( mUseLighting )
  1332. {
  1333. const LightInfo *sunlight = LIGHTMGR->getSpecialLight(LightManager::slSunLightType);
  1334. ambient = sunlight->getColor();
  1335. }
  1336. if ( mGlowIntensity.red > 0 ||
  1337. mGlowIntensity.green > 0 ||
  1338. mGlowIntensity.blue > 0 )
  1339. {
  1340. ambient *= mGlowIntensity;
  1341. }
  1342. // Setup render state
  1343. if (mDefaultSB.isNull())
  1344. {
  1345. GFXStateBlockDesc desc;
  1346. desc.zWriteEnable = false;
  1347. desc.setAlphaTest(true, GFXCmpGreaterEqual, 1);
  1348. desc.setBlend(true, GFXBlendSrcAlpha, GFXBlendInvSrcAlpha);
  1349. mDefaultSB = GFX->createStateBlock(desc);
  1350. desc.samplersDefined = true;
  1351. mDistantSB = GFX->createStateBlock(desc);
  1352. }
  1353. GFX->setStateBlock(mDefaultSB);
  1354. // Everything is rendered from these buffers.
  1355. GFX->setPrimitiveBuffer(mRainIB);
  1356. GFX->setVertexBuffer(mRainVB);
  1357. // Set the constants used by the shaders.
  1358. if (mDropShader)
  1359. {
  1360. Point2F fadeStartEnd( mFadeDistance, mFadeDistanceEnd );
  1361. mDropShaderConsts->setSafe(mDropShaderModelViewSC, proj);
  1362. mDropShaderConsts->setSafe(mDropShaderFadeStartEndSC, fadeStartEnd);
  1363. mDropShaderConsts->setSafe(mDropShaderCameraPosSC, camPos);
  1364. mDropShaderConsts->setSafe(mDropShaderAmbientSC, Point3F(ambient.red, ambient.green, ambient.blue));
  1365. }
  1366. if (mSplashShader)
  1367. {
  1368. Point2F fadeStartEnd( mFadeDistance, mFadeDistanceEnd );
  1369. mSplashShaderConsts->setSafe(mSplashShaderModelViewSC, proj);
  1370. mSplashShaderConsts->setSafe(mSplashShaderFadeStartEndSC, fadeStartEnd);
  1371. mSplashShaderConsts->setSafe(mSplashShaderCameraPosSC, camPos);
  1372. mSplashShaderConsts->setSafe(mSplashShaderAmbientSC, Point3F(ambient.red, ambient.green, ambient.blue));
  1373. }
  1374. // Time to render the drops...
  1375. const Raindrop *curr = mDropHead;
  1376. U32 vertCount = 0;
  1377. GFX->setTexture(0, mDropHandle);
  1378. // Use the shader or setup the pipeline
  1379. // for fixed function rendering.
  1380. if (mDropShader)
  1381. {
  1382. GFX->setShader( mDropShader );
  1383. GFX->setShaderConstBuffer( mDropShaderConsts );
  1384. }
  1385. else
  1386. {
  1387. GFX->setupGenericShaders(GFXDevice::GSTexture);
  1388. // We don't support distance fade or lighting without shaders.
  1389. GFX->setStateBlock(mDistantSB);
  1390. }
  1391. while (curr)
  1392. {
  1393. // Skip ones that are not drops (hit something and
  1394. // may have been converted into a splash) or they
  1395. // are behind the camera.
  1396. if (!curr->valid || !curr->toRender)
  1397. {
  1398. curr = curr->next;
  1399. continue;
  1400. }
  1401. pos = curr->renderPosition;
  1402. // two forms of billboards - true billboards (which we set
  1403. // above outside this loop) or axis-aligned with velocity
  1404. // (this codeblock) the axis-aligned billboards are aligned
  1405. // with the velocity of the raindrop, and tilted slightly
  1406. // towards the camera
  1407. if (!useBillboards)
  1408. {
  1409. orthoDir = camPos - pos;
  1410. distance = orthoDir.len();
  1411. // Inline the normalize so we don't
  1412. // calculate the ortho len twice.
  1413. if (distance > 0.0)
  1414. orthoDir *= 1.0f / distance;
  1415. else
  1416. orthoDir.set( 0, 0, 1 );
  1417. velocity = windVel / curr->mass;
  1418. // We do not optimize this for the "still" case
  1419. // because its not a typical scenario.
  1420. if (mRotateWithCamVel)
  1421. velocity -= camVel / (distance > 2.0f ? distance : 2.0f) * 0.3f;
  1422. velocity.z -= curr->velocity;
  1423. velocity.normalize();
  1424. right = mCross(-velocity, orthoDir);
  1425. right.normalize();
  1426. up = mCross(orthoDir, right) * 0.5 - velocity * 0.5;
  1427. up.normalize();
  1428. right *= dropSize;
  1429. up *= dropSize;
  1430. rightUp = right + up;
  1431. leftUp = -right + up;
  1432. }
  1433. // Do we need to relock the buffer?
  1434. if ( !vertPtr )
  1435. vertPtr = mRainVB.lock();
  1436. if(!vertPtr) return;
  1437. // Set the proper texture coords... (it's fun!)
  1438. tc = &mTexCoords[4*curr->texCoordIndex];
  1439. vertPtr->point = pos + leftUp;
  1440. vertPtr->texCoord = *tc;
  1441. tc++;
  1442. vertPtr++;
  1443. vertPtr->point = pos + rightUp;
  1444. vertPtr->texCoord = *tc;
  1445. tc++;
  1446. vertPtr++;
  1447. vertPtr->point = pos - leftUp;
  1448. vertPtr->texCoord = *tc;
  1449. tc++;
  1450. vertPtr++;
  1451. vertPtr->point = pos - rightUp;
  1452. vertPtr->texCoord = *tc;
  1453. tc++;
  1454. vertPtr++;
  1455. // Do we need to render to clear the buffer?
  1456. vertCount += 4;
  1457. if ( (vertCount + 4) >= mRainVB->mNumVerts ) {
  1458. mRainVB.unlock();
  1459. GFX->drawIndexedPrimitive(GFXTriangleList, 0, 0, vertCount, 0, vertCount / 2);
  1460. vertPtr = NULL;
  1461. vertCount = 0;
  1462. }
  1463. curr = curr->next;
  1464. }
  1465. // Do we have stuff left to render?
  1466. if ( vertCount > 0 ) {
  1467. mRainVB.unlock();
  1468. GFX->drawIndexedPrimitive(GFXTriangleList, 0, 0, vertCount, 0, vertCount / 2);
  1469. vertCount = 0;
  1470. vertPtr = NULL;
  1471. }
  1472. // Setup the billboard for the splashes.
  1473. MatrixF camMat = state->getCameraTransform();
  1474. camMat.inverse();
  1475. camMat.getRow(0, &right);
  1476. camMat.getRow(2, &up);
  1477. if (!mFollowCam)
  1478. {
  1479. mWorldToObj.mulV(right);
  1480. mWorldToObj.mulV(up);
  1481. }
  1482. right.normalize();
  1483. up.normalize();
  1484. right *= mSplashSize;
  1485. up *= mSplashSize;
  1486. rightUp = right + up;
  1487. leftUp = -right + up;
  1488. // Render the visible splashes.
  1489. curr = mSplashHead;
  1490. GFX->setTexture(0, mSplashHandle);
  1491. if (mSplashShader)
  1492. {
  1493. GFX->setShader( mSplashShader );
  1494. GFX->setShaderConstBuffer(mSplashShaderConsts);
  1495. }
  1496. else
  1497. GFX->setupGenericShaders(GFXDevice::GSTexture);
  1498. while (curr)
  1499. {
  1500. if (!curr->toRender)
  1501. {
  1502. curr = curr->nextSplashDrop;
  1503. continue;
  1504. }
  1505. pos = curr->hitPos;
  1506. tc = &mSplashCoords[4*curr->texCoordIndex];
  1507. // Do we need to relock the buffer?
  1508. if ( !vertPtr )
  1509. vertPtr = mRainVB.lock();
  1510. if(!vertPtr) return;
  1511. vertPtr->point = pos + leftUp;
  1512. vertPtr->texCoord = *tc;
  1513. tc++;
  1514. vertPtr++;
  1515. vertPtr->point = pos + rightUp;
  1516. vertPtr->texCoord = *tc;
  1517. tc++;
  1518. vertPtr++;
  1519. vertPtr->point = pos - leftUp;
  1520. vertPtr->texCoord = *tc;
  1521. tc++;
  1522. vertPtr++;
  1523. vertPtr->point = pos - rightUp;
  1524. vertPtr->texCoord = *tc;
  1525. tc++;
  1526. vertPtr++;
  1527. // Do we need to flush the buffer by rendering?
  1528. vertCount += 4;
  1529. if ( (vertCount + 4) >= mRainVB->mNumVerts ) {
  1530. mRainVB.unlock();
  1531. GFX->drawIndexedPrimitive(GFXTriangleList, 0, 0, vertCount, 0, vertCount / 2);
  1532. vertPtr = NULL;
  1533. vertCount = 0;
  1534. }
  1535. curr = curr->nextSplashDrop;
  1536. }
  1537. // Do we have stuff left to render?
  1538. if ( vertCount > 0 ) {
  1539. mRainVB.unlock();
  1540. GFX->drawIndexedPrimitive(GFXTriangleList, 0, 0, vertCount, 0, vertCount / 2);
  1541. }
  1542. mLastRenderFrame = ShapeBase::sLastRenderFrame;
  1543. GFX->popWorldMatrix();
  1544. PROFILE_END();
  1545. }