2
0

sfxEmitter.cpp 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188
  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/sfx/sfxEmitter.h"
  24. #include "sfx/sfxSystem.h"
  25. #include "sfx/sfxTrack.h"
  26. #include "sfx/sfxSource.h"
  27. #include "sfx/sfxTypes.h"
  28. #include "scene/sceneRenderState.h"
  29. #include "core/stream/bitStream.h"
  30. #include "sim/netConnection.h"
  31. #include "math/mathIO.h"
  32. #include "math/mQuat.h"
  33. #include "renderInstance/renderPassManager.h"
  34. #include "gfx/gfxTransformSaver.h"
  35. #include "gfx/gfxDrawUtil.h"
  36. #include "gfx/primBuilder.h"
  37. #include "console/engineAPI.h"
  38. IMPLEMENT_CO_NETOBJECT_V1( SFXEmitter );
  39. ConsoleDocClass( SFXEmitter,
  40. "@brief An invisible 3D object that emits sound.\n\n"
  41. "Sound emitters are used to place sounds in the level. They are full 3D objects with their own position and orientation and "
  42. "when assigned 3D sounds, the transform and velocity of the sound emitter object will be applied to the 3D sound.\n\n"
  43. "Sound emitters can be set up of in either of two ways:\n"
  44. "<ul>\n"
  45. "<li><p>By assigning an existing SFXTrack to the emitter's #track property.</p>\n"
  46. "<p>In this case the general sound setup (3D, streaming, looping, etc.) will be taken from #track. However, the emitter's "
  47. "own properties will still override their corresponding properties in the #track's SFXDescription.</p></li>\n"
  48. "<li><p>By directly assigning a sound file to the emitter's #fileName property.</p>\n"
  49. "<p>In this case, the sound file will be set up for playback according to the properties defined on the emitter.</p>\n"
  50. "</ul>\n\n"
  51. "Using #playOnAdd emitters can be configured to start playing immediately when they are added to the system (e.g. when the level "
  52. "objects are loaded from the mission file).\n\n"
  53. "@note A sound emitter need not necessarily emit a 3D sound. Instead, sound emitters may also be used to play "
  54. "non-positional sounds. For placing background audio to a level, however, it is usually easier to use LevelInfo::soundAmbience.\n\n"
  55. "@section SFXEmitter_networking Sound Emitters and Networking\n\n"
  56. "It is important to be aware of the fact that sounds will only play client-side whereas SFXEmitter objects are server-side "
  57. "entities. This means that a server-side object has no connection to the actual sound playing on the client. It is thus "
  58. "not possible for the server-object to perform queries about playback status and other source-related properties as these "
  59. "may in fact differ from client to client.\n\n"
  60. "@ingroup SFX\n"
  61. );
  62. extern bool gEditingMission;
  63. bool SFXEmitter::smRenderEmitters;
  64. F32 SFXEmitter::smRenderPointSize = 0.8f;
  65. F32 SFXEmitter::smRenderRadialIncrements = 5.f;
  66. F32 SFXEmitter::smRenderSweepIncrements = 5.f;
  67. F32 SFXEmitter::smRenderPointDistance = 5.f;
  68. ColorI SFXEmitter::smRenderColorPlayingInRange( 50, 255, 50, 255 );
  69. ColorI SFXEmitter::smRenderColorPlayingOutOfRange( 50, 128, 50, 255 );
  70. ColorI SFXEmitter::smRenderColorStoppedInRange( 0, 0, 0, 255 );
  71. ColorI SFXEmitter::smRenderColorStoppedOutOfRange( 128, 128, 128, 255 );
  72. ColorI SFXEmitter::smRenderColorInnerCone( 0, 0, 255, 255 );
  73. ColorI SFXEmitter::smRenderColorOuterCone( 255, 0, 255, 255 );
  74. ColorI SFXEmitter::smRenderColorOutsideVolume( 255, 0, 0, 255 );
  75. ColorI SFXEmitter::smRenderColorRangeSphere( 200, 0, 0, 90 );
  76. //-----------------------------------------------------------------------------
  77. SFXEmitter::SFXEmitter()
  78. : SceneObject(),
  79. mSource( NULL ),
  80. mUseTrackDescriptionOnly( false ),
  81. mPlayOnAdd( true )
  82. {
  83. mTypeMask |= MarkerObjectType;
  84. mNetFlags.set( Ghostable | ScopeAlways );
  85. mDescription.mIs3D = true;
  86. mDescription.mIsLooping = true;
  87. mDescription.mIsStreaming = false;
  88. mDescription.mFadeInTime = -1.f;
  89. mDescription.mFadeOutTime = -1.f;
  90. mLocalProfile.mFilename = StringTable->EmptyString();
  91. mLocalProfile._registerSignals();
  92. INIT_ASSET(Sound);
  93. mObjBox.minExtents.set( -1.f, -1.f, -1.f );
  94. mObjBox.maxExtents.set( 1.f, 1.f, 1.f );
  95. }
  96. //-----------------------------------------------------------------------------
  97. SFXEmitter::~SFXEmitter()
  98. {
  99. mLocalProfile._unregisterSignals();
  100. SFX_DELETE( mSource );
  101. }
  102. //-----------------------------------------------------------------------------
  103. void SFXEmitter::consoleInit()
  104. {
  105. Con::addVariable( "$SFXEmitter::renderEmitters", TypeBool, &smRenderEmitters,
  106. "Whether to render enhanced range feedback in the editor on all emitters regardless of selection state.\n"
  107. "@ingroup SFX\n");
  108. //TODO: not implemented ATM
  109. //Con::addVariable( "$SFXEmitter::renderPointSize", TypeF32, &smRenderPointSize );
  110. Con::addVariable( "$SFXEmitter::renderPointDistance", TypeF32, &smRenderPointDistance,
  111. "The distance between individual points in the sound emitter rendering in the editor as the points move from the emitter's center away to maxDistance.\n"
  112. "@ingroup SFX\n");
  113. Con::addVariable( "$SFXEmitter::renderRadialIncrements", TypeF32, &smRenderRadialIncrements,
  114. "The stepping (in degrees) for the radial sweep along the axis of the XY plane sweep for sound emitter rendering in the editor.\n"
  115. "@ingroup SFX\n");
  116. Con::addVariable( "$SFXEmitter::renderSweepIncrements", TypeF32, &smRenderSweepIncrements,
  117. "The stepping (in degrees) for the radial sweep on the XY plane for sound emitter rendering in the editor.\n"
  118. "@ingroup SFX\n");
  119. Con::addVariable( "$SFXEmitter::renderColorPlayingInRange", TypeColorI, &smRenderColorPlayingInRange,
  120. "The color with which to render a sound emitter's marker cube in the editor when the emitter's sound is playing and in range of the listener.\n"
  121. "@ingroup SFX\n" );
  122. Con::addVariable( "$SFXEmitter::renderColorPlayingOutOfRange", TypeColorI, &smRenderColorPlayingOutOfRange,
  123. "The color with which to render a sound emitter's marker cube in the editor when the emitter's sound is playing but out of the range of the listener.\n"
  124. "@ingroup SFX\n" );
  125. Con::addVariable( "$SFXEmitter::renderColorStoppedInRange", TypeColorI, &smRenderColorStoppedInRange,
  126. "The color with which to render a sound emitter's marker cube in the editor when the emitter's sound is not playing but the emitter is in range of the listener.\n"
  127. "@ingroup SFX\n" );
  128. Con::addVariable( "$SFXEmitter::renderColorStoppedOutOfRange", TypeColorI, &smRenderColorStoppedOutOfRange,
  129. "The color with which to render a sound emitter's marker cube in the editor when the emitter's sound is not playing and the emitter is out of range of the listener.\n"
  130. "@ingroup SFX\n" );
  131. Con::addVariable( "$SFXEmitter::renderColorInnerCone", TypeColorI, &smRenderColorInnerCone,
  132. "The color with which to render dots in the inner sound cone (Editor only).\n"
  133. "@ingroup SFX\n");
  134. Con::addVariable( "$SFXEmitter::renderColorOuterCone", TypeColorI, &smRenderColorOuterCone,
  135. "The color with which to render dots in the outer sound cone (Editor only).\n"
  136. "@ingroup SFX\n" );
  137. Con::addVariable( "$SFXEmitter::renderColorOutsideVolume", TypeColorI, &smRenderColorOutsideVolume,
  138. "The color with which to render dots outside of the outer sound cone (Editor only).\n"
  139. "@ingroup SFX\n" );
  140. Con::addVariable( "$SFXEmitter::renderColorRangeSphere", TypeColorI, &smRenderColorRangeSphere,
  141. "The color of the range sphere with which to render sound emitters in the editor.\n"
  142. "@ingroup SFX\n" );
  143. }
  144. //-----------------------------------------------------------------------------
  145. void SFXEmitter::initPersistFields()
  146. {
  147. docsURL;
  148. addGroup( "Media" );
  149. INITPERSISTFIELD_SOUNDASSET(Sound, SFXEmitter, "");
  150. /*addField("track", TypeSFXTrackName, Offset(mTrack, SFXEmitter),
  151. "The track which the emitter should play.\n"
  152. "@note If assigned, this field will take precedence over a #fileName that may also be assigned to the "
  153. "emitter." );
  154. addField( "fileName", TypeStringFilename, Offset( mLocalProfile.mFilename, SFXEmitter),
  155. "The sound file to play.\n"
  156. "Use @b either this property @b or #track. If both are assigned, #track takes precendence. The primary purpose of this "
  157. "field is to avoid the need for the user to define SFXTrack datablocks for all sounds used in a level." );*/
  158. endGroup( "Media");
  159. addGroup( "Sound" );
  160. addField( "playOnAdd", TypeBool, Offset( mPlayOnAdd, SFXEmitter ),
  161. "Whether playback of the emitter's sound should start as soon as the emitter object is added to the level.\n"
  162. "If this is true, the emitter will immediately start to play when the level is loaded." );
  163. addField( "useTrackDescriptionOnly", TypeBool, Offset( mUseTrackDescriptionOnly, SFXEmitter ),
  164. "If this is true, all fields except for #playOnAdd and #track are ignored on the emitter object.\n"
  165. "This is useful to prevent fields in the #track's description from being overridden by emitter fields." );
  166. addField( "isLooping", TypeBool, Offset( mDescription.mIsLooping, SFXEmitter ),
  167. "Whether to play #fileName in an infinite loop.\n"
  168. "If a #track is assigned, the value of this field is ignored.\n"
  169. "@see SFXDescription::isLooping" );
  170. addField( "isStreaming", TypeBool, Offset( mDescription.mIsStreaming, SFXEmitter ),
  171. "Whether to use streamed playback for #fileName.\n"
  172. "If a #track is assigned, the value of this field is ignored.\n"
  173. "@see SFXDescription::isStreaming\n\n"
  174. "@ref SFX_streaming" );
  175. addField( "sourceGroup", TypeSFXSourceName, Offset( mDescription.mSourceGroup, SFXEmitter ),
  176. "The SFXSource to which to assign the sound of this emitter as a child.\n"
  177. "@note This field is ignored if #useTrackDescriptionOnly is true.\n\n"
  178. "@see SFXDescription::sourceGroup" );
  179. addField( "volume", TypeF32, Offset( mDescription.mVolume, SFXEmitter ),
  180. "Volume level to apply to the sound.\n"
  181. "@note This field is ignored if #useTrackDescriptionOnly is true.\n\n"
  182. "@see SFXDescription::volume" );
  183. addField( "pitch", TypeF32, Offset( mDescription.mPitch, SFXEmitter ),
  184. "Pitch shift to apply to the sound. Default is 1 = play at normal speed.\n"
  185. "@note This field is ignored if #useTrackDescriptionOnly is true.\n\n"
  186. "@see SFXDescription::pitch" );
  187. addField( "fadeInTime", TypeF32, Offset( mDescription.mFadeInTime, SFXEmitter ),
  188. "Number of seconds to gradually fade in volume from zero when playback starts.\n"
  189. "@note This field is ignored if #useTrackDescriptionOnly is true.\n\n"
  190. "@see SFXDescription::fadeInTime" );
  191. addField( "fadeOutTime", TypeF32, Offset( mDescription.mFadeOutTime, SFXEmitter ),
  192. "Number of seconds to gradually fade out volume down to zero when playback is stopped or paused.\n"
  193. "@note This field is ignored if #useTrackDescriptionOnly is true.\n\n"
  194. "@see SFXDescription::fadeOutTime" );
  195. endGroup( "Sound");
  196. addGroup( "3D Sound" );
  197. addField( "is3D", TypeBool, Offset( mDescription.mIs3D, SFXEmitter ),
  198. "Whether to play #fileName as a positional (3D) sound or not.\n"
  199. "If a #track is assigned, the value of this field is ignored.\n\n"
  200. "@see SFXDescription::is3D" );
  201. addField( "referenceDistance", TypeF32, Offset( mDescription.mMinDistance, SFXEmitter ),
  202. "Distance at which to start volume attenuation of the 3D sound.\n"
  203. "@note This field is ignored if #useTrackDescriptionOnly is true.\n\n"
  204. "@see SFXDescription::referenceDistance" );
  205. addField( "maxDistance", TypeF32, Offset( mDescription.mMaxDistance, SFXEmitter ),
  206. "Distance at which to stop volume attenuation of the 3D sound.\n"
  207. "@note This field is ignored if #useTrackDescriptionOnly is true.\n\n"
  208. "@see SFXDescription::maxDistance" );
  209. addField( "scatterDistance", TypePoint3F, Offset( mDescription.mScatterDistance, SFXEmitter ),
  210. "Bounds on random offset to apply to initial 3D sound position.\n"
  211. "@note This field is ignored if #useTrackDescriptionOnly is true.\n\n"
  212. "@see SFXDescription::scatterDistance" );
  213. addField( "coneInsideAngle", TypeS32, Offset( mDescription.mConeInsideAngle, SFXEmitter ),
  214. "Angle of inner volume cone of 3D sound in degrees.\n"
  215. "@note This field is ignored if #useTrackDescriptionOnly is true.\n\n"
  216. "@see SFXDescription::coneInsideAngle" );
  217. addField( "coneOutsideAngle", TypeS32, Offset( mDescription.mConeOutsideAngle, SFXEmitter ),
  218. "Angle of outer volume cone of 3D sound in degrees\n"
  219. "@note This field is ignored if #useTrackDescriptionOnly is true.\n\n"
  220. "@see SFXDescription::coneOutsideAngle" );
  221. addField( "coneOutsideVolume", TypeF32, Offset( mDescription.mConeOutsideVolume, SFXEmitter ),
  222. "Volume scale factor of outside of outer volume 3D sound cone.\n"
  223. "@note This field is ignored if #useTrackDescriptionOnly is true.\n\n"
  224. "@see SFXDescription::coneOutsideVolume" );
  225. endGroup( "3D Sound" );
  226. Parent::initPersistFields();
  227. }
  228. //-----------------------------------------------------------------------------
  229. U32 SFXEmitter::packUpdate( NetConnection *con, U32 mask, BitStream *stream )
  230. {
  231. U32 retMask = Parent::packUpdate( con, mask, stream );
  232. if( stream->writeFlag( mask & InitialUpdateMask ) )
  233. {
  234. // If this is the initial update then all the source
  235. // values are dirty and must be transmitted.
  236. mask |= TransformUpdateMask;
  237. mDirty = AllDirtyMask;
  238. // Clear the source masks... they are not
  239. // used during an initial update!
  240. mask &= ~AllSourceMasks;
  241. }
  242. stream->writeFlag( mPlayOnAdd );
  243. // transform
  244. if( stream->writeFlag( mask & TransformUpdateMask ) )
  245. stream->writeAffineTransform( mObjToWorld );
  246. // track
  247. PACK_ASSET(con, Sound);
  248. //if (stream->writeFlag(mDirty.test(Track)))
  249. // sfxWrite( stream, mTrack );
  250. // filename
  251. //if( stream->writeFlag( mDirty.test( Filename ) ) )
  252. // stream->writeString( mLocalProfile.mFilename );
  253. // volume
  254. if( stream->writeFlag( mDirty.test( Volume ) ) )
  255. stream->write( mDescription.mVolume );
  256. // pitch
  257. if( stream->writeFlag( mDirty.test( Pitch ) ) )
  258. stream->write( mDescription.mPitch );
  259. // islooping
  260. if( stream->writeFlag( mDirty.test( IsLooping ) ) )
  261. stream->writeFlag( mDescription.mIsLooping );
  262. // isStreaming
  263. if( stream->writeFlag( mDirty.test( IsStreaming ) ) )
  264. stream->writeFlag( mDescription.mIsStreaming );
  265. // is3d
  266. if( stream->writeFlag( mDirty.test( Is3D ) ) )
  267. stream->writeFlag( mDescription.mIs3D );
  268. // minDistance
  269. if( stream->writeFlag( mDirty.test( MinDistance ) ) )
  270. stream->write( mDescription.mMinDistance );
  271. // maxdistance
  272. if( stream->writeFlag( mDirty.test( MaxDistance) ) )
  273. stream->write( mDescription.mMaxDistance );
  274. // coneinsideangle
  275. if( stream->writeFlag( mDirty.test( ConeInsideAngle ) ) )
  276. stream->write( mDescription.mConeInsideAngle );
  277. // coneoutsideangle
  278. if( stream->writeFlag( mDirty.test( ConeOutsideAngle ) ) )
  279. stream->write( mDescription.mConeOutsideAngle );
  280. // coneoutsidevolume
  281. if( stream->writeFlag( mDirty.test( ConeOutsideVolume ) ) )
  282. stream->write( mDescription.mConeOutsideVolume );
  283. // sourcegroup
  284. if( stream->writeFlag( mDirty.test( SourceGroup ) ) )
  285. sfxWrite( stream, mDescription.mSourceGroup );
  286. // fadein
  287. if( stream->writeFlag( mDirty.test( FadeInTime ) ) )
  288. stream->write( mDescription.mFadeInTime );
  289. // fadeout
  290. if( stream->writeFlag( mDirty.test( FadeOutTime ) ) )
  291. stream->write( mDescription.mFadeOutTime );
  292. // scatterdistance
  293. if( stream->writeFlag( mDirty.test( ScatterDistance ) ) )
  294. mathWrite( *stream, mDescription.mScatterDistance );
  295. mDirty.clear();
  296. stream->writeFlag( mUseTrackDescriptionOnly );
  297. // We should never have both source masks
  298. // enabled at the same time!
  299. AssertFatal( ( mask & AllSourceMasks ) != AllSourceMasks,
  300. "SFXEmitter::packUpdate() - Bad source mask!" );
  301. // Write the source playback state.
  302. stream->writeFlag( mask & SourcePlayMask );
  303. stream->writeFlag( mask & SourceStopMask );
  304. return retMask;
  305. }
  306. //-----------------------------------------------------------------------------
  307. bool SFXEmitter::_readDirtyFlag( BitStream* stream, U32 mask )
  308. {
  309. bool flag = stream->readFlag();
  310. if ( flag )
  311. mDirty.set( mask );
  312. return flag;
  313. }
  314. //-----------------------------------------------------------------------------
  315. void SFXEmitter::unpackUpdate( NetConnection *conn, BitStream *stream )
  316. {
  317. Parent::unpackUpdate( conn, stream );
  318. // initial update?
  319. bool initialUpdate = stream->readFlag();
  320. mPlayOnAdd = stream->readFlag();
  321. // transform
  322. if ( _readDirtyFlag( stream, Transform ) )
  323. {
  324. MatrixF mat;
  325. stream->readAffineTransform(&mat);
  326. Parent::setTransform(mat);
  327. }
  328. // track
  329. UNPACK_ASSET(conn, Sound);
  330. /*if (_readDirtyFlag(stream, Track))
  331. {
  332. String errorStr;
  333. if( !sfxReadAndResolve( stream, &mTrack, errorStr ) )
  334. Con::errorf( "%s", errorStr.c_str() );
  335. }
  336. // filename
  337. if ( _readDirtyFlag( stream, Filename ) )
  338. mLocalProfile.mFilename = stream->readSTString();*/
  339. // volume
  340. if ( _readDirtyFlag( stream, Volume ) )
  341. stream->read( &mDescription.mVolume );
  342. // pitch
  343. if( _readDirtyFlag( stream, Pitch ) )
  344. stream->read( &mDescription.mPitch );
  345. // islooping
  346. if ( _readDirtyFlag( stream, IsLooping ) )
  347. mDescription.mIsLooping = stream->readFlag();
  348. if( _readDirtyFlag( stream, IsStreaming ) )
  349. mDescription.mIsStreaming = stream->readFlag();
  350. // is3d
  351. if ( _readDirtyFlag( stream, Is3D ) )
  352. mDescription.mIs3D = stream->readFlag();
  353. // mindistance
  354. if ( _readDirtyFlag( stream, MinDistance ) )
  355. stream->read( &mDescription.mMinDistance );
  356. // maxdistance
  357. if ( _readDirtyFlag( stream, MaxDistance ) )
  358. {
  359. stream->read( &mDescription.mMaxDistance );
  360. mObjScale.set( mDescription.mMaxDistance, mDescription.mMaxDistance, mDescription.mMaxDistance );
  361. }
  362. // coneinsideangle
  363. if ( _readDirtyFlag( stream, ConeInsideAngle ) )
  364. stream->read( &mDescription.mConeInsideAngle );
  365. // coneoutsideangle
  366. if ( _readDirtyFlag( stream, ConeOutsideAngle ) )
  367. stream->read( &mDescription.mConeOutsideAngle );
  368. // coneoutsidevolume
  369. if ( _readDirtyFlag( stream, ConeOutsideVolume ) )
  370. stream->read( &mDescription.mConeOutsideVolume );
  371. // sourcegroup
  372. if ( _readDirtyFlag( stream, SourceGroup ) )
  373. {
  374. String errorStr;
  375. if( !sfxReadAndResolve( stream, &mDescription.mSourceGroup, errorStr ) )
  376. Con::errorf( "%s", errorStr.c_str() );
  377. }
  378. // fadein
  379. if ( _readDirtyFlag( stream, FadeInTime ) )
  380. stream->read( &mDescription.mFadeInTime );
  381. // fadeout
  382. if( _readDirtyFlag( stream, FadeOutTime ) )
  383. stream->read( &mDescription.mFadeOutTime );
  384. // scatterdistance
  385. if( _readDirtyFlag( stream, ScatterDistance ) )
  386. mathRead( *stream, &mDescription.mScatterDistance );
  387. mUseTrackDescriptionOnly = stream->readFlag();
  388. // update the emitter now?
  389. if ( !initialUpdate )
  390. _update();
  391. // Check the source playback masks.
  392. if ( stream->readFlag() ) // SourcePlayMask
  393. play();
  394. if ( stream->readFlag() ) // SourceStopMask
  395. stop();
  396. }
  397. //-----------------------------------------------------------------------------
  398. void SFXEmitter::onStaticModified( const char* slotName, const char* newValue )
  399. {
  400. // NOTE: The signature for this function is very
  401. // misleading... slotName is a StringTableEntry.
  402. // We don't check for changes on the client side.
  403. if ( isClientObject() )
  404. return;
  405. // Lookup and store the property names once here
  406. // and we can then just do pointer compares.
  407. static StringTableEntry slotPosition = StringTable->lookup( "position" );
  408. static StringTableEntry slotRotation = StringTable->lookup( "rotation" );
  409. static StringTableEntry slotScale = StringTable->lookup( "scale" );
  410. static StringTableEntry slotTrack = StringTable->lookup( "track" );
  411. static StringTableEntry slotFilename = StringTable->lookup( "fileName" );
  412. static StringTableEntry slotVolume = StringTable->lookup( "volume" );
  413. static StringTableEntry slotPitch = StringTable->lookup( "pitch" );
  414. static StringTableEntry slotIsLooping = StringTable->lookup( "isLooping" );
  415. static StringTableEntry slotIsStreaming= StringTable->lookup( "isStreaming" );
  416. static StringTableEntry slotIs3D = StringTable->lookup( "is3D" );
  417. static StringTableEntry slotRefDist = StringTable->lookup( "referenceDistance" );
  418. static StringTableEntry slotMaxDist = StringTable->lookup( "maxDistance" );
  419. static StringTableEntry slotConeInAng = StringTable->lookup( "coneInsideAngle" );
  420. static StringTableEntry slotConeOutAng = StringTable->lookup( "coneOutsideAngle" );
  421. static StringTableEntry slotConeOutVol = StringTable->lookup( "coneOutsideVolume" );
  422. static StringTableEntry slotFadeInTime = StringTable->lookup( "fadeInTime" );
  423. static StringTableEntry slotFadeOutTime= StringTable->lookup( "fadeOutTime" );
  424. static StringTableEntry slotScatterDistance = StringTable->lookup( "scatterDistance" );
  425. static StringTableEntry slotSourceGroup= StringTable->lookup( "sourceGroup" );
  426. static StringTableEntry slotUseTrackDescriptionOnly = StringTable->lookup( "useTrackDescriptionOnly" );
  427. // Set the dirty flags.
  428. mDirty.clear();
  429. if( slotName == slotPosition ||
  430. slotName == slotRotation ||
  431. slotName == slotScale )
  432. mDirty.set( Transform );
  433. else if( slotName == slotTrack )
  434. mDirty.set( Track );
  435. else if( slotName == slotFilename )
  436. mDirty.set( Filename );
  437. else if( slotName == slotVolume )
  438. mDirty.set( Volume );
  439. else if( slotName == slotPitch )
  440. mDirty.set( Pitch );
  441. else if( slotName == slotIsLooping )
  442. mDirty.set( IsLooping );
  443. else if( slotName == slotIsStreaming )
  444. mDirty.set( IsStreaming );
  445. else if( slotName == slotIs3D )
  446. mDirty.set( Is3D );
  447. else if( slotName == slotRefDist )
  448. mDirty.set( MinDistance );
  449. else if( slotName == slotMaxDist )
  450. mDirty.set( MaxDistance );
  451. else if( slotName == slotConeInAng )
  452. mDirty.set( ConeInsideAngle );
  453. else if( slotName == slotConeOutAng )
  454. mDirty.set( ConeOutsideAngle );
  455. else if( slotName == slotConeOutVol )
  456. mDirty.set( ConeOutsideVolume );
  457. else if( slotName == slotFadeInTime )
  458. mDirty.set( FadeInTime );
  459. else if( slotName == slotFadeOutTime )
  460. mDirty.set( FadeOutTime );
  461. else if( slotName == slotScatterDistance )
  462. mDirty.set( ScatterDistance );
  463. else if( slotName == slotSourceGroup )
  464. mDirty.set( SourceGroup );
  465. else if( slotName == slotUseTrackDescriptionOnly )
  466. mDirty.set( TrackOnly );
  467. if( mDirty )
  468. setMaskBits( DirtyUpdateMask );
  469. }
  470. //-----------------------------------------------------------------------------
  471. void SFXEmitter::inspectPostApply()
  472. {
  473. // Parent will call setScale so sync up scale with distance.
  474. F32 maxDistance = mDescription.mMaxDistance;
  475. if( mUseTrackDescriptionOnly && mSoundAsset )
  476. maxDistance = mSoundAsset->getSfxDescription()->mMaxDistance;
  477. mObjScale.set( maxDistance, maxDistance, maxDistance );
  478. Parent::inspectPostApply();
  479. }
  480. //-----------------------------------------------------------------------------
  481. bool SFXEmitter::onAdd()
  482. {
  483. if( !Parent::onAdd() )
  484. return false;
  485. if( isServerObject() )
  486. {
  487. // Validate the data we'll be passing across
  488. // the network to the client.
  489. mDescription.validate();
  490. // Read an old 'profile' field for backwards-compatibility.
  491. /*
  492. if(mSoundAsset.isNull() || !mSoundAsset->getSfxProfile())
  493. {
  494. static const char* sProfile = StringTable->insert( "profile" );
  495. const char* profileName = getDataField( sProfile, NULL );
  496. if( profileName && profileName[ 0 ] )
  497. {
  498. if( !Sim::findObject( profileName, mTrack ) )
  499. Con::errorf( "SFXEmitter::onAdd - No SFXTrack '%s' in SFXEmitter '%i' (%s)", profileName, getId(), getName() );
  500. else
  501. {
  502. // Remove the old 'profile' field.
  503. setDataField( sProfile, NULL, "" );
  504. }
  505. }
  506. }
  507. // Convert a legacy 'channel' field, if we have one.
  508. static const char* sChannel = StringTable->insert( "channel" );
  509. const char* channelValue = getDataField( sChannel, NULL );
  510. if( channelValue && channelValue[ 0 ] )
  511. {
  512. const char* group = Con::evaluatef( "return sfxOldChannelToGroup( %s );", channelValue );
  513. SFXSource* sourceGroup;
  514. if( !Sim::findObject( group, sourceGroup ) )
  515. Con::errorf( "SFXEmitter::onAdd - could not resolve channel '%s' to SFXSource", channelValue );
  516. else
  517. {
  518. static const char* sSourceGroup = StringTable->insert( "sourceGroup" );
  519. setDataField( sSourceGroup, NULL, sourceGroup->getIdString() );
  520. // Remove the old 'channel' field.
  521. setDataField( sChannel, NULL, "" );
  522. }
  523. }*/
  524. }
  525. else
  526. {
  527. _update();
  528. // Do we need to start playback?
  529. if( mPlayOnAdd && mSource )
  530. mSource->play();
  531. }
  532. // Setup the bounds.
  533. mObjScale.set( mDescription.mMaxDistance, mDescription.mMaxDistance, mDescription.mMaxDistance );
  534. resetWorldBox();
  535. addToScene();
  536. return true;
  537. }
  538. //-----------------------------------------------------------------------------
  539. void SFXEmitter::onRemove()
  540. {
  541. SFX_DELETE( mSource );
  542. removeFromScene();
  543. Parent::onRemove();
  544. }
  545. //-----------------------------------------------------------------------------
  546. void SFXEmitter::_update()
  547. {
  548. AssertFatal( isClientObject(), "SFXEmitter::_update() - This shouldn't happen on the server!" );
  549. // Store the playback status so we
  550. // we can restore it.
  551. SFXStatus prevState = mSource ? mSource->getStatus() : SFXStatusNull;
  552. if (mSoundAsset.notNull() )
  553. {
  554. //mLocalProfile = *mSoundAsset->getSfxProfile();
  555. mDescription = *mSoundAsset->getSfxDescription();
  556. }
  557. // Make sure all the settings are valid.
  558. mDescription.validate();
  559. const MatrixF& transform = getTransform();
  560. const VectorF& velocity = getVelocity();
  561. // Did we change the source?
  562. if( mDirty.test( Track | Filename | Is3D | IsLooping | IsStreaming | TrackOnly ) )
  563. {
  564. SFX_DELETE( mSource );
  565. // Do we have a track?
  566. if( mSoundAsset && mSoundAsset->getSfxProfile() )
  567. {
  568. mSource = SFX->createSource(mSoundAsset->getSfxProfile(), &transform, &velocity );
  569. if( !mSource )
  570. Con::errorf( "SFXEmitter::_update() - failed to create sound for track %i (%s)",
  571. mSoundAsset->getSfxProfile()->getId(), mSoundAsset->getSfxProfile()->getName() );
  572. // If we're supposed to play when the emitter is
  573. // added to the scene then also restart playback
  574. // when the profile changes.
  575. prevState = mPlayOnAdd ? SFXStatusPlaying : prevState;
  576. // Force an update of properties set on the local description.
  577. mDirty.set( AllDirtyMask );
  578. }
  579. // Else take the local profile
  580. else
  581. {
  582. // Clear the resource and buffer on profile
  583. // to force reload.
  584. mLocalProfile.mResource = NULL;
  585. mLocalProfile.mBuffer = NULL;
  586. if( mLocalProfile.mFilename != StringTable->EmptyString() )
  587. {
  588. mSource = SFX->createSource( &mLocalProfile, &transform, &velocity );
  589. if( !mSource )
  590. Con::errorf( "SFXEmitter::_update() - failed to create sound for: %s",
  591. mLocalProfile.mFilename );
  592. prevState = mPlayOnAdd ? SFXStatusPlaying : prevState;
  593. }
  594. }
  595. mDirty.clear( Track | Filename | Is3D | IsLooping | IsStreaming | TrackOnly );
  596. }
  597. // Cheat if the editor is open and the looping state
  598. // is toggled on a local profile sound. It makes the
  599. // editor feel responsive and that things are working.
  600. if( gEditingMission &&
  601. (mSoundAsset.isNull() || !mSoundAsset->getSfxProfile()) &&
  602. mPlayOnAdd &&
  603. mDirty.test( IsLooping ) )
  604. prevState = SFXStatusPlaying;
  605. bool useTrackDescriptionOnly = ( mUseTrackDescriptionOnly && mSoundAsset.notNull() && mSoundAsset->getSfxProfile());
  606. // The rest only applies if we have a source.
  607. if( mSource )
  608. {
  609. // Set the volume irrespective of the profile.
  610. if( mDirty.test( Volume ) && !useTrackDescriptionOnly )
  611. mSource->setVolume( mDescription.mVolume );
  612. if( mDirty.test( Pitch ) && !useTrackDescriptionOnly )
  613. mSource->setPitch( mDescription.mPitch );
  614. if( mDirty.test( FadeInTime | FadeOutTime ) && !useTrackDescriptionOnly )
  615. mSource->setFadeTimes( mDescription.mFadeInTime, mDescription.mFadeOutTime );
  616. if( mDirty.test( SourceGroup ) && mDescription.mSourceGroup && !useTrackDescriptionOnly )
  617. mDescription.mSourceGroup->addObject( mSource );
  618. // Skip these 3d only settings.
  619. if( mDescription.mIs3D )
  620. {
  621. if( mDirty.test( Transform ) )
  622. {
  623. mSource->setTransform( transform );
  624. mSource->setVelocity( velocity );
  625. }
  626. if( mDirty.test( MinDistance | MaxDistance ) && !useTrackDescriptionOnly )
  627. {
  628. mSource->setMinMaxDistance( mDescription.mMinDistance,
  629. mDescription.mMaxDistance );
  630. }
  631. if( mDirty.test( ConeInsideAngle | ConeOutsideAngle | ConeOutsideVolume ) && !useTrackDescriptionOnly )
  632. {
  633. mSource->setCone( F32( mDescription.mConeInsideAngle ),
  634. F32( mDescription.mConeOutsideAngle ),
  635. mDescription.mConeOutsideVolume );
  636. }
  637. mDirty.clear( Transform | MinDistance | MaxDistance | ConeInsideAngle | ConeOutsideAngle | ConeOutsideVolume );
  638. }
  639. // Restore the pre-update playback state.
  640. if( prevState == SFXStatusPlaying )
  641. mSource->play();
  642. mDirty.clear( Volume | Pitch | Transform | FadeInTime | FadeOutTime | SourceGroup );
  643. }
  644. }
  645. //-----------------------------------------------------------------------------
  646. void SFXEmitter::prepRenderImage( SceneRenderState* state )
  647. {
  648. // Only render in editor.
  649. if( !gEditingMission )
  650. return;
  651. ObjectRenderInst* ri = state->getRenderPass()->allocInst< ObjectRenderInst >();
  652. ri->renderDelegate.bind( this, &SFXEmitter::_renderObject );
  653. ri->type = RenderPassManager::RIT_Editor;
  654. ri->defaultKey = 0;
  655. ri->defaultKey2 = 0;
  656. state->getRenderPass()->addInst( ri );
  657. }
  658. //-----------------------------------------------------------------------------
  659. void SFXEmitter::_renderObject( ObjectRenderInst* ri, SceneRenderState* state, BaseMatInstance* overrideMat )
  660. {
  661. // Check to see if the emitter is in range and playing
  662. // and assign a proper color depending on this.
  663. ColorI color;
  664. if( _getPlaybackStatus() == SFXStatusPlaying )
  665. {
  666. if( isInRange() )
  667. color = smRenderColorPlayingInRange;
  668. else
  669. color = smRenderColorPlayingOutOfRange;
  670. }
  671. else
  672. {
  673. if( isInRange() )
  674. color = smRenderColorStoppedInRange;
  675. else
  676. color = smRenderColorStoppedOutOfRange;
  677. }
  678. // Draw the cube.
  679. GFXStateBlockDesc desc;
  680. desc.setZReadWrite( true, false );
  681. desc.setBlend( true );
  682. desc.setCullMode( GFXCullNone );
  683. GFXDrawUtil *drawer = GFX->getDrawUtil();
  684. drawer->drawCube( desc, Point3F( 0.5f, 0.5f, 0.5f ), getBoxCenter(), color );
  685. // Render visual feedback for 3D sounds.
  686. if( ( smRenderEmitters || isSelected() ) && is3D() )
  687. _render3DVisualFeedback();
  688. }
  689. //-----------------------------------------------------------------------------
  690. void SFXEmitter::_render3DVisualFeedback()
  691. {
  692. GFXTransformSaver saver;
  693. GFX->multWorld( getRenderTransform() );
  694. GFXStateBlockDesc desc;
  695. desc.setZReadWrite( true, false );
  696. desc.setBlend( true );
  697. desc.setCullMode( GFXCullNone );
  698. if( mRenderSB == NULL )
  699. mRenderSB = GFX->createStateBlock( desc );
  700. GFX->setStateBlock( mRenderSB );
  701. // Render the max range sphere.
  702. if( smRenderColorRangeSphere.alpha > 0 )
  703. GFX->getDrawUtil()->drawSphere( desc, mDescription.mMaxDistance, Point3F( 0.f, 0.f, 0.f ), smRenderColorRangeSphere );
  704. //TODO: some point size support in GFX would be nice
  705. // Prepare primitive list. Make sure we stay within limits.
  706. F32 radialIncrements = smRenderRadialIncrements;
  707. F32 sweepIncrements = smRenderSweepIncrements;
  708. F32 pointDistance = smRenderPointDistance;
  709. F32 numPoints;
  710. while( 1 )
  711. {
  712. numPoints = mCeil( 360.f / radialIncrements ) *
  713. mCeil( 360.f / sweepIncrements ) *
  714. ( mDescription.mMaxDistance / pointDistance );
  715. if( numPoints < 65536 )
  716. break;
  717. radialIncrements *= 1.1f;
  718. sweepIncrements *= 1.1f;
  719. pointDistance *= 1.5;
  720. }
  721. PrimBuild::begin( GFXPointList, numPoints );
  722. // Render inner cone.
  723. _renderCone(
  724. radialIncrements,
  725. sweepIncrements,
  726. pointDistance,
  727. mDescription.mConeInsideAngle, 0.f,
  728. mDescription.mVolume, mDescription.mVolume,
  729. smRenderColorInnerCone );
  730. // Outer Cone and Outside volume only get rendered if mConeOutsideVolume > 0
  731. if( mDescription.mConeOutsideVolume > 0.f )
  732. {
  733. const F32 outsideVolume = mDescription.mVolume * mDescription.mConeOutsideVolume;
  734. // Render outer cone.
  735. _renderCone(
  736. radialIncrements,
  737. sweepIncrements,
  738. pointDistance,
  739. mDescription.mConeOutsideAngle, mDescription.mConeInsideAngle,
  740. outsideVolume, mDescription.mVolume,
  741. smRenderColorOuterCone );
  742. // Render outside volume.
  743. _renderCone(
  744. radialIncrements,
  745. sweepIncrements,
  746. pointDistance,
  747. 360.f, mDescription.mConeOutsideAngle,
  748. outsideVolume, outsideVolume,
  749. smRenderColorOutsideVolume );
  750. }
  751. // Commit primitive list.
  752. PrimBuild::end();
  753. }
  754. //-----------------------------------------------------------------------------
  755. void SFXEmitter::_renderCone( F32 radialIncrements, F32 sweepIncrements,
  756. F32 pointDistance,
  757. F32 startAngle, F32 stopAngle,
  758. F32 startVolume, F32 stopVolume,
  759. const ColorI& color )
  760. {
  761. if( startAngle == stopAngle )
  762. return;
  763. const F32 startAngleRadians = mDegToRad( startAngle );
  764. const F32 stopAngleRadians = mDegToRad( stopAngle );
  765. const F32 radialIncrementsRadians = mDegToRad( radialIncrements );
  766. // Unit quaternions representing the start and end angle so we
  767. // can interpolate between the two without flipping.
  768. QuatF rotateZStart( EulerF( 0.f, 0.f, startAngleRadians / 2.f ) );
  769. QuatF rotateZEnd( EulerF( 0.f, 0.f, stopAngleRadians / 2.f ) );
  770. // Do an angular sweep on one side of our XY disc. Since we do a full 360 radial sweep
  771. // around Y for each angle, we only need to sweep over one side.
  772. const F32 increment = 1.f / ( ( ( startAngle / 2.f ) - ( stopAngle / 2.f ) ) / sweepIncrements );
  773. for( F32 t = 0.f; t < 1.0f; t += increment )
  774. {
  775. // Quaternion to rotate point into place on XY disc.
  776. QuatF rotateZ;
  777. rotateZ.interpolate( rotateZStart, rotateZEnd, t );
  778. // Quaternion to rotate one position around Y axis. Used for radial sweep.
  779. QuatF rotateYOne( EulerF( 0.f, radialIncrementsRadians, 0.f ) );
  780. // Do a radial sweep each step along the distance axis. For each step, volume is
  781. // the same for any point on the sweep circle.
  782. for( F32 y = pointDistance; y <= mDescription.mMaxDistance; y += pointDistance )
  783. {
  784. ColorI c = color;
  785. // Compute volume at current point. First off, find the interpolated volume
  786. // in the cone. Only for the outer cone will this actually result in
  787. // interpolation. For the remaining angles, the cone volume is constant.
  788. F32 volume = mLerp( startVolume, stopVolume, t );
  789. if( volume == 0.f )
  790. c.alpha = 0;
  791. else
  792. {
  793. // Apply distance attenuation.
  794. F32 attenuatedVolume = SFXDistanceAttenuation(
  795. SFX->getDistanceModel(),
  796. mDescription.mMinDistance,
  797. mDescription.mMaxDistance,
  798. y,
  799. volume,
  800. SFX->getRolloffFactor() ); //RDTODO
  801. // Fade alpha according to how much volume we
  802. // have left at the current point.
  803. c.alpha = F32( c.alpha ) * ( attenuatedVolume / 1.f );
  804. }
  805. PrimBuild::color( c );
  806. // Create points by doing a full 360 degree radial sweep around Y.
  807. Point3F p( 0.f, y, 0.f );
  808. rotateZ.mulP( p, &p );
  809. for( F32 radialAngle = 0.f; radialAngle < 360.f; radialAngle += radialIncrements )
  810. {
  811. PrimBuild::vertex3f( p.x, p.y, p.z );
  812. rotateYOne.mulP( p, &p );
  813. }
  814. }
  815. }
  816. }
  817. //-----------------------------------------------------------------------------
  818. void SFXEmitter::play()
  819. {
  820. if( mSource )
  821. mSource->play();
  822. else
  823. {
  824. // By clearing the playback masks first we
  825. // ensure the last playback command called
  826. // within a single tick is the one obeyed.
  827. clearMaskBits( AllSourceMasks );
  828. setMaskBits( SourcePlayMask );
  829. }
  830. }
  831. //-----------------------------------------------------------------------------
  832. void SFXEmitter::stop()
  833. {
  834. if ( mSource )
  835. mSource->stop();
  836. else
  837. {
  838. // By clearing the playback masks first we
  839. // ensure the last playback command called
  840. // within a single tick is the one obeyed.
  841. clearMaskBits( AllSourceMasks );
  842. setMaskBits( SourceStopMask );
  843. }
  844. }
  845. //-----------------------------------------------------------------------------
  846. SFXStatus SFXEmitter::_getPlaybackStatus() const
  847. {
  848. const SFXEmitter* emitter = this;
  849. // We only have a source playing on client objects, so if this is a server
  850. // object, we want to know the playback status on the local client connection's
  851. // version of this emitter.
  852. if( isServerObject() )
  853. {
  854. S32 index = NetConnection::getLocalClientConnection()->getGhostIndex( ( NetObject* ) this );
  855. if( index != -1 )
  856. emitter = dynamic_cast< SFXEmitter* >( NetConnection::getConnectionToServer()->resolveGhost( index ) );
  857. else
  858. emitter = NULL;
  859. }
  860. if( emitter && emitter->mSource )
  861. return emitter->mSource->getStatus();
  862. return SFXStatusNull;
  863. }
  864. //-----------------------------------------------------------------------------
  865. bool SFXEmitter::is3D() const
  866. {
  867. if( mSoundAsset.notNull() && mSoundAsset->getSfxProfile() != NULL )
  868. return mSoundAsset->getSfxProfile()->getDescription()->mIs3D;
  869. else
  870. return mDescription.mIs3D;
  871. }
  872. //-----------------------------------------------------------------------------
  873. bool SFXEmitter::isInRange() const
  874. {
  875. if( !mDescription.mIs3D )
  876. return false;
  877. const SFXListenerProperties& listener = SFX->getListener();
  878. const Point3F listenerPos = listener.getTransform().getPosition();
  879. const Point3F emitterPos = getPosition();
  880. const F32 dist = mDescription.mMaxDistance;
  881. return ( ( emitterPos - listenerPos ).len() <= dist );
  882. }
  883. //-----------------------------------------------------------------------------
  884. void SFXEmitter::setTransform( const MatrixF &mat )
  885. {
  886. // Set the transform directly from the
  887. // matrix created by inspector.
  888. Parent::setTransform( mat );
  889. setMaskBits( TransformUpdateMask );
  890. }
  891. //-----------------------------------------------------------------------------
  892. void SFXEmitter::setScale( const VectorF &scale )
  893. {
  894. F32 maxDistance;
  895. if( mUseTrackDescriptionOnly && mSoundAsset.notNull() && mSoundAsset->getSfxProfile())
  896. maxDistance = mSoundAsset->getSfxProfile()->getDescription()->mMaxDistance;
  897. else
  898. {
  899. // Use the average of the three coords.
  900. maxDistance = ( scale.x + scale.y + scale.z ) / 3.0f;
  901. maxDistance = getMax( maxDistance, mDescription.mMinDistance );
  902. mDescription.mMaxDistance = maxDistance;
  903. mDirty.set( MaxDistance );
  904. setMaskBits( DirtyUpdateMask );
  905. }
  906. Parent::setScale( VectorF( maxDistance, maxDistance, maxDistance ) );
  907. }
  908. //=============================================================================
  909. // Console Methods.
  910. //=============================================================================
  911. // MARK: ---- Console Methods ----
  912. //-----------------------------------------------------------------------------
  913. DefineEngineMethod( SFXEmitter, play, void, (),,
  914. "Manually start playback of the emitter's sound.\n"
  915. "If this is called on the server-side object, the play command will be related to all client-side ghosts.\n" )
  916. {
  917. object->play();
  918. }
  919. //-----------------------------------------------------------------------------
  920. DefineEngineMethod( SFXEmitter, stop, void, (),,
  921. "Manually stop playback of the emitter's sound.\n"
  922. "If this is called on the server-side object, the stop command will be related to all client-side ghosts.\n" )
  923. {
  924. object->stop();
  925. }
  926. //-----------------------------------------------------------------------------
  927. DefineEngineMethod( SFXEmitter, getSource, SFXSource*, (),,
  928. "Get the sound source object from the emitter.\n\n"
  929. "@return The sound source used by the emitter or null."
  930. "@note This method will return null when called on the server-side SFXEmitter object. Only client-side ghosts "
  931. "actually hold on to %SFXSources.\n\n" )
  932. {
  933. return object->getSource();
  934. }