Stylized gradient pixel glint

Modification of the other glint shader I just posted: https://godotshaders.com/shader/gradient-pixel-glint-2/

Notable differences:

  • This shader uses texelFetch() sampling method instead of texture(). texelFetch doesn’t have any antialiasing, so perfect for crisp pixel art games.
  • Since texelFetch is used, you have to input your gradient texture width in the parameter “gradient_width”, so make sure the value matches your texture.
  • I added a parameter “threshold” that allows the glint to ignore dark pixels.
Shader code
// CC0, shadecore_dev
shader_type canvas_item;

/* Glint texture. Any 1xN size texture can be used. */
uniform sampler2D gradient_texture;
/* Glint texture width in pixels. */
uniform float gradient_width : hint_range(0.0, 512.0, 1.0) = 32.0;

uniform float shine_progress : hint_range(0.0, 1.0, 0.01) = 0.0;
uniform float shine_size : hint_range(0.01, 1.0, 0.01) = 0.1;
uniform float shine_angle : hint_range(0.0, 89.9, 0.1) = 45.0;

/* Any pixels darker than this threshold will not receive glint. */
uniform float threshold : hint_range(0.0, 1.0) = 0.01;

float scale(float value, float inMin, float inMax, float outMin, float outMax) {
    return (value - inMin) * (outMax - outMin) / (inMax - inMin) + outMin;
}

void fragment() {
	vec2 pixelation = 1.0 / TEXTURE_PIXEL_SIZE;
	vec2 pixelated_uv = floor(UV * pixelation) / pixelation;
	COLOR = texture(TEXTURE, UV);
    
	float slope = tan(radians(shine_angle));

	float progress = scale(shine_progress, 0.0, 1.0, -1.0 - shine_size - shine_size * slope, 1.0 * slope + shine_size + shine_size * slope);

	float shine_distance = (slope * pixelated_uv.x - pixelated_uv.y - progress) / (sqrt(slope * slope + 1.0));

	float shine_color_coord = clamp((shine_distance / (2.0 * shine_size)) + 0.5, 0.0, 1.0);

	vec4 raw_color = texelFetch(gradient_texture, ivec2(int(shine_color_coord * gradient_width), 0), 0);

	float shine = step(shine_distance, shine_size) - step(shine_distance, -shine_size);
	
	float max_component = max(max(COLOR.r, COLOR.g), COLOR.b);

	COLOR.rgb = mix(COLOR.rgb, raw_color.rgb, shine * raw_color.a * float(max_component >= threshold));
}
Live Preview
Tags
2d, canvas, glint, pixel
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 shadecore_dev

Related shaders

guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments