SoundSource3D.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. //
  2. // Copyright (c) 2008-2013 the Urho3D project.
  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 deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // 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 FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "Precompiled.h"
  23. #include "Audio.h"
  24. #include "Context.h"
  25. #include "Node.h"
  26. #include "Sound.h"
  27. #include "SoundListener.h"
  28. #include "SoundSource3D.h"
  29. #include "DebugNew.h"
  30. namespace Urho3D
  31. {
  32. static const float DEFAULT_NEARDISTANCE = 0.0f;
  33. static const float DEFAULT_FARDISTANCE = 100.0f;
  34. static const float DEFAULT_ROLLOFF = 2.0f;
  35. static const float MIN_ROLLOFF = 0.1f;
  36. extern const char* AUDIO_CATEGORY;
  37. SoundSource3D::SoundSource3D(Context* context) :
  38. SoundSource(context),
  39. nearDistance_(DEFAULT_NEARDISTANCE),
  40. farDistance_(DEFAULT_FARDISTANCE),
  41. rolloffFactor_(DEFAULT_ROLLOFF)
  42. {
  43. // Start from zero volume until attenuation properly calculated
  44. attenuation_ = 0.0f;
  45. }
  46. void SoundSource3D::RegisterObject(Context* context)
  47. {
  48. context->RegisterFactory<SoundSource3D>(AUDIO_CATEGORY);
  49. COPY_BASE_ATTRIBUTES(SoundSource3D, SoundSource);
  50. // Remove Attenuation and Panning as attribute as they are constantly being updated
  51. REMOVE_ATTRIBUTE(SoundSource3D, "Attenuation");
  52. REMOVE_ATTRIBUTE(SoundSource3D, "Panning");
  53. ATTRIBUTE(SoundSource3D, VAR_FLOAT, "Near Distance", nearDistance_, DEFAULT_NEARDISTANCE, AM_DEFAULT);
  54. ATTRIBUTE(SoundSource3D, VAR_FLOAT, "Far Distance", farDistance_, DEFAULT_FARDISTANCE, AM_DEFAULT);
  55. ATTRIBUTE(SoundSource3D, VAR_FLOAT, "Rolloff Factor", rolloffFactor_, DEFAULT_ROLLOFF, AM_DEFAULT);
  56. }
  57. void SoundSource3D::Update(float timeStep)
  58. {
  59. CalculateAttenuation();
  60. SoundSource::Update(timeStep);
  61. }
  62. void SoundSource3D::SetDistanceAttenuation(float nearDistance, float farDistance, float rolloffFactor)
  63. {
  64. nearDistance_ = Max(nearDistance, 0.0f);
  65. farDistance_ = Max(farDistance, 0.0f);
  66. rolloffFactor_ = Max(rolloffFactor, MIN_ROLLOFF);
  67. MarkNetworkUpdate();
  68. }
  69. void SoundSource3D::SetFarDistance(float distance)
  70. {
  71. farDistance_ = Max(distance, 0.0f);
  72. MarkNetworkUpdate();
  73. }
  74. void SoundSource3D::SetNearDistance(float distance)
  75. {
  76. nearDistance_ = Max(distance, 0.0f);
  77. MarkNetworkUpdate();
  78. }
  79. void SoundSource3D::SetRolloffFactor(float factor)
  80. {
  81. rolloffFactor_ = Max(factor, MIN_ROLLOFF);
  82. MarkNetworkUpdate();
  83. }
  84. void SoundSource3D::CalculateAttenuation()
  85. {
  86. if (!audio_)
  87. return;
  88. float interval = farDistance_ - nearDistance_;
  89. if (interval > 0.0f && node_)
  90. {
  91. SoundListener* listener = audio_->GetListener();
  92. // Listener must either be sceneless or in the same scene, else attenuate sound to silence
  93. if (listener && listener->IsEnabledEffective() && (!listener->GetScene() || listener->GetScene() == GetScene()))
  94. {
  95. Node* listenerNode = listener->GetNode();
  96. Vector3 relativePos(listenerNode->GetWorldRotation().Inverse() * (node_->GetWorldPosition() - listenerNode->GetWorldPosition()));
  97. float distance = Clamp(relativePos.Length() - nearDistance_, 0.0f, interval);
  98. float attenuation = powf(1.0f - distance / interval, rolloffFactor_);
  99. float panning = relativePos.Normalized().x_;
  100. attenuation_ = attenuation;
  101. panning_ = panning;
  102. }
  103. else
  104. attenuation_ = 0.0f;
  105. }
  106. else
  107. attenuation_ = 0.0f;
  108. }
  109. }