SoundSource3D.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. //
  2. // Copyright (c) 2008-2017 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/Audio.h"
  24. #include "../Audio/Sound.h"
  25. #include "../Audio/SoundListener.h"
  26. #include "../Audio/SoundSource3D.h"
  27. #include "../Core/Context.h"
  28. #include "../Graphics/DebugRenderer.h"
  29. #include "../Scene/Node.h"
  30. namespace Atomic
  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. static const Color INNER_COLOR(1.0f, 0.5f, 1.0f);
  38. static const Color OUTER_COLOR(1.0f, 0.0f, 1.0f);
  39. extern const char* AUDIO_CATEGORY;
  40. SoundSource3D::SoundSource3D(Context* context) :
  41. SoundSource(context),
  42. nearDistance_(DEFAULT_NEARDISTANCE),
  43. farDistance_(DEFAULT_FARDISTANCE),
  44. innerAngle_(DEFAULT_ANGLE),
  45. outerAngle_(DEFAULT_ANGLE),
  46. rolloffFactor_(DEFAULT_ROLLOFF)
  47. {
  48. // Start from zero volume until attenuation properly calculated
  49. attenuation_ = 0.0f;
  50. }
  51. void SoundSource3D::RegisterObject(Context* context)
  52. {
  53. context->RegisterFactory<SoundSource3D>(AUDIO_CATEGORY);
  54. ATOMIC_COPY_BASE_ATTRIBUTES(SoundSource);
  55. // Remove Attenuation and Panning as attribute as they are constantly being updated
  56. ATOMIC_REMOVE_ATTRIBUTE("Attenuation");
  57. ATOMIC_REMOVE_ATTRIBUTE("Panning");
  58. ATOMIC_ATTRIBUTE("Near Distance", float, nearDistance_, DEFAULT_NEARDISTANCE, AM_DEFAULT);
  59. ATOMIC_ATTRIBUTE("Far Distance", float, farDistance_, DEFAULT_FARDISTANCE, AM_DEFAULT);
  60. ATOMIC_ATTRIBUTE("Inner Angle", float, innerAngle_, DEFAULT_ANGLE, AM_DEFAULT);
  61. ATOMIC_ATTRIBUTE("Outer Angle", float, outerAngle_, DEFAULT_ANGLE, AM_DEFAULT);
  62. ATOMIC_ATTRIBUTE("Rolloff Factor", float, rolloffFactor_, DEFAULT_ROLLOFF, AM_DEFAULT);
  63. }
  64. void SoundSource3D::DrawDebugGeometry(DebugRenderer* debug, bool depthTest)
  65. {
  66. if (!debug || !node_ || !IsEnabledEffective())
  67. return;
  68. const Matrix3x4& worldTransform = node_->GetWorldTransform();
  69. Vector3 worldPosition = worldTransform.Translation();
  70. Quaternion worldRotation = worldTransform.Rotation();
  71. // Draw cones for directional sounds, or spheres for non-directional
  72. if (innerAngle_ < DEFAULT_ANGLE && outerAngle_ > 0.0f)
  73. {
  74. const Quaternion rotation = worldRotation * Quaternion(Vector3::UP, Vector3::FORWARD);
  75. debug->AddSphereSector(Sphere(worldPosition, nearDistance_), rotation, innerAngle_, false, INNER_COLOR, depthTest);
  76. debug->AddSphereSector(Sphere(worldPosition, nearDistance_), rotation, outerAngle_, false, OUTER_COLOR, depthTest);
  77. debug->AddSphereSector(Sphere(worldPosition, farDistance_), rotation, innerAngle_, true, INNER_COLOR, depthTest);
  78. debug->AddSphereSector(Sphere(worldPosition, farDistance_), rotation, outerAngle_, true, OUTER_COLOR, depthTest);
  79. }
  80. else
  81. {
  82. debug->AddSphere(Sphere(worldPosition, nearDistance_), INNER_COLOR, depthTest);
  83. debug->AddSphere(Sphere(worldPosition, farDistance_), OUTER_COLOR, depthTest);
  84. }
  85. }
  86. void SoundSource3D::Update(float timeStep)
  87. {
  88. CalculateAttenuation();
  89. SoundSource::Update(timeStep);
  90. }
  91. void SoundSource3D::SetDistanceAttenuation(float nearDistance, float farDistance, float rolloffFactor)
  92. {
  93. nearDistance_ = Max(nearDistance, 0.0f);
  94. farDistance_ = Max(farDistance, 0.0f);
  95. rolloffFactor_ = Max(rolloffFactor, MIN_ROLLOFF);
  96. MarkNetworkUpdate();
  97. }
  98. void SoundSource3D::SetAngleAttenuation(float innerAngle, float outerAngle)
  99. {
  100. innerAngle_ = Clamp(innerAngle, 0.0f, DEFAULT_ANGLE);
  101. outerAngle_ = Clamp(outerAngle, 0.0f, DEFAULT_ANGLE);
  102. MarkNetworkUpdate();
  103. }
  104. void SoundSource3D::SetFarDistance(float distance)
  105. {
  106. farDistance_ = Max(distance, 0.0f);
  107. MarkNetworkUpdate();
  108. }
  109. void SoundSource3D::SetNearDistance(float distance)
  110. {
  111. nearDistance_ = Max(distance, 0.0f);
  112. MarkNetworkUpdate();
  113. }
  114. void SoundSource3D::SetInnerAngle(float angle)
  115. {
  116. innerAngle_ = Clamp(angle, 0.0f, DEFAULT_ANGLE);
  117. MarkNetworkUpdate();
  118. }
  119. void SoundSource3D::SetOuterAngle(float angle)
  120. {
  121. outerAngle_ = Clamp(angle, 0.0f, DEFAULT_ANGLE);
  122. MarkNetworkUpdate();
  123. }
  124. void SoundSource3D::SetRolloffFactor(float factor)
  125. {
  126. rolloffFactor_ = Max(factor, MIN_ROLLOFF);
  127. MarkNetworkUpdate();
  128. }
  129. void SoundSource3D::CalculateAttenuation()
  130. {
  131. if (!audio_)
  132. return;
  133. float interval = farDistance_ - nearDistance_;
  134. if (node_)
  135. {
  136. SoundListener* listener = audio_->GetListener();
  137. // Listener must either be sceneless or in the same scene, else attenuate sound to silence
  138. if (listener && listener->IsEnabledEffective() && (!listener->GetScene() || listener->GetScene() == GetScene()))
  139. {
  140. Node* listenerNode = listener->GetNode();
  141. Vector3 relativePos
  142. (listenerNode->GetWorldRotation().Inverse() * (node_->GetWorldPosition() - listenerNode->GetWorldPosition()));
  143. float distance = relativePos.Length();
  144. // Distance attenuation
  145. if (interval > 0.0f)
  146. attenuation_ = powf(1.0f - Clamp(distance - nearDistance_, 0.0f, interval) / interval, rolloffFactor_);
  147. else
  148. attenuation_ = distance <= nearDistance_ ? 1.0f : 0.0f;
  149. // Panning
  150. panning_ = relativePos.Normalized().x_;
  151. // Angle attenuation
  152. if (innerAngle_ < DEFAULT_ANGLE && outerAngle_ > 0.0f)
  153. {
  154. Vector3 listenerRelativePos
  155. (node_->GetWorldRotation().Inverse() * (listenerNode->GetWorldPosition() - node_->GetWorldPosition()));
  156. float listenerDot = Vector3::FORWARD.DotProduct(listenerRelativePos.Normalized());
  157. float listenerAngle = acosf(listenerDot) * M_RADTODEG * 2.0f;
  158. float angleInterval = Max(outerAngle_ - innerAngle_, 0.0f);
  159. float angleAttenuation = 1.0f;
  160. if (angleInterval > 0.0f)
  161. {
  162. if (listenerAngle > innerAngle_)
  163. {
  164. angleAttenuation = powf(1.0f - Clamp(listenerAngle - innerAngle_, 0.0f, angleInterval) / angleInterval,
  165. rolloffFactor_);
  166. }
  167. }
  168. else
  169. angleAttenuation = listenerAngle <= innerAngle_ ? 1.0f : 0.0f;
  170. attenuation_ *= angleAttenuation;
  171. }
  172. }
  173. else
  174. attenuation_ = 0.0f;
  175. }
  176. else
  177. attenuation_ = 0.0f;
  178. }
  179. }