levelInfo.cpp 13 KB

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