- Home /
Vignette shader hard edges
When I tried the vignette effect, it gave me the hard edges you can see in the picture below. I gave a shot at doing my own shader for it, and even if I'm able to make them more sublte, it isn't quite as smooth as I'd like. I know it's due to the gap between the 256 levels of grayscale, but i'd like to know if there's any workaround for this.
The screenshot below is the camera facing a gray plane. I included my shader code below.
Shader "test/VignettingShader" {
Properties {
_MainTex ("Base", 2D) = "white" {}
}
CGINCLUDE
#include "UnityCG.cginc"
ENDCG
Subshader {
Pass {
ZTest Always Cull Off ZWrite Off
Fog { Mode off }
CGPROGRAM
#pragma fragmentoption ARB_precision_hint_fastest
#pragma vertex vert
#pragma fragment frag
struct v2f {
float4 pos : POSITION;
float2 uv : TEXCOORD0;
};
sampler2D _MainTex;
v2f vert( appdata_img v ) {
v2f o;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
o.uv = v.texcoord.xy;
return o;
}
half4 frag(v2f i) : COLOR {
float2 texCoord=i.uv;
// Take texture color
half4 color = tex2D (_MainTex, i.uv);
//vignette effect
texCoord -= 0.5;
float vignette = 1.0 - pow(dot(texCoord, texCoord),1);
vignette = pow(vignette,5);//determine vignette amount
color*=vignette;
return color;
}
ENDCG
}
}
Fallback off
}
Answer by ScroodgeM · Aug 25, 2012 at 07:19 PM
these hard edges are not shader or calculations limit. this is a screen limit. 99% of world screens works with 32bit color, 8 bit per color channel. so you can't increase this 1/256 color level step.
but... you can do something 8)
replace
return color;
with
return color + float4(0, 0.0013, 0.0026, 0);
this will smooth color step
(50,50,50) -> (51,51,51)
to something like
(50,50,50) -> (51,50,50) -> (51,51,50) -> (51,51,51)
some type of fake, but looks triple smoothly 8)
Thanks, it gives a small tint but barely noticeable, clever solution anyway.