Skip to content
This repository was archived by the owner on Nov 30, 2020. It is now read-only.

Commit a68c91b

Browse files
committed
Added a "keep alpha" mode to FXAA
1 parent aee3478 commit a68c91b

6 files changed

Lines changed: 43 additions & 11 deletions

File tree

PostProcessing/Editor/PostProcessLayerEditor.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public sealed class PostProcessLayerEditor : BaseEditor<PostProcessLayer>
2424
SerializedProperty m_TaaStationaryBlending;
2525
SerializedProperty m_TaaMotionBlending;
2626
SerializedProperty m_FxaaMobileOptimized;
27+
SerializedProperty m_FxaaKeepAlpha;
2728

2829
SerializedProperty m_AOEnabled;
2930
SerializedProperty m_AOIntensity;
@@ -68,6 +69,7 @@ void OnEnable()
6869
m_TaaStationaryBlending = FindProperty(x => x.temporalAntialiasing.stationaryBlending);
6970
m_TaaMotionBlending = FindProperty(x => x.temporalAntialiasing.motionBlending);
7071
m_FxaaMobileOptimized = FindProperty(x => x.fastApproximateAntialiasing.mobileOptimized);
72+
m_FxaaKeepAlpha = FindProperty(x => x.fastApproximateAntialiasing.keepAlpha);
7173

7274
m_AOEnabled = FindProperty(x => x.ambientOcclusion.enabled);
7375
m_AOIntensity = FindProperty(x => x.ambientOcclusion.intensity);
@@ -203,6 +205,7 @@ void DoAntialiasing()
203205
else if (m_AntialiasingMode.intValue == (int)PostProcessLayer.Antialiasing.FastApproximateAntialiasing)
204206
{
205207
EditorGUILayout.PropertyField(m_FxaaMobileOptimized);
208+
EditorGUILayout.PropertyField(m_FxaaKeepAlpha);
206209
}
207210
}
208211
EditorGUI.indentLevel--;

PostProcessing/Runtime/Effects/FastApproximateAntialiasing.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,8 @@ public sealed class FastApproximateAntialiasing
77
{
88
[Tooltip("Boost performances by lowering the effect quality. This settings is meant to be used on mobile and other low-end platforms.")]
99
public bool mobileOptimized = false;
10+
11+
[Tooltip("Keep alpha channel. This will slightly lower the effect quality but allows rendering against a transparent background.")]
12+
public bool keepAlpha = false;
1013
}
1114
}

PostProcessing/Runtime/PostProcessLayer.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,10 @@ int RenderBuiltins(PostProcessRenderContext context, bool isFinalPass, int relea
684684
tempTarget = m_TargetPool.Get();
685685
cmd.GetTemporaryRT(tempTarget, context.width, context.height, 24, FilterMode.Bilinear, context.sourceFormat);
686686
context.destination = tempTarget;
687+
688+
// Handle FXAA's keep alpha mode
689+
if (antialiasingMode == Antialiasing.FastApproximateAntialiasing && !fastApproximateAntialiasing.keepAlpha)
690+
uberSheet.properties.SetFloat(ShaderIDs.LumaInAlpha, 1f);
687691
}
688692

