groundCover.h 12 KB

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