-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathCTPartAudioController.cs
More file actions
103 lines (76 loc) · 2.41 KB
/
CTPartAudioController.cs
File metadata and controls
103 lines (76 loc) · 2.41 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
using System;
using System.Collections;
using UnityEngine;
namespace CameraTools
{
public class CTPartAudioController : MonoBehaviour
{
Vessel vessel;
Part part;
public AudioSource audioSource;
float origMinDist = 1;
float origMaxDist = 1;
float modMinDist = 10;
float modMaxDist = 10000;
AudioRolloffMode origRolloffMode;
void Awake()
{
part = GetComponentInParent<Part>();
vessel = part.vessel;
CamTools.OnResetCTools += OnResetCTools;
}
void Start()
{
if(!audioSource)
{
Destroy(this);
return;
}
origMinDist = audioSource.minDistance;
origMaxDist = audioSource.maxDistance;
origRolloffMode = audioSource.rolloffMode;
audioSource.rolloffMode = AudioRolloffMode.Logarithmic;
audioSource.spatialBlend = 1;
}
void FixedUpdate()
{
if(!audioSource)
{
Destroy(this);
return;
}
if(!part || !vessel)
{
Destroy(this);
return;
}
float angleToCam = Vector3.Angle(vessel.srf_velocity, FlightCamera.fetch.mainCamera.transform.position - vessel.transform.position);
angleToCam = Mathf.Clamp(angleToCam, 1, 180);
float srfSpeed = (float)vessel.srfSpeed;
srfSpeed = Mathf.Min(srfSpeed, 550f);
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;
}
lagAudioFactor *= waveFrontFactor;
audioSource.minDistance = Mathf.Lerp(origMinDist, modMinDist * lagAudioFactor, Mathf.Clamp01((float)vessel.srfSpeed/30));
audioSource.maxDistance = Mathf.Lerp(origMaxDist,Mathf.Clamp(modMaxDist * lagAudioFactor, audioSource.minDistance, 16000), Mathf.Clamp01((float)vessel.srfSpeed/30));
}
void OnDestroy()
{
CamTools.OnResetCTools -= OnResetCTools;
}
void OnResetCTools()
{
audioSource.minDistance = origMinDist;
audioSource.maxDistance = origMaxDist;
audioSource.rolloffMode = origRolloffMode;
Destroy(this);
}
}
}