Better dithered gradient

An upgrade to the Smooth Gradient shader using a simpler and more efficient dithering algorithm.

Noise granularity uniform – the strength of the noise: the higher it is and the more noticeable the noise will be

Shader code
shader_type canvas_item;
render_mode unshaded;

uniform vec2 direction = vec2(0.0, -1.0);
uniform vec4 start_color : source_color;
uniform vec4 end_color : source_color;
uniform float noise_granularity = 5.0;

float random(vec2 seed, float pmin, float pmax)
{
	return pmin + fract(sin(dot(seed.xy, vec2(12.9898, 78.233))) * 43758.5453123) * (pmax - pmin);
}

float get_delta(vec2 uv)
{
	vec2 dir = normalize(direction);
	return (dir.x < 0.0 ? (1.0 - uv.x) : uv.x) * dir.x * dir.x + (dir.y < 0.0 ? (1.0 - uv.y) : uv.y) * dir.y * dir.y;
}

void fragment()
{
	float noise = noise_granularity / 255.0;
	float delta = get_delta(UV) + random(UV, -noise, noise);
	COLOR = mix(start_color, end_color, delta);
}
Tags
gradient dithering dithered debanding debanded deband smooth
The shader code and all code snippets in this post are under CC0 license and can be used freely without the author's permission. Images and videos, and assets depicted in those, do not fall under this license. For more info, see our License terms.

More from Astral_Sheep

Smooth Color Gradient

Simple 2D Highlight

Related shaders

page flip better

Gradient Generator

Clip Studio Gradient Map [ with blend mode ]

Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments