Skip to content

Commit

Permalink
enable debug voxels in flight
Browse files Browse the repository at this point in the history
  • Loading branch information
dkavolis committed Nov 24, 2020
1 parent e22225a commit 5560079
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 19 deletions.
8 changes: 7 additions & 1 deletion FerramAerospaceResearch/FARAeroComponents/FARVesselAero.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ public class FARVesselAero : VesselModule
private VehicleAerodynamics _vehicleAero;
private VesselIntakeRamDrag _vesselIntakeRamDrag;

internal VehicleAerodynamics VehicleAero
{
get { return _vehicleAero; }
}

public double Length
{
get { return _vehicleAero.Length; }
Expand Down Expand Up @@ -447,7 +452,8 @@ public void VesselUpdate(bool recalcGeoModules)
_voxelCount,
vessel.Parts,
_currentGeoModules,
!setup))
!setup,
vessel))
{
_updateRateLimiter = FARSettingsScenarioModule.VoxelSettings.minPhysTicksPerUpdate - 2;
_updateQueued = true;
Expand Down
25 changes: 14 additions & 11 deletions FerramAerospaceResearch/FARAeroComponents/VehicleAerodynamics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ You should have received a copy of the GNU General Public License
using FerramAerospaceResearch.FARPartGeometry;
using FerramAerospaceResearch.FARPartGeometry.GeometryModification;
using FerramAerospaceResearch.FARThreading;
using FerramAerospaceResearch.Settings;
using UnityEngine;

namespace FerramAerospaceResearch.FARAeroComponents
Expand Down Expand Up @@ -105,7 +106,7 @@ internal class VehicleAerodynamics
private int firstSection;

private bool visualizing;
private bool voxelizing;
public bool Voxelizing { get; private set; }

public VehicleAerodynamics()
{
Expand Down Expand Up @@ -335,12 +336,13 @@ public bool TryVoxelUpdate(
int voxelCount,
List<Part> vehiclePartList,
List<GeometryPartModule> currentGeoModules,
bool updateGeometryPartModules = true
bool updateGeometryPartModules = true,
Vessel vessel = null
)
{
//set to true when this function ends; only continue to voxelizing if the voxelization thread has not been queued
//this should catch conditions where this function is called again before the voxelization thread starts
if (voxelizing)
if (Voxelizing)
return false;
//only continue if the voxelizing thread has not locked this object
if (!Monitor.TryEnter(this, 0))
Expand Down Expand Up @@ -373,8 +375,8 @@ public bool TryVoxelUpdate(
_voxel?.CleanupVoxel();

//set flag so that this function can't run again before voxelizing completes and queue voxelizing thread
voxelizing = true;
VoxelizationThreadpool.Instance.QueueVoxelization(CreateVoxel);
Voxelizing = true;
VoxelizationThreadpool.Instance.QueueVoxelization(() => CreateVoxel(vessel));
return true;
}
finally
Expand All @@ -384,14 +386,14 @@ public bool TryVoxelUpdate(
}

//And this actually creates the voxel and then begins the aero properties determination
private void CreateVoxel()
private void CreateVoxel(Vessel vessel = null)
{
lock (this) //lock this object to prevent race with main thread
{
try
{
//Actually voxelize it
_voxel = VehicleVoxel.CreateNewVoxel(_currentGeoModules, _voxelCount);
_voxel = VehicleVoxel.CreateNewVoxel(_currentGeoModules, _voxelCount, vessel: vessel);
if (_vehicleCrossSection.Length < _voxel.MaxArrayLength)
_vehicleCrossSection = _voxel.EmptyCrossSectionArray;

Expand All @@ -408,14 +410,14 @@ private void CreateVoxel()
finally
{
//Always, when we finish up, if we're in flight, cleanup the voxel
if (HighLogic.LoadedSceneIsFlight && _voxel != null)
if (HighLogic.LoadedSceneIsFlight && !VoxelizationSettings.DebugInFlight && _voxel != null)
{
_voxel.CleanupVoxel();
_voxel = null;
}

//And unset the flag so that the main thread can queue it again
voxelizing = false;
Voxelizing = false;
}
}
}
Expand Down Expand Up @@ -1029,8 +1031,9 @@ private void CalculateVesselAeroProperties()
for (int i = front; i <= back; i++)
{
if (double.IsNaN(_vehicleCrossSection[i].area))
ThreadSafeDebugLogger
.Instance.RegisterMessage("FAR VOXEL ERROR: Voxel CrossSection Area is NaN at section " + i);
ThreadSafeDebugLogger.Instance
.RegisterMessage("FAR VOXEL ERROR: Voxel CrossSection Area is NaN at section " +
i);

filledVolume += _vehicleCrossSection[i].area;
}
Expand Down
17 changes: 17 additions & 0 deletions FerramAerospaceResearch/FARGUI/FARFlightGUI/FlightGUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ You should have received a copy of the GNU General Public License
using ferram4;
using FerramAerospaceResearch.FARAeroComponents;
using FerramAerospaceResearch.Resources;
using FerramAerospaceResearch.Settings;
using KSP.IO;
using KSP.Localization;
using StringLeakTest;
Expand Down Expand Up @@ -362,6 +363,7 @@ private void MainFlightGUIWindow(int windowId)
GUIUtils.TextEntryForInt(Localizer.Format("FARFlightGUIFltLogFlushPeriod"),
150,
flightDataLogger.FlushPeriod);
DebugVisualizationGUI();

GUILayout.Label(Localizer.Format("FARFlightGUIFltAssistance"));

Expand All @@ -371,6 +373,21 @@ private void MainFlightGUIWindow(int windowId)
GUI.DragWindow();
}

private void DebugVisualizationGUI()
{
if (!VoxelizationSettings.DebugInFlight)
return;
GUILayout.BeginHorizontal();
GUI.enabled = !_vesselAero.VehicleAero.Voxelizing;
if (GUILayout.Button(Localizer.Format("FARDebugVoxels")))
{
_vesselAero.VehicleAero.DebugVisualizeVoxels(vessel.transform.localToWorldMatrix);
}

GUI.enabled = true;
GUILayout.EndHorizontal();
}

private void FlightDataWindow(int windowId)
{
_flightDataGUI.DataDisplay();
Expand Down
15 changes: 10 additions & 5 deletions FerramAerospaceResearch/FARPartGeometry/VehicleVoxel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,11 @@ public class VehicleVoxel
private int xCellLength, yCellLength, zCellLength;
private int threadsQueued;

private VehicleVoxel()
private VehicleVoxel(Vessel vessel)
{
VoxelizationThreadpool.Instance.RunOnMainThread(() =>
{
voxelMesh = DebugVoxelMesh.Create();
voxelMesh = DebugVoxelMesh.Create(vessel == null ? null : vessel.vesselTransform);
voxelMesh.gameObject.SetActive(false);
});
}
Expand Down Expand Up @@ -152,10 +152,11 @@ public static VehicleVoxel CreateNewVoxel(
List<GeometryPartModule> geoModules,
int elementCount,
bool multiThreaded = true,
bool solidify = true
bool solidify = true,
Vessel vessel = null
)
{
var newVoxel = new VehicleVoxel();
var newVoxel = new VehicleVoxel(vessel);

newVoxel.CreateVoxel(geoModules, elementCount, multiThreaded, solidify);

Expand Down Expand Up @@ -1365,6 +1366,9 @@ public void ClearVisualVoxels()
public void VisualizeVoxel(Matrix4x4 vesselLocalToWorldMatrix)
{
FARLogger.Debug("Creating visual voxels");
// Unity's <from>to<to> matrix naming is confusing, would be better to use <to>from<from> to actually
// correspond with multiplication order...
Matrix4x4 vesselLocalToVoxelMeshMatrix = voxelMesh.transform.worldToLocalMatrix * vesselLocalToWorldMatrix;
var builder = new DebugVoxel.Builder();
var tintMap = new PartTint();
voxelMesh.Clear(builder, xLength * yLength * zLength * 128, false);
Expand All @@ -1373,7 +1377,8 @@ public void VisualizeVoxel(Matrix4x4 vesselLocalToWorldMatrix)
for (int j = 0; j < yLength; j++)
{
for (int k = 0; k < zLength; k++)
voxelChunks[i, j, k]?.VisualizeVoxels(vesselLocalToWorldMatrix, tintMap, voxelMesh, builder);
voxelChunks[i, j, k]
?.VisualizeVoxels(vesselLocalToVoxelMeshMatrix, tintMap, voxelMesh, builder);
}
}

Expand Down
6 changes: 4 additions & 2 deletions FerramAerospaceResearch/Settings/VoxelizationSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ namespace FerramAerospaceResearch.Settings
[ConfigNode("ColorMap")]
public class ColorMap
{
[ConfigValueIgnore]
public static readonly ColorMap Default = new ColorMap
[ConfigValueIgnore] public static readonly ColorMap Default = new ColorMap
{
Name = "default",
Colors = {new Color(0.18f, 0f, 0.106f)}
Expand Down Expand Up @@ -41,10 +40,13 @@ public static class VoxelizationSettings

[ConfigValue("ColorMap")] public static List<ColorMap> ColorMaps { get; } = new List<ColorMap>();

[ConfigValue("debugInFlight")] public static bool DebugInFlight { get; set; } = false;

public static ColorMap FirstOrDefault()
{
return FirstOrDefault(Default);
}

public static ColorMap FirstOrDefault(string name)
{
if (string.IsNullOrEmpty(name))
Expand Down
3 changes: 3 additions & 0 deletions GameData/FerramAerospaceResearch/FARConfig.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ FARConfig
}

Voxelization {
// enable debug voxels in flight mode
debugInFlight = false

// check out colorcet (Python) for generating more maps
default = glasbey_bw

Expand Down

0 comments on commit 5560079

Please sign in to comment.