levelInfo.cpp 11 KB

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