-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRaycastedSound.cs
More file actions
228 lines (186 loc) · 8.46 KB
/
RaycastedSound.cs
File metadata and controls
228 lines (186 loc) · 8.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
using UdonSharp;
using UnityEngine;
using VRC.SDKBase;
using VRC.Udon;
public class RaycastedSound : UdonSharpBehaviour
{
// Defining public variables, that will be visible in Unity Edtior
public AudioSource AudioSource; //!< AudioSource to apply the effect
[Header("Cutoff filter")]
public bool Cutoff = true; //!< Cutoff by wall toggle
[Range(0f, 1f)] public float WallCutoff = 0.56f; //!< Cutoff by single wall
[Range(0.01f, 1f)] public float DistanceCutoff = 0.3f; //!< Cutoff by distance multiplier
public float CutoffChangeSmoothing = 10f; //!< Smoothness of Cutoff changes
public float CutoffStartDistance = 20f; //!< Cutoff by distance starting distance
[Header("Volume filter")]
public bool VolumeDecrease = true; //!< Volume decrease by wall
[Range(0f, 1f)] public float VolumeMax = 1f; //!< AudioSourve maximum volume
[Range(0f, 1f)] public float VolumeMin = 0.01f; //!< AudioSorce minimum volume
[Range(0f, 1f)] public float VolDecPerWall = 0.05f; //!< Volume decrease by single wall
public float WallVolumeDecreaseSmoothing = 10f; //!< Smoothness of Cutoff changes
[Header("Spatial Blend filter")]
public bool SpatialBlendChange = true; //!< Blending of sound direction toggle
public float SpatialBlendChangeSmoothing = 10f; //!< Smoothness of Spatial Blend changes
[Range(0f, 1f)] public float SpatialBlendChangePerWall = 0.042f; //!< Spatial Blend change by single wall
[Range(0f, 1f)] public float SpatialMax = 1f; //!< Upper bound of spatial blend
[Header("Other")]
[Range(0f, 2f)] public float RaycastPlayerHeightCorrection= 1f; //!< Raycast player height correction
// variables that will be used for calculations
private float Distance; //!< Distance from audio source to listener
private float Distcut; //!< Target cutoff by distance
private float Wallcut; //!< Target cutoff by walls
// Variables for smoothing changes:
private float CurrentVolume; //!< Current volume of audio source
private float TargetVolume; //!< Target volume of audio souce
private float CurrentCutoff; //!< Current cutoff
private float TargetCutoff; //!< Target cutoff
private float CurrentBlend; //!< Current spatial blend
private float TargetBlend; //!< Target spatial blend
// Player API:
private VRCPlayerApi Player; //!< API VRCPlayer
private Vector3 AudioListenerTransformPosition; //!< Current position of the player
// Raycast:
private int hits; //!< Raycast hits array
private Ray ray; //!< Raycast ray
/*!
\brief Method that called before the first frame.
Default Unity Engine method
*/
void Start()
{
// making sure that lowpass filter enabled and getting the local players VRCPlayerAPI
gameObject.GetComponent<AudioLowPassFilter>().enabled = true;
Player = Networking.LocalPlayer;
}
/*!
\brief Method that called every 1/50s.
Default Unity Engine method
\warning if gameObject has no component AudioLowPassFilter - udon will throw runtime exception.
*/
void FixedUpdate()
{
AudioListenerTransformPosition = Player.GetPosition() + Vector3.up * RaycastPlayerHeightCorrection;
Distance = Vector3.Distance(transform.position, AudioListenerTransformPosition);
if(Distance < AudioSource.maxDistance){
ApplyFilters();
}
}
/*!
\brief Calls filters if they're on.
Checks if the each filter enabled in the settings and calls coresponging method for applying it, if it is.
*/
private void ApplyFilters()
{
hits = GetRaycastHitsCount();
if(VolumeDecrease) VolumeDecreaseFilter();
if(Cutoff) CutoffFilter();
if(SpatialBlendChange) SpatialBlendChangeFilter();
}
/*!
\brief Applies Spatial Blend filter depending on the raycast.
Calculates target value for Spatial Blend effect and applies it to AudioSource.
Makes sound less directional if audio source are behind the wall.
\warning This filter has major effect on overall volume of the sound, less directional audio becomes much louder. Use carefully.
*/
private void SpatialBlendChangeFilter()
{
CurrentBlend = AudioSource.spatialBlend;
TargetBlend = SpatialMax - SpatialBlendChangePerWall * hits;
// if we have smoothing > 0 - calculating values depending on SpatialBlendChangeSmoothing
// never use spatial blend change without smoothing.
if(SpatialBlendChangeSmoothing != 0){
AudioSource.spatialBlend = Smooth(CurrentBlend, TargetBlend, SpatialBlendChangeSmoothing);
}else{
AudioSource.spatialBlend = TargetBlend;
}
}
/*!
\brief Applies Cutoff filter depending on the raycast.
Calculates target value for Cutoff effect and applies it to AudioSource.
Makes sound deeper and less pronounced while behind the wall.
\warning This filter has major effect on overall volume of the sound.
*/
private void CutoffFilter()
{
// if distance cutoff isn't = 0 - calculating it
if(DistanceCutoff != 0) Distcut = 22000 * DistcutSigmoidFunction(Distance);
else Distcut = 22000;
// if there is at least 1 wall - calculating cutoff
if(hits != 0) Wallcut = WallcutFunction();
else Wallcut = 0;
// calculating target cutoff
TargetCutoff = Distcut - Wallcut;
CurrentCutoff = gameObject.GetComponent<AudioLowPassFilter>().cutoffFrequency;
// if smoothing !=0 changing it using smoothing function
// or just changing cutoff filter immidiately (don't use that)
if(CutoffChangeSmoothing != 0){
gameObject.GetComponent<AudioLowPassFilter>().cutoffFrequency = Smooth(CurrentCutoff, TargetCutoff, CutoffChangeSmoothing);
}else{
gameObject.GetComponent<AudioLowPassFilter>().cutoffFrequency = TargetCutoff;
}
}
/*!
\brief Applies Volume depending on the raycast.
Calculates target value for overall volume and applies it to AudioSource.
Makes sound quiter while behind the wall.
*/
private void VolumeDecreaseFilter()
{
CurrentVolume = AudioSource.volume;
TargetVolume = VolumeMax - VolDecPerWall * hits;
// ensuring that target volume is not lower than minimum value
if(VolumeMin != 0 && TargetVolume < VolumeMin) TargetVolume = VolumeMin;
// applying it to audioSource
if(WallVolumeDecreaseSmoothing != 0){
AudioSource.volume = Smooth(CurrentVolume, TargetVolume, WallVolumeDecreaseSmoothing);
}else{
AudioSource.volume = TargetVolume;
}
}
/*!
\brief Raycast hits counter.
Calculates target value for Cutoff effect and applies it to AudioSource
Makes sound deeper and less pronounced while behind the wall
\warning Counts only hits to object on 11th layer
\todo Make it possible to change the target layer in unity editor
*/
private int GetRaycastHitsCount()
{
ray = new Ray(transform.position, AudioListenerTransformPosition - transform.position);
return Physics.RaycastAll(ray, Distance, 1 << 11).Length;
}
/*!
\brief Sigmoid function for cutoff filter
\param[in] Distance Distance between VRCPlayer and audioSource
\return Target Cutoff multiplier
*/
private float DistcutSigmoidFunction(float distance)
{
return 1 - 1 / (1 + Mathf.Pow(1 + DistanceCutoff, -distance + CutoffStartDistance));
}
/*!
\brief Function for cutoff by raycast
Mathematical function that calculates the target cutoff frequency depending on raycast data
\return Target Cutoff value
*/
private float WallcutFunction()
{
return Mathf.Atan(hits * (1 + 10 * WallCutoff)) * Distcut * WallCutoff;
}
/*!
\brief Smoothing values when they change
\param[in] current Current value in the parameters of filter
\param[in] target The target value
\param[in] smoothing Coefficient of smoothing
\return Next "current" Value
*/
private float Smooth(float current, float target, float smoothing)
{
if(current < target){
return current + ((target - current) / smoothing);
}else{
return current - ((current - target) / smoothing);
}
}
}