levelInfo.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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. /// @see AccumulationVolume
  62. extern GFXTexHandle gLevelAccuMap;
  63. /// Default SFXAmbience used to reset the global soundscape.
  64. static SFXAmbience sDefaultAmbience;
  65. //-----------------------------------------------------------------------------
  66. LevelInfo::LevelInfo()
  67. : mNearClip( 0.1f ),
  68. mVisibleDistance( 1000.0f ),
  69. mVisibleGhostDistance ( 0 ),
  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. mAccuTextureName = "";
  85. mAccuTexture = NULL;
  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. }
  95. //-----------------------------------------------------------------------------
  96. void LevelInfo::initPersistFields()
  97. {
  98. addGroup( "Visibility" );
  99. addField( "nearClip", TypeF32, Offset( mNearClip, LevelInfo ), "Closest distance from the camera's position to render the world." );
  100. addField( "visibleDistance", TypeF32, Offset( mVisibleDistance, LevelInfo ), "Furthest distance from the camera's position to render the world." );
  101. addField( "visibleGhostDistance", TypeF32, Offset( mVisibleGhostDistance, LevelInfo ), "Furthest distance from the camera's position to render players. Defaults to visibleDistance." );
  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. addProtectedField("AccuTexture", TypeStringFilename, Offset(mAccuTextureName, LevelInfo),
  127. &_setLevelAccuTexture, &defaultProtectedGetFn, "Accumulation texture.");
  128. endGroup( "Lighting" );
  129. addGroup( "Sound" );
  130. addField( "soundAmbience", TypeSFXAmbienceName, Offset( mSoundAmbience, LevelInfo ), "The global ambient sound environment." );
  131. addField( "soundDistanceModel", TypeSFXDistanceModel, Offset( mSoundDistanceModel, LevelInfo ), "The distance attenuation model to use." );
  132. endGroup( "Sound" );
  133. Parent::initPersistFields();
  134. }
  135. //-----------------------------------------------------------------------------
  136. void LevelInfo::inspectPostApply()
  137. {
  138. _updateSceneGraph();
  139. setMaskBits( 0xFFFFFFFF );
  140. Parent::inspectPostApply();
  141. }
  142. //-----------------------------------------------------------------------------
  143. U32 LevelInfo::packUpdate(NetConnection *conn, U32 mask, BitStream *stream)
  144. {
  145. U32 retMask = Parent::packUpdate(conn, mask, stream);
  146. stream->write( mNearClip );
  147. stream->write( mVisibleDistance );
  148. stream->write( mDecalBias );
  149. stream->write( mFogData.density );
  150. stream->write( mFogData.densityOffset );
  151. stream->write( mFogData.atmosphereHeight );
  152. stream->write( mFogData.color );
  153. stream->write( mCanvasClearColor );
  154. stream->write( mWorldSize );
  155. stream->writeFlag( mAdvancedLightmapSupport );
  156. stream->write( mAmbientLightBlendPhase );
  157. mathWrite( *stream, mAmbientLightBlendCurve );
  158. sfxWrite( stream, mSoundAmbience );
  159. stream->writeInt( mSoundDistanceModel, 1 );
  160. stream->write(mAccuTextureName);
  161. return retMask;
  162. }
  163. //-----------------------------------------------------------------------------
  164. void LevelInfo::unpackUpdate(NetConnection *conn, BitStream *stream)
  165. {
  166. Parent::unpackUpdate(conn, stream);
  167. stream->read( &mNearClip );
  168. stream->read( &mVisibleDistance );
  169. stream->read( &mDecalBias );
  170. stream->read( &mFogData.density );
  171. stream->read( &mFogData.densityOffset );
  172. stream->read( &mFogData.atmosphereHeight );
  173. stream->read( &mFogData.color );
  174. stream->read( &mCanvasClearColor );
  175. stream->read( &mWorldSize );
  176. mAdvancedLightmapSupport = stream->readFlag();
  177. stream->read( &mAmbientLightBlendPhase );
  178. mathRead( *stream, &mAmbientLightBlendCurve );
  179. String errorStr;
  180. if( !sfxReadAndResolve( stream, &mSoundAmbience, errorStr ) )
  181. Con::errorf( "%s", errorStr.c_str() );
  182. mSoundDistanceModel = ( SFXDistanceModel ) stream->readInt( 1 );
  183. if( isProperlyAdded() )
  184. {
  185. _updateSceneGraph();
  186. if( mSoundscape )
  187. {
  188. if( mSoundAmbience )
  189. mSoundscape->setAmbience( mSoundAmbience );
  190. else
  191. mSoundscape->setAmbience( &sDefaultAmbience );
  192. }
  193. SFX->setDistanceModel( mSoundDistanceModel );
  194. }
  195. stream->read(&mAccuTextureName);
  196. setLevelAccuTexture(mAccuTextureName);
  197. }
  198. //-----------------------------------------------------------------------------
  199. bool LevelInfo::onAdd()
  200. {
  201. if ( !Parent::onAdd() )
  202. return false;
  203. // If no sound ambience has been set, default to
  204. // 'AudioAmbienceDefault'.
  205. if( !mSoundAmbience )
  206. Sim::findObject( "AudioAmbienceDefault", mSoundAmbience );
  207. // Set up sound on client.
  208. if( isClientObject() )
  209. {
  210. SFX->setDistanceModel( mSoundDistanceModel );
  211. // Set up the global ambient soundscape.
  212. mSoundscape = SFX->getSoundscapeManager()->getGlobalSoundscape();
  213. if( mSoundAmbience )
  214. mSoundscape->setAmbience( mSoundAmbience );
  215. }
  216. _updateSceneGraph();
  217. return true;
  218. }
  219. //-----------------------------------------------------------------------------
  220. void LevelInfo::onRemove()
  221. {
  222. if( mSoundscape )
  223. mSoundscape->setAmbience( &sDefaultAmbience );
  224. Parent::onRemove();
  225. }
  226. //-----------------------------------------------------------------------------
  227. void LevelInfo::_updateSceneGraph()
  228. {
  229. // Clamp above zero before setting on the sceneGraph.
  230. // If we don't we get serious crashes.
  231. if ( mNearClip <= 0.0f )
  232. mNearClip = 0.001f;
  233. SceneManager* scene = isClientObject() ? gClientSceneGraph : gServerSceneGraph;
  234. scene->setNearClip( mNearClip );
  235. scene->setVisibleDistance( mVisibleDistance );
  236. scene->setVisibleGhostDistance( mVisibleGhostDistance );
  237. gDecalBias = mDecalBias;
  238. // Set ambient lighting properties.
  239. scene->setAmbientLightTransitionTime( mAmbientLightBlendPhase * 1000.f );
  240. scene->setAmbientLightTransitionCurve( mAmbientLightBlendCurve );
  241. // Copy our AirFogData into the sceneGraph.
  242. scene->setFogData( mFogData );
  243. // If the level info specifies that MRT pre-pass should be used in this scene
  244. // enable it via the appropriate light manager
  245. // (Basic lighting doesn't do anything different right now)
  246. #ifndef TORQUE_DEDICATED
  247. if(isClientObject())
  248. _onLMActivate(LIGHTMGR->getId(), true);
  249. #endif
  250. // TODO: This probably needs to be moved.
  251. gCanvasClearColor = mCanvasClearColor;
  252. }
  253. //-----------------------------------------------------------------------------
  254. void LevelInfo::_onLMActivate(const char *lm, bool enable)
  255. {
  256. #ifndef TORQUE_DEDICATED
  257. // Advanced light manager
  258. if(enable && String(lm) == String("ADVLM"))
  259. {
  260. AssertFatal(dynamic_cast<AdvancedLightManager *>(LIGHTMGR), "Bad light manager type!");
  261. AdvancedLightManager *lightMgr = static_cast<AdvancedLightManager *>(LIGHTMGR);
  262. lightMgr->getLightBinManager()->MRTLightmapsDuringPrePass(mAdvancedLightmapSupport);
  263. }
  264. #endif
  265. }
  266. bool LevelInfo::_setLevelAccuTexture(void *object, const char *index, const char *data)
  267. {
  268. LevelInfo* volume = reinterpret_cast< LevelInfo* >(object);
  269. volume->setLevelAccuTexture(data);
  270. return false;
  271. }
  272. void LevelInfo::setLevelAccuTexture(const String& name)
  273. {
  274. mAccuTextureName = name;
  275. if (isClientObject() && mAccuTextureName.isNotEmpty())
  276. {
  277. mAccuTexture.set(mAccuTextureName, &GFXDefaultStaticDiffuseProfile, "AccumulationVolume::mAccuTexture");
  278. if (mAccuTexture.isNull())
  279. Con::warnf("AccumulationVolume::setTexture - Unable to load texture: %s", mAccuTextureName.c_str());
  280. else
  281. gLevelAccuMap = mAccuTexture;
  282. }
  283. }