Skip to content

Commit

Permalink
Fix DepthMasks whiting out windows in LDR
Browse files Browse the repository at this point in the history
  • Loading branch information
LGhassen committed Aug 26, 2024
1 parent 5d358db commit ed34eba
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 4 deletions.
12 changes: 9 additions & 3 deletions DeferredKSP/Deferred.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using System;
using UnityEngine.SceneManagement;

[assembly: AssemblyVersion("1.2.1")]
[assembly: AssemblyVersion("1.2.2")]
[assembly: KSPAssemblyDependency("0Harmony", 0, 0)]
[assembly: KSPAssemblyDependency("Shabby", 0, 0)]
namespace Deferred
Expand All @@ -19,7 +19,7 @@ public class Deferred : MonoBehaviour

void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{
SetupCustomReflectionsAndAmbient(settings);
SetupCustomLightingAndAmbientShaders(settings);

HandleCameras();

Expand Down Expand Up @@ -142,8 +142,14 @@ private void EnableDeferredShadingOnCamera(Camera camera)
}
}

private static void SetupCustomReflectionsAndAmbient(Settings settings)
private static void SetupCustomLightingAndAmbientShaders(Settings settings)
{
// Modified final pass shader for depthMask compatibility in LDR
GraphicsSettings.SetShaderMode(BuiltinShaderType.DeferredShading, BuiltinShaderMode.UseCustom);
GraphicsSettings.SetCustomShader(BuiltinShaderType.DeferredShading,
ShaderLoader.DeferredShaders["Deferred/Internal-DeferredShading"]);

// Modified reflections pass that also does ambient (so that it's truly deferred)
GraphicsSettings.SetShaderMode(BuiltinShaderType.DeferredReflections, BuiltinShaderMode.UseCustom);
GraphicsSettings.SetCustomShader(BuiltinShaderType.DeferredReflections,
ShaderLoader.DeferredShaders["Deferred/Internal-DeferredReflections"]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ Pass
ZTest Always
Blend [_SrcBlend] [_DstBlend]

ColorMask RGB // Do not overwrite emission alpha channel flagging actual surfaces from depthMasks in LDR

CGPROGRAM
#pragma target 3.0
#pragma vertex vert
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
// Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt)
//
// Modified to make depth masks work in LDR. In LDR stencil is used to check for non-background objects,
// if a depthMask is on an IVA window it will write to the non-background bit which Unity doesn't give
// control over (only a few stencil bits are actually exposed) messing up the background.
// To get around this,check using the unused two-bit alpha channel of the emission buffer in LDR.
// Emission alpha gets initialized to 0.0 but overriden to 1.0 by any "normal" shader (but not depthMask)

Shader "Deferred/Internal-DeferredShading" {
Properties {
_LightTexture0 ("", any) = "" {}
_LightTextureB0 ("", 2D) = "" {}
_ShadowMapTexture ("", any) = "" {}
_SrcBlend ("", Float) = 1
_DstBlend ("", Float) = 1
}
SubShader {

// Pass 1: Lighting pass
// LDR case - Lighting encoded into a subtractive ARGB8 buffer
// HDR case - Lighting additively blended into floating point buffer
Pass {
ZWrite Off
Blend [_SrcBlend] [_DstBlend]

ColorMask RGB // Do not overwrite emission alpha channel flagging actual surfaces

CGPROGRAM
#pragma target 3.0
#pragma vertex vert_deferred
#pragma fragment frag
#pragma multi_compile_lightpass
#pragma multi_compile ___ UNITY_HDR_ON

#pragma exclude_renderers nomrt

#include "UnityCG.cginc"
#include "UnityDeferredLibrary.cginc"
#include "UnityPBSLighting.cginc"
#include "UnityStandardUtils.cginc"
#include "UnityGBuffer.cginc"
#include "UnityStandardBRDF.cginc"

sampler2D _CameraGBufferTexture0;
sampler2D _CameraGBufferTexture1;
sampler2D _CameraGBufferTexture2;

half4 CalculateLight (unity_v2f_deferred i)
{
float3 wpos;
float2 uv;
float atten, fadeDist;
UnityLight light;
UNITY_INITIALIZE_OUTPUT(UnityLight, light);
UnityDeferredCalculateLightParams (i, wpos, uv, light.dir, atten, fadeDist);

light.color = _LightColor.rgb * atten;

// unpack Gbuffer
half4 gbuffer0 = tex2D (_CameraGBufferTexture0, uv);
half4 gbuffer1 = tex2D (_CameraGBufferTexture1, uv);
half4 gbuffer2 = tex2D (_CameraGBufferTexture2, uv);
UnityStandardData data = UnityStandardDataFromGbuffer(gbuffer0, gbuffer1, gbuffer2);

float3 eyeVec = normalize(wpos-_WorldSpaceCameraPos);
half oneMinusReflectivity = 1 - SpecularStrength(data.specularColor.rgb);

UnityIndirect ind;
UNITY_INITIALIZE_OUTPUT(UnityIndirect, ind);
ind.diffuse = 0;
ind.specular = 0;

half4 res = UNITY_BRDF_PBS (data.diffuseColor, data.specularColor, oneMinusReflectivity, data.smoothness, data.normalWorld, -eyeVec, light, ind);

return res;
}

#ifdef UNITY_HDR_ON
half4
#else
fixed4
#endif
frag (unity_v2f_deferred i) : SV_Target
{
half4 c = CalculateLight(i);
#ifdef UNITY_HDR_ON
return c;
#else
return exp2(-c);
#endif
}

ENDCG
}


// Pass 2: Final decode pass.
// Used only with HDR off, to decode the logarithmic buffer into the main RT
Pass {
ZTest Always Cull Off ZWrite Off

Stencil {
ref [_StencilNonBackground]
readmask [_StencilNonBackground]
// Normally just comp would be sufficient, but there's a bug and only front face stencil state is set (case 583207)
compback equal
compfront equal
}


CGPROGRAM
#pragma target 3.0
#pragma vertex vert
#pragma fragment frag
#pragma exclude_renderers nomrt

#include "UnityCG.cginc"

sampler2D _LightBuffer;
struct v2f {
float4 vertex : SV_POSITION;
float2 texcoord : TEXCOORD0;
};

v2f vert (float4 vertex : POSITION, float2 texcoord : TEXCOORD0)
{
v2f o;
o.vertex = UnityObjectToClipPos(vertex);
o.texcoord = texcoord.xy;
#ifdef UNITY_SINGLE_PASS_STEREO
o.texcoord = TransformStereoScreenSpaceTex(o.texcoord, 1.0f);
#endif
return o;
}

fixed4 frag (v2f i) : SV_Target
{
float4 encodedLight = tex2D(_LightBuffer, i.texcoord);

if (encodedLight.a == 0.0) // Identify depthMasks only since we can't rely on stencil
discard;

return float4(-log2(encodedLight.rgb), 0.0);
}
ENDCG
}

}
Fallback Off
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ Shader "DepthMask" {
ColorMask 0
Pass {}
}
}
}

0 comments on commit ed34eba

Please sign in to comment.