bevyengine / bevy

A refreshingly simple data-driven game engine built in Rust
https://bevyengine.org
Apache License 2.0
36.23k stars 3.57k forks source link

overlapping viewport from different camera causes over-culling of entity rendering #15182

Open JonnyPower opened 2 months ago

JonnyPower commented 2 months ago

Bevy version

0.14.1

Relevant system information

2024-09-13T05:04:31.937728Z INFO bevy_render::renderer: AdapterInfo { name: "Apple M1", vendor: 0, device: 0, device_type: IntegratedGpu, driver: "", driver_info: "", backend: Metal }

What you did

I've reproduced a minimal example of this issue at the below repo;

https://github.com/JonnyPower/bevy-viewport-culling-issue/blob/main/src/main.rs

When running, as you move camera around note how some of the coloured tiles are culled in primary camera. Then if you reverse the camera spawn order you'll notice them being culled in the secondary camera.

https://github.com/user-attachments/assets/4656d2d6-6961-4445-931c-98499a0c683c

What went wrong

I have a 2d game with two cameras, one that follows the local player, and a "spy camera" that follows the other player. When these cameras overlap, entities start being culled.

theory: When multiple cameras are rendering same entities, the most recent camera to be spawned wins when determining what is culled.

Additional information

https://github.com/user-attachments/assets/39b0352e-e42c-4f63-b442-6b5c0568cdd1

https://github.com/user-attachments/assets/f17efa85-52f9-4b22-942e-b20854c5501e

https://github.com/user-attachments/assets/391b1731-548d-44d1-b17a-45a38918fee5

https://github.com/user-attachments/assets/97e2e230-6108-4b83-9747-58183b9ceccb

JonnyPower commented 2 months ago

thought it may have been fixed by #14287 in 0.14.2 - confirmed it is not and still happens in 0.14.2 as well

JonnyPower commented 2 months ago

@jdrbc was able to replicate similar behaviour on an M3

Arrekin commented 1 month ago

I think I suffer from a similar issue(I can't play any videos in the original post) but for me, the order of cameras does not matter, and it just glitches randomly in one camera or the other(but never in both at the same time) by culling objects that should be visible(not all of them, just some).

Windows 10 AdapterInfo { name: "NVIDIA GeForce GTX 1080", vendor: 4318, device: 7040, device_type: DiscreteGpu, driver: "NVIDIA", driver_info: "561.09", backend: Vulkan }

https://github.com/user-attachments/assets/bf683e67-6ee3-44e2-82ac-adf9ca6b9da4

The disappearing things are just Sprites filled with Gray colour but I tried it with many different objects, at various depths, and it all works like in the video.
In the video the order of main camera is 0 and the bottom right render-to-texture camera has order 1

Arrekin commented 3 weeks ago

I confirmed that VisibleEntities in cameras are being calculated properly. Both images show exactly the same thing with the only difference being that I moved the main camera a pixel or two(and that is enough to make white sprites disappear. In the last image, you can see the Entities list for each camera - the output remains the same regardless of actual visibility so I assume the culling calculations are ok and the problem is somewhere down the line. --- PROBLEM: missing white sprites in the smaller camera --- Image --- OK: all sprites are shown on both cameras --- Image --- LIST OF VisibleEntities DOES NOT CHANGE IN BOTH CASES --- There are 5 white sprites in the smaller camera and 7 in the bigger one. That is correct, but does not match what is displayed. Image

JonnyPower commented 3 weeks ago

nice sleuthing @Arrekin - hopefully that helps someone see the downstream issue

niveau0 commented 2 weeks ago

Since I got the same problem that sprites disappear because of second camera, I extracted a quick example from my game code:

https://github.com/niveau0/bevy-test

if you start it, pressing b moves the main camera to a problematic position where sprites disappear. Maybe it helps. If it is a culling problem I would expect that NoFrustrumCulling would help, but it doesnt.

Additional info: I already tried to render the minimap camera to an image, which does not help.