Skip to content

Instantly share code, notes, and snippets.

@t-karcher
Last active August 24, 2023 15:08
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save t-karcher/4e78645021bc43a6ce377d9a18c2b1f5 to your computer and use it in GitHub Desktop.
Save t-karcher/4e78645021bc43a6ce377d9a18c2b1f5 to your computer and use it in GitHub Desktop.
Godot shader bending a flat 2d world (e.g. a platformer) to a tiny planet.
shader_type canvas_item;
uniform sampler2D screen_texture : hint_screen_texture, filter_nearest;
uniform float radius = 3.0;
void fragment() {
vec2 uv = SCREEN_UV;
uv.y = 1.0 - uv.y; // seems like the origin changed in Godot 4.
vec2 surface = vec2(0.5, 0.2);
vec2 center = surface - vec2(0, radius);
float base = length(uv - center);
float height = base - radius;
float xdiff = (uv.x - surface.x) / base * height;
uv = clamp(vec2 (uv.x - xdiff, surface.y + height), vec2(0.0, 0.0), vec2(1.0, 1.0));
uv.y = 1.0 - uv.y; // flip back after all calculations
COLOR.rgb = textureLod(screen_texture, uv, 0.0).rgb;
}
@Calinou
Copy link

Calinou commented Aug 23, 2023

@Calinou: Do you know why I had to flip the Y-axis of SCREEN_UV for this shader to work in Godot 4? (The code was working perfectly fine without lines 8 and 15 in Godot 3.x)

Godot 3.x used flipped viewports (it followed OpenGL convention), but this is no longer required in Godot 4 as we could break compatibility to make it the right way around.

@t-karcher
Copy link
Author

t-karcher commented Aug 23, 2023

Thanks for the confirmation/clarification! Since neither Maik nor myself were able to find this info, I just proposed to add it to the official migration tutorial: godotengine/godot-docs#7833

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment