Issue with normal map in spatial shader for terrain

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By mabysh

Hello everyone. I’m trying to implement spatial shader example from godot tutorial to render terrain from heightmap, but using my own texture instead of SimplexNoise. Here is my simple shader code:

shader_type spatial;
render_mode world_vertex_coords;

uniform sampler2D heightmap;
uniform sampler2D normalmap;

void vertex() {
    UV = (VERTEX.xz + 0.5) / 256.0; // texture size is 256
    vec2 rg = texture(heightmap, UV).rg;
    VERTEX.y = (rg.g * 256.0 + rg.r) * .125;
}

void fragment() {
    NORMALMAP = texture(normalmap, UV).rgb;
}

And the problem is that terrain lit wrong. Consider this screenshot. This hill is sampled from heightmap. Light is coming from top right corner, shpere is lit correctly, but hill is lit as if light source was in left top corner.

Here i rotated sky by 90 degrees, sphere is fine, but hill is lit from completely wrong direction.

Plane mesh was created in Blender and imported as .obj file, so I assume all required attributes are there. What am i missing? Should I calculate TBN vectors somehow? Assigning NORMAL in vertex shader instead of NORMALMAP also gives weird result. I’m kinda newbie in game development and learning by donig so any help is appreciated. Thanks in advance!
Textures i’m using:
enter image description here
enter image description here