Parallax3d with on-screen buttons
parent
a17ae88a9f
commit
3edb3687cb
@ -0,0 +1,138 @@
|
|||||||
|
use bevy::{color::palettes::css::*, prelude::*};
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
App::new()
|
||||||
|
.add_plugins(DefaultPlugins.set(WindowPlugin {
|
||||||
|
primary_window: Some(Window {
|
||||||
|
resolution: (640.0, 480.0).into(),
|
||||||
|
..default()
|
||||||
|
}),
|
||||||
|
..default()
|
||||||
|
}))
|
||||||
|
.insert_resource(ClearColor(WHITE.into()))
|
||||||
|
.add_systems(Startup, setup_3d)
|
||||||
|
.add_systems(Update, (move_camera_buttons, button_color))
|
||||||
|
.run();
|
||||||
|
}
|
||||||
|
|
||||||
|
fn setup_3d(
|
||||||
|
mut commands: Commands,
|
||||||
|
mut meshes: ResMut<Assets<Mesh>>,
|
||||||
|
mut materials: ResMut<Assets<StandardMaterial>>,
|
||||||
|
) {
|
||||||
|
commands.spawn((
|
||||||
|
Camera::default(),
|
||||||
|
Camera3d::default(),
|
||||||
|
AmbientLight {
|
||||||
|
brightness: 1280.0,
|
||||||
|
..default()
|
||||||
|
},
|
||||||
|
));
|
||||||
|
|
||||||
|
commands.spawn((
|
||||||
|
Mesh3d(meshes.add(Cuboid::default())),
|
||||||
|
MeshMaterial3d(materials.add(StandardMaterial {
|
||||||
|
base_color: RED.into(),
|
||||||
|
..Default::default()
|
||||||
|
})),
|
||||||
|
Transform::from_xyz(0.0, 0.0, -5.0),
|
||||||
|
));
|
||||||
|
|
||||||
|
commands.spawn((
|
||||||
|
Mesh3d(meshes.add(Cuboid::default())),
|
||||||
|
MeshMaterial3d(materials.add(StandardMaterial {
|
||||||
|
base_color: GREEN.into(),
|
||||||
|
..Default::default()
|
||||||
|
})),
|
||||||
|
Transform::from_xyz(0.0, 0.0, -10.0),
|
||||||
|
));
|
||||||
|
|
||||||
|
commands.spawn((
|
||||||
|
Mesh3d(meshes.add(Cuboid::default())),
|
||||||
|
MeshMaterial3d(materials.add(StandardMaterial {
|
||||||
|
base_color: BLUE.into(),
|
||||||
|
..Default::default()
|
||||||
|
})),
|
||||||
|
Transform::from_xyz(0.0, 0.0, -20.0),
|
||||||
|
));
|
||||||
|
|
||||||
|
commands
|
||||||
|
.spawn(Node {
|
||||||
|
align_self: AlignSelf::End,
|
||||||
|
justify_self: JustifySelf::Center,
|
||||||
|
..default()
|
||||||
|
})
|
||||||
|
.with_children(|parent| {
|
||||||
|
parent.spawn((
|
||||||
|
Node {
|
||||||
|
padding: UiRect::all(Val::Px(20.0)),
|
||||||
|
margin: UiRect::all(Val::Px(10.0)),
|
||||||
|
..default()
|
||||||
|
},
|
||||||
|
Button,
|
||||||
|
children![
|
||||||
|
Text::new("<-"),
|
||||||
|
Node {
|
||||||
|
align_self: AlignSelf::Center,
|
||||||
|
justify_self: JustifySelf::Center,
|
||||||
|
..default()
|
||||||
|
},
|
||||||
|
],
|
||||||
|
Movement(0.1),
|
||||||
|
BackgroundColor(ORANGE.into()),
|
||||||
|
));
|
||||||
|
parent.spawn((
|
||||||
|
Node {
|
||||||
|
align_self: AlignSelf::Center,
|
||||||
|
justify_self: JustifySelf::Center,
|
||||||
|
..default()
|
||||||
|
},
|
||||||
|
Text::new("movement"),
|
||||||
|
TextColor(BLACK.into()),
|
||||||
|
));
|
||||||
|
parent.spawn((
|
||||||
|
Node {
|
||||||
|
padding: UiRect::all(Val::Px(20.0)),
|
||||||
|
margin: UiRect::all(Val::Px(10.0)),
|
||||||
|
..default()
|
||||||
|
},
|
||||||
|
Button,
|
||||||
|
children![
|
||||||
|
Text::new("->"),
|
||||||
|
Node {
|
||||||
|
align_self: AlignSelf::Center,
|
||||||
|
justify_self: JustifySelf::Center,
|
||||||
|
..default()
|
||||||
|
},
|
||||||
|
],
|
||||||
|
Movement(-0.1),
|
||||||
|
BackgroundColor(ORANGE.into()),
|
||||||
|
));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
fn button_color(
|
||||||
|
mut query: Query<(&Interaction, &mut BackgroundColor), (Changed<Interaction>, With<Button>)>,
|
||||||
|
) {
|
||||||
|
query.iter_mut().for_each(|(i, mut bg)| {
|
||||||
|
bg.0 = match i {
|
||||||
|
Interaction::None => ORANGE.into(),
|
||||||
|
Interaction::Hovered => ORANGE_RED.into(),
|
||||||
|
Interaction::Pressed => RED.into(),
|
||||||
|
};
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Component)]
|
||||||
|
struct Movement(f32);
|
||||||
|
|
||||||
|
fn move_camera_buttons(
|
||||||
|
query: Query<(&Interaction, &Movement), With<Button>>,
|
||||||
|
mut camera: Single<&mut Transform, With<Camera>>,
|
||||||
|
) {
|
||||||
|
query.iter().for_each(|(i, m)| {
|
||||||
|
if let (Interaction::Pressed, Movement(speed)) = (i, m) {
|
||||||
|
camera.translation.x += speed;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
@ -1,67 +0,0 @@
|
|||||||
use bevy::{color::palettes::css::*, prelude::*};
|
|
||||||
|
|
||||||
fn main() {
|
|
||||||
App::new()
|
|
||||||
.add_plugins(DefaultPlugins)
|
|
||||||
.insert_resource(ClearColor(WHITE.into()))
|
|
||||||
.add_systems(Startup, setup_3d)
|
|
||||||
.add_systems(Update, move_camera)
|
|
||||||
.run();
|
|
||||||
}
|
|
||||||
|
|
||||||
fn setup_3d(
|
|
||||||
mut commands: Commands,
|
|
||||||
mut meshes: ResMut<Assets<Mesh>>,
|
|
||||||
mut materials: ResMut<Assets<StandardMaterial>>,
|
|
||||||
) {
|
|
||||||
commands.spawn((
|
|
||||||
Camera::default(),
|
|
||||||
Camera3d::default(),
|
|
||||||
AmbientLight {
|
|
||||||
brightness: 1280.0,
|
|
||||||
..default()
|
|
||||||
},
|
|
||||||
));
|
|
||||||
|
|
||||||
commands.spawn((
|
|
||||||
Mesh3d(meshes.add(Cuboid::default())),
|
|
||||||
MeshMaterial3d(materials.add(StandardMaterial {
|
|
||||||
base_color: RED.into(),
|
|
||||||
..Default::default()
|
|
||||||
})),
|
|
||||||
Transform::from_xyz(0.0, 0.0, -10.0),
|
|
||||||
));
|
|
||||||
|
|
||||||
commands.spawn((
|
|
||||||
Mesh3d(meshes.add(Cuboid::default())),
|
|
||||||
MeshMaterial3d(materials.add(StandardMaterial {
|
|
||||||
base_color: GREEN.into(),
|
|
||||||
..Default::default()
|
|
||||||
})),
|
|
||||||
Transform::from_xyz(0.0, 0.0, -50.0),
|
|
||||||
));
|
|
||||||
|
|
||||||
commands.spawn((
|
|
||||||
Mesh3d(meshes.add(Cuboid::default())),
|
|
||||||
MeshMaterial3d(materials.add(StandardMaterial {
|
|
||||||
base_color: BLUE.into(),
|
|
||||||
..Default::default()
|
|
||||||
})),
|
|
||||||
Transform::from_xyz(0.0, 0.0, -100.0),
|
|
||||||
));
|
|
||||||
}
|
|
||||||
|
|
||||||
fn move_camera(keys: Res<ButtonInput<KeyCode>>, mut camera: Single<&mut Transform, With<Camera>>) {
|
|
||||||
const SPEED: f32 = 0.1;
|
|
||||||
if keys.pressed(KeyCode::ArrowLeft) {
|
|
||||||
camera.translation.x += SPEED;
|
|
||||||
} else if keys.pressed(KeyCode::ArrowRight) {
|
|
||||||
camera.translation.x -= SPEED;
|
|
||||||
}
|
|
||||||
|
|
||||||
if keys.pressed(KeyCode::ArrowUp) {
|
|
||||||
camera.translation.y -= SPEED;
|
|
||||||
} else if keys.pressed(KeyCode::ArrowDown) {
|
|
||||||
camera.translation.y += SPEED;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue