Skip to content

Commit 74a0467

Browse files
authored
Merge pull request #433 from Csantucci/sound-cutoff-improve Don't unload sound sources of the player train when camera near it
2 parents de02eda + 5f52b34 commit 74a0467

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

Source/RunActivity/Viewer3D/Sound.cs

+20-9
Original file line numberDiff line numberDiff line change
@@ -493,9 +493,20 @@ public static WorldLocation UidLocation(UiD uid)
493493
public class SoundSource : SoundSourceBase
494494
{
495495
/// <summary>
496-
/// Squeared cutoff distance. No sound is audible above that
496+
/// Squared cutoff distance. No sound is audible above that, except for the actual player train,
497+
/// where cutoff occurs at a distance wich is higher than the train length plus offset to
498+
/// approximately take into account distance from camera to car
497499
/// </summary>
498-
private const int CUTOFFDISTANCE = 4000000;
500+
private int CutOffDistanceM2
501+
{
502+
get
503+
{
504+
const int staticDistanceM2 = 4000000;
505+
var isPlayer = Car?.Train?.IsActualPlayerTrain ?? false;
506+
var correctedLength = isPlayer ? Car.Train.Length + 50 : 0;
507+
return (int)Math.Max(staticDistanceM2, correctedLength * correctedLength);
508+
}
509+
}
499510
/// <summary>
500511
/// Max distance for OpenAL inverse distance model. Equals to Math.Sqrt(CUTOFFDISTANCE)
501512
/// </summary>
@@ -659,9 +670,9 @@ public override void Uninitialize()
659670
public bool MstsMonoTreatment;
660671

661672
/// <summary>
662-
/// Current distance to camera, squared meter. Is used for comparision to <see cref="CUTOFFDISTANCE"/>, to determine if is out-of-scope
673+
/// Current distance to camera, squared meter. Is used for comparision to <see cref="CutOffDistanceM2"/>, to determine if is out-of-scope
663674
/// </summary>
664-
public float DistanceSquared = CUTOFFDISTANCE + 1;
675+
public float DistanceSquared = float.MaxValue;
665676
/// <summary>
666677
/// Out-of-scope state in previous <see cref="Update"/> loop
667678
/// </summary>
@@ -994,7 +1005,7 @@ public override bool Update()
9941005
} // Update
9951006

9961007
/// <summary>
997-
/// Calculate current distance to camera, and compare it to <see cref="CUTOFFDISTANCE"/>
1008+
/// Calculate current distance to camera, and compare it to <see cref="CutOffDistanceM2"/>
9981009
/// </summary>
9991010
/// <returns>True, if is now out-of-scope</returns>
10001011
public bool isOutOfDistance()
@@ -1009,13 +1020,13 @@ public bool isOutOfDistance()
10091020
float.IsNaN(WorldLocation.Location.Y) ||
10101021
float.IsNaN(WorldLocation.Location.Z))
10111022
{
1012-
DistanceSquared = CUTOFFDISTANCE + 1;
1023+
DistanceSquared = float.MaxValue;
10131024
return true;
10141025
}
10151026

10161027
DistanceSquared = WorldLocation.GetDistanceSquared(WorldLocation, Viewer.Camera.CameraWorldLocation);
10171028

1018-
return DistanceSquared > CUTOFFDISTANCE;
1029+
return DistanceSquared > CutOffDistanceM2;
10191030
}
10201031

10211032
/// <summary>
@@ -1034,7 +1045,7 @@ public bool Activate()
10341045
{
10351046
// (ActivationConditions.Distance == 0) means distance checking disabled
10361047
if ((ActivationConditions.Distance == 0 || DistanceSquared < ActivationConditions.Distance * ActivationConditions.Distance) &&
1037-
DistanceSquared < CUTOFFDISTANCE)
1048+
DistanceSquared < CutOffDistanceM2)
10381049
return true;
10391050
}
10401051
else
@@ -1059,7 +1070,7 @@ public bool DeActivate()
10591070
if (WorldLocation != WorldLocation.None)
10601071
{
10611072
if (DeactivationConditions.Distance != 0 && DistanceSquared > DeactivationConditions.Distance * DeactivationConditions.Distance ||
1062-
DistanceSquared > CUTOFFDISTANCE)
1073+
DistanceSquared > CutOffDistanceM2)
10631074
return true;
10641075
}
10651076

0 commit comments

Comments
 (0)