From 86e1ce7bf7f890ccfe41ffdb43a5216e4933a408 Mon Sep 17 00:00:00 2001 From: Luca Martinelli Date: Wed, 18 Jun 2025 21:38:48 +0200 Subject: [PATCH] Disable AudioStreamPlayer2D/3D area bus override by default to improve performance This reduces the number of physics checks performed by each AudioStreamPlayer2D/3D node in the scene. This is accomplished by setting the default area mask to 0 and exiting early when the area mask is to 0 to improve performance. Note that in existing projects, the value will only be set to 0 if it was previously set to the default value (1). The feature can be re-enabled by setting the area mask to 1 (or any other value) on each AudioStreamPlayer2D/3D node. --- doc/classes/AudioStreamPlayer2D.xml | 2 +- doc/classes/AudioStreamPlayer3D.xml | 2 +- scene/2d/audio_stream_player_2d.cpp | 4 ++++ scene/2d/audio_stream_player_2d.h | 2 +- scene/3d/audio_stream_player_3d.cpp | 4 ++++ scene/3d/audio_stream_player_3d.h | 2 +- 6 files changed, 12 insertions(+), 4 deletions(-) diff --git a/doc/classes/AudioStreamPlayer2D.xml b/doc/classes/AudioStreamPlayer2D.xml index 76e8ee022c..0437a5c20b 100644 --- a/doc/classes/AudioStreamPlayer2D.xml +++ b/doc/classes/AudioStreamPlayer2D.xml @@ -53,7 +53,7 @@ - + Determines which [Area2D] layers affect the sound for reverb and audio bus effects. Areas can be used to redirect [AudioStream]s so that they play in a certain audio bus. An example of how you might use this is making a "water" area so that sounds played in the water are redirected through an audio bus to make them sound like they are being played underwater. diff --git a/doc/classes/AudioStreamPlayer3D.xml b/doc/classes/AudioStreamPlayer3D.xml index 408c551068..68a5564f3c 100644 --- a/doc/classes/AudioStreamPlayer3D.xml +++ b/doc/classes/AudioStreamPlayer3D.xml @@ -53,7 +53,7 @@ - + Determines which [Area3D] layers affect the sound for reverb and audio bus effects. Areas can be used to redirect [AudioStream]s so that they play in a certain audio bus. An example of how you might use this is making a "water" area so that sounds played in the water are redirected through an audio bus to make them sound like they are being played underwater. diff --git a/scene/2d/audio_stream_player_2d.cpp b/scene/2d/audio_stream_player_2d.cpp index 429805f277..0e98231dd4 100644 --- a/scene/2d/audio_stream_player_2d.cpp +++ b/scene/2d/audio_stream_player_2d.cpp @@ -82,6 +82,10 @@ void AudioStreamPlayer2D::_notification(int p_what) { // Interacts with PhysicsServer2D, so can only be called during _physics_process. StringName AudioStreamPlayer2D::_get_actual_bus() { #ifndef PHYSICS_2D_DISABLED + if (area_mask == 0) { + return internal->bus; + } + Vector2 global_pos = get_global_position(); //check if any area is diverting sound into a bus diff --git a/scene/2d/audio_stream_player_2d.h b/scene/2d/audio_stream_player_2d.h index b3e996922a..c54e1b59eb 100644 --- a/scene/2d/audio_stream_player_2d.h +++ b/scene/2d/audio_stream_player_2d.h @@ -68,7 +68,7 @@ private: static void _listener_changed_cb(void *self) { reinterpret_cast(self)->force_update_panning = true; } - uint32_t area_mask = 1; + uint32_t area_mask = 0; float max_distance = 2000.0; float attenuation = 1.0; diff --git a/scene/3d/audio_stream_player_3d.cpp b/scene/3d/audio_stream_player_3d.cpp index 0fcbe80d44..38a366f6e7 100644 --- a/scene/3d/audio_stream_player_3d.cpp +++ b/scene/3d/audio_stream_player_3d.cpp @@ -306,6 +306,10 @@ void AudioStreamPlayer3D::_notification(int p_what) { #ifndef PHYSICS_3D_DISABLED // Interacts with PhysicsServer3D, so can only be called during _physics_process Area3D *AudioStreamPlayer3D::_get_overriding_area() { + if (area_mask == 0) { + return nullptr; + } + //check if any area is diverting sound into a bus Ref world_3d = get_world_3d(); ERR_FAIL_COND_V(world_3d.is_null(), nullptr); diff --git a/scene/3d/audio_stream_player_3d.h b/scene/3d/audio_stream_player_3d.h index 9dd97a7a93..13435ea4b0 100644 --- a/scene/3d/audio_stream_player_3d.h +++ b/scene/3d/audio_stream_player_3d.h @@ -100,7 +100,7 @@ private: #endif // PHYSICS_3D_DISABLED Vector _update_panning(); - uint32_t area_mask = 1; + uint32_t area_mask = 0; AudioServer::PlaybackType playback_type = AudioServer::PlaybackType::PLAYBACK_TYPE_DEFAULT;