123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395 |
- //-----------------------------------------------------------------------------
- // Copyright (c) 2012 GarageGames, LLC
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to
- // deal in the Software without restriction, including without limitation the
- // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
- // sell copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
- // IN THE SOFTWARE.
- //-----------------------------------------------------------------------------
- //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
- // Arcane-FX for MIT Licensed Open Source version of Torque 3D from GarageGames
- // Copyright (C) 2015 Faust Logic, Inc.
- //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
- #ifndef _FOLIAGEREPLICATOR_H_
- #define _FOLIAGEREPLICATOR_H_
- #ifndef _SCENEOBJECT_H_
- #include "scene/sceneObject.h"
- #endif
- #ifndef _GFXTEXTUREHANDLE_H_
- #include "gfx/gfxTextureHandle.h"
- #endif
- #ifndef _GFXPRIMITIVEBUFFER_H_
- #include "gfx/gfxPrimitiveBuffer.h"
- #endif
- #ifndef _GBITMAP_H_
- #include "gfx/bitmap/gBitmap.h"
- #endif
- #ifndef _RENDERPASSMANAGER_H_
- #include "renderInstance/renderPassManager.h"
- #endif
- #ifndef _MATHUTIL_FRUSTUM_H_
- #include "math/util/frustum.h"
- #endif
- #pragma warning( push, 4 )
- #define AREA_ANIMATION_ARC (1.0f / 360.0f)
- #define FXFOLIAGEREPLICATOR_COLLISION_MASK ( TerrainObjectType | \
- StaticShapeObjectType | \
- WaterObjectType )
- #define FXFOLIAGEREPLICATOR_NOWATER_COLLISION_MASK ( TerrainObjectType | \
- StaticShapeObjectType )
- #define FXFOLIAGE_ALPHA_EPSILON 1e-4
- //------------------------------------------------------------------------------
- // Class: fxFoliageItem
- //------------------------------------------------------------------------------
- class fxFoliageItem
- {
- public:
- MatrixF Transform;
- F32 Width;
- F32 Height;
- Box3F FoliageBox;
- bool Flipped;
- F32 SwayPhase;
- F32 SwayTimeRatio;
- F32 LightPhase;
- F32 LightTimeRatio;
- U32 LastFrameSerialID;
- };
- //------------------------------------------------------------------------------
- // Class: fxFoliageCulledList
- //------------------------------------------------------------------------------
- class fxFoliageCulledList
- {
- public:
- fxFoliageCulledList() {};
- fxFoliageCulledList(Box3F SearchBox, fxFoliageCulledList* InVec);
- ~fxFoliageCulledList() {};
- void FindCandidates(const Box3F& SearchBox, fxFoliageCulledList* InVec);
- U32 GetListCount(void) { return mCulledObjectSet.size(); };
- fxFoliageItem* GetElement(U32 index) { return mCulledObjectSet[index]; };
- Vector<fxFoliageItem*> mCulledObjectSet; // Culled Object Set.
- };
- //------------------------------------------------------------------------------
- // Class: fxFoliageQuadNode
- //------------------------------------------------------------------------------
- class fxFoliageQuadrantNode {
- public:
- U32 Level;
- Box3F QuadrantBox;
- fxFoliageQuadrantNode* QuadrantChildNode[4];
- Vector<fxFoliageItem*> RenderList;
- // Used in DrawIndexPrimitive call.
- U32 startIndex;
- U32 primitiveCount;
- };
- //------------------------------------------------------------------------------
- // Class: fxFoliageRenderList
- //------------------------------------------------------------------------------
- class fxFoliageRenderList
- {
- public:
- Box3F mBox; // Clipping Box.
- Frustum mFrustum; // View frustum.
- Vector<fxFoliageItem*> mVisObjectSet; // Visible Object Set.
- F32 mHeightLerp; // Height Lerp.
- public:
- bool IsQuadrantVisible(const Box3F VisBox, const MatrixF& RenderTransform);
- void SetupClipPlanes(SceneRenderState* state, const F32 FarClipPlane);
- void DrawQuadBox(const Box3F& QuadBox, const LinearColorF Colour);
- };
- // Define a vertex
- GFXDeclareVertexFormat( GFXVertexFoliage )
- {
- Point3F point;
- Point3F normal;
- Point2F texCoord;
- Point2F texCoord2;
- };
- //------------------------------------------------------------------------------
- // Class: fxFoliageReplicator
- //------------------------------------------------------------------------------
- class fxFoliageReplicator : public SceneObject
- {
- private:
- typedef SceneObject Parent;
- protected:
- void CreateFoliage(void);
- void DestroyFoliage(void);
- void DestroyFoliageItems();
- void SyncFoliageReplicators(void);
- Box3F FetchQuadrant(const Box3F& Box, U32 Quadrant);
- void ProcessQuadrant(fxFoliageQuadrantNode* pParentNode, fxFoliageCulledList* pCullList, U32 Quadrant);
- void ProcessNodeChildren(fxFoliageQuadrantNode* pParentNode, fxFoliageCulledList* pCullList);
- enum { FoliageReplicationMask = (1 << 0) };
- U32 mCreationAreaAngle;
- bool mClientReplicationStarted;
- U32 mCurrentFoliageCount;
- Vector<fxFoliageQuadrantNode*> mFoliageQuadTree;
- Vector<fxFoliageItem*> mReplicatedFoliage;
- fxFoliageRenderList mFrustumRenderSet;
- GFXVertexBufferHandle<GFXVertexFoliage> mVertexBuffer;
- GFXPrimitiveBufferHandle mPrimBuffer;
- GFXShaderRef mShader;
- ShaderData* mShaderData;
- GBitmap* mAlphaLookup;
- MRandomLCG RandomGen;
- F32 mFadeInGradient;
- F32 mFadeOutGradient;
- S32 mLastRenderTime;
- F32 mGlobalSwayPhase;
- F32 mGlobalSwayTimeRatio;
- F32 mGlobalLightPhase;
- F32 mGlobalLightTimeRatio;
- U32 mFrameSerialID;
- U32 mQuadTreeLevels; // Quad-Tree Levels.
- U32 mPotentialFoliageNodes; // Potential Foliage Nodes.
- U32 mNextAllocatedNodeIdx; // Next Allocated Node Index.
- U32 mBillboardsAcquired; // Billboards Acquired.
- // Used for alpha lookup in the pixel shader
- GFXTexHandle mAlphaTexture;
- GFXStateBlockRef mPlacementSB;
- GFXStateBlockRef mRenderSB;
- GFXStateBlockRef mDebugSB;
- GFXShaderConstBufferRef mFoliageShaderConsts;
- GFXShaderConstHandle* mFoliageShaderProjectionSC;
- GFXShaderConstHandle* mFoliageShaderWorldSC;
- GFXShaderConstHandle* mFoliageShaderGlobalSwayPhaseSC;
- GFXShaderConstHandle* mFoliageShaderSwayMagnitudeSideSC;
- GFXShaderConstHandle* mFoliageShaderSwayMagnitudeFrontSC;
- GFXShaderConstHandle* mFoliageShaderGlobalLightPhaseSC;
- GFXShaderConstHandle* mFoliageShaderLuminanceMagnitudeSC;
- GFXShaderConstHandle* mFoliageShaderLuminanceMidpointSC;
- GFXShaderConstHandle* mFoliageShaderDistanceRangeSC;
- GFXShaderConstHandle* mFoliageShaderCameraPosSC;
- GFXShaderConstHandle* mFoliageShaderTrueBillboardSC;
- //pixel shader
- GFXShaderConstHandle* mFoliageShaderGroundAlphaSC;
- GFXShaderConstHandle* mFoliageShaderAmbientColorSC;
- GFXShaderConstHandle* mDiffuseTextureSC;
- GFXShaderConstHandle* mAlphaMapTextureSC;
- bool mDirty;
-
- void SetupShader();
- void SetupBuffers();
- void renderObject(ObjectRenderInst *ri, SceneRenderState *state, BaseMatInstance*);
- void renderBuffers(SceneRenderState* state);
- void renderArc(const F32 fRadiusX, const F32 fRadiusY);
- void renderPlacementArea(const F32 ElapsedTime);
- void renderQuad(fxFoliageQuadrantNode* quadNode, const MatrixF& RenderTransform, const bool UseDebug);
- void computeAlphaTex();
- public:
- fxFoliageReplicator();
- ~fxFoliageReplicator();
- void StartUp(void);
- void ShowReplication(void);
- void HideReplication(void);
- // SceneObject
- virtual void prepRenderImage( SceneRenderState *state );
- // SimObject
- bool onAdd();
- void onRemove();
- void inspectPostApply();
- // NetObject
- U32 packUpdate(NetConnection *conn, U32 mask, BitStream *stream);
- void unpackUpdate(NetConnection *conn, BitStream *stream);
- // Editor
- void onGhostAlwaysDone();
- // ConObject.
- static void initPersistFields();
- // Field Data.
- class tagFieldData
- {
- public:
- bool mUseDebugInfo;
- F32 mDebugBoxHeight;
- U32 mSeed;
- StringTableEntry mFoliageFile;
- GFXTexHandle mFoliageTexture;
- U32 mFoliageCount;
- U32 mFoliageRetries;
- U32 mInnerRadiusX;
- U32 mInnerRadiusY;
- U32 mOuterRadiusX;
- U32 mOuterRadiusY;
- F32 mMinWidth;
- F32 mMaxWidth;
- F32 mMinHeight;
- F32 mMaxHeight;
- bool mFixAspectRatio;
- bool mFixSizeToMax;
- F32 mOffsetZ;
- bool mRandomFlip;
- bool mUseTrueBillboards;
- bool mUseCulling;
- U32 mCullResolution;
- F32 mViewDistance;
- F32 mViewClosest;
- F32 mFadeInRegion;
- F32 mFadeOutRegion;
- F32 mAlphaCutoff;
- F32 mGroundAlpha;
- bool mSwayOn;
- bool mSwaySync;
- F32 mSwayMagnitudeSide;
- F32 mSwayMagnitudeFront;
- F32 mMinSwayTime;
- F32 mMaxSwayTime;
- bool mLightOn;
- bool mLightSync;
- F32 mMinLuminance;
- F32 mMaxLuminance;
- F32 mLightTime;
- bool mAllowOnTerrain;
- bool mAllowStatics;
- bool mAllowOnWater;
- bool mAllowWaterSurface;
- S32 mAllowedTerrainSlope;
- bool mHideFoliage;
- bool mShowPlacementArea;
- U32 mPlacementBandHeight;
- LinearColorF mPlaceAreaColour;
- F32 mAmbientModulationBias;
- tagFieldData()
- {
- // Set Defaults.
- mUseDebugInfo = false;
- mDebugBoxHeight = 1.0f;
- mSeed = 1376312589;
- mFoliageFile = StringTable->EmptyString();
- mFoliageTexture = GFXTexHandle();
- mFoliageCount = 10;
- mFoliageRetries = 100;
- mInnerRadiusX = 0;
- mInnerRadiusY = 0;
- mOuterRadiusX = 128;
- mOuterRadiusY = 128;
- mMinWidth = 1;
- mMaxWidth = 3;
- mMinHeight = 1;
- mMaxHeight = 5;
- mFixAspectRatio = true;
- mFixSizeToMax = false;
- mOffsetZ = 0;
- mRandomFlip = true;
- mUseTrueBillboards = false;
- mUseCulling = true;
- mCullResolution = 64;
- mViewDistance = 50.0f;
- mViewClosest = 1.0f;
- mFadeInRegion = 10.0f;
- mFadeOutRegion = 1.0f;
- mAlphaCutoff = 0.2f;
- mGroundAlpha = 1.0f;
- mSwayOn = false;
- mSwaySync = false;
- mSwayMagnitudeSide = 0.1f;
- mSwayMagnitudeFront = 0.2f;
- mMinSwayTime = 3.0f;
- mMaxSwayTime = 10.0f;
- mLightOn = false;
- mLightSync = false;
- mMinLuminance = 0.7f;
- mMaxLuminance = 1.0f;
- mLightTime = 5.0f;
- mAllowOnTerrain = true;
- mAllowStatics = true;
- mAllowOnWater = false;
- mAllowWaterSurface = false;
- mAllowedTerrainSlope = 90;
- mHideFoliage = false;
- mShowPlacementArea = true;
- mPlacementBandHeight = 25;
- mPlaceAreaColour .set(0.4f, 0, 0.8f);
- mAmbientModulationBias = 1.0f;
- }
- } mFieldData;
- // Declare Console Object.
- DECLARE_CONOBJECT(fxFoliageReplicator);
- };
- #pragma warning( pop )
- #endif // _FOLIAGEREPLICATOR_H_
|