Spatial 2D Audio: improved the reverb effect parameter calculation

This commit is contained in:
ChaoticByte 2024-10-05 10:33:07 +02:00
parent 3ec5a9edf7
commit 46f5b788a1
No known key found for this signature in database
5 changed files with 12 additions and 29 deletions

View file

@ -1,10 +1,6 @@
class_name SpatialAudioStreamPlayer2D
extends AudioStreamPlayer2D
# EXPORTED VARS
@export var radius: float = 1000.0
# DETERMINE REVERB AMOUNT
const RAYCAST_VECS: Array[Vector2] = [
@ -17,9 +13,9 @@ const RAYCAST_VECS: Array[Vector2] = [
Vector2(0, 1), # 6 - bottom
Vector2(-1, 1) # 7 - bottom left
]
const REVERB_MULT_PARALLEL = 2.0
const REVERB_MULT_OPP_45DEG = 1.0
const REVERB_MULT_90DEG = 0.5
const REVERB_MULT_PARALLEL = 1.0
const REVERB_MULT_OPP_45DEG = 0.5
const REVERB_MULT_90DEG = 0.25
const REVERB_PAIRS = [
# [<index in raycasts array>, <index in raycasts array>, <multiplier>]
# parallel walls
@ -46,21 +42,14 @@ const REVERB_PAIRS = [
[4, 6, REVERB_MULT_90DEG],
[5, 7, REVERB_MULT_90DEG]
]
var max_reverb_pairs_sum: float = ( # i'm shure I messed up this calculation
radius * ( # but it works
(4.0 * REVERB_MULT_PARALLEL)
+ (8.0 * REVERB_MULT_OPP_45DEG)
+ (8.0 * REVERB_MULT_90DEG)
)
)
const REVERB_PAIRS_SUM_DIVISOR = 2500.0 # i dunno
var raycasts: Array[RayCast2D] = []
func create_raycasts():
for v in RAYCAST_VECS:
var r = RayCast2D.new()
r.target_position = v.normalized() * radius
r.target_position = v.normalized() * max_distance
raycasts.append(r)
self.add_child(r)
@ -83,10 +72,9 @@ func determine_reverb_params() -> Array[float]:
for p in REVERB_PAIRS:
if collision_points[p[0]] != Vector2(INF, INF) and collision_points[p[1]] != Vector2(INF, INF):
reverb_pairs_sum += (
collision_points[p[0]].distance_to(collision_points[p[1]])
* p[2]
collision_points[p[0]].distance_to(collision_points[p[1]]) * p[2]
)
var room_size = reverb_pairs_sum / max_reverb_pairs_sum
var room_size = reverb_pairs_sum / REVERB_PAIRS_SUM_DIVISOR
var wetness = n_coll / 8.0
return [room_size, wetness]
@ -129,4 +117,5 @@ func _ready() -> void:
func _physics_process(_delta: float) -> void:
var reverb_params = determine_reverb_params()
print(reverb_params)
update_reverb(reverb_params[0], reverb_params[1])

View file

@ -2,7 +2,7 @@
[ext_resource type="Script" path="res://core/polygon/polygon.gd" id="1_ga37f"]
[sub_resource type="OccluderPolygon2D" id="OccluderPolygon2D_lp5j7"]
[sub_resource type="OccluderPolygon2D" id="OccluderPolygon2D_81uph"]
[node name="Polygon" type="CollisionPolygon2D"]
script = ExtResource("1_ga37f")
@ -11,4 +11,4 @@ script = ExtResource("1_ga37f")
[node name="LightOccluder2D" type="LightOccluder2D" parent="."]
editor_description = "For particle collisions"
occluder = SubResource("OccluderPolygon2D_lp5j7")
occluder = SubResource("OccluderPolygon2D_81uph")

View file

@ -15,5 +15,5 @@ polygon = PackedVector2Array(-104, 232, -104, 320, 1024, 320, 1024, 232)
polygon = PackedVector2Array(-208, 184, -144, 184, -144, -400, 1008, -400, 1008, 176, 1072, 176, 1072, -464, -208, -464)
[node name="Rain" parent="." instance=ExtResource("2_31352")]
position = Vector2(544, -396.25)
position = Vector2(433.72, -396.25)
amount_ratio = 1.0

View file

@ -52,6 +52,3 @@ func _physics_process(delta: float) -> void:
if collider is RigidBody2D:
var impulse = -collision.get_normal() * (velocity.length() / collider.mass) * rigidbody_impulse_mult
collider.apply_central_impulse(impulse)
func _ready() -> void:
$AudioListener2D.make_current()

View file

@ -39,11 +39,8 @@ target_position = Vector2(0, -8.1)
[node name="Polygon2D" type="Polygon2D" parent="."]
polygon = PackedVector2Array(-9, -9, -9, 9, 9, 9, 9, -9)
[node name="AudioListener2D" type="AudioListener2D" parent="."]
[node name="SpatialAudioStreamPlayer2D" type="AudioStreamPlayer2D" parent="."]
position = Vector2(0, -8)
stream = ExtResource("2_p4pcw")
volume_db = 10.0
volume_db = 15.0
autoplay = true
script = ExtResource("3_yaiah")