Skip to content

Commit e3bac37

Browse files
committed
#49: reduce cost of mach effects when not in atmosphere
1 parent 2e657a5 commit e3bac37

4 files changed

Lines changed: 43 additions & 34 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
- Avoid creating ShipEffects VesselModule for non-loaded vessels
99
- Cache references to mixer groups
1010
- Fix NRE in comb filter during launch or when parts break
11+
- Reduce performance cost of mach effects when not in atmosphere
1112

1213
## 0.9.13 - 2026-04-20
1314

Source/RocketSoundEnhancement/EffectBehaviours/RSE_AudioEffects.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,6 @@ public void FixedUpdate()
241241
doppler = Mathf.MoveTowards(doppler, dopplerRaw, 0.5f * TimeWarp.fixedDeltaTime);
242242
}
243243

244-
angle = (1 + Vector3.Dot(hostPart.vessel.GetComponent<ShipEffects>().MachTipCameraNormal, (transform.up + hostPart.vessel.velocityD).normalized)) * 90;
245-
246244
bool isActiveAndInternal = hostPart.vessel.isActiveVessel && (InternalCamera.Instance.isActive || MapView.MapCamera.isActiveAndEnabled);
247245
if (isActiveAndInternal)
248246
{
@@ -251,10 +249,14 @@ public void FixedUpdate()
251249
return;
252250
}
253251

252+
var shipEffects = hostPart.vessel.GetComponent<ShipEffects>();
253+
254+
angle = shipEffects.Angle;
255+
254256
if (Settings.MachEffectsAmount > 0)
255257
{
256-
mach = Mathf.Clamp01(hostPart.vessel.GetComponent<ShipEffects>().Mach);
257-
machAngle = hostPart.vessel.GetComponent<ShipEffects>().MachAngle;
258+
mach = Mathf.Clamp01(shipEffects.Mach);
259+
machAngle = shipEffects.MachAngle;
258260
machPass = Mathf.Lerp(1, Settings.MachEffectLowerLimit, Mathf.Clamp01(angle / machAngle) * mach);
259261
}
260262
else

Source/RocketSoundEnhancement/PartModules/RSE_Module.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,6 @@ public virtual void FixedUpdate()
184184
doppler = Mathf.MoveTowards(doppler, dopplerRaw, 0.5f * TimeWarp.fixedDeltaTime);
185185
}
186186

187-
angle = (1 + Vector3.Dot(vessel.GetComponent<ShipEffects>().MachTipCameraNormal, (transform.up + vessel.velocityD).normalized)) * 90;
188-
189187
bool isActiveAndInternal = vessel == FlightGlobals.ActiveVessel && InternalCamera.Instance.isActive;
190188
if (isActiveAndInternal)
191189
{
@@ -194,10 +192,14 @@ public virtual void FixedUpdate()
194192
return;
195193
}
196194

195+
var shipEffects = vessel.GetComponent<ShipEffects>();
196+
197+
angle = shipEffects.Angle;
198+
197199
if (Settings.MachEffectsAmount > 0)
198200
{
199-
mach = Mathf.Clamp01(vessel.GetComponent<ShipEffects>().Mach);
200-
machAngle = vessel.GetComponent<ShipEffects>().MachAngle;
201+
mach = Mathf.Clamp01(shipEffects.Mach);
202+
machAngle = shipEffects.MachAngle;
201203
machPass = Mathf.Lerp(1, Settings.MachEffectLowerLimit, Mathf.Clamp01(angle / machAngle) * mach);
202204
}
203205
else

Source/RocketSoundEnhancement/ShipEffects.cs

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ public static AerodynamicsFX AeroFX
4242
public float MachAngle = 90;
4343
public float MachPass = 1;
4444
public float Mach = 0;
45-
public Vector3 MachTipCameraNormal = new Vector3();
4645

4746
public bool SonicBoomed = true;
4847

@@ -224,27 +223,24 @@ private void FixedUpdate()
224223
{
225224
VesselMass -= excludedPart.mass;
226225
}
226+
}
227+
228+
private void LateUpdate()
229+
{
230+
if (!HighLogic.LoadedSceneIsFlight || !initialized || gamePaused || noPhysics || ignoreVessel)
231+
return;
232+
233+
if (vesselDataDirty)
234+
{
235+
CacheVesselData();
236+
}
227237

228238
if (Settings.EnableAudioEffects && Settings.MufflerQuality > AudioMufflerQuality.Normal)
229239
{
230240
bool isActiveAndInternal = vessel == FlightGlobals.ActiveVessel && InternalCamera.Instance.isActive;
231-
var velocityDirection = vessel.velocityD.normalized * vessel.vesselSize.magnitude;
232-
var positionTip = transform.position + velocityDirection;
233-
var positionRear = transform.position - velocityDirection;
234-
var vesselTip = transform.position;
235-
var vesselRear = transform.position;
236-
RaycastHit tipHit;
237-
238-
if (Physics.BoxCast(positionTip, vessel.vesselSize, -vessel.velocityD.normalized, out tipHit))
239-
{
240-
vesselTip = tipHit.point;
241-
}
242-
243241
var cameraPosition = CameraManager.GetCurrentCamera().transform.position;
244242

245-
MachTipCameraNormal = (cameraPosition - vesselTip).normalized;
246243
Distance = Vector3.Distance(cameraPosition, transform.position);
247-
Angle = (1 + Vector3.Dot(MachTipCameraNormal, vessel.velocityD.normalized)) * 90;
248244

249245
if (isActiveAndInternal)
250246
{
@@ -253,6 +249,25 @@ private void FixedUpdate()
253249
return;
254250
}
255251

252+
if (vessel.staticPressurekPa > 0)
253+
{
254+
var vesselTip = transform.position;
255+
var velocityDirection = vessel.velocityD.normalized * vessel.vesselSize.magnitude;
256+
var positionTip = transform.position + velocityDirection;
257+
Vector3 MachTipCameraNormal = (cameraPosition - vesselTip).normalized;
258+
RaycastHit tipHit;
259+
260+
if (Physics.BoxCast(positionTip, vessel.vesselSize, -vessel.velocityD.normalized, out tipHit))
261+
{
262+
vesselTip = tipHit.point;
263+
}
264+
Angle = (1 + Vector3.Dot(MachTipCameraNormal, vessel.velocityD.normalized)) * 90;
265+
}
266+
else
267+
{
268+
Angle = 0;
269+
}
270+
256271
if (Settings.MachEffectsAmount > 0)
257272
{
258273
Mach = (float)vessel.mach * Mathf.Clamp01((float)(vessel.staticPressurekPa * 1000) / 404.1f);
@@ -266,17 +281,6 @@ private void FixedUpdate()
266281
MachPass = 1;
267282
}
268283
}
269-
}
270-
271-
private void LateUpdate()
272-
{
273-
if (!HighLogic.LoadedSceneIsFlight || !initialized || gamePaused || noPhysics || ignoreVessel)
274-
return;
275-
276-
if (vesselDataDirty)
277-
{
278-
CacheVesselData();
279-
}
280284

281285
foreach (var soundLayerGroup in ShipEffectsConfig.SoundLayerGroups)
282286
{

0 commit comments

Comments
 (0)