Last active
December 24, 2019 19:52
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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