groundCover.h

Engine/source/T3D/fx/groundCover.h

More...

Classes:

Public Defines

define

Public Functions

Detailed Description

Public Defines

MAX_COVERTYPES() 8

Public Functions

GFXDeclareVertexFormat(GCVertex )

  1
  2//-----------------------------------------------------------------------------
  3// Copyright (c) 2012 GarageGames, LLC
  4//
  5// Permission is hereby granted, free of charge, to any person obtaining a copy
  6// of this software and associated documentation files (the "Software"), to
  7// deal in the Software without restriction, including without limitation the
  8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9// sell copies of the Software, and to permit persons to whom the Software is
 10// furnished to do so, subject to the following conditions:
 11//
 12// The above copyright notice and this permission notice shall be included in
 13// all copies or substantial portions of the Software.
 14//
 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 21// IN THE SOFTWARE.
 22//-----------------------------------------------------------------------------
 23
 24#ifndef _GROUNDCOVER_H_
 25#define _GROUNDCOVER_H_
 26
 27#ifndef _SCENEOBJECT_H_
 28#include "scene/sceneObject.h"
 29#endif
 30#ifndef _MATHUTIL_FRUSTUM_H_
 31#include "math/util/frustum.h"
 32#endif
 33#ifndef _GFXTEXTUREHANDLE_H_
 34#include "gfx/gfxTextureHandle.h"
 35#endif
 36#ifndef _GFX_GFXPRIMITIVEBUFFER_H_
 37#include "gfx/gfxPrimitiveBuffer.h"
 38#endif
 39#ifndef _RENDERPASSMANAGER_H_
 40#include "renderInstance/renderPassManager.h"
 41#endif
 42#ifndef _MATTEXTURETARGET_H_
 43#include "materials/matTextureTarget.h"
 44#endif
 45#ifndef _SHADERFEATURE_H_
 46#include "shaderGen/shaderFeature.h"
 47#endif
 48
 49class TerrainBlock;
 50class GroundCoverCell;
 51class TSShapeInstance;
 52class Material;
 53class MaterialParameters;
 54class MaterialParameterHandle;
 55
 56
 57///
 58#define MAX_COVERTYPES 8
 59
 60
 61GFXDeclareVertexFormat( GCVertex )
 62{
 63   Point3F point;
 64
 65   Point3F normal;
 66
 67   // .rgb = ambient
 68   // .a = corner index
 69   GFXVertexColor ambient;
 70
 71   // .x = size x
 72   // .y = size y
 73   // .z = type
 74   // .w = wind amplitude
 75   Point4F params;
 76};
 77
 78struct GroundCoverShaderConstData
 79{
 80   Point2F fadeInfo;
 81   Point3F gustInfo;
 82   Point2F turbInfo;
 83   Point3F camRight;
 84   Point3F camUp;
 85};
 86
 87class GroundCover;
 88
 89class GroundCoverShaderConstHandles : public ShaderFeatureConstHandles
 90{
 91public:
 92
 93   GroundCoverShaderConstHandles();
 94
 95   virtual void init( GFXShader *shader );
 96
 97   virtual void setConsts( SceneRenderState *state, 
 98                           const SceneData &sgData,
 99                           GFXShaderConstBuffer *buffer );
100
101   GroundCover *mGroundCover;
102
103   GFXShaderConstHandle *mTypeRectsSC;
104   GFXShaderConstHandle *mFadeSC;
105   GFXShaderConstHandle *mWindDirSC;
106   GFXShaderConstHandle *mGustInfoSC;
107   GFXShaderConstHandle *mTurbInfoSC;
108   GFXShaderConstHandle *mCamRightSC;
109   GFXShaderConstHandle *mCamUpSC;
110};
111
112
113class GroundCover : public SceneObject
114{
115   friend class GroundCoverShaderConstHandles;
116   friend class GroundCoverCell;
117   typedef SceneObject Parent;
118
119public:
120
121   GroundCover();
122   ~GroundCover();
123
124   DECLARE_CONOBJECT(GroundCover);
125
126   static void consoleInit();
127   static void initPersistFields();
128
129   bool onAdd();
130   void onRemove();
131   void inspectPostApply();
132
133   // Network
134   U32 packUpdate( NetConnection *, U32 mask, BitStream *stream );
135   void unpackUpdate( NetConnection *, BitStream *stream );
136
137   // Rendering
138   void prepRenderImage( SceneRenderState *state );
139   
140   // Editor
141   void onTerrainUpdated( U32 flags, TerrainBlock *tblock, const Point2I& min, const Point2I& max );
142
143   // Misc
144   const GroundCoverShaderConstData& getShaderConstData() const { return mShaderConstData; }
145
146   /// Sets the global ground cover LOD scalar which controls
147   /// the percentage of the maximum designed cover to put down.
148   /// It scales both rendering cost and placement CPU performance.
149   /// Returns the actual value set.
150   static F32 setQualityScale( F32 scale ) { return smDensityScale = mClampF( scale, 0.0f, 1.0f ); }
151
152   /// Returns the current quality scale... see above.
153   static F32 getQualityScale() { return smDensityScale; }
154
155protected:      
156
157   enum MaskBits 
158   {      
159      TerrainBlockMask  = Parent::NextFreeMask << 0,
160      NextFreeMask      = Parent::NextFreeMask << 1
161   };
162
163   MaterialParameters *mMatParams;
164   MaterialParameterHandle *mTypeRectsParam;   
165   MaterialParameterHandle *mFadeParams;
166   MaterialParameterHandle *mWindDirParam;
167   MaterialParameterHandle *mGustInfoParam;
168   MaterialParameterHandle *mTurbInfoParam;
169   MaterialParameterHandle *mCamRightParam;
170   MaterialParameterHandle *mCamUpParam;
171
172   /// This RNG seed is saved and sent to clients
173   /// for generating the same cover.
174   S32 mRandomSeed;
175
176   /// This is the outer generation radius from
177   /// the current camera position.
178   F32 mRadius;
179
180   // Offset along the Z axis to render the ground cover.
181   F32 mZOffset;
182
183   /// This is less than or equal to mRadius and
184   /// defines when fading of cover elements begins.
185   F32 mFadeRadius;
186
187   /// This is the distance at which DTS elements are 
188   /// completely culled out.
189   F32 mShapeCullRadius;
190
191   /// Whether shapes rendered by the GroundCover should cast shadows.
192   bool mShapesCastShadows;
193
194   /// This is used to scale the various culling radii 
195   /// when rendering a reflection... typically for water.
196   F32 mReflectRadiusScale;
197
198   /// This is the number of cells per axis in the grid.
199   U32 mGridSize;
200
201   typedef Vector<GroundCoverCell*> CellVector;
202
203   /// This is the allocator for GridCell chunks.
204   CellVector mAllocCellList;
205   CellVector mFreeCellList;
206
207   /// This is the grid of active cells.
208   CellVector mCellGrid;
209
210   /// This is a scratch grid used while updating
211   /// the cell grid.
212   CellVector mScratchGrid;
213
214   /// This is the index to the first grid cell.
215   Point2I mGridIndex;
216
217   /// The maximum amount of cover elements to include in
218   /// the grid at any one time.  The actual amount may be
219   /// less than this based on randomization.
220   S32 mMaxPlacement;
221
222   /// Used to detect changes in cell placement count from 
223   /// the global quality scale so we can regen the cells.
224   S32 mLastPlacementCount;
225
226   /// Used for culling cells to update and render.
227   Frustum mCuller;
228
229   /// Debug parameter for displaying the grid cells.
230   bool mDebugRenderCells;
231
232   /// Debug parameter for turning off billboard rendering.
233   bool mDebugNoBillboards;
234
235   /// Debug parameter for turning off shape rendering.
236   bool mDebugNoShapes;
237
238   /// Debug parameter for locking the culling frustum which
239   /// will freeze the cover generation.
240   bool mDebugLockFrustum;
241
242   /// Stat for number of rendered cells.
243   static U32 smStatRenderedCells;
244
245   /// Stat for number of rendered billboards.
246   static U32 smStatRenderedBillboards;
247
248   /// Stat for number of rendered billboard batches.
249   static U32 smStatRenderedBatches;
250
251   /// Stat for number of rendered shapes.
252   static U32 smStatRenderedShapes;
253
254   /// The global ground cover LOD scalar which controls
255   /// the percentage of the maximum amount of cover to put
256   /// down.  It scales both rendering cost and placement
257   /// CPU performance.
258   static F32 smDensityScale;   
259
260   String mMaterialName;
261   Material *mMaterial;
262   BaseMatInstance *mMatInst;
263
264   GroundCoverShaderConstData mShaderConstData;
265
266   /// This is the maximum amout of degrees the billboard will
267   /// tilt down to match the camera.
268   F32 mMaxBillboardTiltAngle;
269
270   /// The probability of one cover type verses another.
271   F32 mProbability[MAX_COVERTYPES];
272
273   /// The minimum random size for each cover type.
274   F32 mSizeMin[MAX_COVERTYPES];
275
276   /// The maximum random size of this cover type.
277   F32 mSizeMax[MAX_COVERTYPES];
278
279   /// An exponent used to bias between the minimum
280   /// and maximum random sizes.
281   F32 mSizeExponent[MAX_COVERTYPES];
282
283   /// The wind effect scale.
284   F32 mWindScale[MAX_COVERTYPES];
285
286   /// The maximum slope angle in degrees for placement.
287   F32 mMaxSlope[MAX_COVERTYPES];
288
289   /// The minimum world space elevation for placement.
290   F32 mMinElevation[MAX_COVERTYPES];
291
292   /// The maximum world space elevation for placement.
293   F32 mMaxElevation[MAX_COVERTYPES];
294
295   /// Terrain material name to limit coverage to, or
296   /// left empty to cover entire terrain.
297   StringTableEntry mLayer[MAX_COVERTYPES];
298
299   /// Inverts the data layer test making the 
300   /// layer an exclusion mask.
301   bool mInvertLayer[MAX_COVERTYPES];
302
303   /// The minimum amount of elements in a clump.
304   S32 mMinClumpCount[MAX_COVERTYPES];
305
306   /// The maximum amount of elements in a clump.
307   S32 mMaxClumpCount[MAX_COVERTYPES];
308
309   /// An exponent used to bias between the minimum
310   /// and maximum clump counts for a particular clump.
311   F32 mClumpCountExponent[MAX_COVERTYPES];
312
313   /// The maximum clump radius.
314   F32 mClumpRadius[MAX_COVERTYPES];
315
316   /// This is a cached array of billboard aspect scales
317   /// used to avoid some calculations when generating cells.
318   F32 mBillboardAspectScales[MAX_COVERTYPES];
319
320   RectF mBillboardRects[MAX_COVERTYPES];
321
322   /// The cover shape filenames.
323   StringTableEntry mShapeFilenames[MAX_COVERTYPES];
324
325   /// The cover shape instances.
326   TSShapeInstance* mShapeInstances[MAX_COVERTYPES];
327
328   /// This is the same as mProbability, but normalized for use
329   /// during the cover placement process. 
330   F32 mNormalizedProbability[MAX_COVERTYPES];
331
332   /// A shared primitive buffer setup for drawing the maximum amount
333   /// of billboards you could possibly have in a single cell.
334   GFXPrimitiveBufferHandle mPrimBuffer;
335
336   /// The length in meters between peaks in the wind gust.
337   F32 mWindGustLength;
338
339   /// Controls how often the wind gust peaks per second.
340   F32 mWindGustFrequency;
341
342   /// The maximum distance in meters that the peak wind 
343   /// gust will displace an element.
344   F32 mWindGustStrength;
345
346   /// The direction of the wind.
347   Point2F mWindDirection;
348
349   /// Controls the overall rapidity of the wind turbulence.
350   F32 mWindTurbulenceFrequency;
351
352   /// The maximum distance in meters that the turbulence can
353   /// displace a ground cover element.
354   F32 mWindTurbulenceStrength;
355
356   void _initMaterial();
357
358   bool _initShader();
359
360   void _initShapes();
361
362   void _deleteShapes();
363
364   /// Called when GroundCover parameters are changed and
365   /// things need to be reinitialized to continue.
366   void _initialize( U32 cellCount, U32 cellPlacementCount );
367
368   /// Updates the cover grid by removing cells that
369   /// have fallen outside of mRadius and adding new 
370   /// ones that have come into view.
371   void _updateCoverGrid( const Frustum &culler );
372
373   /// Clears the cell grid, moves all the allocated cells to
374   /// the free list, and deletes excess free cells.
375   void _freeCells();
376
377   /// Clears the cell grid and deletes all the free cells.
378   void _deleteCells();
379
380   /// Returns a cell to the free list.
381   void _recycleCell( GroundCoverCell* cell );
382
383   /// Generates a new cell using the recycle list when possible.
384   GroundCoverCell* _generateCell(  const Point2I& index,
385                                    const Box3F& bounds, 
386                                    U32 placementCount,
387                                    S32 randSeed );
388
389   void _debugRender( ObjectRenderInst *ri, SceneRenderState *state, BaseMatInstance *overrideMat );
390};
391
392#endif // _GROUNDCOVER_H_
393