levelInfo.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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. #include "platform/platform.h"
  23. #include "T3D/levelInfo.h"
  24. #include "console/consoleTypes.h"
  25. #include "core/stream/bitStream.h"
  26. #include "scene/sceneManager.h"
  27. #include "lighting/advanced/advancedLightManager.h"
  28. #include "lighting/advanced/advancedLightBinManager.h"
  29. #include "sfx/sfxAmbience.h"
  30. #include "sfx/sfxSoundscape.h"
  31. #include "sfx/sfxSystem.h"
  32. #include "sfx/sfxTypes.h"
  33. #include "console/engineAPI.h"
  34. #include "math/mathIO.h"
  35. #include "torqueConfig.h"
  36. #include "T3D/accumulationVolume.h"
  37. #include "console/typeValidators.h"
  38. #include "materials/materialManager.h"
  39. IMPLEMENT_CO_NETOBJECT_V1(LevelInfo);
  40. ConsoleDocClass( LevelInfo,
  41. "@brief Stores and controls the rendering and status information for a game level.\n\n"
  42. "@tsexample\n"
  43. "new LevelInfo(theLevelInfo)\n"
  44. "{\n"
  45. " visibleDistance = \"1000\";\n"
  46. " fogColor = \"0.6 0.6 0.7 1\";\n"
  47. " fogDensity = \"0\";\n"
  48. " fogDensityOffset = \"700\";\n"
  49. " fogAtmosphereHeight = \"0\";\n"
  50. " canvasClearColor = \"0 0 0 255\";\n"
  51. " canSaveDynamicFields = \"1\";\n"
  52. " levelName = \"Blank Room\";\n"
  53. " desc0 = \"A blank room ready to be populated with Torque objects.\";\n"
  54. " Enabled = \"1\";\n"
  55. "};\n"
  56. "@endtsexample\n"
  57. "@ingroup enviroMisc\n"
  58. );
  59. /// The color used to clear the canvas.
  60. /// @see GuiCanvas
  61. extern ColorI gCanvasClearColor;
  62. /// @see DecalManager
  63. extern F32 gDecalBias;
  64. /// @see AccumulationVolume
  65. extern GFXTexHandle gLevelAccuMap;
  66. /// Default SFXAmbience used to reset the global soundscape.
  67. extern SFXAmbience* sDefaultAmbience;
  68. //-----------------------------------------------------------------------------
  69. LevelInfo::LevelInfo()
  70. : mWorldSize( 10000.0f ),
  71. mNearClip( 0.1f ),
  72. mVisibleDistance( 1000.0f ),
  73. mVisibleGhostDistance ( 0 ),
  74. mDecalBias( 0.0015f ),
  75. mCanvasClearColor( 255, 0, 255, 255 ),
  76. mAmbientLightBlendPhase( 1.f ),
  77. mSoundAmbience( NULL ),
  78. mSoundDistanceModel( SFXDistanceModelLinear ),
  79. mSoundscape( NULL ),
  80. mDampness(0.0)
  81. {
  82. mFogData.density = 0.0f;
  83. mFogData.densityOffset = 0.0f;
  84. mFogData.atmosphereHeight = 0.0f;
  85. mFogData.color.set( 0.5f, 0.5f, 0.5f, 1.0f ),
  86. mNetFlags.set( ScopeAlways | Ghostable );
  87. mAdvancedLightmapSupport = true;
  88. // Register with the light manager activation signal, and we need to do it first
  89. // so the advanced light bin manager can be instructed about MRT lightmaps
  90. LightManager::smActivateSignal.notify(this, &LevelInfo::_onLMActivate, 0.01f);
  91. }
  92. //-----------------------------------------------------------------------------
  93. LevelInfo::~LevelInfo()
  94. {
  95. LightManager::smActivateSignal.remove(this, &LevelInfo::_onLMActivate);
  96. if (!mAccuTextureAsset.isNull())
  97. {
  98. gLevelAccuMap.free();
  99. }
  100. }
  101. //-----------------------------------------------------------------------------
  102. void LevelInfo::initPersistFields()
  103. {
  104. docsURL;
  105. addGroup( "Visibility" );
  106. addFieldV( "nearClip", TypeRangedF32, Offset( mNearClip, LevelInfo ), &CommonValidators::PositiveFloat, "Closest distance from the camera's position to render the world." );
  107. addFieldV( "visibleDistance", TypeRangedF32, Offset( mVisibleDistance, LevelInfo ), &CommonValidators::PositiveFloat, "Furthest distance from the camera's position to render the world." );
  108. addFieldV( "visibleGhostDistance", TypeRangedF32, Offset( mVisibleGhostDistance, LevelInfo ), &CommonValidators::PositiveFloat, "Furthest distance from the camera's position to render players. Defaults to visibleDistance." );
  109. addFieldV( "decalBias", TypeRangedF32, Offset( mDecalBias, LevelInfo ), &CommonValidators::PositiveFloat,
  110. "NearPlane bias used when rendering Decal and DecalRoad. This should be tuned to the visibleDistance in your level." );
  111. addFieldV("dampness", TypeRangedF32, Offset(mDampness, LevelInfo), &CommonValidators::NormalizedFloat,
  112. "@brief dampness influence");
  113. endGroup( "Visibility" );
  114. addGroup( "Fog" );
  115. addField( "fogColor", TypeColorF, Offset( mFogData.color, LevelInfo ),
  116. "The default color for the scene fog." );
  117. addFieldV( "fogDensity", TypeRangedF32, Offset( mFogData.density, LevelInfo ), &CommonValidators::NormalizedFloat,
  118. "The 0 to 1 density value for the exponential fog falloff." );
  119. addFieldV( "fogDensityOffset", TypeRangedF32, Offset( mFogData.densityOffset, LevelInfo ), &CommonValidators::PositiveFloat,
  120. "An offset from the camera in meters for moving the start of the fog effect." );
  121. addFieldV( "fogAtmosphereHeight", TypeRangedF32, Offset( mFogData.atmosphereHeight, LevelInfo ), &CommonValidators::PositiveFloat,
  122. "A height in meters for altitude fog falloff." );
  123. endGroup( "Fog" );
  124. addGroup( "LevelInfo" );
  125. addField( "canvasClearColor", TypeColorI, Offset( mCanvasClearColor, LevelInfo ),
  126. "The color used to clear the background before the scene or any GUIs are rendered." );
  127. endGroup( "LevelInfo" );
  128. addGroup( "Lighting" );
  129. addFieldV( "ambientLightBlendPhase", TypeRangedF32, Offset( mAmbientLightBlendPhase, LevelInfo ), &CommonValidators::PositiveFloat,
  130. "Number of seconds it takes to blend from one ambient light color to a different one." );
  131. addField( "ambientLightBlendCurve", TypeEaseF, Offset( mAmbientLightBlendCurve, LevelInfo ),
  132. "Interpolation curve to use for blending from one ambient light color to a different one." );
  133. //addField( "advancedLightmapSupport", TypeBool, Offset( mAdvancedLightmapSupport, LevelInfo ),
  134. // "Enable expanded support for mixing static and dynamic lighting (more costly)" );
  135. INITPERSISTFIELD_IMAGEASSET(AccuTexture, LevelInfo, "Accumulation texture.");
  136. endGroup( "Lighting" );
  137. addGroup( "Sound" );
  138. addField( "soundAmbience", TypeSFXAmbienceName, Offset( mSoundAmbience, LevelInfo ), "The global ambient sound environment." );
  139. addField( "soundDistanceModel", TypeSFXDistanceModel, Offset( mSoundDistanceModel, LevelInfo ), "The distance attenuation model to use." );
  140. endGroup( "Sound" );
  141. Parent::initPersistFields();
  142. }
  143. //-----------------------------------------------------------------------------
  144. void LevelInfo::inspectPostApply()
  145. {
  146. _updateSceneGraph();
  147. setMaskBits( 0xFFFFFFFF );
  148. Parent::inspectPostApply();
  149. }
  150. //-----------------------------------------------------------------------------
  151. U32 LevelInfo::packUpdate(NetConnection *conn, U32 mask, BitStream *stream)
  152. {
  153. U32 retMask = Parent::packUpdate(conn, mask, stream);
  154. stream->write( mNearClip );
  155. stream->write( mVisibleDistance );
  156. stream->write( mDecalBias );
  157. stream->write(mDampness);
  158. stream->write( mFogData.density );
  159. stream->write( mFogData.densityOffset );
  160. stream->write( mFogData.atmosphereHeight );
  161. stream->write( mFogData.color );
  162. stream->write( mCanvasClearColor );
  163. stream->write( mWorldSize );
  164. stream->writeFlag( mAdvancedLightmapSupport );
  165. stream->write( mAmbientLightBlendPhase );
  166. mathWrite( *stream, mAmbientLightBlendCurve );
  167. sfxWrite( stream, mSoundAmbience );
  168. stream->writeInt( mSoundDistanceModel, 1 );
  169. PACK_ASSET_REFACTOR(conn, AccuTexture);
  170. return retMask;
  171. }
  172. //-----------------------------------------------------------------------------
  173. void LevelInfo::unpackUpdate(NetConnection *conn, BitStream *stream)
  174. {
  175. Parent::unpackUpdate(conn, stream);
  176. stream->read( &mNearClip );
  177. stream->read( &mVisibleDistance );
  178. stream->read( &mDecalBias );
  179. stream->read(&mDampness);
  180. MATMGR->setDampness(mDampness);
  181. stream->read( &mFogData.density );
  182. stream->read( &mFogData.densityOffset );
  183. stream->read( &mFogData.atmosphereHeight );
  184. stream->read( &mFogData.color );
  185. stream->read( &mCanvasClearColor );
  186. stream->read( &mWorldSize );
  187. mAdvancedLightmapSupport = stream->readFlag();
  188. stream->read( &mAmbientLightBlendPhase );
  189. mathRead( *stream, &mAmbientLightBlendCurve );
  190. String errorStr;
  191. if( !sfxReadAndResolve( stream, &mSoundAmbience, errorStr ) )
  192. Con::errorf( "%s", errorStr.c_str() );
  193. mSoundDistanceModel = ( SFXDistanceModel ) stream->readInt( 1 );
  194. if( isProperlyAdded() )
  195. {
  196. _updateSceneGraph();
  197. if( mSoundscape )
  198. {
  199. if( mSoundAmbience )
  200. mSoundscape->setAmbience( mSoundAmbience );
  201. else
  202. mSoundscape->setAmbience( sDefaultAmbience );
  203. }
  204. SFX->setDistanceModel( mSoundDistanceModel );
  205. }
  206. UNPACK_ASSET_REFACTOR(conn, AccuTexture);
  207. setLevelAccuTexture();
  208. }
  209. //-----------------------------------------------------------------------------
  210. bool LevelInfo::onAdd()
  211. {
  212. if ( !Parent::onAdd() )
  213. return false;
  214. // If no sound ambience has been set, default to
  215. // 'AudioAmbienceDefault'.
  216. if( !mSoundAmbience )
  217. Sim::findObject( "AudioAmbienceDefault", mSoundAmbience );
  218. // Set up sound on client.
  219. if( isClientObject() )
  220. {
  221. SFX->setDistanceModel( mSoundDistanceModel );
  222. // Set up the global ambient soundscape.
  223. mSoundscape = SFX->getSoundscapeManager()->getGlobalSoundscape();
  224. if( mSoundAmbience )
  225. mSoundscape->setAmbience( mSoundAmbience );
  226. }
  227. _updateSceneGraph();
  228. return true;
  229. }
  230. //-----------------------------------------------------------------------------
  231. void LevelInfo::onRemove()
  232. {
  233. if( mSoundscape )
  234. mSoundscape->setAmbience( sDefaultAmbience );
  235. Parent::onRemove();
  236. }
  237. //-----------------------------------------------------------------------------
  238. void LevelInfo::_updateSceneGraph()
  239. {
  240. // Clamp above zero before setting on the sceneGraph.
  241. // If we don't we get serious crashes.
  242. if ( mNearClip <= 0.0f )
  243. mNearClip = 0.001f;
  244. SceneManager* scene = isClientObject() ? gClientSceneGraph : gServerSceneGraph;
  245. scene->setNearClip( mNearClip );
  246. scene->setVisibleDistance( mVisibleDistance );
  247. scene->setVisibleGhostDistance( mVisibleGhostDistance );
  248. gDecalBias = mDecalBias;
  249. // Set ambient lighting properties.
  250. scene->setAmbientLightTransitionTime( mAmbientLightBlendPhase * 1000.f );
  251. scene->setAmbientLightTransitionCurve( mAmbientLightBlendCurve );
  252. // Copy our AirFogData into the sceneGraph.
  253. scene->setFogData( mFogData );
  254. // If the level info specifies that MRT pre-pass should be used in this scene
  255. // enable it via the appropriate light manager
  256. // (Basic lighting doesn't do anything different right now)
  257. #ifndef TORQUE_DEDICATED
  258. if(isClientObject())
  259. _onLMActivate(LIGHTMGR->getId(), true);
  260. #endif
  261. // TODO: This probably needs to be moved.
  262. gCanvasClearColor = mCanvasClearColor;
  263. }
  264. //-----------------------------------------------------------------------------
  265. void LevelInfo::_onLMActivate(const char *lm, bool enable)
  266. {
  267. #ifndef TORQUE_DEDICATED
  268. // Advanced light manager
  269. if(enable && String(lm) == String("ADVLM"))
  270. {
  271. AssertFatal(dynamic_cast<AdvancedLightManager *>(LIGHTMGR), "Bad light manager type!");
  272. AdvancedLightManager *lightMgr = static_cast<AdvancedLightManager *>(LIGHTMGR);
  273. lightMgr->getLightBinManager()->MRTLightmapsDuringDeferred(mAdvancedLightmapSupport);
  274. }
  275. #endif
  276. }
  277. void LevelInfo::setLevelAccuTexture()
  278. {
  279. if (isClientObject() && getAccuTexture())
  280. {
  281. gLevelAccuMap = getAccuTexture();
  282. }
  283. AccumulationVolume::refreshVolumes();
  284. }