Skip to content

Commit

Permalink
Fix #377: set renderqueue value of current part higher than depth mas…
Browse files Browse the repository at this point in the history
…k to prevent artifacts around windows
  • Loading branch information
JonnyOThan committed Oct 26, 2024
1 parent 075423f commit a7506b6
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
21 changes: 20 additions & 1 deletion FreeIva/FreeIva.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ public void Start()
#endif

Settings.LoadSettings();
SetRenderQueues(FlightGlobals.ActiveVessel.rootPart);

Physics.IgnoreLayerCollision((int)Layers.Kerbals, (int)Layers.InternalSpace);
Physics.IgnoreLayerCollision((int)Layers.Kerbals, (int)Layers.Kerbals, false);
Expand Down Expand Up @@ -463,6 +462,23 @@ public static void GetInternalModulesBoundingCamera(Part p, List<InternalModuleF
Profiler.EndSample();
}

static void SetModelRenderQueue(InternalModuleFreeIva internalModule, int fromRenderQueue, int toRenderQueue)
{
if (internalModule == null) return;
var internalModel = internalModule.internalModel;
if (internalModel == null) return;
var modelTransform = internalModel.transform.Find("model");
if (modelTransform == null) return;

foreach (var renderer in modelTransform.GetComponentsInChildren<Renderer>())
{
if (renderer.material.renderQueue == fromRenderQueue)
{
renderer.material.renderQueue = toRenderQueue;
}
}
}

public static void SetCurrentPart(InternalModel newModel)
{
var newModule = InternalModuleFreeIva.GetForModel(newModel);
Expand All @@ -474,6 +490,9 @@ public static void SetCurrentPart(InternalModel newModel)

if (FreeIva.CurrentInternalModuleFreeIva != newModule)
{
SetModelRenderQueue(FreeIva.CurrentInternalModuleFreeIva, InternalModuleFreeIva.CURRENT_PART_RENDER_QUEUE, InternalModuleFreeIva.OPAQUE_RENDER_QUEUE);
SetModelRenderQueue(newModule, InternalModuleFreeIva.OPAQUE_RENDER_QUEUE, InternalModuleFreeIva.CURRENT_PART_RENDER_QUEUE);

CurrentInternalModuleFreeIva = newModule;
CameraManager.Instance.activeInternalPart = CurrentPart;
}
Expand Down
11 changes: 6 additions & 5 deletions FreeIva/InternalModules/InternalModuleFreeIva.cs
Original file line number Diff line number Diff line change
Expand Up @@ -415,22 +415,23 @@ private static void FindShaders()

// transparents are normally 3000, opaque geometry is 2000
// we need something that will render before opaque geometry so that it writes to the z-buffer early and prevents other internals from drawing behind it
public static readonly int WINDOW_RENDER_QUEUE = 1999;
public const int CURRENT_PART_RENDER_QUEUE = 999;
public const int WINDOW_RENDER_QUEUE = 1999;
public const int OPAQUE_RENDER_QUEUE = 2000;

private void OnLoad_WindowRenderer(MeshRenderer meshRenderer)
{
// TODO: should we be using sharedMaterial in here instead?
meshRenderer.material.renderQueue = WINDOW_RENDER_QUEUE;
meshRenderer.sharedMaterial.renderQueue = WINDOW_RENDER_QUEUE;

// if deferred rendering is active, transparencies are always drawn after opaque geometry regardless of renderqueue
// so we need to add a depth mask material to this mesh which will draw before the opaque geometry to mask it out
if (x_deferredInstalled)
{
var materials = meshRenderer.materials;
var materials = meshRenderer.sharedMaterials;
int lastIndex = materials.Length;
Array.Resize(ref materials, lastIndex + 1);
materials[lastIndex] = Utils.GetDepthMaskCullingMaterial();
meshRenderer.materials = materials;
meshRenderer.sharedMaterials = materials;
}
}

Expand Down
2 changes: 0 additions & 2 deletions FreeIva/KerbalIvaAddon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ public void Update()

FreeIva.EnableInternals();
UpdateActiveKerbal();
FreeIva.SetRenderQueues(FreeIva.CurrentPart);

var freeIvaModule = FreeIva.CurrentPart.GetModule<ModuleFreeIva>();
if (freeIvaModule != null && freeIvaModule.allowsUnbuckling)
Expand Down Expand Up @@ -719,7 +718,6 @@ public void Unbuckle(bool feedbackEnabled = true)
}

FreeIva.EnableInternals();
FreeIva.SetRenderQueues(FreeIva.CurrentPart);
OriginalSeat = ActiveKerbal.seat;

Gravity = Gravity && Settings.EnableCollisions;
Expand Down

0 comments on commit a7506b6

Please sign in to comment.