groundCover.h 13 KB

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