levelInfo.cpp 11 KB

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