Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2D camera renders nothing if scale is < 1.0 #239

Closed
inodentry opened this issue Aug 19, 2020 · 14 comments
Closed

2D camera renders nothing if scale is < 1.0 #239

inodentry opened this issue Aug 19, 2020 · 14 comments
Labels
A-Rendering Drawing game state to the screen C-Bug An unexpected or incorrect behavior

Comments

@inodentry
Copy link
Contributor

As I am trying to get my scene to fit on-screen (difficult with the default orthographic projection, but that's another issue), I noticed that nothing is being rendered if the camera scale is < 1.0.

let mut camera = Camera2dComponents::default();
camera.scale = Scale(0.9);
commands.spawn(camera);

If the scale is above 1.0, the view becomes "zoomed out" (everything renders smaller). If the scale is any value below 1.0 (like in the example above), I get a blank screen with nothing rendered.

@cart
Copy link
Member

cart commented Aug 19, 2020

i have a feeling this has to do with depth / clip distance. if thats the problem, we could consider increasing the near/far distance to see if that resolves this problem.

@karroffel karroffel added C-Bug An unexpected or incorrect behavior A-Rendering Drawing game state to the screen labels Aug 19, 2020
@luleyleo
Copy link

I just ran into this problem, and setting far to 1000.0 / scale fixed it for me.

@Matthew-Maclean
Copy link

Matthew-Maclean commented Sep 13, 2020

@Finnerale When you set far to 1000.0 / scale, did you just plug far into the translation and orthographic camera, Or were there more places to use it? Those were all the places I could see to use it, but I still can't get anything to draw. I also tried with far = f32::MAX, but I couldn't get anything. I could get it working with scales that were very close to 1. It worked at scale = 0.99999, but not 0.9999.

@luleyleo
Copy link

@Matthew-Maclean All I did, was setting OrthographicProjection { far: 1000.0 / scale } and Scale(scale) then my sprites showed up with any scale I tried.

@Matthew-Maclean
Copy link

@Finnerale Ok, so you didn't touch the translation. That method worked for me. Thanks.

@concave-sphere
Copy link

I am experiencing the same behavior, but setting far to 1000.0 / scale does not fix it for me. However, it does work for me (everything(*) shows up at all the scales I tried) if I do this after adjusting the scale:

        camera_transform.translation.z = 999.0 * camera_transform.scale.x;

(*) I'm drawing a grid of lines (with lyon) and they start to "twinkle" in and out when I zoom out too far, but I'm guessing that's a different category of problem, something to do with rendering polygons that are smaller than a pixel.

@alice-i-cecile alice-i-cecile added the S-Needs-Investigation This issue requires detective work to figure out what's going wrong label Oct 28, 2021
@alice-i-cecile
Copy link
Member

alice-i-cecile commented Mar 21, 2022

Closing as stale: the renderer has been completely reworked. Feel free to reopen if you have a modern reproduction.

@colepoirier
Copy link
Contributor

@alice-i-cecile unfortunately this is still an issue. Here is a minimum reproducible example using bevy main as of today: https://github.com/colepoirier/bevy_2d_camera_scale_repro/tree/master.

@alice-i-cecile alice-i-cecile removed the S-Needs-Investigation This issue requires detective work to figure out what's going wrong label Mar 21, 2022
@ostwilkens
Copy link
Contributor

I investigated a bit, but don't know enough about the rendering pipeline to have opinions on what to do about it.

The view_proj uniform is set in bevy_render > src > view > mod.rs:155.
It is used in bevy_sprite > src > render > sprite.wgsl:26 to set VertexOutput.position.

out.position.z scales with camera.transform.scale.

if camera.transform.scale = Vec3::splat(1.0); then out.position.z = 0
if camera.transform.scale = Vec3::splat(0.9); then out.position.z = -0.12
if camera.transform.scale = Vec3::splat(0.8); then out.position.z = -0.25

When out.position.z is less than 0, it doesn't render.

@superdump
Copy link
Contributor

Adjust the orthographic projection scale, not the camera transform scale. The latter will end up moving the vertices outside the view frustum defined by the projection. Scaling the orthographic projection only adjusts the left/right/top/bottom bounds of the view frustum and doesn't touch the near/far, which is what you want for zooming.

@targrub
Copy link
Contributor

targrub commented Oct 7, 2022

@alice-i-cecile unfortunately this is still an issue. Here is a minimum reproducible example using bevy main as of today: https://github.com/colepoirier/bevy_2d_camera_scale_repro/tree/master.

When I run this on Bevy|main, I always see the blue square, and scale is always reported as Vec3(1.0, 1.0, 1.0). a) Is this exercising the failure scenario?, b) Is the problem fixed?, c) Does the example need to be adjusted?

@alice-i-cecile
Copy link
Member

This is a valid reproduction. Does the perceived zoom level actually change? If not, I would probably call this "basically fixed, but needs a warning somewhere". If it does, via implicitly updating the orthographic projection's scale field, I would call this fixed but likely in need of more documentation.

@TimJentzsch
Copy link
Contributor

I cannot reproduce the issue with Bevy 0.12 anymore, closing this issue.

I used the reproduction code by @colepoirier, adapted to 0.12: colepoirier/bevy_2d_camera_scale_repro#1

I can see the zoom effect and the sprite is always rendered, also for a scale of Vec3(0.9, 0.9, 0.9).

@the-az-dev
Copy link

the-az-dev commented Jul 6, 2024

This code sheet saves my nerves and time (thankfully GPT):

commands.spawn(Camera2dBundle {
        transform: Transform::from_xyz(100., 0., 1000.), // Set Z transform to 1000.0 for saving far distance in general option
        projection: OrthographicProjection {
            scaling_mode: ScalingMode::FixedVertical(500.0), // you can change scale based on your needings
            ..default()
        }.into(),
        ..default()
    })

So, the main problem is that, Camera have un-predictable option on zooming at long far distance when Transform.Z equals 0. To fix this feature you need to set static posizition at Z cordinate and set scaling based on your needings. It can helps even in another problem, when you want save zoom parameters when you change window size.

You don`t need to write code-lines where you setup distance (between your game objects and camera) and scaling for your camera. You can set this when you create Camera 2d and this can save your time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Rendering Drawing game state to the screen C-Bug An unexpected or incorrect behavior
Projects
None yet
Development

No branches or pull requests