How in the world does REGION_RECT work

Godot Version

4.5

Question

I’m new to shaders and having a very hard time creating a shader that applies to a region of an AtlasTexture. UV refers to the entire texture. Supposedly, I can use REGION_RECT to get around this, but I cannot for the life of me understand how. I’ve tried utilizing the example provided in the PR: https://github.com/godotengine/godot/pull/90436#issuecomment-2776191472, but either I’m utterly misunderstanding it or it’s just not applicable code to my use-case.


vec2 uv_region = (UV - REGION_RECT.xy) / REGION_RECT.zw; gives me the whole texture, squashed. JUST vec2 uv_region = (UV - REGION_RECT.xy); seems to return the region starting at 0, 0 (aka the top left corner) instead of the one specified.

I can’t find any other examples of it’s use; from what I understand it’s new to 4.5, so that’s understandable. Can anyone explain to me how I can use it to get the UV of just that region? Also, are both these inspector region editors applicable for REGION_RECT? It’s a bit confusing that there’s one that’s part of the AtlasTexture resource and then another entirely separate one that does the same thing.


What exactly are you trying to achieve?

I’m working on a pixel-perfect tree-swaying-in-the-wind shader. I know pixel-perfect could just be gotten via the project viewport settings, but using that setting ruins the smooth character movement that I’m attempted to preserve. So I’ve landed on a middle-ground of a shader. My background assets (like trees) are on spritesheets and the shader I’ve made depends on using the UV.y to determine how far pixels are displaced (causing the sway to increase with the height of the tree). This doesn’t work well with an AtlasTexture and was causing a lot of problems, so I wanted a way to get the UV of just the selected region.

Since I posted this, I’ve actually ended up just separating the trees into individual image files, as I think my sanity is worth it. The shader works great when it’s not on an AtlasTexture.

REGION_RECT is the uv of the region. Check if UV is inside that rect and act accordingly.