SoundSource3D.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 DEFAULT_ANGLE = 360.0f;
  36. static const float MIN_ROLLOFF = 0.1f;
  37. extern const char* AUDIO_CATEGORY;
  38. SoundSource3D::SoundSource3D(Context* context) :
  39. SoundSource(context),
  40. nearDistance_(DEFAULT_NEARDISTANCE),
  41. farDistance_(DEFAULT_FARDISTANCE),
  42. outerAngle_(DEFAULT_ANGLE),
  43. innerAngle_(DEFAULT_ANGLE),
  44. rolloffFactor_(DEFAULT_ROLLOFF)
  45. {
  46. // Start from zero volume until attenuation properly calculated
  47. attenuation_ = 0.0f;
  48. }
  49. void SoundSource3D::RegisterObject(Context* context)
  50. {
  51. context->RegisterFactory<SoundSource3D>(AUDIO_CATEGORY);
  52. COPY_BASE_ATTRIBUTES(SoundSource3D, SoundSource);
  53. // Remove Attenuation and Panning as attribute as they are constantly being updated
  54. REMOVE_ATTRIBUTE(SoundSource3D, "Attenuation");
  55. REMOVE_ATTRIBUTE(SoundSource3D, "Panning");
  56. ATTRIBUTE(SoundSource3D, VAR_FLOAT, "Near Distance", nearDistance_, DEFAULT_NEARDISTANCE, AM_DEFAULT);
  57. ATTRIBUTE(SoundSource3D, VAR_FLOAT, "Far Distance", farDistance_, DEFAULT_FARDISTANCE, AM_DEFAULT);
  58. ATTRIBUTE(SoundSource3D, VAR_FLOAT, "Inner Angle", innerAngle_, DEFAULT_ANGLE, AM_DEFAULT);
  59. ATTRIBUTE(SoundSource3D, VAR_FLOAT, "Outer Angle", outerAngle_, DEFAULT_ANGLE, AM_DEFAULT);
  60. ATTRIBUTE(SoundSource3D, VAR_FLOAT, "Rolloff Factor", rolloffFactor_, DEFAULT_ROLLOFF, AM_DEFAULT);
  61. }
  62. void SoundSource3D::Update(float timeStep)
  63. {
  64. CalculateAttenuation();
  65. SoundSource::Update(timeStep);
  66. }
  67. void SoundSource3D::SetDistanceAttenuation(float nearDistance, float farDistance, float rolloffFactor)
  68. {
  69. nearDistance_ = Max(nearDistance, 0.0f);
  70. farDistance_ = Max(farDistance, 0.0f);
  71. rolloffFactor_ = Max(rolloffFactor, MIN_ROLLOFF);
  72. MarkNetworkUpdate();
  73. }
  74. void SoundSource3D::SetAngleAttenuation(float innerAngle, float outerAngle)
  75. {
  76. innerAngle_ = Clamp(innerAngle, 0.0f, DEFAULT_ANGLE);
  77. outerAngle_ = Clamp(outerAngle, 0.0f, DEFAULT_ANGLE);
  78. MarkNetworkUpdate();
  79. }
  80. void SoundSource3D::SetFarDistance(float distance)
  81. {
  82. farDistance_ = Max(distance, 0.0f);
  83. MarkNetworkUpdate();
  84. }
  85. void SoundSource3D::SetNearDistance(float distance)
  86. {
  87. nearDistance_ = Max(distance, 0.0f);
  88. MarkNetworkUpdate();
  89. }
  90. void SoundSource3D::SetInnerAngle(float angle)
  91. {
  92. innerAngle_ = Clamp(angle, 0.0f, DEFAULT_ANGLE);
  93. MarkNetworkUpdate();
  94. }
  95. void SoundSource3D::SetOuterAngle(float angle)
  96. {
  97. outerAngle_ = Clamp(angle, 0.0f, DEFAULT_ANGLE);
  98. MarkNetworkUpdate();
  99. }
  100. void SoundSource3D::SetRolloffFactor(float factor)
  101. {
  102. rolloffFactor_ = Max(factor, MIN_ROLLOFF);
  103. MarkNetworkUpdate();
  104. }
  105. void SoundSource3D::CalculateAttenuation()
  106. {
  107. if (!audio_)
  108. return;
  109. float interval = farDistance_ - nearDistance_;
  110. if (node_)
  111. {
  112. SoundListener* listener = audio_->GetListener();
  113. // Listener must either be sceneless or in the same scene, else attenuate sound to silence
  114. if (listener && listener->IsEnabledEffective() && (!listener->GetScene() || listener->GetScene() == GetScene()))
  115. {
  116. Node* listenerNode = listener->GetNode();
  117. Vector3 relativePos(listenerNode->GetWorldRotation().Inverse() * (node_->GetWorldPosition() - listenerNode->GetWorldPosition()));
  118. float distance = relativePos.Length();
  119. // Distance attenuation
  120. if (interval > 0.0f)
  121. attenuation_ = powf(1.0f - Clamp(distance - nearDistance_, 0.0f, interval) / interval, rolloffFactor_);
  122. else
  123. attenuation_ = distance <= nearDistance_ ? 1.0f : 0.0f;
  124. // Panning
  125. panning_ = relativePos.Normalized().x_;
  126. // Angle attenuation
  127. if (innerAngle_ < DEFAULT_ANGLE)
  128. {
  129. Vector3 listenerRelativePos(node_->GetWorldRotation().Inverse() * (listenerNode->GetWorldPosition() -
  130. node_->GetWorldPosition()));
  131. float listenerDot = Vector3::FORWARD.DotProduct(listenerRelativePos.Normalized());
  132. float listenerAngle = acosf(listenerDot) * M_RADTODEG * 2.0f;
  133. float angleInterval = Max(outerAngle_ - innerAngle_, 0.0f);
  134. float angleAttenuation = 1.0f;
  135. if (angleInterval > 0.0f)
  136. {
  137. if (listenerAngle > innerAngle_)
  138. {
  139. angleAttenuation = powf(1.0f - Clamp(listenerAngle - innerAngle_, 0.0f, angleInterval) / angleInterval,
  140. rolloffFactor_);
  141. }
  142. }
  143. else
  144. angleAttenuation = listenerAngle <= innerAngle_ ? 1.0f : 0.0f;
  145. attenuation_ *= angleAttenuation;
  146. }
  147. }
  148. else
  149. attenuation_ = 0.0f;
  150. }
  151. else
  152. attenuation_ = 0.0f;
  153. }
  154. }