fxFoliageReplicator.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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 _FOLIAGEREPLICATOR_H_
  23. #define _FOLIAGEREPLICATOR_H_
  24. #ifndef _SCENEOBJECT_H_
  25. #include "scene/sceneObject.h"
  26. #endif
  27. #ifndef _GFXTEXTUREHANDLE_H_
  28. #include "gfx/gfxTextureHandle.h"
  29. #endif
  30. #ifndef _GFXPRIMITIVEBUFFER_H_
  31. #include "gfx/gfxPrimitiveBuffer.h"
  32. #endif
  33. #ifndef _GBITMAP_H_
  34. #include "gfx/bitmap/gBitmap.h"
  35. #endif
  36. #ifndef _RENDERPASSMANAGER_H_
  37. #include "renderInstance/renderPassManager.h"
  38. #endif
  39. #ifndef _MATHUTIL_FRUSTUM_H_
  40. #include "math/util/frustum.h"
  41. #endif
  42. #pragma warning( push, 4 )
  43. #define AREA_ANIMATION_ARC (1.0f / 360.0f)
  44. #define FXFOLIAGEREPLICATOR_COLLISION_MASK ( TerrainObjectType | \
  45. StaticShapeObjectType | \
  46. WaterObjectType )
  47. #define FXFOLIAGEREPLICATOR_NOWATER_COLLISION_MASK ( TerrainObjectType | \
  48. StaticShapeObjectType )
  49. #define FXFOLIAGE_ALPHA_EPSILON 1e-4
  50. //------------------------------------------------------------------------------
  51. // Class: fxFoliageItem
  52. //------------------------------------------------------------------------------
  53. class fxFoliageItem
  54. {
  55. public:
  56. MatrixF Transform;
  57. F32 Width;
  58. F32 Height;
  59. Box3F FoliageBox;
  60. bool Flipped;
  61. F32 SwayPhase;
  62. F32 SwayTimeRatio;
  63. F32 LightPhase;
  64. F32 LightTimeRatio;
  65. U32 LastFrameSerialID;
  66. };
  67. //------------------------------------------------------------------------------
  68. // Class: fxFoliageCulledList
  69. //------------------------------------------------------------------------------
  70. class fxFoliageCulledList
  71. {
  72. public:
  73. fxFoliageCulledList() {};
  74. fxFoliageCulledList(Box3F SearchBox, fxFoliageCulledList* InVec);
  75. ~fxFoliageCulledList() {};
  76. void FindCandidates(const Box3F& SearchBox, fxFoliageCulledList* InVec);
  77. U32 GetListCount(void) { return mCulledObjectSet.size(); };
  78. fxFoliageItem* GetElement(U32 index) { return mCulledObjectSet[index]; };
  79. Vector<fxFoliageItem*> mCulledObjectSet; // Culled Object Set.
  80. };
  81. //------------------------------------------------------------------------------
  82. // Class: fxFoliageQuadNode
  83. //------------------------------------------------------------------------------
  84. class fxFoliageQuadrantNode {
  85. public:
  86. U32 Level;
  87. Box3F QuadrantBox;
  88. fxFoliageQuadrantNode* QuadrantChildNode[4];
  89. Vector<fxFoliageItem*> RenderList;
  90. // Used in DrawIndexPrimitive call.
  91. U32 startIndex;
  92. U32 primitiveCount;
  93. };
  94. //------------------------------------------------------------------------------
  95. // Class: fxFoliageRenderList
  96. //------------------------------------------------------------------------------
  97. class fxFoliageRenderList
  98. {
  99. public:
  100. Box3F mBox; // Clipping Box.
  101. Frustum mFrustum; // View frustum.
  102. Vector<fxFoliageItem*> mVisObjectSet; // Visible Object Set.
  103. F32 mHeightLerp; // Height Lerp.
  104. public:
  105. bool IsQuadrantVisible(const Box3F VisBox, const MatrixF& RenderTransform);
  106. void SetupClipPlanes(SceneRenderState* state, const F32 FarClipPlane);
  107. void DrawQuadBox(const Box3F& QuadBox, const ColorF Colour);
  108. };
  109. // Define a vertex
  110. GFXDeclareVertexFormat( GFXVertexFoliage )
  111. {
  112. Point3F point;
  113. Point3F normal;
  114. Point2F texCoord;
  115. Point2F texCoord2;
  116. };
  117. //------------------------------------------------------------------------------
  118. // Class: fxFoliageReplicator
  119. //------------------------------------------------------------------------------
  120. class fxFoliageReplicator : public SceneObject
  121. {
  122. private:
  123. typedef SceneObject Parent;
  124. protected:
  125. void CreateFoliage(void);
  126. void DestroyFoliage(void);
  127. void DestroyFoliageItems();
  128. void SyncFoliageReplicators(void);
  129. Box3F FetchQuadrant(const Box3F& Box, U32 Quadrant);
  130. void ProcessQuadrant(fxFoliageQuadrantNode* pParentNode, fxFoliageCulledList* pCullList, U32 Quadrant);
  131. void ProcessNodeChildren(fxFoliageQuadrantNode* pParentNode, fxFoliageCulledList* pCullList);
  132. enum { FoliageReplicationMask = (1 << 0) };
  133. U32 mCreationAreaAngle;
  134. bool mClientReplicationStarted;
  135. U32 mCurrentFoliageCount;
  136. Vector<fxFoliageQuadrantNode*> mFoliageQuadTree;
  137. Vector<fxFoliageItem*> mReplicatedFoliage;
  138. fxFoliageRenderList mFrustumRenderSet;
  139. GFXVertexBufferHandle<GFXVertexFoliage> mVertexBuffer;
  140. GFXPrimitiveBufferHandle mPrimBuffer;
  141. GFXShaderRef mShader;
  142. ShaderData* mShaderData;
  143. GBitmap* mAlphaLookup;
  144. MRandomLCG RandomGen;
  145. F32 mFadeInGradient;
  146. F32 mFadeOutGradient;
  147. S32 mLastRenderTime;
  148. F32 mGlobalSwayPhase;
  149. F32 mGlobalSwayTimeRatio;
  150. F32 mGlobalLightPhase;
  151. F32 mGlobalLightTimeRatio;
  152. U32 mFrameSerialID;
  153. U32 mQuadTreeLevels; // Quad-Tree Levels.
  154. U32 mPotentialFoliageNodes; // Potential Foliage Nodes.
  155. U32 mNextAllocatedNodeIdx; // Next Allocated Node Index.
  156. U32 mBillboardsAcquired; // Billboards Acquired.
  157. // Used for alpha lookup in the pixel shader
  158. GFXTexHandle mAlphaTexture;
  159. GFXStateBlockRef mPlacementSB;
  160. GFXStateBlockRef mRenderSB;
  161. GFXStateBlockRef mDebugSB;
  162. GFXShaderConstBufferRef mFoliageShaderConsts;
  163. GFXShaderConstHandle* mFoliageShaderProjectionSC;
  164. GFXShaderConstHandle* mFoliageShaderWorldSC;
  165. GFXShaderConstHandle* mFoliageShaderGlobalSwayPhaseSC;
  166. GFXShaderConstHandle* mFoliageShaderSwayMagnitudeSideSC;
  167. GFXShaderConstHandle* mFoliageShaderSwayMagnitudeFrontSC;
  168. GFXShaderConstHandle* mFoliageShaderGlobalLightPhaseSC;
  169. GFXShaderConstHandle* mFoliageShaderLuminanceMagnitudeSC;
  170. GFXShaderConstHandle* mFoliageShaderLuminanceMidpointSC;
  171. GFXShaderConstHandle* mFoliageShaderDistanceRangeSC;
  172. GFXShaderConstHandle* mFoliageShaderCameraPosSC;
  173. GFXShaderConstHandle* mFoliageShaderTrueBillboardSC;
  174. //pixel shader
  175. GFXShaderConstHandle* mFoliageShaderGroundAlphaSC;
  176. GFXShaderConstHandle* mFoliageShaderAmbientColorSC;
  177. GFXShaderConstHandle* mDiffuseTextureSC;
  178. GFXShaderConstHandle* mAlphaMapTextureSC;
  179. bool mDirty;
  180. void SetupShader();
  181. void SetupBuffers();
  182. void renderObject(ObjectRenderInst *ri, SceneRenderState *state, BaseMatInstance*);
  183. void renderBuffers(SceneRenderState* state);
  184. void renderArc(const F32 fRadiusX, const F32 fRadiusY);
  185. void renderPlacementArea(const F32 ElapsedTime);
  186. void renderQuad(fxFoliageQuadrantNode* quadNode, const MatrixF& RenderTransform, const bool UseDebug);
  187. void computeAlphaTex();
  188. public:
  189. fxFoliageReplicator();
  190. ~fxFoliageReplicator();
  191. void StartUp(void);
  192. void ShowReplication(void);
  193. void HideReplication(void);
  194. // SceneObject
  195. virtual void prepRenderImage( SceneRenderState *state );
  196. // SimObject
  197. bool onAdd();
  198. void onRemove();
  199. void inspectPostApply();
  200. // NetObject
  201. U32 packUpdate(NetConnection *conn, U32 mask, BitStream *stream);
  202. void unpackUpdate(NetConnection *conn, BitStream *stream);
  203. // Editor
  204. void onGhostAlwaysDone();
  205. // ConObject.
  206. static void initPersistFields();
  207. // Field Data.
  208. class tagFieldData
  209. {
  210. public:
  211. bool mUseDebugInfo;
  212. F32 mDebugBoxHeight;
  213. U32 mSeed;
  214. StringTableEntry mFoliageFile;
  215. GFXTexHandle mFoliageTexture;
  216. U32 mFoliageCount;
  217. U32 mFoliageRetries;
  218. U32 mInnerRadiusX;
  219. U32 mInnerRadiusY;
  220. U32 mOuterRadiusX;
  221. U32 mOuterRadiusY;
  222. F32 mMinWidth;
  223. F32 mMaxWidth;
  224. F32 mMinHeight;
  225. F32 mMaxHeight;
  226. bool mFixAspectRatio;
  227. bool mFixSizeToMax;
  228. F32 mOffsetZ;
  229. bool mRandomFlip;
  230. bool mUseTrueBillboards;
  231. bool mUseCulling;
  232. U32 mCullResolution;
  233. F32 mViewDistance;
  234. F32 mViewClosest;
  235. F32 mFadeInRegion;
  236. F32 mFadeOutRegion;
  237. F32 mAlphaCutoff;
  238. F32 mGroundAlpha;
  239. bool mSwayOn;
  240. bool mSwaySync;
  241. F32 mSwayMagnitudeSide;
  242. F32 mSwayMagnitudeFront;
  243. F32 mMinSwayTime;
  244. F32 mMaxSwayTime;
  245. bool mLightOn;
  246. bool mLightSync;
  247. F32 mMinLuminance;
  248. F32 mMaxLuminance;
  249. F32 mLightTime;
  250. bool mAllowOnTerrain;
  251. bool mAllowStatics;
  252. bool mAllowOnWater;
  253. bool mAllowWaterSurface;
  254. S32 mAllowedTerrainSlope;
  255. bool mHideFoliage;
  256. bool mShowPlacementArea;
  257. U32 mPlacementBandHeight;
  258. ColorF mPlaceAreaColour;
  259. tagFieldData()
  260. {
  261. // Set Defaults.
  262. mUseDebugInfo = false;
  263. mDebugBoxHeight = 1.0f;
  264. mSeed = 1376312589;
  265. mFoliageFile = StringTable->EmptyString();
  266. mFoliageTexture = GFXTexHandle();
  267. mFoliageCount = 10;
  268. mFoliageRetries = 100;
  269. mInnerRadiusX = 0;
  270. mInnerRadiusY = 0;
  271. mOuterRadiusX = 128;
  272. mOuterRadiusY = 128;
  273. mMinWidth = 1;
  274. mMaxWidth = 3;
  275. mMinHeight = 1;
  276. mMaxHeight = 5;
  277. mFixAspectRatio = true;
  278. mFixSizeToMax = false;
  279. mOffsetZ = 0;
  280. mRandomFlip = true;
  281. mUseTrueBillboards = false;
  282. mUseCulling = true;
  283. mCullResolution = 64;
  284. mViewDistance = 50.0f;
  285. mViewClosest = 1.0f;
  286. mFadeInRegion = 10.0f;
  287. mFadeOutRegion = 1.0f;
  288. mAlphaCutoff = 0.2f;
  289. mGroundAlpha = 1.0f;
  290. mSwayOn = false;
  291. mSwaySync = false;
  292. mSwayMagnitudeSide = 0.1f;
  293. mSwayMagnitudeFront = 0.2f;
  294. mMinSwayTime = 3.0f;
  295. mMaxSwayTime = 10.0f;
  296. mLightOn = false;
  297. mLightSync = false;
  298. mMinLuminance = 0.7f;
  299. mMaxLuminance = 1.0f;
  300. mLightTime = 5.0f;
  301. mAllowOnTerrain = true;
  302. mAllowStatics = true;
  303. mAllowOnWater = false;
  304. mAllowWaterSurface = false;
  305. mAllowedTerrainSlope = 90;
  306. mHideFoliage = false;
  307. mShowPlacementArea = true;
  308. mPlacementBandHeight = 25;
  309. mPlaceAreaColour .set(0.4f, 0, 0.8f);
  310. }
  311. } mFieldData;
  312. // Declare Console Object.
  313. DECLARE_CONOBJECT(fxFoliageReplicator);
  314. };
  315. #pragma warning( pop )
  316. #endif // _FOLIAGEREPLICATOR_H_