2
0

precipitation.cpp 55 KB

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