Skip to content

Commit

Permalink
Fix #425: prevent internals from rendering through windows with deferred
Browse files Browse the repository at this point in the history
-still needs some polish though
  • Loading branch information
JonnyOThan committed Sep 22, 2024
1 parent b953914 commit 552ab2b
Showing 1 changed file with 33 additions and 8 deletions.
41 changes: 33 additions & 8 deletions FreeIva/InternalModules/InternalModuleFreeIva.cs
Original file line number Diff line number Diff line change
Expand Up @@ -389,13 +389,9 @@ private void OnLoad_DepthMasks()
static Dictionary<Shader, Shader> x_windowShaderTranslations = null;
static Shader[] x_windowShaders = null;

// 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;

private void OnLoad_Windows(ConfigNode node)
private static void FindShaders()
{
bool hasWindows = false;
// NOTE: this a separate function because Shabby will transpile any function that contains Shader.Find, so pulling it out here improves debugging

if (x_windowShaderTranslations == null)
{
Expand All @@ -415,7 +411,33 @@ private void OnLoad_Windows(ConfigNode node)
Shader.Find("KSP/Alpha/Unlit Transparent"),
};
}
}

// 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;

private void OnLoad_WindowRenderer(MeshRenderer meshRenderer)
{
// TODO: should we be using sharedMaterial in here instead?
meshRenderer.material.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
var materials = meshRenderer.materials;
int lastIndex = materials.Length;
Array.Resize(ref materials, lastIndex + 1);
materials[lastIndex] = Utils.GetDepthMaskCullingMaterial();
meshRenderer.materials = materials;
}

private void OnLoad_Windows(ConfigNode node)
{
bool hasWindows = false;

FindShaders();

// handle explicit window transforms
var windowNames = node.GetValues("windowName");
foreach (var windowName in windowNames)
{
Expand All @@ -424,12 +446,15 @@ private void OnLoad_Windows(ConfigNode node)
{
foreach (var meshRenderer in windowTransform.GetComponentsInChildren<MeshRenderer>())
{
// TODO: should we be using sharedMaterial in here instead?

// do shader replacements to ensure z-write
if (x_windowShaderTranslations.TryGetValue(meshRenderer.material.shader, out Shader newShader))
{
meshRenderer.material.shader = newShader;
}

meshRenderer.material.renderQueue = WINDOW_RENDER_QUEUE;
OnLoad_WindowRenderer(meshRenderer);
}

hasWindows = true;
Expand All @@ -446,7 +471,7 @@ private void OnLoad_Windows(ConfigNode node)
if (x_windowShaders.Contains(meshRenderer.material.shader))
{
hasWindows = true;
meshRenderer.material.renderQueue = WINDOW_RENDER_QUEUE;
OnLoad_WindowRenderer(meshRenderer);
meshRenderer.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
Debug.Log($"[FreeIva] INTERNAL '{internalModel.internalName}' auto-detected window transform '{meshRenderer.transform.name}'");
}
Expand Down

0 comments on commit 552ab2b

Please sign in to comment.