From 5560079e6682e762dfff53ee4422979b90998b69 Mon Sep 17 00:00:00 2001 From: dkavolis <12998363+dkavolis@users.noreply.github.com> Date: Tue, 24 Nov 2020 18:32:52 +0000 Subject: [PATCH] enable debug voxels in flight --- .../FARAeroComponents/FARVesselAero.cs | 8 +++++- .../FARAeroComponents/VehicleAerodynamics.cs | 25 +++++++++++-------- .../FARGUI/FARFlightGUI/FlightGUI.cs | 17 +++++++++++++ .../FARPartGeometry/VehicleVoxel.cs | 15 +++++++---- .../Settings/VoxelizationSettings.cs | 6 +++-- .../FerramAerospaceResearch/FARConfig.cfg | 3 +++ 6 files changed, 55 insertions(+), 19 deletions(-) diff --git a/FerramAerospaceResearch/FARAeroComponents/FARVesselAero.cs b/FerramAerospaceResearch/FARAeroComponents/FARVesselAero.cs index 00923e357..300b56cf0 100644 --- a/FerramAerospaceResearch/FARAeroComponents/FARVesselAero.cs +++ b/FerramAerospaceResearch/FARAeroComponents/FARVesselAero.cs @@ -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; } @@ -447,7 +452,8 @@ public void VesselUpdate(bool recalcGeoModules) _voxelCount, vessel.Parts, _currentGeoModules, - !setup)) + !setup, + vessel)) { _updateRateLimiter = FARSettingsScenarioModule.VoxelSettings.minPhysTicksPerUpdate - 2; _updateQueued = true; diff --git a/FerramAerospaceResearch/FARAeroComponents/VehicleAerodynamics.cs b/FerramAerospaceResearch/FARAeroComponents/VehicleAerodynamics.cs index 2916ab562..c539c0249 100644 --- a/FerramAerospaceResearch/FARAeroComponents/VehicleAerodynamics.cs +++ b/FerramAerospaceResearch/FARAeroComponents/VehicleAerodynamics.cs @@ -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 @@ -105,7 +106,7 @@ internal class VehicleAerodynamics private int firstSection; private bool visualizing; - private bool voxelizing; + public bool Voxelizing { get; private set; } public VehicleAerodynamics() { @@ -335,12 +336,13 @@ public bool TryVoxelUpdate( int voxelCount, List vehiclePartList, List 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)) @@ -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 @@ -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; @@ -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; } } } @@ -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; } diff --git a/FerramAerospaceResearch/FARGUI/FARFlightGUI/FlightGUI.cs b/FerramAerospaceResearch/FARGUI/FARFlightGUI/FlightGUI.cs index ed8abe398..165dcd0d7 100644 --- a/FerramAerospaceResearch/FARGUI/FARFlightGUI/FlightGUI.cs +++ b/FerramAerospaceResearch/FARGUI/FARFlightGUI/FlightGUI.cs @@ -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; @@ -362,6 +363,7 @@ private void MainFlightGUIWindow(int windowId) GUIUtils.TextEntryForInt(Localizer.Format("FARFlightGUIFltLogFlushPeriod"), 150, flightDataLogger.FlushPeriod); + DebugVisualizationGUI(); GUILayout.Label(Localizer.Format("FARFlightGUIFltAssistance")); @@ -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(); diff --git a/FerramAerospaceResearch/FARPartGeometry/VehicleVoxel.cs b/FerramAerospaceResearch/FARPartGeometry/VehicleVoxel.cs index 4340888bb..5cded647a 100644 --- a/FerramAerospaceResearch/FARPartGeometry/VehicleVoxel.cs +++ b/FerramAerospaceResearch/FARPartGeometry/VehicleVoxel.cs @@ -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); }); } @@ -152,10 +152,11 @@ public static VehicleVoxel CreateNewVoxel( List 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); @@ -1365,6 +1366,9 @@ public void ClearVisualVoxels() public void VisualizeVoxel(Matrix4x4 vesselLocalToWorldMatrix) { FARLogger.Debug("Creating visual voxels"); + // Unity's to matrix naming is confusing, would be better to use 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); @@ -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); } } diff --git a/FerramAerospaceResearch/Settings/VoxelizationSettings.cs b/FerramAerospaceResearch/Settings/VoxelizationSettings.cs index d2bee4383..a7d4d914a 100644 --- a/FerramAerospaceResearch/Settings/VoxelizationSettings.cs +++ b/FerramAerospaceResearch/Settings/VoxelizationSettings.cs @@ -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)} @@ -41,10 +40,13 @@ public static class VoxelizationSettings [ConfigValue("ColorMap")] public static List ColorMaps { get; } = new List(); + [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)) diff --git a/GameData/FerramAerospaceResearch/FARConfig.cfg b/GameData/FerramAerospaceResearch/FARConfig.cfg index f3f91f834..652fef2d1 100644 --- a/GameData/FerramAerospaceResearch/FARConfig.cfg +++ b/GameData/FerramAerospaceResearch/FARConfig.cfg @@ -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