Unity Room Acoustics Visualization
Tongyu Zhou (April 2022)
This tutorial demonstrates how you can create a live heatmap depicting the amplitudes of different audio sources in a room using Unity, as shown in the video on the right. For simplicity, I will not be explaining any code logic, but just showing you how to set it up.
Time Needed: ~3 Hours
Populate a Simple Scene with 3D Audio Sources
Set up a basic Unity scene. This can be done by dragging a couple of pre-built prefabs into the scene, as demonstrated in Unity's native VR Basics Tutorial.
Add Audio Sources to the objects you want to visualize sounds for. This can be done by right clicking on an object in the hierarchy -> Audio -> Audio Source. When you click on the generated audio source, you should see a dialog box similar to the image on the right.
Make sure the following options are selected:
Play on Awake
Drag Spatial Blend to 3D
3D Sound Settings -> Volume Rolloff to Linear Rolloff
Set the Max Distance to something small (default was 500)
Add an audio clip of your choice to the AudioClip slot.
Add the Heatmap Generation Code
In your scene, create a new GameObject by right clicking in the hierarchy -> Create Empty.
In the Editor, click on Add Component -> New Script -> name the script GradientGenerator
Copy the following code into the script file:
Adjust Heatmap Parameters
Once you have copied and pasted the code, you should see the dialog on the right.
X Size and Z Size determine the width and length of the heatmap. Adjust this based on the size of your room scene.
Drag all your AudioSources into the Audio Objects list.
Add a Gradient Shader via the following:
Window > Package Manager > make sure Universal RP (render pipeline) and Shader Graph are installed
Check Edit > Project Settings > Graphics and make sure there is a UniversalRenderPipelineAsset. If not, navigate to the Packages section > right click > follow the dialog below to create a pipeline asset, then drag that into the empty field
Create a shader graph: In Projects, right click > Create > Shader > Blank shader graph
Open the new shader graph in the editor > Active Targets -> Universal
Drag this new shader into the shader field of the object material
Adjust the Gradient colors based on your personal preference. I am using one that ranges from blue -> green -> yellow -> red.
And voila! You should see a heatmap based on the max distance of each audio source: