#Unable to query `OrthographicProjection` on 0.11

1 messages · Page 1 of 1 (latest)

inland pumice
#

Hi, I seem to be unable to query the OrthographicProjection. How can I query for it? Here's a small example of what I'm doing, which fails with:

called `Result::unwrap()` on an `Err` value: NoEntities("bevy_ecs::query::state::QueryState<&mut bevy_render::camera::projection::OrthographicProjection, bevy_ecs::query::filter::With<test::MyGameCamera>>")
use bevy::{prelude::*, render::camera::ScalingMode};

fn main() {
    App::new()
        .add_plugins(DefaultPlugins)
        .add_systems(Startup, setup)
        .add_systems(Update, update)
        .run();
}

fn setup(
    mut commands: Commands,
    mut meshes: ResMut<Assets<Mesh>>,
    mut materials: ResMut<Assets<StandardMaterial>>,
) {
    // camera
    commands.spawn((
        Camera3dBundle {
            projection: OrthographicProjection {
                scale: 3.0,
                scaling_mode: ScalingMode::WindowSize(256.0),
                ..default()
            }
            .into(),
            transform: Transform::from_xyz(5.0, 5.0, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
            ..default()
        },
        MyGameCamera {},
    ));

    commands.spawn(PbrBundle {
        mesh: meshes.add(shape::Plane::from_size(5.0).into()),
        material: materials.add(Color::rgb(0.3, 0.5, 0.3).into()),
        ..default()
    });
    commands.spawn(PbrBundle {
        mesh: meshes.add(Mesh::from(shape::Cube { size: 1.0 })),
        material: materials.add(Color::rgb(0.8, 0.7, 0.6).into()),
        transform: Transform::from_xyz(0.0, 0.5, 0.0),
        ..default()
    });

    commands.spawn(PointLightBundle {
        transform: Transform::from_xyz(3.0, 8.0, 5.0),
        ..default()
    });
}

fn update(mut q: Query<&mut OrthographicProjection, With<MyGameCamera>>) {
    let mut projection = q.single_mut();

    projection.scale *= 1.25;
    projection.scale = projection.scale.clamp(0.5, 5.0);
}

#[derive(Component)]
pub struct MyGameCamera;
inland pumice
#

Going to answer my own question; after some more coffee you can query for Projection and then match on it. However I'm unsure if that's the 'way its supposed to be done' 🙂

if let Projection::Orthographic(p) = projection.as_mut() {
    p.scale += zoom_speed * time.delta_seconds();
}
lost garnet
#

@inland pumice Camera3dBundle inserts a Projection component, not a PerspectiveProjection or OrthographicProjection
If you want your system to be generic (if you're making a library for example, very few use cases for a game), you can make it generic over the trait CameraProjection, but you'll have to add it to the app several times for each variant of the trait.

#

If you want to do that you can see how it's done you can check update_frusta in Bevy's codebase: https://github.com/bevyengine/bevy/blob/10797d4f159e8686719b32cf95eec69134b11eaa/crates/bevy_render/src/view/visibility/mod.rs#L270 and how it's added above.
I have a PR to do that to another system: https://github.com/bevyengine/bevy/pull/9226/files

GitHub

Objective
Fixes #9077 (see this issue for motivations)
Solution
Implement 1 and 2 of the "How to fix it" section of #9077
update_directional_light_cascades is split into clear_dir...

GitHub

A refreshingly simple data-driven game engine built in Rust - bevyengine/bevy

inland pumice
#

Right, no need for generics. I was mostly confused that I couldn't select OrthographicProjection directly anymore and instead have to go through Projection; probably reading some older stuff didn't help 🙂

lost garnet
#

@inland pumice Maybe what you read applied for Camera2dBundle, which does add an OrthographicProjection component directly