Spatial 2D sound w/ reverb, first attempt
This commit is contained in:
parent
7833a1c756
commit
3ec5a9edf7
17 changed files with 192 additions and 13 deletions
3
assets/sounds/CREDITS.txt
Normal file
3
assets/sounds/CREDITS.txt
Normal file
|
@ -0,0 +1,3 @@
|
|||
The following files are from Pixabay:
|
||||
|
||||
- clock-tick-76039.mp3
|
BIN
assets/sounds/clock-tick-76039.mp3
Normal file
BIN
assets/sounds/clock-tick-76039.mp3
Normal file
Binary file not shown.
19
assets/sounds/clock-tick-76039.mp3.import
Normal file
19
assets/sounds/clock-tick-76039.mp3.import
Normal file
|
@ -0,0 +1,19 @@
|
|||
[remap]
|
||||
|
||||
importer="mp3"
|
||||
type="AudioStreamMP3"
|
||||
uid="uid://cs6l2t3jtags8"
|
||||
path="res://.godot/imported/clock-tick-76039.mp3-7b65fd0a52c329ab567ab5301044e89b.mp3str"
|
||||
|
||||
[deps]
|
||||
|
||||
source_file="res://assets/sounds/clock-tick-76039.mp3"
|
||||
dest_files=["res://.godot/imported/clock-tick-76039.mp3-7b65fd0a52c329ab567ab5301044e89b.mp3str"]
|
||||
|
||||
[params]
|
||||
|
||||
loop=true
|
||||
loop_offset=0.0
|
||||
bpm=0.0
|
||||
beat_count=0
|
||||
bar_beats=4
|
132
core/audio/spatial_audio_player.gd
Normal file
132
core/audio/spatial_audio_player.gd
Normal file
|
@ -0,0 +1,132 @@
|
|||
class_name SpatialAudioStreamPlayer2D
|
||||
extends AudioStreamPlayer2D
|
||||
|
||||
# EXPORTED VARS
|
||||
|
||||
@export var radius: float = 1000.0
|
||||
|
||||
# DETERMINE REVERB AMOUNT
|
||||
|
||||
const RAYCAST_VECS: Array[Vector2] = [
|
||||
Vector2(-1, 0), # 0 - left
|
||||
Vector2(-1, -1), # 1 - top left
|
||||
Vector2(0, -1), # 2 - top
|
||||
Vector2(1, -1), # 3 - top right
|
||||
Vector2(1, 0), # 4 - right
|
||||
Vector2(1, 1), # 5 - bottom right
|
||||
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_PAIRS = [
|
||||
# [<index in raycasts array>, <index in raycasts array>, <multiplier>]
|
||||
# parallel walls
|
||||
[0, 4, REVERB_MULT_PARALLEL], # left : right
|
||||
[2, 6, REVERB_MULT_PARALLEL], # top : bottom
|
||||
[1, 5, REVERB_MULT_PARALLEL], # top left : bottom right
|
||||
[3, 7, REVERB_MULT_PARALLEL], # top right : bottom left
|
||||
# opposing walls at an 45deg angle
|
||||
[0, 3, REVERB_MULT_OPP_45DEG],
|
||||
[0, 5, REVERB_MULT_OPP_45DEG],
|
||||
[1, 4, REVERB_MULT_OPP_45DEG],
|
||||
[1, 6, REVERB_MULT_OPP_45DEG],
|
||||
[2, 5, REVERB_MULT_OPP_45DEG],
|
||||
[2, 7, REVERB_MULT_OPP_45DEG],
|
||||
[3, 6, REVERB_MULT_OPP_45DEG],
|
||||
[4, 7, REVERB_MULT_OPP_45DEG],
|
||||
# walls at an 90deg angle
|
||||
[0, 2, REVERB_MULT_90DEG],
|
||||
[0, 6, REVERB_MULT_90DEG],
|
||||
[1, 3, REVERB_MULT_90DEG],
|
||||
[1, 7, REVERB_MULT_90DEG],
|
||||
[2, 4, REVERB_MULT_90DEG],
|
||||
[3, 5, REVERB_MULT_90DEG],
|
||||
[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)
|
||||
)
|
||||
)
|
||||
|
||||
var raycasts: Array[RayCast2D] = []
|
||||
|
||||
func create_raycasts():
|
||||
for v in RAYCAST_VECS:
|
||||
var r = RayCast2D.new()
|
||||
r.target_position = v.normalized() * radius
|
||||
raycasts.append(r)
|
||||
self.add_child(r)
|
||||
|
||||
func determine_reverb_params() -> Array[float]:
|
||||
# returns [room_size, wetness]
|
||||
var n_coll = 0
|
||||
var collision_points: Array[Vector2] = []
|
||||
for r in raycasts:
|
||||
if r.is_colliding():
|
||||
var collider = r.get_collider()
|
||||
if collider is StaticBody2D or collider is RigidBody2D:
|
||||
n_coll += 1
|
||||
var collision_point = r.get_collision_point()
|
||||
collision_points.append(collision_point)
|
||||
else:
|
||||
collision_points.append(Vector2(INF, INF))
|
||||
else:
|
||||
collision_points.append(Vector2(INF, INF))
|
||||
var reverb_pairs_sum: float = 0.0
|
||||
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]
|
||||
)
|
||||
var room_size = reverb_pairs_sum / max_reverb_pairs_sum
|
||||
var wetness = n_coll / 8.0
|
||||
return [room_size, wetness]
|
||||
|
||||
# AUDIO EFFECTS
|
||||
|
||||
var audio_effect_reverb = AudioEffectReverb.new()
|
||||
|
||||
func update_reverb(room_size: float, wetness: float):
|
||||
audio_effect_reverb.room_size = room_size
|
||||
audio_effect_reverb.wet = wetness
|
||||
|
||||
# AUDIO BUS
|
||||
|
||||
const BUS_NAME_PREFIX = "Spatial2DBus#"
|
||||
const BUS_EFFECT_POS_REVERB = 0
|
||||
var bus_idx: int = -1
|
||||
var bus_name: String = ""
|
||||
|
||||
func create_audio_bus():
|
||||
bus_idx = AudioServer.bus_count
|
||||
bus_name = "BUS_NAME_PREFIX%s" % bus_idx
|
||||
AudioServer.add_bus(bus_idx)
|
||||
AudioServer.set_bus_name(bus_idx, bus_name)
|
||||
AudioServer.set_bus_send(bus_idx, bus)
|
||||
self.bus = bus_name
|
||||
|
||||
func create_audio_bus_effects():
|
||||
if bus_idx == -1:
|
||||
push_error("Audio bus isn't created yet, bus create_audio_effects is called!")
|
||||
return
|
||||
# reverb
|
||||
AudioServer.add_bus_effect(bus_idx, audio_effect_reverb, BUS_EFFECT_POS_REVERB)
|
||||
|
||||
#
|
||||
|
||||
func _ready() -> void:
|
||||
create_audio_bus()
|
||||
create_audio_bus_effects()
|
||||
create_raycasts()
|
||||
|
||||
func _physics_process(_delta: float) -> void:
|
||||
var reverb_params = determine_reverb_params()
|
||||
update_reverb(reverb_params[0], reverb_params[1])
|
7
core/audio/spatial_audio_player.tscn
Normal file
7
core/audio/spatial_audio_player.tscn
Normal file
|
@ -0,0 +1,7 @@
|
|||
[gd_scene load_steps=2 format=3 uid="uid://b0oj3olekssau"]
|
||||
|
||||
[ext_resource type="Script" path="res://core/audio/spatial_audio_player.gd" id="1_j22e2"]
|
||||
|
||||
[node name="SpatialAudioPlayer" type="AudioStreamPlayer2D"]
|
||||
playback_type = 1
|
||||
script = ExtResource("1_j22e2")
|
|
@ -1,8 +1,8 @@
|
|||
[gd_scene load_steps=3 format=3 uid="uid://cbynoofsjcl45"]
|
||||
|
||||
[ext_resource type="Script" path="res://core/polygon.gd" id="1_ga37f"]
|
||||
[ext_resource type="Script" path="res://core/polygon/polygon.gd" id="1_ga37f"]
|
||||
|
||||
[sub_resource type="OccluderPolygon2D" id="OccluderPolygon2D_dhpxk"]
|
||||
[sub_resource type="OccluderPolygon2D" id="OccluderPolygon2D_lp5j7"]
|
||||
|
||||
[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_dhpxk")
|
||||
occluder = SubResource("OccluderPolygon2D_lp5j7")
|
|
@ -1,6 +1,6 @@
|
|||
[gd_scene load_steps=3 format=3 uid="uid://c6w7lrydi43ts"]
|
||||
|
||||
[ext_resource type="PackedScene" uid="uid://cbynoofsjcl45" path="res://core/polygon.tscn" id="1_cup10"]
|
||||
[ext_resource type="PackedScene" uid="uid://cbynoofsjcl45" path="res://core/polygon/polygon.tscn" id="1_cup10"]
|
||||
[ext_resource type="Script" path="res://levels/intro.gd" id="1_pgj82"]
|
||||
|
||||
[node name="Intro" type="Node2D"]
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
[gd_scene load_steps=3 format=3 uid="uid://dqf665b540tfg"]
|
||||
|
||||
[ext_resource type="PackedScene" uid="uid://cbynoofsjcl45" path="res://core/polygon.tscn" id="1_xm2ft"]
|
||||
[ext_resource type="PackedScene" uid="uid://cbynoofsjcl45" path="res://core/polygon/polygon.tscn" id="1_xm2ft"]
|
||||
[ext_resource type="PackedScene" uid="uid://b8cjp6hiqycxe" path="res://core/rain.tscn" id="2_31352"]
|
||||
|
||||
[node name="Test" type="Node2D"]
|
||||
|
@ -11,6 +11,9 @@ position = Vector2(1680, 80)
|
|||
[node name="Polygon" parent="StaticBody2D" instance=ExtResource("1_xm2ft")]
|
||||
polygon = PackedVector2Array(-104, 232, -104, 320, 1024, 320, 1024, 232)
|
||||
|
||||
[node name="Polygon2" parent="StaticBody2D" instance=ExtResource("1_xm2ft")]
|
||||
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(1128, -400)
|
||||
position = Vector2(544, -396.25)
|
||||
amount_ratio = 1.0
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
[gd_scene load_steps=6 format=3 uid="uid://c13h0uf5fgx60"]
|
||||
|
||||
[ext_resource type="Script" path="res://main.gd" id="1_m407c"]
|
||||
[ext_resource type="Script" path="res://main/main.gd" id="1_m407c"]
|
||||
[ext_resource type="PackedScene" uid="uid://dxp3dru2lrv6h" path="res://core/shaders/outline/outline_canvas_layer.tscn" id="2_ta1pa"]
|
||||
[ext_resource type="PackedScene" uid="uid://jmy11lqcs4c" path="res://core/shaders/distortion/distortion_canvas_layer.tscn" id="3_emkei"]
|
||||
[ext_resource type="Script" path="res://core/text_viewport.gd" id="4_hxymt"]
|
||||
[ext_resource type="Script" path="res://main/text_viewport.gd" id="4_hxymt"]
|
||||
[ext_resource type="PackedScene" uid="uid://ebb4pfxklatj" path="res://player/player.tscn" id="5_0agpg"]
|
||||
|
||||
[node name="Main" type="Control"]
|
||||
|
@ -27,6 +27,7 @@ stretch = true
|
|||
[node name="MainViewport" type="SubViewport" parent="MainViewportContainer"]
|
||||
handle_input_locally = false
|
||||
canvas_item_default_texture_filter = 0
|
||||
audio_listener_enable_2d = true
|
||||
size = Vector2i(800, 450)
|
||||
render_target_update_mode = 4
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
[gd_scene load_steps=4 format=3 uid="uid://bqmpoix37kutp"]
|
||||
|
||||
[ext_resource type="PackedScene" uid="uid://cbynoofsjcl45" path="res://core/polygon.tscn" id="1_8p275"]
|
||||
[ext_resource type="PackedScene" uid="uid://cbynoofsjcl45" path="res://core/polygon/polygon.tscn" id="1_8p275"]
|
||||
[ext_resource type="Script" path="res://menu/menu.gd" id="1_g2w4y"]
|
||||
[ext_resource type="PackedScene" uid="uid://c40fli7qcma78" path="res://menu/slot/slot.tscn" id="3_rc4dm"]
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
[gd_scene load_steps=3 format=3 uid="uid://c40fli7qcma78"]
|
||||
|
||||
[ext_resource type="PackedScene" uid="uid://cbynoofsjcl45" path="res://core/polygon.tscn" id="1_2cb7u"]
|
||||
[ext_resource type="PackedScene" uid="uid://cbynoofsjcl45" path="res://core/polygon/polygon.tscn" id="1_2cb7u"]
|
||||
[ext_resource type="Script" path="res://menu/slot/slot.gd" id="1_3dgi0"]
|
||||
|
||||
[node name="Slot" type="Node2D"]
|
||||
|
|
|
@ -52,3 +52,6 @@ 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()
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
[gd_scene load_steps=4 format=3 uid="uid://ebb4pfxklatj"]
|
||||
[gd_scene load_steps=6 format=3 uid="uid://ebb4pfxklatj"]
|
||||
|
||||
[ext_resource type="Script" path="res://player/player.gd" id="1_fob34"]
|
||||
[ext_resource type="AudioStream" uid="uid://cs6l2t3jtags8" path="res://assets/sounds/clock-tick-76039.mp3" id="2_p4pcw"]
|
||||
[ext_resource type="Script" path="res://core/audio/spatial_audio_player.gd" id="3_yaiah"]
|
||||
|
||||
[sub_resource type="OccluderPolygon2D" id="OccluderPolygon2D_p33ik"]
|
||||
polygon = PackedVector2Array(-9, -9, -9, 9, 9, 9, 9, -9)
|
||||
|
@ -9,6 +11,7 @@ polygon = PackedVector2Array(-9, -9, -9, 9, 9, 9, 9, -9)
|
|||
size = Vector2(16, 16)
|
||||
|
||||
[node name="Player" type="CharacterBody2D"]
|
||||
collision_layer = 2
|
||||
floor_block_on_wall = false
|
||||
script = ExtResource("1_fob34")
|
||||
|
||||
|
@ -35,3 +38,12 @@ 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
|
||||
autoplay = true
|
||||
script = ExtResource("3_yaiah")
|
||||
|
|
|
@ -11,7 +11,7 @@ config_version=5
|
|||
[application]
|
||||
|
||||
config/name="rectangular"
|
||||
run/main_scene="res://main.tscn"
|
||||
run/main_scene="res://main/main.tscn"
|
||||
config/features=PackedStringArray("4.3", "Forward Plus")
|
||||
boot_splash/bg_color=Color(0, 0, 0, 1)
|
||||
config/icon="res://icon.svg"
|
||||
|
@ -30,7 +30,6 @@ settings/stdout/print_fps=true
|
|||
|
||||
window/size/viewport_width=800
|
||||
window/size/viewport_height=450
|
||||
window/size/mode=3
|
||||
window/stretch/mode="viewport"
|
||||
window/stretch/aspect="expand"
|
||||
window/stretch/scale_mode="integer"
|
||||
|
|
Reference in a new issue