Supposed inconsistency between editor and in-game shaders (SOLVED)

[SOLVED]
Howdy yall! I’m trying to make a directional lighting to be affected by the perspective/camera. Thing is, it seems that in the editor i get the desired result, but in-game its much darker:
image
image

Heres how it should look like, using lovr:
image

Could i be doing something wrong? I sorta used the default model material as a guide, tho i couldnt replicate it perfectly with a directional light, i tinkered with it until i made it be affected by the camera. Although it shoots straight from the camera and not at an angle as it should:
image

uniform mediump mat4 mtx_worldview;
uniform mediump mat4 mtx_view;
uniform mediump mat4 mtx_proj;
uniform mediump mat4 mtx_normal;

uniform highp sampler2D gradient_map;

attribute mediump vec4 position;
attribute mediump vec2 texcoord0;
attribute mediump vec3 normal;

attribute mediump float uv_factor;
attribute mediump vec3 light_direction;

varying mediump vec3 mapped_light;

void main()
{
	vec3 L = -vec3(normalize(mtx_view * vec4(normalize(light_direction), 1.0)));
    vec3 N = vec3(normalize(mtx_normal * vec4(normalize(normal), 1.0)));
    float light_factor = .5 + dot(N, L) * .5;

    mapped_light = vec3(texture(gradient_map, vec2(texcoord0.x, light_factor)));
	gl_Position = mtx_proj * mtx_worldview * vec4(position.xyz, 1.0);
}

i figured it was obvious that i needed the direction in view space, but the normals i wasnt sure what to do to make it work. The normal matrix makes it relative to the camera but it shoots straight from the camera like i said (im still not sure what this normal matrix is supposed to be used for). Any help is greatly appreciated!

2 Likes

Could you upload a minimal project with this model so it’s easier to help debug the shader so it looks right in engine?

1 Like

I sorta solved the problem by leaving the light position in world space alone, but the editor/in-game problem is still happening. I sent a zip of the project:
Gradient Lighting.zip (107.3 KB)

SOLVED:
changed the light and normal calculations to this and worked:

vec3 L = -normalize(vec3(light_direction));
vec3 N = normalize(vec3(mtx_normal * vec4(normal, 1.0)));

i still dont know what the normal matrix is supposed to be/do neither how come normalizing only after all the calculations are done fixed the issue.
Not even kidding. Changing the code just a tiny bit already breaks it:

vec3 L = -normalize(vec3(light_direction));
vec3 N = vec3(normalize(mtx_normal * vec4(normal, 1.0)));

Although, now that i think about it probably has something to do with the fact that the W component is taking into account for normalization, thus removing it removes its effect as well