[3.0] How to setup objectspace normal map correctly

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By xdeadmonkx
:warning: Old Version Published before Godot 3 was released.

Hello, does anyone tried to set up objectspace normal map in godot 3?
i do it this way:

uniform sampler2D uNormal;

void fragment()
NORMAL = texture(uNormal, UV).rgb;

all is good if i don’t change camera position/rotation, but when i change it lighting/shadow areas begin to move but it must be in the same position because light source is in the same place (
maybe i missing something in shader?

:bust_in_silhouette: Reply From: Zylann

There are a few things wrong here:

First, RGB isn’t in -1…1, but in 0…1. You need to convert it so that normals can point in negative coordinates:

vec3 normal = texture(uNormal, UV).rgb * 2.0 - 1.0;

Next, you need to transform this normal from object space to view space, so taking care of the object’s world matrix and the camera’s matrix:

NORMAL = (INV_CAMERA_MATRIX * (WORLD_MATRIX * vec4(normal, 0.0))).xyz;

I hope this is correct :slight_smile:

Thanx a lot, it worked.
After some modifications i made a working shader for blender objectspace normalmaps and vertex color; -

shader_type spatial;
uniform sampler2D uNormal;

void fragment()
	ALBEDO=COLOR.rgb * 2.0;
	vec3 normal = texture(uNormal, UV).rgb * 2.0 - 1.0;
	float y = normal.y;
	normal.y = -normal.z;
	normal.z = y;
	NORMAL =  (INV_CAMERA_MATRIX * (WORLD_MATRIX * vec4(normal, 0.0))).rgb;

xdeadmonkx | 2017-08-04 18:24