Skip to content

Commit d2a5153

Browse files
committed
Merge pull request godotengine#96677 from Wierdox/fix_audio_stream_player_3d_still_processing_when_out_of_range
Fix AudioStreamPlayer3D still processing when out of range
2 parents 7e62565 + a3158d8 commit d2a5153

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

scene/3d/audio_stream_player_3d.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,10 +401,19 @@ Vector<AudioFrame> AudioStreamPlayer3D::_update_panning() {
401401
if (area && area->is_using_reverb_bus() && area->get_reverb_uniformity() > 0) {
402402
total_max = MAX(total_max, listener_area_pos.length());
403403
}
404-
if (total_max > max_distance) {
404+
if (dist > total_max || total_max > max_distance) {
405+
if (!was_further_than_max_distance_last_frame) {
406+
HashMap<StringName, Vector<AudioFrame>> bus_volumes;
407+
for (Ref<AudioStreamPlayback> &playback : internal->stream_playbacks) {
408+
// So the player gets muted and mostly stops mixing when out of range.
409+
AudioServer::get_singleton()->set_playback_bus_volumes_linear(playback, bus_volumes);
410+
}
411+
was_further_than_max_distance_last_frame = true; // Cache so we don't set the volume over and over.
412+
}
405413
continue; //can't hear this sound in this listener
406414
}
407415
}
416+
was_further_than_max_distance_last_frame = false;
408417

409418
float multiplier = Math::db_to_linear(_get_attenuation_db(dist));
410419
if (max_distance > 0) {

scene/3d/audio_stream_player_3d.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ class AudioStreamPlayer3D : public Node3D {
105105
float linear_attenuation = 0;
106106

107107
float max_distance = 0.0;
108+
bool was_further_than_max_distance_last_frame = false;
108109

109110
Ref<VelocityTracker3D> velocity_tracker;
110111

0 commit comments

Comments
 (0)