浏览代码

Fix AudioStreamPlayer3D still processing when out of range

Wierdox 1 年之前
父节点
当前提交
a3158d89bb
共有 2 个文件被更改,包括 11 次插入1 次删除
  1. 10 1
      scene/3d/audio_stream_player_3d.cpp
  2. 1 0
      scene/3d/audio_stream_player_3d.h

+ 10 - 1
scene/3d/audio_stream_player_3d.cpp

@@ -401,10 +401,19 @@ Vector<AudioFrame> 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<StringName, Vector<AudioFrame>> bus_volumes;
+					for (Ref<AudioStreamPlayback> &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) {

+ 1 - 0
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<VelocityTracker3D> velocity_tracker;