levelInfo.cpp 12 KB

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