precipitation.cpp 55 KB

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