2021-06-02 02:59:17 +00:00
|
|
|
use bevy::{
|
|
|
|
core::Time,
|
|
|
|
diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin},
|
|
|
|
ecs::prelude::*,
|
|
|
|
input::Input,
|
2021-07-01 23:48:55 +00:00
|
|
|
math::{Quat, Vec3},
|
2021-07-01 23:54:58 +00:00
|
|
|
pbr2::{PbrBundle, PointLight, PointLightBundle, StandardMaterial},
|
2021-07-01 23:48:55 +00:00
|
|
|
prelude::{App, Assets, BuildChildren, KeyCode, Transform},
|
2021-06-02 02:59:17 +00:00
|
|
|
render2::{
|
|
|
|
camera::PerspectiveCameraBundle,
|
|
|
|
color::Color,
|
|
|
|
mesh::{shape, Mesh},
|
|
|
|
},
|
|
|
|
PipelinedDefaultPlugins,
|
|
|
|
};
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
App::new()
|
|
|
|
.add_plugins(PipelinedDefaultPlugins)
|
|
|
|
.add_plugin(FrameTimeDiagnosticsPlugin::default())
|
|
|
|
.add_plugin(LogDiagnosticsPlugin::default())
|
|
|
|
.add_startup_system(setup.system())
|
|
|
|
.add_system(movement.system())
|
|
|
|
.run();
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Movable;
|
|
|
|
|
|
|
|
/// set up a simple 3D scene
|
|
|
|
fn setup(
|
|
|
|
mut commands: Commands,
|
|
|
|
mut meshes: ResMut<Assets<Mesh>>,
|
|
|
|
mut materials: ResMut<Assets<StandardMaterial>>,
|
|
|
|
) {
|
|
|
|
// plane
|
|
|
|
commands.spawn_bundle(PbrBundle {
|
|
|
|
mesh: meshes.add(Mesh::from(shape::Plane { size: 5.0 })),
|
2021-06-18 18:21:18 +00:00
|
|
|
material: materials.add(StandardMaterial {
|
2021-06-27 23:10:23 +00:00
|
|
|
base_color: Color::INDIGO,
|
|
|
|
perceptual_roughness: 1.0,
|
2021-06-18 18:21:18 +00:00
|
|
|
..Default::default()
|
|
|
|
}),
|
2021-06-02 02:59:17 +00:00
|
|
|
..Default::default()
|
|
|
|
});
|
2021-07-01 23:48:55 +00:00
|
|
|
|
|
|
|
let mut transform = Transform::from_xyz(2.5, 2.5, 0.0);
|
|
|
|
transform.rotate(Quat::from_rotation_z(std::f32::consts::FRAC_PI_2));
|
|
|
|
commands.spawn_bundle(PbrBundle {
|
|
|
|
mesh: meshes.add(Mesh::from(shape::Plane { size: 5.0 })),
|
|
|
|
transform,
|
|
|
|
material: materials.add(StandardMaterial {
|
|
|
|
base_color: Color::INDIGO,
|
|
|
|
perceptual_roughness: 1.0,
|
|
|
|
..Default::default()
|
|
|
|
}),
|
|
|
|
..Default::default()
|
|
|
|
});
|
|
|
|
|
|
|
|
let mut transform = Transform::from_xyz(0.0, 2.5, -2.5);
|
|
|
|
transform.rotate(Quat::from_rotation_x(std::f32::consts::FRAC_PI_2));
|
|
|
|
commands.spawn_bundle(PbrBundle {
|
|
|
|
mesh: meshes.add(Mesh::from(shape::Plane { size: 5.0 })),
|
|
|
|
transform,
|
|
|
|
material: materials.add(StandardMaterial {
|
|
|
|
base_color: Color::INDIGO,
|
|
|
|
perceptual_roughness: 1.0,
|
|
|
|
..Default::default()
|
|
|
|
}),
|
|
|
|
..Default::default()
|
|
|
|
});
|
2021-06-02 02:59:17 +00:00
|
|
|
// cube
|
|
|
|
commands
|
|
|
|
.spawn_bundle(PbrBundle {
|
|
|
|
mesh: meshes.add(Mesh::from(shape::Cube { size: 1.0 })),
|
2021-06-18 18:21:18 +00:00
|
|
|
material: materials.add(StandardMaterial {
|
2021-06-27 23:10:23 +00:00
|
|
|
base_color: Color::PINK,
|
2021-06-18 18:21:18 +00:00
|
|
|
..Default::default()
|
|
|
|
}),
|
2021-06-02 02:59:17 +00:00
|
|
|
transform: Transform::from_xyz(0.0, 1.0, 0.0),
|
|
|
|
..Default::default()
|
|
|
|
})
|
|
|
|
.insert(Movable);
|
|
|
|
// sphere
|
|
|
|
commands
|
|
|
|
.spawn_bundle(PbrBundle {
|
2021-06-18 18:21:18 +00:00
|
|
|
mesh: meshes.add(Mesh::from(shape::UVSphere {
|
|
|
|
radius: 0.5,
|
|
|
|
..Default::default()
|
|
|
|
})),
|
|
|
|
material: materials.add(StandardMaterial {
|
2021-06-27 23:10:23 +00:00
|
|
|
base_color: Color::LIME_GREEN,
|
2021-06-18 18:21:18 +00:00
|
|
|
..Default::default()
|
|
|
|
}),
|
2021-06-02 02:59:17 +00:00
|
|
|
transform: Transform::from_xyz(1.5, 1.0, 1.5),
|
|
|
|
..Default::default()
|
|
|
|
})
|
|
|
|
.insert(Movable);
|
2021-07-01 23:48:55 +00:00
|
|
|
|
2021-06-02 02:59:17 +00:00
|
|
|
// light
|
2021-07-01 23:48:55 +00:00
|
|
|
commands
|
2021-07-01 23:54:58 +00:00
|
|
|
.spawn_bundle(PointLightBundle {
|
2021-07-01 23:48:55 +00:00
|
|
|
// transform: Transform::from_xyz(5.0, 8.0, 2.0),
|
|
|
|
transform: Transform::from_xyz(1.0, 2.0, 0.0),
|
2021-07-01 23:54:58 +00:00
|
|
|
point_light: PointLight {
|
2021-07-01 23:48:55 +00:00
|
|
|
color: Color::RED,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
..Default::default()
|
|
|
|
})
|
|
|
|
.with_children(|builder| {
|
|
|
|
builder.spawn_bundle(PbrBundle {
|
|
|
|
mesh: meshes.add(Mesh::from(shape::UVSphere {
|
|
|
|
radius: 0.1,
|
|
|
|
..Default::default()
|
|
|
|
})),
|
|
|
|
material: materials.add(StandardMaterial {
|
|
|
|
base_color: Color::RED,
|
|
|
|
emissive: Color::rgba_linear(100.0, 0.0, 0.0, 0.0),
|
|
|
|
..Default::default()
|
|
|
|
}),
|
|
|
|
..Default::default()
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// light
|
|
|
|
commands
|
2021-07-01 23:54:58 +00:00
|
|
|
.spawn_bundle(PointLightBundle {
|
2021-07-01 23:48:55 +00:00
|
|
|
// transform: Transform::from_xyz(5.0, 8.0, 2.0),
|
|
|
|
transform: Transform::from_xyz(-1.0, 2.0, 0.0),
|
2021-07-01 23:54:58 +00:00
|
|
|
point_light: PointLight {
|
2021-07-01 23:48:55 +00:00
|
|
|
color: Color::GREEN,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
..Default::default()
|
|
|
|
})
|
|
|
|
.with_children(|builder| {
|
|
|
|
builder.spawn_bundle(PbrBundle {
|
|
|
|
mesh: meshes.add(Mesh::from(shape::UVSphere {
|
|
|
|
radius: 0.1,
|
|
|
|
..Default::default()
|
|
|
|
})),
|
|
|
|
material: materials.add(StandardMaterial {
|
|
|
|
base_color: Color::GREEN,
|
|
|
|
emissive: Color::rgba_linear(0.0, 100.0, 0.0, 0.0),
|
|
|
|
..Default::default()
|
|
|
|
}),
|
|
|
|
..Default::default()
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// light
|
|
|
|
commands
|
2021-07-01 23:54:58 +00:00
|
|
|
.spawn_bundle(PointLightBundle {
|
2021-07-01 23:48:55 +00:00
|
|
|
// transform: Transform::from_xyz(5.0, 8.0, 2.0),
|
|
|
|
transform: Transform::from_xyz(0.0, 4.0, 0.0),
|
2021-07-01 23:54:58 +00:00
|
|
|
point_light: PointLight {
|
2021-07-01 23:48:55 +00:00
|
|
|
color: Color::BLUE,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
..Default::default()
|
|
|
|
})
|
|
|
|
.with_children(|builder| {
|
|
|
|
builder.spawn_bundle(PbrBundle {
|
|
|
|
mesh: meshes.add(Mesh::from(shape::UVSphere {
|
|
|
|
radius: 0.1,
|
|
|
|
..Default::default()
|
|
|
|
})),
|
|
|
|
material: materials.add(StandardMaterial {
|
|
|
|
base_color: Color::BLUE,
|
|
|
|
emissive: Color::rgba_linear(0.0, 0.0, 100.0, 0.0),
|
|
|
|
..Default::default()
|
|
|
|
}),
|
|
|
|
..Default::default()
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-06-02 02:59:17 +00:00
|
|
|
// camera
|
|
|
|
commands.spawn_bundle(PerspectiveCameraBundle {
|
2021-07-01 23:48:55 +00:00
|
|
|
transform: Transform::from_xyz(-2.0, 5.0, 7.5)
|
|
|
|
.looking_at(Vec3::new(0.0, 2.0, 0.0), Vec3::Y),
|
2021-06-02 02:59:17 +00:00
|
|
|
..Default::default()
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn movement(
|
|
|
|
input: Res<Input<KeyCode>>,
|
|
|
|
time: Res<Time>,
|
|
|
|
mut query: Query<&mut Transform, With<Movable>>,
|
|
|
|
) {
|
|
|
|
for mut transform in query.iter_mut() {
|
|
|
|
let mut direction = Vec3::ZERO;
|
|
|
|
if input.pressed(KeyCode::Up) {
|
|
|
|
direction.y += 1.0;
|
|
|
|
}
|
|
|
|
if input.pressed(KeyCode::Down) {
|
|
|
|
direction.y -= 1.0;
|
|
|
|
}
|
|
|
|
if input.pressed(KeyCode::Left) {
|
|
|
|
direction.x -= 1.0;
|
|
|
|
}
|
|
|
|
if input.pressed(KeyCode::Right) {
|
|
|
|
direction.x += 1.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
transform.translation += time.delta_seconds() * 2.0 * direction;
|
|
|
|
}
|
|
|
|
}
|