godot: PointLight2D doesn't respect Nearest texture filter for normal-maps or shadows

TLDR - jump to this post: https://github.com/godotengine/godot/issues/76266#issuecomment-1751739538


Godot version

4.0.2.stable.official

System information

Linux & Windows, various hardware

Issue description

We can see the expected behavior for PointLight2D - each pixel of the Sprite2D is shaded without any interpolation or filtering, when we zoom in. light_not_scaled_no_normal_map

When the light texture is scaled down, we can see that the shading actually happens for each pixel of the scaled light texture, even if pixels of the Sprite2D are bigger. I assume this is also expected behavior. light_scaled_down_4x_no_normal_map

But when we use the CanvasTexture with NormalMap, suddenly the lighting is being interpolated for each screen pixel (the light pixels suppose to be the same size as sprite’s). Setting the texture filter to Nearest does not fix this. light_not_scaled_with_normal_map

It seems that the light() shader is being run for each screen pixel (despite Camera2D zooming), which besides some performance impact, would be acceptable on its own. But this also causes the CanvasTextures to look ugly for pixel-art games with in-game zooming (and it looks even worse for some custom shaders).

Steps to reproduce

  1. Add Sprite2D and set it’s texture to CanvasTexture
  2. Load some Diffuse and NormalMap textures
  3. Add Camera2D and zoom it in to see individual pixels clearly
  4. Add PointLight2D with some texture
  5. Set texture filter to Nearest for all components (and observe there is no difference)

Minimal reproduction project

light_pixel_test.zip

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Reactions: 1
  • Comments: 30 (15 by maintainers)

Most upvoted comments

What is the status for this issue? If it is considered a bug/feature I would like to give it a try 😊

This couldn’t fix the problem on my side. The engine compiled just fine though. If you have made it so far to figure out how to make it an option/switchable you can make a PR, this would put this change into perspective and if needed or if there are other methods available, this would be discussed as a PR then.

I’m working on adding the option to switch, it turned out to be more complicated than I imagined but I think I managed to do it.

https://github.com/user-attachments/assets/3948d8b5-20c9-4aa4-af7b-effce4b2fa4d

As for why it didn’t work for you, you probably already know this but, if you changed the canvas.glsl after building the project it won’t work because it actually uses the canvas.glsl.gen.h file which is generated from canvas.glsl. Either rebuild it or change canvas.glsl.gen.h directly to test it. If you already did this and it didn’t work then it’s interesting why it didn’t work for you.

Is there another solution, or is this a Godot bug? I’m yet to be replied.

To create pixelized shadows, you could use a light() function in a custom shader that modifies vertex position in the fragment shader to affect where light is sampled. This is essentially the 2D counterpart to https://github.com/godotengine/godot-proposals/issues/974.

However, this functionality isn’t implemented in the engine yet.

Exactly. I want the game art (textures and shaders) to be in low resolution, but physics and movement in high resolution. This is a common approach for many games.

This is already supported. This is what the viewport stretch mode achieves 🙂

Movement and physics still occurs at the same precision level regardless of stretch mode (it only affects visuals).