groundCover.cpp 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759
  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. docsURL;
  435. addGroup( "GroundCover General" );
  436. INITPERSISTFIELD_MATERIALASSET(Material, GroundCover, "Material used by all GroundCover segments.");
  437. addField( "radius", TypeF32, Offset( mRadius, GroundCover ), "Outer generation radius from the current camera position." );
  438. addField( "dissolveRadius",TypeF32, Offset( mFadeRadius, GroundCover ), "This is less than or equal to radius and defines when fading of cover elements begins." );
  439. addField( "reflectScale", TypeF32, Offset( mReflectRadiusScale, GroundCover ), "Scales the various culling radii when rendering a reflection. Typically for water." );
  440. addField( "gridSize", TypeS32, Offset( mGridSize, GroundCover ), "The number of cells per axis in the grid." );
  441. addField( "zOffset", TypeF32, Offset( mZOffset, GroundCover ), "Offset along the Z axis to render the ground cover." );
  442. addField( "seed", TypeS32, Offset( mRandomSeed, GroundCover ), "This RNG seed is saved and sent to clients for generating the same cover." );
  443. addField( "maxElements", TypeS32, Offset( mMaxPlacement, GroundCover ), "The maximum amount of cover elements to include in the grid at any one time." );
  444. addField( "maxBillboardTiltAngle", TypeF32, Offset( mMaxBillboardTiltAngle, GroundCover ),"The maximum amout of degrees the billboard will tilt down to match the camera." );
  445. addField( "shapeCullRadius", TypeF32, Offset( mShapeCullRadius, GroundCover ), "This is the distance at which DTS elements are completely culled out." );
  446. addField( "shapesCastShadows", TypeBool, Offset( mShapesCastShadows, GroundCover ), "Whether DTS elements should cast shadows or not." );
  447. addArray( "Types", MAX_COVERTYPES );
  448. addField( "billboardUVs", TypeRectUV, Offset( mBillboardRects, GroundCover ), MAX_COVERTYPES, "Subset material UV coordinates for this cover billboard." );
  449. addField("shapeFilename", TypeFilename, Offset(mShapeName, GroundCover), MAX_COVERTYPES, "The cover shape filename. [Optional]", AbstractClassRep::FIELD_HideInInspectors);
  450. INITPERSISTFIELD_SHAPEASSET_ARRAY(Shape, MAX_COVERTYPES, GroundCover, "The cover shape. [Optional]");
  451. addField( "layer", TypeTerrainMaterialAssetId, Offset( mLayer, GroundCover ), MAX_COVERTYPES, "Terrain material assetId to limit coverage to, or blank to not limit." );
  452. addField( "invertLayer", TypeBool, Offset( mInvertLayer, GroundCover ), MAX_COVERTYPES, "Indicates that the terrain material index given in 'layer' is an exclusion mask." );
  453. addField( "probability", TypeF32, Offset( mProbability, GroundCover ), MAX_COVERTYPES, "The probability of one cover type verses another (relative to all cover types)." );
  454. addField( "sizeMin", TypeF32, Offset( mSizeMin, GroundCover ), MAX_COVERTYPES, "The minimum random size for each cover type." );
  455. addField( "sizeMax", TypeF32, Offset( mSizeMax, GroundCover ), MAX_COVERTYPES, "The maximum random size of this cover type." );
  456. addField( "sizeExponent", TypeF32, Offset( mSizeExponent, GroundCover ), MAX_COVERTYPES, "An exponent used to bias between the minimum and maximum random sizes." );
  457. addField( "windScale", TypeF32, Offset( mWindScale, GroundCover ), MAX_COVERTYPES, "The wind effect scale." );
  458. addField( "minSlope", TypeF32, Offset(mMinSlope, GroundCover), MAX_COVERTYPES, "The minimum slope angle in degrees for placement.");
  459. addField( "maxSlope", TypeF32, Offset( mMaxSlope, GroundCover ), MAX_COVERTYPES, "The maximum slope angle in degrees for placement." );
  460. addField("conformToNormal",TypeBool, Offset(mConformToNormal, GroundCover), MAX_COVERTYPES, "Use the terrain's slope for angle");
  461. addField("minRotX", TypeF32, Offset(mMinRotX, GroundCover), MAX_COVERTYPES, "minumum amount of rotation along the X axis to add");
  462. addField("maxRotX", TypeF32, Offset(mMaxRotX, GroundCover), MAX_COVERTYPES, "maximum amount of rotation along the X axis to add");
  463. addField("minRotY", TypeF32, Offset(mMinRotY, GroundCover), MAX_COVERTYPES, "minumum amount of rotation along the Y axis to add");
  464. addField("maxRotY", TypeF32, Offset(mMaxRotY, GroundCover), MAX_COVERTYPES, "maximum amount of rotation along the Y axis to add");
  465. addField( "minElevation", TypeF32, Offset( mMinElevation, GroundCover ), MAX_COVERTYPES, "The minimum world space elevation for placement." );
  466. addField( "maxElevation", TypeF32, Offset( mMaxElevation, GroundCover ), MAX_COVERTYPES, "The maximum world space elevation for placement." );
  467. addField( "minClumpCount", TypeS32, Offset( mMinClumpCount, GroundCover ), MAX_COVERTYPES, "The minimum amount of elements in a clump." );
  468. addField( "maxClumpCount", TypeS32, Offset( mMaxClumpCount, GroundCover ), MAX_COVERTYPES, "The maximum amount of elements in a clump." );
  469. addField( "clumpExponent", TypeF32, Offset( mClumpCountExponent, GroundCover ), MAX_COVERTYPES, "An exponent used to bias between the minimum and maximum clump counts for a particular clump." );
  470. addField( "clumpRadius", TypeF32, Offset( mClumpRadius, GroundCover ), MAX_COVERTYPES, "The maximum clump radius." );
  471. endArray( "Types" );
  472. endGroup( "GroundCover General" );
  473. addGroup( "GroundCover Wind" );
  474. addField( "windDirection", TypePoint2F, Offset( mWindDirection, GroundCover ), "The direction of the wind." );
  475. addField( "windGustLength", TypeF32, Offset( mWindGustLength, GroundCover ), "The length in meters between peaks in the wind gust." );
  476. addField( "windGustFrequency",TypeF32, Offset( mWindGustFrequency, GroundCover ), "Controls how often the wind gust peaks per second." );
  477. addField( "windGustStrength", TypeF32, Offset( mWindGustStrength, GroundCover ), "The maximum distance in meters that the peak wind gust will displace an element." );
  478. addField( "windTurbulenceFrequency", TypeF32, Offset( mWindTurbulenceFrequency, GroundCover ),"Controls the overall rapidity of the wind turbulence." );
  479. addField( "windTurbulenceStrength", TypeF32, Offset( mWindTurbulenceStrength, GroundCover ), "The maximum distance in meters that the turbulence can displace a ground cover element." );
  480. endGroup( "GroundCover Wind" );
  481. addGroup( "GroundCover Debug" );
  482. addField( "lockFrustum", TypeBool, Offset( mDebugLockFrustum, GroundCover ), "Debug parameter for locking the culling frustum which will freeze the cover generation." );
  483. addField( "renderCells", TypeBool, Offset( mDebugRenderCells, GroundCover ), "Debug parameter for displaying the grid cells." );
  484. addField( "noBillboards", TypeBool, Offset( mDebugNoBillboards, GroundCover ), "Debug parameter for turning off billboard rendering." );
  485. addField( "noShapes", TypeBool, Offset( mDebugNoShapes, GroundCover ), "Debug parameter for turning off shape rendering." );
  486. endGroup( "GroundCover Debug" );
  487. Parent::initPersistFields();
  488. }
  489. void GroundCover::consoleInit()
  490. {
  491. Con::addVariable( "$pref::GroundCover::densityScale", TypeF32, &smDensityScale, "A global LOD scalar which can reduce the overall density of placed GroundCover.\n"
  492. "@ingroup Foliage\n");
  493. Con::addVariable("$pref::GroundCover::fadeScale", TypeF32, &smFadeScale, "A global fade scalar which can reduce the overall rendered distance of placed GroundCover.\n"
  494. "@ingroup Foliage\n");
  495. Con::addVariable( "$GroundCover::renderedCells", TypeS32, &smStatRenderedCells, "Stat for number of rendered cells.\n"
  496. "@ingroup Foliage\n");
  497. Con::addVariable( "$GroundCover::renderedBillboards", TypeS32, &smStatRenderedBillboards, "Stat for number of rendered billboards.\n"
  498. "@ingroup Foliage\n");
  499. Con::addVariable( "$GroundCover::renderedBatches", TypeS32, &smStatRenderedBatches, "Stat for number of rendered billboard batches.\n"
  500. "@ingroup Foliage\n");
  501. Con::addVariable( "$GroundCover::renderedShapes", TypeS32, &smStatRenderedShapes, "Stat for number of rendered shapes.\n"
  502. "@ingroup Foliage\n");
  503. Parent::consoleInit();
  504. }
  505. bool GroundCover::onAdd()
  506. {
  507. if (!Parent::onAdd())
  508. return false;
  509. // We don't use any bounds.
  510. setGlobalBounds();
  511. resetWorldBox();
  512. // Prepare some client side things.
  513. if ( isClientObject() )
  514. {
  515. _initMaterial();
  516. _initShapes();
  517. // Hook ourselves up to get terrain change notifications.
  518. TerrainBlock::smUpdateSignal.notify( this, &GroundCover::onTerrainUpdated );
  519. }
  520. addToScene();
  521. return true;
  522. }
  523. void GroundCover::onRemove()
  524. {
  525. Parent::onRemove();
  526. _deleteCells();
  527. _deleteShapes();
  528. if ( isClientObject() )
  529. {
  530. TerrainBlock::smUpdateSignal.remove( this, &GroundCover::onTerrainUpdated );
  531. }
  532. removeFromScene();
  533. }
  534. void GroundCover::inspectPostApply()
  535. {
  536. Parent::inspectPostApply();
  537. // We flag all the parameters as changed because
  538. // we're feeling lazy and there is not a good way
  539. // to track what parameters changed.
  540. //
  541. // TODO: Add a mask bit option to addField() and/or
  542. // addGroup() which is passed to inspectPostApply
  543. // for detection of changed elements.
  544. //
  545. setMaskBits(U32(-1) );
  546. }
  547. U32 GroundCover::packUpdate( NetConnection *connection, U32 mask, BitStream *stream )
  548. {
  549. U32 retMask = Parent::packUpdate( connection, mask, stream );
  550. if (stream->writeFlag(mask & InitialUpdateMask))
  551. {
  552. // TODO: We could probably optimize a few of these
  553. // based on reasonable units at some point.
  554. PACK_ASSET(connection, Material);
  555. stream->write( mRadius );
  556. stream->write( mZOffset );
  557. stream->write( mFadeRadius );
  558. stream->write( mShapeCullRadius );
  559. stream->writeFlag( mShapesCastShadows );
  560. stream->write( mReflectRadiusScale );
  561. stream->write( mGridSize );
  562. stream->write( mRandomSeed );
  563. stream->write( mMaxPlacement );
  564. stream->write( mMaxBillboardTiltAngle );
  565. stream->write( mWindDirection.x );
  566. stream->write( mWindDirection.y );
  567. stream->write( mWindGustLength );
  568. stream->write( mWindGustFrequency );
  569. stream->write( mWindGustStrength );
  570. stream->write( mWindTurbulenceFrequency );
  571. stream->write( mWindTurbulenceStrength );
  572. for ( S32 i=0; i < MAX_COVERTYPES; i++ )
  573. {
  574. stream->write( mProbability[i] );
  575. stream->write( mSizeMin[i] );
  576. stream->write( mSizeMax[i] );
  577. stream->write( mSizeExponent[i] );
  578. stream->write( mWindScale[i] );
  579. stream->write( mMinSlope[i] );
  580. stream->write( mMaxSlope[i] );
  581. stream->writeFlag(mConformToNormal[i]);
  582. stream->write(mMinRotX[i]);
  583. stream->write(mMaxRotX[i]);
  584. stream->write(mMinRotY[i]);
  585. stream->write(mMaxRotY[i]);
  586. stream->write( mMinElevation[i] );
  587. stream->write( mMaxElevation[i] );
  588. stream->writeString( mLayer[i] );
  589. stream->writeFlag( mInvertLayer[i] );
  590. stream->write( mMinClumpCount[i] );
  591. stream->write( mMaxClumpCount[i] );
  592. stream->write( mClumpCountExponent[i] );
  593. stream->write( mClumpRadius[i] );
  594. stream->write( mBillboardRects[i].point.x );
  595. stream->write( mBillboardRects[i].point.y );
  596. stream->write( mBillboardRects[i].extent.x );
  597. stream->write( mBillboardRects[i].extent.y );
  598. PACK_ASSET_ARRAY(connection, Shape, i);
  599. }
  600. stream->writeFlag( mDebugRenderCells );
  601. stream->writeFlag( mDebugNoBillboards );
  602. stream->writeFlag( mDebugNoShapes );
  603. stream->writeFlag( mDebugLockFrustum );
  604. }
  605. return retMask;
  606. }
  607. void GroundCover::unpackUpdate( NetConnection *connection, BitStream *stream )
  608. {
  609. Parent::unpackUpdate( connection, stream );
  610. if (stream->readFlag())
  611. {
  612. UNPACK_ASSET(connection, Material);
  613. stream->read( &mRadius );
  614. stream->read( &mZOffset );
  615. stream->read( &mFadeRadius );
  616. stream->read( &mShapeCullRadius );
  617. mShapesCastShadows = stream->readFlag();
  618. stream->read( &mReflectRadiusScale );
  619. stream->read( &mGridSize );
  620. stream->read( &mRandomSeed );
  621. stream->read( &mMaxPlacement );
  622. stream->read( &mMaxBillboardTiltAngle );
  623. stream->read( &mWindDirection.x );
  624. stream->read( &mWindDirection.y );
  625. stream->read( &mWindGustLength );
  626. stream->read( &mWindGustFrequency );
  627. stream->read( &mWindGustStrength );
  628. stream->read( &mWindTurbulenceFrequency );
  629. stream->read( &mWindTurbulenceStrength );
  630. for ( S32 i=0; i < MAX_COVERTYPES; i++ )
  631. {
  632. stream->read( &mProbability[i] );
  633. stream->read( &mSizeMin[i] );
  634. stream->read( &mSizeMax[i] );
  635. stream->read( &mSizeExponent[i] );
  636. stream->read( &mWindScale[i] );
  637. stream->read( &mMinSlope[i] );
  638. stream->read( &mMaxSlope[i] );
  639. mConformToNormal[i] = stream->readFlag();
  640. stream->read(&mMinRotX[i]);
  641. stream->read(&mMaxRotX[i]);
  642. stream->read(&mMinRotY[i]);
  643. stream->read(&mMaxRotY[i]);
  644. stream->read( &mMinElevation[i] );
  645. stream->read( &mMaxElevation[i] );
  646. mLayer[i] = stream->readSTString();
  647. mInvertLayer[i] = stream->readFlag();
  648. stream->read( &mMinClumpCount[i] );
  649. stream->read( &mMaxClumpCount[i] );
  650. stream->read( &mClumpCountExponent[i] );
  651. stream->read( &mClumpRadius[i] );
  652. stream->read( &mBillboardRects[i].point.x );
  653. stream->read( &mBillboardRects[i].point.y );
  654. stream->read( &mBillboardRects[i].extent.x );
  655. stream->read( &mBillboardRects[i].extent.y );
  656. UNPACK_ASSET_ARRAY(connection, Shape, i);
  657. }
  658. mDebugRenderCells = stream->readFlag();
  659. mDebugNoBillboards = stream->readFlag();
  660. mDebugNoShapes = stream->readFlag();
  661. mDebugLockFrustum = stream->readFlag();
  662. // We have no way to easily know what changed, so by clearing
  663. // the cells we force a reinit and regeneration of the cells.
  664. // It's sloppy, but it works for now.
  665. _freeCells();
  666. if ( isProperlyAdded() )
  667. _initMaterial();
  668. }
  669. }
  670. void GroundCover::_initMaterial()
  671. {
  672. SAFE_DELETE(mMaterialInst);
  673. if (mMaterialAsset.notNull() && mMaterialAsset->getStatus() == MaterialAsset::Ok)
  674. mMaterialInst = mMaterial->createMatInstance();
  675. else
  676. mMaterialInst = MATMGR->createMatInstance("WarningMaterial");
  677. // Add our special feature that makes it all work...
  678. FeatureSet features = MATMGR->getDefaultFeatures();
  679. features.addFeature( MFT_Foliage );
  680. // Our feature requires a pointer back to this object
  681. // to properly setup its shader consts.
  682. mMaterialInst->setUserObject( this );
  683. // DO IT!
  684. mMaterialInst->init( features, getGFXVertexFormat<GCVertex>() );
  685. }
  686. void GroundCover::_initShapes()
  687. {
  688. _deleteShapes();
  689. for ( S32 i=0; i < MAX_COVERTYPES; i++ )
  690. {
  691. if ( mShapeAsset[i].isNull() || mShape[i] == nullptr)
  692. continue;
  693. if ( isClientObject() && !mShape[i]->preloadMaterialList(mShape[i].getPath()) && NetConnection::filesWereDownloaded() )
  694. {
  695. Con::warnf( "GroundCover::_initShapes() material preload failed for shape: %s", mShapeAssetId[i] );
  696. continue;
  697. }
  698. // Create the shape instance.
  699. mShapeInstances[i] = new TSShapeInstance(mShape[i], isClientObject() );
  700. }
  701. }
  702. void GroundCover::_deleteShapes()
  703. {
  704. for ( S32 i=0; i < MAX_COVERTYPES; i++ )
  705. {
  706. delete mShapeInstances[i];
  707. mShapeInstances[i] = NULL;
  708. }
  709. }
  710. void GroundCover::_deleteCells()
  711. {
  712. // Delete the allocation list.
  713. for ( S32 i=0; i < mAllocCellList.size(); i++ )
  714. delete mAllocCellList[i];
  715. mAllocCellList.clear();
  716. // Zero out the rest of the stuff.
  717. _freeCells();
  718. }
  719. void GroundCover::_freeCells()
  720. {
  721. // Zero the grid and scratch space.
  722. mCellGrid.clear();
  723. mScratchGrid.clear();
  724. // Compact things... remove excess allocated cells.
  725. const U32 maxCells = mGridSize * mGridSize;
  726. if ( mAllocCellList.size() > maxCells )
  727. {
  728. for ( S32 i=maxCells; i < mAllocCellList.size(); i++ )
  729. delete mAllocCellList[i];
  730. mAllocCellList.setSize( maxCells );
  731. }
  732. // Move all the alloced cells into the free list.
  733. mFreeCellList.clear();
  734. mFreeCellList.merge( mAllocCellList );
  735. // Release the primitive buffer.
  736. mPrimBuffer = NULL;
  737. }
  738. void GroundCover::_recycleCell( GroundCoverCell* cell )
  739. {
  740. mFreeCellList.push_back( cell );
  741. }
  742. void GroundCover::_initialize( U32 cellCount, U32 cellPlacementCount )
  743. {
  744. // Cleanup everything... we're starting over.
  745. _freeCells();
  746. _deleteShapes();
  747. // Nothing to do without a count!
  748. if ( cellPlacementCount == 0 )
  749. return;
  750. // Reset the grid sizes.
  751. mCellGrid.setSize( cellCount );
  752. dMemset( mCellGrid.address(), 0, mCellGrid.memSize() );
  753. mScratchGrid.setSize( cellCount );
  754. // Rebuild the texture aspect scales for each type.
  755. F32 textureAspect = 1.0f;
  756. if( mMaterialInst && mMaterialInst->isValid())
  757. {
  758. Material* mat = dynamic_cast<Material*>(mMaterialInst->getMaterial());
  759. if(mat)
  760. {
  761. GFXTexHandle tex;
  762. if (mat->getDiffuseMapResource(0))
  763. tex = mat->getDiffuseMapResource(0);
  764. else if (mat->getDiffuseMap(0) != StringTable->EmptyString())
  765. tex = GFXTexHandle(mat->getDiffuseMap(0), &GFXStaticTextureSRGBProfile, "GroundCover texture aspect ratio check");
  766. if(tex.isValid())
  767. {
  768. U32 w = tex.getWidth();
  769. U32 h = tex.getHeight();
  770. if(h > 0)
  771. textureAspect = F32(w) / F32(h);
  772. }
  773. }
  774. }
  775. for ( S32 i=0; i < MAX_COVERTYPES; i++ )
  776. {
  777. if ( mBillboardRects[i].extent.y > 0.0f )
  778. {
  779. mBillboardAspectScales[i] = textureAspect * mBillboardRects[i].extent.x / mBillboardRects[i].extent.y;
  780. }
  781. else
  782. mBillboardAspectScales[i] = 0.0f;
  783. }
  784. // Load the shapes again.
  785. _initShapes();
  786. // Set the primitive buffer up for the maximum placement in a cell.
  787. mPrimBuffer.set( GFX, cellPlacementCount * 6, 0, GFXBufferTypeStatic );
  788. U16 *idxBuff;
  789. mPrimBuffer.lock(&idxBuff);
  790. for ( U32 i=0; i < cellPlacementCount; i++ )
  791. {
  792. //
  793. // The vertex pattern in the VB for each
  794. // billboard is as follows...
  795. //
  796. // 0----1
  797. // |\ |
  798. // | \ |
  799. // | \ |
  800. // | \|
  801. // 3----2
  802. //
  803. // We setup the index order below to ensure
  804. // sequential, cache friendly, access.
  805. //
  806. U32 offset = i * 4;
  807. idxBuff[i*6+0] = 0 + offset;
  808. idxBuff[i*6+1] = 1 + offset;
  809. idxBuff[i*6+2] = 2 + offset;
  810. idxBuff[i*6+3] = 2 + offset;
  811. idxBuff[i*6+4] = 3 + offset;
  812. idxBuff[i*6+5] = 0 + offset;
  813. }
  814. mPrimBuffer.unlock();
  815. // Generate the normalized probability.
  816. F32 total = 0.0f;
  817. for ( S32 i=0; i < MAX_COVERTYPES; i++ )
  818. {
  819. // If the element isn't gonna render... then
  820. // set the probability to zero.
  821. if ( mShapeInstances[i] == NULL && mBillboardAspectScales[i] <= 0.0001f )
  822. {
  823. mNormalizedProbability[i] = 0.0f;
  824. }
  825. else
  826. {
  827. mNormalizedProbability[i] = mProbability[i];
  828. total += mProbability[i];
  829. }
  830. }
  831. if ( total > 0.0f )
  832. {
  833. for ( S32 i=0; i < MAX_COVERTYPES; i++ )
  834. mNormalizedProbability[i] /= total;
  835. }
  836. }
  837. GroundCoverCell* GroundCover::_generateCell( const Point2I& index,
  838. const Box3F& bounds,
  839. U32 placementCount,
  840. S32 randSeed )
  841. {
  842. PROFILE_SCOPE(GroundCover_GenerateCell);
  843. const Vector<SceneObject*> terrainBlocks = getContainer()->getTerrains();
  844. if ( terrainBlocks.empty() )
  845. return NULL;
  846. // Grab a free cell or allocate a new one.
  847. GroundCoverCell* cell;
  848. if ( mFreeCellList.empty() )
  849. {
  850. cell = new GroundCoverCell();
  851. mAllocCellList.push_back( cell );
  852. }
  853. else
  854. {
  855. cell = mFreeCellList.last();
  856. mFreeCellList.pop_back();
  857. }
  858. cell->mDirty = true;
  859. cell->mIndex = index;
  860. cell->mBounds = bounds;
  861. Point3F pos( 0, 0, 0 );
  862. Box3F renderBounds = bounds;
  863. Point3F point;
  864. Point3F normal;
  865. F32 h;
  866. Point2F cp, uv;
  867. bool hit;
  868. GroundCoverCell::Placement p;
  869. F32 rotation;
  870. F32 size;
  871. F32 sizeExponent;
  872. Point2I lpos;
  873. //F32 value;
  874. VectorF right;
  875. StringTableEntry matName = StringTable->EmptyString();
  876. bool firstElem = true;
  877. TerrainBlock *terrainBlock = NULL;
  878. cell->mBillboards.clear();
  879. cell->mBillboards.reserve( placementCount );
  880. cell->mShapes.clear();
  881. cell->mShapes.reserve( placementCount );
  882. F32 terrainSquareSize,
  883. oneOverTerrainLength,
  884. oneOverTerrainSquareSize;
  885. const GBitmap* terrainLM = NULL;
  886. // The RNG that we'll use in generation.
  887. MRandom rand( 0 );
  888. // We process one type at a time.
  889. for ( U32 type=0; type < MAX_COVERTYPES; type++ )
  890. {
  891. // How many cover elements do we need to generate for this type?
  892. const S32 typeCount = mNormalizedProbability[type] * (F32)placementCount;
  893. if ( typeCount <= 0 )
  894. continue;
  895. // Grab the terrain layer for this type.
  896. /*
  897. const TerrainDataLayer* dataLayer = NULL;
  898. const bool typeInvertLayer = mInvertLayer[type];
  899. if ( mLayer[type] > -1 )
  900. {
  901. dataLayer = mTerrainBlock->getDataLayer( mLayer[type] );
  902. if ( dataLayer )
  903. {
  904. // Do an initial check to see if we can place any place anything
  905. // at all... if the layer area for this element is empty then
  906. // there is nothing more to do.
  907. RectI area( (S32)mFloor( ( bounds.minExtents.x - pos.x ) * oneOverTerrainSquareSize ),
  908. (S32)mFloor( ( bounds.minExtents.y - pos.y ) * oneOverTerrainSquareSize ),
  909. (S32)mCeil( ( bounds.maxExtents.x - pos.x ) * oneOverTerrainSquareSize ),
  910. (S32)mCeil( ( bounds.maxExtents.y - pos.y ) * oneOverTerrainSquareSize ) );
  911. area.extent -= area.point;
  912. if ( dataLayer->testFill( area, typeInvertLayer ? 255 : 0 ) )
  913. continue;
  914. }
  915. }
  916. // If the layer is not inverted and we have no data
  917. // then we have nothing to draw.
  918. if ( !typeInvertLayer && !dataLayer )
  919. continue;
  920. */
  921. // We set the seed we were passed which is based on this grids position
  922. // in the world and add the type value. This keeps changes to one type
  923. // from effecting the outcome of the others.
  924. rand.setSeed( randSeed + type );
  925. // Setup for doing clumps.
  926. S32 clumps = 0;
  927. Point2F clumpCenter(0.0f, 0.0f);
  928. const S32 clumpMin = getMax( 1, (S32)mMinClumpCount[type] );
  929. F32 clumpExponent;
  930. // We mult this by -1 each billboard we make then use
  931. // it to scale the billboard x axis to flip them. This
  932. // essentially gives us twice the variation for free.
  933. F32 flipBB = -1.0f;
  934. // Precompute a few other type specific values.
  935. const bool typeConformToNormal = mConformToNormal[type];
  936. const F32 typeMinRotX = (mMaxRotX[type] > mMinRotX[type]) ? mMinRotX[type] : mMaxRotX[type];
  937. const F32 typeMaxRotX = (mMaxRotX[type] > mMinRotX[type]) ? mMaxRotX[type] : mMinRotX[type];
  938. const F32 typeMinRotY = (mMaxRotY[type] > mMinRotY[type]) ? mMinRotY[type] : mMaxRotY[type];
  939. const F32 typeMaxRotY = (mMaxRotY[type] > mMinRotY[type]) ? mMaxRotY[type] : mMinRotY[type];
  940. const F32 typeSizeRange = mSizeMax[type] - mSizeMin[type];
  941. const F32 typeMinSlope = mMinSlope[type];
  942. const F32 typeMaxSlope = mMaxSlope[type];
  943. const F32 typeMaxElevation = mMaxElevation[type];
  944. const F32 typeMinElevation = mMinElevation[type];
  945. const bool typeIsShape = mShapeInstances[ type ] != NULL;
  946. const Box3F typeShapeBounds = typeIsShape ? mShapeInstances[ type ]->getShape()->mBounds : Box3F();
  947. const F32 typeWindScale = mWindScale[type];
  948. StringTableEntry typeLayer = mLayer[type];
  949. const bool typeInvertLayer = mInvertLayer[type];
  950. // We can set this once here... all the placements for this are the same.
  951. p.type = type;
  952. p.windAmplitude = typeWindScale;
  953. p.lmColor.set(1.0f,1.0f,1.0f);
  954. // Generate all the cover elements for this type.
  955. for ( S32 i=0; i < typeCount; i++ )
  956. {
  957. // Do all the other random things here first as to not
  958. // disturb the random sequence if the terrain geometry
  959. // or cover layers change.
  960. // Get the random position.
  961. cp.set( rand.randF(), rand.randF() );
  962. // Prepare the clump info.
  963. clumpExponent = mClampF( mPow( rand.randF(), mClumpCountExponent[type] ), 0.0f, 1.0f );
  964. if ( clumps <= 0 )
  965. {
  966. // We're starting a new clump.
  967. clumps = ( clumpMin + mFloor( ( mMaxClumpCount[type] - clumpMin ) * clumpExponent ) ) - 1;
  968. cp.set( bounds.minExtents.x + cp.x * bounds.len_x(),
  969. bounds.minExtents.y + cp.y * bounds.len_y() );
  970. clumpCenter = cp;
  971. }
  972. else
  973. {
  974. clumps--;
  975. cp.set( clumpCenter.x - ( ( cp.x - 0.5f ) * mClumpRadius[type] ),
  976. clumpCenter.y - ( ( cp.y - 0.5f ) * mClumpRadius[type] ) );
  977. }
  978. // Which terrain do I place on?
  979. if ( terrainBlocks.size() == 1 )
  980. terrainBlock = dynamic_cast< TerrainBlock* >( terrainBlocks.first() );
  981. else
  982. {
  983. for ( U32 blockIDx = 0; blockIDx < terrainBlocks.size(); blockIDx++ )
  984. {
  985. TerrainBlock *terrain = dynamic_cast< TerrainBlock* >( terrainBlocks[ blockIDx ] );
  986. if( !terrain )
  987. continue;
  988. const Box3F &terrBounds = terrain->getWorldBox();
  989. if ( cp.x < terrBounds.minExtents.x || cp.x > terrBounds.maxExtents.x ||
  990. cp.y < terrBounds.minExtents.y || cp.y > terrBounds.maxExtents.y )
  991. continue;
  992. terrainBlock = terrain;
  993. break;
  994. }
  995. }
  996. // This should only happen if the generation went off
  997. // the edge of the terrain blocks.
  998. if ( !terrainBlock )
  999. continue;
  1000. terrainLM = terrainBlock->getLightMap();
  1001. pos = terrainBlock->getPosition();
  1002. terrainSquareSize = (F32)terrainBlock->getSquareSize();
  1003. oneOverTerrainLength = 1.0f / terrainBlock->getWorldBlockSize();
  1004. oneOverTerrainSquareSize = 1.0f / terrainSquareSize;
  1005. // The size is calculated using an exponent to control
  1006. // the frequency between min and max sizes.
  1007. sizeExponent = mClampF( mPow( rand.randF(), mSizeExponent[type] ), 0.0f, 1.0f );
  1008. size = mSizeMin[type] + ( typeSizeRange * sizeExponent );
  1009. // Generate a random z rotation.
  1010. rotation = rand.randF() * M_2PI_F;
  1011. // Flip the billboard now for the next generation.
  1012. flipBB *= -1.0f;
  1013. PROFILE_START( GroundCover_TerrainRayCast );
  1014. // Transform billboard point into terrain's frame of reference.
  1015. Point3F pp = Point3F(cp.x, cp.y, 0);
  1016. terrainBlock->getWorldTransform().mulP(pp);
  1017. hit = terrainBlock->getNormalHeightMaterial( Point2F ( pp.x, pp.y ),
  1018. &normal, &h, matName );
  1019. PROFILE_END(); // GroundCover_TerrainRayCast
  1020. // TODO: When did we loose the world space elevation when
  1021. // getting the terrain height?
  1022. h += pos.z + mZOffset;
  1023. if ( !hit || h > typeMaxElevation || h < typeMinElevation ||
  1024. ( typeLayer[0] && !typeInvertLayer && matName != typeLayer ) ||
  1025. ( typeLayer[0] && typeInvertLayer && matName == typeLayer ) )
  1026. continue;
  1027. // Do we need to check slope?
  1028. if ( !mIsZero( typeMaxSlope ) )
  1029. {
  1030. if (mAcos(normal.z) > mDegToRad(typeMaxSlope))
  1031. continue;
  1032. }
  1033. if (!mIsZero(typeMinSlope))
  1034. {
  1035. if (mAcos(normal.z) < mDegToRad(typeMinSlope))
  1036. continue;
  1037. }
  1038. point.set( cp.x, cp.y, h );
  1039. p.point = point;
  1040. p.rotation = rotation;
  1041. p.normal = normal;
  1042. if (!typeConformToNormal)
  1043. {
  1044. p.normal.y = 0;
  1045. p.normal.x = 0;
  1046. }
  1047. p.normal.x += rand.randF(typeMinRotX, typeMaxRotX);
  1048. p.normal.y += rand.randF(typeMinRotY, typeMaxRotY);
  1049. // Grab the terrain lightmap color at this position.
  1050. //
  1051. // TODO: Can't we remove this test? The terrain
  1052. // lightmap should never be null... NEVER!
  1053. //
  1054. if ( terrainLM )
  1055. {
  1056. // TODO: We could probably call terrainLM->getBits()
  1057. // once outside the loop then pre-calculate the scalar
  1058. // for converting a world position into a lexel...
  1059. // avoiding the extra protections inside of sampleTexel().
  1060. uv.x = (point.x + pos.x) * oneOverTerrainLength;
  1061. uv.y = (point.y + pos.y) * oneOverTerrainLength;
  1062. uv.x -= mFloor(uv.x);
  1063. uv.y -= mFloor(uv.y);
  1064. p.lmColor = terrainLM->sampleTexel(uv.x,uv.y);
  1065. }
  1066. // Put it into the right list by type.
  1067. //
  1068. // TODO: Could we break up the generation into
  1069. // two separate loops for shapes and billboards
  1070. // and gain performance?
  1071. //
  1072. if ( typeIsShape )
  1073. {
  1074. // TODO: Convert the size into a real size... not scale!
  1075. // TODO: We could probably cache the shape bounds
  1076. // into a primitive array and avoid the double pointer
  1077. // dereference per placement.
  1078. p.size.set( size, size, size );
  1079. p.worldBox = typeShapeBounds;
  1080. p.worldBox.minExtents *= size;
  1081. p.worldBox.maxExtents *= size;
  1082. p.worldBox.minExtents += point;
  1083. p.worldBox.maxExtents += point;
  1084. cell->mShapes.push_back( p );
  1085. }
  1086. else
  1087. {
  1088. p.size.y = size;
  1089. p.size.x = size * flipBB * mBillboardAspectScales[type];
  1090. p.worldBox.maxExtents = p.worldBox.minExtents = point;
  1091. cell->mBillboards.push_back( p );
  1092. }
  1093. // Update the render bounds.
  1094. if ( firstElem )
  1095. {
  1096. renderBounds = p.worldBox;
  1097. firstElem = false;
  1098. }
  1099. else
  1100. {
  1101. renderBounds.extend( p.worldBox.minExtents );
  1102. renderBounds.extend( p.worldBox.maxExtents );
  1103. }
  1104. } // for ( S32 i=0; i < typeCount; i++ )
  1105. } // for ( U32 type=0; type < NumCoverTypes; type++ )
  1106. cell->mRenderBounds = renderBounds;
  1107. cell->mBounds.minExtents.z = renderBounds.minExtents.z;
  1108. cell->mBounds.maxExtents.z = renderBounds.maxExtents.z;
  1109. return cell;
  1110. }
  1111. void GroundCover::onTerrainUpdated( U32 flags, TerrainBlock *tblock, const Point2I& min, const Point2I& max )
  1112. {
  1113. if ( isServerObject() )
  1114. return;
  1115. // Free all the cells if we've gotten a lightmap update.
  1116. if ( flags & TerrainBlock::LightmapUpdate )
  1117. {
  1118. _freeCells();
  1119. return;
  1120. }
  1121. // TODO: EmptyUpdate doesn't work yet... fix editor/terrain.
  1122. // If this is a height or opacity update only clear
  1123. // the cells that have changed.
  1124. if ( flags & TerrainBlock::HeightmapUpdate ||
  1125. flags & TerrainBlock::LayersUpdate ||
  1126. flags & TerrainBlock::EmptyUpdate )
  1127. {
  1128. // Convert the min and max into world space.
  1129. const F32 size = tblock->getSquareSize();
  1130. const Point3F pos = tblock->getPosition();
  1131. // TODO: I don't think this works right with tiling!
  1132. Box3F dirty( F32( min.x * size ) + pos.x, F32( min.y * size ) + pos.y, 0.0f,
  1133. F32( max.x * size ) + pos.x, F32( max.y * size ) + pos.y, 0.0f );
  1134. // Now free any cells that overlap it!
  1135. for ( S32 i = 0; i < mCellGrid.size(); i++ )
  1136. {
  1137. GroundCoverCell* cell = mCellGrid[ i ];
  1138. if ( !cell )
  1139. continue;
  1140. const Box3F& bounds = cell->getBounds();
  1141. dirty.minExtents.z = bounds.minExtents.z;
  1142. dirty.maxExtents.z = bounds.maxExtents.z;
  1143. if ( bounds.isOverlapped( dirty ) )
  1144. {
  1145. mCellGrid[ i ] = NULL;
  1146. _recycleCell( cell );
  1147. }
  1148. }
  1149. }
  1150. }
  1151. void GroundCover::_updateCoverGrid( const Frustum &culler )
  1152. {
  1153. PROFILE_SCOPE( GroundCover_UpdateCoverGrid );
  1154. mGridSize = getMax( mGridSize, (U32)2 );
  1155. // How many cells in the grid?
  1156. const U32 cells = mGridSize * mGridSize;
  1157. // Whats the max placement count for each cell considering
  1158. // the grid size and quality scale LOD value.
  1159. const S32 placementCount = getMax( ( (F32)mMaxPlacement * smDensityScale ) / F32( mGridSize * mGridSize ), 0.0f );
  1160. // If the cell grid isn't sized or the placement count
  1161. // changed (most likely because of quality lod) then we
  1162. // need to initialize the system again.
  1163. if ( mCellGrid.empty() || placementCount != mLastPlacementCount )
  1164. {
  1165. _initialize( cells, placementCount );
  1166. mLastPlacementCount = placementCount;
  1167. }
  1168. // Without a count... we don't function at all.
  1169. if ( placementCount == 0 )
  1170. return;
  1171. // Clear the scratch grid.
  1172. dMemset( mScratchGrid.address(), 0, mScratchGrid.memSize() );
  1173. // Calculate the normal cell size here.
  1174. const F32 cellSize = ( mRadius * 2.0f ) / (F32)(mGridSize - 1);
  1175. // Figure out the root index of the new grid based on the camera position.
  1176. Point2I index( (S32)mFloor( ( culler.getPosition().x - mRadius ) / cellSize ),
  1177. (S32)mFloor( ( culler.getPosition().y - mRadius ) / cellSize ) );
  1178. // Figure out the cell shift between the old and new grid positions.
  1179. Point2I shift = mGridIndex - index;
  1180. // If we've shifted more than one in either axis then we've warped.
  1181. bool didWarp = shift.x > 1 || shift.x < -1 ||
  1182. shift.y > 1 || shift.y < -1 ? true : false;
  1183. // Go thru the grid shifting each cell we find and
  1184. // placing them in the scratch grid.
  1185. for ( S32 i = 0; i < mCellGrid.size(); i++ )
  1186. {
  1187. GroundCoverCell* cell = mCellGrid[ i ];
  1188. if ( !cell )
  1189. continue;
  1190. // Whats our new index?
  1191. Point2I newIndex = cell->shiftIndex( shift );
  1192. // Is this cell outside of the new grid?
  1193. if ( newIndex.x < 0 || newIndex.x >= mGridSize ||
  1194. newIndex.y < 0 || newIndex.y >= mGridSize )
  1195. {
  1196. _recycleCell( cell );
  1197. continue;
  1198. }
  1199. // Place the cell in the scratch grid.
  1200. mScratchGrid[ ( newIndex.y * mGridSize ) + newIndex.x ] = cell;
  1201. }
  1202. // Get the terrain elevation range for setting the default cell bounds.
  1203. F32 terrainMinHeight = -5000.0f,
  1204. terrainMaxHeight = 5000.0f;
  1205. // Go thru the scratch grid copying each cell back to the
  1206. // cell grid and creating new cells as needed.
  1207. //
  1208. // By limiting ourselves to only one new cell generation per
  1209. // update we're lowering the performance hiccup during movement
  1210. // without getting into the complexity of threading. The delay
  1211. // in generation is rarely noticeable in normal play.
  1212. //
  1213. // The only caveat is that we need to generate the entire visible
  1214. // grid when we warp.
  1215. U32 cellsGenerated = 0;
  1216. for ( S32 i = 0; i < mScratchGrid.size(); i++ )
  1217. {
  1218. GroundCoverCell* cell = mScratchGrid[ i ];
  1219. if ( !cell && ( cellsGenerated == 0 || didWarp ) )
  1220. {
  1221. // Get the index point of this new cell.
  1222. S32 y = i / mGridSize;
  1223. S32 x = i - ( y * mGridSize );
  1224. Point2I newIndex = index + Point2I( x, y );
  1225. // What will be the world placement bounds for this cell.
  1226. Box3F bounds;
  1227. bounds.minExtents.set( newIndex.x * cellSize, newIndex.y * cellSize, terrainMinHeight );
  1228. bounds.maxExtents.set( bounds.minExtents.x + cellSize, bounds.minExtents.y + cellSize, terrainMaxHeight );
  1229. if ( mCuller.isCulled( bounds ) )
  1230. {
  1231. mCellGrid[ i ] = NULL;
  1232. continue;
  1233. }
  1234. // We need to allocate a new cell.
  1235. //
  1236. // TODO: This is the expensive call and where we should optimize. In
  1237. // particular the next best optimization would be to take advantage of
  1238. // multiple cores so that we can generate all the cells in one update.
  1239. //
  1240. // Instead of generating the cell here we would allocate a cell and stick
  1241. // it into a thread safe queue (maybe lockless) as well as the mCellGrid.
  1242. // Once all were allocated we would do something like this...
  1243. //
  1244. // TorqueParallelProcess( cellsToGenerateQueue, _generateCell );
  1245. //
  1246. // Internally this function would pass the queue to some global pre-allocated
  1247. // worker threads which are locked to a particular core. While the main
  1248. // thread waits for the worker threads to finish it will process cells itself.
  1249. //
  1250. cell = _generateCell( newIndex - index,
  1251. bounds,
  1252. placementCount,
  1253. mRandomSeed + mAbs( newIndex.x ) + mAbs( newIndex.y ) );
  1254. // Increment our generation count.
  1255. if ( cell )
  1256. ++cellsGenerated;
  1257. }
  1258. mCellGrid[ i ] = cell;
  1259. }
  1260. // Store the new grid index.
  1261. mGridIndex = index;
  1262. }
  1263. void GroundCover::prepRenderImage( SceneRenderState *state )
  1264. {
  1265. // Reset stats each time we hit the diffuse pass.
  1266. if (mMaterialInst == nullptr)
  1267. return;
  1268. if( state->isDiffusePass() )
  1269. {
  1270. smStatRenderedCells = 0;
  1271. smStatRenderedBillboards = 0;
  1272. smStatRenderedBatches = 0;
  1273. smStatRenderedShapes = 0;
  1274. }
  1275. // TODO: Make sure that the ground cover stops rendering
  1276. // if you're inside a zoned interior.
  1277. if ( state->isReflectPass() && mReflectRadiusScale <= 0.0f )
  1278. return;
  1279. // Nothing to do if this is a shadow pass and shapes in this GoundCover
  1280. // should not be casting shadows.
  1281. if( state->isShadowPass() && !mShapesCastShadows )
  1282. return;
  1283. GFXTransformSaver saver;
  1284. // Setup the frustum culler.
  1285. if ( ( mCuller.getPosition().isZero() || !mDebugLockFrustum ) && !state->isShadowPass() )
  1286. mCuller = state->getCullingFrustum();
  1287. // Update the cells, but only during the diffuse pass.
  1288. // We don't want cell generation to thrash when the reflection camera
  1289. // position doesn't match the diffuse camera!
  1290. if ( state->isDiffusePass() )
  1291. _updateCoverGrid( mCuller );
  1292. // Render billboards but not into shadow passes.
  1293. if ( !state->isShadowPass() && mMaterialInst->isValid() && !mDebugNoBillboards )
  1294. {
  1295. PROFILE_SCOPE( GroundCover_RenderBillboards );
  1296. // Take zoom into account.
  1297. F32 screenScale = state->getWorldToScreenScale().y / state->getViewport().extent.y;
  1298. // Set the far distance for billboards.
  1299. F32 radius = mRadius * smFadeScale;
  1300. mCuller.setFarDist(radius * screenScale );
  1301. F32 cullScale = 1.0f;
  1302. if ( state->isReflectPass() )
  1303. cullScale = mReflectRadiusScale;
  1304. // Setup our shader const data.
  1305. // Must be done prior to submitting our render instance.
  1306. mShaderConstData.fadeInfo.set( mFadeRadius * smFadeScale * cullScale * screenScale, radius * cullScale * screenScale );
  1307. const F32 simTime = Sim::getCurrentTime() * 0.001f;
  1308. mShaderConstData.gustInfo.set( mWindGustLength, mWindGustFrequency * simTime, mWindGustStrength );
  1309. mShaderConstData.turbInfo.set( mWindTurbulenceFrequency * simTime, mWindTurbulenceStrength );
  1310. // Use the camera's forward vector to calculate the camera's right
  1311. // and up vectors. This removes any camera banking from affecting
  1312. // the ground cover.
  1313. const MatrixF &camMat = state->getDiffuseCameraTransform();
  1314. Point3F camDir, camUp, camRight;
  1315. camMat.getColumn( 1, &camDir );
  1316. mCross( camDir, Point3F::UnitZ, &camRight );
  1317. if ( camRight.magnitudeSafe() == 0.0f )
  1318. {
  1319. camRight.set( 0.0f, -1.0f, 0.0f );
  1320. }
  1321. camRight.normalizeSafe();
  1322. mCross( camRight, camDir, &camUp );
  1323. // Limit the camera up vector to keep the billboards
  1324. // from leaning too far down into the terrain.
  1325. VectorF lookDir( camDir.x, camDir.y, 0.0f );
  1326. F32 angle;
  1327. if ( !lookDir.isZero() )
  1328. {
  1329. lookDir.normalize();
  1330. angle = mAcos( mDot( camUp, lookDir ) );
  1331. }
  1332. else
  1333. {
  1334. angle = camDir.z < 0.0f ? 0.0f : ( M_PI_F / 2.0f );
  1335. }
  1336. const F32 maxBillboardTiltRads = mDegToRad( mMaxBillboardTiltAngle );
  1337. if ( angle < (M_PI_F / 2.0f) - maxBillboardTiltRads )
  1338. {
  1339. QuatF quat( AngAxisF( camRight, maxBillboardTiltRads ) );
  1340. quat.mulP( VectorF( 0.0f, 0.0f, 1.0f ), &camUp );
  1341. }
  1342. mShaderConstData.camRight = camRight;
  1343. mShaderConstData.camUp = camUp;
  1344. // Cull and submit render instances for cells.
  1345. for ( S32 i = 0; i < mCellGrid.size(); i++ )
  1346. {
  1347. GroundCoverCell* cell = mCellGrid[ i ];
  1348. if ( !cell )
  1349. continue;
  1350. if ( mCuller.isCulled( cell->getRenderBounds() ) )
  1351. continue;
  1352. cell->renderBillboards( state, mMaterialInst, &mPrimBuffer );
  1353. }
  1354. }
  1355. // Render TS shapes.
  1356. if ( !mDebugNoShapes )
  1357. {
  1358. // Prepare to render the grid shapes.
  1359. PROFILE_SCOPE(GroundCover_RenderShapes);
  1360. // Set up our TS render state.
  1361. TSRenderState rdata;
  1362. rdata.setSceneState( state );
  1363. // We might have some forward lit materials
  1364. // so pass down a query to gather lights.
  1365. LightQuery query;
  1366. rdata.setLightQuery( &query );
  1367. // TODO: Add a special fade out for DTS?
  1368. mCuller.setFarDist( mShapeCullRadius*smFadeScale);
  1369. for ( S32 i = 0; i < mCellGrid.size(); i++ )
  1370. {
  1371. GroundCoverCell* cell = mCellGrid[ i ];
  1372. if ( !cell || mDebugNoShapes )
  1373. continue;
  1374. const Box3F &renderBounds = cell->getRenderBounds();
  1375. U32 clipMask = mCuller.testPlanes( renderBounds, Frustum::PlaneMaskAll );
  1376. if ( clipMask == -1 )
  1377. continue;
  1378. smStatRenderedCells++;
  1379. // Use the cell bounds as the light query volume.
  1380. //
  1381. // This means all forward lit items in this cell will
  1382. // get the same lights, but it performs much better.
  1383. query.init( renderBounds );
  1384. // Render the shapes in this cell... only pass the culler if the
  1385. // cell wasn't fully within the frustum.
  1386. smStatRenderedShapes += cell->renderShapes(
  1387. rdata,
  1388. clipMask != 0 ? &mCuller : NULL,
  1389. mShapeInstances );
  1390. }
  1391. }
  1392. if ( mDebugRenderCells )
  1393. {
  1394. RenderPassManager *pass = state->getRenderPass();
  1395. ObjectRenderInst *ri = pass->allocInst<ObjectRenderInst>();
  1396. ri->type = RenderPassManager::RIT_Editor;
  1397. ri->renderDelegate.bind( this, &GroundCover::_debugRender );
  1398. pass->addInst( ri );
  1399. }
  1400. }
  1401. void GroundCover::_debugRender( ObjectRenderInst *ri, SceneRenderState *state, BaseMatInstance *overrideMat )
  1402. {
  1403. GFXDrawUtil* drawer = GFX->getDrawUtil();
  1404. GFXStateBlockDesc desc;
  1405. desc.setZReadWrite( true, false );
  1406. desc.setBlend( true );
  1407. desc.fillMode = GFXFillWireframe;
  1408. for ( S32 i = 0; i < mCellGrid.size(); i++ )
  1409. {
  1410. GroundCoverCell* cell = mCellGrid[ i ];
  1411. if ( !cell || ( cell->mBillboards.size() + cell->mShapes.size() ) == 0 )
  1412. continue;
  1413. if ( mCuller.isCulled( cell->getRenderBounds() ) )
  1414. continue;
  1415. drawer->drawCube( desc, cell->getRenderBounds().getExtents(), cell->getRenderBounds().getCenter(), ColorI( 0, 255, 0 ) );
  1416. }
  1417. }