groundCover.cpp 59 KB

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