Almost all concepts for this post and project are taken directly from the Tunic audio GDC talk by Kevin Regamey. https://youtu.be/sCIK78OHrIY
All credit to him and everyone else at Power Up Audio for their awesome work on Tunic. Go check out the game if you haven't already. If you like awesome sound design, hidden secrets, and chill electronic music you won't regret it.
In said GDC talk Kevin describes what I've taken to calling "screenspace spatialization"™️. In its simplest form it is a system for panning and fading out audio not based on 2D or 3D positioning relative to the listener (as is the norm for 2D and 3D events in FMOD, Wwise, etc) but instead based on the relationship between the emitter of a sound and its position on (or off) the screen. This is very nifty for isometric games specifically, where the camera angle and positioning can make using your typical 3D audio systems mismatch with the player's field of vision in awkward ways.
In Kevin's talk, this effect is achieved through five custom parameters. I'll walk through my small demo implementation of them and then briefly touch on the mysterious fifth parameter as it is a bit more complicated.
Video Demo
Parameters 1 and 2. "ss_x_position" & "ss_y_position"
Screen space x position and screen space y position. In the example from the Tunic audio talk these are normalized to between -1 and 1 on both axis with -1 being the leftmost or bottom of the screen and 1 being the top or rightmost of the screen, (0, 0) then would be the exact center of the screen.
// Updates ss_x_position, ss_y_position
private void updateScreenSpacePosition()
{
ss_pos = camera.WorldToScreenPoint(transform.position);
ss_x_position = 2.0f * (ss_pos.x/camera.pixelWidth - 0.5f);
ss_y_position = 2.0f * (ss_pos.y/camera.pixelHeight - 0.5f);
}
(graphic design is clearly not my passion)
ss_x_position will be your primary driver of stereo panning, with some relatively small gain curves for both x and y positions to accentuate sources in the center of the screen.
(Example curves for ss_x_position)
(Example curves for ss_y_position)
You can also use screenspace positions to drive filters to create the illusion of vertical depth, sort of a 2D HRTF of sorts.
Parameter 3. "offscreen_distance"
This parameter describes how far offscreen the audio source is. 0 if it is onscreen and >0 once it is offscreen. This parameter is slightly deceptive in its complexity at first glance as to accurately measure the distance from the closest screen edge point requires eight zones, each with its own slightly different distance calculation.
(My crappy MS Paint rendition of the situation)
Instead of explaining it in English it's probably more efficient to share a C# code snippet calculating the values.
This can be reduced to only a few lines of code without any conditionals through the use of some Math.Max and Min calls but this example is clearer about where the "zones" are. Consider finding the cleaner version your homework assignment.
In my implementation ss_x_position and ss_y_position are driving offscreen_distance. This version of the variable can be interpreted as something like "how many screen lengths away the source is".
// case 1 - to the left of X-border but not above or below Y-border
// closest point = leftmost X-border
if (ss_x_position < -1.0f && ss_y_position <= 1.0f && ss_y_position >= -1.0f)
{
offscreen_distance = -1.0f - ss_x_position;
}
// case 2 - To the right of X-border but not above or below Y-border
// closest point = righmost X-border
else if(ss_x_position > 1.0f && ss_y_position <= 1.0f && ss_y_position >= -1.0f)
{
offscreen_distance = ss_x_position - 1.0f;
}
// case 3 above the Y-border but not right or left of the X-border
// closest point = top border
else if (ss_y_position > 1.0f && ss_x_position <= 1.0f && ss_x_position >= -1.0f)
{
offscreen_distance = ss_y_position - 1.0f;
}
// case 4 below the Y-border but not right or left of the X-border
// closest point = bottom border
else if (ss_y_position < -1.0f && ss_x_position <= 1.0f && ss_x_position >= -1.0f)
{
offscreen_distance = -1.0f - ss_y_position;
}
// corner cases
//top left corner
else if (ss_x_position < -1.0f && ss_y_position >= 1.0f)
{
offscreen_distance = (float)Math.Sqrt(Math.Pow(-1.0f - ss_x_position, 2.0f) + Math.Pow(ss_y_position - 1.0f, 2.0f));
}
// top right corner
else if (ss_x_position > 1.0f && ss_y_position >= 1.0f)
{
offscreen_distance = (float)Math.Sqrt(Math.Pow(ss_x_position - 1.0f, 2.0f) + Math.Pow(ss_y_position - 1.0f, 2.0f));
}
// bottom left corner
else if (ss_x_position < -1.0f && ss_y_position < -1.0f)
{
offscreen_distance = (float)Math.Sqrt(Math.Pow(-1.0f - ss_x_position, 2.0f) + Math.Pow(-1.0f - ss_y_position, 2.0f));
}
// bottom right corner
else if (ss_x_position > 1.0f && ss_y_position < -1.0f)
{
offscreen_distance = (float)Math.Sqrt(Math.Pow(ss_x_position - 1.0f, 2.0f) + Math.Pow(-1.0f - ss_y_position, 2.0f));
}
Offscreen distance will be your most dramatic driver of event volume. This will be the home for most of your attenuation for offscreen objects.
(Example of an attenuation curve for offscreen_distance)
Parameter 4. "altitude_delta"
Maybe a bit more traditional of an audio spatialization parameter. Simply the difference in the Unity Y-axis (it will always be the Z-axis in my heart) position between the player and a given audio source. Mainly affects volume, but can also be used to drive filtering to give a sense of height perception.
(Attenuation due to offscreen_distance)
Parameter 5. "ortho_size"
This represents the orthographic size parameter of the camera. This appears similarly to a zoom in/out.
(ortho size of 5)
(ortho size of 15)
(ortho size of 1)
How does this pertain to audio? Our screenspace positional parameters are already scaled to the screen, they should behave as expected with the change in perspective. Why do we need this parameter? In the Tunic audio talk there isn't actually any information about how this parameter was used, just that it existed.
Taking this as a challenge, I thought it would be best to attempt to reverse engineer an application myself. My best guess is that this parameter was used to alter the mix, perhaps by triggering snapshots and/or modifying the behavior of the other parameters (screen space positions and offscreen distance specifically).
Considering the situation where the camera screenspace contains a small portion of the game world (ortho size 1) VS a situation where the screenspace covers a larger portion of the game world (ortho size 15), what would we wish to be different about the mix? My conclusion was that with a smaller visible world space you may want the viewer to hear slightly more from offscreen sound sources. Given how small the visible portion of the game world is, it could be important to give the player information about their surroundings with sound, not to mention feel more natural.
The opposite may be true for the scenario when there is a large portion of the game world visible at once. In this situation it is not so important for the player to hear offscreen sounds, as they have plenty of visible space between the player character and the screen edges to react to new visual information. It may also be best in this state to give special emphasis on sound sources closer to the screen center and add some extra attenuation to sounds closer to the edges of the screen to reduce audio chaos and provide focus in the soundscape.
(Example of how one might use ortho size to trigger snapshots)
Bonus Demo
A bit further into the Tunic audio talk, a system a system for tonal sound effects tuned to the music is demonstrated. I'm not going to walk through the entire implementation here (perhaps in a post of it's own) but here's a little demo of my recreation of that system as well just for fun.