Skip to content

Commit

Permalink
wait for the vessel to fully load in flight (fix #89)
Browse files Browse the repository at this point in the history
  • Loading branch information
dkavolis committed May 5, 2020
1 parent ca05e6e commit df80ee0
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 19 deletions.
59 changes: 40 additions & 19 deletions FerramAerospaceResearch/FARPartGeometry/GeometryPartModule.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
Ferram Aerospace Research v0.15.11.4 "Mach"
=========================
Aerodynamics model for Kerbal Space Program
Expand Down Expand Up @@ -43,6 +43,7 @@ You should have received a copy of the GNU General Public License
*/

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
Expand Down Expand Up @@ -210,13 +211,48 @@ public override void OnStart(StartState state)
_sceneSetup = true; //this exists only to ensure that OnStart has occurred first
if (ignoreLayer0 < 0)
ignoreLayer0 = LayerMask.NameToLayer("TransparentFX");

if (part.collider == null &&
!part.Modules.Contains<ModuleWheelBase>() &&
!part.Modules.Contains<KerbalEVA>() &&
!part.Modules.Contains<FlagSite>())
return;

if (HighLogic.LoadedSceneIsEditor)
StartCoroutine(DoRebuildMeshEditor());
else if (HighLogic.LoadedSceneIsFlight)
StartCoroutine(DoRebuildMeshFlight());
}

private IEnumerator DoRebuildMeshFlight()
{
var waiter = new WaitForFixedUpdate();

while (!FlightGlobals.ready)
yield return waiter;

// have to wait for the vessel to be loaded fully so that unused model transforms are disabled before
// gathering meshes for voxelization
while (part.vessel.HoldPhysics)
yield return waiter;

RebuildAllMeshData();
}

private IEnumerator DoRebuildMeshEditor()
{
var waiter = new WaitForFixedUpdate();

while (!ApplicationLauncher.Ready)
yield return waiter;

// TODO: on ship loading all model transforms are enabled same as in flight, need to wait for something

RebuildAllMeshData();
}

private void FixedUpdate()
{
//waiting prevents changes in physics in flight or in predictions because the voxel switches to colliders rather than meshes
if (ReadyToBuildMesh())
RebuildAllMeshData();
if (!_ready && _meshesToUpdate == 0)
{
overallMeshBounds = SetBoundsFromMeshes();
Expand All @@ -227,21 +263,6 @@ private void FixedUpdate()
CheckAnimations();
}

private bool ReadyToBuildMesh()
{
bool returnVal = !_started && _sceneSetup;

returnVal &= HighLogic.LoadedSceneIsFlight && FlightGlobals.ready ||
HighLogic.LoadedSceneIsEditor && ApplicationLauncher.Ready;

returnVal &= part.collider != null ||
part.Modules.Contains<ModuleWheelBase>() ||
part.Modules.Contains<KerbalEVA>() ||
part.Modules.Contains<FlagSite>();

return returnVal;
}

public void ClearMeshData()
{
meshDataList = null;
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 comments on commit df80ee0

Please sign in to comment.