precipitation.cpp 56 KB

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