levelInfo.cpp 12 KB

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