precipitation.cpp 56 KB

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