groundCover.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. #ifndef _GROUNDCOVER_H_
  23. #define _GROUNDCOVER_H_
  24. #ifndef _SCENEOBJECT_H_
  25. #include "scene/sceneObject.h"
  26. #endif
  27. #ifndef _MATHUTIL_FRUSTUM_H_
  28. #include "math/util/frustum.h"
  29. #endif
  30. #ifndef _GFXTEXTUREHANDLE_H_
  31. #include "gfx/gfxTextureHandle.h"
  32. #endif
  33. #ifndef _GFX_GFXPRIMITIVEBUFFER_H_
  34. #include "gfx/gfxPrimitiveBuffer.h"
  35. #endif
  36. #ifndef _RENDERPASSMANAGER_H_
  37. #include "renderInstance/renderPassManager.h"
  38. #endif
  39. #ifndef _MATTEXTURETARGET_H_
  40. #include "materials/matTextureTarget.h"
  41. #endif
  42. #ifndef _SHADERFEATURE_H_
  43. #include "shaderGen/shaderFeature.h"
  44. #endif
  45. class TerrainBlock;
  46. class GroundCoverCell;
  47. class TSShapeInstance;
  48. class Material;
  49. class MaterialParameters;
  50. class MaterialParameterHandle;
  51. ///
  52. #define MAX_COVERTYPES 8
  53. GFXDeclareVertexFormat( GCVertex )
  54. {
  55. Point3F point;
  56. Point3F normal;
  57. // .rgb = ambient
  58. // .a = corner index
  59. GFXVertexColor ambient;
  60. // .x = size x
  61. // .y = size y
  62. // .z = type
  63. // .w = wind amplitude
  64. Point4F params;
  65. };
  66. struct GroundCoverShaderConstData
  67. {
  68. Point2F fadeInfo;
  69. Point3F gustInfo;
  70. Point2F turbInfo;
  71. Point3F camRight;
  72. Point3F camUp;
  73. };
  74. class GroundCover;
  75. class GroundCoverShaderConstHandles : public ShaderFeatureConstHandles
  76. {
  77. public:
  78. GroundCoverShaderConstHandles();
  79. virtual void init( GFXShader *shader );
  80. virtual void setConsts( SceneRenderState *state,
  81. const SceneData &sgData,
  82. GFXShaderConstBuffer *buffer );
  83. GroundCover *mGroundCover;
  84. GFXShaderConstHandle *mTypeRectsSC;
  85. GFXShaderConstHandle *mFadeSC;
  86. GFXShaderConstHandle *mWindDirSC;
  87. GFXShaderConstHandle *mGustInfoSC;
  88. GFXShaderConstHandle *mTurbInfoSC;
  89. GFXShaderConstHandle *mCamRightSC;
  90. GFXShaderConstHandle *mCamUpSC;
  91. };
  92. class GroundCover : public SceneObject
  93. {
  94. friend class GroundCoverShaderConstHandles;
  95. friend class GroundCoverCell;
  96. typedef SceneObject Parent;
  97. public:
  98. GroundCover();
  99. ~GroundCover();
  100. DECLARE_CONOBJECT(GroundCover);
  101. static void consoleInit();
  102. static void initPersistFields();
  103. bool onAdd();
  104. void onRemove();
  105. void inspectPostApply();
  106. // Network
  107. U32 packUpdate( NetConnection *, U32 mask, BitStream *stream );
  108. void unpackUpdate( NetConnection *, BitStream *stream );
  109. // Rendering
  110. void prepRenderImage( SceneRenderState *state );
  111. // Editor
  112. void onTerrainUpdated( U32 flags, TerrainBlock *tblock, const Point2I& min, const Point2I& max );
  113. // Misc
  114. const GroundCoverShaderConstData& getShaderConstData() const { return mShaderConstData; }
  115. /// Sets the global ground cover LOD scalar which controls
  116. /// the percentage of the maximum designed cover to put down.
  117. /// It scales both rendering cost and placement CPU performance.
  118. /// Returns the actual value set.
  119. static F32 setQualityScale( F32 scale ) { return smDensityScale = mClampF( scale, 0.0f, 1.0f ); }
  120. /// Returns the current quality scale... see above.
  121. static F32 getQualityScale() { return smDensityScale; }
  122. protected:
  123. enum MaskBits
  124. {
  125. TerrainBlockMask = Parent::NextFreeMask << 0,
  126. NextFreeMask = Parent::NextFreeMask << 1
  127. };
  128. MaterialParameters *mMatParams;
  129. MaterialParameterHandle *mTypeRectsParam;
  130. MaterialParameterHandle *mFadeParams;
  131. MaterialParameterHandle *mWindDirParam;
  132. MaterialParameterHandle *mGustInfoParam;
  133. MaterialParameterHandle *mTurbInfoParam;
  134. MaterialParameterHandle *mCamRightParam;
  135. MaterialParameterHandle *mCamUpParam;
  136. /// This RNG seed is saved and sent to clients
  137. /// for generating the same cover.
  138. S32 mRandomSeed;
  139. /// This is the outer generation radius from
  140. /// the current camera position.
  141. F32 mRadius;
  142. // Offset along the Z axis to render the ground cover.
  143. F32 mZOffset;
  144. /// This is less than or equal to mRadius and
  145. /// defines when fading of cover elements begins.
  146. F32 mFadeRadius;
  147. /// This is the distance at which DTS elements are
  148. /// completely culled out.
  149. F32 mShapeCullRadius;
  150. /// Whether shapes rendered by the GroundCover should cast shadows.
  151. bool mShapesCastShadows;
  152. /// This is used to scale the various culling radii
  153. /// when rendering a reflection... typically for water.
  154. F32 mReflectRadiusScale;
  155. /// This is the number of cells per axis in the grid.
  156. U32 mGridSize;
  157. typedef Vector<GroundCoverCell*> CellVector;
  158. /// This is the allocator for GridCell chunks.
  159. CellVector mAllocCellList;
  160. CellVector mFreeCellList;
  161. /// This is the grid of active cells.
  162. CellVector mCellGrid;
  163. /// This is a scratch grid used while updating
  164. /// the cell grid.
  165. CellVector mScratchGrid;
  166. /// This is the index to the first grid cell.
  167. Point2I mGridIndex;
  168. /// The maximum amount of cover elements to include in
  169. /// the grid at any one time. The actual amount may be
  170. /// less than this based on randomization.
  171. S32 mMaxPlacement;
  172. /// Used to detect changes in cell placement count from
  173. /// the global quality scale so we can regen the cells.
  174. S32 mLastPlacementCount;
  175. /// Used for culling cells to update and render.
  176. Frustum mCuller;
  177. /// Debug parameter for displaying the grid cells.
  178. bool mDebugRenderCells;
  179. /// Debug parameter for turning off billboard rendering.
  180. bool mDebugNoBillboards;
  181. /// Debug parameter for turning off shape rendering.
  182. bool mDebugNoShapes;
  183. /// Debug parameter for locking the culling frustum which
  184. /// will freeze the cover generation.
  185. bool mDebugLockFrustum;
  186. /// Stat for number of rendered cells.
  187. static U32 smStatRenderedCells;
  188. /// Stat for number of rendered billboards.
  189. static U32 smStatRenderedBillboards;
  190. /// Stat for number of rendered billboard batches.
  191. static U32 smStatRenderedBatches;
  192. /// Stat for number of rendered shapes.
  193. static U32 smStatRenderedShapes;
  194. /// The global ground cover LOD scalar which controls
  195. /// the percentage of the maximum amount of cover to put
  196. /// down. It scales both rendering cost and placement
  197. /// CPU performance.
  198. static F32 smDensityScale;
  199. String mMaterialName;
  200. Material *mMaterial;
  201. BaseMatInstance *mMatInst;
  202. GroundCoverShaderConstData mShaderConstData;
  203. /// This is the maximum amout of degrees the billboard will
  204. /// tilt down to match the camera.
  205. F32 mMaxBillboardTiltAngle;
  206. /// The probability of one cover type verses another.
  207. F32 mProbability[MAX_COVERTYPES];
  208. /// The minimum random size for each cover type.
  209. F32 mSizeMin[MAX_COVERTYPES];
  210. /// The maximum random size of this cover type.
  211. F32 mSizeMax[MAX_COVERTYPES];
  212. /// An exponent used to bias between the minimum
  213. /// and maximum random sizes.
  214. F32 mSizeExponent[MAX_COVERTYPES];
  215. /// The wind effect scale.
  216. F32 mWindScale[MAX_COVERTYPES];
  217. /// The maximum slope angle in degrees for placement.
  218. F32 mMaxSlope[MAX_COVERTYPES];
  219. /// The minimum world space elevation for placement.
  220. F32 mMinElevation[MAX_COVERTYPES];
  221. /// The maximum world space elevation for placement.
  222. F32 mMaxElevation[MAX_COVERTYPES];
  223. /// Terrain material name to limit coverage to, or
  224. /// left empty to cover entire terrain.
  225. StringTableEntry mLayer[MAX_COVERTYPES];
  226. /// Inverts the data layer test making the
  227. /// layer an exclusion mask.
  228. bool mInvertLayer[MAX_COVERTYPES];
  229. /// The minimum amount of elements in a clump.
  230. S32 mMinClumpCount[MAX_COVERTYPES];
  231. /// The maximum amount of elements in a clump.
  232. S32 mMaxClumpCount[MAX_COVERTYPES];
  233. /// An exponent used to bias between the minimum
  234. /// and maximum clump counts for a particular clump.
  235. F32 mClumpCountExponent[MAX_COVERTYPES];
  236. /// The maximum clump radius.
  237. F32 mClumpRadius[MAX_COVERTYPES];
  238. /// This is a cached array of billboard aspect scales
  239. /// used to avoid some calculations when generating cells.
  240. F32 mBillboardAspectScales[MAX_COVERTYPES];
  241. RectF mBillboardRects[MAX_COVERTYPES];
  242. /// The cover shape filenames.
  243. StringTableEntry mShapeFilenames[MAX_COVERTYPES];
  244. /// The cover shape instances.
  245. TSShapeInstance* mShapeInstances[MAX_COVERTYPES];
  246. /// This is the same as mProbability, but normalized for use
  247. /// during the cover placement process.
  248. F32 mNormalizedProbability[MAX_COVERTYPES];
  249. /// A shared primitive buffer setup for drawing the maximum amount
  250. /// of billboards you could possibly have in a single cell.
  251. GFXPrimitiveBufferHandle mPrimBuffer;
  252. /// The length in meters between peaks in the wind gust.
  253. F32 mWindGustLength;
  254. /// Controls how often the wind gust peaks per second.
  255. F32 mWindGustFrequency;
  256. /// The maximum distance in meters that the peak wind
  257. /// gust will displace an element.
  258. F32 mWindGustStrength;
  259. /// The direction of the wind.
  260. Point2F mWindDirection;
  261. /// Controls the overall rapidity of the wind turbulence.
  262. F32 mWindTurbulenceFrequency;
  263. /// The maximum distance in meters that the turbulence can
  264. /// displace a ground cover element.
  265. F32 mWindTurbulenceStrength;
  266. void _initMaterial();
  267. bool _initShader();
  268. void _initShapes();
  269. void _deleteShapes();
  270. /// Called when GroundCover parameters are changed and
  271. /// things need to be reinitialized to continue.
  272. void _initialize( U32 cellCount, U32 cellPlacementCount );
  273. /// Updates the cover grid by removing cells that
  274. /// have fallen outside of mRadius and adding new
  275. /// ones that have come into view.
  276. void _updateCoverGrid( const Frustum &culler );
  277. /// Clears the cell grid, moves all the allocated cells to
  278. /// the free list, and deletes excess free cells.
  279. void _freeCells();
  280. /// Clears the cell grid and deletes all the free cells.
  281. void _deleteCells();
  282. /// Returns a cell to the free list.
  283. void _recycleCell( GroundCoverCell* cell );
  284. /// Generates a new cell using the recycle list when possible.
  285. GroundCoverCell* _generateCell( const Point2I& index,
  286. const Box3F& bounds,
  287. U32 placementCount,
  288. S32 randSeed );
  289. void _debugRender( ObjectRenderInst *ri, SceneRenderState *state, BaseMatInstance *overrideMat );
  290. };
  291. #endif // _GROUNDCOVER_H_