precipitation.cpp 55 KB

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