From a3158d89bb7cba513fc2aa47cdc23fe39d2a1ad3 Mon Sep 17 00:00:00 2001 From: Wierdox <104049283+Wierdox@users.noreply.github.com> Date: Sat, 7 Sep 2024 02:13:54 -0700 Subject: [PATCH] Fix AudioStreamPlayer3D still processing when out of range --- scene/3d/audio_stream_player_3d.cpp | 11 ++++++++++- scene/3d/audio_stream_player_3d.h | 1 + 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/scene/3d/audio_stream_player_3d.cpp b/scene/3d/audio_stream_player_3d.cpp index 591528b9156..98bee2115c2 100644 --- a/scene/3d/audio_stream_player_3d.cpp +++ b/scene/3d/audio_stream_player_3d.cpp @@ -401,10 +401,19 @@ Vector AudioStreamPlayer3D::_update_panning() { if (area && area->is_using_reverb_bus() && area->get_reverb_uniformity() > 0) { total_max = MAX(total_max, listener_area_pos.length()); } - if (total_max > max_distance) { + if (dist > total_max || total_max > max_distance) { + if (!was_further_than_max_distance_last_frame) { + HashMap> bus_volumes; + for (Ref &playback : internal->stream_playbacks) { + // So the player gets muted and mostly stops mixing when out of range. + AudioServer::get_singleton()->set_playback_bus_volumes_linear(playback, bus_volumes); + } + was_further_than_max_distance_last_frame = true; // Cache so we don't set the volume over and over. + } continue; //can't hear this sound in this listener } } + was_further_than_max_distance_last_frame = false; float multiplier = Math::db_to_linear(_get_attenuation_db(dist)); if (max_distance > 0) { diff --git a/scene/3d/audio_stream_player_3d.h b/scene/3d/audio_stream_player_3d.h index 72356faad7f..91104a06c74 100644 --- a/scene/3d/audio_stream_player_3d.h +++ b/scene/3d/audio_stream_player_3d.h @@ -105,6 +105,7 @@ private: float linear_attenuation = 0; float max_distance = 0.0; + bool was_further_than_max_distance_last_frame = false; Ref velocity_tracker;