Shader notes and learnings
Links
Godot 3.0 doc’s define built-in properties. Link. The stable docs removed the documentation for these properties.
Notes
SCREEN_UV = FRAGCOORD.xy / VIEWPORT_SIZE
SCREEN_UV = FRAGCOORD.xy * SCREEN_PIXEL_SIZE.xy
VIEWPORT_SIZE = 1. / SCREEN_PIXEL_SIZE
Note: this is the resolution of the entire game screen, not just the node the shader is applied to.
VIEWPORT_SIZE
is not available in canvas_item shaders
UV
— the texture coordinates of the object the shader is attached .
SCREEN_UV
— the coordinates of the viewport, or whatever you’re using as the screen (ie: if you’re grabbing a backbuffer which changes what SCREEN_UV
references)
SCREEN_UV
’s origin is bottom left.
UV
’s origin is top left.
FRAGCOORD
's origin is bottom left.
textureSize(SCREEN_TEXTURE, 0)
is a built in function to get the viewport size.
With textureLod()
, 0.0 is the highest resolution and higher numbers are more blurry.
FRAGCOORD
increases by 1 for each pixel in the viewport NOT the physical display / OS window size. Origin is bottom left. Unit: Viewport pixels. So FRAGCOORD
is not just pixel coordinates of the node the shader is attached to.
Upscaling Example
Assumes your viewport draws your content at 1x. This shader script upscales the final content drawn to the viewport. The viewport must be sized to the final resolution. That is, if the original content is 100x100 and you are trying to scale by a factor of 3, the viewport size should be 300x300.
vec2 get_native_pixel_uv(vec2 coord, vec2 screen_pixel_size,
float scale) { float viewport_size_y = 1. / screen_pixel_size.y; vec2 adjusted_uv = vec2(floor(coord.x / scale) * screen_pixel_size.x, floor(viewport_size_y - (viewport_size_y - coord.y) / scale) * screen_pixel_size.y); adjusted_uv += screen_pixel_size / 2.; return adjusted_uv;}void fragment() { vec2 adjusted_uv = get_native_pixel_uv(FRAGCOORD.xy, SCREEN_PIXEL_SIZE); COLOR = texture(SCREEN_TEXTURE, adjusted_uv);}