| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
use std::f32::consts::FRAC_PI_2; |
| 46 |
|
| 47 |
use bevy::{ |
| 48 |
camera::visibility::RenderLayers, color::palettes::tailwind, |
| 49 |
input::mouse::AccumulatedMouseMotion, light::NotShadowCaster, prelude::*, |
| 50 |
}; |
| 51 |
|
| 52 |
fn main() { |
| 53 |
App::new() |
| 54 |
.add_plugins(DefaultPlugins) |
| 55 |
.add_systems( |
| 56 |
Startup, |
| 57 |
( |
| 58 |
spawn_view_model, |
| 59 |
spawn_world_model, |
| 60 |
spawn_lights, |
| 61 |
spawn_text, |
| 62 |
), |
| 63 |
) |
| 64 |
.add_systems(Update, (move_player, change_fov)) |
| 65 |
.run(); |
| 66 |
} |
| 67 |
|
| 68 |
#[derive(Debug, Component)] |
| 69 |
struct Player; |
| 70 |
|
| 71 |
#[derive(Debug, Component, Deref, DerefMut)] |
| 72 |
struct CameraSensitivity(Vec2); |
| 73 |
|
| 74 |
impl Default for CameraSensitivity { |
| 75 |
fn default() -> Self { |
| 76 |
Self( |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
Vec2::new(0.003, 0.002), |
| 83 |
) |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
#[derive(Debug, Component)] |
| 88 |
struct WorldModelCamera; |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
const DEFAULT_RENDER_LAYER: usize = 0; |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
const VIEW_MODEL_RENDER_LAYER: usize = 1; |
| 98 |
|
| 99 |
fn spawn_view_model( |
| 100 |
mut commands: Commands, |
| 101 |
mut meshes: ResMut<Assets<Mesh>>, |
| 102 |
mut materials: ResMut<Assets<StandardMaterial>>, |
| 103 |
) { |
| 104 |
let arm = meshes.add(Cuboid::new(0.1, 0.1, 0.5)); |
| 105 |
let arm_material = materials.add(Color::from(tailwind::TEAL_200)); |
| 106 |
|
| 107 |
commands.spawn(( |
| 108 |
Player, |
| 109 |
CameraSensitivity::default(), |
| 110 |
Transform::from_xyz(0.0, 1.0, 0.0), |
| 111 |
Visibility::default(), |
| 112 |
children![ |
| 113 |
( |
| 114 |
WorldModelCamera, |
| 115 |
Camera3d::default(), |
| 116 |
Projection::from(PerspectiveProjection { |
| 117 |
fov: 90.0_f32.to_radians(), |
| 118 |
..default() |
| 119 |
}), |
| 120 |
), |
| 121 |
|
| 122 |
( |
| 123 |
Camera3d::default(), |
| 124 |
Camera { |
| 125 |
|
| 126 |
order: 1, |
| 127 |
..default() |
| 128 |
}, |
| 129 |
Projection::from(PerspectiveProjection { |
| 130 |
fov: 70.0_f32.to_radians(), |
| 131 |
..default() |
| 132 |
}), |
| 133 |
|
| 134 |
RenderLayers::layer(VIEW_MODEL_RENDER_LAYER), |
| 135 |
), |
| 136 |
|
| 137 |
( |
| 138 |
Mesh3d(arm), |
| 139 |
MeshMaterial3d(arm_material), |
| 140 |
Transform::from_xyz(0.2, -0.1, -0.25), |
| 141 |
|
| 142 |
RenderLayers::layer(VIEW_MODEL_RENDER_LAYER), |
| 143 |
|
| 144 |
NotShadowCaster, |
| 145 |
), |
| 146 |
], |
| 147 |
)); |
| 148 |
} |
| 149 |
|
| 150 |
fn spawn_world_model( |
| 151 |
mut commands: Commands, |
| 152 |
mut meshes: ResMut<Assets<Mesh>>, |
| 153 |
mut materials: ResMut<Assets<StandardMaterial>>, |
| 154 |
) { |
| 155 |
let floor = meshes.add(Plane3d::new(Vec3::Y, Vec2::splat(10.0))); |
| 156 |
let cube = meshes.add(Cuboid::new(2.0, 0.5, 1.0)); |
| 157 |
let material = materials.add(Color::WHITE); |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
commands.spawn((Mesh3d(floor), MeshMaterial3d(material.clone()))); |
| 163 |
|
| 164 |
commands.spawn(( |
| 165 |
Mesh3d(cube.clone()), |
| 166 |
MeshMaterial3d(material.clone()), |
| 167 |
Transform::from_xyz(0.0, 0.25, -3.0), |
| 168 |
)); |
| 169 |
|
| 170 |
commands.spawn(( |
| 171 |
Mesh3d(cube), |
| 172 |
MeshMaterial3d(material), |
| 173 |
Transform::from_xyz(0.75, 1.75, 0.0), |
| 174 |
)); |
| 175 |
} |
| 176 |
|
| 177 |
fn spawn_lights(mut commands: Commands) { |
| 178 |
commands.spawn(( |
| 179 |
PointLight { |
| 180 |
color: Color::from(tailwind::ROSE_300), |
| 181 |
shadow_maps_enabled: true, |
| 182 |
..default() |
| 183 |
}, |
| 184 |
Transform::from_xyz(-2.0, 4.0, -0.75), |
| 185 |
|
| 186 |
RenderLayers::from_layers(&[DEFAULT_RENDER_LAYER, VIEW_MODEL_RENDER_LAYER]), |
| 187 |
)); |
| 188 |
} |
| 189 |
|
| 190 |
fn spawn_text(mut commands: Commands) { |
| 191 |
commands |
| 192 |
.spawn(Node { |
| 193 |
position_type: PositionType::Absolute, |
| 194 |
bottom: px(12), |
| 195 |
left: px(12), |
| 196 |
..default() |
| 197 |
}) |
| 198 |
.with_child(Text::new(concat!( |
| 199 |
"Move the camera with your mouse.\n", |
| 200 |
"Press arrow up to decrease the FOV of the world model.\n", |
| 201 |
"Press arrow down to increase the FOV of the world model." |
| 202 |
))); |
| 203 |
} |
| 204 |
|
| 205 |
fn move_player( |
| 206 |
accumulated_mouse_motion: Res<AccumulatedMouseMotion>, |
| 207 |
player: Single<(&mut Transform, &CameraSensitivity), With<Player>>, |
| 208 |
) { |
| 209 |
let (mut transform, camera_sensitivity) = player.into_inner(); |
| 210 |
|
| 211 |
let delta = accumulated_mouse_motion.delta; |
| 212 |
|
| 213 |
if delta != Vec2::ZERO { |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
let delta_yaw = -delta.x * camera_sensitivity.x; |
| 221 |
let delta_pitch = -delta.y * camera_sensitivity.y; |
| 222 |
|
| 223 |
let (yaw, pitch, roll) = transform.rotation.to_euler(EulerRot::YXZ); |
| 224 |
let yaw = yaw + delta_yaw; |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
const PITCH_LIMIT: f32 = FRAC_PI_2 - 0.01; |
| 233 |
let pitch = (pitch + delta_pitch).clamp(-PITCH_LIMIT, PITCH_LIMIT); |
| 234 |
|
| 235 |
transform.rotation = Quat::from_euler(EulerRot::YXZ, yaw, pitch, roll); |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
fn change_fov( |
| 240 |
input: Res<ButtonInput<KeyCode>>, |
| 241 |
mut world_model_projection: Single<&mut Projection, With<WorldModelCamera>>, |
| 242 |
) { |
| 243 |
let Projection::Perspective(perspective) = world_model_projection.as_mut() else { |
| 244 |
unreachable!( |
| 245 |
"The `Projection` component was explicitly built with `Projection::Perspective`" |
| 246 |
); |
| 247 |
}; |
| 248 |
|
| 249 |
if input.pressed(KeyCode::ArrowUp) { |
| 250 |
perspective.fov -= 1.0_f32.to_radians(); |
| 251 |
perspective.fov = perspective.fov.max(20.0_f32.to_radians()); |
| 252 |
} |
| 253 |
if input.pressed(KeyCode::ArrowDown) { |
| 254 |
perspective.fov += 1.0_f32.to_radians(); |
| 255 |
perspective.fov = perspective.fov.min(160.0_f32.to_radians()); |
| 256 |
} |
| 257 |
} |