Object-space Normal Maps in gdshader

I’ve seen some conflicting threads on using Object Normals in gdshaders, thankfully someone on the Godot Effects and Shaders discord helped out :grinning_face_with_smiling_eyes:

It was surprisingly simple, but knowledge in Discords can be easily lost/hard to find. If anyone’s searching for a solution, hopefully Google picks up this snippet.

shader_type spatial;

uniform sampler2D object_normals;
varying mat3 mvnm;

void vertex() {
    mvnm = MODELVIEW_NORMAL_MATRIX;
}

void fragment() {
    vec3 normal_sample = texture(object_normals, UV).rgb;
    NORMAL = normalize(mvnm * normal_sample);
}
5 Likes