groundCover.h 12 KB

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