fxFoliageReplicator.h 13 KB

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