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