Hey Guys,
I am trying to achieve a gradient color background for my 2d mobile game, like this →
Energy: Anti Stress loops android game
I tried 12 pixel image and set it to bilinear filter mode and stretch it to get the gradient. But I am getting gradient banding issue when playing it in my mobile (116 pixel image also tried).
So I went for gradient shaders and tried gradient with noise shaders too from the below links, but the result looks same.
https://startupfreakgame.com/2017/01/15/screen-space-gradient-shader-with-dithering-in-unity/
https://www.reddit.com/r/Unity3D/comments/e5wmbn/smooth_gradient_shader_using_noise_link_in_comment/
Here is the current shader code I am using:
Shader "Custom/ScreenGradient"
{
Properties
{
_MainTex("Texture", 2D) = "white" {}
_NoiseTex("Noise Texture", 2D) = "white" {}
_BottomColor("Bottom Color", Color) = (1,1,1,1)
_TopColor("Top Color", Color) = (1,1,1,1)
_Offset("Offset", float) = 0
}
SubShader
{
Tags
{
"Queue" = "Transparent"
"IgnoreProjector" = "True"
"RenderType" = "Transparent"
"PreviewType" = "Plane"
"CanUseSpriteAtlas" = "True"
}
Cull Off
Lighting Off
Blend One OneMinusSrcAlpha
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata_t
{
float4 position : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float4 position : SV_POSITION;
float2 uv : TEXCOORD0;
half4 color : COLOR;
};
sampler2D _MainTex;
sampler2D _NoiseTex;
float4 _MainTex_ST;
half4 _BottomColor;
half4 _TopColor;
float _Offset;
v2f vert(appdata_t IN)
{
v2f OUT;
OUT.position = UnityObjectToClipPos(IN.position);
OUT.uv = TRANSFORM_TEX(IN.uv, _MainTex);
float factor = mad(OUT.position.y, -0.5, 0.5);
factor *= 1 + _Offset*2;
factor -= _Offset;
factor = clamp(factor, 0, 1);
//factor = smoothstep(0.0f , 1.0f, factor);
OUT.color = lerp(_BottomColor, _TopColor, factor);
//OUT.color = smoothstep(0.0f, 1.0f, OUT.color);
return OUT;
}
float3 getNoise(float2 uv)
{
/*float3 noise = tex2D(_NoiseTex, uv * 100 + _Time * 50);
noise = mad(noise, 2.0, -0.5);
return noise/255;*/
float2 scale = 10;
return tex2D(_NoiseTex, uv * float4(10.0f, 10.0f, 0.0f, 0.0f)) / 200.0f;
}
half4 frag(v2f IN) : SV_Target
{
half4 texCol = tex2D(_MainTex, IN.uv);
half4 c;
c.rgb = IN.color.rgb + getNoise(IN.uv);
c.rgb *= texCol.a;
c.a = texCol.a;
return c;
}
ENDCG
}
}
}
Using a blue noise texture from here:
http://momentsingraphics.de/Media/BlueNoise/FreeBlueNoiseTextures.zip
Result:
URP gradient shader graph method also tried. I want the background looks exactly same like the Energy loops game. I see there are some noise texture also they have included in the background but the gradient banding is not visible in their game in my mobile. I am a noob in shaders, any help would be much appreciated.
Thanks