fxFoliageReplicator.h 13 KB

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