-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathCTAtmosphericAudioController.cs
194 lines (162 loc) · 5.77 KB
/
CTAtmosphericAudioController.cs
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
using System;
using UnityEngine;
namespace CameraTools
{
public class CTAtmosphericAudioController : MonoBehaviour
{
AudioSource windAudioSource;
AudioSource windHowlAudioSource;
AudioSource windTearAudioSource;
AudioSource sonicBoomSource;
Vessel vessel;
bool playedBoom = false;
void Awake()
{
vessel = GetComponent<Vessel>();
windAudioSource = gameObject.AddComponent<AudioSource>();
windAudioSource.minDistance = 10;
windAudioSource.maxDistance = 10000;
windAudioSource.dopplerLevel = .35f;
windAudioSource.spatialBlend = 1;
AudioClip windclip = GameDatabase.Instance.GetAudioClip("CameraTools/Sounds/windloop");
if(!windclip)
{
Destroy (this);
return;
}
windAudioSource.clip = windclip;
windHowlAudioSource = gameObject.AddComponent<AudioSource>();
windHowlAudioSource.minDistance = 10;
windHowlAudioSource.maxDistance = 7000;
windHowlAudioSource.dopplerLevel = .5f;
windHowlAudioSource.pitch = 0.25f;
windHowlAudioSource.clip = GameDatabase.Instance.GetAudioClip("CameraTools/Sounds/windhowl");
windHowlAudioSource.spatialBlend = 1;
windTearAudioSource = gameObject.AddComponent<AudioSource>();
windTearAudioSource.minDistance = 10;
windTearAudioSource.maxDistance = 5000;
windTearAudioSource.dopplerLevel = 0.45f;
windTearAudioSource.pitch = 0.65f;
windTearAudioSource.clip = GameDatabase.Instance.GetAudioClip("CameraTools/Sounds/windtear");
windTearAudioSource.spatialBlend = 1;
sonicBoomSource = new GameObject().AddComponent<AudioSource>();
sonicBoomSource.transform.parent = vessel.transform;
sonicBoomSource.transform.localPosition = Vector3.zero;
sonicBoomSource.minDistance = 50;
sonicBoomSource.maxDistance = 20000;
sonicBoomSource.dopplerLevel = 0;
sonicBoomSource.clip = GameDatabase.Instance.GetAudioClip("CameraTools/Sounds/sonicBoom");
sonicBoomSource.volume = Mathf.Clamp01(vessel.GetTotalMass()/4f);
sonicBoomSource.Stop();
sonicBoomSource.spatialBlend = 1;
float angleToCam = Vector3.Angle(vessel.srf_velocity, FlightCamera.fetch.mainCamera.transform.position - vessel.transform.position);
angleToCam = Mathf.Clamp(angleToCam, 1, 180);
if(vessel.srfSpeed / (angleToCam) < 3.67f)
{
playedBoom = true;
}
CamTools.OnResetCTools += OnResetCTools;
}
void FixedUpdate()
{
if(!vessel)
{
return;
}
if(Time.timeScale > 0 && vessel.dynamicPressurekPa > 0)
{
float srfSpeed = (float)vessel.srfSpeed;
srfSpeed = Mathf.Min(srfSpeed, 550f);
float angleToCam = Vector3.Angle(vessel.srf_velocity, FlightCamera.fetch.mainCamera.transform.position - vessel.transform.position);
angleToCam = Mathf.Clamp(angleToCam, 1, 180);
float lagAudioFactor = (75000 / (Vector3.Distance(vessel.transform.position, FlightCamera.fetch.mainCamera.transform.position) * srfSpeed * angleToCam / 90));
lagAudioFactor = Mathf.Clamp(lagAudioFactor * lagAudioFactor * lagAudioFactor, 0, 4);
lagAudioFactor += srfSpeed / 230;
float waveFrontFactor = ((3.67f * angleToCam)/srfSpeed);
waveFrontFactor = Mathf.Clamp(waveFrontFactor * waveFrontFactor * waveFrontFactor, 0, 2);
if(vessel.srfSpeed > CamTools.speedOfSound)
{
waveFrontFactor = (srfSpeed / (angleToCam) < 3.67f) ? waveFrontFactor + ((srfSpeed/(float)CamTools.speedOfSound)*waveFrontFactor) : 0;
if(waveFrontFactor > 0)
{
if(!playedBoom)
{
sonicBoomSource.transform.position = vessel.transform.position + (-vessel.srf_velocity);
sonicBoomSource.PlayOneShot(sonicBoomSource.clip);
}
playedBoom = true;
}
else
{
}
}
else if(CamTools.speedOfSound / (angleToCam) < 3.67f)
{
playedBoom = true;
}
lagAudioFactor *= waveFrontFactor;
float sqrAccel = (float)vessel.acceleration.sqrMagnitude;
//windloop
if(!windAudioSource.isPlaying)
{
windAudioSource.Play();
//Debug.Log("vessel dynamic pressure: " + vessel.dynamicPressurekPa);
}
float pressureFactor = Mathf.Clamp01((float)vessel.dynamicPressurekPa / 50f);
float massFactor = Mathf.Clamp01(vessel.GetTotalMass() / 60f);
float gFactor = Mathf.Clamp(sqrAccel / 225, 0, 1.5f);
windAudioSource.volume = massFactor * pressureFactor * gFactor * lagAudioFactor;
//windhowl
if(!windHowlAudioSource.isPlaying)
{
windHowlAudioSource.Play();
}
float pressureFactor2 = Mathf.Clamp01((float)vessel.dynamicPressurekPa / 20f);
float massFactor2 = Mathf.Clamp01(vessel.GetTotalMass() / 30f);
windHowlAudioSource.volume = pressureFactor2 * massFactor2 * lagAudioFactor;
windHowlAudioSource.maxDistance = Mathf.Clamp(lagAudioFactor * 2500, windTearAudioSource.minDistance, 16000);
//windtear
if(!windTearAudioSource.isPlaying)
{
windTearAudioSource.Play();
}
float pressureFactor3 = Mathf.Clamp01((float)vessel.dynamicPressurekPa / 40f);
float massFactor3 = Mathf.Clamp01(vessel.GetTotalMass() / 10f);
//float gFactor3 = Mathf.Clamp(sqrAccel / 325, 0.25f, 1f);
windTearAudioSource.volume = pressureFactor3 * massFactor3;
windTearAudioSource.minDistance = lagAudioFactor * 1;
windTearAudioSource.maxDistance = Mathf.Clamp(lagAudioFactor * 2500, windTearAudioSource.minDistance, 16000);
}
else
{
if(windAudioSource.isPlaying)
{
windAudioSource.Stop();
}
if(windHowlAudioSource.isPlaying)
{
windHowlAudioSource.Stop();
}
if(windTearAudioSource.isPlaying)
{
windTearAudioSource.Stop();
}
}
}
void OnDestroy()
{
if(sonicBoomSource)
{
Destroy(sonicBoomSource.gameObject);
}
CamTools.OnResetCTools -= OnResetCTools;
}
void OnResetCTools()
{
Destroy(windAudioSource);
Destroy(windHowlAudioSource);
Destroy(windTearAudioSource);
Destroy(this);
}
}
}