precipitation.cpp 55 KB

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