precipitation.cpp 56 KB

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