mirror of
https://github.com/bevyengine/bevy
synced 2025-02-16 14:08:32 +00:00
rustfmt examples
This commit is contained in:
parent
fdbe42dd7d
commit
ee03942e40
6 changed files with 279 additions and 234 deletions
|
@ -1,7 +1,5 @@
|
||||||
use bevy::*;
|
use bevy::*;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
AppBuilder::new()
|
AppBuilder::new().add_defaults().run();
|
||||||
.add_defaults()
|
}
|
||||||
.run();
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,6 +1,10 @@
|
||||||
use bevy::*;
|
use bevy::{
|
||||||
use bevy::{render::*, asset::{Asset, AssetStorage, Handle}, math::{Mat4, Vec3}, Schedulable};
|
asset::{Asset, AssetStorage, Handle},
|
||||||
use rand::{rngs::StdRng, Rng, SeedableRng, random};
|
math::{Mat4, Vec3},
|
||||||
|
render::*,
|
||||||
|
Schedulable, *,
|
||||||
|
};
|
||||||
|
use rand::{random, rngs::StdRng, Rng, SeedableRng};
|
||||||
use std::collections::VecDeque;
|
use std::collections::VecDeque;
|
||||||
|
|
||||||
struct Person;
|
struct Person;
|
||||||
|
@ -21,16 +25,16 @@ struct Wander {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
AppBuilder::new()
|
AppBuilder::new().add_defaults().setup(&setup).run();
|
||||||
.add_defaults()
|
|
||||||
.setup(&setup)
|
|
||||||
.run();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn setup(world: &mut World, scheduler: &mut SystemScheduler<AppStage>) {
|
fn setup(world: &mut World, scheduler: &mut SystemScheduler<AppStage>) {
|
||||||
let cube = Mesh::load(MeshType::Cube);
|
let cube = Mesh::load(MeshType::Cube);
|
||||||
let cube_handle = {
|
let cube_handle = {
|
||||||
let mut mesh_storage = world.resources.get_mut::<AssetStorage<Mesh, MeshType>>().unwrap();
|
let mut mesh_storage = world
|
||||||
|
.resources
|
||||||
|
.get_mut::<AssetStorage<Mesh, MeshType>>()
|
||||||
|
.unwrap();
|
||||||
mesh_storage.add(cube, "cube")
|
mesh_storage.add(cube, "cube")
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -41,54 +45,64 @@ fn setup(world: &mut World, scheduler: &mut SystemScheduler<AppStage>) {
|
||||||
scheduler.add_system(AppStage::Update, build_move_system());
|
scheduler.add_system(AppStage::Update, build_move_system());
|
||||||
scheduler.add_system(AppStage::Update, build_print_status_system());
|
scheduler.add_system(AppStage::Update, build_print_status_system());
|
||||||
|
|
||||||
world.insert((), vec![
|
world.insert(
|
||||||
// lights
|
(),
|
||||||
(
|
vec![
|
||||||
Light {
|
// lights
|
||||||
color: wgpu::Color {
|
(
|
||||||
r: 0.8,
|
Light {
|
||||||
g: 0.8,
|
color: wgpu::Color {
|
||||||
b: 0.5,
|
r: 0.8,
|
||||||
a: 1.0,
|
g: 0.8,
|
||||||
|
b: 0.5,
|
||||||
|
a: 1.0,
|
||||||
|
},
|
||||||
|
fov: f32::to_radians(60.0),
|
||||||
|
depth: 0.1..50.0,
|
||||||
|
target_view: None,
|
||||||
},
|
},
|
||||||
fov: f32::to_radians(60.0),
|
Material::new(math::vec4(0.5, 0.3, 0.3, 1.0)),
|
||||||
depth: 0.1 .. 50.0,
|
LocalToWorld::identity(),
|
||||||
target_view: None,
|
Translation::new(4.0, -4.0, 5.0),
|
||||||
},
|
Rotation::from_euler_angles(0.0, 0.0, 0.0),
|
||||||
Material::new(math::vec4(0.5, 0.3, 0.3, 1.0)),
|
),
|
||||||
LocalToWorld::identity(),
|
],
|
||||||
Translation::new(4.0, -4.0, 5.0),
|
);
|
||||||
Rotation::from_euler_angles(0.0, 0.0, 0.0)
|
|
||||||
),
|
|
||||||
]);
|
|
||||||
|
|
||||||
world.insert((), vec![
|
world.insert(
|
||||||
// camera
|
(),
|
||||||
(
|
vec![
|
||||||
Camera::new(CameraType::Projection {
|
// camera
|
||||||
fov: std::f32::consts::PI / 4.0,
|
(
|
||||||
near: 1.0,
|
Camera::new(CameraType::Projection {
|
||||||
far: 1000.0,
|
fov: std::f32::consts::PI / 4.0,
|
||||||
aspect_ratio: 1.0,
|
near: 1.0,
|
||||||
}),
|
far: 1000.0,
|
||||||
ActiveCamera,
|
aspect_ratio: 1.0,
|
||||||
LocalToWorld(Mat4::look_at_rh(
|
}),
|
||||||
Vec3::new(6.0, -40.0, 20.0),
|
ActiveCamera,
|
||||||
Vec3::new(0.0, 0.0, 0.0),
|
LocalToWorld(Mat4::look_at_rh(
|
||||||
Vec3::new(0.0, 0.0, 1.0),)),
|
Vec3::new(6.0, -40.0, 20.0),
|
||||||
)
|
Vec3::new(0.0, 0.0, 0.0),
|
||||||
]);
|
Vec3::new(0.0, 0.0, 1.0),
|
||||||
|
)),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
|
||||||
let mut rng = StdRng::from_entropy();
|
let mut rng = StdRng::from_entropy();
|
||||||
for _ in 0 .. 70000 {
|
for _ in 0..70000 {
|
||||||
create_person(world, cube_handle.clone(),
|
create_person(
|
||||||
Translation::new(rng.gen_range(-50.0, 50.0), 0.0, rng.gen_range(-50.0, 50.0)));
|
world,
|
||||||
|
cube_handle.clone(),
|
||||||
|
Translation::new(rng.gen_range(-50.0, 50.0), 0.0, rng.gen_range(-50.0, 50.0)),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn build_wander_system() -> Box<dyn Schedulable> {
|
fn build_wander_system() -> Box<dyn Schedulable> {
|
||||||
let mut rng = StdRng::from_entropy();
|
let mut rng = StdRng::from_entropy();
|
||||||
|
|
||||||
SystemBuilder::new("Wander")
|
SystemBuilder::new("Wander")
|
||||||
.read_resource::<Time>()
|
.read_resource::<Time>()
|
||||||
.with_query(<(
|
.with_query(<(
|
||||||
|
@ -97,10 +111,7 @@ fn build_wander_system() -> Box<dyn Schedulable> {
|
||||||
Write<Wander>,
|
Write<Wander>,
|
||||||
Write<NavigationPoint>,
|
Write<NavigationPoint>,
|
||||||
)>::query())
|
)>::query())
|
||||||
.build(move |
|
.build(move |_, world, time, person_query| {
|
||||||
_, world,
|
|
||||||
time ,
|
|
||||||
person_query| {
|
|
||||||
for (_, translation, mut wander, mut navigation_point) in person_query.iter(world) {
|
for (_, translation, mut wander, mut navigation_point) in person_query.iter(world) {
|
||||||
wander.elapsed += time.delta_seconds;
|
wander.elapsed += time.delta_seconds;
|
||||||
if wander.elapsed >= wander.duration {
|
if wander.elapsed >= wander.duration {
|
||||||
|
@ -108,11 +119,14 @@ fn build_wander_system() -> Box<dyn Schedulable> {
|
||||||
rng.gen_range(-1.0, 1.0),
|
rng.gen_range(-1.0, 1.0),
|
||||||
rng.gen_range(-1.0, 1.0),
|
rng.gen_range(-1.0, 1.0),
|
||||||
rng.gen_range(0.0, 0.001),
|
rng.gen_range(0.0, 0.001),
|
||||||
).normalize();
|
)
|
||||||
let distance = rng.gen_range(wander.distance_bounds.x(), wander.distance_bounds.y());
|
.normalize();
|
||||||
|
let distance =
|
||||||
|
rng.gen_range(wander.distance_bounds.x(), wander.distance_bounds.y());
|
||||||
navigation_point.target = translation.0 + direction * distance;
|
navigation_point.target = translation.0 + direction * distance;
|
||||||
wander.elapsed = 0.0;
|
wander.elapsed = 0.0;
|
||||||
wander.duration = rng.gen_range(wander.duration_bounds.x(), wander.duration_bounds.y());
|
wander.duration =
|
||||||
|
rng.gen_range(wander.duration_bounds.x(), wander.duration_bounds.y());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
@ -126,9 +140,7 @@ fn build_navigate_system() -> Box<dyn Schedulable> {
|
||||||
Write<Velocity>,
|
Write<Velocity>,
|
||||||
Write<NavigationPoint>,
|
Write<NavigationPoint>,
|
||||||
)>::query())
|
)>::query())
|
||||||
.build(move |
|
.build(move |_, world, _, person_query| {
|
||||||
_, world,
|
|
||||||
_, person_query| {
|
|
||||||
for (_, translation, mut velocity, navigation_point) in person_query.iter(world) {
|
for (_, translation, mut velocity, navigation_point) in person_query.iter(world) {
|
||||||
let distance = navigation_point.target - translation.0;
|
let distance = navigation_point.target - translation.0;
|
||||||
if distance.length() > 0.01 {
|
if distance.length() > 0.01 {
|
||||||
|
@ -144,11 +156,8 @@ fn build_navigate_system() -> Box<dyn Schedulable> {
|
||||||
fn build_move_system() -> Box<dyn Schedulable> {
|
fn build_move_system() -> Box<dyn Schedulable> {
|
||||||
SystemBuilder::new("Move")
|
SystemBuilder::new("Move")
|
||||||
.read_resource::<Time>()
|
.read_resource::<Time>()
|
||||||
.with_query(<(
|
.with_query(<(Write<Translation>, Read<Velocity>)>::query())
|
||||||
Write<Translation>,
|
.build(move |_, world, time, person_query| {
|
||||||
Read<Velocity>,
|
|
||||||
)>::query())
|
|
||||||
.build(move |_, world, time , person_query| {
|
|
||||||
for (mut translation, velocity) in person_query.iter(world) {
|
for (mut translation, velocity) in person_query.iter(world) {
|
||||||
translation.0 += velocity.value * time.delta_seconds;
|
translation.0 += velocity.value * time.delta_seconds;
|
||||||
}
|
}
|
||||||
|
@ -163,10 +172,8 @@ fn build_print_status_system() -> Box<dyn Schedulable> {
|
||||||
let mut frame_time_values = VecDeque::new();
|
let mut frame_time_values = VecDeque::new();
|
||||||
SystemBuilder::new("PrintStatus")
|
SystemBuilder::new("PrintStatus")
|
||||||
.read_resource::<Time>()
|
.read_resource::<Time>()
|
||||||
.with_query(<(
|
.with_query(<(Read<Person>,)>::query())
|
||||||
Read<Person>,
|
.build(move |_, world, time, person_query| {
|
||||||
)>::query())
|
|
||||||
.build(move |_, world, time , person_query| {
|
|
||||||
elapsed += time.delta_seconds;
|
elapsed += time.delta_seconds;
|
||||||
frame_time_values.push_front(time.delta_seconds);
|
frame_time_values.push_front(time.delta_seconds);
|
||||||
frame_time_total += time.delta_seconds;
|
frame_time_total += time.delta_seconds;
|
||||||
|
@ -177,8 +184,11 @@ fn build_print_status_system() -> Box<dyn Schedulable> {
|
||||||
}
|
}
|
||||||
if elapsed > 1.0 {
|
if elapsed > 1.0 {
|
||||||
// println!("fps: {}", if time.delta_seconds == 0.0 { 0.0 } else { 1.0 / time.delta_seconds });
|
// println!("fps: {}", if time.delta_seconds == 0.0 { 0.0 } else { 1.0 / time.delta_seconds });
|
||||||
if frame_time_count > 0 && frame_time_total > 0.0 {
|
if frame_time_count > 0 && frame_time_total > 0.0 {
|
||||||
println!("fps: {}", 1.0 / (frame_time_total / frame_time_count as f32))
|
println!(
|
||||||
|
"fps: {}",
|
||||||
|
1.0 / (frame_time_total / frame_time_count as f32)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
println!("peeps: {}", person_query.iter(world).count());
|
println!("peeps: {}", person_query.iter(world).count());
|
||||||
elapsed = 0.0;
|
elapsed = 0.0;
|
||||||
|
@ -187,9 +197,10 @@ fn build_print_status_system() -> Box<dyn Schedulable> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn create_person(world: &mut World, mesh_handle: Handle<Mesh>, translation: Translation) {
|
fn create_person(world: &mut World, mesh_handle: Handle<Mesh>, translation: Translation) {
|
||||||
world.insert((), vec![
|
world.insert(
|
||||||
(
|
(),
|
||||||
Person{},
|
vec![(
|
||||||
|
Person {},
|
||||||
Wander {
|
Wander {
|
||||||
duration_bounds: math::vec2(3.0, 10.0),
|
duration_bounds: math::vec2(3.0, 10.0),
|
||||||
distance_bounds: math::vec2(-50.0, 50.0),
|
distance_bounds: math::vec2(-50.0, 50.0),
|
||||||
|
@ -206,7 +217,7 @@ fn create_person(world: &mut World, mesh_handle: Handle<Mesh>, translation: Tran
|
||||||
Material::new(math::vec4(0.5, 0.3, 0.3, 1.0) * random::<f32>()),
|
Material::new(math::vec4(0.5, 0.3, 0.3, 1.0) * random::<f32>()),
|
||||||
mesh_handle,
|
mesh_handle,
|
||||||
LocalToWorld::identity(),
|
LocalToWorld::identity(),
|
||||||
translation
|
translation,
|
||||||
),
|
)],
|
||||||
]);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,5 @@ use bevy::*;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
asset::load_gltf("examples/assets/Box.gltf").unwrap();
|
asset::load_gltf("examples/assets/Box.gltf").unwrap();
|
||||||
AppBuilder::new()
|
AppBuilder::new().add_defaults().run();
|
||||||
.add_defaults()
|
}
|
||||||
.run();
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,22 +1,21 @@
|
||||||
use bevy::{*, render::*, asset::{Asset, AssetStorage}, math::{Mat4, Quat, Vec3}, Schedulable, Parent};
|
use bevy::{
|
||||||
|
asset::{Asset, AssetStorage},
|
||||||
|
math::{Mat4, Quat, Vec3},
|
||||||
|
render::*,
|
||||||
|
Parent, Schedulable, *,
|
||||||
|
};
|
||||||
|
|
||||||
struct Rotator;
|
struct Rotator;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
AppBuilder::new()
|
AppBuilder::new().add_defaults().setup(&setup).run();
|
||||||
.add_defaults()
|
|
||||||
.setup(&setup)
|
|
||||||
.run();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn build_rotator_system() -> Box<dyn Schedulable> {
|
fn build_rotator_system() -> Box<dyn Schedulable> {
|
||||||
SystemBuilder::new("Rotator")
|
SystemBuilder::new("Rotator")
|
||||||
.read_resource::<Time>()
|
.read_resource::<Time>()
|
||||||
.with_query(<(
|
.with_query(<(Write<Rotator>, Write<Rotation>)>::query())
|
||||||
Write<Rotator>,
|
.build(move |_, world, time, light_query| {
|
||||||
Write<Rotation>,
|
|
||||||
)>::query())
|
|
||||||
.build(move |_, world, time , light_query| {
|
|
||||||
for (_, mut rotation) in light_query.iter(world) {
|
for (_, mut rotation) in light_query.iter(world) {
|
||||||
rotation.0 = rotation.0 * Quat::from_rotation_x(3.0 * time.delta_seconds);
|
rotation.0 = rotation.0 * Quat::from_rotation_x(3.0 * time.delta_seconds);
|
||||||
}
|
}
|
||||||
|
@ -25,11 +24,17 @@ fn build_rotator_system() -> Box<dyn Schedulable> {
|
||||||
|
|
||||||
fn setup(world: &mut World, scheduler: &mut SystemScheduler<AppStage>) {
|
fn setup(world: &mut World, scheduler: &mut SystemScheduler<AppStage>) {
|
||||||
let cube = Mesh::load(MeshType::Cube);
|
let cube = Mesh::load(MeshType::Cube);
|
||||||
let plane = Mesh::load(MeshType::Plane{ size: 10.0 });
|
let plane = Mesh::load(MeshType::Plane { size: 10.0 });
|
||||||
|
|
||||||
let (cube_handle, plane_handle) = {
|
let (cube_handle, plane_handle) = {
|
||||||
let mut mesh_storage = world.resources.get_mut::<AssetStorage<Mesh, MeshType>>().unwrap();
|
let mut mesh_storage = world
|
||||||
(mesh_storage.add(cube, "cube"), mesh_storage.add(plane, "plane"))
|
.resources
|
||||||
|
.get_mut::<AssetStorage<Mesh, MeshType>>()
|
||||||
|
.unwrap();
|
||||||
|
(
|
||||||
|
mesh_storage.add(cube, "cube"),
|
||||||
|
mesh_storage.add(plane, "plane"),
|
||||||
|
)
|
||||||
};
|
};
|
||||||
|
|
||||||
let transform_system_bundle = transform_system_bundle::build(world);
|
let transform_system_bundle = transform_system_bundle::build(world);
|
||||||
|
@ -37,42 +42,49 @@ fn setup(world: &mut World, scheduler: &mut SystemScheduler<AppStage>) {
|
||||||
scheduler.add_system(AppStage::Update, build_rotator_system());
|
scheduler.add_system(AppStage::Update, build_rotator_system());
|
||||||
|
|
||||||
// plane
|
// plane
|
||||||
world.insert((), vec![
|
world.insert(
|
||||||
(
|
(),
|
||||||
|
vec![(
|
||||||
plane_handle.clone(),
|
plane_handle.clone(),
|
||||||
Material::new(math::vec4(0.1, 0.2, 0.1, 1.0)),
|
Material::new(math::vec4(0.1, 0.2, 0.1, 1.0)),
|
||||||
LocalToWorld::identity(),
|
LocalToWorld::identity(),
|
||||||
Translation::new(0.0, 0.0, -5.0)
|
Translation::new(0.0, 0.0, -5.0),
|
||||||
),
|
)],
|
||||||
]);
|
);
|
||||||
|
|
||||||
// cube
|
|
||||||
let parent_cube = *world.insert((), vec![
|
|
||||||
(
|
|
||||||
cube_handle.clone(),
|
|
||||||
Material::new(math::vec4(0.5, 0.3, 0.3, 1.0)),
|
|
||||||
LocalToWorld::identity(),
|
|
||||||
Translation::new(0.0, 0.0, 1.0),
|
|
||||||
Rotation::from_euler_angles(0.0, 0.0, 0.0),
|
|
||||||
Rotator,
|
|
||||||
)
|
|
||||||
]).first().unwrap();
|
|
||||||
|
|
||||||
// cube
|
// cube
|
||||||
world.insert((), vec![
|
let parent_cube = *world
|
||||||
(
|
.insert(
|
||||||
|
(),
|
||||||
|
vec![(
|
||||||
|
cube_handle.clone(),
|
||||||
|
Material::new(math::vec4(0.5, 0.3, 0.3, 1.0)),
|
||||||
|
LocalToWorld::identity(),
|
||||||
|
Translation::new(0.0, 0.0, 1.0),
|
||||||
|
Rotation::from_euler_angles(0.0, 0.0, 0.0),
|
||||||
|
Rotator,
|
||||||
|
)],
|
||||||
|
)
|
||||||
|
.first()
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
// cube
|
||||||
|
world.insert(
|
||||||
|
(),
|
||||||
|
vec![(
|
||||||
cube_handle,
|
cube_handle,
|
||||||
Material::new(math::vec4(0.5, 0.3, 0.3, 1.0)),
|
Material::new(math::vec4(0.5, 0.3, 0.3, 1.0)),
|
||||||
LocalToWorld::identity(),
|
LocalToWorld::identity(),
|
||||||
Translation::new(0.0, 0.0, 3.0),
|
Translation::new(0.0, 0.0, 3.0),
|
||||||
Parent(parent_cube),
|
Parent(parent_cube),
|
||||||
LocalToParent::identity(),
|
LocalToParent::identity(),
|
||||||
)
|
)],
|
||||||
]);
|
);
|
||||||
|
|
||||||
// light
|
// light
|
||||||
world.insert((), vec![
|
world.insert(
|
||||||
(
|
(),
|
||||||
|
vec![(
|
||||||
Light {
|
Light {
|
||||||
color: wgpu::Color {
|
color: wgpu::Color {
|
||||||
r: 0.8,
|
r: 0.8,
|
||||||
|
@ -81,19 +93,20 @@ fn setup(world: &mut World, scheduler: &mut SystemScheduler<AppStage>) {
|
||||||
a: 1.0,
|
a: 1.0,
|
||||||
},
|
},
|
||||||
fov: f32::to_radians(60.0),
|
fov: f32::to_radians(60.0),
|
||||||
depth: 0.1 .. 50.0,
|
depth: 0.1..50.0,
|
||||||
target_view: None,
|
target_view: None,
|
||||||
},
|
},
|
||||||
Material::new(math::vec4(0.5, 0.3, 0.3, 1.0)),
|
Material::new(math::vec4(0.5, 0.3, 0.3, 1.0)),
|
||||||
LocalToWorld::identity(),
|
LocalToWorld::identity(),
|
||||||
Translation::new(4.0, -4.0, 5.0),
|
Translation::new(4.0, -4.0, 5.0),
|
||||||
Rotation::from_euler_angles(0.0, 0.0, 0.0)
|
Rotation::from_euler_angles(0.0, 0.0, 0.0),
|
||||||
),
|
)],
|
||||||
]);
|
);
|
||||||
|
|
||||||
// camera
|
// camera
|
||||||
world.insert((), vec![
|
world.insert(
|
||||||
(
|
(),
|
||||||
|
vec![(
|
||||||
Camera::new(CameraType::Projection {
|
Camera::new(CameraType::Projection {
|
||||||
fov: std::f32::consts::PI / 4.0,
|
fov: std::f32::consts::PI / 4.0,
|
||||||
near: 1.0,
|
near: 1.0,
|
||||||
|
@ -104,7 +117,8 @@ fn setup(world: &mut World, scheduler: &mut SystemScheduler<AppStage>) {
|
||||||
LocalToWorld(Mat4::look_at_rh(
|
LocalToWorld(Mat4::look_at_rh(
|
||||||
Vec3::new(3.0, -15.0, 8.0),
|
Vec3::new(3.0, -15.0, 8.0),
|
||||||
Vec3::new(0.0, 0.0, 0.0),
|
Vec3::new(0.0, 0.0, 0.0),
|
||||||
Vec3::new(0.0, 0.0, 1.0),)),
|
Vec3::new(0.0, 0.0, 1.0),
|
||||||
)
|
)),
|
||||||
]);
|
)],
|
||||||
}
|
);
|
||||||
|
}
|
||||||
|
|
|
@ -1,47 +1,58 @@
|
||||||
use bevy::{*, render::*, asset::{Asset, AssetStorage}, math::{Mat4, Vec3}};
|
use bevy::{
|
||||||
|
asset::{Asset, AssetStorage},
|
||||||
|
math::{Mat4, Vec3},
|
||||||
|
render::*,
|
||||||
|
*,
|
||||||
|
};
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
AppBuilder::new()
|
AppBuilder::new().add_defaults().setup(&setup).run();
|
||||||
.add_defaults()
|
|
||||||
.setup(&setup)
|
|
||||||
.run();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn setup(world: &mut World, scheduler: &mut SystemScheduler<AppStage>) {
|
fn setup(world: &mut World, scheduler: &mut SystemScheduler<AppStage>) {
|
||||||
let cube = Mesh::load(MeshType::Cube);
|
let cube = Mesh::load(MeshType::Cube);
|
||||||
let plane = Mesh::load(MeshType::Plane{ size: 10.0 });
|
let plane = Mesh::load(MeshType::Plane { size: 10.0 });
|
||||||
|
|
||||||
let (cube_handle, plane_handle) = {
|
let (cube_handle, plane_handle) = {
|
||||||
let mut mesh_storage = world.resources.get_mut::<AssetStorage<Mesh, MeshType>>().unwrap();
|
let mut mesh_storage = world
|
||||||
(mesh_storage.add(cube, "cube"), mesh_storage.add(plane, "plane"))
|
.resources
|
||||||
|
.get_mut::<AssetStorage<Mesh, MeshType>>()
|
||||||
|
.unwrap();
|
||||||
|
(
|
||||||
|
mesh_storage.add(cube, "cube"),
|
||||||
|
mesh_storage.add(plane, "plane"),
|
||||||
|
)
|
||||||
};
|
};
|
||||||
|
|
||||||
let transform_system_bundle = transform_system_bundle::build(world);
|
let transform_system_bundle = transform_system_bundle::build(world);
|
||||||
scheduler.add_systems(AppStage::Update, transform_system_bundle);
|
scheduler.add_systems(AppStage::Update, transform_system_bundle);
|
||||||
|
|
||||||
// plane
|
// plane
|
||||||
world.insert((), vec![
|
world.insert(
|
||||||
(
|
(),
|
||||||
|
vec![(
|
||||||
plane_handle.clone(),
|
plane_handle.clone(),
|
||||||
Material::new(math::vec4(0.1, 0.2, 0.1, 1.0)),
|
Material::new(math::vec4(0.1, 0.2, 0.1, 1.0)),
|
||||||
LocalToWorld::identity(),
|
LocalToWorld::identity(),
|
||||||
Translation::new(0.0, 0.0, 0.0)
|
Translation::new(0.0, 0.0, 0.0),
|
||||||
),
|
)],
|
||||||
]);
|
);
|
||||||
|
|
||||||
// cube
|
// cube
|
||||||
world.insert((), vec![
|
world.insert(
|
||||||
(
|
(),
|
||||||
|
vec![(
|
||||||
cube_handle,
|
cube_handle,
|
||||||
Material::new(math::vec4(0.5, 0.3, 0.3, 1.0)),
|
Material::new(math::vec4(0.5, 0.3, 0.3, 1.0)),
|
||||||
LocalToWorld::identity(),
|
LocalToWorld::identity(),
|
||||||
Translation::new(0.0, 0.0, 1.0)
|
Translation::new(0.0, 0.0, 1.0),
|
||||||
)
|
)],
|
||||||
]);
|
);
|
||||||
|
|
||||||
// light
|
// light
|
||||||
world.insert((), vec![
|
world.insert(
|
||||||
(
|
(),
|
||||||
|
vec![(
|
||||||
Light {
|
Light {
|
||||||
color: wgpu::Color {
|
color: wgpu::Color {
|
||||||
r: 0.8,
|
r: 0.8,
|
||||||
|
@ -50,19 +61,20 @@ fn setup(world: &mut World, scheduler: &mut SystemScheduler<AppStage>) {
|
||||||
a: 1.0,
|
a: 1.0,
|
||||||
},
|
},
|
||||||
fov: f32::to_radians(60.0),
|
fov: f32::to_radians(60.0),
|
||||||
depth: 0.1 .. 50.0,
|
depth: 0.1..50.0,
|
||||||
target_view: None,
|
target_view: None,
|
||||||
},
|
},
|
||||||
Material::new(math::vec4(0.5, 0.3, 0.3, 1.0)),
|
Material::new(math::vec4(0.5, 0.3, 0.3, 1.0)),
|
||||||
LocalToWorld::identity(),
|
LocalToWorld::identity(),
|
||||||
Translation::new(4.0, -4.0, 5.0),
|
Translation::new(4.0, -4.0, 5.0),
|
||||||
Rotation::from_euler_angles(0.0, 0.0, 0.0)
|
Rotation::from_euler_angles(0.0, 0.0, 0.0),
|
||||||
),
|
)],
|
||||||
]);
|
);
|
||||||
|
|
||||||
// camera
|
// camera
|
||||||
world.insert((), vec![
|
world.insert(
|
||||||
(
|
(),
|
||||||
|
vec![(
|
||||||
Camera::new(CameraType::Projection {
|
Camera::new(CameraType::Projection {
|
||||||
fov: std::f32::consts::PI / 4.0,
|
fov: std::f32::consts::PI / 4.0,
|
||||||
near: 1.0,
|
near: 1.0,
|
||||||
|
@ -73,7 +85,8 @@ fn setup(world: &mut World, scheduler: &mut SystemScheduler<AppStage>) {
|
||||||
LocalToWorld(Mat4::look_at_rh(
|
LocalToWorld(Mat4::look_at_rh(
|
||||||
Vec3::new(3.0, -8.0, 5.0),
|
Vec3::new(3.0, -8.0, 5.0),
|
||||||
Vec3::new(0.0, 0.0, 0.0),
|
Vec3::new(0.0, 0.0, 0.0),
|
||||||
Vec3::new(0.0, 0.0, 1.0),)),
|
Vec3::new(0.0, 0.0, 1.0),
|
||||||
)
|
)),
|
||||||
]);
|
)],
|
||||||
}
|
);
|
||||||
|
}
|
||||||
|
|
159
examples/ui.rs
159
examples/ui.rs
|
@ -1,32 +1,39 @@
|
||||||
use bevy::{*, render::*, asset::{Asset, AssetStorage}, math::{Mat4, Vec3}};
|
use bevy::{
|
||||||
|
asset::{Asset, AssetStorage},
|
||||||
|
math::{Mat4, Vec3},
|
||||||
|
render::*,
|
||||||
|
*,
|
||||||
|
};
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
AppBuilder::new()
|
AppBuilder::new().add_defaults().setup(&setup).run();
|
||||||
.add_defaults()
|
|
||||||
.setup(&setup)
|
|
||||||
.run();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn setup(world: &mut World, scheduler: &mut SystemScheduler<AppStage>) {
|
fn setup(world: &mut World, scheduler: &mut SystemScheduler<AppStage>) {
|
||||||
let cube = Mesh::load(MeshType::Cube);
|
let cube = Mesh::load(MeshType::Cube);
|
||||||
let cube_handle = {
|
let cube_handle = {
|
||||||
let mut mesh_storage = world.resources.get_mut::<AssetStorage<Mesh, MeshType>>().unwrap();
|
let mut mesh_storage = world
|
||||||
|
.resources
|
||||||
|
.get_mut::<AssetStorage<Mesh, MeshType>>()
|
||||||
|
.unwrap();
|
||||||
mesh_storage.add(cube, "cube")
|
mesh_storage.add(cube, "cube")
|
||||||
};
|
};
|
||||||
|
|
||||||
let transform_system_bundle = transform_system_bundle::build(world);
|
let transform_system_bundle = transform_system_bundle::build(world);
|
||||||
scheduler.add_systems(AppStage::Update, transform_system_bundle);
|
scheduler.add_systems(AppStage::Update, transform_system_bundle);
|
||||||
|
|
||||||
world.insert((), vec![
|
world.insert(
|
||||||
(
|
(),
|
||||||
|
vec![(
|
||||||
cube_handle,
|
cube_handle,
|
||||||
LocalToWorld::identity(),
|
LocalToWorld::identity(),
|
||||||
Material::new(math::vec4(0.5, 0.3, 0.3, 1.0)),
|
Material::new(math::vec4(0.5, 0.3, 0.3, 1.0)),
|
||||||
)
|
)],
|
||||||
]);
|
);
|
||||||
|
|
||||||
world.insert((), vec![
|
world.insert(
|
||||||
(
|
(),
|
||||||
|
vec![(
|
||||||
Light {
|
Light {
|
||||||
color: wgpu::Color {
|
color: wgpu::Color {
|
||||||
r: 0.8,
|
r: 0.8,
|
||||||
|
@ -35,74 +42,78 @@ fn setup(world: &mut World, scheduler: &mut SystemScheduler<AppStage>) {
|
||||||
a: 1.0,
|
a: 1.0,
|
||||||
},
|
},
|
||||||
fov: f32::to_radians(60.0),
|
fov: f32::to_radians(60.0),
|
||||||
depth: 0.1 .. 50.0,
|
depth: 0.1..50.0,
|
||||||
target_view: None,
|
target_view: None,
|
||||||
},
|
},
|
||||||
LocalToWorld::identity(),
|
LocalToWorld::identity(),
|
||||||
Translation::new(4.0, -4.0, 5.0),
|
Translation::new(4.0, -4.0, 5.0),
|
||||||
Rotation::from_euler_angles(0.0, 0.0, 0.0)
|
Rotation::from_euler_angles(0.0, 0.0, 0.0),
|
||||||
),
|
)],
|
||||||
]);
|
);
|
||||||
|
|
||||||
world.insert((), vec![
|
world.insert(
|
||||||
// camera
|
(),
|
||||||
(
|
vec![
|
||||||
Camera::new(CameraType::Projection {
|
// camera
|
||||||
fov: std::f32::consts::PI / 4.0,
|
(
|
||||||
near: 1.0,
|
Camera::new(CameraType::Projection {
|
||||||
far: 1000.0,
|
fov: std::f32::consts::PI / 4.0,
|
||||||
aspect_ratio: 1.0,
|
near: 1.0,
|
||||||
}),
|
far: 1000.0,
|
||||||
ActiveCamera,
|
aspect_ratio: 1.0,
|
||||||
LocalToWorld(Mat4::look_at_rh(
|
}),
|
||||||
Vec3::new(3.0, -5.0, 3.0),
|
ActiveCamera,
|
||||||
Vec3::new(0.0, 0.0, 0.0),
|
LocalToWorld(Mat4::look_at_rh(
|
||||||
Vec3::new(0.0, 0.0, 1.0),)),
|
Vec3::new(3.0, -5.0, 3.0),
|
||||||
)
|
Vec3::new(0.0, 0.0, 0.0),
|
||||||
]);
|
Vec3::new(0.0, 0.0, 1.0),
|
||||||
|
)),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
|
||||||
world.insert((), vec![
|
world.insert(
|
||||||
// camera
|
(),
|
||||||
(
|
vec![
|
||||||
Camera::new(CameraType::Orthographic {
|
// camera
|
||||||
left: 0.0,
|
(
|
||||||
right: 0.0,
|
Camera::new(CameraType::Orthographic {
|
||||||
bottom: 0.0,
|
left: 0.0,
|
||||||
top: 0.0,
|
right: 0.0,
|
||||||
near: 0.0,
|
bottom: 0.0,
|
||||||
far: 1.0,
|
top: 0.0,
|
||||||
}),
|
near: 0.0,
|
||||||
ActiveCamera2d,
|
far: 1.0,
|
||||||
)
|
}),
|
||||||
]);
|
ActiveCamera2d,
|
||||||
|
),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
|
||||||
world.insert((), vec![
|
world.insert(
|
||||||
(
|
(),
|
||||||
Rect {
|
vec![(Rect {
|
||||||
position: math::vec2(75.0, 75.0),
|
position: math::vec2(75.0, 75.0),
|
||||||
dimensions: math::vec2(100.0, 100.0),
|
dimensions: math::vec2(100.0, 100.0),
|
||||||
color: math::vec4(0.0, 1.0, 0.0, 1.0),
|
color: math::vec4(0.0, 1.0, 0.0, 1.0),
|
||||||
},
|
},)],
|
||||||
)
|
);
|
||||||
]);
|
|
||||||
|
|
||||||
world.insert((), vec![
|
world.insert(
|
||||||
(
|
(),
|
||||||
Rect {
|
vec![(Rect {
|
||||||
position: math::vec2(50.0, 50.0),
|
position: math::vec2(50.0, 50.0),
|
||||||
dimensions: math::vec2(100.0, 100.0),
|
dimensions: math::vec2(100.0, 100.0),
|
||||||
color: math::vec4(1.0, 0.0, 0.0, 1.0),
|
color: math::vec4(1.0, 0.0, 0.0, 1.0),
|
||||||
},
|
},)],
|
||||||
)
|
);
|
||||||
]);
|
|
||||||
|
|
||||||
world.insert((), vec![
|
world.insert(
|
||||||
(
|
(),
|
||||||
Rect {
|
vec![(Rect {
|
||||||
position: math::vec2(100.0, 100.0),
|
position: math::vec2(100.0, 100.0),
|
||||||
dimensions: math::vec2(100.0, 100.0),
|
dimensions: math::vec2(100.0, 100.0),
|
||||||
color: math::vec4(0.0, 0.0, 1.0, 1.0),
|
color: math::vec4(0.0, 0.0, 1.0, 1.0),
|
||||||
},
|
},)],
|
||||||
)
|
);
|
||||||
]);
|
}
|
||||||
}
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue