feat: material-aware sound occlusion + early reflections for localization
New Acoustics system (globals/acoustics.gd): - Occlusion: rays from listener to source collect up to 3 occluding bodies; each surface's material adds transmission loss and lowers the lowpass cutoff. A gunshot through wood stays warm (-8dB, 1.4kHz), brick muffles hard (-15dB, 600Hz), concrete harder (-18dB, 450Hz), metal keeps more highs (-11dB, 1kHz), glass barely muffles (-5dB, 2.6kHz). Applied via each AudioStreamPlayer3D's attenuation filter. - Early reflections: loud sounds (gunfire, explosions) probe 8 directions for nearby walls; each close surface plays a quiet, delayed (path/343ms), material-filtered copy from the mirror point — shots audibly slap back off structures so players can triangulate. Sub-30ms echoes are skipped (they perceptually merge with the direct sound). - Surface classification: builder-set acoustic_material meta -> node-name keywords -> metallic-material inference -> concrete default; cached. Integration: - AudioManager.play_3d applies occlusion at fire time and re-filters all playing positional sounds every 0.12s (walking behind a wall mid-sound audibly muffles it); flight loops opt in via "acoustic_occluded" group - Remote players' gunshots were entirely SILENT — rpc_play_fire_effects now plays the right weapon's shot at the shooter's position, occluded and reflected like everything else - Remote players' footsteps were also silent — now positional + occluded, cadence scaled to their speed - Test level tagged: brick arena walls, wooden platforms/obstacles, metal wall-run corridor/rails/dash platforms; dust2 sandstone reads as brick Tests: debug/acoustics_test.gd (12 checks: per-material loss ordering, multi-wall stacking, reflection delay/filter, name classification) — all pass; movement 11/11; smoke 0 failures. Co-Authored-By: Claude Fable 5 <[email protected]>
This commit is contained in:
co-authored by
Claude Fable 5
parent
44460b52ae
commit
a51595e3b9
@@ -40,6 +40,20 @@ const BUS_HINTS := {
|
||||
"reload": "Weapons",
|
||||
}
|
||||
|
||||
## weapon_name (as broadcast in fire RPCs) -> fire sound id.
|
||||
const WEAPON_SOUND_IDS := {
|
||||
"AK-47": "ak47_fire", "M4": "m4_fire", "MP7": "mp7_fire",
|
||||
"DMR": "dmr_fire", "AWP": "awp_fire",
|
||||
"Double Barrel Shotgun": "shotgun_fire", "Nail Gun": "nailgun_fire",
|
||||
"Plasma Gun": "plasma_fire", "Rocket Launcher": "rocket_fire",
|
||||
"Rocket Swarm": "swarm_fire", "Mortar": "mortar_fire",
|
||||
"Knife": "knife_swing",
|
||||
}
|
||||
|
||||
## Sounds loud enough to slap back off nearby walls (early reflections).
|
||||
const OCCLUSION_TICK := 0.12
|
||||
var _occlusion_accum := 0.0
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
_setup_buses()
|
||||
@@ -48,6 +62,33 @@ func _ready() -> void:
|
||||
print("AudioManager: %d sounds registered" % _library.size())
|
||||
|
||||
|
||||
func _physics_process(delta: float) -> void:
|
||||
# Re-filter playing positional sounds as the listener/source move so
|
||||
# walking behind a wall audibly muffles loops and long tails too.
|
||||
_occlusion_accum += delta
|
||||
if _occlusion_accum < OCCLUSION_TICK:
|
||||
return
|
||||
_occlusion_accum = 0.0
|
||||
var cam := get_viewport().get_camera_3d()
|
||||
if cam == null:
|
||||
return
|
||||
var listener := cam.global_position
|
||||
for p in _pool_3d:
|
||||
if p.playing and not p.get_meta("is_reflection", false):
|
||||
Acoustics.apply_occlusion(p, Acoustics.occlusion(
|
||||
cam.get_world_3d(), listener, p.global_position))
|
||||
# External looping players (projectile flight sounds, etc.) opt in by
|
||||
# joining this group.
|
||||
for node in get_tree().get_nodes_in_group("acoustic_occluded"):
|
||||
if node is AudioStreamPlayer3D and node.playing and node.is_inside_tree():
|
||||
Acoustics.apply_occlusion(node, Acoustics.occlusion(
|
||||
cam.get_world_3d(), listener, node.global_position))
|
||||
|
||||
|
||||
func weapon_sound_id(weapon_name: String) -> String:
|
||||
return WEAPON_SOUND_IDS.get(weapon_name, "")
|
||||
|
||||
|
||||
# ── Bus layout ────────────────────────────────────────────────────────────────
|
||||
|
||||
func _setup_buses() -> void:
|
||||
@@ -156,7 +197,9 @@ func _build_pools() -> void:
|
||||
_pool_2d.append(p)
|
||||
|
||||
|
||||
## Play a positional sound. Safe to call every frame — players are pooled.
|
||||
## Play a positional sound with material-aware occlusion; loud sounds also
|
||||
## spawn early reflections off nearby surfaces so they can be located.
|
||||
## Safe to call every frame — players are pooled.
|
||||
func play_3d(id: String, global_pos: Vector3, volume_db_offset: float = 0.0,
|
||||
pitch_scale: float = 1.0) -> AudioStreamPlayer3D:
|
||||
var entry = _library.get(id)
|
||||
@@ -169,10 +212,43 @@ func play_3d(id: String, global_pos: Vector3, volume_db_offset: float = 0.0,
|
||||
player.volume_db = entry["volume_db"] + volume_db_offset
|
||||
player.pitch_scale = pitch_scale * randf_range(1.0 - entry["pitch_var"], 1.0 + entry["pitch_var"])
|
||||
player.global_position = global_pos
|
||||
player.set_meta("is_reflection", false)
|
||||
var cam := get_viewport().get_camera_3d()
|
||||
if cam:
|
||||
var occ := Acoustics.occlusion(cam.get_world_3d(), cam.global_position, global_pos)
|
||||
Acoustics.apply_occlusion(player, occ)
|
||||
if _is_loud(id):
|
||||
_spawn_reflections(player.stream, global_pos, cam, entry, volume_db_offset)
|
||||
else:
|
||||
Acoustics.apply_occlusion(player, { "db": 0.0, "cutoff": Acoustics.OPEN_CUTOFF, "surfaces": 0 })
|
||||
player.play()
|
||||
return player
|
||||
|
||||
|
||||
func _is_loud(id: String) -> bool:
|
||||
return id.findn("fire") != -1 or id.findn("explosion") != -1
|
||||
|
||||
|
||||
## Delayed, filtered copies of a loud sound from nearby wall mirror points.
|
||||
func _spawn_reflections(stream: AudioStream, source: Vector3, cam: Camera3D,
|
||||
entry: Dictionary, volume_db_offset: float) -> void:
|
||||
var refls := Acoustics.reflections(cam.get_world_3d(), source, cam.global_position)
|
||||
for r in refls:
|
||||
get_tree().create_timer(r.delay).timeout.connect(func():
|
||||
var p := _pool_3d[_next_3d]
|
||||
_next_3d = (_next_3d + 1) % POOL_SIZE_3D
|
||||
p.stream = stream
|
||||
p.bus = entry["bus"]
|
||||
p.volume_db = entry["volume_db"] + volume_db_offset + r.db
|
||||
p.pitch_scale = randf_range(0.97, 1.0)
|
||||
p.global_position = r.point
|
||||
p.set_meta("is_reflection", true)
|
||||
p.attenuation_filter_cutoff_hz = r.cutoff
|
||||
p.attenuation_filter_db = Acoustics.DEFAULT_FILTER_DB
|
||||
p.play()
|
||||
)
|
||||
|
||||
|
||||
## Play a non-positional sound (UI clicks, local confirms).
|
||||
func play_ui(id: String, volume_db_offset: float = 0.0) -> AudioStreamPlayer:
|
||||
var entry = _library.get(id)
|
||||
|
||||
Reference in New Issue
Block a user