VolumetricFog.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. DECLARE_SHAPEASSET(VolumetricFog, Shape, onShapeChanged);
  79. DECLARE_ASSET_NET_SETGET(VolumetricFog, Shape, FogShapeMask);
  80. protected:
  81. // Rendertargets;
  82. GFXTextureTargetRef z_buf;
  83. NamedTexTargetRef mDeferredTarget;
  84. NamedTexTargetRef mDepthBufferTarget;
  85. NamedTexTargetRef mFrontBufferTarget;
  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. ColorI mFogColor;
  128. F32 mFogDensity;
  129. bool mIgnoreWater;
  130. bool mReflect;
  131. Vector<meshes> det_size;
  132. bool mShapeLoaded;
  133. F32 mPixelSize;
  134. F32 mFadeSize;
  135. U32 mCurDetailLevel;
  136. U32 mNumDetailLevels;
  137. F32 mObjSize;
  138. F32 mRadius;
  139. OrientedBox3F ColBox;
  140. VectorF mObjScale;
  141. F32 mMinDisplaySize;
  142. F32 mInvScale;
  143. // Fog Modulation data
  144. DECLARE_IMAGEASSET(VolumetricFog, Texture, onImageChanged, GFXStaticTextureSRGBProfile);
  145. DECLARE_ASSET_NET_SETGET(VolumetricFog, Texture, FogModulationMask);
  146. bool mIsTextured;
  147. F32 mTexTiles;
  148. F32 mStrength;
  149. Point2F mSpeed1;
  150. Point2F mSpeed2;
  151. Point4F mSpeed;
  152. Point2F mTexScale;
  153. // Fog Rendering data
  154. Point3F camPos;
  155. Point2F mViewPoint;
  156. F32 mFOV;
  157. F32 viewDist;
  158. bool mIsVBDirty;
  159. bool mIsPBDirty;
  160. bool mCamInFog;
  161. bool mResizing;
  162. PlatformWindow *mPlatformWindow;
  163. // Reflections
  164. F32 mFogReflStrength;
  165. // PostFX
  166. PostEffect *glowFX;
  167. bool mUseGlow;
  168. F32 mGlowStrength;
  169. U8 mGlowing;
  170. F32 mCurGlow;
  171. bool mModifLightRays;
  172. F32 mLightRayMod;
  173. F32 mOldLightRayStrength;
  174. GameConnection* conn;
  175. U32 mCounter;
  176. void ResizeRT(PlatformWindow *win, bool resize);
  177. protected:
  178. // Protected methods
  179. bool onAdd();
  180. void onRemove();
  181. void handleResize(VolumetricFogRTManager *RTM, bool resize);
  182. void handleCanvasResize(GuiCanvas* canvas);
  183. bool LoadShape();
  184. bool setupRenderer();
  185. void InitTexture();
  186. bool UpdateBuffers(U32 dl,bool force=true);
  187. void processTick(const Move *move);
  188. void _enterFog(ShapeBase *control);
  189. void _leaveFog(ShapeBase *control);
  190. static bool _setShapeAsset(void* obj, const char* index, const char* data);
  191. void onImageChanged() {}
  192. public:
  193. // Public methods
  194. VolumetricFog();
  195. ~VolumetricFog();
  196. static void initPersistFields();
  197. virtual void inspectPostApply();
  198. U32 packUpdate(NetConnection *conn, U32 mask, BitStream *stream);
  199. void unpackUpdate(NetConnection *conn, BitStream *stream);
  200. void prepRenderImage(SceneRenderState* state);
  201. void render(ObjectRenderInst *ri, SceneRenderState *state, BaseMatInstance *overrideMat);
  202. void reflect_render(ObjectRenderInst *ri, SceneRenderState *state, BaseMatInstance *overrideMat);
  203. // Methods for modifying & networking various fog elements
  204. // Used in script
  205. void setFogColor(LinearColorF color);
  206. void setFogColor(ColorI color);
  207. void setFogDensity(F32 density);
  208. void setFogModulation(F32 strength, Point2F speed1, Point2F speed2);
  209. void setFogGlow(bool on_off, F32 strength);
  210. void setFogLightray(bool on_off, F32 strength);
  211. bool isInsideFog();
  212. bool setShapeAsset(const StringTableEntry shapeAssetId);
  213. void onShapeChanged() {}
  214. DECLARE_CONOBJECT(VolumetricFog);
  215. DECLARE_CALLBACK(void, onEnterFog, (SimObjectId obj));
  216. DECLARE_CALLBACK(void, onLeaveFog, (SimObjectId obj));
  217. };
  218. #endif