groundCover.cpp 57 KB

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