levelInfo.cpp 13 KB

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