- Home /
Disabling AA Changes how SS Effects Work
Hello unity answers community!
I'm currently trying to figure out why, when I disable Anti-aliasing in my quality settings the way in which my screen space effect works changes.
The intent of my screen space effect is to apply an additive colour after rendering is done as an overlay. Depending on what is happening during the game this colour shifts and changes. The screen space effect uses Graphics.Blit() in a script attached to the main game camera using in the OnRenderImage(RenderTexture source, RenderTexture destination) function as prescribed in the unity documentation.
I've managed to isolate the issue such that it is reproducible in a separate scene using just a cube, a camera and the screen space effect. Here is the shader code:
Shader "my screenspace shader" {
Properties {
_Color ("Main Color", Color) = (1,1,1,1)
}
SubShader {
Tags {"Queue"="Geometry" "IgnoreProjector"="True" "RenderType"="Transparent"}
pass {
ZTest Off
Blend DstColor Zero, SrcAlpha Zero
Cull Off Lighting Off ZWrite Off
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma fragmentoption ARB_precision_hint_fastest
#include "UnityCG.cginc"
half4 _Color;
struct v2f {
float4 vertex : POSITION;
float2 texcoord : TEXCOORD0;
};
v2f vert(appdata_base i)
{
v2f vout;
vout.vertex = mul(UNITY_MATRIX_MVP, i.vertex);
vout.texcoord = i.texcoord;
return vout;
}
half4 frag(v2f vout) : COLOR
{
half4 ret = _Color + half4(1 - _Color.a, 1 - _Color.a, 1 - _Color.a, 1 - _Color.a);
return ret;
}
ENDCG
}
}
}
When I have AA enabled, the effect overlays the colour additively as expected. However, when I disabled AA in the quality settings the screen becomes whichever colour is set as _Color in the shader. In addition to this, if I have any other screen space effect after this shader, things become really strange (black screens) even with AA on.
The shader is incredibly simple, and I am pulling my hair out over how disabling AA can cause it to behave so much differently. If someone out there has any knowledge which could explain the issue or to solve it, I'd be very happy to hear it.
Thanks!
Answer by GeraldOBBI · May 13, 2013 at 04:39 PM
Well, I finally figured it out. Turns out that there's a bug in Unity's rendering engine where this shader combines its output with the render texture when AA is on. It's true behaviour comes out when AA is disabled, causing a solid colour to be drawn. I had forgotten to add the _MainTex property so that Graphics.Blit() can set the camera's render texture to its _MainTex property. The correct shader can be found below:
Shader "my screenspace shader"
{
Properties
{
_Color ("Main Color", Color) = (1,1,1,1)
_MainTex ("Render Texture", 2D) = "white" {}
}
SubShader
{
Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
pass
{
ZTest Off
Cull Off Lighting Off ZWrite Off
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma fragmentoption ARB_precision_hint_fastest
#include "UnityCG.cginc"
half4 _Color;
sampler2D _MainTex;
struct v2f {
float4 vertex : SV_POSITION;
float2 texcoord : TEXCOORD0;
};
v2f vert(appdata_base i)
{
v2f vout;
vout.vertex = mul(UNITY_MATRIX_MVP, i.vertex);
vout.vertex.z = 0;
vout.texcoord = i.texcoord.xy;
return vout;
}
half4 frag(v2f vout) : COLOR
{
half4 renderColour = tex2D(_MainTex, vout.texcoord);
half4 ret = lerp(renderColour, renderColour * (_Color + fixed4(1 - _Color.a, 1 - _Color.a, 1 - _Color.a, 1 - _Color.a)), _Color.a);
return ret;
}
ENDCG
}
}
}