precipitation.cpp 55 KB

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