Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 33 additions & 33 deletions CameraTools/CTAtmosphericAudioController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,49 +17,53 @@ public class CTAtmosphericAudioController : MonoBehaviour
void Awake()
{
vessel = GetComponent<Vessel>();
windAudioSource = gameObject.AddComponent<AudioSource>();

windAudioSource = new 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)
if (!windclip)
{
Destroy (this);
Destroy(this);
return;
}
windAudioSource.clip = windclip;
windAudioSource.transform.parent = vessel.transform;

windHowlAudioSource = gameObject.AddComponent<AudioSource>();
windHowlAudioSource = new 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;
windHowlAudioSource.transform.parent = vessel.transform;

windTearAudioSource = gameObject.AddComponent<AudioSource>();
windTearAudioSource = new 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;
windTearAudioSource.transform.parent = vessel.transform;

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.volume = Mathf.Clamp01(vessel.GetTotalMass() / 4f);
sonicBoomSource.Stop();
sonicBoomSource.spatialBlend = 1;
sonicBoomSource.transform.parent = vessel.transform;

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)
if (vessel.srfSpeed / (angleToCam) < 3.67f)
{
playedBoom = true;
}
Expand All @@ -70,32 +74,32 @@ void Awake()

void FixedUpdate()
{
if(!vessel)
if (!vessel || !vessel.loaded || !vessel.isActiveAndEnabled)
{
return;
}
if(Time.timeScale > 0 && vessel.dynamicPressurekPa > 0)
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);
float waveFrontFactor = ((3.67f * angleToCam) / srfSpeed);
waveFrontFactor = Mathf.Clamp(waveFrontFactor * waveFrontFactor * waveFrontFactor, 0, 2);


if(vessel.srfSpeed > CamTools.speedOfSound)
if (vessel.srfSpeed > CamTools.speedOfSound)
{
waveFrontFactor = (srfSpeed / (angleToCam) < 3.67f) ? waveFrontFactor + ((srfSpeed/(float)CamTools.speedOfSound)*waveFrontFactor) : 0;
if(waveFrontFactor > 0)
waveFrontFactor = (srfSpeed / (angleToCam) < 3.67f) ? waveFrontFactor + ((srfSpeed / (float)CamTools.speedOfSound) * waveFrontFactor) : 0;
if (waveFrontFactor > 0)
{
if(!playedBoom)
if (!playedBoom)
{
sonicBoomSource.transform.position = vessel.transform.position + (-vessel.srf_velocity);
sonicBoomSource.PlayOneShot(sonicBoomSource.clip);
Expand All @@ -107,7 +111,7 @@ void FixedUpdate()

}
}
else if(CamTools.speedOfSound / (angleToCam) < 3.67f)
else if (CamTools.speedOfSound / (angleToCam) < 3.67f)
{
playedBoom = true;
}
Expand All @@ -117,10 +121,10 @@ void FixedUpdate()
float sqrAccel = (float)vessel.acceleration.sqrMagnitude;

//windloop
if(!windAudioSource.isPlaying)
if (!windAudioSource.isPlaying)
{
windAudioSource.Play();
//Debug.Log("[CameraTools]: vessel dynamic pressure: " + vessel.dynamicPressurekPa);
// Debug.Log("[CameraTools]: vessel dynamic pressure: " + vessel.dynamicPressurekPa);
}
float pressureFactor = Mathf.Clamp01((float)vessel.dynamicPressurekPa / 50f);
float massFactor = Mathf.Clamp01(vessel.GetTotalMass() / 60f);
Expand All @@ -129,7 +133,7 @@ void FixedUpdate()


//windhowl
if(!windHowlAudioSource.isPlaying)
if (!windHowlAudioSource.isPlaying)
{
windHowlAudioSource.Play();
}
Expand All @@ -139,7 +143,7 @@ void FixedUpdate()
windHowlAudioSource.maxDistance = Mathf.Clamp(lagAudioFactor * 2500, windTearAudioSource.minDistance, 16000);

//windtear
if(!windTearAudioSource.isPlaying)
if (!windTearAudioSource.isPlaying)
{
windTearAudioSource.Play();
}
Expand All @@ -150,21 +154,21 @@ void FixedUpdate()

windTearAudioSource.minDistance = lagAudioFactor * 1;
windTearAudioSource.maxDistance = Mathf.Clamp(lagAudioFactor * 2500, windTearAudioSource.minDistance, 16000);

}
else
{
if(windAudioSource.isPlaying)
if (windAudioSource.isPlaying)
{
windAudioSource.Stop();
}

if(windHowlAudioSource.isPlaying)
if (windHowlAudioSource.isPlaying)
{
windHowlAudioSource.Stop();
}

if(windTearAudioSource.isPlaying)
if (windTearAudioSource.isPlaying)
{
windTearAudioSource.Stop();
}
Expand All @@ -173,19 +177,15 @@ void FixedUpdate()

void OnDestroy()
{
if(sonicBoomSource)
{
Destroy(sonicBoomSource.gameObject);
}
if (sonicBoomSource) Destroy(sonicBoomSource.gameObject);
if (windAudioSource) Destroy(windAudioSource.gameObject);
if (windHowlAudioSource) Destroy(windHowlAudioSource.gameObject);
if (windTearAudioSource) Destroy(windTearAudioSource.gameObject);
CamTools.OnResetCTools -= OnResetCTools;
}

void OnResetCTools()
{
Destroy(windAudioSource);
Destroy(windHowlAudioSource);
Destroy(windTearAudioSource);

Destroy(this);
}
}
Expand Down
24 changes: 12 additions & 12 deletions CameraTools/CTPartAudioController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ void Awake()

