Problems with per texel shading

Godot Version

4.5

Question

I’m trying to implement a texel space lighting system with a custom shader. I’ve found one or two people attempting this idea but there’s not that much information. I ended up more or less copying this code

vec2 tex_size = vec2(textureSize(texture_albedo, 0));
vec2 nearest_texel = (floor(base_uv * tex_size) + 0.5) / tex_size;
vec2 offset = nearest_texel - base_uv;
vec2 x_derivative = dFdx(base_uv);
vec2 y_derivative = dFdy(base_uv);

offset /= x_derivative.s*y_derivative.t - y_derivative.s*x_derivative.t;
vec2 frag_delta = vec2
(
offset.x*y_derivative.t - offset.y*y_derivative.s,
offset.y*x_derivative.s - offset.x*x_derivative.t
);
vec3 view_delta = dFdx(VERTEX)*frag_delta.s + dFdy(VERTEX)*frag_delta.t;
LIGHT_VERTEX += view_delta;

This was the result:

It looks a bit like what I’m going for, mostly when the light is very close to the model. But the pixels seem to be partially shaded like its combining normal lighting and the pixel grid, there seems to be rounded shadows on the contours of the objects that might be related to smooth shading, and the effect seems to fade out as the light source gets further away with more distant objects seemingly being shaded normally.