fxFoliageReplicator.cpp 74 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818
  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. // Written by Melvyn May, Started on 4th August 2002.
  23. //
  24. // "My code is written for the Torque community, so do your worst with it,
  25. // just don't rip-it-off and call it your own without even thanking me".
  26. //
  27. // - Melv.
  28. //
  29. //
  30. // Conversion to TSE By Brian "bzztbomb" Richardson 9/2005
  31. // This was a neat piece of code! Thanks Melv!
  32. // I've switched this to use one large indexed primitive buffer. All animation
  33. // is then done in the vertex shader. This means we have a static vertex/primitive
  34. // buffer that never changes! How spiff! Because of this, the culling code was
  35. // changed to render out full quadtree nodes, we don't try to cull each individual
  36. // node ourselves anymore. This means to get good performance, you probably need to do the
  37. // following:
  38. // 1. If it's a small area to cover, turn off culling completely.
  39. // 2. You want to tune the parameters to make sure there are a lot of billboards within
  40. // each quadrant.
  41. //
  42. // POTENTIAL TODO LIST:
  43. // TODO: Clamp item alpha to fog alpha
  44. #include "platform/platform.h"
  45. #include "T3D/fx/fxFoliageReplicator.h"
  46. #include "gfx/gfxDevice.h"
  47. #include "gfx/primBuilder.h" // Used for debug / mission edit rendering
  48. #include "console/consoleTypes.h"
  49. #include "core/stream/bitStream.h"
  50. #include "math/mRandom.h"
  51. #include "math/mathIO.h"
  52. #include "console/simBase.h"
  53. #include "scene/sceneManager.h"
  54. #include "renderInstance/renderPassManager.h"
  55. #include "scene/sceneRenderState.h"
  56. #include "sim/netConnection.h"
  57. #include "materials/shaderData.h"
  58. #include "console/engineAPI.h"
  59. const U32 AlphaTexLen = 1024;
  60. GFXImplementVertexFormat( GFXVertexFoliage )
  61. {
  62. addElement( "POSITION", GFXDeclType_Float3 );
  63. addElement( "NORMAL", GFXDeclType_Float3 );
  64. addElement( "TEXCOORD", GFXDeclType_Float2, 0 );
  65. addElement( "TEXCOORD", GFXDeclType_Float2, 1 );
  66. }
  67. //------------------------------------------------------------------------------
  68. //
  69. // Put the function in /example/common/editor/ObjectBuilderGui.gui [around line 458] ...
  70. //
  71. // function ObjectBuilderGui::buildfxFoliageReplicator(%this)
  72. // {
  73. // %this.className = "fxFoliageReplicator";
  74. // %this.process();
  75. // }
  76. //
  77. //------------------------------------------------------------------------------
  78. //
  79. // Put this in /example/common/editor/EditorGui.cs in [function Creator::init( %this )]
  80. //
  81. // %Environment_Item[8] = "fxFoliageReplicator"; <-- ADD THIS.
  82. //
  83. //------------------------------------------------------------------------------
  84. //
  85. // Put this in /example/common/client/missionDownload.cs in [function clientCmdMissionStartPhase3(%seq,%missionName)] (line 65)
  86. // after codeline 'onPhase2Complete();'.
  87. //
  88. // StartFoliageReplication();
  89. //
  90. //------------------------------------------------------------------------------
  91. //
  92. // Put this in /engine/console/simBase.h (around line 509) in
  93. //
  94. // namespace Sim
  95. // {
  96. // DeclareNamedSet(fxFoliageSet) <-- ADD THIS (Note no semi-colon).
  97. //
  98. //------------------------------------------------------------------------------
  99. //
  100. // Put this in /engine/console/simBase.cc (around line 19) in
  101. //
  102. // ImplementNamedSet(fxFoliageSet) <-- ADD THIS (Note no semi-colon).
  103. //
  104. //------------------------------------------------------------------------------
  105. //
  106. // Put this in /engine/console/simManager.cc [function void init()] (around line 269).
  107. //
  108. // namespace Sim
  109. // {
  110. // InstantiateNamedSet(fxFoliageSet); <-- ADD THIS (Including Semi-colon).
  111. //
  112. //------------------------------------------------------------------------------
  113. extern bool gEditingMission;
  114. //------------------------------------------------------------------------------
  115. IMPLEMENT_CO_NETOBJECT_V1(fxFoliageReplicator);
  116. ConsoleDocClass( fxFoliageReplicator,
  117. "@brief An emitter to replicate fxFoliageItem objects across an area.\n"
  118. "@ingroup Foliage\n"
  119. );
  120. //------------------------------------------------------------------------------
  121. //
  122. // Trig Table Lookups.
  123. //
  124. //------------------------------------------------------------------------------
  125. const F32 PeriodLen = (F32) 2.0f * (F32) M_PI;
  126. const F32 PeriodLenMinus = (F32) (2.0f * M_PI) - 0.01f;
  127. //------------------------------------------------------------------------------
  128. //
  129. // Class: fxFoliageRenderList
  130. //
  131. //------------------------------------------------------------------------------
  132. void fxFoliageRenderList::SetupClipPlanes( SceneRenderState* state, const F32 farClipPlane )
  133. {
  134. const F32 nearPlane = state->getNearPlane();
  135. const F32 farPlane = farClipPlane;
  136. const Frustum& frustum = state->getFrustum();
  137. // [rene, 23-Feb-11] Why isn't this preserving the ortho state of the original frustum?
  138. mFrustum.set( false,//zoneState.frustum.isOrtho(),
  139. frustum.getNearLeft(),
  140. frustum.getNearRight(),
  141. frustum.getNearTop(),
  142. frustum.getNearBottom(),
  143. nearPlane,
  144. farPlane,
  145. frustum.getTransform()
  146. );
  147. mBox = mFrustum.getBounds();
  148. }
  149. //------------------------------------------------------------------------------
  150. inline void fxFoliageRenderList::DrawQuadBox(const Box3F& QuadBox, const ColorF Colour)
  151. {
  152. // Define our debug box.
  153. static Point3F BoxPnts[] = {
  154. Point3F(0,0,0),
  155. Point3F(0,0,1),
  156. Point3F(0,1,0),
  157. Point3F(0,1,1),
  158. Point3F(1,0,0),
  159. Point3F(1,0,1),
  160. Point3F(1,1,0),
  161. Point3F(1,1,1)
  162. };
  163. static U32 BoxVerts[][4] = {
  164. {0,2,3,1}, // -x
  165. {7,6,4,5}, // +x
  166. {0,1,5,4}, // -y
  167. {3,2,6,7}, // +y
  168. {0,4,6,2}, // -z
  169. {3,7,5,1} // +z
  170. };
  171. // Project our Box Points.
  172. Point3F ProjectionPoints[8];
  173. for( U32 i=0; i<8; i++ )
  174. {
  175. ProjectionPoints[i].set(BoxPnts[i].x ? QuadBox.maxExtents.x : QuadBox.minExtents.x,
  176. BoxPnts[i].y ? QuadBox.maxExtents.y : QuadBox.minExtents.y,
  177. BoxPnts[i].z ? (mHeightLerp * QuadBox.maxExtents.z) + (1-mHeightLerp) * QuadBox.minExtents.z : QuadBox.minExtents.z);
  178. }
  179. PrimBuild::color(Colour);
  180. // Draw the Box.
  181. for(U32 x = 0; x < 6; x++)
  182. {
  183. // Draw a line-loop.
  184. PrimBuild::begin(GFXLineStrip, 5);
  185. for(U32 y = 0; y < 4; y++)
  186. {
  187. PrimBuild::vertex3f(ProjectionPoints[BoxVerts[x][y]].x,
  188. ProjectionPoints[BoxVerts[x][y]].y,
  189. ProjectionPoints[BoxVerts[x][y]].z);
  190. }
  191. PrimBuild::vertex3f(ProjectionPoints[BoxVerts[x][0]].x,
  192. ProjectionPoints[BoxVerts[x][0]].y,
  193. ProjectionPoints[BoxVerts[x][0]].z);
  194. PrimBuild::end();
  195. }
  196. }
  197. //------------------------------------------------------------------------------
  198. bool fxFoliageRenderList::IsQuadrantVisible(const Box3F VisBox, const MatrixF& RenderTransform)
  199. {
  200. // Can we trivially accept the visible box?
  201. if ( !mFrustum.isCulled( VisBox ) )
  202. return true;
  203. // Not visible.
  204. return false;
  205. }
  206. //------------------------------------------------------------------------------
  207. //
  208. // Class: fxFoliageCulledList
  209. //
  210. //------------------------------------------------------------------------------
  211. fxFoliageCulledList::fxFoliageCulledList(Box3F SearchBox, fxFoliageCulledList* InVec)
  212. {
  213. // Find the Candidates.
  214. FindCandidates(SearchBox, InVec);
  215. }
  216. //------------------------------------------------------------------------------
  217. void fxFoliageCulledList::FindCandidates(Box3F SearchBox, fxFoliageCulledList* InVec)
  218. {
  219. // Search the Culled List.
  220. for (U32 i = 0; i < InVec->GetListCount(); i++)
  221. {
  222. // Is this Box overlapping our search box?
  223. if (SearchBox.isOverlapped(InVec->GetElement(i)->FoliageBox))
  224. {
  225. // Yes, so add it to our culled list.
  226. mCulledObjectSet.push_back(InVec->GetElement(i));
  227. }
  228. }
  229. }
  230. //------------------------------------------------------------------------------
  231. //
  232. // Class: fxFoliageReplicator
  233. //
  234. //------------------------------------------------------------------------------
  235. fxFoliageReplicator::fxFoliageReplicator()
  236. {
  237. // Setup NetObject.
  238. mTypeMask |= StaticObjectType;
  239. mNetFlags.set(Ghostable | ScopeAlways);
  240. // Reset Client Replication Started.
  241. mClientReplicationStarted = false;
  242. // Reset Foliage Count.
  243. mCurrentFoliageCount = 0;
  244. // Reset Creation Area Angle Animation.
  245. mCreationAreaAngle = 0;
  246. // Reset Last Render Time.
  247. mLastRenderTime = 0;
  248. // Reset Foliage Nodes.
  249. mPotentialFoliageNodes = 0;
  250. // Reset Billboards Acquired.
  251. mBillboardsAcquired = 0;
  252. // Reset Frame Serial ID.
  253. mFrameSerialID = 0;
  254. mAlphaLookup = NULL;
  255. mDirty = true;
  256. mFoliageShaderProjectionSC = NULL;
  257. mFoliageShaderWorldSC = NULL;
  258. mFoliageShaderGlobalSwayPhaseSC = NULL;
  259. mFoliageShaderSwayMagnitudeSideSC = NULL;
  260. mFoliageShaderSwayMagnitudeFrontSC = NULL;
  261. mFoliageShaderGlobalLightPhaseSC = NULL;
  262. mFoliageShaderLuminanceMagnitudeSC = NULL;
  263. mFoliageShaderLuminanceMidpointSC = NULL;
  264. mFoliageShaderDistanceRangeSC = NULL;
  265. mFoliageShaderCameraPosSC = NULL;
  266. mFoliageShaderTrueBillboardSC = NULL;
  267. mFoliageShaderGroundAlphaSC = NULL;
  268. mFoliageShaderAmbientColorSC = NULL;
  269. mShaderData = NULL;
  270. }
  271. //------------------------------------------------------------------------------
  272. fxFoliageReplicator::~fxFoliageReplicator()
  273. {
  274. if (mAlphaLookup)
  275. delete mAlphaLookup;
  276. mPlacementSB = NULL;
  277. }
  278. //------------------------------------------------------------------------------
  279. void fxFoliageReplicator::initPersistFields()
  280. {
  281. // Add out own persistent fields.
  282. addGroup( "Debugging" ); // MM: Added Group Header.
  283. addField( "UseDebugInfo", TypeBool, Offset( mFieldData.mUseDebugInfo, fxFoliageReplicator ), "Culling bins are drawn when set to true." );
  284. addField( "DebugBoxHeight", TypeF32, Offset( mFieldData.mDebugBoxHeight, fxFoliageReplicator ), "Height multiplier for drawn culling bins.");
  285. addField( "HideFoliage", TypeBool, Offset( mFieldData.mHideFoliage, fxFoliageReplicator ), "Foliage is hidden when set to true." );
  286. addField( "ShowPlacementArea", TypeBool, Offset( mFieldData.mShowPlacementArea, fxFoliageReplicator ), "Draw placement rings when set to true." );
  287. addField( "PlacementAreaHeight", TypeS32, Offset( mFieldData.mPlacementBandHeight, fxFoliageReplicator ), "Height of the placement ring in world units." );
  288. addField( "PlacementColour", TypeColorF, Offset( mFieldData.mPlaceAreaColour, fxFoliageReplicator ), "Color of the placement ring." );
  289. endGroup( "Debugging" ); // MM: Added Group Footer.
  290. addGroup( "Media" ); // MM: Added Group Header.
  291. addField( "Seed", TypeS32, Offset( mFieldData.mSeed, fxFoliageReplicator ), "Random seed for foliage placement." );
  292. addField( "FoliageFile", TypeFilename, Offset( mFieldData.mFoliageFile, fxFoliageReplicator ), "Image file for the foliage texture." );
  293. addField( "FoliageCount", TypeS32, Offset( mFieldData.mFoliageCount, fxFoliageReplicator ), "Maximum foliage instance count." );
  294. addField( "FoliageRetries", TypeS32, Offset( mFieldData.mFoliageRetries, fxFoliageReplicator ), "Number of times to try placing a foliage instance before giving up." );
  295. endGroup( "Media" ); // MM: Added Group Footer.
  296. addGroup( "Area" ); // MM: Added Group Header.
  297. addField( "InnerRadiusX", TypeS32, Offset( mFieldData.mInnerRadiusX, fxFoliageReplicator ), "Placement area inner radius on the X axis" );
  298. addField( "InnerRadiusY", TypeS32, Offset( mFieldData.mInnerRadiusY, fxFoliageReplicator ), "Placement area inner radius on the Y axis" );
  299. addField( "OuterRadiusX", TypeS32, Offset( mFieldData.mOuterRadiusX, fxFoliageReplicator ), "Placement area outer radius on the X axis" );
  300. addField( "OuterRadiusY", TypeS32, Offset( mFieldData.mOuterRadiusY, fxFoliageReplicator ), "Placement area outer radius on the Y axis" );
  301. endGroup( "Area" ); // MM: Added Group Footer.
  302. addGroup( "Dimensions" ); // MM: Added Group Header.
  303. addField( "MinWidth", TypeF32, Offset( mFieldData.mMinWidth, fxFoliageReplicator ), "Minimum width of foliage billboards" );
  304. addField( "MaxWidth", TypeF32, Offset( mFieldData.mMaxWidth, fxFoliageReplicator ), "Maximum width of foliage billboards" );
  305. addField( "MinHeight", TypeF32, Offset( mFieldData.mMinHeight, fxFoliageReplicator ), "Minimum height of foliage billboards" );
  306. addField( "MaxHeight", TypeF32, Offset( mFieldData.mMaxHeight, fxFoliageReplicator ), "Maximum height of foliage billboards" );
  307. addField( "FixAspectRatio", TypeBool, Offset( mFieldData.mFixAspectRatio, fxFoliageReplicator ), "Maintain aspect ratio of image if true. This option ignores MaxWidth." );
  308. addField( "FixSizeToMax", TypeBool, Offset( mFieldData.mFixSizeToMax, fxFoliageReplicator ), "Use only MaxWidth and MaxHeight for billboard size. Ignores MinWidth and MinHeight." );
  309. addField( "OffsetZ", TypeF32, Offset( mFieldData.mOffsetZ, fxFoliageReplicator ), "Offset billboards by this amount vertically." );
  310. addField( "RandomFlip", TypeBool, Offset( mFieldData.mRandomFlip, fxFoliageReplicator ), "Randomly flip billboards left-to-right." );
  311. addField( "UseTrueBillboards", TypeBool, Offset( mFieldData.mUseTrueBillboards, fxFoliageReplicator ), "Use camera facing billboards ( including the z axis )." );
  312. endGroup( "Dimensions" ); // MM: Added Group Footer.
  313. addGroup( "Culling" ); // MM: Added Group Header.
  314. addField( "UseCulling", TypeBool, Offset( mFieldData.mUseCulling, fxFoliageReplicator ), "Use culling bins when enabled." );
  315. addField( "CullResolution", TypeS32, Offset( mFieldData.mCullResolution, fxFoliageReplicator ), "Minimum size of culling bins. Must be >= 8 and <= OuterRadius." );
  316. addField( "ViewDistance", TypeF32, Offset( mFieldData.mViewDistance, fxFoliageReplicator ), "Maximum distance from camera where foliage appears." );
  317. addField( "ViewClosest", TypeF32, Offset( mFieldData.mViewClosest, fxFoliageReplicator ), "Minimum distance from camera where foliage appears." );
  318. addField( "FadeInRegion", TypeF32, Offset( mFieldData.mFadeInRegion, fxFoliageReplicator ), "Region beyond ViewDistance where foliage fades in/out." );
  319. addField( "FadeOutRegion", TypeF32, Offset( mFieldData.mFadeOutRegion, fxFoliageReplicator ), "Region before ViewClosest where foliage fades in/out." );
  320. addField( "AlphaCutoff", TypeF32, Offset( mFieldData.mAlphaCutoff, fxFoliageReplicator ), "Minimum alpha value allowed on foliage instances." );
  321. addField( "GroundAlpha", TypeF32, Offset( mFieldData.mGroundAlpha, fxFoliageReplicator ), "Alpha of the foliage at ground level. 0 = transparent, 1 = opaque." );
  322. endGroup( "Culling" ); // MM: Added Group Footer.
  323. addGroup( "Animation" ); // MM: Added Group Header.
  324. addField( "SwayOn", TypeBool, Offset( mFieldData.mSwayOn, fxFoliageReplicator ), "Foliage should sway randomly when true." );
  325. addField( "SwaySync", TypeBool, Offset( mFieldData.mSwaySync, fxFoliageReplicator ), "Foliage instances should sway together when true and SwayOn is enabled." );
  326. addField( "SwayMagSide", TypeF32, Offset( mFieldData.mSwayMagnitudeSide, fxFoliageReplicator ), "Left-to-right sway magnitude." );
  327. addField( "SwayMagFront", TypeF32, Offset( mFieldData.mSwayMagnitudeFront, fxFoliageReplicator ), "Front-to-back sway magnitude." );
  328. addField( "MinSwayTime", TypeF32, Offset( mFieldData.mMinSwayTime, fxFoliageReplicator ), "Minumum sway cycle time in seconds." );
  329. addField( "MaxSwayTime", TypeF32, Offset( mFieldData.mMaxSwayTime, fxFoliageReplicator ), "Maximum sway cycle time in seconds." );
  330. endGroup( "Animation" ); // MM: Added Group Footer.
  331. addGroup( "Lighting" ); // MM: Added Group Header.
  332. addField( "LightOn", TypeBool, Offset( mFieldData.mLightOn, fxFoliageReplicator ), "Foliage should be illuminated with changing lights when true." );
  333. addField( "LightSync", TypeBool, Offset( mFieldData.mLightSync, fxFoliageReplicator ), "Foliage instances have the same lighting when set and LightOn is set." );
  334. addField( "MinLuminance", TypeF32, Offset( mFieldData.mMinLuminance, fxFoliageReplicator ), "Minimum luminance for foliage instances." );
  335. addField( "MaxLuminance", TypeF32, Offset( mFieldData.mMaxLuminance, fxFoliageReplicator ), "Maximum luminance for foliage instances." );
  336. addField( "LightTime", TypeF32, Offset( mFieldData.mLightTime, fxFoliageReplicator ), "Time before foliage illumination cycle repeats." );
  337. endGroup( "Lighting" ); // MM: Added Group Footer.
  338. addGroup( "Restrictions" ); // MM: Added Group Header.
  339. addField( "AllowOnTerrain", TypeBool, Offset( mFieldData.mAllowOnTerrain, fxFoliageReplicator ), "Foliage will be placed on terrain when set." );
  340. addField( "AllowOnInteriors", TypeBool, Offset( mFieldData.mAllowOnInteriors, fxFoliageReplicator ), "Foliage will be placed on InteriorInstances when set." );
  341. addField( "AllowOnStatics", TypeBool, Offset( mFieldData.mAllowStatics, fxFoliageReplicator ), "Foliage will be placed on Static shapes when set." );
  342. addField( "AllowOnWater", TypeBool, Offset( mFieldData.mAllowOnWater, fxFoliageReplicator ), "Foliage will be placed on/under water when set." );
  343. addField( "AllowWaterSurface", TypeBool, Offset( mFieldData.mAllowWaterSurface, fxFoliageReplicator ), "Foliage will be placed on water when set. Requires AllowOnWater." );
  344. addField( "AllowedTerrainSlope", TypeS32, Offset( mFieldData.mAllowedTerrainSlope, fxFoliageReplicator ), "Maximum surface angle allowed for foliage instances." );
  345. endGroup( "Restrictions" ); // MM: Added Group Footer.
  346. // Initialise parents' persistent fields.
  347. Parent::initPersistFields();
  348. }
  349. //------------------------------------------------------------------------------
  350. void fxFoliageReplicator::CreateFoliage(void)
  351. {
  352. F32 HypX, HypY;
  353. F32 Angle;
  354. U32 RelocationRetry;
  355. Point3F FoliagePosition;
  356. Point3F FoliageStart;
  357. Point3F FoliageEnd;
  358. Point3F FoliageScale;
  359. bool CollisionResult;
  360. RayInfo RayEvent;
  361. // Let's get a minimum bounding volume.
  362. Point3F MinPoint( -0.5, -0.5, -0.5 );
  363. Point3F MaxPoint( 0.5, 0.5, 0.5 );
  364. // Check Host.
  365. AssertFatal(isClientObject(), "Trying to create Foliage on Server, this is bad!")
  366. // Cannot continue without Foliage Texture!
  367. if (dStrlen(mFieldData.mFoliageFile) == 0)
  368. return;
  369. // Check that we can position somewhere!
  370. if (!( mFieldData.mAllowOnTerrain ||
  371. mFieldData.mAllowOnInteriors ||
  372. mFieldData.mAllowStatics ||
  373. mFieldData.mAllowOnWater))
  374. {
  375. // Problem ...
  376. Con::warnf(ConsoleLogEntry::General, "fxFoliageReplicator - Could not place Foliage, All alloweds are off!");
  377. // Return here.
  378. return;
  379. }
  380. // Destroy Foliage if we've already got some.
  381. if (mCurrentFoliageCount != 0) DestroyFoliage();
  382. // Inform the user if culling has been disabled!
  383. if (!mFieldData.mUseCulling)
  384. {
  385. // Console Output.
  386. Con::printf("fxFoliageReplicator - Culling has been disabled!");
  387. }
  388. // ----------------------------------------------------------------------------------------------------------------------
  389. // > Calculate the Potential Foliage Nodes Required to achieve the selected culling resolution.
  390. // > Populate Quad-tree structure to depth determined by culling resolution.
  391. //
  392. // A little explanation is called for here ...
  393. //
  394. // The approach to this problem has been choosen to make it *much* easier for
  395. // the user to control the quad-tree culling resolution. The user enters a single
  396. // world-space value 'mCullResolution' which controls the highest resolution at
  397. // which the replicator will check visibility culling.
  398. //
  399. // example: If 'mCullResolution' is 32 and the size of the replicated area is 128 radius
  400. // (256 diameter) then this results in the replicator creating a quad-tree where
  401. // there are 256/32 = 8x8 blocks. Each of these can be checked to see if they
  402. // reside within the viewing frustum and if not then they get culled therefore
  403. // removing the need to parse all the billboards that occcupy that region.
  404. // Most of the time you will get better than this as the culling algorithm will
  405. // check the culling pyramid from the top to bottom e.g. the follow 'blocks'
  406. // will be checked:-
  407. //
  408. // 1 x 256 x 256 (All of replicated area)
  409. // 4 x 128 x 128 (4 corners of above)
  410. // 16 x 64 x 64 (16 x 4 corners of above)
  411. // etc.
  412. //
  413. //
  414. // 1. First-up, the replicator needs to create a fixed-list of quad-tree nodes to work with.
  415. //
  416. // To calculate this we take the largest outer-radius value set in the replicator and
  417. // calculate how many quad-tree levels are required to achieve the selected 'mCullResolution'.
  418. // One of the initial problems is that the replicator has seperate radii values for X & Y.
  419. // This can lead to a culling resolution smaller in one axis than the other if there is a
  420. // difference between the Outer-Radii. Unfortunately, we just live with this as there is
  421. // not much we can do here if we still want to allow the user to have this kind of
  422. // elliptical placement control.
  423. //
  424. // To calculate the number of nodes needed we using the following equation:-
  425. //
  426. // Note:- We are changing the Logarithmic bases from 10 -> 2 ... grrrr!
  427. //
  428. // Cr = mCullResolution
  429. // Rs = Maximum Radii Diameter
  430. //
  431. //
  432. // ( Log10( Rs / Cr ) )
  433. // int ( ---------------- + 0.5 )
  434. // ( Log10( 2 ) )
  435. //
  436. // ---------|
  437. // |
  438. // | n
  439. // / 4
  440. // /
  441. // ---------|
  442. // n = 0
  443. //
  444. //
  445. // So basically we calculate the number of blocks in 1D at the highest resolution, then
  446. // calculate the inverse exponential (base 2 - 1D) to achieve that quantity of blocks.
  447. // We round that upto the next highest integer = e. We then sum 4 to the power 0->e
  448. // which gives us the correct number of nodes required. e is also stored as the starting
  449. // level value for populating the quad-tree (see 3. below).
  450. //
  451. // 2. We then proceed to calculate the billboard positions as normal and calculate and assign
  452. // each billboard a basic volume (rather than treat each as a point). We need to take into
  453. // account possible front/back swaying as well as the basic plane dimensions here.
  454. // When all the billboards have been choosen we then proceed to populate the quad-tree.
  455. //
  456. // 3. To populate the quad-tree we start with a box which completely encapsulates the volume
  457. // occupied by all the billboards and enter into a recursive procedure to process that node.
  458. // Processing this node involves splitting it into quadrants in X/Y untouched (for now).
  459. // We then find candidate billboards with each of these quadrants searching using the
  460. // current subset of shapes from the parent (this reduces the searching to a minimum and
  461. // is very efficient).
  462. //
  463. // If a quadrant does not enclose any billboards then the node is dropped otherwise it
  464. // is processed again using the same procedure.
  465. //
  466. // This happens until we have recursed through the maximum number of levels as calculated
  467. // using the summation max (see equation above). When level 0 is reached, the current list
  468. // of enclosed objects is stored within the node (for the rendering algorithm).
  469. //
  470. // 4. When this is complete we have finished here. The next stage is when rendering takes place.
  471. // An algorithm steps through the quad-tree from the top and does visibility culling on
  472. // each box (with respect to the viewing frustum) and culls as appropriate. If the box is
  473. // visible then the next level is checked until we reach level 0 where the node contains
  474. // a complete subset of billboards enclosed by the visible box.
  475. //
  476. //
  477. // Using the above algorithm we can now generate *massive* quantities of billboards and (using the
  478. // appropriate 'mCullResolution') only visible blocks of billboards will be processed.
  479. //
  480. // - Melv.
  481. //
  482. // ----------------------------------------------------------------------------------------------------------------------
  483. // ----------------------------------------------------------------------------------------------------------------------
  484. // Step 1.
  485. // ----------------------------------------------------------------------------------------------------------------------
  486. // Calculate the maximum dimension.
  487. F32 MaxDimension = 2.0f * ( (mFieldData.mOuterRadiusX > mFieldData.mOuterRadiusY) ? mFieldData.mOuterRadiusX : mFieldData.mOuterRadiusY );
  488. // Let's check that our cull resolution is not greater than half our maximum dimension (and less than 1).
  489. if (mFieldData.mCullResolution > (MaxDimension/2) || mFieldData.mCullResolution < 8)
  490. {
  491. // Problem ...
  492. Con::warnf(ConsoleLogEntry::General, "fxFoliageReplicator - Could create Foliage, invalid Culling Resolution!");
  493. Con::warnf(ConsoleLogEntry::General, "fxFoliageReplicator - Culling Resolution *must* be >=8 or <= %0.2f!", (MaxDimension/2));
  494. // Return here.
  495. return;
  496. }
  497. // Take first Timestamp.
  498. F32 mStartCreationTime = (F32) Platform::getRealMilliseconds();
  499. // Calculate the quad-tree levels needed for selected 'mCullResolution'.
  500. mQuadTreeLevels = (U32)(mCeil(mLog( MaxDimension / mFieldData.mCullResolution ) / mLog( 2.0f )));
  501. // Calculate the number of potential nodes required.
  502. mPotentialFoliageNodes = 0;
  503. for (U32 n = 0; n <= mQuadTreeLevels; n++)
  504. mPotentialFoliageNodes += (U32)(mCeil(mPow(4.0f, (F32) n))); // Ceil to be safe!
  505. // ----------------------------------------------------------------------------------------------------------------------
  506. // Step 2.
  507. // ----------------------------------------------------------------------------------------------------------------------
  508. // Set Seed.
  509. RandomGen.setSeed(mFieldData.mSeed);
  510. // Add Foliage.
  511. for (U32 idx = 0; idx < mFieldData.mFoliageCount; idx++)
  512. {
  513. fxFoliageItem* pFoliageItem;
  514. Point3F FoliageOffsetPos;
  515. // Reset Relocation Retry.
  516. RelocationRetry = mFieldData.mFoliageRetries;
  517. // Find it a home ...
  518. do
  519. {
  520. // Get the fxFoliageReplicator Position.
  521. FoliagePosition = getPosition();
  522. // Calculate a random offset
  523. HypX = RandomGen.randF((F32) mFieldData.mInnerRadiusX < mFieldData.mOuterRadiusX ? mFieldData.mInnerRadiusX : mFieldData.mOuterRadiusX, (F32) mFieldData.mOuterRadiusX);
  524. HypY = RandomGen.randF((F32) mFieldData.mInnerRadiusY < mFieldData.mOuterRadiusY ? mFieldData.mInnerRadiusY : mFieldData.mOuterRadiusY, (F32) mFieldData.mOuterRadiusY);
  525. Angle = RandomGen.randF(0, (F32) M_2PI);
  526. // Calcualte the new position.
  527. FoliagePosition.x += HypX * mCos(Angle);
  528. FoliagePosition.y += HypY * mSin(Angle);
  529. // Initialise RayCast Search Start/End Positions.
  530. FoliageStart = FoliageEnd = FoliagePosition;
  531. FoliageStart.z = 2000.f;
  532. FoliageEnd.z= -2000.f;
  533. // Perform Ray Cast Collision on Client.
  534. CollisionResult = gClientContainer.castRay( FoliageStart, FoliageEnd, FXFOLIAGEREPLICATOR_COLLISION_MASK, &RayEvent);
  535. // Did we hit anything?
  536. if (CollisionResult)
  537. {
  538. // For now, let's pretend we didn't get a collision.
  539. CollisionResult = false;
  540. // Yes, so get it's type.
  541. U32 CollisionType = RayEvent.object->getTypeMask();
  542. // Check Illegal Placements, fail if we hit a disallowed type.
  543. if (((CollisionType & TerrainObjectType) && !mFieldData.mAllowOnTerrain) ||
  544. ((CollisionType & InteriorObjectType) && !mFieldData.mAllowOnInteriors) ||
  545. ((CollisionType & StaticShapeObjectType ) && !mFieldData.mAllowStatics) ||
  546. ((CollisionType & WaterObjectType) && !mFieldData.mAllowOnWater) ) continue;
  547. // If we collided with water and are not allowing on the water surface then let's find the
  548. // terrain underneath and pass this on as the original collision else fail.
  549. if ((CollisionType & WaterObjectType) && !mFieldData.mAllowWaterSurface &&
  550. !gClientContainer.castRay( FoliageStart, FoliageEnd, FXFOLIAGEREPLICATOR_NOWATER_COLLISION_MASK, &RayEvent)) continue;
  551. // We passed with flying colour so carry on.
  552. CollisionResult = true;
  553. }
  554. // Invalidate if we are below Allowed Terrain Angle.
  555. if (RayEvent.normal.z < mSin(mDegToRad(90.0f-mFieldData.mAllowedTerrainSlope))) CollisionResult = false;
  556. // Wait until we get a collision.
  557. } while(!CollisionResult && --RelocationRetry);
  558. // Check for Relocation Problem.
  559. if (RelocationRetry > 0)
  560. {
  561. // Adjust Impact point.
  562. RayEvent.point.z += mFieldData.mOffsetZ;
  563. // Set New Position.
  564. FoliagePosition = RayEvent.point;
  565. }
  566. else
  567. {
  568. // Warning.
  569. Con::warnf(ConsoleLogEntry::General, "fxFoliageReplicator - Could not find satisfactory position for Foliage!");
  570. // Skip to next.
  571. continue;
  572. }
  573. // Monitor the total volume.
  574. FoliageOffsetPos = FoliagePosition - getPosition();
  575. MinPoint.setMin(FoliageOffsetPos);
  576. MaxPoint.setMax(FoliageOffsetPos);
  577. // Create our Foliage Item.
  578. pFoliageItem = new fxFoliageItem;
  579. // Reset Frame Serial.
  580. pFoliageItem->LastFrameSerialID = 0;
  581. // Reset Transform.
  582. pFoliageItem->Transform.identity();
  583. // Set Position.
  584. pFoliageItem->Transform.setColumn(3, FoliagePosition);
  585. // Are we fixing size @ max?
  586. if (mFieldData.mFixSizeToMax)
  587. {
  588. // Yes, so set height maximum height.
  589. pFoliageItem->Height = mFieldData.mMaxHeight;
  590. // Is the Aspect Ratio Fixed?
  591. if (mFieldData.mFixAspectRatio)
  592. // Yes, so lock to height.
  593. pFoliageItem->Width = pFoliageItem->Height;
  594. else
  595. // No, so set width to maximum width.
  596. pFoliageItem->Width = mFieldData.mMaxWidth;
  597. }
  598. else
  599. {
  600. // No, so choose a new Scale.
  601. pFoliageItem->Height = RandomGen.randF(mFieldData.mMinHeight, mFieldData.mMaxHeight);
  602. // Is the Aspect Ratio Fixed?
  603. if (mFieldData.mFixAspectRatio)
  604. // Yes, so lock to height.
  605. pFoliageItem->Width = pFoliageItem->Height;
  606. else
  607. // No, so choose a random width.
  608. pFoliageItem->Width = RandomGen.randF(mFieldData.mMinWidth, mFieldData.mMaxWidth);
  609. }
  610. // Are we randomly flipping horizontally?
  611. if (mFieldData.mRandomFlip)
  612. // Yes, so choose a random flip for this object.
  613. pFoliageItem->Flipped = (RandomGen.randF(0, 1000) < 500.0f) ? false : true;
  614. else
  615. // No, so turn-off flipping.
  616. pFoliageItem->Flipped = false;
  617. // Calculate Foliage Item World Box.
  618. // NOTE:- We generate a psuedo-volume here. It's basically the volume to which the
  619. // plane can move and this includes swaying!
  620. //
  621. // Is Sway On?
  622. if (mFieldData.mSwayOn)
  623. {
  624. // Yes, so take swaying into account...
  625. pFoliageItem->FoliageBox.minExtents = FoliagePosition +
  626. Point3F(-pFoliageItem->Width / 2.0f - mFieldData.mSwayMagnitudeSide,
  627. -0.5f - mFieldData.mSwayMagnitudeFront,
  628. pFoliageItem->Height );
  629. pFoliageItem->FoliageBox.maxExtents = FoliagePosition +
  630. Point3F(+pFoliageItem->Width / 2.0f + mFieldData.mSwayMagnitudeSide,
  631. +0.5f + mFieldData.mSwayMagnitudeFront,
  632. pFoliageItem->Height );
  633. }
  634. else
  635. {
  636. // No, so give it a minimum volume...
  637. pFoliageItem->FoliageBox.minExtents = FoliagePosition +
  638. Point3F(-pFoliageItem->Width / 2.0f,
  639. -0.5f,
  640. pFoliageItem->Height );
  641. pFoliageItem->FoliageBox.maxExtents = FoliagePosition +
  642. Point3F(+pFoliageItem->Width / 2.0f,
  643. +0.5f,
  644. pFoliageItem->Height );
  645. }
  646. // Store Shape in Replicated Shapes Vector.
  647. mReplicatedFoliage.push_back(pFoliageItem);
  648. // Increase Foliage Count.
  649. mCurrentFoliageCount++;
  650. }
  651. // Is Lighting On?
  652. if (mFieldData.mLightOn)
  653. {
  654. // Yes, so reset Global Light phase.
  655. mGlobalLightPhase = 0.0f;
  656. // Set Global Light Time Ratio.
  657. mGlobalLightTimeRatio = PeriodLenMinus / mFieldData.mLightTime;
  658. // Yes, so step through Foliage.
  659. for (U32 idx = 0; idx < mCurrentFoliageCount; idx++)
  660. {
  661. fxFoliageItem* pFoliageItem;
  662. // Fetch the Foliage Item.
  663. pFoliageItem = mReplicatedFoliage[idx];
  664. // Do we have an item?
  665. if (pFoliageItem)
  666. {
  667. // Yes, so are lights syncronised?
  668. if (mFieldData.mLightSync)
  669. {
  670. pFoliageItem->LightTimeRatio = 1.0f;
  671. pFoliageItem->LightPhase = 0.0f;
  672. }
  673. else
  674. {
  675. // No, so choose a random Light phase.
  676. pFoliageItem->LightPhase = RandomGen.randF(0, PeriodLenMinus);
  677. // Set Light Time Ratio.
  678. pFoliageItem->LightTimeRatio = PeriodLenMinus / mFieldData.mLightTime;
  679. }
  680. }
  681. }
  682. }
  683. // Is Swaying Enabled?
  684. if (mFieldData.mSwayOn)
  685. {
  686. // Yes, so reset Global Sway phase.
  687. mGlobalSwayPhase = 0.0f;
  688. // Always set Global Sway Time Ratio.
  689. mGlobalSwayTimeRatio = PeriodLenMinus / RandomGen.randF(mFieldData.mMinSwayTime, mFieldData.mMaxSwayTime);
  690. // Yes, so step through Foliage.
  691. for (U32 idx = 0; idx < mCurrentFoliageCount; idx++)
  692. {
  693. fxFoliageItem* pFoliageItem;
  694. // Fetch the Foliage Item.
  695. pFoliageItem = mReplicatedFoliage[idx];
  696. // Do we have an item?
  697. if (pFoliageItem)
  698. {
  699. // Are we using Sway Sync?
  700. if (mFieldData.mSwaySync)
  701. {
  702. pFoliageItem->SwayPhase = 0;
  703. pFoliageItem->SwayTimeRatio = mGlobalSwayTimeRatio;
  704. }
  705. else
  706. {
  707. // No, so choose a random Sway phase.
  708. pFoliageItem->SwayPhase = RandomGen.randF(0, PeriodLenMinus);
  709. // Set to random Sway Time.
  710. pFoliageItem->SwayTimeRatio = PeriodLenMinus / RandomGen.randF(mFieldData.mMinSwayTime, mFieldData.mMaxSwayTime);
  711. }
  712. }
  713. }
  714. }
  715. // Update our Object Volume.
  716. mObjBox.minExtents.set(MinPoint);
  717. mObjBox.maxExtents.set(MaxPoint);
  718. setTransform(mObjToWorld);
  719. // ----------------------------------------------------------------------------------------------------------------------
  720. // Step 3.
  721. // ----------------------------------------------------------------------------------------------------------------------
  722. // Reset Next Allocated Node to Stack base.
  723. mNextAllocatedNodeIdx = 0;
  724. // Allocate a new Node.
  725. fxFoliageQuadrantNode* pNewNode = new fxFoliageQuadrantNode;
  726. // Store it in the Quad-tree.
  727. mFoliageQuadTree.push_back(pNewNode);
  728. // Populate Initial Node.
  729. //
  730. // Set Start Level.
  731. pNewNode->Level = mQuadTreeLevels;
  732. // Calculate Total Foliage Area.
  733. pNewNode->QuadrantBox = getWorldBox();
  734. // Reset Quadrant child nodes.
  735. pNewNode->QuadrantChildNode[0] =
  736. pNewNode->QuadrantChildNode[1] =
  737. pNewNode->QuadrantChildNode[2] =
  738. pNewNode->QuadrantChildNode[3] = NULL;
  739. // Create our initial cull list with *all* billboards into.
  740. fxFoliageCulledList CullList;
  741. CullList.mCulledObjectSet = mReplicatedFoliage;
  742. // Move to next node Index.
  743. mNextAllocatedNodeIdx++;
  744. // Let's start this thing going by recursing it's children.
  745. ProcessNodeChildren(pNewNode, &CullList);
  746. // Calculate Elapsed Time and take new Timestamp.
  747. F32 ElapsedTime = (Platform::getRealMilliseconds() - mStartCreationTime) * 0.001f;
  748. // Console Output.
  749. Con::printf("fxFoliageReplicator - Lev: %d PotNodes: %d Used: %d Objs: %d Time: %0.4fs.",
  750. mQuadTreeLevels,
  751. mPotentialFoliageNodes,
  752. mNextAllocatedNodeIdx-1,
  753. mBillboardsAcquired,
  754. ElapsedTime);
  755. // Dump (*very*) approximate allocated memory.
  756. F32 MemoryAllocated = (F32) ((mNextAllocatedNodeIdx-1) * sizeof(fxFoliageQuadrantNode));
  757. MemoryAllocated += mCurrentFoliageCount * sizeof(fxFoliageItem);
  758. MemoryAllocated += mCurrentFoliageCount * sizeof(fxFoliageItem*);
  759. Con::printf("fxFoliageReplicator - Approx. %0.2fMb allocated.", MemoryAllocated / 1048576.0f);
  760. // ----------------------------------------------------------------------------------------------------------------------
  761. SetupBuffers();
  762. // Take first Timestamp.
  763. mLastRenderTime = Platform::getVirtualMilliseconds();
  764. }
  765. void fxFoliageReplicator::SetupShader()
  766. {
  767. if ( !mShaderData )
  768. {
  769. if ( !Sim::findObject( "fxFoliageReplicatorShader", mShaderData ) )
  770. {
  771. Con::errorf( "fxFoliageReplicator::SetupShader - could not find ShaderData named fxFoliageReplicatorShader" );
  772. return;
  773. }
  774. }
  775. Vector<GFXShaderMacro> macros;
  776. if ( mFieldData.mUseTrueBillboards )
  777. macros.push_back( GFXShaderMacro( "TRUE_BILLBOARD" ) );
  778. mShader = mShaderData->getShader( macros );
  779. if ( !mShader )
  780. return;
  781. mFoliageShaderConsts = mShader->allocConstBuffer();
  782. mFoliageShaderProjectionSC = mShader->getShaderConstHandle( "$projection" );
  783. mFoliageShaderWorldSC = mShader->getShaderConstHandle( "$world" );
  784. mFoliageShaderGlobalSwayPhaseSC = mShader->getShaderConstHandle( "$GlobalSwayPhase" );
  785. mFoliageShaderSwayMagnitudeSideSC = mShader->getShaderConstHandle( "$SwayMagnitudeSide" );
  786. mFoliageShaderSwayMagnitudeFrontSC = mShader->getShaderConstHandle( "$SwayMagnitudeFront" );
  787. mFoliageShaderGlobalLightPhaseSC = mShader->getShaderConstHandle( "$GlobalLightPhase" );
  788. mFoliageShaderLuminanceMagnitudeSC = mShader->getShaderConstHandle( "$LuminanceMagnitude" );
  789. mFoliageShaderLuminanceMidpointSC = mShader->getShaderConstHandle( "$LuminanceMidpoint" );
  790. mFoliageShaderDistanceRangeSC = mShader->getShaderConstHandle( "$DistanceRange" );
  791. mFoliageShaderCameraPosSC = mShader->getShaderConstHandle( "$CameraPos" );
  792. mFoliageShaderTrueBillboardSC = mShader->getShaderConstHandle( "$TrueBillboard" );
  793. mFoliageShaderGroundAlphaSC = mShader->getShaderConstHandle( "$groundAlpha" );
  794. mFoliageShaderAmbientColorSC = mShader->getShaderConstHandle( "$ambient" );
  795. mDiffuseTextureSC = mShader->getShaderConstHandle( "$diffuseMap" );
  796. mAlphaMapTextureSC = mShader->getShaderConstHandle( "$alphaMap" );
  797. }
  798. // Ok, what we do is let the older code setup the FoliageItem list and the QuadTree.
  799. // Then we build the Vertex and Primitive buffers here. It would probably be
  800. // slightly more memory efficient to build the buffers directly, but we
  801. // want to sort the items within the buffer by the quadtreenodes
  802. void fxFoliageReplicator::SetupBuffers()
  803. {
  804. // Following two arrays are used to build the vertex and primitive buffers.
  805. Point3F basePoints[8];
  806. basePoints[0] = Point3F(-0.5f, 0.0f, 1.0f);
  807. basePoints[1] = Point3F(-0.5f, 0.0f, 0.0f);
  808. basePoints[2] = Point3F(0.5f, 0.0f, 0.0f);
  809. basePoints[3] = Point3F(0.5f, 0.0f, 1.0f);
  810. Point2F texCoords[4];
  811. texCoords[0] = Point2F(0.0, 0.0);
  812. texCoords[1] = Point2F(0.0, 1.0);
  813. texCoords[2] = Point2F(1.0, 1.0);
  814. texCoords[3] = Point2F(1.0, 0.0);
  815. // Init our Primitive Buffer
  816. U32 indexSize = mFieldData.mFoliageCount * 6;
  817. U16* indices = new U16[indexSize];
  818. // Two triangles per particle
  819. for (U16 i = 0; i < mFieldData.mFoliageCount; i++) {
  820. U16* idx = &indices[i*6]; // hey, no offset math below, neat
  821. U16 vertOffset = i*4;
  822. idx[0] = vertOffset + 0;
  823. idx[1] = vertOffset + 1;
  824. idx[2] = vertOffset + 2;
  825. idx[3] = vertOffset + 2;
  826. idx[4] = vertOffset + 3;
  827. idx[5] = vertOffset + 0;
  828. }
  829. // Init the prim buffer and copy our indexes over
  830. U16 *ibIndices;
  831. mPrimBuffer.set(GFX, indexSize, 0, GFXBufferTypeStatic);
  832. mPrimBuffer.lock(&ibIndices);
  833. dMemcpy(ibIndices, indices, indexSize * sizeof(U16));
  834. mPrimBuffer.unlock();
  835. delete[] indices;
  836. // Now, let's init the vertex buffer
  837. U32 currPrimitiveStartIndex = 0;
  838. mVertexBuffer.set(GFX, mFieldData.mFoliageCount * 4, GFXBufferTypeStatic);
  839. mVertexBuffer.lock();
  840. U32 idx = 0;
  841. for (S32 qtIdx = 0; qtIdx < mFoliageQuadTree.size(); qtIdx++) {
  842. fxFoliageQuadrantNode* quadNode = mFoliageQuadTree[qtIdx];
  843. if (quadNode->Level == 0) {
  844. quadNode->startIndex = currPrimitiveStartIndex;
  845. quadNode->primitiveCount = 0;
  846. // Ok, there should be data in here!
  847. for (S32 i = 0; i < quadNode->RenderList.size(); i++) {
  848. fxFoliageItem* pFoliageItem = quadNode->RenderList[i];
  849. if (pFoliageItem->LastFrameSerialID == 0) {
  850. pFoliageItem->LastFrameSerialID++;
  851. // Dump it into the vertex buffer
  852. for (U32 vertIndex = 0; vertIndex < 4; vertIndex++) {
  853. GFXVertexFoliage *vert = &mVertexBuffer[(idx*4) + vertIndex];
  854. // This is the position of the billboard.
  855. vert->point = pFoliageItem->Transform.getPosition();
  856. // Normal contains the point of the billboard (except for the y component, see below)
  857. vert->normal = basePoints[vertIndex];
  858. vert->normal.x *= pFoliageItem->Width;
  859. vert->normal.z *= pFoliageItem->Height;
  860. // Handle texture coordinates
  861. vert->texCoord = texCoords[vertIndex];
  862. if (pFoliageItem->Flipped)
  863. vert->texCoord.x = 1.0f - vert->texCoord.x;
  864. // Handle sway. Sway is stored in a texture coord. The x coordinate is the sway phase multiplier,
  865. // the y coordinate determines if this vertex actually sways or not.
  866. if ((vertIndex == 0) || (vertIndex == 3)) {
  867. vert->texCoord2.set(pFoliageItem->SwayTimeRatio / mGlobalSwayTimeRatio, 1.0f);
  868. } else {
  869. vert->texCoord2.set(0.0f, 0.0f);
  870. }
  871. // Handle lighting, lighting happens at the same time as global so this is just an offset.
  872. vert->normal.y = pFoliageItem->LightPhase;
  873. }
  874. idx++;
  875. quadNode->primitiveCount += 2;
  876. currPrimitiveStartIndex += 6;
  877. }
  878. }
  879. }
  880. }
  881. mVertexBuffer.unlock();
  882. DestroyFoliageItems();
  883. }
  884. //------------------------------------------------------------------------------
  885. Box3F fxFoliageReplicator::FetchQuadrant(Box3F Box, U32 Quadrant)
  886. {
  887. Box3F QuadrantBox;
  888. // Select Quadrant.
  889. switch(Quadrant)
  890. {
  891. // UL.
  892. case 0:
  893. QuadrantBox.minExtents = Box.minExtents + Point3F(0, Box.len_y()/2, 0);
  894. QuadrantBox.maxExtents = QuadrantBox.minExtents + Point3F(Box.len_x()/2, Box.len_y()/2, Box.len_z());
  895. break;
  896. // UR.
  897. case 1:
  898. QuadrantBox.minExtents = Box.minExtents + Point3F(Box.len_x()/2, Box.len_y()/2, 0);
  899. QuadrantBox.maxExtents = QuadrantBox.minExtents + Point3F(Box.len_x()/2, Box.len_y()/2, Box.len_z());
  900. break;
  901. // LL.
  902. case 2:
  903. QuadrantBox.minExtents = Box.minExtents;
  904. QuadrantBox.maxExtents = QuadrantBox.minExtents + Point3F(Box.len_x()/2, Box.len_y()/2, Box.len_z());
  905. break;
  906. // LR.
  907. case 3:
  908. QuadrantBox.minExtents = Box.minExtents + Point3F(Box.len_x()/2, 0, 0);
  909. QuadrantBox.maxExtents = QuadrantBox.minExtents + Point3F(Box.len_x()/2, Box.len_y()/2, Box.len_z());
  910. break;
  911. default:
  912. return Box;
  913. }
  914. return QuadrantBox;
  915. }
  916. //------------------------------------------------------------------------------
  917. void fxFoliageReplicator::ProcessNodeChildren(fxFoliageQuadrantNode* pParentNode, fxFoliageCulledList* pCullList)
  918. {
  919. // ---------------------------------------------------------------
  920. // Split Node into Quadrants and Process each.
  921. // ---------------------------------------------------------------
  922. // Process All Quadrants (UL/UR/LL/LR).
  923. for (U32 q = 0; q < 4; q++)
  924. ProcessQuadrant(pParentNode, pCullList, q);
  925. }
  926. //------------------------------------------------------------------------------
  927. void fxFoliageReplicator::ProcessQuadrant(fxFoliageQuadrantNode* pParentNode, fxFoliageCulledList* pCullList, U32 Quadrant)
  928. {
  929. // Fetch Quadrant Box.
  930. const Box3F QuadrantBox = FetchQuadrant(pParentNode->QuadrantBox, Quadrant);
  931. // Create our new Cull List.
  932. fxFoliageCulledList CullList(QuadrantBox, pCullList);
  933. // Did we get any objects?
  934. if (CullList.GetListCount() > 0)
  935. {
  936. // Yes, so allocate a new Node.
  937. fxFoliageQuadrantNode* pNewNode = new fxFoliageQuadrantNode;
  938. // Store it in the Quad-tree.
  939. mFoliageQuadTree.push_back(pNewNode);
  940. // Move to next node Index.
  941. mNextAllocatedNodeIdx++;
  942. // Populate Quadrant Node.
  943. //
  944. // Next Sub-level.
  945. pNewNode->Level = pParentNode->Level - 1;
  946. // Calculate Quadrant Box.
  947. pNewNode->QuadrantBox = QuadrantBox;
  948. // Reset Child Nodes.
  949. pNewNode->QuadrantChildNode[0] =
  950. pNewNode->QuadrantChildNode[1] =
  951. pNewNode->QuadrantChildNode[2] =
  952. pNewNode->QuadrantChildNode[3] = NULL;
  953. // Put a reference in parent.
  954. pParentNode->QuadrantChildNode[Quadrant] = pNewNode;
  955. // If we're not at sub-level 0 then process this nodes children.
  956. if (pNewNode->Level != 0) ProcessNodeChildren(pNewNode, &CullList);
  957. // If we've reached sub-level 0 then store Cull List (for rendering).
  958. if (pNewNode->Level == 0)
  959. {
  960. // Store the render list from our culled object set.
  961. pNewNode->RenderList = CullList.mCulledObjectSet;
  962. // Keep track of the total billboard acquired.
  963. mBillboardsAcquired += CullList.GetListCount();
  964. }
  965. }
  966. }
  967. //------------------------------------------------------------------------------
  968. void fxFoliageReplicator::SyncFoliageReplicators(void)
  969. {
  970. // Check Host.
  971. AssertFatal(isServerObject(), "We *MUST* be on server when Synchronising Foliage!")
  972. // Find the Replicator Set.
  973. SimSet *fxFoliageSet = dynamic_cast<SimSet*>(Sim::findObject("fxFoliageSet"));
  974. // Return if Error.
  975. if (!fxFoliageSet)
  976. {
  977. // Console Warning.
  978. Con::warnf("fxFoliageReplicator - Cannot locate the 'fxFoliageSet', this is bad!");
  979. // Return here.
  980. return;
  981. }
  982. // Parse Replication Object(s).
  983. for (SimSetIterator itr(fxFoliageSet); *itr; ++itr)
  984. {
  985. // Fetch the Replicator Object.
  986. fxFoliageReplicator* Replicator = static_cast<fxFoliageReplicator*>(*itr);
  987. // Set Foliage Replication Mask.
  988. if (Replicator->isServerObject())
  989. {
  990. Con::printf("fxFoliageReplicator - Restarting fxFoliageReplicator Object...");
  991. Replicator->setMaskBits(FoliageReplicationMask);
  992. }
  993. }
  994. // Info ...
  995. Con::printf("fxFoliageReplicator - Client Foliage Sync has completed.");
  996. }
  997. //------------------------------------------------------------------------------
  998. // Lets chill our memory requirements out a little
  999. void fxFoliageReplicator::DestroyFoliageItems()
  1000. {
  1001. // Remove shapes.
  1002. for (S32 idx = 0; idx < mReplicatedFoliage.size(); idx++)
  1003. {
  1004. fxFoliageItem* pFoliageItem;
  1005. // Fetch the Foliage Item.
  1006. pFoliageItem = mReplicatedFoliage[idx];
  1007. // Delete Shape.
  1008. if (pFoliageItem) delete pFoliageItem;
  1009. }
  1010. // Clear the Replicated Foliage Vector.
  1011. mReplicatedFoliage.clear();
  1012. // Clear out old references also
  1013. for (S32 qtIdx = 0; qtIdx < mFoliageQuadTree.size(); qtIdx++) {
  1014. fxFoliageQuadrantNode* quadNode = mFoliageQuadTree[qtIdx];
  1015. if (quadNode->Level == 0) {
  1016. quadNode->RenderList.clear();
  1017. }
  1018. }
  1019. }
  1020. void fxFoliageReplicator::DestroyFoliage(void)
  1021. {
  1022. // Check Host.
  1023. AssertFatal(isClientObject(), "Trying to destroy Foliage on Server, this is bad!")
  1024. // Destroy Quad-tree.
  1025. mPotentialFoliageNodes = 0;
  1026. // Reset Billboards Acquired.
  1027. mBillboardsAcquired = 0;
  1028. // Finish if we didn't create any shapes.
  1029. if (mCurrentFoliageCount == 0) return;
  1030. DestroyFoliageItems();
  1031. // Let's remove the Quad-Tree allocations.
  1032. for ( Vector<fxFoliageQuadrantNode*>::iterator QuadNodeItr = mFoliageQuadTree.begin();
  1033. QuadNodeItr != mFoliageQuadTree.end();
  1034. QuadNodeItr++ )
  1035. {
  1036. // Remove the node.
  1037. delete *QuadNodeItr;
  1038. }
  1039. // Clear the Foliage Quad-Tree Vector.
  1040. mFoliageQuadTree.clear();
  1041. // Clear the Frustum Render Set Vector.
  1042. mFrustumRenderSet.mVisObjectSet.clear();
  1043. // Reset Foliage Count.
  1044. mCurrentFoliageCount = 0;
  1045. }
  1046. //------------------------------------------------------------------------------
  1047. void fxFoliageReplicator::StartUp(void)
  1048. {
  1049. // Flag, Client Replication Started.
  1050. mClientReplicationStarted = true;
  1051. // Create foliage on Client.
  1052. if (isClientObject()) CreateFoliage();
  1053. }
  1054. //------------------------------------------------------------------------------
  1055. bool fxFoliageReplicator::onAdd()
  1056. {
  1057. if(!Parent::onAdd()) return(false);
  1058. // Add the Replicator to the Replicator Set.
  1059. dynamic_cast<SimSet*>(Sim::findObject("fxFoliageSet"))->addObject(this);
  1060. // Set Default Object Box.
  1061. mObjBox.minExtents.set( -0.5, -0.5, -0.5 );
  1062. mObjBox.maxExtents.set( 0.5, 0.5, 0.5 );
  1063. resetWorldBox();
  1064. setRenderTransform(mObjToWorld);
  1065. // Add to Scene.
  1066. addToScene();
  1067. // Are we on the client?
  1068. if ( isClientObject() )
  1069. {
  1070. // Yes, so load foliage texture.
  1071. if( mFieldData.mFoliageFile != NULL && dStrlen(mFieldData.mFoliageFile) > 0 )
  1072. mFieldData.mFoliageTexture = GFXTexHandle( mFieldData.mFoliageFile, &GFXDefaultStaticDiffuseProfile, avar("%s() - mFieldData.mFoliageTexture (line %d)", __FUNCTION__, __LINE__) );
  1073. if ((GFXTextureObject*) mFieldData.mFoliageTexture == NULL)
  1074. Con::printf("fxFoliageReplicator: %s is an invalid or missing foliage texture file.", mFieldData.mFoliageFile);
  1075. mAlphaLookup = new GBitmap(AlphaTexLen, 1);
  1076. computeAlphaTex();
  1077. // Register for notification when GhostAlways objects are done loading
  1078. NetConnection::smGhostAlwaysDone.notify( this, &fxFoliageReplicator::onGhostAlwaysDone );
  1079. SetupShader();
  1080. }
  1081. // Return OK.
  1082. return(true);
  1083. }
  1084. //------------------------------------------------------------------------------
  1085. void fxFoliageReplicator::onRemove()
  1086. {
  1087. // Remove the Replicator from the Replicator Set.
  1088. dynamic_cast<SimSet*>(Sim::findObject("fxFoliageSet"))->removeObject(this);
  1089. NetConnection::smGhostAlwaysDone.remove( this, &fxFoliageReplicator::onGhostAlwaysDone );
  1090. // Remove from Scene.
  1091. removeFromScene();
  1092. // Are we on the Client?
  1093. if (isClientObject())
  1094. {
  1095. // Yes, so destroy Foliage.
  1096. DestroyFoliage();
  1097. // Remove Texture.
  1098. mFieldData.mFoliageTexture = NULL;
  1099. mShader = NULL;
  1100. }
  1101. // Do Parent.
  1102. Parent::onRemove();
  1103. }
  1104. //------------------------------------------------------------------------------
  1105. void fxFoliageReplicator::onGhostAlwaysDone()
  1106. {
  1107. if ( isClientObject() )
  1108. CreateFoliage();
  1109. }
  1110. //------------------------------------------------------------------------------
  1111. void fxFoliageReplicator::inspectPostApply()
  1112. {
  1113. // Set Parent.
  1114. Parent::inspectPostApply();
  1115. // Set Foliage Replication Mask (this object only).
  1116. setMaskBits(FoliageReplicationMask);
  1117. mDirty = true;
  1118. }
  1119. //------------------------------------------------------------------------------
  1120. DefineEngineFunction(StartFoliageReplication, void,(),, "Activates the foliage replicator.\n"
  1121. "@tsexample\n"
  1122. "// Call the function\n"
  1123. "StartFoliageReplication();\n"
  1124. "@endtsexample\n"
  1125. "@ingroup Foliage")
  1126. {
  1127. // Find the Replicator Set.
  1128. SimSet *fxFoliageSet = dynamic_cast<SimSet*>(Sim::findObject("fxFoliageSet"));
  1129. // Return if Error.
  1130. if (!fxFoliageSet)
  1131. {
  1132. // Console Warning.
  1133. Con::warnf("fxFoliageReplicator - Cannot locate the 'fxFoliageSet', this is bad!");
  1134. // Return here.
  1135. return;
  1136. }
  1137. // Parse Replication Object(s).
  1138. U32 startupCount = 0;
  1139. for (SimSetIterator itr(fxFoliageSet); *itr; ++itr)
  1140. {
  1141. // Fetch the Replicator Object.
  1142. fxFoliageReplicator* Replicator = static_cast<fxFoliageReplicator*>(*itr);
  1143. // Start Client Objects Only.
  1144. if (Replicator->isClientObject())
  1145. {
  1146. Replicator->StartUp();
  1147. startupCount++;
  1148. }
  1149. }
  1150. // Info ...
  1151. Con::printf("fxFoliageReplicator - replicated client foliage for %d objects", startupCount);
  1152. }
  1153. //------------------------------------------------------------------------------
  1154. void fxFoliageReplicator::prepRenderImage( SceneRenderState* state )
  1155. {
  1156. ObjectRenderInst *ri = state->getRenderPass()->allocInst<ObjectRenderInst>();
  1157. ri->renderDelegate.bind(this, &fxFoliageReplicator::renderObject);
  1158. ri->type = RenderPassManager::RIT_Foliage;
  1159. state->getRenderPass()->addInst( ri );
  1160. }
  1161. //
  1162. // RENDERING
  1163. //
  1164. void fxFoliageReplicator::computeAlphaTex()
  1165. {
  1166. // Distances used in alpha
  1167. const F32 ClippedViewDistance = mFieldData.mViewDistance;
  1168. const F32 MaximumViewDistance = ClippedViewDistance + mFieldData.mFadeInRegion;
  1169. // This is used for the alpha computation in the shader.
  1170. for (U32 i = 0; i < AlphaTexLen; i++) {
  1171. F32 Distance = ((float) i / (float) AlphaTexLen) * MaximumViewDistance;
  1172. F32 ItemAlpha = 1.0f;
  1173. // Are we fading out?
  1174. if (Distance < mFieldData.mViewClosest)
  1175. {
  1176. // Yes, so set fade-out.
  1177. ItemAlpha = 1.0f - ((mFieldData.mViewClosest - Distance) * mFadeOutGradient);
  1178. }
  1179. // No, so are we fading in?
  1180. else if (Distance > ClippedViewDistance)
  1181. {
  1182. // Yes, so set fade-in
  1183. ItemAlpha = 1.0f - ((Distance - ClippedViewDistance) * mFadeInGradient);
  1184. }
  1185. // Set texture info
  1186. ColorI c((U8) (255.0f * ItemAlpha), 0, 0);
  1187. mAlphaLookup->setColor(i, 0, c);
  1188. }
  1189. mAlphaTexture.set(mAlphaLookup, &GFXDefaultStaticDiffuseProfile, false, String("fxFoliage Replicator Alpha Texture") );
  1190. }
  1191. // Renders a triangle stripped oval
  1192. void fxFoliageReplicator::renderArc(const F32 fRadiusX, const F32 fRadiusY)
  1193. {
  1194. PrimBuild::begin(GFXTriangleStrip, 720);
  1195. for (U32 Angle = mCreationAreaAngle; Angle < (mCreationAreaAngle+360); Angle++)
  1196. {
  1197. F32 XPos, YPos;
  1198. // Calculate Position.
  1199. XPos = fRadiusX * mCos(mDegToRad(-(F32)Angle));
  1200. YPos = fRadiusY * mSin(mDegToRad(-(F32)Angle));
  1201. // Set Colour.
  1202. PrimBuild::color4f(mFieldData.mPlaceAreaColour.red,
  1203. mFieldData.mPlaceAreaColour.green,
  1204. mFieldData.mPlaceAreaColour.blue,
  1205. AREA_ANIMATION_ARC * (Angle-mCreationAreaAngle));
  1206. PrimBuild::vertex3f(XPos, YPos, -(F32)mFieldData.mPlacementBandHeight/2.0f);
  1207. PrimBuild::vertex3f(XPos, YPos, +(F32)mFieldData.mPlacementBandHeight/2.0f);
  1208. }
  1209. PrimBuild::end();
  1210. }
  1211. // This currently uses the primbuilder, could convert out, but why allocate the buffer if we
  1212. // never edit the misison?
  1213. void fxFoliageReplicator::renderPlacementArea(const F32 ElapsedTime)
  1214. {
  1215. if (gEditingMission && mFieldData.mShowPlacementArea)
  1216. {
  1217. GFX->pushWorldMatrix();
  1218. GFX->multWorld(getTransform());
  1219. if (!mPlacementSB)
  1220. {
  1221. GFXStateBlockDesc transparent;
  1222. transparent.setCullMode(GFXCullNone);
  1223. transparent.alphaTestEnable = true;
  1224. transparent.setZReadWrite(true);
  1225. transparent.zWriteEnable = false;
  1226. transparent.setBlend(true, GFXBlendSrcAlpha, GFXBlendInvSrcAlpha);
  1227. mPlacementSB = GFX->createStateBlock( transparent );
  1228. }
  1229. GFX->setStateBlock(mPlacementSB);
  1230. // Do we need to draw the Outer Radius?
  1231. if (mFieldData.mOuterRadiusX || mFieldData.mOuterRadiusY)
  1232. renderArc((F32) mFieldData.mOuterRadiusX, (F32) mFieldData.mOuterRadiusY);
  1233. // Inner radius?
  1234. if (mFieldData.mInnerRadiusX || mFieldData.mInnerRadiusY)
  1235. renderArc((F32) mFieldData.mInnerRadiusX, (F32) mFieldData.mInnerRadiusY);
  1236. GFX->popWorldMatrix();
  1237. mCreationAreaAngle = (U32)(mCreationAreaAngle + (1000 * ElapsedTime));
  1238. mCreationAreaAngle = mCreationAreaAngle % 360;
  1239. }
  1240. }
  1241. void fxFoliageReplicator::renderObject(ObjectRenderInst *ri, SceneRenderState *state, BaseMatInstance* overrideMat)
  1242. {
  1243. if (overrideMat)
  1244. return;
  1245. if ( !mShader )
  1246. return;
  1247. // If we're rendering and we haven't placed any foliage yet - do it.
  1248. if(!mClientReplicationStarted)
  1249. {
  1250. Con::warnf("fxFoliageReplicator::renderObject - tried to render a non replicated fxFoliageReplicator; replicating it now...");
  1251. StartUp();
  1252. }
  1253. // Calculate Elapsed Time and take new Timestamp.
  1254. S32 Time = Platform::getVirtualMilliseconds();
  1255. F32 ElapsedTime = (Time - mLastRenderTime) * 0.001f;
  1256. mLastRenderTime = Time;
  1257. renderPlacementArea(ElapsedTime);
  1258. if (mCurrentFoliageCount > 0) {
  1259. if ( mRenderSB.isNull() || mDirty)
  1260. {
  1261. mDirty = false;
  1262. GFXStateBlockDesc desc;
  1263. // Debug SB
  1264. desc.samplersDefined = true;
  1265. desc.samplers[0].textureColorOp = GFXTOPDisable;
  1266. desc.samplers[1].textureColorOp = GFXTOPDisable;
  1267. mDebugSB = GFX->createStateBlock(desc);
  1268. // Render SB
  1269. desc.samplers[0].textureColorOp = GFXTOPModulate;
  1270. desc.samplers[1].textureColorOp = GFXTOPModulate;
  1271. desc.samplers[1].addressModeU = GFXAddressClamp;
  1272. desc.samplers[1].addressModeV = GFXAddressClamp;
  1273. desc.setBlend(true, GFXBlendSrcAlpha, GFXBlendInvSrcAlpha);
  1274. desc.setAlphaTest(true, GFXCmpGreater, (U8) (255.0f * mFieldData.mAlphaCutoff));
  1275. desc.setCullMode(GFXCullNone);
  1276. mRenderSB = GFX->createStateBlock(desc);
  1277. }
  1278. if (!mFieldData.mHideFoliage) {
  1279. // Animate Global Sway Phase (Modulus).
  1280. mGlobalSwayPhase = mGlobalSwayPhase + (mGlobalSwayTimeRatio * ElapsedTime);
  1281. // Animate Global Light Phase (Modulus).
  1282. mGlobalLightPhase = mGlobalLightPhase + (mGlobalLightTimeRatio * ElapsedTime);
  1283. // Compute other light parameters
  1284. const F32 LuminanceMidPoint = (mFieldData.mMinLuminance + mFieldData.mMaxLuminance) / 2.0f;
  1285. const F32 LuminanceMagnitude = mFieldData.mMaxLuminance - LuminanceMidPoint;
  1286. // Distances used in alpha
  1287. const F32 ClippedViewDistance = mFieldData.mViewDistance;
  1288. const F32 MaximumViewDistance = ClippedViewDistance + mFieldData.mFadeInRegion;
  1289. if (mFoliageShaderConsts.isValid())
  1290. {
  1291. mFoliageShaderConsts->setSafe(mFoliageShaderGlobalSwayPhaseSC, mGlobalSwayPhase);
  1292. mFoliageShaderConsts->setSafe(mFoliageShaderSwayMagnitudeSideSC, mFieldData.mSwayMagnitudeSide);
  1293. mFoliageShaderConsts->setSafe(mFoliageShaderSwayMagnitudeFrontSC, mFieldData.mSwayMagnitudeFront);
  1294. mFoliageShaderConsts->setSafe(mFoliageShaderGlobalLightPhaseSC, mGlobalLightPhase);
  1295. mFoliageShaderConsts->setSafe(mFoliageShaderLuminanceMagnitudeSC, LuminanceMagnitude);
  1296. mFoliageShaderConsts->setSafe(mFoliageShaderLuminanceMidpointSC, LuminanceMidPoint);
  1297. // Set up our shader constants
  1298. // Projection matrix
  1299. MatrixF proj = GFX->getProjectionMatrix();
  1300. //proj.transpose();
  1301. mFoliageShaderConsts->setSafe(mFoliageShaderProjectionSC, proj);
  1302. // World transform matrix
  1303. MatrixF world = GFX->getWorldMatrix();
  1304. //world.transpose();
  1305. mFoliageShaderConsts->setSafe(mFoliageShaderWorldSC, world);
  1306. Point3F camPos = state->getCameraPosition();
  1307. mFoliageShaderConsts->setSafe(mFoliageShaderDistanceRangeSC, MaximumViewDistance);
  1308. mFoliageShaderConsts->setSafe(mFoliageShaderCameraPosSC, camPos);
  1309. mFoliageShaderConsts->setSafe(mFoliageShaderTrueBillboardSC, mFieldData.mUseTrueBillboards ? 1.0f : 0.0f );
  1310. mFoliageShaderConsts->setSafe(mFoliageShaderGroundAlphaSC, Point4F(mFieldData.mGroundAlpha, mFieldData.mGroundAlpha, mFieldData.mGroundAlpha, mFieldData.mGroundAlpha));
  1311. if (mFoliageShaderAmbientColorSC->isValid())
  1312. mFoliageShaderConsts->set(mFoliageShaderAmbientColorSC, state->getAmbientLightColor());
  1313. GFX->setShaderConstBuffer(mFoliageShaderConsts);
  1314. }
  1315. // Blend ops
  1316. // Set up our texture and color ops.
  1317. GFX->setStateBlock(mRenderSB);
  1318. GFX->setShader( mShader );
  1319. GFX->setTexture(mDiffuseTextureSC->getSamplerRegister(), mFieldData.mFoliageTexture);
  1320. // computeAlphaTex(); // Uncomment if we figure out how to clamp to fogAndHaze
  1321. GFX->setTexture(mAlphaMapTextureSC->getSamplerRegister(), mAlphaTexture);
  1322. // Setup our buffers
  1323. GFX->setVertexBuffer(mVertexBuffer);
  1324. GFX->setPrimitiveBuffer(mPrimBuffer);
  1325. // If we use culling, we're going to send chunks of our buffers to the card
  1326. if (mFieldData.mUseCulling)
  1327. {
  1328. // Setup the Clip-Planes.
  1329. F32 FarClipPlane = getMin((F32)state->getFarPlane(),
  1330. mFieldData.mViewDistance + mFieldData.mFadeInRegion);
  1331. mFrustumRenderSet.SetupClipPlanes(state, FarClipPlane);
  1332. renderQuad(mFoliageQuadTree[0], getRenderTransform(), false);
  1333. // Multipass, don't want to interrupt the vb state
  1334. if (mFieldData.mUseDebugInfo)
  1335. {
  1336. // hey man, we're done, so it doesn't matter if we kill it to render the next part
  1337. GFX->setStateBlock(mDebugSB);
  1338. renderQuad(mFoliageQuadTree[0], getRenderTransform(), true);
  1339. }
  1340. }
  1341. else
  1342. {
  1343. // Draw the whole shebang!
  1344. GFX->drawIndexedPrimitive(GFXTriangleList, 0, 0, mVertexBuffer->mNumVerts,
  1345. 0, mPrimBuffer->mIndexCount / 3);
  1346. }
  1347. }
  1348. }
  1349. }
  1350. void fxFoliageReplicator::renderQuad(fxFoliageQuadrantNode* quadNode, const MatrixF& RenderTransform, const bool UseDebug)
  1351. {
  1352. if (quadNode != NULL) {
  1353. if (mFrustumRenderSet.IsQuadrantVisible(quadNode->QuadrantBox, RenderTransform))
  1354. {
  1355. // Draw the Quad Box (Debug Only).
  1356. if (UseDebug)
  1357. mFrustumRenderSet.DrawQuadBox(quadNode->QuadrantBox, ColorF(0.0f, 1.0f, 0.1f, 1.0f));
  1358. if (quadNode->Level != 0) {
  1359. for (U32 i = 0; i < 4; i++)
  1360. renderQuad(quadNode->QuadrantChildNode[i], RenderTransform, UseDebug);
  1361. } else {
  1362. if (!UseDebug)
  1363. if(quadNode->primitiveCount)
  1364. GFX->drawIndexedPrimitive(GFXTriangleList, 0, 0, mVertexBuffer->mNumVerts,
  1365. quadNode->startIndex, quadNode->primitiveCount);
  1366. }
  1367. } else {
  1368. // Use a different color to say "I think I'm not visible!"
  1369. if (UseDebug)
  1370. mFrustumRenderSet.DrawQuadBox(quadNode->QuadrantBox, ColorF(1.0f, 0.8f, 0.1f, 1.0f));
  1371. }
  1372. }
  1373. }
  1374. //------------------------------------------------------------------------------
  1375. // NETWORK
  1376. //------------------------------------------------------------------------------
  1377. U32 fxFoliageReplicator::packUpdate(NetConnection * con, U32 mask, BitStream * stream)
  1378. {
  1379. // Pack Parent.
  1380. U32 retMask = Parent::packUpdate(con, mask, stream);
  1381. // Write Foliage Replication Flag.
  1382. if (stream->writeFlag(mask & FoliageReplicationMask))
  1383. {
  1384. stream->writeAffineTransform(mObjToWorld); // Foliage Master-Object Position.
  1385. stream->writeFlag(mFieldData.mUseDebugInfo); // Foliage Debug Information Flag.
  1386. stream->write(mFieldData.mDebugBoxHeight); // Foliage Debug Height.
  1387. stream->write(mFieldData.mSeed); // Foliage Seed.
  1388. stream->write(mFieldData.mFoliageCount); // Foliage Count.
  1389. stream->write(mFieldData.mFoliageRetries); // Foliage Retries.
  1390. stream->writeString(mFieldData.mFoliageFile); // Foliage File.
  1391. stream->write(mFieldData.mInnerRadiusX); // Foliage Inner Radius X.
  1392. stream->write(mFieldData.mInnerRadiusY); // Foliage Inner Radius Y.
  1393. stream->write(mFieldData.mOuterRadiusX); // Foliage Outer Radius X.
  1394. stream->write(mFieldData.mOuterRadiusY); // Foliage Outer Radius Y.
  1395. stream->write(mFieldData.mMinWidth); // Foliage Minimum Width.
  1396. stream->write(mFieldData.mMaxWidth); // Foliage Maximum Width.
  1397. stream->write(mFieldData.mMinHeight); // Foliage Minimum Height.
  1398. stream->write(mFieldData.mMaxHeight); // Foliage Maximum Height.
  1399. stream->write(mFieldData.mFixAspectRatio); // Foliage Fix Aspect Ratio.
  1400. stream->write(mFieldData.mFixSizeToMax); // Foliage Fix Size to Max.
  1401. stream->write(mFieldData.mOffsetZ); // Foliage Offset Z.
  1402. stream->writeFlag(mFieldData.mRandomFlip); // Foliage Random Flip.
  1403. stream->writeFlag(mFieldData.mUseTrueBillboards); // Foliage faces the camera (including z axis)
  1404. stream->write(mFieldData.mUseCulling); // Foliage Use Culling.
  1405. stream->write(mFieldData.mCullResolution); // Foliage Cull Resolution.
  1406. stream->write(mFieldData.mViewDistance); // Foliage View Distance.
  1407. stream->write(mFieldData.mViewClosest); // Foliage View Closest.
  1408. stream->write(mFieldData.mFadeInRegion); // Foliage Fade-In Region.
  1409. stream->write(mFieldData.mFadeOutRegion); // Foliage Fade-Out Region.
  1410. stream->write(mFieldData.mAlphaCutoff); // Foliage Alpha Cutoff.
  1411. stream->write(mFieldData.mGroundAlpha); // Foliage Ground Alpha.
  1412. stream->writeFlag(mFieldData.mSwayOn); // Foliage Sway On Flag.
  1413. stream->writeFlag(mFieldData.mSwaySync); // Foliage Sway Sync Flag.
  1414. stream->write(mFieldData.mSwayMagnitudeSide); // Foliage Sway Magnitude Side2Side.
  1415. stream->write(mFieldData.mSwayMagnitudeFront); // Foliage Sway Magnitude Front2Back.
  1416. stream->write(mFieldData.mMinSwayTime); // Foliage Minimum Sway Time.
  1417. stream->write(mFieldData.mMaxSwayTime); // Foliage Maximum way Time.
  1418. stream->writeFlag(mFieldData.mLightOn); // Foliage Light On Flag.
  1419. stream->writeFlag(mFieldData.mLightSync); // Foliage Light Sync
  1420. stream->write(mFieldData.mMinLuminance); // Foliage Minimum Luminance.
  1421. stream->write(mFieldData.mMaxLuminance); // Foliage Maximum Luminance.
  1422. stream->write(mFieldData.mLightTime); // Foliage Light Time.
  1423. stream->writeFlag(mFieldData.mAllowOnTerrain); // Allow on Terrain.
  1424. stream->writeFlag(mFieldData.mAllowOnInteriors); // Allow on Interiors.
  1425. stream->writeFlag(mFieldData.mAllowStatics); // Allow on Statics.
  1426. stream->writeFlag(mFieldData.mAllowOnWater); // Allow on Water.
  1427. stream->writeFlag(mFieldData.mAllowWaterSurface); // Allow on Water Surface.
  1428. stream->write(mFieldData.mAllowedTerrainSlope); // Foliage Offset Z.
  1429. stream->writeFlag(mFieldData.mHideFoliage); // Hide Foliage.
  1430. stream->writeFlag(mFieldData.mShowPlacementArea); // Show Placement Area Flag.
  1431. stream->write(mFieldData.mPlacementBandHeight); // Placement Area Height.
  1432. stream->write(mFieldData.mPlaceAreaColour); // Placement Area Colour.
  1433. }
  1434. // Were done ...
  1435. return(retMask);
  1436. }
  1437. //------------------------------------------------------------------------------
  1438. void fxFoliageReplicator::unpackUpdate(NetConnection * con, BitStream * stream)
  1439. {
  1440. // Unpack Parent.
  1441. Parent::unpackUpdate(con, stream);
  1442. // Read Replication Details.
  1443. if(stream->readFlag())
  1444. {
  1445. MatrixF ReplicatorObjectMatrix;
  1446. stream->readAffineTransform(&ReplicatorObjectMatrix); // Foliage Master Object Position.
  1447. mFieldData.mUseDebugInfo = stream->readFlag(); // Foliage Debug Information Flag.
  1448. stream->read(&mFieldData.mDebugBoxHeight); // Foliage Debug Height.
  1449. stream->read(&mFieldData.mSeed); // Foliage Seed.
  1450. stream->read(&mFieldData.mFoliageCount); // Foliage Count.
  1451. stream->read(&mFieldData.mFoliageRetries); // Foliage Retries.
  1452. mFieldData.mFoliageFile = stream->readSTString(); // Foliage File.
  1453. stream->read(&mFieldData.mInnerRadiusX); // Foliage Inner Radius X.
  1454. stream->read(&mFieldData.mInnerRadiusY); // Foliage Inner Radius Y.
  1455. stream->read(&mFieldData.mOuterRadiusX); // Foliage Outer Radius X.
  1456. stream->read(&mFieldData.mOuterRadiusY); // Foliage Outer Radius Y.
  1457. stream->read(&mFieldData.mMinWidth); // Foliage Minimum Width.
  1458. stream->read(&mFieldData.mMaxWidth); // Foliage Maximum Width.
  1459. stream->read(&mFieldData.mMinHeight); // Foliage Minimum Height.
  1460. stream->read(&mFieldData.mMaxHeight); // Foliage Maximum Height.
  1461. stream->read(&mFieldData.mFixAspectRatio); // Foliage Fix Aspect Ratio.
  1462. stream->read(&mFieldData.mFixSizeToMax); // Foliage Fix Size to Max.
  1463. stream->read(&mFieldData.mOffsetZ); // Foliage Offset Z.
  1464. mFieldData.mRandomFlip = stream->readFlag(); // Foliage Random Flip.
  1465. bool wasTrueBB = mFieldData.mUseTrueBillboards;
  1466. mFieldData.mUseTrueBillboards = stream->readFlag(); // Foliage is camera facing (including z axis).
  1467. stream->read(&mFieldData.mUseCulling); // Foliage Use Culling.
  1468. stream->read(&mFieldData.mCullResolution); // Foliage Cull Resolution.
  1469. stream->read(&mFieldData.mViewDistance); // Foliage View Distance.
  1470. stream->read(&mFieldData.mViewClosest); // Foliage View Closest.
  1471. stream->read(&mFieldData.mFadeInRegion); // Foliage Fade-In Region.
  1472. stream->read(&mFieldData.mFadeOutRegion); // Foliage Fade-Out Region.
  1473. stream->read(&mFieldData.mAlphaCutoff); // Foliage Alpha Cutoff.
  1474. stream->read(&mFieldData.mGroundAlpha); // Foliage Ground Alpha.
  1475. mFieldData.mSwayOn = stream->readFlag(); // Foliage Sway On Flag.
  1476. mFieldData.mSwaySync = stream->readFlag(); // Foliage Sway Sync Flag.
  1477. stream->read(&mFieldData.mSwayMagnitudeSide); // Foliage Sway Magnitude Side2Side.
  1478. stream->read(&mFieldData.mSwayMagnitudeFront); // Foliage Sway Magnitude Front2Back.
  1479. stream->read(&mFieldData.mMinSwayTime); // Foliage Minimum Sway Time.
  1480. stream->read(&mFieldData.mMaxSwayTime); // Foliage Maximum way Time.
  1481. mFieldData.mLightOn = stream->readFlag(); // Foliage Light On Flag.
  1482. mFieldData.mLightSync = stream->readFlag(); // Foliage Light Sync
  1483. stream->read(&mFieldData.mMinLuminance); // Foliage Minimum Luminance.
  1484. stream->read(&mFieldData.mMaxLuminance); // Foliage Maximum Luminance.
  1485. stream->read(&mFieldData.mLightTime); // Foliage Light Time.
  1486. mFieldData.mAllowOnTerrain = stream->readFlag(); // Allow on Terrain.
  1487. mFieldData.mAllowOnInteriors = stream->readFlag(); // Allow on Interiors.
  1488. mFieldData.mAllowStatics = stream->readFlag(); // Allow on Statics.
  1489. mFieldData.mAllowOnWater = stream->readFlag(); // Allow on Water.
  1490. mFieldData.mAllowWaterSurface = stream->readFlag(); // Allow on Water Surface.
  1491. stream->read(&mFieldData.mAllowedTerrainSlope); // Allowed Terrain Slope.
  1492. mFieldData.mHideFoliage = stream->readFlag(); // Hide Foliage.
  1493. mFieldData.mShowPlacementArea = stream->readFlag(); // Show Placement Area Flag.
  1494. stream->read(&mFieldData.mPlacementBandHeight); // Placement Area Height.
  1495. stream->read(&mFieldData.mPlaceAreaColour);
  1496. // Calculate Fade-In/Out Gradients.
  1497. mFadeInGradient = 1.0f / mFieldData.mFadeInRegion;
  1498. mFadeOutGradient = 1.0f / mFieldData.mFadeOutRegion;
  1499. // Set Transform.
  1500. setTransform(ReplicatorObjectMatrix);
  1501. // Load Foliage Texture on the client.
  1502. if( mFieldData.mFoliageFile != NULL && dStrlen(mFieldData.mFoliageFile) > 0 )
  1503. mFieldData.mFoliageTexture = GFXTexHandle( mFieldData.mFoliageFile, &GFXDefaultStaticDiffuseProfile, avar("%s() - mFieldData.mFoliageTexture (line %d)", __FUNCTION__, __LINE__) );
  1504. if ((GFXTextureObject*) mFieldData.mFoliageTexture == NULL)
  1505. Con::printf("fxFoliageReplicator: %s is an invalid or missing foliage texture file.", mFieldData.mFoliageFile);
  1506. // Set Quad-Tree Box Height Lerp.
  1507. mFrustumRenderSet.mHeightLerp = mFieldData.mDebugBoxHeight;
  1508. // Create Foliage (if Replication has begun).
  1509. if (mClientReplicationStarted)
  1510. {
  1511. CreateFoliage();
  1512. mDirty = true;
  1513. }
  1514. if ( isProperlyAdded() && mFieldData.mUseTrueBillboards != wasTrueBB )
  1515. SetupShader();
  1516. }
  1517. }