precipitation.cpp 56 KB

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