How does a shader know what LOD to use?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By rkkn

seems like it should be very simple, but I can’t find it

fragment shader can specify LOD when reading from a texture, but none of the built-ins look helpful for determining how scaled/zoomed you are in the first place so that you can know which LOD to pick

:bust_in_silhouette: Reply From: DaddyMonster

A proper inbuilt LOD system with all the bells and whistles is coming in the near future with Godot 4. For now you need to code it yourself. That said, it’s pretty easy to do.

If you have low poly versions of the mesh then I’d recommend making two MeshInstances and just toggling visibility on them according to the distance to the camera. This is by far the simplest implementation and it’s very effective.

Equally you can handle this in a shader by preloading multiple textures as uniforms and then adding a uniform distance float to jump between them. However, you have to pay for this extra gpu computation so it’s questionable whether you’d be gaining. Each game is different but I’d personally handle this cpu side every time. If it’s just textures you need then you can override them / have multiple passes as easy solutions.

Word of warning: just bare in mind that if you have a lot of textures then additional low poly textures [counter-intuitively] runs the risk of slowing your game down. Everything that’s preloaded goes into gpu memory which is fine until it fills up and then GLES3 has to start juggling resources in and out of memory which can lead to drops. That said, most gpus have tonnes of memory these days, just something to bear in mind. It’ll slow the game on startup too, hard drive fetches take eons.

Optimisation can easily backfire so the rule of thumb is to only add these things if you have a framerate issue.

The question was asking about texture LOD (aka. mipmap usage), not mesh LOD.

I believe mipmap LOD is automatically determined by the graphics API depending on distance between the camera and the surface. Godot will use texture mipmaps automatically as long as the texture was imported with mipmaps (which is the case by default in 3D, but not in 2D). You can toggle mipmap importing for each texture by selecting them in the FileSystem dock then going to the Import dock.

Calinou | 2021-11-04 00:31