Trying to add dither to shader with vertex color
I attempted to write an unlit shader that has vertex color using dither to deal with color banding issues. I followed this tutorial: https://startupfreakgame.com/2017/01/15/screen-space-gradient-shader-with-dithering-in-unity/
Here is the code I came up with:
Shader "Unlit/TEST"
{
Properties
{
_Color ("Main Color", Color) = (0.5,0.5,0.5,1)
_NoiseTex ("Noise Texture", 2D) = "white" {}
}
Subshader
{
Tags { "RenderType"="Opaque" }
Pass
{
Tags { "LightMode" = "Always" }
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma fragmentoption ARB_precision_hint_fastest
#include "UnityCG.cginc"
struct appdata {
float4 vertex : POSITION;
fixed4 color : COLOR;
float3 normal : NORMAL;
};
struct v2f
{
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
fixed4 color : COLOR;
};
v2f vert (appdata v)
{
v2f o;
o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
o.color = v.color;
return o;
}
uniform float4 _Color;
uniform sampler2D _NoiseTex;
float3 getNoise(float2 uv)
{
float3 noise = tex2D(_NoiseTex, uv * 100 + _Time * 50);
noise = mad(noise, 2.0, -0.5);
return noise/255;
}
fixed4 frag (v2f i) : COLOR
{
i.color.rgb += getNoise(i.uv);
return _Color * 2.0 * i.color;
}
ENDCG
}
}
Fallback "VertexLit"
}
What this does is it gets the random value from the blue noise map, but it effects the color of the entire mesh rather than each individual pixel. I've searched the far reaches of the internet and can't seem to find a way to get this working. I'm a noob with shaders, and any help is appreciated.
Answer by BHS · Jan 30, 2020 at 10:37 PM
@IlisanVlad To implement, just use the following after applying the noise function and noise texture within the frag function:
half4 c;
c.rgb = TheColorToFix.rgb + getNoise(i.uv);
c.rgb *= YourTextureColor.a;
c.a = YourTextureColor.a;
return c;
In the case of @tedesignz1, I believe this should work (untested):
half4 c;
c.rgb = _Color.rgb + getNoise(i.uv);
c.rgb *= i.color.a;
c.a = i.color.a;
return c;
Answer by IlisanVlad · Sep 22, 2019 at 08:39 PM
Did you find any solution? I am losing my mind trying to solve this...
Your answer
Follow this Question
Related Questions
Shader displaying with weird colors 1 Answer
Can someone add (or help me add) a wind effect to my current shader? 0 Answers
Help Please,Help Pease 0 Answers
Water Shader foam does only work in SceneEditor 0 Answers
shader graph messes up the texture (2D) 0 Answers