From 3edb3687cb52a3f9cec779f786d24ec34c2fd049 Mon Sep 17 00:00:00 2001 From: Elijah Voigt Date: Mon, 22 Sep 2025 21:02:30 -0700 Subject: [PATCH] Parallax3d with on-screen buttons --- -rust-analyzer.log | 0 Cargo.toml | 8 ++ examples/{ => demos}/parallax2d.rs | 20 ++++- examples/demos/parallax3d.rs | 138 +++++++++++++++++++++++++++++ examples/parallax3d.rs | 67 -------------- examples/scroll.rs | 9 +- 6 files changed, 168 insertions(+), 74 deletions(-) delete mode 100644 -rust-analyzer.log rename examples/{ => demos}/parallax2d.rs (82%) create mode 100644 examples/demos/parallax3d.rs delete mode 100644 examples/parallax3d.rs diff --git a/-rust-analyzer.log b/-rust-analyzer.log deleted file mode 100644 index e69de29..0000000 diff --git a/Cargo.toml b/Cargo.toml index a348cbf..cd4099c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,6 +3,14 @@ name = "games" version = "0.1.0" edition = "2024" +[[example]] +name = "demo_parallax2d" +path = "examples/demos/parallax2d.rs" + +[[example]] +name = "demo_parallax3d" +path = "examples/demos/parallax3d.rs" + [features] hide_debug = [] diff --git a/examples/parallax2d.rs b/examples/demos/parallax2d.rs similarity index 82% rename from examples/parallax2d.rs rename to examples/demos/parallax2d.rs index 83e82f1..23f625e 100644 --- a/examples/parallax2d.rs +++ b/examples/demos/parallax2d.rs @@ -31,7 +31,11 @@ fn setup_2d( MeshMaterial2d(materials.add(Color::from(RED))), Transform::from_xyz(0.0, 0.0, -1.0), ParallaxDistance(1.0), - children![(Text2d::new("Parallax Distance: 1"), TextColor(BLACK.into()), Transform::from_xyz(150.0, 50.0, 0.0))], + children![( + Text2d::new("Parallax Distance: 1"), + TextColor(BLACK.into()), + Transform::from_xyz(150.0, 50.0, 0.0) + )], )); commands.spawn(( @@ -39,7 +43,11 @@ fn setup_2d( MeshMaterial2d(materials.add(Color::from(GREEN))), Transform::from_xyz(0.0, 0.0, -5.0), ParallaxDistance(5.0), - children![(Text2d::new("Parallax Distance: 5"), TextColor(BLACK.into()), Transform::from_xyz(150.0, 0.0, 0.0))], + children![( + Text2d::new("Parallax Distance: 5"), + TextColor(BLACK.into()), + Transform::from_xyz(150.0, 0.0, 0.0) + )], )); commands.spawn(( @@ -47,7 +55,11 @@ fn setup_2d( MeshMaterial2d(materials.add(Color::from(BLUE))), Transform::from_xyz(0.0, 0.0, -10.0), ParallaxDistance(10.0), - children![(Text2d::new("Parallax Distance: 10"), TextColor(BLACK.into()), Transform::from_xyz(150.0, -50.0, 0.0))], + children![( + Text2d::new("Parallax Distance: 10"), + TextColor(BLACK.into()), + Transform::from_xyz(150.0, -50.0, 0.0) + )], )); } @@ -72,7 +84,7 @@ struct CameraMovement(Vec3); fn track_camera_movement( mut events: EventWriter, camera: Query<&Transform, (With, Changed)>, - mut last: Local + mut last: Local, ) { camera.iter().for_each(|Transform { translation, .. }| { events.write(CameraMovement(*last - *translation)); diff --git a/examples/demos/parallax3d.rs b/examples/demos/parallax3d.rs new file mode 100644 index 0000000..891aee4 --- /dev/null +++ b/examples/demos/parallax3d.rs @@ -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>, + mut materials: ResMut>, +) { + 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, With