sfxEmitter.cpp 46 KB

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