void Start()
{
if(!audioSource)
if (!audioSource)
{
Destroy(this);
return;
Expand All @@ -38,18 +38,18 @@ void Start()
origRolloffMode = audioSource.rolloffMode;
audioSource.rolloffMode = AudioRolloffMode.Logarithmic;
audioSource.spatialBlend = 1;

}

void FixedUpdate()
{
if(!audioSource)
if (!audioSource)
{
Destroy(this);
return;
}

if(!part || !vessel)
if (!part || !vessel || !FlightCamera.fetch)
{
Destroy(this);
return;
Expand All @@ -66,25 +66,25 @@ void FixedUpdate()
lagAudioFactor = Mathf.Clamp(lagAudioFactor * lagAudioFactor * lagAudioFactor, 0, 4);
lagAudioFactor += srfSpeed / 230;

float waveFrontFactor = ((3.67f * angleToCam)/srfSpeed);
float waveFrontFactor = ((3.67f * angleToCam) / srfSpeed);
waveFrontFactor = Mathf.Clamp(waveFrontFactor * waveFrontFactor * waveFrontFactor, 0, 2);
if(vessel.srfSpeed > CamTools.speedOfSound)
if (vessel.srfSpeed > CamTools.speedOfSound)
{
waveFrontFactor = (srfSpeed / (angleToCam) < 3.67f) ? waveFrontFactor + ((srfSpeed/(float)CamTools.speedOfSound)*waveFrontFactor): 0;
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));

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()
Expand Down
27 changes: 19 additions & 8 deletions CameraTools/CTPersistantFIeld.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ public CTPersistantField() { }
public static void Save()
{
ConfigNode fileNode = ConfigNode.Load(settingsURL);
if (fileNode == null)
fileNode = new ConfigNode();

if (!fileNode.HasNode("CToolsSettings"))
fileNode.AddNode("CToolsSettings");

ConfigNode settings = fileNode.GetNode("CToolsSettings");

foreach (var field in typeof(CamTools).GetFields())
Expand All @@ -28,19 +34,24 @@ public static void Save()
public static void Load()
{
ConfigNode fileNode = ConfigNode.Load(settingsURL);
ConfigNode settings = fileNode.GetNode("CToolsSettings");
if (fileNode == null) return; // No config file.

foreach (var field in typeof(CamTools).GetFields())
if (fileNode.HasNode("CToolsSettings"))
{
if (field == null) continue;
if (!field.IsDefined(typeof(CTPersistantField), false)) continue;
ConfigNode settings = fileNode.GetNode("CToolsSettings");

if (settings.HasValue(field.Name))
foreach (var field in typeof(CamTools).GetFields())
{
object parsedValue = ParseValue(field.FieldType, settings.GetValue(field.Name));
if (parsedValue != null)
if (field == null) continue;
if (!field.IsDefined(typeof(CTPersistantField), false)) continue;

if (settings.HasValue(field.Name))
{
field.SetValue(CamTools.fetch, parsedValue);
object parsedValue = ParseValue(field.FieldType, settings.GetValue(field.Name));
if (parsedValue != null)
{
field.SetValue(CamTools.fetch, parsedValue);
}
}
}
}
Expand Down
Loading