689693
// Depth of field final combination pass used to be done in Uber which led to artifacts
@@ -761,6 +765,9 @@ void RenderFinalPass(PostProcessRenderContext context, int releaseTargetAfterUse
761765
? "FXAA_LOW"
762766
: "FXAA"
763767
);
768+
769+
if (fastApproximateAntialiasing.keepAlpha)
770+
uberSheet.EnableKeyword("FXAA_KEEP_ALPHA");
764771
}
765772
else if (antialiasingMode == Antialiasing.SubpixelMorphologicalAntialiasing && subpixelMorphologicalAntialiasing.IsSupported())
766773
{

PostProcessing/Runtime/Utils/ShaderIDs.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ static class ShaderIDs
9393
internal static readonly int GrainTex = Shader.PropertyToID("_GrainTex");
9494
internal static readonly int Phase = Shader.PropertyToID("_Phase");
9595

96+
internal static readonly int LumaInAlpha = Shader.PropertyToID("_LumaInAlpha");
97+
9698
internal static readonly int DitheringTex = Shader.PropertyToID("_DitheringTex");
9799
internal static readonly int Dithering_Coords = Shader.PropertyToID("_Dithering_Coords");
98100

PostProcessing/Shaders/Builtins/FinalPass.shader

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,21 @@ Shader "Hidden/PostProcessing/FinalPass"
44

55
#pragma multi_compile __ UNITY_COLORSPACE_GAMMA
66
#pragma multi_compile __ FXAA FXAA_LOW
7+
#pragma multi_compile __ FXAA_KEEP_ALPHA
78
#include "../StdLib.hlsl"
89
#include "../Colors.hlsl"
910
#include "Dithering.hlsl"
1011

1112
// PS3 and XBOX360 aren't supported in Unity anymore, only use the PC variant
1213
#define FXAA_PC 1
1314

14-
// Luma is encoded in alpha after the first Uber pass
15-
#define FXAA_GREEN_AS_LUMA 0
15+
#if FXAA_KEEP_ALPHA
16+
// Luma hasn't been encoded in alpha
17+
#define FXAA_GREEN_AS_LUMA 1
18+
#else
19+
// Luma is encoded in alpha after the first Uber pass
20+
#define FXAA_GREEN_AS_LUMA 0
21+
#endif
1622

1723
#if FXAA_LOW
1824
#define FXAA_QUALITY__PRESET 28
@@ -65,6 +71,12 @@ Shader "Hidden/PostProcessing/FinalPass"
6571
0.0, // fxaaConsoleEdgeThresholdMin (unused)
6672
0.0 // fxaaConsole360ConstDir (unused)
6773
);
74+
75+
#if FXAA_KEEP_ALPHA
76+
{
77+
color.a = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, uvSPR).a;
78+
}
79+
#endif
6880
}
6981
#else
7082
{

PostProcessing/Shaders/Builtins/Uber.shader

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ Shader "Hidden/PostProcessing/Uber"
6464
half2 _Grain_Params1; // x: lum_contrib, y: intensity
6565
float4 _Grain_Params2; // x: xscale, h: yscale, z: xoffset, w: yoffset
6666

67+
// Misc
68+
half _LumaInAlpha;
69+
6770
half4 FragUber(VaryingsDefault i) : SV_Target
6871
{
6972
float2 uv = i.texcoord;
@@ -170,8 +173,8 @@ Shader "Hidden/PostProcessing/Uber"
170173
}
171174
#endif
172175

173-
half3 new_color = color * lerp(_Vignette_Color, (1.0).xxx, vfactor);
174-
color.rgb = lerp(color, new_color, _Vignette_Opacity);
176+
half3 new_color = color.rgb * lerp(_Vignette_Color, (1.0).xxx, vfactor);
177+
color.rgb = lerp(color.rgb, new_color, _Vignette_Opacity);
175178
color.a = lerp(1.0, color.a, vfactor);
176179
}
177180
}
@@ -202,19 +205,21 @@ Shader "Hidden/PostProcessing/Uber"
202205
}
203206
#endif
204207

205-
half4 output;
208+
half4 output = color;
206209

207210
#if FINALPASS
208211
{
209-
color.rgb = Dither(color.rgb, i.texcoord);
210-
output = color;
212+
output.rgb = Dither(output.rgb, i.texcoord);
211213
}
212214
#else
213215
{
214-
// Put saturated luma in alpha for FXAA - higher quality than "green as luma" and
215-
// necessary as RGB values will potentially still be HDR for the FXAA pass
216-
half luma = Luminance(saturate(color));
217-
output = half4(color.rgb, luma);
216+
if (_LumaInAlpha > 0.5)
217+
{
218+
// Put saturated luma in alpha for FXAA - higher quality than "green as luma" and
219+
// necessary as RGB values will potentially still be HDR for the FXAA pass
220+
half luma = Luminance(saturate(output));
221+
output.a = luma;
222+
}
218223
}
219224
#endif
220225

0 commit comments

Comments
 (0)