VolumetricFog.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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 _VolumetricFog_H_
  23. #define _VolumetricFog_H_
  24. #ifndef _SCENEOBJECT_H_
  25. #include "scene/sceneObject.h"
  26. #endif
  27. #ifndef _MATTEXTURETARGET_H_
  28. #include "materials/matTextureTarget.h"
  29. #endif
  30. #ifndef _GFXSHADER_H_
  31. #include "gfx/gfxShader.h"
  32. #endif
  33. #ifndef _GFXTARGET_H_
  34. #include "gfx/gfxTarget.h"
  35. #endif
  36. #ifndef _GFXVERTEXBUFFER_H_
  37. #include "gfx/gfxVertexBuffer.h"
  38. #endif
  39. #ifndef _TSSHAPE_H_
  40. #include "ts/tsShape.h"
  41. #endif
  42. #ifndef _POST_EFFECT_H_
  43. #include "postFx/postEffect.h"
  44. #endif
  45. #include "gui/core/guiCanvas.h"
  46. #ifndef _ASSET_PTR_H_
  47. #include "assets/assetPtr.h"
  48. #endif
  49. #ifndef SHAPEASSET_H
  50. #include "T3D/assets/ShapeAsset.h"
  51. #endif
  52. class VolumetricFogRTManager;
  53. class VolumetricFog : public SceneObject
  54. {
  55. typedef SceneObject Parent;
  56. // Maskbits for updating
  57. enum
  58. {
  59. VolumetricFogMask = Parent::NextFreeMask,
  60. FogColorMask = Parent::NextFreeMask << 1,
  61. FogDensityMask = Parent::NextFreeMask << 2,
  62. FogModulationMask = Parent::NextFreeMask << 3,
  63. FogPostFXMask = Parent::NextFreeMask << 4,
  64. FogShapeMask = Parent::NextFreeMask << 5,
  65. NextFreeMask = Parent::NextFreeMask << 6
  66. };
  67. // Struct which holds the shape details
  68. struct meshes
  69. {
  70. F32 det_size;
  71. S32 sub_shape;
  72. S32 obj_det;
  73. U32 num_verts;
  74. GFXVertexPNTT *verts;
  75. Vector <GFXPrimitive> *piArray;
  76. Vector <U32> *indices;
  77. };
  78. protected:
  79. // Rendertargets;
  80. GFXTextureTargetRef z_buf;
  81. NamedTexTargetRef mDeferredTarget;
  82. NamedTexTargetRef mDepthBufferTarget;
  83. NamedTexTargetRef mFrontBufferTarget;
  84. // Fog Modulation texture
  85. GFXTexHandle mTexture;
  86. // Shaders
  87. GFXShaderRef mShader;
  88. GFXShaderRef mDeferredShader;
  89. GFXShaderRef mReflectionShader;
  90. // Stateblocks
  91. GFXStateBlockDesc descD;
  92. GFXStateBlockDesc descF;
  93. GFXStateBlockDesc desc_preD;
  94. GFXStateBlockDesc desc_preF;
  95. GFXStateBlockDesc desc_refl;
  96. GFXStateBlockRef mStateblockD;
  97. GFXStateBlockRef mStateblockF;
  98. GFXStateBlockRef mStateblock_preD;
  99. GFXStateBlockRef mStateblock_preF;
  100. GFXStateBlockRef mStateblock_refl;
  101. // Shaderconstants
  102. GFXShaderConstBufferRef mShaderConsts;
  103. GFXShaderConstHandle *mModelViewProjSC;
  104. GFXShaderConstHandle *mFadeSizeSC;
  105. GFXShaderConstHandle *mFogColorSC;
  106. GFXShaderConstHandle *mFogDensitySC;
  107. GFXShaderConstHandle *mPreBias;
  108. GFXShaderConstHandle *mAccumTime;
  109. GFXShaderConstHandle *mIsTexturedSC;
  110. GFXShaderConstHandle *mModSpeedSC;
  111. GFXShaderConstHandle *mModStrengthSC;
  112. GFXShaderConstHandle *mViewPointSC;
  113. GFXShaderConstHandle *mTexScaleSC;
  114. GFXShaderConstHandle *mTexTilesSC;
  115. GFXShaderConstBufferRef mPPShaderConsts;
  116. GFXShaderConstHandle *mPPModelViewProjSC;
  117. GFXShaderConstHandle *mAmbientColorSC;
  118. GFXShaderConstBufferRef mReflShaderConsts;
  119. GFXShaderConstHandle *mReflModelViewProjSC;
  120. GFXShaderConstHandle *mReflFogColorSC;
  121. GFXShaderConstHandle *mReflFogDensitySC;
  122. GFXShaderConstHandle *mReflFogStrengthSC;
  123. // Vertex and Prim. Buffer
  124. GFXVertexBufferHandle<GFXVertexPNTT> mVB;
  125. GFXPrimitiveBufferHandle mPB;
  126. // Fog volume data;
  127. AssetPtr<ShapeAsset> mShapeAsset;
  128. StringTableEntry mShapeAssetId;
  129. StringTableEntry mShapeName;
  130. ColorI mFogColor;
  131. F32 mFogDensity;
  132. bool mIgnoreWater;
  133. bool mReflect;
  134. Vector<meshes> det_size;
  135. bool mShapeLoaded;
  136. F32 mPixelSize;
  137. F32 mFadeSize;
  138. U32 mCurDetailLevel;
  139. U32 mNumDetailLevels;
  140. F32 mObjSize;
  141. F32 mRadius;
  142. OrientedBox3F ColBox;
  143. VectorF mObjScale;
  144. F32 mMinDisplaySize;
  145. F32 mInvScale;
  146. // Fog Modulation data
  147. String mTextureName;
  148. bool mIsTextured;
  149. F32 mTexTiles;
  150. F32 mStrength;
  151. Point2F mSpeed1;
  152. Point2F mSpeed2;
  153. Point4F mSpeed;
  154. Point2F mTexScale;
  155. // Fog Rendering data
  156. Point3F camPos;
  157. Point2F mViewPoint;
  158. F32 mFOV;
  159. F32 viewDist;
  160. bool mIsVBDirty;
  161. bool mIsPBDirty;
  162. bool mCamInFog;
  163. bool mResizing;
  164. PlatformWindow *mPlatformWindow;
  165. // Reflections
  166. F32 mFogReflStrength;
  167. // PostFX
  168. PostEffect *glowFX;
  169. bool mUseGlow;
  170. F32 mGlowStrength;
  171. U8 mGlowing;
  172. F32 mCurGlow;
  173. bool mModifLightRays;
  174. F32 mLightRayMod;
  175. F32 mOldLightRayStrength;
  176. GameConnection* conn;
  177. U32 mCounter;
  178. void ResizeRT(PlatformWindow *win, bool resize);
  179. protected:
  180. // Protected methods
  181. bool onAdd();
  182. void onRemove();
  183. void handleResize(VolumetricFogRTManager *RTM, bool resize);
  184. void handleCanvasResize(GuiCanvas* canvas);
  185. bool LoadShape();
  186. bool setupRenderer();
  187. void InitTexture();
  188. bool UpdateBuffers(U32 dl,bool force=true);
  189. void processTick(const Move *move);
  190. void _enterFog(ShapeBase *control);
  191. void _leaveFog(ShapeBase *control);
  192. static bool _setShapeAsset(void* obj, const char* index, const char* data);
  193. public:
  194. // Public methods
  195. VolumetricFog();
  196. ~VolumetricFog();
  197. static void initPersistFields();
  198. virtual void inspectPostApply();
  199. U32 packUpdate(NetConnection *conn, U32 mask, BitStream *stream);
  200. void unpackUpdate(NetConnection *conn, BitStream *stream);
  201. void prepRenderImage(SceneRenderState* state);
  202. void render(ObjectRenderInst *ri, SceneRenderState *state, BaseMatInstance *overrideMat);
  203. void reflect_render(ObjectRenderInst *ri, SceneRenderState *state, BaseMatInstance *overrideMat);
  204. // Methods for modifying & networking various fog elements
  205. // Used in script
  206. void setFogColor(LinearColorF color);
  207. void setFogColor(ColorI color);
  208. void setFogDensity(F32 density);
  209. void setFogModulation(F32 strength, Point2F speed1, Point2F speed2);
  210. void setFogGlow(bool on_off, F32 strength);
  211. void setFogLightray(bool on_off, F32 strength);
  212. bool isInsideFog();
  213. bool setShapeAsset(const StringTableEntry shapeAssetId);
  214. DECLARE_CONOBJECT(VolumetricFog);
  215. DECLARE_CALLBACK(void, onEnterFog, (SimObjectId obj));
  216. DECLARE_CALLBACK(void, onLeaveFog, (SimObjectId obj));
  217. };
  218. #endif