use std::{f32::consts::PI, ops::Range};
use bevy::{input::mouse::AccumulatedMouseScroll, prelude::*, render::camera::ScalingMode};
#[derive(Debug, Resource)]
struct CameraSettings {
pub orthographic_viewport_height: f32,
pub orthographic_zoom_range: Range<f32>,
pub orthographic_zoom_speed: f32,
pub perspective_zoom_range: Range<f32>,
pub perspective_zoom_speed: f32,
}
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.insert_resource(CameraSettings {
orthographic_viewport_height: 5.,
orthographic_zoom_range: 0.1..10.0,
orthographic_zoom_speed: 0.2,
perspective_zoom_range: (PI / 5.)..(PI - 0.2),
perspective_zoom_speed: 0.05,
})
.add_systems(Startup, (setup, instructions))
.add_systems(Update, (switch_projection, zoom))
.run();
}
fn setup(
asset_server: Res<AssetServer>,
camera_settings: Res<CameraSettings>,
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
commands.spawn((
Name::new("Camera"),
Camera3d::default(),
Projection::from(OrthographicProjection {
scaling_mode: ScalingMode::FixedVertical {
viewport_height: camera_settings.orthographic_viewport_height,
},
scale: 1.,
..OrthographicProjection::default_3d()
}),
Transform::from_xyz(5.0, 5.0, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
));
commands.spawn((
Name::new("Plane"),
Mesh3d(meshes.add(Plane3d::default().mesh().size(5.0, 5.0))),
MeshMaterial3d(materials.add(StandardMaterial {
base_color: Color::srgb(0.3, 0.5, 0.3),
cull_mode: None,
..default()
})),
));
commands.spawn((
Name::new("Fox"),
SceneRoot(
asset_server.load(GltfAssetLabel::Scene(0).from_asset("models/animated/Fox.glb")),
),
Transform::from_translation(Vec3::splat(0.0)).with_scale(Vec3::splat(0.025)),
));
commands.spawn((
Name::new("Light"),
PointLight::default(),
Transform::from_xyz(3.0, 8.0, 5.0),
));
}
fn instructions(mut commands: Commands) {
commands.spawn((
Name::new("Instructions"),
Text::new(
"Scroll mouse wheel to zoom in/out\n\
Space: switch between orthographic and perspective projections",
),
Node {
position_type: PositionType::Absolute,
top: Val::Px(12.),
left: Val::Px(12.),
..default()
},
));
}
fn switch_projection(
mut camera: Single<&mut Projection, With<Camera>>,
camera_settings: Res<CameraSettings>,
keyboard_input: Res<ButtonInput<KeyCode>>,
) {
if keyboard_input.just_pressed(KeyCode::Space) {
**camera = match **camera {
Projection::Orthographic(_) => Projection::Perspective(PerspectiveProjection {
fov: camera_settings.perspective_zoom_range.start,
..default()
}),
Projection::Perspective(_) => Projection::Orthographic(OrthographicProjection {
scaling_mode: ScalingMode::FixedVertical {
viewport_height: camera_settings.orthographic_viewport_height,
},
..OrthographicProjection::default_3d()
}),
}
}
}
fn zoom(
camera: Single<&mut Projection, With<Camera>>,
camera_settings: Res<CameraSettings>,
mouse_wheel_input: Res<AccumulatedMouseScroll>,
) {
match *camera.into_inner() {
Projection::Orthographic(ref mut orthographic) => {
let delta_zoom = -mouse_wheel_input.delta.y * camera_settings.orthographic_zoom_speed;
let multiplicative_zoom = 1. + delta_zoom;
orthographic.scale = (orthographic.scale * multiplicative_zoom).clamp(
camera_settings.orthographic_zoom_range.start,
camera_settings.orthographic_zoom_range.end,
);
}
Projection::Perspective(ref mut perspective) => {
let delta_zoom = -mouse_wheel_input.delta.y * camera_settings.perspective_zoom_speed;
perspective.fov = (perspective.fov + delta_zoom).clamp(
camera_settings.perspective_zoom_range.start,
camera_settings.perspective_zoom_range.end,
);
}
}
}