Skip to content

Instantly share code, notes, and snippets.

@MightyPrinny
Last active December 24, 2019 19:52
Show Gist options
  • Save MightyPrinny/bdaabd21f4668dd44a3924c87ba550af to your computer and use it in GitHub Desktop.
Save MightyPrinny/bdaabd21f4668dd44a3924c87ba550af to your computer and use it in GitHub Desktop.
Godot shader to make pixel art look less jittery when the resolution isn't a multiple of the internal resolution.
shader_type canvas_item;
uniform vec2 texelsPerPixel = vec2(1.5,1.5);
void fragment()
{
vec2 uv = SCREEN_UV/SCREEN_PIXEL_SIZE;
vec2 locationWithinTexel = fract(uv);
vec2 interpolationAmount = clamp(locationWithinTexel/texelsPerPixel ,0 , 0.5)
+ clamp( (locationWithinTexel - 1.0) / (texelsPerPixel + .5), 0, 0.5);
vec2 finalTextureCoords = (floor(uv) +
interpolationAmount) * SCREEN_PIXEL_SIZE;
COLOR = texture(SCREEN_TEXTURE, finalTextureCoords);
}
/*
Use on a ColorRect that covers the screen
and give it a script that sets texelsPerPixel to size_of_screen/internal_resolution when the screen is resized.
Something like this(gdscript)
var internal_res = Vector2(ProjectSettings.get_setting("display/window/size/width"),ProjectSettings.get_setting("display/window/size/height"))
var scl = max(1,int(min(int(get_viewport().size.x/internal_res.x),int(get_viewport().size.y/internal_res.y))/2)*2)
var tpp = (get_viewport().size/(internal_res*scl))
material.set_shader_param("texelsPerPixel",tpp)
With this code the effect will be less strong at higher resolutions
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment