precipitation.cpp 56 KB

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