godotengine / godotengine/godot
extra_cull_margin breaks draw ordering for custom shaders with the orthographic camera
- Dominant language
- C++
- Stars
- 117k
- Forks
- 26.8k
- PR merge metrics
- PR metrics pending
Description
### Tested versions
- Reproducible on 4.2.1 stable & 4.3 dev snapshot 4
### System information
macOS
### Issue description
`extra_cull_margin` completely breaks draw order for orthographic cameras + custom shaders. I'm not sure what's going on under the hood causing the issue and how the build-in shaders avoid this issue. The same behaviour does not exist for the perspective camera. This bug has been driving me absolutely nuts the last couple of days. Please, someone with knowledge of rendering internals save me from this madness.
### Steps to reproduce
Set up two quads, one offset to the left at `Z=0` and one on the right further back at `Z=-7.6`. Both quads use the same, dead simple custom shader:
```
shader_type spatial;
render_mode unshaded;
uniform sampler2D texture;
void fragment() {
vec4 tex = texture(texture, UV);
ALBEDO = vec3(tex.x, tex.y, tex.z);
ALPHA = tex.a;
}
```
Set the `extra_cull_margin` of the quad that's further back to `1000`.
Scene looks like this:
Expected render:
Actual:
### Minimal reproduction project (MRP)
[orthographic-camera-bug-demo.zip](https://github.com/godotengine/godot/files/14897993/orthographic-camera-bug-demo.zip)
You can just run the demo project and every second, it'll alternate between default cull margins, and the `1000` cull margin on the far-away sprite, which causes it to incorrectly draw on top of the closer sprite.
Contributor guide
Research direction
Start by running the attached orthographic-camera-bug-demo.zip minimal reproduction project and compare its default and 1000 extra_cull_margin states. Investigate the rendering path for orthographic cameras using the supplied custom spatial shader, then compare it with perspective-camera behavior and built-in shaders. Done means the far-away quad no longer draws over the closer quad when extra_cull_margin is increased.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- computer-graphics
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100