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.
This commit is contained in:
Luca Martinelli
2025-06-18 21:38:48 +02:00
committed by Hugo Locurcio
parent 1f012286cf
commit 86e1ce7bf7
6 changed files with 12 additions and 4 deletions
+1 -1
View File
@@ -53,7 +53,7 @@
</method>
</methods>
<members>
<member name="area_mask" type="int" setter="set_area_mask" getter="get_area_mask" default="1">
<member name="area_mask" type="int" setter="set_area_mask" getter="get_area_mask" default="0">
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.
</member>
<member name="attenuation" type="float" setter="set_attenuation" getter="get_attenuation" default="1.0">
+1 -1
View File
@@ -53,7 +53,7 @@
</method>
</methods>
<members>
<member name="area_mask" type="int" setter="set_area_mask" getter="get_area_mask" default="1">
<member name="area_mask" type="int" setter="set_area_mask" getter="get_area_mask" default="0">
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.
</member>
<member name="attenuation_filter_cutoff_hz" type="float" setter="set_attenuation_filter_cutoff_hz" getter="get_attenuation_filter_cutoff_hz" default="5000.0">
+4
View File
@@ -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
+1 -1
View File
@@ -68,7 +68,7 @@ private:
static void _listener_changed_cb(void *self) { reinterpret_cast<AudioStreamPlayer2D *>(self)->force_update_panning = true; }
uint32_t area_mask = 1;
uint32_t area_mask = 0;
float max_distance = 2000.0;
float attenuation = 1.0;
+4
View File
@@ -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<World3D> world_3d = get_world_3d();
ERR_FAIL_COND_V(world_3d.is_null(), nullptr);
+1 -1
View File
@@ -100,7 +100,7 @@ private:
#endif // PHYSICS_3D_DISABLED
Vector<AudioFrame> _update_panning();
uint32_t area_mask = 1;
uint32_t area_mask = 0;
AudioServer::PlaybackType playback_type = AudioServer::PlaybackType::PLAYBACK_TYPE_DEFAULT;