groundCover.cpp 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758
  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/groundCover.h"
  24. #include "core/resourceManager.h"
  25. #include "core/stream/bitStream.h"
  26. #include "console/consoleTypes.h"
  27. #include "scene/sceneRenderState.h"
  28. #include "terrain/terrData.h"
  29. #include "renderInstance/renderPassManager.h"
  30. #include "gfx/gfxDrawUtil.h"
  31. #include "gfx/primBuilder.h"
  32. #include "T3D/gameBase/gameConnection.h"
  33. #include "gfx/gfxVertexBuffer.h"
  34. #include "gfx/gfxStructs.h"
  35. #include "ts/tsShapeInstance.h"
  36. #include "lighting/lightManager.h"
  37. #include "lighting/lightInfo.h"
  38. #include "materials/shaderData.h"
  39. #include "gfx/gfxTransformSaver.h"
  40. #include "shaderGen/shaderGenVars.h"
  41. #include "materials/matTextureTarget.h"
  42. #include "gfx/util/screenspace.h"
  43. #include "materials/materialDefinition.h"
  44. #include "materials/materialManager.h"
  45. #include "materials/sceneData.h"
  46. #include "materials/materialFeatureTypes.h"
  47. #include "materials/matInstance.h"
  48. #include "renderInstance/renderDeferredMgr.h"
  49. #include "console/engineAPI.h"
  50. #include "T3D/assets/MaterialAsset.h"
  51. #include "T3D/assets/TerrainMaterialAsset.h"
  52. /// This is used for rendering ground cover billboards.
  53. GFXImplementVertexFormat( GCVertex )
  54. {
  55. addElement( "POSITION", GFXDeclType_Float3 );
  56. addElement( "NORMAL", GFXDeclType_Float3 );
  57. addElement( "COLOR", GFXDeclType_Color );
  58. addElement( "TEXCOORD", GFXDeclType_Float4, 0 );
  59. };
  60. GroundCoverShaderConstHandles::GroundCoverShaderConstHandles()
  61. : mGroundCover( NULL ),
  62. mTypeRectsSC( NULL ),
  63. mFadeSC( NULL ),
  64. mWindDirSC( NULL ),
  65. mGustInfoSC( NULL ),
  66. mTurbInfoSC( NULL ),
  67. mCamRightSC( NULL ),
  68. mCamUpSC( NULL )
  69. {
  70. }
  71. void GroundCoverShaderConstHandles::init( GFXShader *shader )
  72. {
  73. mTypeRectsSC = shader->getShaderConstHandle( "$gc_typeRects" );
  74. mFadeSC = shader->getShaderConstHandle( "$gc_fadeParams" );
  75. mWindDirSC = shader->getShaderConstHandle( "$gc_windDir" );
  76. mGustInfoSC = shader->getShaderConstHandle( "$gc_gustInfo" );
  77. mTurbInfoSC = shader->getShaderConstHandle( "$gc_turbInfo" );
  78. mCamRightSC = shader->getShaderConstHandle( "$gc_camRight" );
  79. mCamUpSC = shader->getShaderConstHandle( "$gc_camUp" );
  80. }
  81. void GroundCoverShaderConstHandles::setConsts( SceneRenderState *state, const SceneData &sgData, GFXShaderConstBuffer *buffer )
  82. {
  83. AlignedArray<Point4F> rectData( MAX_COVERTYPES, sizeof( Point4F ), (U8*)(mGroundCover->mBillboardRects), false );
  84. buffer->setSafe( mTypeRectsSC, rectData );
  85. const GroundCoverShaderConstData &data = mGroundCover->getShaderConstData();
  86. buffer->setSafe( mFadeSC, data.fadeInfo );
  87. buffer->setSafe( mWindDirSC, mGroundCover->mWindDirection );
  88. buffer->setSafe( mGustInfoSC, data.gustInfo );
  89. buffer->setSafe( mTurbInfoSC, data.turbInfo );
  90. buffer->setSafe( mCamRightSC, data.camRight );
  91. buffer->setSafe( mCamUpSC, data.camUp );
  92. }
  93. /// This defines one grid cell.
  94. class GroundCoverCell
  95. {
  96. protected:
  97. friend class GroundCover;
  98. struct Placement
  99. {
  100. Point3F point;
  101. Point3F normal;
  102. Point3F size;
  103. F32 rotation;
  104. U32 type;
  105. F32 windAmplitude;
  106. Box3F worldBox;
  107. LinearColorF lmColor;
  108. };
  109. /// This is the x,y index for this cell.
  110. Point2I mIndex;
  111. /// The worldspace bounding box this cell.
  112. Box3F mBounds;
  113. /// The worldspace bounding box of the renderable
  114. /// content within this cell.
  115. Box3F mRenderBounds;
  116. /// The instances of billboard cover elements in this cell.
  117. Vector<Placement> mBillboards;
  118. /// The instances of shape cover elements in this cell.
  119. Vector<Placement> mShapes;
  120. typedef GFXVertexBufferHandle<GCVertex> VBHandle;
  121. typedef Vector< VBHandle > VBHandleVector;
  122. /// The vertex buffers that hold all the
  123. /// prepared billboards for this cell.
  124. VBHandleVector mVBs;
  125. /// Used to mark the cell dirty and in need
  126. /// of a rebuild.
  127. bool mDirty;
  128. /// Repacks the billboards into the vertex buffer.
  129. void _rebuildVB();
  130. public:
  131. GroundCoverCell() : mDirty(false) {}
  132. ~GroundCoverCell()
  133. {
  134. mVBs.clear();
  135. }
  136. const Point2I& shiftIndex( const Point2I& shift ) { return mIndex += shift; }
  137. /// The worldspace bounding box this cell.
  138. const Box3F& getBounds() const { return mBounds; }
  139. /// The worldspace bounding box of the renderable
  140. /// content within this cell.
  141. const Box3F& getRenderBounds() const { return mRenderBounds; }
  142. Point3F getCenter() const { return mBounds.getCenter(); }
  143. VectorF getSize() const { return VectorF( mBounds.len_x() / 2.0f,
  144. mBounds.len_y() / 2.0f,
  145. mBounds.len_z() / 2.0f ); }
  146. void renderBillboards( SceneRenderState *state, BaseMatInstance *mat, GFXPrimitiveBufferHandle *pb );
  147. U32 renderShapes( const TSRenderState &rdata,
  148. Frustum *culler,
  149. TSShapeInstance** shapes );
  150. };
  151. void GroundCoverCell::_rebuildVB()
  152. {
  153. if ( mBillboards.empty() )
  154. return;
  155. PROFILE_SCOPE(GroundCover_RebuildVB);
  156. // The maximum verts we can put in one vertex buffer batch.
  157. const U32 MAX_BILLBOARDS = 0xFFFF / 4;
  158. // How many batches will we need in total?
  159. const U32 batches = mCeil( (F32)mBillboards.size() / (F32)MAX_BILLBOARDS );
  160. // So... how many billboards do we need in
  161. // each batch? We're trying to evenly divide
  162. // the amount across all the VBs.
  163. const U32 batchBB = mBillboards.size() / batches;
  164. // Init the vertex buffer list to the right size. Any
  165. // VBs already in there will remain unless we're truncating
  166. // the list... those are freed.
  167. mVBs.setSize( batches );
  168. // Get the iter to the first billboard.
  169. Vector<Placement>::const_iterator iter = mBillboards.begin();
  170. // Prepare each batch.
  171. U32 bb, remaining = mBillboards.size();
  172. for ( U32 b = 0; b < batches; b++ )
  173. {
  174. // Grab a reference to the vb.
  175. VBHandle &vb = mVBs[b];
  176. // How many billboards in this batch?
  177. bb = getMin( batchBB, remaining );
  178. remaining -= bb;
  179. // Ok... now how many verts is that?
  180. const U32 verts = bb * 4;
  181. // Create the VB hasn't been created or if its
  182. // too small then resize it.
  183. if ( vb.isNull() || vb->mNumVerts < verts )
  184. {
  185. PROFILE_START(GroundCover_CreateVB);
  186. vb.set( GFX, verts, GFXBufferTypeStatic );
  187. PROFILE_END();
  188. }
  189. // Fill this puppy!
  190. GCVertex* vertPtr = vb.lock( 0, verts );
  191. GFXVertexColor color;
  192. Vector<Placement>::const_iterator last = iter + bb;
  193. for ( ; iter != last; iter++ )
  194. {
  195. const Point3F &position = (*iter).point;
  196. const Point3F &normal = (*iter).normal;
  197. const S32 &type = (*iter).type;
  198. const Point3F &size = (*iter).size;
  199. const F32 &windAmplitude = (*iter).windAmplitude;
  200. color = LinearColorF((*iter).lmColor).toColorI();
  201. U8 *col = (U8 *)const_cast<U32 *>( (const U32 *)color );
  202. vertPtr->point = position;
  203. vertPtr->normal = normal;
  204. vertPtr->params.x = size.x;
  205. vertPtr->params.y = size.y;
  206. vertPtr->params.z = type;
  207. vertPtr->params.w = 0;
  208. col[3] = 0;
  209. vertPtr->ambient = color;
  210. ++vertPtr;
  211. vertPtr->point = position;
  212. vertPtr->normal = normal;
  213. vertPtr->params.x = size.x;
  214. vertPtr->params.y = size.y;
  215. vertPtr->params.z = type;
  216. vertPtr->params.w = 0;
  217. col[3] = 1;
  218. vertPtr->ambient = color;
  219. ++vertPtr;
  220. vertPtr->point = position;
  221. vertPtr->normal = normal;
  222. vertPtr->params.x = size.x;
  223. vertPtr->params.y = size.y;
  224. vertPtr->params.z = type;
  225. vertPtr->params.w = windAmplitude;
  226. col[3] = 2;
  227. vertPtr->ambient = color;
  228. ++vertPtr;
  229. vertPtr->point = position;
  230. vertPtr->normal = normal;
  231. vertPtr->params.x = size.x;
  232. vertPtr->params.y = size.y;
  233. vertPtr->params.z = type;
  234. vertPtr->params.w = windAmplitude;
  235. col[3] = 3;
  236. vertPtr->ambient = color;
  237. ++vertPtr;
  238. }
  239. vb.unlock();
  240. }
  241. }
  242. U32 GroundCoverCell::renderShapes( const TSRenderState &rdata,
  243. Frustum *culler,
  244. TSShapeInstance** shapes )
  245. {
  246. MatrixF worldMat;
  247. TSShapeInstance* shape;
  248. Point3F camVector;
  249. F32 dist;
  250. F32 invScale;
  251. const SceneRenderState *state = rdata.getSceneState();
  252. U32 totalRendered = 0;
  253. Vector<Placement>::const_iterator iter = mShapes.begin();
  254. for ( ; iter != mShapes.end(); iter++ )
  255. {
  256. // Grab a reference here once.
  257. const Placement& inst = (*iter);
  258. // If we were pass a culler then us it to test the shape world box.
  259. if ( culler && culler->isCulled( inst.worldBox ) )
  260. continue;
  261. shape = shapes[ inst.type ];
  262. camVector = inst.point - state->getDiffuseCameraPosition();
  263. dist = getMax( camVector.len(), 0.01f );
  264. worldMat.set( EulerF(inst.normal.x, inst.normal.y, inst.rotation), inst.point );
  265. // TSShapeInstance::render() uses the
  266. // world matrix for the RenderInst.
  267. worldMat.scale( inst.size );
  268. GFX->setWorldMatrix( worldMat );
  269. // Obey the normal screen space lod metrics. The shapes should
  270. // be tuned to lod out quickly for ground cover.
  271. //
  272. // Note: The profile doesn't indicate that lod selection is
  273. // very expensive... in fact its less than 1/10th of the cost
  274. // of the render() call below.
  275. PROFILE_START(GroundCover_RenderShapes_SelectDetail);
  276. invScale = (1.0f/getMax(getMax(inst.size.x,inst.size.y),inst.size.z));
  277. shape->setDetailFromDistance( state, dist * invScale );
  278. PROFILE_END(); // GroundCover_RenderShapes_SelectDetail
  279. // Note: This is the most expensive call of this loop. We
  280. // need to rework the render call completely to optimize it.
  281. PROFILE_START(GroundCover_RenderShapes_Render);
  282. shape->render( rdata );
  283. PROFILE_END(); // GroundCover_RenderShapes_Render
  284. totalRendered++;
  285. }
  286. return totalRendered;
  287. }
  288. void GroundCoverCell::renderBillboards( SceneRenderState *state, BaseMatInstance *mat, GFXPrimitiveBufferHandle *pb )
  289. {
  290. if ( mDirty )
  291. {
  292. _rebuildVB();
  293. mDirty = false;
  294. }
  295. // Do we have anything to render?
  296. if ( mBillboards.size() == 0 || mVBs.empty() || !mat )
  297. return;
  298. // TODO: Maybe add support for non-facing billboards
  299. // with random rotations and optional crosses. We could
  300. // stick them into the buffer after the normal billboards,
  301. // then change shader consts.
  302. RenderPassManager *pass = state->getRenderPass();
  303. // Draw each batch.
  304. U32 remaining = mBillboards.size();
  305. const U32 batches = mVBs.size();
  306. const U32 batchBB = remaining / batches;
  307. for ( U32 b = 0; b < batches; b++ )
  308. {
  309. // Grab a reference to the vb.
  310. VBHandle &vb = mVBs[b];
  311. // How many billboards in this batch?
  312. U32 bb = getMin( batchBB, remaining );
  313. remaining -= bb;
  314. MeshRenderInst *ri = pass->allocInst<MeshRenderInst>();
  315. ri->type = RenderPassManager::RIT_Mesh;
  316. ri->matInst = mat;
  317. ri->vertBuff = &vb;
  318. ri->primBuff = pb;
  319. ri->objectToWorld = &MatrixF::Identity;
  320. ri->worldToCamera = pass->allocSharedXform(RenderPassManager::View);
  321. ri->projection = pass->allocSharedXform(RenderPassManager::Projection);
  322. ri->defaultKey = mat->getStateHint();
  323. ri->prim = pass->allocPrim();
  324. ri->prim->numPrimitives = bb * 2;
  325. ri->prim->numVertices = bb * 4;
  326. ri->prim->startIndex = 0;
  327. ri->prim->startVertex = 0;
  328. ri->prim->minIndex = 0;
  329. ri->prim->type = GFXTriangleList;
  330. // If we need lights then set them up.
  331. if ( mat->isForwardLit() )
  332. {
  333. LightQuery query;
  334. query.init( mBounds );
  335. query.getLights( ri->lights, 8 );
  336. }
  337. pass->addInst( ri );
  338. GroundCover::smStatRenderedBatches++;
  339. GroundCover::smStatRenderedBillboards += bb;
  340. }
  341. GroundCover::smStatRenderedCells++;
  342. }
  343. U32 GroundCover::smStatRenderedCells = 0;
  344. U32 GroundCover::smStatRenderedBillboards = 0;
  345. U32 GroundCover::smStatRenderedBatches = 0;
  346. U32 GroundCover::smStatRenderedShapes = 0;
  347. F32 GroundCover::smDensityScale = 1.0f;
  348. F32 GroundCover::smFadeScale = 1.0f;
  349. ConsoleDocClass( GroundCover,
  350. "@brief Covers the ground in a field of objects (IE: Grass, Flowers, etc)."
  351. "@ingroup Foliage\n"
  352. );
  353. GroundCover::GroundCover()
  354. {
  355. mTypeMask |= StaticObjectType | StaticShapeObjectType;
  356. mNetFlags.set( Ghostable | ScopeAlways );
  357. mRadius = 200.0f;
  358. mZOffset = 0.0f;
  359. mFadeRadius = 50.0f;
  360. mShapeCullRadius = 75.0f;
  361. mShapesCastShadows = true;
  362. mReflectRadiusScale = 0.25f;
  363. mGridSize = 7;
  364. // By initializing this to a big value we
  365. // ensure we warp on first render.
  366. mGridIndex.set( S32_MAX, S32_MAX );
  367. mMaxPlacement = 1000;
  368. mLastPlacementCount = 0;
  369. mDebugRenderCells = false;
  370. mDebugNoBillboards = false;
  371. mDebugNoShapes = false;
  372. mDebugLockFrustum = false;
  373. mRandomSeed = 1;
  374. INIT_ASSET(Material);
  375. mMaterialInst = NULL;
  376. mMatParams = NULL;
  377. mTypeRectsParam = NULL;
  378. mFadeParams = NULL;
  379. mWindDirParam = NULL;
  380. mGustInfoParam = NULL;
  381. mTurbInfoParam = NULL;
  382. mCamRightParam = NULL;
  383. mCamUpParam = NULL;
  384. mMaxBillboardTiltAngle = 90.0f;
  385. // TODO: This really doesn't belong here... we need a
  386. // real wind system for Torque scenes. This data
  387. // would be part of a global scene wind or area wind
  388. // emitter.
  389. //
  390. // Tom Spilman - 10/16/2007
  391. mWindGustLength = 20.0f;
  392. mWindGustFrequency = 0.5f;
  393. mWindGustStrength = 0.5f;
  394. mWindDirection.set( 1.0f, 0.0f );
  395. mWindTurbulenceFrequency = 1.2f;
  396. mWindTurbulenceStrength = 0.125f;
  397. for ( S32 i=0; i < MAX_COVERTYPES; i++ )
  398. {
  399. mProbability[i] = 0.0f;
  400. mSizeMin[i] = 1.0f;
  401. mSizeMax[i] = 1.0f;
  402. mSizeExponent[i] = 1.0f;
  403. mWindScale[i] = 1.0f;
  404. mMinSlope[i] = 0.0f;
  405. mMaxSlope[i] = 0.0f;
  406. mConformToNormal[i] = false;
  407. mMinRotX[i] = 0.0f;
  408. mMaxRotX[i] = 0.0f;
  409. mMinRotY[i] = 0.0f;
  410. mMaxRotY[i] = 0.0f;
  411. mMinElevation[i] = -99999.0f;
  412. mMaxElevation[i] = 99999.0f;
  413. mLayer[i] = StringTable->EmptyString();
  414. mInvertLayer[i] = false;
  415. mMinClumpCount[i] = 1;
  416. mMaxClumpCount[i] = 1;
  417. mClumpCountExponent[i] = 1.0f;
  418. mClumpRadius[i] = 1.0f;
  419. mBillboardRects[i].point.set( 0.0f, 0.0f );
  420. mBillboardRects[i].extent.set( 1.0f, 1.0f );
  421. INIT_ASSET_ARRAY(Shape, i);
  422. mShapeInstances[i] = NULL;
  423. mBillboardAspectScales[i] = 1.0f;
  424. mNormalizedProbability[i] = 0.0f;
  425. }
  426. }
  427. GroundCover::~GroundCover()
  428. {
  429. SAFE_DELETE( mMaterialInst );
  430. }
  431. IMPLEMENT_CO_NETOBJECT_V1(GroundCover);
  432. void GroundCover::initPersistFields()
  433. {
  434. addGroup( "GroundCover General" );
  435. INITPERSISTFIELD_MATERIALASSET(Material, GroundCover, "Material used by all GroundCover segments.");
  436. addField( "radius", TypeF32, Offset( mRadius, GroundCover ), "Outer generation radius from the current camera position." );
  437. addField( "dissolveRadius",TypeF32, Offset( mFadeRadius, GroundCover ), "This is less than or equal to radius and defines when fading of cover elements begins." );
  438. addField( "reflectScale", TypeF32, Offset( mReflectRadiusScale, GroundCover ), "Scales the various culling radii when rendering a reflection. Typically for water." );
  439. addField( "gridSize", TypeS32, Offset( mGridSize, GroundCover ), "The number of cells per axis in the grid." );
  440. addField( "zOffset", TypeF32, Offset( mZOffset, GroundCover ), "Offset along the Z axis to render the ground cover." );
  441. addField( "seed", TypeS32, Offset( mRandomSeed, GroundCover ), "This RNG seed is saved and sent to clients for generating the same cover." );
  442. addField( "maxElements", TypeS32, Offset( mMaxPlacement, GroundCover ), "The maximum amount of cover elements to include in the grid at any one time." );
  443. addField( "maxBillboardTiltAngle", TypeF32, Offset( mMaxBillboardTiltAngle, GroundCover ),"The maximum amout of degrees the billboard will tilt down to match the camera." );
  444. addField( "shapeCullRadius", TypeF32, Offset( mShapeCullRadius, GroundCover ), "This is the distance at which DTS elements are completely culled out." );
  445. addField( "shapesCastShadows", TypeBool, Offset( mShapesCastShadows, GroundCover ), "Whether DTS elements should cast shadows or not." );
  446. addArray( "Types", MAX_COVERTYPES );
  447. addField( "billboardUVs", TypeRectUV, Offset( mBillboardRects, GroundCover ), MAX_COVERTYPES, "Subset material UV coordinates for this cover billboard." );
  448. addField("shapeFilename", TypeFilename, Offset(mShapeName, GroundCover), MAX_COVERTYPES, "The cover shape filename. [Optional]", AbstractClassRep::FIELD_HideInInspectors);
  449. INITPERSISTFIELD_SHAPEASSET_ARRAY(Shape, MAX_COVERTYPES, GroundCover, "The cover shape. [Optional]");
  450. addField( "layer", TypeTerrainMaterialAssetId, Offset( mLayer, GroundCover ), MAX_COVERTYPES, "Terrain material assetId to limit coverage to, or blank to not limit." );
  451. addField( "invertLayer", TypeBool, Offset( mInvertLayer, GroundCover ), MAX_COVERTYPES, "Indicates that the terrain material index given in 'layer' is an exclusion mask." );
  452. addField( "probability", TypeF32, Offset( mProbability, GroundCover ), MAX_COVERTYPES, "The probability of one cover type verses another (relative to all cover types)." );
  453. addField( "sizeMin", TypeF32, Offset( mSizeMin, GroundCover ), MAX_COVERTYPES, "The minimum random size for each cover type." );
  454. addField( "sizeMax", TypeF32, Offset( mSizeMax, GroundCover ), MAX_COVERTYPES, "The maximum random size of this cover type." );
  455. addField( "sizeExponent", TypeF32, Offset( mSizeExponent, GroundCover ), MAX_COVERTYPES, "An exponent used to bias between the minimum and maximum random sizes." );
  456. addField( "windScale", TypeF32, Offset( mWindScale, GroundCover ), MAX_COVERTYPES, "The wind effect scale." );
  457. addField( "minSlope", TypeF32, Offset(mMinSlope, GroundCover), MAX_COVERTYPES, "The minimum slope angle in degrees for placement.");
  458. addField( "maxSlope", TypeF32, Offset( mMaxSlope, GroundCover ), MAX_COVERTYPES, "The maximum slope angle in degrees for placement." );
  459. addField("conformToNormal",TypeBool, Offset(mConformToNormal, GroundCover), MAX_COVERTYPES, "Use the terrain's slope for angle");
  460. addField("minRotX", TypeF32, Offset(mMinRotX, GroundCover), MAX_COVERTYPES, "minumum amount of rotation along the X axis to add");
  461. addField("maxRotX", TypeF32, Offset(mMaxRotX, GroundCover), MAX_COVERTYPES, "maximum amount of rotation along the X axis to add");
  462. addField("minRotY", TypeF32, Offset(mMinRotY, GroundCover), MAX_COVERTYPES, "minumum amount of rotation along the Y axis to add");
  463. addField("maxRotY", TypeF32, Offset(mMaxRotY, GroundCover), MAX_COVERTYPES, "maximum amount of rotation along the Y axis to add");
  464. addField( "minElevation", TypeF32, Offset( mMinElevation, GroundCover ), MAX_COVERTYPES, "The minimum world space elevation for placement." );
  465. addField( "maxElevation", TypeF32, Offset( mMaxElevation, GroundCover ), MAX_COVERTYPES, "The maximum world space elevation for placement." );
  466. addField( "minClumpCount", TypeS32, Offset( mMinClumpCount, GroundCover ), MAX_COVERTYPES, "The minimum amount of elements in a clump." );
  467. addField( "maxClumpCount", TypeS32, Offset( mMaxClumpCount, GroundCover ), MAX_COVERTYPES, "The maximum amount of elements in a clump." );
  468. addField( "clumpExponent", TypeF32, Offset( mClumpCountExponent, GroundCover ), MAX_COVERTYPES, "An exponent used to bias between the minimum and maximum clump counts for a particular clump." );
  469. addField( "clumpRadius", TypeF32, Offset( mClumpRadius, GroundCover ), MAX_COVERTYPES, "The maximum clump radius." );
  470. endArray( "Types" );
  471. endGroup( "GroundCover General" );
  472. addGroup( "GroundCover Wind" );
  473. addField( "windDirection", TypePoint2F, Offset( mWindDirection, GroundCover ), "The direction of the wind." );
  474. addField( "windGustLength", TypeF32, Offset( mWindGustLength, GroundCover ), "The length in meters between peaks in the wind gust." );
  475. addField( "windGustFrequency",TypeF32, Offset( mWindGustFrequency, GroundCover ), "Controls how often the wind gust peaks per second." );
  476. addField( "windGustStrength", TypeF32, Offset( mWindGustStrength, GroundCover ), "The maximum distance in meters that the peak wind gust will displace an element." );
  477. addField( "windTurbulenceFrequency", TypeF32, Offset( mWindTurbulenceFrequency, GroundCover ),"Controls the overall rapidity of the wind turbulence." );
  478. addField( "windTurbulenceStrength", TypeF32, Offset( mWindTurbulenceStrength, GroundCover ), "The maximum distance in meters that the turbulence can displace a ground cover element." );
  479. endGroup( "GroundCover Wind" );
  480. addGroup( "GroundCover Debug" );
  481. addField( "lockFrustum", TypeBool, Offset( mDebugLockFrustum, GroundCover ), "Debug parameter for locking the culling frustum which will freeze the cover generation." );
  482. addField( "renderCells", TypeBool, Offset( mDebugRenderCells, GroundCover ), "Debug parameter for displaying the grid cells." );
  483. addField( "noBillboards", TypeBool, Offset( mDebugNoBillboards, GroundCover ), "Debug parameter for turning off billboard rendering." );
  484. addField( "noShapes", TypeBool, Offset( mDebugNoShapes, GroundCover ), "Debug parameter for turning off shape rendering." );
  485. endGroup( "GroundCover Debug" );
  486. Parent::initPersistFields();
  487. }
  488. void GroundCover::consoleInit()
  489. {
  490. Con::addVariable( "$pref::GroundCover::densityScale", TypeF32, &smDensityScale, "A global LOD scalar which can reduce the overall density of placed GroundCover.\n"
  491. "@ingroup Foliage\n");
  492. Con::addVariable("$pref::GroundCover::fadeScale", TypeF32, &smFadeScale, "A global fade scalar which can reduce the overall rendered distance of placed GroundCover.\n"
  493. "@ingroup Foliage\n");
  494. Con::addVariable( "$GroundCover::renderedCells", TypeS32, &smStatRenderedCells, "Stat for number of rendered cells.\n"
  495. "@ingroup Foliage\n");
  496. Con::addVariable( "$GroundCover::renderedBillboards", TypeS32, &smStatRenderedBillboards, "Stat for number of rendered billboards.\n"
  497. "@ingroup Foliage\n");
  498. Con::addVariable( "$GroundCover::renderedBatches", TypeS32, &smStatRenderedBatches, "Stat for number of rendered billboard batches.\n"
  499. "@ingroup Foliage\n");
  500. Con::addVariable( "$GroundCover::renderedShapes", TypeS32, &smStatRenderedShapes, "Stat for number of rendered shapes.\n"
  501. "@ingroup Foliage\n");
  502. Parent::consoleInit();
  503. }
  504. bool GroundCover::onAdd()
  505. {
  506. if (!Parent::onAdd())
  507. return false;
  508. // We don't use any bounds.
  509. setGlobalBounds();
  510. resetWorldBox();
  511. // Prepare some client side things.
  512. if ( isClientObject() )
  513. {
  514. _initMaterial();
  515. _initShapes();
  516. // Hook ourselves up to get terrain change notifications.
  517. TerrainBlock::smUpdateSignal.notify( this, &GroundCover::onTerrainUpdated );
  518. }
  519. addToScene();
  520. return true;
  521. }
  522. void GroundCover::onRemove()
  523. {
  524. Parent::onRemove();
  525. _deleteCells();
  526. _deleteShapes();
  527. if ( isClientObject() )
  528. {
  529. TerrainBlock::smUpdateSignal.remove( this, &GroundCover::onTerrainUpdated );
  530. }
  531. removeFromScene();
  532. }
  533. void GroundCover::inspectPostApply()
  534. {
  535. Parent::inspectPostApply();
  536. // We flag all the parameters as changed because
  537. // we're feeling lazy and there is not a good way
  538. // to track what parameters changed.
  539. //
  540. // TODO: Add a mask bit option to addField() and/or
  541. // addGroup() which is passed to inspectPostApply
  542. // for detection of changed elements.
  543. //
  544. setMaskBits(U32(-1) );
  545. }
  546. U32 GroundCover::packUpdate( NetConnection *connection, U32 mask, BitStream *stream )
  547. {
  548. U32 retMask = Parent::packUpdate( connection, mask, stream );
  549. if (stream->writeFlag(mask & InitialUpdateMask))
  550. {
  551. // TODO: We could probably optimize a few of these
  552. // based on reasonable units at some point.
  553. PACK_ASSET(connection, Material);
  554. stream->write( mRadius );
  555. stream->write( mZOffset );
  556. stream->write( mFadeRadius );
  557. stream->write( mShapeCullRadius );
  558. stream->writeFlag( mShapesCastShadows );
  559. stream->write( mReflectRadiusScale );
  560. stream->write( mGridSize );
  561. stream->write( mRandomSeed );
  562. stream->write( mMaxPlacement );
  563. stream->write( mMaxBillboardTiltAngle );
  564. stream->write( mWindDirection.x );
  565. stream->write( mWindDirection.y );
  566. stream->write( mWindGustLength );
  567. stream->write( mWindGustFrequency );
  568. stream->write( mWindGustStrength );
  569. stream->write( mWindTurbulenceFrequency );
  570. stream->write( mWindTurbulenceStrength );
  571. for ( S32 i=0; i < MAX_COVERTYPES; i++ )
  572. {
  573. stream->write( mProbability[i] );
  574. stream->write( mSizeMin[i] );
  575. stream->write( mSizeMax[i] );
  576. stream->write( mSizeExponent[i] );
  577. stream->write( mWindScale[i] );
  578. stream->write( mMinSlope[i] );
  579. stream->write( mMaxSlope[i] );
  580. stream->writeFlag(mConformToNormal[i]);
  581. stream->write(mMinRotX[i]);
  582. stream->write(mMaxRotX[i]);
  583. stream->write(mMinRotY[i]);
  584. stream->write(mMaxRotY[i]);
  585. stream->write( mMinElevation[i] );
  586. stream->write( mMaxElevation[i] );
  587. stream->writeString( mLayer[i] );
  588. stream->writeFlag( mInvertLayer[i] );
  589. stream->write( mMinClumpCount[i] );
  590. stream->write( mMaxClumpCount[i] );
  591. stream->write( mClumpCountExponent[i] );
  592. stream->write( mClumpRadius[i] );
  593. stream->write( mBillboardRects[i].point.x );
  594. stream->write( mBillboardRects[i].point.y );
  595. stream->write( mBillboardRects[i].extent.x );
  596. stream->write( mBillboardRects[i].extent.y );
  597. PACK_ASSET_ARRAY(connection, Shape, i);
  598. }
  599. stream->writeFlag( mDebugRenderCells );
  600. stream->writeFlag( mDebugNoBillboards );
  601. stream->writeFlag( mDebugNoShapes );
  602. stream->writeFlag( mDebugLockFrustum );
  603. }
  604. return retMask;
  605. }
  606. void GroundCover::unpackUpdate( NetConnection *connection, BitStream *stream )
  607. {
  608. Parent::unpackUpdate( connection, stream );
  609. if (stream->readFlag())
  610. {
  611. UNPACK_ASSET(connection, Material);
  612. stream->read( &mRadius );
  613. stream->read( &mZOffset );
  614. stream->read( &mFadeRadius );
  615. stream->read( &mShapeCullRadius );
  616. mShapesCastShadows = stream->readFlag();
  617. stream->read( &mReflectRadiusScale );
  618. stream->read( &mGridSize );
  619. stream->read( &mRandomSeed );
  620. stream->read( &mMaxPlacement );
  621. stream->read( &mMaxBillboardTiltAngle );
  622. stream->read( &mWindDirection.x );
  623. stream->read( &mWindDirection.y );
  624. stream->read( &mWindGustLength );
  625. stream->read( &mWindGustFrequency );
  626. stream->read( &mWindGustStrength );
  627. stream->read( &mWindTurbulenceFrequency );
  628. stream->read( &mWindTurbulenceStrength );
  629. for ( S32 i=0; i < MAX_COVERTYPES; i++ )
  630. {
  631. stream->read( &mProbability[i] );
  632. stream->read( &mSizeMin[i] );
  633. stream->read( &mSizeMax[i] );
  634. stream->read( &mSizeExponent[i] );
  635. stream->read( &mWindScale[i] );
  636. stream->read( &mMinSlope[i] );
  637. stream->read( &mMaxSlope[i] );
  638. mConformToNormal[i] = stream->readFlag();
  639. stream->read(&mMinRotX[i]);
  640. stream->read(&mMaxRotX[i]);
  641. stream->read(&mMinRotY[i]);
  642. stream->read(&mMaxRotY[i]);
  643. stream->read( &mMinElevation[i] );
  644. stream->read( &mMaxElevation[i] );
  645. mLayer[i] = stream->readSTString();
  646. mInvertLayer[i] = stream->readFlag();
  647. stream->read( &mMinClumpCount[i] );
  648. stream->read( &mMaxClumpCount[i] );
  649. stream->read( &mClumpCountExponent[i] );
  650. stream->read( &mClumpRadius[i] );
  651. stream->read( &mBillboardRects[i].point.x );
  652. stream->read( &mBillboardRects[i].point.y );
  653. stream->read( &mBillboardRects[i].extent.x );
  654. stream->read( &mBillboardRects[i].extent.y );
  655. UNPACK_ASSET_ARRAY(connection, Shape, i);
  656. }
  657. mDebugRenderCells = stream->readFlag();
  658. mDebugNoBillboards = stream->readFlag();
  659. mDebugNoShapes = stream->readFlag();
  660. mDebugLockFrustum = stream->readFlag();
  661. // We have no way to easily know what changed, so by clearing
  662. // the cells we force a reinit and regeneration of the cells.
  663. // It's sloppy, but it works for now.
  664. _freeCells();
  665. if ( isProperlyAdded() )
  666. _initMaterial();
  667. }
  668. }
  669. void GroundCover::_initMaterial()
  670. {
  671. SAFE_DELETE(mMaterialInst);
  672. if (mMaterialAsset.notNull() && mMaterialAsset->getStatus() == MaterialAsset::Ok)
  673. mMaterialInst = mMaterial->createMatInstance();
  674. else
  675. mMaterialInst = MATMGR->createMatInstance("WarningMaterial");
  676. // Add our special feature that makes it all work...
  677. FeatureSet features = MATMGR->getDefaultFeatures();
  678. features.addFeature( MFT_Foliage );
  679. // Our feature requires a pointer back to this object
  680. // to properly setup its shader consts.
  681. mMaterialInst->setUserObject( this );
  682. // DO IT!
  683. mMaterialInst->init( features, getGFXVertexFormat<GCVertex>() );
  684. }
  685. void GroundCover::_initShapes()
  686. {
  687. _deleteShapes();
  688. for ( S32 i=0; i < MAX_COVERTYPES; i++ )
  689. {
  690. if ( mShapeAsset[i].isNull() || mShape[i] == nullptr)
  691. continue;
  692. if ( isClientObject() && !mShape[i]->preloadMaterialList(mShape[i].getPath()) && NetConnection::filesWereDownloaded() )
  693. {
  694. Con::warnf( "GroundCover::_initShapes() material preload failed for shape: %s", mShapeAssetId[i] );
  695. continue;
  696. }
  697. // Create the shape instance.
  698. mShapeInstances[i] = new TSShapeInstance(mShape[i], isClientObject() );
  699. }
  700. }
  701. void GroundCover::_deleteShapes()
  702. {
  703. for ( S32 i=0; i < MAX_COVERTYPES; i++ )
  704. {
  705. delete mShapeInstances[i];
  706. mShapeInstances[i] = NULL;
  707. }
  708. }
  709. void GroundCover::_deleteCells()
  710. {
  711. // Delete the allocation list.
  712. for ( S32 i=0; i < mAllocCellList.size(); i++ )
  713. delete mAllocCellList[i];
  714. mAllocCellList.clear();
  715. // Zero out the rest of the stuff.
  716. _freeCells();
  717. }
  718. void GroundCover::_freeCells()
  719. {
  720. // Zero the grid and scratch space.
  721. mCellGrid.clear();
  722. mScratchGrid.clear();
  723. // Compact things... remove excess allocated cells.
  724. const U32 maxCells = mGridSize * mGridSize;
  725. if ( mAllocCellList.size() > maxCells )
  726. {
  727. for ( S32 i=maxCells; i < mAllocCellList.size(); i++ )
  728. delete mAllocCellList[i];
  729. mAllocCellList.setSize( maxCells );
  730. }
  731. // Move all the alloced cells into the free list.
  732. mFreeCellList.clear();
  733. mFreeCellList.merge( mAllocCellList );
  734. // Release the primitive buffer.
  735. mPrimBuffer = NULL;
  736. }
  737. void GroundCover::_recycleCell( GroundCoverCell* cell )
  738. {
  739. mFreeCellList.push_back( cell );
  740. }
  741. void GroundCover::_initialize( U32 cellCount, U32 cellPlacementCount )
  742. {
  743. // Cleanup everything... we're starting over.
  744. _freeCells();
  745. _deleteShapes();
  746. // Nothing to do without a count!
  747. if ( cellPlacementCount == 0 )
  748. return;
  749. // Reset the grid sizes.
  750. mCellGrid.setSize( cellCount );
  751. dMemset( mCellGrid.address(), 0, mCellGrid.memSize() );
  752. mScratchGrid.setSize( cellCount );
  753. // Rebuild the texture aspect scales for each type.
  754. F32 textureAspect = 1.0f;
  755. if( mMaterialInst && mMaterialInst->isValid())
  756. {
  757. Material* mat = dynamic_cast<Material*>(mMaterialInst->getMaterial());
  758. if(mat)
  759. {
  760. GFXTexHandle tex;
  761. if (mat->getDiffuseMapResource(0))
  762. tex = mat->getDiffuseMapResource(0);
  763. else if (mat->getDiffuseMap(0) != StringTable->EmptyString())
  764. tex = GFXTexHandle(mat->getDiffuseMap(0), &GFXStaticTextureSRGBProfile, "GroundCover texture aspect ratio check");
  765. if(tex.isValid())
  766. {
  767. U32 w = tex.getWidth();
  768. U32 h = tex.getHeight();
  769. if(h > 0)
  770. textureAspect = F32(w) / F32(h);
  771. }
  772. }
  773. }
  774. for ( S32 i=0; i < MAX_COVERTYPES; i++ )
  775. {
  776. if ( mBillboardRects[i].extent.y > 0.0f )
  777. {
  778. mBillboardAspectScales[i] = textureAspect * mBillboardRects[i].extent.x / mBillboardRects[i].extent.y;
  779. }
  780. else
  781. mBillboardAspectScales[i] = 0.0f;
  782. }
  783. // Load the shapes again.
  784. _initShapes();
  785. // Set the primitive buffer up for the maximum placement in a cell.
  786. mPrimBuffer.set( GFX, cellPlacementCount * 6, 0, GFXBufferTypeStatic );
  787. U16 *idxBuff;
  788. mPrimBuffer.lock(&idxBuff);
  789. for ( U32 i=0; i < cellPlacementCount; i++ )
  790. {
  791. //
  792. // The vertex pattern in the VB for each
  793. // billboard is as follows...
  794. //
  795. // 0----1
  796. // |\ |
  797. // | \ |
  798. // | \ |
  799. // | \|
  800. // 3----2
  801. //
  802. // We setup the index order below to ensure
  803. // sequential, cache friendly, access.
  804. //
  805. U32 offset = i * 4;
  806. idxBuff[i*6+0] = 0 + offset;
  807. idxBuff[i*6+1] = 1 + offset;
  808. idxBuff[i*6+2] = 2 + offset;
  809. idxBuff[i*6+3] = 2 + offset;
  810. idxBuff[i*6+4] = 3 + offset;
  811. idxBuff[i*6+5] = 0 + offset;
  812. }
  813. mPrimBuffer.unlock();
  814. // Generate the normalized probability.
  815. F32 total = 0.0f;
  816. for ( S32 i=0; i < MAX_COVERTYPES; i++ )
  817. {
  818. // If the element isn't gonna render... then
  819. // set the probability to zero.
  820. if ( mShapeInstances[i] == NULL && mBillboardAspectScales[i] <= 0.0001f )
  821. {
  822. mNormalizedProbability[i] = 0.0f;
  823. }
  824. else
  825. {
  826. mNormalizedProbability[i] = mProbability[i];
  827. total += mProbability[i];
  828. }
  829. }
  830. if ( total > 0.0f )
  831. {
  832. for ( S32 i=0; i < MAX_COVERTYPES; i++ )
  833. mNormalizedProbability[i] /= total;
  834. }
  835. }
  836. GroundCoverCell* GroundCover::_generateCell( const Point2I& index,
  837. const Box3F& bounds,
  838. U32 placementCount,
  839. S32 randSeed )
  840. {
  841. PROFILE_SCOPE(GroundCover_GenerateCell);
  842. const Vector<SceneObject*> terrainBlocks = getContainer()->getTerrains();
  843. if ( terrainBlocks.empty() )
  844. return NULL;
  845. // Grab a free cell or allocate a new one.
  846. GroundCoverCell* cell;
  847. if ( mFreeCellList.empty() )
  848. {
  849. cell = new GroundCoverCell();
  850. mAllocCellList.push_back( cell );
  851. }
  852. else
  853. {
  854. cell = mFreeCellList.last();
  855. mFreeCellList.pop_back();
  856. }
  857. cell->mDirty = true;
  858. cell->mIndex = index;
  859. cell->mBounds = bounds;
  860. Point3F pos( 0, 0, 0 );
  861. Box3F renderBounds = bounds;
  862. Point3F point;
  863. Point3F normal;
  864. F32 h;
  865. Point2F cp, uv;
  866. bool hit;
  867. GroundCoverCell::Placement p;
  868. F32 rotation;
  869. F32 size;
  870. F32 sizeExponent;
  871. Point2I lpos;
  872. //F32 value;
  873. VectorF right;
  874. StringTableEntry matName = StringTable->EmptyString();
  875. bool firstElem = true;
  876. TerrainBlock *terrainBlock = NULL;
  877. cell->mBillboards.clear();
  878. cell->mBillboards.reserve( placementCount );
  879. cell->mShapes.clear();
  880. cell->mShapes.reserve( placementCount );
  881. F32 terrainSquareSize,
  882. oneOverTerrainLength,
  883. oneOverTerrainSquareSize;
  884. const GBitmap* terrainLM = NULL;
  885. // The RNG that we'll use in generation.
  886. MRandom rand( 0 );
  887. // We process one type at a time.
  888. for ( U32 type=0; type < MAX_COVERTYPES; type++ )
  889. {
  890. // How many cover elements do we need to generate for this type?
  891. const S32 typeCount = mNormalizedProbability[type] * (F32)placementCount;
  892. if ( typeCount <= 0 )
  893. continue;
  894. // Grab the terrain layer for this type.
  895. /*
  896. const TerrainDataLayer* dataLayer = NULL;
  897. const bool typeInvertLayer = mInvertLayer[type];
  898. if ( mLayer[type] > -1 )
  899. {
  900. dataLayer = mTerrainBlock->getDataLayer( mLayer[type] );
  901. if ( dataLayer )
  902. {
  903. // Do an initial check to see if we can place any place anything
  904. // at all... if the layer area for this element is empty then
  905. // there is nothing more to do.
  906. RectI area( (S32)mFloor( ( bounds.minExtents.x - pos.x ) * oneOverTerrainSquareSize ),
  907. (S32)mFloor( ( bounds.minExtents.y - pos.y ) * oneOverTerrainSquareSize ),
  908. (S32)mCeil( ( bounds.maxExtents.x - pos.x ) * oneOverTerrainSquareSize ),
  909. (S32)mCeil( ( bounds.maxExtents.y - pos.y ) * oneOverTerrainSquareSize ) );
  910. area.extent -= area.point;
  911. if ( dataLayer->testFill( area, typeInvertLayer ? 255 : 0 ) )
  912. continue;
  913. }
  914. }
  915. // If the layer is not inverted and we have no data
  916. // then we have nothing to draw.
  917. if ( !typeInvertLayer && !dataLayer )
  918. continue;
  919. */
  920. // We set the seed we were passed which is based on this grids position
  921. // in the world and add the type value. This keeps changes to one type
  922. // from effecting the outcome of the others.
  923. rand.setSeed( randSeed + type );
  924. // Setup for doing clumps.
  925. S32 clumps = 0;
  926. Point2F clumpCenter(0.0f, 0.0f);
  927. const S32 clumpMin = getMax( 1, (S32)mMinClumpCount[type] );
  928. F32 clumpExponent;
  929. // We mult this by -1 each billboard we make then use
  930. // it to scale the billboard x axis to flip them. This
  931. // essentially gives us twice the variation for free.
  932. F32 flipBB = -1.0f;
  933. // Precompute a few other type specific values.
  934. const bool typeConformToNormal = mConformToNormal[type];
  935. const F32 typeMinRotX = (mMaxRotX[type] > mMinRotX[type]) ? mMinRotX[type] : mMaxRotX[type];
  936. const F32 typeMaxRotX = (mMaxRotX[type] > mMinRotX[type]) ? mMaxRotX[type] : mMinRotX[type];
  937. const F32 typeMinRotY = (mMaxRotY[type] > mMinRotY[type]) ? mMinRotY[type] : mMaxRotY[type];
  938. const F32 typeMaxRotY = (mMaxRotY[type] > mMinRotY[type]) ? mMaxRotY[type] : mMinRotY[type];
  939. const F32 typeSizeRange = mSizeMax[type] - mSizeMin[type];
  940. const F32 typeMinSlope = mMinSlope[type];
  941. const F32 typeMaxSlope = mMaxSlope[type];
  942. const F32 typeMaxElevation = mMaxElevation[type];
  943. const F32 typeMinElevation = mMinElevation[type];
  944. const bool typeIsShape = mShapeInstances[ type ] != NULL;
  945. const Box3F typeShapeBounds = typeIsShape ? mShapeInstances[ type ]->getShape()->mBounds : Box3F();
  946. const F32 typeWindScale = mWindScale[type];
  947. StringTableEntry typeLayer = mLayer[type];
  948. const bool typeInvertLayer = mInvertLayer[type];
  949. // We can set this once here... all the placements for this are the same.
  950. p.type = type;
  951. p.windAmplitude = typeWindScale;
  952. p.lmColor.set(1.0f,1.0f,1.0f);
  953. // Generate all the cover elements for this type.
  954. for ( S32 i=0; i < typeCount; i++ )
  955. {
  956. // Do all the other random things here first as to not
  957. // disturb the random sequence if the terrain geometry
  958. // or cover layers change.
  959. // Get the random position.
  960. cp.set( rand.randF(), rand.randF() );
  961. // Prepare the clump info.
  962. clumpExponent = mClampF( mPow( rand.randF(), mClumpCountExponent[type] ), 0.0f, 1.0f );
  963. if ( clumps <= 0 )
  964. {
  965. // We're starting a new clump.
  966. clumps = ( clumpMin + mFloor( ( mMaxClumpCount[type] - clumpMin ) * clumpExponent ) ) - 1;
  967. cp.set( bounds.minExtents.x + cp.x * bounds.len_x(),
  968. bounds.minExtents.y + cp.y * bounds.len_y() );
  969. clumpCenter = cp;
  970. }
  971. else
  972. {
  973. clumps--;
  974. cp.set( clumpCenter.x - ( ( cp.x - 0.5f ) * mClumpRadius[type] ),
  975. clumpCenter.y - ( ( cp.y - 0.5f ) * mClumpRadius[type] ) );
  976. }
  977. // Which terrain do I place on?
  978. if ( terrainBlocks.size() == 1 )
  979. terrainBlock = dynamic_cast< TerrainBlock* >( terrainBlocks.first() );
  980. else
  981. {
  982. for ( U32 blockIDx = 0; blockIDx < terrainBlocks.size(); blockIDx++ )
  983. {
  984. TerrainBlock *terrain = dynamic_cast< TerrainBlock* >( terrainBlocks[ blockIDx ] );
  985. if( !terrain )
  986. continue;
  987. const Box3F &terrBounds = terrain->getWorldBox();
  988. if ( cp.x < terrBounds.minExtents.x || cp.x > terrBounds.maxExtents.x ||
  989. cp.y < terrBounds.minExtents.y || cp.y > terrBounds.maxExtents.y )
  990. continue;
  991. terrainBlock = terrain;
  992. break;
  993. }
  994. }
  995. // This should only happen if the generation went off
  996. // the edge of the terrain blocks.
  997. if ( !terrainBlock )
  998. continue;
  999. terrainLM = terrainBlock->getLightMap();
  1000. pos = terrainBlock->getPosition();
  1001. terrainSquareSize = (F32)terrainBlock->getSquareSize();
  1002. oneOverTerrainLength = 1.0f / terrainBlock->getWorldBlockSize();
  1003. oneOverTerrainSquareSize = 1.0f / terrainSquareSize;
  1004. // The size is calculated using an exponent to control
  1005. // the frequency between min and max sizes.
  1006. sizeExponent = mClampF( mPow( rand.randF(), mSizeExponent[type] ), 0.0f, 1.0f );
  1007. size = mSizeMin[type] + ( typeSizeRange * sizeExponent );
  1008. // Generate a random z rotation.
  1009. rotation = rand.randF() * M_2PI_F;
  1010. // Flip the billboard now for the next generation.
  1011. flipBB *= -1.0f;
  1012. PROFILE_START( GroundCover_TerrainRayCast );
  1013. // Transform billboard point into terrain's frame of reference.
  1014. Point3F pp = Point3F(cp.x, cp.y, 0);
  1015. terrainBlock->getWorldTransform().mulP(pp);
  1016. hit = terrainBlock->getNormalHeightMaterial( Point2F ( pp.x, pp.y ),
  1017. &normal, &h, matName );
  1018. PROFILE_END(); // GroundCover_TerrainRayCast
  1019. // TODO: When did we loose the world space elevation when
  1020. // getting the terrain height?
  1021. h += pos.z + mZOffset;
  1022. if ( !hit || h > typeMaxElevation || h < typeMinElevation ||
  1023. ( typeLayer[0] && !typeInvertLayer && matName != typeLayer ) ||
  1024. ( typeLayer[0] && typeInvertLayer && matName == typeLayer ) )
  1025. continue;
  1026. // Do we need to check slope?
  1027. if ( !mIsZero( typeMaxSlope ) )
  1028. {
  1029. if (mAcos(normal.z) > mDegToRad(typeMaxSlope))
  1030. continue;
  1031. }
  1032. if (!mIsZero(typeMinSlope))
  1033. {
  1034. if (mAcos(normal.z) < mDegToRad(typeMinSlope))
  1035. continue;
  1036. }
  1037. point.set( cp.x, cp.y, h );
  1038. p.point = point;
  1039. p.rotation = rotation;
  1040. p.normal = normal;
  1041. if (!typeConformToNormal)
  1042. {
  1043. p.normal.y = 0;
  1044. p.normal.x = 0;
  1045. }
  1046. p.normal.x += rand.randF(typeMinRotX, typeMaxRotX);
  1047. p.normal.y += rand.randF(typeMinRotY, typeMaxRotY);
  1048. // Grab the terrain lightmap color at this position.
  1049. //
  1050. // TODO: Can't we remove this test? The terrain
  1051. // lightmap should never be null... NEVER!
  1052. //
  1053. if ( terrainLM )
  1054. {
  1055. // TODO: We could probably call terrainLM->getBits()
  1056. // once outside the loop then pre-calculate the scalar
  1057. // for converting a world position into a lexel...
  1058. // avoiding the extra protections inside of sampleTexel().
  1059. uv.x = (point.x + pos.x) * oneOverTerrainLength;
  1060. uv.y = (point.y + pos.y) * oneOverTerrainLength;
  1061. uv.x -= mFloor(uv.x);
  1062. uv.y -= mFloor(uv.y);
  1063. p.lmColor = terrainLM->sampleTexel(uv.x,uv.y);
  1064. }
  1065. // Put it into the right list by type.
  1066. //
  1067. // TODO: Could we break up the generation into
  1068. // two separate loops for shapes and billboards
  1069. // and gain performance?
  1070. //
  1071. if ( typeIsShape )
  1072. {
  1073. // TODO: Convert the size into a real size... not scale!
  1074. // TODO: We could probably cache the shape bounds
  1075. // into a primitive array and avoid the double pointer
  1076. // dereference per placement.
  1077. p.size.set( size, size, size );
  1078. p.worldBox = typeShapeBounds;
  1079. p.worldBox.minExtents *= size;
  1080. p.worldBox.maxExtents *= size;
  1081. p.worldBox.minExtents += point;
  1082. p.worldBox.maxExtents += point;
  1083. cell->mShapes.push_back( p );
  1084. }
  1085. else
  1086. {
  1087. p.size.y = size;
  1088. p.size.x = size * flipBB * mBillboardAspectScales[type];
  1089. p.worldBox.maxExtents = p.worldBox.minExtents = point;
  1090. cell->mBillboards.push_back( p );
  1091. }
  1092. // Update the render bounds.
  1093. if ( firstElem )
  1094. {
  1095. renderBounds = p.worldBox;
  1096. firstElem = false;
  1097. }
  1098. else
  1099. {
  1100. renderBounds.extend( p.worldBox.minExtents );
  1101. renderBounds.extend( p.worldBox.maxExtents );
  1102. }
  1103. } // for ( S32 i=0; i < typeCount; i++ )
  1104. } // for ( U32 type=0; type < NumCoverTypes; type++ )
  1105. cell->mRenderBounds = renderBounds;
  1106. cell->mBounds.minExtents.z = renderBounds.minExtents.z;
  1107. cell->mBounds.maxExtents.z = renderBounds.maxExtents.z;
  1108. return cell;
  1109. }
  1110. void GroundCover::onTerrainUpdated( U32 flags, TerrainBlock *tblock, const Point2I& min, const Point2I& max )
  1111. {
  1112. if ( isServerObject() )
  1113. return;
  1114. // Free all the cells if we've gotten a lightmap update.
  1115. if ( flags & TerrainBlock::LightmapUpdate )
  1116. {
  1117. _freeCells();
  1118. return;
  1119. }
  1120. // TODO: EmptyUpdate doesn't work yet... fix editor/terrain.
  1121. // If this is a height or opacity update only clear
  1122. // the cells that have changed.
  1123. if ( flags & TerrainBlock::HeightmapUpdate ||
  1124. flags & TerrainBlock::LayersUpdate ||
  1125. flags & TerrainBlock::EmptyUpdate )
  1126. {
  1127. // Convert the min and max into world space.
  1128. const F32 size = tblock->getSquareSize();
  1129. const Point3F pos = tblock->getPosition();
  1130. // TODO: I don't think this works right with tiling!
  1131. Box3F dirty( F32( min.x * size ) + pos.x, F32( min.y * size ) + pos.y, 0.0f,
  1132. F32( max.x * size ) + pos.x, F32( max.y * size ) + pos.y, 0.0f );
  1133. // Now free any cells that overlap it!
  1134. for ( S32 i = 0; i < mCellGrid.size(); i++ )
  1135. {
  1136. GroundCoverCell* cell = mCellGrid[ i ];
  1137. if ( !cell )
  1138. continue;
  1139. const Box3F& bounds = cell->getBounds();
  1140. dirty.minExtents.z = bounds.minExtents.z;
  1141. dirty.maxExtents.z = bounds.maxExtents.z;
  1142. if ( bounds.isOverlapped( dirty ) )
  1143. {
  1144. mCellGrid[ i ] = NULL;
  1145. _recycleCell( cell );
  1146. }
  1147. }
  1148. }
  1149. }
  1150. void GroundCover::_updateCoverGrid( const Frustum &culler )
  1151. {
  1152. PROFILE_SCOPE( GroundCover_UpdateCoverGrid );
  1153. mGridSize = getMax( mGridSize, (U32)2 );
  1154. // How many cells in the grid?
  1155. const U32 cells = mGridSize * mGridSize;
  1156. // Whats the max placement count for each cell considering
  1157. // the grid size and quality scale LOD value.
  1158. const S32 placementCount = getMax( ( (F32)mMaxPlacement * smDensityScale ) / F32( mGridSize * mGridSize ), 0.0f );
  1159. // If the cell grid isn't sized or the placement count
  1160. // changed (most likely because of quality lod) then we
  1161. // need to initialize the system again.
  1162. if ( mCellGrid.empty() || placementCount != mLastPlacementCount )
  1163. {
  1164. _initialize( cells, placementCount );
  1165. mLastPlacementCount = placementCount;
  1166. }
  1167. // Without a count... we don't function at all.
  1168. if ( placementCount == 0 )
  1169. return;
  1170. // Clear the scratch grid.
  1171. dMemset( mScratchGrid.address(), 0, mScratchGrid.memSize() );
  1172. // Calculate the normal cell size here.
  1173. const F32 cellSize = ( mRadius * 2.0f ) / (F32)(mGridSize - 1);
  1174. // Figure out the root index of the new grid based on the camera position.
  1175. Point2I index( (S32)mFloor( ( culler.getPosition().x - mRadius ) / cellSize ),
  1176. (S32)mFloor( ( culler.getPosition().y - mRadius ) / cellSize ) );
  1177. // Figure out the cell shift between the old and new grid positions.
  1178. Point2I shift = mGridIndex - index;
  1179. // If we've shifted more than one in either axis then we've warped.
  1180. bool didWarp = shift.x > 1 || shift.x < -1 ||
  1181. shift.y > 1 || shift.y < -1 ? true : false;
  1182. // Go thru the grid shifting each cell we find and
  1183. // placing them in the scratch grid.
  1184. for ( S32 i = 0; i < mCellGrid.size(); i++ )
  1185. {
  1186. GroundCoverCell* cell = mCellGrid[ i ];
  1187. if ( !cell )
  1188. continue;
  1189. // Whats our new index?
  1190. Point2I newIndex = cell->shiftIndex( shift );
  1191. // Is this cell outside of the new grid?
  1192. if ( newIndex.x < 0 || newIndex.x >= mGridSize ||
  1193. newIndex.y < 0 || newIndex.y >= mGridSize )
  1194. {
  1195. _recycleCell( cell );
  1196. continue;
  1197. }
  1198. // Place the cell in the scratch grid.
  1199. mScratchGrid[ ( newIndex.y * mGridSize ) + newIndex.x ] = cell;
  1200. }
  1201. // Get the terrain elevation range for setting the default cell bounds.
  1202. F32 terrainMinHeight = -5000.0f,
  1203. terrainMaxHeight = 5000.0f;
  1204. // Go thru the scratch grid copying each cell back to the
  1205. // cell grid and creating new cells as needed.
  1206. //
  1207. // By limiting ourselves to only one new cell generation per
  1208. // update we're lowering the performance hiccup during movement
  1209. // without getting into the complexity of threading. The delay
  1210. // in generation is rarely noticeable in normal play.
  1211. //
  1212. // The only caveat is that we need to generate the entire visible
  1213. // grid when we warp.
  1214. U32 cellsGenerated = 0;
  1215. for ( S32 i = 0; i < mScratchGrid.size(); i++ )
  1216. {
  1217. GroundCoverCell* cell = mScratchGrid[ i ];
  1218. if ( !cell && ( cellsGenerated == 0 || didWarp ) )
  1219. {
  1220. // Get the index point of this new cell.
  1221. S32 y = i / mGridSize;
  1222. S32 x = i - ( y * mGridSize );
  1223. Point2I newIndex = index + Point2I( x, y );
  1224. // What will be the world placement bounds for this cell.
  1225. Box3F bounds;
  1226. bounds.minExtents.set( newIndex.x * cellSize, newIndex.y * cellSize, terrainMinHeight );
  1227. bounds.maxExtents.set( bounds.minExtents.x + cellSize, bounds.minExtents.y + cellSize, terrainMaxHeight );
  1228. if ( mCuller.isCulled( bounds ) )
  1229. {
  1230. mCellGrid[ i ] = NULL;
  1231. continue;
  1232. }
  1233. // We need to allocate a new cell.
  1234. //
  1235. // TODO: This is the expensive call and where we should optimize. In
  1236. // particular the next best optimization would be to take advantage of
  1237. // multiple cores so that we can generate all the cells in one update.
  1238. //
  1239. // Instead of generating the cell here we would allocate a cell and stick
  1240. // it into a thread safe queue (maybe lockless) as well as the mCellGrid.
  1241. // Once all were allocated we would do something like this...
  1242. //
  1243. // TorqueParallelProcess( cellsToGenerateQueue, _generateCell );
  1244. //
  1245. // Internally this function would pass the queue to some global pre-allocated
  1246. // worker threads which are locked to a particular core. While the main
  1247. // thread waits for the worker threads to finish it will process cells itself.
  1248. //
  1249. cell = _generateCell( newIndex - index,
  1250. bounds,
  1251. placementCount,
  1252. mRandomSeed + mAbs( newIndex.x ) + mAbs( newIndex.y ) );
  1253. // Increment our generation count.
  1254. if ( cell )
  1255. ++cellsGenerated;
  1256. }
  1257. mCellGrid[ i ] = cell;
  1258. }
  1259. // Store the new grid index.
  1260. mGridIndex = index;
  1261. }
  1262. void GroundCover::prepRenderImage( SceneRenderState *state )
  1263. {
  1264. // Reset stats each time we hit the diffuse pass.
  1265. if (mMaterialInst == nullptr)
  1266. return;
  1267. if( state->isDiffusePass() )
  1268. {
  1269. smStatRenderedCells = 0;
  1270. smStatRenderedBillboards = 0;
  1271. smStatRenderedBatches = 0;
  1272. smStatRenderedShapes = 0;
  1273. }
  1274. // TODO: Make sure that the ground cover stops rendering
  1275. // if you're inside a zoned interior.
  1276. if ( state->isReflectPass() && mReflectRadiusScale <= 0.0f )
  1277. return;
  1278. // Nothing to do if this is a shadow pass and shapes in this GoundCover
  1279. // should not be casting shadows.
  1280. if( state->isShadowPass() && !mShapesCastShadows )
  1281. return;
  1282. GFXTransformSaver saver;
  1283. // Setup the frustum culler.
  1284. if ( ( mCuller.getPosition().isZero() || !mDebugLockFrustum ) && !state->isShadowPass() )
  1285. mCuller = state->getCullingFrustum();
  1286. // Update the cells, but only during the diffuse pass.
  1287. // We don't want cell generation to thrash when the reflection camera
  1288. // position doesn't match the diffuse camera!
  1289. if ( state->isDiffusePass() )
  1290. _updateCoverGrid( mCuller );
  1291. // Render billboards but not into shadow passes.
  1292. if ( !state->isShadowPass() && mMaterialInst->isValid() && !mDebugNoBillboards )
  1293. {
  1294. PROFILE_SCOPE( GroundCover_RenderBillboards );
  1295. // Take zoom into account.
  1296. F32 screenScale = state->getWorldToScreenScale().y / state->getViewport().extent.y;
  1297. // Set the far distance for billboards.
  1298. F32 radius = mRadius * smFadeScale;
  1299. mCuller.setFarDist(radius * screenScale );
  1300. F32 cullScale = 1.0f;
  1301. if ( state->isReflectPass() )
  1302. cullScale = mReflectRadiusScale;
  1303. // Setup our shader const data.
  1304. // Must be done prior to submitting our render instance.
  1305. mShaderConstData.fadeInfo.set( mFadeRadius * smFadeScale * cullScale * screenScale, radius * cullScale * screenScale );
  1306. const F32 simTime = Sim::getCurrentTime() * 0.001f;
  1307. mShaderConstData.gustInfo.set( mWindGustLength, mWindGustFrequency * simTime, mWindGustStrength );
  1308. mShaderConstData.turbInfo.set( mWindTurbulenceFrequency * simTime, mWindTurbulenceStrength );
  1309. // Use the camera's forward vector to calculate the camera's right
  1310. // and up vectors. This removes any camera banking from affecting
  1311. // the ground cover.
  1312. const MatrixF &camMat = state->getDiffuseCameraTransform();
  1313. Point3F camDir, camUp, camRight;
  1314. camMat.getColumn( 1, &camDir );
  1315. mCross( camDir, Point3F::UnitZ, &camRight );
  1316. if ( camRight.magnitudeSafe() == 0.0f )
  1317. {
  1318. camRight.set( 0.0f, -1.0f, 0.0f );
  1319. }
  1320. camRight.normalizeSafe();
  1321. mCross( camRight, camDir, &camUp );
  1322. // Limit the camera up vector to keep the billboards
  1323. // from leaning too far down into the terrain.
  1324. VectorF lookDir( camDir.x, camDir.y, 0.0f );
  1325. F32 angle;
  1326. if ( !lookDir.isZero() )
  1327. {
  1328. lookDir.normalize();
  1329. angle = mAcos( mDot( camUp, lookDir ) );
  1330. }
  1331. else
  1332. {
  1333. angle = camDir.z < 0.0f ? 0.0f : ( M_PI_F / 2.0f );
  1334. }
  1335. const F32 maxBillboardTiltRads = mDegToRad( mMaxBillboardTiltAngle );
  1336. if ( angle < (M_PI_F / 2.0f) - maxBillboardTiltRads )
  1337. {
  1338. QuatF quat( AngAxisF( camRight, maxBillboardTiltRads ) );
  1339. quat.mulP( VectorF( 0.0f, 0.0f, 1.0f ), &camUp );
  1340. }
  1341. mShaderConstData.camRight = camRight;
  1342. mShaderConstData.camUp = camUp;
  1343. // Cull and submit render instances for cells.
  1344. for ( S32 i = 0; i < mCellGrid.size(); i++ )
  1345. {
  1346. GroundCoverCell* cell = mCellGrid[ i ];
  1347. if ( !cell )
  1348. continue;
  1349. if ( mCuller.isCulled( cell->getRenderBounds() ) )
  1350. continue;
  1351. cell->renderBillboards( state, mMaterialInst, &mPrimBuffer );
  1352. }
  1353. }
  1354. // Render TS shapes.
  1355. if ( !mDebugNoShapes )
  1356. {
  1357. // Prepare to render the grid shapes.
  1358. PROFILE_SCOPE(GroundCover_RenderShapes);
  1359. // Set up our TS render state.
  1360. TSRenderState rdata;
  1361. rdata.setSceneState( state );
  1362. // We might have some forward lit materials
  1363. // so pass down a query to gather lights.
  1364. LightQuery query;
  1365. rdata.setLightQuery( &query );
  1366. // TODO: Add a special fade out for DTS?
  1367. mCuller.setFarDist( mShapeCullRadius*smFadeScale);
  1368. for ( S32 i = 0; i < mCellGrid.size(); i++ )
  1369. {
  1370. GroundCoverCell* cell = mCellGrid[ i ];
  1371. if ( !cell || mDebugNoShapes )
  1372. continue;
  1373. const Box3F &renderBounds = cell->getRenderBounds();
  1374. U32 clipMask = mCuller.testPlanes( renderBounds, Frustum::PlaneMaskAll );
  1375. if ( clipMask == -1 )
  1376. continue;
  1377. smStatRenderedCells++;
  1378. // Use the cell bounds as the light query volume.
  1379. //
  1380. // This means all forward lit items in this cell will
  1381. // get the same lights, but it performs much better.
  1382. query.init( renderBounds );
  1383. // Render the shapes in this cell... only pass the culler if the
  1384. // cell wasn't fully within the frustum.
  1385. smStatRenderedShapes += cell->renderShapes(
  1386. rdata,
  1387. clipMask != 0 ? &mCuller : NULL,
  1388. mShapeInstances );
  1389. }
  1390. }
  1391. if ( mDebugRenderCells )
  1392. {
  1393. RenderPassManager *pass = state->getRenderPass();
  1394. ObjectRenderInst *ri = pass->allocInst<ObjectRenderInst>();
  1395. ri->type = RenderPassManager::RIT_Editor;
  1396. ri->renderDelegate.bind( this, &GroundCover::_debugRender );
  1397. pass->addInst( ri );
  1398. }
  1399. }
  1400. void GroundCover::_debugRender( ObjectRenderInst *ri, SceneRenderState *state, BaseMatInstance *overrideMat )
  1401. {
  1402. GFXDrawUtil* drawer = GFX->getDrawUtil();
  1403. GFXStateBlockDesc desc;
  1404. desc.setZReadWrite( true, false );
  1405. desc.setBlend( true );
  1406. desc.fillMode = GFXFillWireframe;
  1407. for ( S32 i = 0; i < mCellGrid.size(); i++ )
  1408. {
  1409. GroundCoverCell* cell = mCellGrid[ i ];
  1410. if ( !cell || ( cell->mBillboards.size() + cell->mShapes.size() ) == 0 )
  1411. continue;
  1412. if ( mCuller.isCulled( cell->getRenderBounds() ) )
  1413. continue;
  1414. drawer->drawCube( desc, cell->getRenderBounds().getExtents(), cell->getRenderBounds().getCenter(), ColorI( 0, 255, 0 ) );
  1415. }
  1416. }