Last active
August 24, 2023 15:08
-
-
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.
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 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; | |
} |
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
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.