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. WaterObjectType |
  46. StaticShapeObjectType;
  47. IMPLEMENT_CO_NETOBJECT_V1(Precipitation);
  48. IMPLEMENT_CO_DATABLOCK_V1(PrecipitationData);
  49. ConsoleDocClass( Precipitation,
  50. "@brief Defines a precipitation based storm (rain, snow, etc).\n\n"
  51. "The Precipitation effect works by creating many 'drops' within a fixed size "
  52. "box. This box can be configured to move around with the camera (to simulate "
  53. "level-wide precipitation), or to remain in a fixed position (to simulate "
  54. "localized precipitation). When #followCam is true, the box containing the "
  55. "droplets can be thought of as centered on the camera then pushed slightly "
  56. "forward in the direction the camera is facing so most of the box is in "
  57. "front of the camera (allowing more drops to be visible on screen at once).\n\n"
  58. "The effect can also be configured to create a small 'splash' whenever a drop "
  59. "hits another world object.\n\n"
  60. "@tsexample\n"
  61. "// The following is added to a level file (.mis) by the World Editor\n"
  62. "new Precipitation( TheRain )\n"
  63. "{\n"
  64. " dropSize = \"0.5\";\n"
  65. " splashSize = \"0.5\";\n"
  66. " splashMS = \"250\";\n"
  67. " animateSplashes = \"1\";\n"
  68. " dropAnimateMS = \"0\";\n"
  69. " fadeDist = \"0\";\n"
  70. " fadeDistEnd = \"0\";\n"
  71. " useTrueBillboards = \"0\";\n"
  72. " useLighting = \"0\";\n"
  73. " glowIntensity = \"0 0 0 0\";\n"
  74. " reflect = \"0\";\n"
  75. " rotateWithCamVel = \"1\";\n"
  76. " doCollision = \"1\";\n"
  77. " hitPlayers = \"0\";\n"
  78. " hitVehicles = \"0\";\n"
  79. " followCam = \"1\";\n"
  80. " useWind = \"0\";\n"
  81. " minSpeed = \"1.5\";\n"
  82. " maxSpeed = \"2\";\n"
  83. " minMass = \"0.75\";\n"
  84. " maxMass = \"0.85\";\n"
  85. " useTurbulence = \"0\";\n"
  86. " maxTurbulence = \"0.1\";\n"
  87. " turbulenceSpeed = \"0.2\";\n"
  88. " numDrops = \"1024\";\n"
  89. " boxWidth = \"200\";\n"
  90. " boxHeight = \"100\";\n"
  91. " dataBlock = \"HeavyRain\";\n"
  92. "};\n"
  93. "@endtsexample\n"
  94. "@ingroup FX\n"
  95. "@ingroup Atmosphere\n"
  96. "@see PrecipitationData\n"
  97. );
  98. ConsoleDocClass( PrecipitationData,
  99. "@brief Defines the droplets used in a storm (raindrops, snowflakes, etc).\n\n"
  100. "@tsexample\n"
  101. "datablock PrecipitationData( HeavyRain )\n"
  102. "{\n"
  103. " soundProfile = \"HeavyRainSound\";\n"
  104. " dropTexture = \"art/environment/precipitation/rain\";\n"
  105. " splashTexture = \"art/environment/precipitation/water_splash\";\n"
  106. " dropsPerSide = 4;\n"
  107. " splashesPerSide = 2;\n"
  108. "};\n"
  109. "@endtsexample\n"
  110. "@ingroup FX\n"
  111. "@ingroup Atmosphere\n"
  112. "@see Precipitation\n"
  113. );
  114. //----------------------------------------------------------
  115. // PrecipitationData
  116. //----------------------------------------------------------
  117. PrecipitationData::PrecipitationData()
  118. {
  119. soundProfile = NULL;
  120. mDropName = StringTable->EmptyString();
  121. mDropShaderName = StringTable->EmptyString();
  122. mSplashName = StringTable->EmptyString();
  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. addField( "dropTexture", TypeFilename, Offset(mDropName, PrecipitationData),
  132. "@brief Texture filename for drop particles.\n\n"
  133. "The drop texture can contain several different drop sub-textures "
  134. "arranged in a grid. There must be the same number of rows as columns. A "
  135. "random frame will be chosen for each drop." );
  136. addField( "dropShader", TypeString, Offset(mDropShaderName, PrecipitationData),
  137. "The name of the shader used for raindrops." );
  138. addField( "splashTexture", TypeFilename, Offset(mSplashName, PrecipitationData),
  139. "@brief Texture filename for splash particles.\n\n"
  140. "The splash texture can contain several different splash sub-textures "
  141. "arranged in a grid. There must be the same number of rows as columns. A "
  142. "random frame will be chosen for each splash." );
  143. addField( "splashShader", TypeString, Offset(mSplashShaderName, PrecipitationData),
  144. "The name of the shader used for splashes." );
  145. addField( "dropsPerSide", TypeS32, Offset(mDropsPerSide, PrecipitationData),
  146. "@brief How many rows and columns are in the raindrop texture.\n\n"
  147. "For example, if the texture has 16 raindrops arranged in a grid, this "
  148. "field should be set to 4." );
  149. addField( "splashesPerSide", TypeS32, Offset(mSplashesPerSide, PrecipitationData),
  150. "@brief How many rows and columns are in the splash texture.\n\n"
  151. "For example, if the texture has 9 splashes arranged in a grid, this "
  152. "field should be set to 3." );
  153. Parent::initPersistFields();
  154. }
  155. bool PrecipitationData::preload( bool server, String &errorStr )
  156. {
  157. if( Parent::preload( server, errorStr) == false)
  158. return false;
  159. if( !server && !sfxResolve( &soundProfile, errorStr ) )
  160. return false;
  161. return true;
  162. }
  163. void PrecipitationData::packData(BitStream* stream)
  164. {
  165. Parent::packData(stream);
  166. sfxWrite( stream, soundProfile );
  167. stream->writeString(mDropName);
  168. stream->writeString(mDropShaderName);
  169. stream->writeString(mSplashName);
  170. stream->writeString(mSplashShaderName);
  171. stream->write(mDropsPerSide);
  172. stream->write(mSplashesPerSide);
  173. }
  174. void PrecipitationData::unpackData(BitStream* stream)
  175. {
  176. Parent::unpackData(stream);
  177. sfxRead( stream, &soundProfile );
  178. mDropName = stream->readSTString();
  179. mDropShaderName = stream->readSTString();
  180. mSplashName = stream->readSTString();
  181. mSplashShaderName = stream->readSTString();
  182. stream->read(&mDropsPerSide);
  183. stream->read(&mSplashesPerSide);
  184. }
  185. //----------------------------------------------------------
  186. // Precipitation!
  187. //----------------------------------------------------------
  188. Precipitation::Precipitation()
  189. {
  190. mTypeMask |= ProjectileObjectType;
  191. mDataBlock = NULL;
  192. mTexCoords = NULL;
  193. mSplashCoords = NULL;
  194. mDropShader = NULL;
  195. mDropHandle = NULL;
  196. mSplashShader = NULL;
  197. mSplashHandle = NULL;
  198. mDropHead = NULL;
  199. mSplashHead = NULL;
  200. mNumDrops = 1024;
  201. mPercentage = 1.0;
  202. mMinSpeed = 1.5;
  203. mMaxSpeed = 2.0;
  204. mFollowCam = true;
  205. mLastRenderFrame = 0;
  206. mDropHitMask = 0;
  207. mDropSize = 0.5;
  208. mSplashSize = 0.5;
  209. mUseTrueBillboards = false;
  210. mSplashMS = 250;
  211. mAnimateSplashes = true;
  212. mDropAnimateMS = 0;
  213. mUseLighting = false;
  214. mGlowIntensity = ColorF( 0,0,0,0 );
  215. mReflect = false;
  216. mUseWind = false;
  217. mBoxWidth = 200;
  218. mBoxHeight = 100;
  219. mFadeDistance = 0;
  220. mFadeDistanceEnd = 0;
  221. mMinMass = 0.75f;
  222. mMaxMass = 0.85f;
  223. mMaxTurbulence = 0.1f;
  224. mTurbulenceSpeed = 0.2f;
  225. mUseTurbulence = false;
  226. mRotateWithCamVel = true;
  227. mDoCollision = true;
  228. mDropHitPlayers = false;
  229. mDropHitVehicles = false;
  230. mStormData.valid = false;
  231. mStormData.startPct = 0;
  232. mStormData.endPct = 0;
  233. mStormData.startTime = 0;
  234. mStormData.totalTime = 0;
  235. mTurbulenceData.valid = false;
  236. mTurbulenceData.startTime = 0;
  237. mTurbulenceData.totalTime = 0;
  238. mTurbulenceData.startMax = 0;
  239. mTurbulenceData.startSpeed = 0;
  240. mTurbulenceData.endMax = 0;
  241. mTurbulenceData.endSpeed = 0;
  242. mAmbientSound = NULL;
  243. mDropShaderModelViewSC = NULL;
  244. mDropShaderFadeStartEndSC = NULL;
  245. mDropShaderCameraPosSC = NULL;
  246. mDropShaderAmbientSC = NULL;
  247. mSplashShaderModelViewSC = NULL;
  248. mSplashShaderFadeStartEndSC = NULL;
  249. mSplashShaderCameraPosSC = NULL;
  250. mSplashShaderAmbientSC = NULL;
  251. mMaxVBDrops = 5000;
  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.0f),
  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, GFXBufferTypeDynamic);
  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 = mSplashHead->nextSplashDrop;
  983. }
  984. if (drop->nextSplashDrop)
  985. drop->nextSplashDrop->prevSplashDrop = drop->prevSplashDrop;
  986. if (drop->prevSplashDrop)
  987. drop->prevSplashDrop->nextSplashDrop = drop->nextSplashDrop;
  988. drop->nextSplashDrop = NULL;
  989. drop->prevSplashDrop = NULL;
  990. PROFILE_END();
  991. }
  992. //--------------------------------------------------------------------------
  993. // Processing
  994. //--------------------------------------------------------------------------
  995. void Precipitation::setPercentage(F32 pct)
  996. {
  997. mPercentage = mClampF(pct, 0, 1);
  998. mStormData.valid = false;
  999. if (isServerObject())
  1000. {
  1001. setMaskBits(PercentageMask);
  1002. }
  1003. }
  1004. void Precipitation::modifyStorm(F32 pct, U32 ms)
  1005. {
  1006. if ( ms == 0 )
  1007. {
  1008. setPercentage( pct );
  1009. return;
  1010. }
  1011. pct = mClampF(pct, 0, 1);
  1012. mStormData.endPct = pct;
  1013. mStormData.totalTime = ms;
  1014. if (isServerObject())
  1015. {
  1016. setMaskBits(StormMask);
  1017. return;
  1018. }
  1019. mStormData.startTime = Platform::getVirtualMilliseconds();
  1020. mStormData.startPct = mPercentage;
  1021. mStormData.valid = true;
  1022. }
  1023. void Precipitation::setTurbulence(F32 max, F32 speed, U32 ms)
  1024. {
  1025. if ( ms == 0 && !isServerObject() )
  1026. {
  1027. mUseTurbulence = max > 0;
  1028. mMaxTurbulence = max;
  1029. mTurbulenceSpeed = speed;
  1030. return;
  1031. }
  1032. mTurbulenceData.endMax = max;
  1033. mTurbulenceData.endSpeed = speed;
  1034. mTurbulenceData.totalTime = ms;
  1035. if (isServerObject())
  1036. {
  1037. setMaskBits(TurbulenceMask);
  1038. return;
  1039. }
  1040. mTurbulenceData.startTime = Platform::getVirtualMilliseconds();
  1041. mTurbulenceData.startMax = mMaxTurbulence;
  1042. mTurbulenceData.startSpeed = mTurbulenceSpeed;
  1043. mTurbulenceData.valid = true;
  1044. }
  1045. void Precipitation::interpolateTick(F32 delta)
  1046. {
  1047. AssertFatal(isClientObject(), "Precipitation is doing stuff on the server - BAD!");
  1048. // If we're not being seen then the simulation
  1049. // is paused and we don't need any interpolation.
  1050. if (mLastRenderFrame != ShapeBase::sLastRenderFrame)
  1051. return;
  1052. PROFILE_START(PrecipInterpolate);
  1053. const F32 dt = 1-delta;
  1054. const VectorF windVel = dt * getWindVelocity();
  1055. const F32 turbSpeed = dt * mTurbulenceSpeed;
  1056. Raindrop* curr = mDropHead;
  1057. VectorF turbulence;
  1058. F32 renderTime;
  1059. while (curr)
  1060. {
  1061. if (!curr->valid || !curr->toRender)
  1062. {
  1063. curr = curr->next;
  1064. continue;
  1065. }
  1066. if (mUseTurbulence)
  1067. {
  1068. renderTime = curr->time + turbSpeed;
  1069. turbulence.x = windVel.x + ( mSin(renderTime) * mMaxTurbulence );
  1070. turbulence.y = windVel.y + ( mCos(renderTime) * mMaxTurbulence );
  1071. turbulence.z = windVel.z;
  1072. curr->renderPosition = curr->position + turbulence / curr->mass;
  1073. }
  1074. else
  1075. curr->renderPosition = curr->position + windVel / curr->mass;
  1076. curr->renderPosition.z -= dt * curr->velocity;
  1077. curr = curr->next;
  1078. }
  1079. PROFILE_END();
  1080. }
  1081. void Precipitation::processTick(const Move *)
  1082. {
  1083. //nothing to do on the server
  1084. if (isServerObject() || mDataBlock == NULL || isHidden())
  1085. return;
  1086. const U32 currTime = Platform::getVirtualMilliseconds();
  1087. // Update the storm if necessary
  1088. if (mStormData.valid)
  1089. {
  1090. F32 t = (currTime - mStormData.startTime) / (F32)mStormData.totalTime;
  1091. if (t >= 1)
  1092. {
  1093. mPercentage = mStormData.endPct;
  1094. mStormData.valid = false;
  1095. }
  1096. else
  1097. mPercentage = mStormData.startPct * (1-t) + mStormData.endPct * t;
  1098. fillDropList();
  1099. }
  1100. // Do we need to update the turbulence?
  1101. if ( mTurbulenceData.valid )
  1102. {
  1103. F32 t = (currTime - mTurbulenceData.startTime) / (F32)mTurbulenceData.totalTime;
  1104. if (t >= 1)
  1105. {
  1106. mMaxTurbulence = mTurbulenceData.endMax;
  1107. mTurbulenceSpeed = mTurbulenceData.endSpeed;
  1108. mTurbulenceData.valid = false;
  1109. }
  1110. else
  1111. {
  1112. mMaxTurbulence = mTurbulenceData.startMax * (1-t) + mTurbulenceData.endMax * t;
  1113. mTurbulenceSpeed = mTurbulenceData.startSpeed * (1-t) + mTurbulenceData.endSpeed * t;
  1114. }
  1115. mUseTurbulence = mMaxTurbulence > 0;
  1116. }
  1117. // If we're not being seen then pause the
  1118. // simulation. Precip is generally noisy
  1119. // enough that no one should notice.
  1120. if (mLastRenderFrame != ShapeBase::sLastRenderFrame)
  1121. return;
  1122. //we need to update positions and do some collision here
  1123. GameConnection* conn = GameConnection::getConnectionToServer();
  1124. if (!conn)
  1125. return; //need connection to server
  1126. ShapeBase* camObj = dynamic_cast<ShapeBase*>(conn->getCameraObject());
  1127. if (!camObj)
  1128. return;
  1129. PROFILE_START(PrecipProcess);
  1130. MatrixF camMat;
  1131. camObj->getEyeTransform(&camMat);
  1132. const F32 camFov = camObj->getCameraFov();
  1133. Point3F camPos, camDir;
  1134. Box3F box;
  1135. if (mFollowCam)
  1136. {
  1137. camMat.getColumn(3, &camPos);
  1138. box = Box3F(camPos.x - mBoxWidth / 2, camPos.y - mBoxWidth / 2, camPos.z - mBoxHeight / 2,
  1139. camPos.x + mBoxWidth / 2, camPos.y + mBoxWidth / 2, camPos.z + mBoxHeight / 2);
  1140. camMat.getColumn(1, &camDir);
  1141. camDir.normalize();
  1142. }
  1143. else
  1144. {
  1145. box = mObjBox;
  1146. camMat.getColumn(3, &camPos);
  1147. mWorldToObj.mulP(camPos);
  1148. camMat.getColumn(1, &camDir);
  1149. camDir.normalize();
  1150. mWorldToObj.mulV(camDir);
  1151. }
  1152. const VectorF windVel = getWindVelocity();
  1153. const F32 fovDot = camFov / 180;
  1154. Raindrop* curr = mDropHead;
  1155. //offset the renderbox in the direction of the camera direction
  1156. //in order to have more of the drops actually rendered
  1157. if (mFollowCam)
  1158. {
  1159. box.minExtents.x += camDir.x * mBoxWidth / 4;
  1160. box.maxExtents.x += camDir.x * mBoxWidth / 4;
  1161. box.minExtents.y += camDir.y * mBoxWidth / 4;
  1162. box.maxExtents.y += camDir.y * mBoxWidth / 4;
  1163. box.minExtents.z += camDir.z * mBoxHeight / 4;
  1164. box.maxExtents.z += camDir.z * mBoxHeight / 4;
  1165. }
  1166. VectorF lookVec;
  1167. F32 pct;
  1168. const S32 dropCount = mDataBlock->mDropsPerSide*mDataBlock->mDropsPerSide;
  1169. while (curr)
  1170. {
  1171. // Update the position. This happens even if this
  1172. // is a splash so that the drop respawns when it wraps
  1173. // around to the top again.
  1174. if (mUseTurbulence)
  1175. curr->time += mTurbulenceSpeed;
  1176. curr->position += windVel / curr->mass;
  1177. curr->position.z -= curr->velocity;
  1178. // Wrap the drop if it reaches an edge of the box.
  1179. wrapDrop(curr, box, currTime, windVel);
  1180. // Did the drop pass below the hit position?
  1181. if (curr->valid && curr->position.z < curr->hitPos.z)
  1182. {
  1183. // If this drop was to hit a player or vehicle double
  1184. // check to see if the object has moved out of the way.
  1185. // This keeps us from leaving phantom trails of splashes
  1186. // behind a moving player/vehicle.
  1187. if (curr->hitType & (PlayerObjectType | VehicleObjectType))
  1188. {
  1189. findDropCutoff(curr, box, windVel);
  1190. if (curr->position.z > curr->hitPos.z)
  1191. goto NO_SPLASH; // Ugly, yet simple.
  1192. }
  1193. // The drop is dead.
  1194. curr->valid = false;
  1195. // Convert the drop into a splash or let it
  1196. // wrap around and respawn in wrapDrop().
  1197. if (mSplashMS > 0)
  1198. createSplash(curr);
  1199. // So ugly... yet simple.
  1200. NO_SPLASH:;
  1201. }
  1202. // We do not do cull individual drops when we're not
  1203. // following as it is usually a tight box and all of
  1204. // the particles are in view.
  1205. if (!mFollowCam)
  1206. curr->toRender = true;
  1207. else
  1208. {
  1209. lookVec = curr->position - camPos;
  1210. curr->toRender = mDot(lookVec, camDir) > fovDot;
  1211. }
  1212. // Do we need to animate the drop?
  1213. if (curr->valid && mDropAnimateMS > 0 && curr->toRender)
  1214. {
  1215. pct = (F32)(currTime - curr->animStartTime) / mDropAnimateMS;
  1216. pct = mFmod(pct, 1);
  1217. curr->texCoordIndex = (U32)(dropCount * pct);
  1218. }
  1219. curr = curr->next;
  1220. }
  1221. //update splashes
  1222. curr = mSplashHead;
  1223. Raindrop *next;
  1224. const S32 splashCount = mDataBlock->mSplashesPerSide * mDataBlock->mSplashesPerSide;
  1225. while (curr)
  1226. {
  1227. pct = (F32)(currTime - curr->animStartTime) / mSplashMS;
  1228. if (pct >= 1.0f)
  1229. {
  1230. next = curr->nextSplashDrop;
  1231. destroySplash(curr);
  1232. curr = next;
  1233. continue;
  1234. }
  1235. if (mAnimateSplashes)
  1236. curr->texCoordIndex = (U32)(splashCount * pct);
  1237. curr = curr->nextSplashDrop;
  1238. }
  1239. PROFILE_END_NAMED(PrecipProcess);
  1240. }
  1241. //--------------------------------------------------------------------------
  1242. // Rendering
  1243. //--------------------------------------------------------------------------
  1244. void Precipitation::prepRenderImage(SceneRenderState* state)
  1245. {
  1246. PROFILE_SCOPE(Precipitation_prepRenderImage);
  1247. // We we have no drops then skip rendering
  1248. // and don't bother with the sound.
  1249. if (mMaxVBDrops == 0)
  1250. return;
  1251. // We do nothing if we're not supposed to be reflected.
  1252. if ( state->isReflectPass() && !mReflect )
  1253. return;
  1254. // This should be sufficient for most objects that don't manage zones, and
  1255. // don't need to return a specialized RenderImage...
  1256. ObjectRenderInst *ri = state->getRenderPass()->allocInst<ObjectRenderInst>();
  1257. ri->renderDelegate.bind(this, &Precipitation::renderObject);
  1258. ri->type = RenderPassManager::RIT_Foliage;
  1259. state->getRenderPass()->addInst( ri );
  1260. }
  1261. void Precipitation::renderObject(ObjectRenderInst *ri, SceneRenderState *state, BaseMatInstance* overrideMat)
  1262. {
  1263. if (overrideMat)
  1264. return;
  1265. #ifdef TORQUE_OS_XENON
  1266. return;
  1267. #endif
  1268. GameConnection* conn = GameConnection::getConnectionToServer();
  1269. if (!conn)
  1270. return; //need connection to server
  1271. ShapeBase* camObj = dynamic_cast<ShapeBase*>(conn->getCameraObject());
  1272. if (!camObj)
  1273. return; // need camera object
  1274. PROFILE_START(PrecipRender);
  1275. GFX->pushWorldMatrix();
  1276. MatrixF world = GFX->getWorldMatrix();
  1277. MatrixF proj = GFX->getProjectionMatrix();
  1278. if (!mFollowCam)
  1279. {
  1280. world.mul( getRenderTransform() );
  1281. world.scale( getScale() );
  1282. GFX->setWorldMatrix( world );
  1283. }
  1284. proj.mul(world);
  1285. //GFX2 doesn't require transpose?
  1286. //proj.transpose();
  1287. Point3F camPos = state->getCameraPosition();
  1288. VectorF camVel = camObj->getVelocity();
  1289. if (!mFollowCam)
  1290. {
  1291. getRenderWorldTransform().mulP(camPos);
  1292. getRenderWorldTransform().mulV(camVel);
  1293. }
  1294. const VectorF windVel = getWindVelocity();
  1295. const bool useBillboards = mUseTrueBillboards;
  1296. const F32 dropSize = mDropSize;
  1297. Point3F pos;
  1298. VectorF orthoDir, velocity, right, up, rightUp(0.0f, 0.0f, 0.0f), leftUp(0.0f, 0.0f, 0.0f);
  1299. F32 distance = 0;
  1300. GFXVertexPCT* vertPtr = NULL;
  1301. const Point2F *tc;
  1302. // Do this here and we won't have to in the loop!
  1303. if (useBillboards)
  1304. {
  1305. MatrixF camMat = state->getCameraTransform();
  1306. camMat.inverse();
  1307. camMat.getRow(0,&right);
  1308. camMat.getRow(2,&up);
  1309. if (!mFollowCam)
  1310. {
  1311. mWorldToObj.mulV(right);
  1312. mWorldToObj.mulV(up);
  1313. }
  1314. right.normalize();
  1315. up.normalize();
  1316. right *= mDropSize;
  1317. up *= mDropSize;
  1318. rightUp = right + up;
  1319. leftUp = -right + up;
  1320. }
  1321. // We pass the sunlight as a constant to the
  1322. // shader. Once the lighting and shadow systems
  1323. // are added into TSE we can expand this to include
  1324. // the N nearest lights to the camera + the ambient.
  1325. ColorF ambient( 1, 1, 1 );
  1326. if ( mUseLighting )
  1327. {
  1328. const LightInfo *sunlight = LIGHTMGR->getSpecialLight(LightManager::slSunLightType);
  1329. ambient = sunlight->getColor();
  1330. }
  1331. if ( mGlowIntensity.red > 0 ||
  1332. mGlowIntensity.green > 0 ||
  1333. mGlowIntensity.blue > 0 )
  1334. {
  1335. ambient *= mGlowIntensity;
  1336. }
  1337. // Setup render state
  1338. if (mDefaultSB.isNull())
  1339. {
  1340. GFXStateBlockDesc desc;
  1341. desc.zWriteEnable = false;
  1342. desc.setAlphaTest(true, GFXCmpGreaterEqual, 1);
  1343. desc.setBlend(true, GFXBlendSrcAlpha, GFXBlendInvSrcAlpha);
  1344. mDefaultSB = GFX->createStateBlock(desc);
  1345. desc.samplersDefined = true;
  1346. desc.samplers[0].textureColorOp = GFXTOPModulate;
  1347. desc.samplers[0].colorArg1 = GFXTATexture;
  1348. desc.samplers[0].colorArg2 = GFXTADiffuse;
  1349. desc.samplers[0].alphaOp = GFXTOPSelectARG1;
  1350. desc.samplers[0].alphaArg1 = GFXTATexture;
  1351. desc.samplers[1].textureColorOp = GFXTOPDisable;
  1352. desc.samplers[1].alphaOp = GFXTOPDisable;
  1353. mDistantSB = GFX->createStateBlock(desc);
  1354. }
  1355. GFX->setStateBlock(mDefaultSB);
  1356. // Everything is rendered from these buffers.
  1357. GFX->setPrimitiveBuffer(mRainIB);
  1358. GFX->setVertexBuffer(mRainVB);
  1359. // Set the constants used by the shaders.
  1360. if (mDropShader)
  1361. {
  1362. Point2F fadeStartEnd( mFadeDistance, mFadeDistanceEnd );
  1363. mDropShaderConsts->setSafe(mDropShaderModelViewSC, proj);
  1364. mDropShaderConsts->setSafe(mDropShaderFadeStartEndSC, fadeStartEnd);
  1365. mDropShaderConsts->setSafe(mDropShaderCameraPosSC, camPos);
  1366. mDropShaderConsts->setSafe(mDropShaderAmbientSC, Point3F(ambient.red, ambient.green, ambient.blue));
  1367. }
  1368. if (mSplashShader)
  1369. {
  1370. Point2F fadeStartEnd( mFadeDistance, mFadeDistanceEnd );
  1371. mSplashShaderConsts->setSafe(mSplashShaderModelViewSC, proj);
  1372. mSplashShaderConsts->setSafe(mSplashShaderFadeStartEndSC, fadeStartEnd);
  1373. mSplashShaderConsts->setSafe(mSplashShaderCameraPosSC, camPos);
  1374. mSplashShaderConsts->setSafe(mSplashShaderAmbientSC, Point3F(ambient.red, ambient.green, ambient.blue));
  1375. }
  1376. // Time to render the drops...
  1377. const Raindrop *curr = mDropHead;
  1378. U32 vertCount = 0;
  1379. GFX->setTexture(0, mDropHandle);
  1380. // Use the shader or setup the pipeline
  1381. // for fixed function rendering.
  1382. if (mDropShader)
  1383. {
  1384. GFX->setShader( mDropShader );
  1385. GFX->setShaderConstBuffer( mDropShaderConsts );
  1386. }
  1387. else
  1388. {
  1389. GFX->setupGenericShaders(GFXDevice::GSTexture);
  1390. // We don't support distance fade or lighting without shaders.
  1391. GFX->setStateBlock(mDistantSB);
  1392. }
  1393. while (curr)
  1394. {
  1395. // Skip ones that are not drops (hit something and
  1396. // may have been converted into a splash) or they
  1397. // are behind the camera.
  1398. if (!curr->valid || !curr->toRender)
  1399. {
  1400. curr = curr->next;
  1401. continue;
  1402. }
  1403. pos = curr->renderPosition;
  1404. // two forms of billboards - true billboards (which we set
  1405. // above outside this loop) or axis-aligned with velocity
  1406. // (this codeblock) the axis-aligned billboards are aligned
  1407. // with the velocity of the raindrop, and tilted slightly
  1408. // towards the camera
  1409. if (!useBillboards)
  1410. {
  1411. orthoDir = camPos - pos;
  1412. distance = orthoDir.len();
  1413. // Inline the normalize so we don't
  1414. // calculate the ortho len twice.
  1415. if (distance > 0.0)
  1416. orthoDir *= 1.0f / distance;
  1417. else
  1418. orthoDir.set( 0, 0, 1 );
  1419. velocity = windVel / curr->mass;
  1420. // We do not optimize this for the "still" case
  1421. // because its not a typical scenario.
  1422. if (mRotateWithCamVel)
  1423. velocity -= camVel / (distance > 2.0f ? distance : 2.0f) * 0.3f;
  1424. velocity.z -= curr->velocity;
  1425. velocity.normalize();
  1426. right = mCross(-velocity, orthoDir);
  1427. right.normalize();
  1428. up = mCross(orthoDir, right) * 0.5 - velocity * 0.5;
  1429. up.normalize();
  1430. right *= dropSize;
  1431. up *= dropSize;
  1432. rightUp = right + up;
  1433. leftUp = -right + up;
  1434. }
  1435. // Do we need to relock the buffer?
  1436. if ( !vertPtr )
  1437. vertPtr = mRainVB.lock();
  1438. if(!vertPtr) return;
  1439. // Set the proper texture coords... (it's fun!)
  1440. tc = &mTexCoords[4*curr->texCoordIndex];
  1441. vertPtr->point = pos + leftUp;
  1442. vertPtr->texCoord = *tc;
  1443. tc++;
  1444. vertPtr++;
  1445. vertPtr->point = pos + rightUp;
  1446. vertPtr->texCoord = *tc;
  1447. tc++;
  1448. vertPtr++;
  1449. vertPtr->point = pos - leftUp;
  1450. vertPtr->texCoord = *tc;
  1451. tc++;
  1452. vertPtr++;
  1453. vertPtr->point = pos - rightUp;
  1454. vertPtr->texCoord = *tc;
  1455. tc++;
  1456. vertPtr++;
  1457. // Do we need to render to clear the buffer?
  1458. vertCount += 4;
  1459. if ( (vertCount + 4) >= mRainVB->mNumVerts ) {
  1460. mRainVB.unlock();
  1461. GFX->drawIndexedPrimitive(GFXTriangleList, 0, 0, vertCount, 0, vertCount / 2);
  1462. vertPtr = NULL;
  1463. vertCount = 0;
  1464. }
  1465. curr = curr->next;
  1466. }
  1467. // Do we have stuff left to render?
  1468. if ( vertCount > 0 ) {
  1469. mRainVB.unlock();
  1470. GFX->drawIndexedPrimitive(GFXTriangleList, 0, 0, vertCount, 0, vertCount / 2);
  1471. vertCount = 0;
  1472. vertPtr = NULL;
  1473. }
  1474. // Setup the billboard for the splashes.
  1475. MatrixF camMat = state->getCameraTransform();
  1476. camMat.inverse();
  1477. camMat.getRow(0, &right);
  1478. camMat.getRow(2, &up);
  1479. if (!mFollowCam)
  1480. {
  1481. mWorldToObj.mulV(right);
  1482. mWorldToObj.mulV(up);
  1483. }
  1484. right.normalize();
  1485. up.normalize();
  1486. right *= mSplashSize;
  1487. up *= mSplashSize;
  1488. rightUp = right + up;
  1489. leftUp = -right + up;
  1490. // Render the visible splashes.
  1491. curr = mSplashHead;
  1492. GFX->setTexture(0, mSplashHandle);
  1493. if (mSplashShader)
  1494. {
  1495. GFX->setShader( mSplashShader );
  1496. GFX->setShaderConstBuffer(mSplashShaderConsts);
  1497. }
  1498. else
  1499. GFX->setupGenericShaders(GFXDevice::GSTexture);
  1500. while (curr)
  1501. {
  1502. if (!curr->toRender)
  1503. {
  1504. curr = curr->nextSplashDrop;
  1505. continue;
  1506. }
  1507. pos = curr->hitPos;
  1508. tc = &mSplashCoords[4*curr->texCoordIndex];
  1509. // Do we need to relock the buffer?
  1510. if ( !vertPtr )
  1511. vertPtr = mRainVB.lock();
  1512. if(!vertPtr) return;
  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. }