Skip to content

Commit

Permalink
optimize current part updating
Browse files Browse the repository at this point in the history
  • Loading branch information
JonnyOThan committed Jan 18, 2023
1 parent 7a95595 commit dae7188
Showing 1 changed file with 15 additions and 30 deletions.
45 changes: 15 additions & 30 deletions FreeIva/FreeIva.cs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ public void FixedUpdate()
}

public static int DepthMaskQueue = 999;
private static HashSet<Part> visibleParts = new HashSet<Part>();

/// <summary>
///
/// </summary>
Expand All @@ -242,9 +242,12 @@ public static void SetRenderQueues(Part activePart)
return;
}

List<Part> possibleParts = new List<Part>();
Vector3 _previousCameraPosition = Vector3.zero;
public void UpdateCurrentPart()
{
if (KerbalIvaAddon.Instance.buckled) return;

if (InternalCamera.Instance == null)
{
Debug.LogError("InternalCamera was null");
Expand All @@ -261,7 +264,7 @@ public void UpdateCurrentPart()

// Part colliders are larger than the parts themselves and overlap.
// Find which of the containing parts we're nearest to.
List<Part> possibleParts = new List<Part>();
possibleParts.Clear();

if (CurrentPart != null) // e.g. on part destroyed.
{
Expand Down Expand Up @@ -364,41 +367,23 @@ public void UpdateCurrentPart()
public static bool PartBoundsCamera(Part p)
{
Profiler.BeginSample("PartBoundsCamera");
var part = GameObjectBoundsCamera(p.gameObject);
Profiler.EndSample();
return part;
}
bool result = false;

private static bool GameObjectBoundsCamera(GameObject go)
{
// The transform containing the mesh can be buried several levels deep.
int childCount = go.transform.childCount;
for (int i = 0; i < childCount; i++)
if (p.internalModel != null)
{
Transform child = go.transform.GetChild(i);
if (child.name != "main camera pivot" && child.GetComponent<Part>() == null)
for (var ivaModule = InternalModuleFreeIva.GetForModel(p.internalModel); ivaModule != null; ivaModule = InternalModuleFreeIva.GetForModel(ivaModule.SecondaryInternalModel))
{
GameObject goc = child.gameObject;
MeshFilter[] meshc = goc.GetComponents<MeshFilter>();
for (int m = 0; m < meshc.Length; m++)
Vector3 localPosition = ivaModule.internalModel.transform.InverseTransformPoint(InternalCamera.Instance.transform.position);
if (ivaModule.ShellColliderBounds.Contains(localPosition))
{
Bounds meshBounds = meshc[m].mesh.bounds;
if (meshBounds != null)
{
Vector3 camPos = InternalSpace.InternalToWorld(InternalCamera.Instance.transform.position);
// Bounds are relative to the transform position, not the world.
camPos -= goc.transform.position;

if (meshBounds.Contains(camPos))
return true;
}
result = true;
break;
}
bool foundGrandChild = GameObjectBoundsCamera(goc);
if (foundGrandChild)
return true;
}
}
return false;

Profiler.EndSample();
return result;
}

public static void UpdateCurrentPart(Part newCurrentPart)
Expand Down

0 comments on commit dae7188

Please sign in to comment.