SoundSource3D.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "Audio.h"
  25. #include "Context.h"
  26. #include "Node.h"
  27. #include "Sound.h"
  28. #include "SoundSource3D.h"
  29. #include "XMLElement.h"
  30. #include "DebugNew.h"
  31. static const float DEFAULT_NEARDISTANCE = 0.0f;
  32. static const float DEFAULT_FARDISTANCE = 100.0f;
  33. static const float DEFAULT_ROLLOFF = 2.0f;
  34. static const float MIN_ROLLOFF = 0.1f;
  35. OBJECTTYPESTATIC(SoundSource3D);
  36. SoundSource3D::SoundSource3D(Context* context) :
  37. SoundSource(context),
  38. nearDistance_(DEFAULT_NEARDISTANCE),
  39. farDistance_(DEFAULT_FARDISTANCE),
  40. rolloffFactor_(DEFAULT_ROLLOFF)
  41. {
  42. // Start from zero volume until attenuation properly calculated
  43. attenuation_ = 0.0f;
  44. }
  45. void SoundSource3D::RegisterObject(Context* context)
  46. {
  47. context->RegisterFactory<SoundSource3D>();
  48. COPY_BASE_ATTRIBUTES(SoundSource3D, SoundSource);
  49. ATTRIBUTE(SoundSource3D, VAR_FLOAT, "Near Distance", nearDistance_, DEFAULT_NEARDISTANCE, AM_DEFAULT);
  50. ATTRIBUTE(SoundSource3D, VAR_FLOAT, "Far Distance", farDistance_, DEFAULT_FARDISTANCE, AM_DEFAULT);
  51. ATTRIBUTE(SoundSource3D, VAR_FLOAT, "Rolloff Factor", rolloffFactor_, DEFAULT_ROLLOFF, AM_DEFAULT);
  52. }
  53. void SoundSource3D::Update(float timeStep)
  54. {
  55. CalculateAttenuation();
  56. SoundSource::Update(timeStep);
  57. }
  58. void SoundSource3D::SetDistanceAttenuation(float nearDistance, float farDistance, float rolloffFactor)
  59. {
  60. nearDistance_ = Max(nearDistance, 0.0f);
  61. farDistance_ = Max(farDistance, 0.0f);
  62. rolloffFactor_ = Max(rolloffFactor, MIN_ROLLOFF);
  63. MarkNetworkUpdate();
  64. }
  65. void SoundSource3D::SetFarDistance(float distance)
  66. {
  67. farDistance_ = Max(distance, 0.0f);
  68. MarkNetworkUpdate();
  69. }
  70. void SoundSource3D::SetNearDistance(float distance)
  71. {
  72. nearDistance_ = Max(distance, 0.0f);
  73. MarkNetworkUpdate();
  74. }
  75. void SoundSource3D::SetRolloffFactor(float factor)
  76. {
  77. rolloffFactor_ = Max(factor, MIN_ROLLOFF);
  78. MarkNetworkUpdate();
  79. }
  80. void SoundSource3D::CalculateAttenuation()
  81. {
  82. if (!audio_)
  83. return;
  84. float interval = farDistance_ - nearDistance_;
  85. if (interval > 0.0f && node_)
  86. {
  87. Vector3 relativePos(audio_->GetListenerRotation().Inverse() * (node_->GetWorldPosition() - audio_->GetListenerPosition()));
  88. float distance = Clamp(relativePos.Length() - nearDistance_, 0.0f, interval);
  89. float attenuation = powf(1.0f - distance / interval, rolloffFactor_);
  90. float panning = relativePos.Normalized().x_;
  91. attenuation_ = attenuation;
  92. panning_ = panning;
  93. }
  94. }