quad uses size. polish examples
This commit is contained in:
parent
29bbc05eae
commit
dcb292e00a
BIN
assets/bevy_logo_dark_big.png
Normal file
BIN
assets/bevy_logo_dark_big.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 30 KiB |
@ -1,167 +0,0 @@
|
|||||||
use bevy::prelude::*;
|
|
||||||
use rand::{random, rngs::StdRng, Rng, SeedableRng};
|
|
||||||
|
|
||||||
struct Person;
|
|
||||||
|
|
||||||
struct Velocity {
|
|
||||||
pub value: math::Vec3,
|
|
||||||
}
|
|
||||||
|
|
||||||
struct NavigationPoint {
|
|
||||||
pub target: math::Vec3,
|
|
||||||
}
|
|
||||||
|
|
||||||
struct Wander {
|
|
||||||
pub duration_bounds: math::Vec2,
|
|
||||||
pub distance_bounds: math::Vec2,
|
|
||||||
pub duration: f32,
|
|
||||||
pub elapsed: f32,
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main() {
|
|
||||||
App::build()
|
|
||||||
.setup(setup)
|
|
||||||
.add_system(build_wander_system())
|
|
||||||
.add_system(build_navigate_system())
|
|
||||||
.add_system(build_move_system())
|
|
||||||
.run();
|
|
||||||
}
|
|
||||||
|
|
||||||
fn setup(world: &mut World, resources: &mut Resources) {
|
|
||||||
let mut mesh_storage = resources.get_mut::<AssetStorage<Mesh>>().unwrap();
|
|
||||||
let cube_handle = mesh_storage.add(Mesh::load(MeshType::Cube));
|
|
||||||
|
|
||||||
world.insert(
|
|
||||||
(),
|
|
||||||
vec![
|
|
||||||
// lights
|
|
||||||
(
|
|
||||||
Light::default(),
|
|
||||||
LocalToWorld::identity(),
|
|
||||||
Translation::new(4.0, -4.0, 5.0),
|
|
||||||
Rotation::from_euler_angles(0.0, 0.0, 0.0),
|
|
||||||
),
|
|
||||||
],
|
|
||||||
);
|
|
||||||
|
|
||||||
world.insert(
|
|
||||||
(),
|
|
||||||
vec![
|
|
||||||
// camera
|
|
||||||
(
|
|
||||||
Camera::default(),
|
|
||||||
ActiveCamera,
|
|
||||||
LocalToWorld(Mat4::look_at_rh(
|
|
||||||
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();
|
|
||||||
for _ in 0..70000 {
|
|
||||||
create_person(
|
|
||||||
world,
|
|
||||||
cube_handle,
|
|
||||||
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> {
|
|
||||||
let mut rng = StdRng::from_entropy();
|
|
||||||
|
|
||||||
SystemBuilder::new("Wander")
|
|
||||||
.read_resource::<Time>()
|
|
||||||
.with_query(<(
|
|
||||||
Read<Person>,
|
|
||||||
Read<Translation>,
|
|
||||||
Write<Wander>,
|
|
||||||
Write<NavigationPoint>,
|
|
||||||
)>::query())
|
|
||||||
.build(move |_, world, time, person_query| {
|
|
||||||
for (_, translation, mut wander, mut navigation_point) in person_query.iter_mut(world) {
|
|
||||||
wander.elapsed += time.delta_seconds;
|
|
||||||
if wander.elapsed >= wander.duration {
|
|
||||||
let direction = math::vec3(
|
|
||||||
rng.gen_range(-1.0, 1.0),
|
|
||||||
rng.gen_range(-1.0, 1.0),
|
|
||||||
rng.gen_range(0.0, 0.001),
|
|
||||||
)
|
|
||||||
.normalize();
|
|
||||||
let distance =
|
|
||||||
rng.gen_range(wander.distance_bounds.x(), wander.distance_bounds.y());
|
|
||||||
navigation_point.target = translation.0 + direction * distance;
|
|
||||||
wander.elapsed = 0.0;
|
|
||||||
wander.duration =
|
|
||||||
rng.gen_range(wander.duration_bounds.x(), wander.duration_bounds.y());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
fn build_navigate_system() -> Box<dyn Schedulable> {
|
|
||||||
SystemBuilder::new("Navigate")
|
|
||||||
.with_query(<(
|
|
||||||
Read<Person>,
|
|
||||||
Write<Translation>,
|
|
||||||
Write<Velocity>,
|
|
||||||
Write<NavigationPoint>,
|
|
||||||
)>::query())
|
|
||||||
.build(move |_, world, _, person_query| {
|
|
||||||
for (_, translation, mut velocity, navigation_point) in person_query.iter_mut(world) {
|
|
||||||
let distance = navigation_point.target - translation.0;
|
|
||||||
if distance.length() > 0.01 {
|
|
||||||
let direction = distance.normalize();
|
|
||||||
velocity.value = direction * 2.0;
|
|
||||||
} else {
|
|
||||||
velocity.value = math::vec3(0.0, 0.0, 0.0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
fn build_move_system() -> Box<dyn Schedulable> {
|
|
||||||
SystemBuilder::new("Move")
|
|
||||||
.read_resource::<Time>()
|
|
||||||
.with_query(<(Write<Translation>, Read<Velocity>)>::query())
|
|
||||||
.build(move |_, world, time, person_query| {
|
|
||||||
for (mut translation, velocity) in person_query.iter_mut(world) {
|
|
||||||
translation.0 += velocity.value * time.delta_seconds;
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
fn create_person(world: &mut World, mesh_handle: Handle<Mesh>, translation: Translation) {
|
|
||||||
world.insert(
|
|
||||||
(),
|
|
||||||
vec![(
|
|
||||||
Person {},
|
|
||||||
Wander {
|
|
||||||
duration_bounds: math::vec2(3.0, 10.0),
|
|
||||||
distance_bounds: math::vec2(-50.0, 50.0),
|
|
||||||
elapsed: 0.0,
|
|
||||||
duration: 0.0,
|
|
||||||
},
|
|
||||||
NavigationPoint {
|
|
||||||
target: math::vec3(0.0, 0.0, 0.0),
|
|
||||||
},
|
|
||||||
Velocity {
|
|
||||||
value: math::vec3(0.0, 0.0, 0.0),
|
|
||||||
},
|
|
||||||
StandardMaterial {
|
|
||||||
albedo: (math::vec4(0.5, 0.3, 0.3, 1.0) * random::<f32>()).into(),
|
|
||||||
..Default::default()
|
|
||||||
},
|
|
||||||
Renderable {
|
|
||||||
is_instanced: true,
|
|
||||||
..Default::default()
|
|
||||||
},
|
|
||||||
mesh_handle,
|
|
||||||
LocalToWorld::identity(),
|
|
||||||
translation,
|
|
||||||
)],
|
|
||||||
);
|
|
||||||
}
|
|
@ -10,39 +10,18 @@ pub fn setup_system() -> Box<dyn Schedulable> {
|
|||||||
.write_resource::<AssetStorage<StandardMaterial>>()
|
.write_resource::<AssetStorage<StandardMaterial>>()
|
||||||
.build(move |command_buffer, _, (meshes, materials), _| {
|
.build(move |command_buffer, _, (meshes, materials), _| {
|
||||||
let cube_handle = meshes.add(Mesh::load(MeshType::Cube));
|
let cube_handle = meshes.add(Mesh::load(MeshType::Cube));
|
||||||
let plane_handle = meshes.add(Mesh::load(MeshType::Plane { size: 10.0 }));
|
|
||||||
let cube_material_handle = materials.add(StandardMaterial {
|
let cube_material_handle = materials.add(StandardMaterial {
|
||||||
albedo: Color::rgb(0.5, 0.4, 0.3),
|
albedo: Color::rgb(0.5, 0.4, 0.3),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
});
|
});
|
||||||
let plane_material_handle = materials.add(StandardMaterial {
|
|
||||||
albedo: Color::rgb(0.1, 0.2, 0.1),
|
|
||||||
..Default::default()
|
|
||||||
});
|
|
||||||
|
|
||||||
command_buffer
|
command_buffer
|
||||||
.build()
|
.build()
|
||||||
// plane
|
|
||||||
.add_entity(MeshEntity {
|
|
||||||
mesh: plane_handle,
|
|
||||||
material: plane_material_handle,
|
|
||||||
// renderable: Renderable::instanced(),
|
|
||||||
..Default::default()
|
|
||||||
})
|
|
||||||
// cube
|
// cube
|
||||||
.add_entity(MeshEntity {
|
.add_entity(MeshEntity {
|
||||||
mesh: cube_handle,
|
mesh: cube_handle,
|
||||||
material: cube_material_handle,
|
material: cube_material_handle,
|
||||||
// renderable: Renderable::instanced(),
|
translation: Translation::new(0.0, 0.0, 0.0),
|
||||||
translation: Translation::new(-1.5, 0.0, 1.0),
|
|
||||||
..Default::default()
|
|
||||||
})
|
|
||||||
// cube
|
|
||||||
.add_entity(MeshEntity {
|
|
||||||
mesh: cube_handle,
|
|
||||||
material: cube_material_handle,
|
|
||||||
// renderable: Renderable::instanced(),
|
|
||||||
translation: Translation::new(1.5, 0.0, 1.0),
|
|
||||||
..Default::default()
|
..Default::default()
|
||||||
})
|
})
|
||||||
// light
|
// light
|
||||||
|
@ -5,25 +5,29 @@ fn main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn setup(world: &mut World, resources: &mut Resources) {
|
fn setup(world: &mut World, resources: &mut Resources) {
|
||||||
let mut mesh_storage = resources.get_mut::<AssetStorage<Mesh>>().unwrap();
|
|
||||||
let cube_handle = mesh_storage.add(Mesh::load(MeshType::Cube));
|
|
||||||
|
|
||||||
let mut texture_storage = resources.get_mut::<AssetStorage<Texture>>().unwrap();
|
let mut texture_storage = resources.get_mut::<AssetStorage<Texture>>().unwrap();
|
||||||
let texture = Texture::load(TextureType::Png(
|
let texture = Texture::load(TextureType::Png(
|
||||||
concat!(env!("CARGO_MANIFEST_DIR"), "/assets/bevy_logo_dark.png").to_string(),
|
concat!(env!("CARGO_MANIFEST_DIR"), "/assets/bevy_logo_dark_big.png").to_string(),
|
||||||
));
|
));
|
||||||
|
let aspect = texture.height as f32 / texture.width as f32;
|
||||||
let texture_handle = texture_storage.add(texture);
|
let texture_handle = texture_storage.add(texture);
|
||||||
|
|
||||||
|
let mut mesh_storage = resources.get_mut::<AssetStorage<Mesh>>().unwrap();
|
||||||
|
let quad_width = 8.0;
|
||||||
|
let quad_handle = mesh_storage.add(Mesh::load(MeshType::Quad {
|
||||||
|
size: Vec2::new(quad_width, quad_width * aspect),
|
||||||
|
}));
|
||||||
|
|
||||||
let mut material_storage = resources
|
let mut material_storage = resources
|
||||||
.get_mut::<AssetStorage<StandardMaterial>>()
|
.get_mut::<AssetStorage<StandardMaterial>>()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let cube_material_handle = material_storage.add(StandardMaterial {
|
let material_handle = material_storage.add(StandardMaterial {
|
||||||
albedo_texture: Some(texture_handle),
|
albedo_texture: Some(texture_handle),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
});
|
});
|
||||||
|
|
||||||
let modulated_cube_material_handle = material_storage.add(StandardMaterial {
|
let modulated_material_handle = material_storage.add(StandardMaterial {
|
||||||
albedo: Color::rgba(1.0, 0.0, 0.0, 0.5),
|
albedo: Color::rgba(1.0, 0.0, 0.0, 0.5),
|
||||||
albedo_texture: Some(texture_handle),
|
albedo_texture: Some(texture_handle),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
@ -31,29 +35,31 @@ fn setup(world: &mut World, resources: &mut Resources) {
|
|||||||
|
|
||||||
world
|
world
|
||||||
.build()
|
.build()
|
||||||
// cube
|
// textured quad
|
||||||
.add_entity(MeshEntity {
|
.add_entity(MeshEntity {
|
||||||
mesh: cube_handle,
|
mesh: quad_handle,
|
||||||
material: cube_material_handle,
|
material: material_handle,
|
||||||
translation: Translation::new(1.0, 0.0, 0.0),
|
translation: Translation::new(0.0, 0.0, 0.0),
|
||||||
|
rotation: Rotation::from_euler_angles(0.0, std::f32::consts::PI / 3.0 , 0.0),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
})
|
})
|
||||||
// cube modulated
|
// textured quad modulated
|
||||||
.add_entity(MeshEntity {
|
.add_entity(MeshEntity {
|
||||||
mesh: cube_handle,
|
mesh: quad_handle,
|
||||||
material: modulated_cube_material_handle,
|
material: modulated_material_handle,
|
||||||
translation: Translation::new(-1.0, 0.0, 0.0),
|
translation: Translation::new(0.0, 1.5, 0.0),
|
||||||
|
rotation: Rotation::from_euler_angles(0.0, std::f32::consts::PI / 3.0, 0.0),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
})
|
})
|
||||||
// light
|
// light
|
||||||
.add_entity(LightEntity {
|
.add_entity(LightEntity {
|
||||||
translation: Translation::new(4.0, 4.0, 5.0),
|
translation: Translation::new(0.0, -5.0, 0.0),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
})
|
})
|
||||||
// camera
|
// camera
|
||||||
.add_entity(CameraEntity {
|
.add_entity(CameraEntity {
|
||||||
local_to_world: LocalToWorld(Mat4::look_at_rh(
|
local_to_world: 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),
|
||||||
)),
|
)),
|
||||||
|
@ -80,10 +80,7 @@ impl DrawTarget for UiDrawTarget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let quad = Mesh::load(MeshType::Quad {
|
let quad = Mesh::load(MeshType::Quad {
|
||||||
north_west: math::vec2(-0.5, 0.5),
|
size: math::vec2(1.0, 1.0),
|
||||||
north_east: math::vec2(0.5, 0.5),
|
|
||||||
south_west: math::vec2(-0.5, -0.5),
|
|
||||||
south_east: math::vec2(0.5, -0.5),
|
|
||||||
});
|
});
|
||||||
self.mesh_vertex_buffer = Some(renderer.create_buffer_with_data(
|
self.mesh_vertex_buffer = Some(renderer.create_buffer_with_data(
|
||||||
BufferInfo {
|
BufferInfo {
|
||||||
|
@ -6,10 +6,7 @@ pub enum MeshType {
|
|||||||
size: f32,
|
size: f32,
|
||||||
},
|
},
|
||||||
Quad {
|
Quad {
|
||||||
north_west: Vec2,
|
size: Vec2,
|
||||||
north_east: Vec2,
|
|
||||||
south_west: Vec2,
|
|
||||||
south_east: Vec2,
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -24,18 +21,15 @@ impl Asset<MeshType> for Mesh {
|
|||||||
MeshType::Cube => create_cube(),
|
MeshType::Cube => create_cube(),
|
||||||
MeshType::Plane { size } => create_plane(size),
|
MeshType::Plane { size } => create_plane(size),
|
||||||
MeshType::Quad {
|
MeshType::Quad {
|
||||||
north_west,
|
size
|
||||||
north_east,
|
} => create_quad(size),
|
||||||
south_west,
|
|
||||||
south_east,
|
|
||||||
} => create_quad(north_west, north_east, south_west, south_east),
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Mesh { vertices, indices }
|
Mesh { vertices, indices }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn create_quad(
|
pub fn create_quad_from_vertices(
|
||||||
north_west: Vec2,
|
north_west: Vec2,
|
||||||
north_east: Vec2,
|
north_east: Vec2,
|
||||||
south_west: Vec2,
|
south_west: Vec2,
|
||||||
@ -45,22 +39,22 @@ pub fn create_quad(
|
|||||||
Vertex::from((
|
Vertex::from((
|
||||||
[south_west.x(), south_west.y(), 0.0],
|
[south_west.x(), south_west.y(), 0.0],
|
||||||
[0.0, 0.0, 1.0],
|
[0.0, 0.0, 1.0],
|
||||||
[0.0, 0.0],
|
[0.0, 1.0],
|
||||||
)),
|
)),
|
||||||
Vertex::from((
|
Vertex::from((
|
||||||
[north_west.x(), north_west.y(), 0.0],
|
[north_west.x(), north_west.y(), 0.0],
|
||||||
[0.0, 0.0, 1.0],
|
[0.0, 0.0, 1.0],
|
||||||
[0.0, 1.0],
|
[0.0, 0.0],
|
||||||
)),
|
)),
|
||||||
Vertex::from((
|
Vertex::from((
|
||||||
[north_east.x(), north_east.y(), 0.0],
|
[north_east.x(), north_east.y(), 0.0],
|
||||||
[0.0, 0.0, 1.0],
|
[0.0, 0.0, 1.0],
|
||||||
[1.0, 1.0],
|
[1.0, 0.0],
|
||||||
)),
|
)),
|
||||||
Vertex::from((
|
Vertex::from((
|
||||||
[south_east.x(), south_east.y(), 0.0],
|
[south_east.x(), south_east.y(), 0.0],
|
||||||
[0.0, 0.0, 1.0],
|
[0.0, 0.0, 1.0],
|
||||||
[1.0, 0.0],
|
[1.0, 1.0],
|
||||||
)),
|
)),
|
||||||
];
|
];
|
||||||
|
|
||||||
@ -68,6 +62,17 @@ pub fn create_quad(
|
|||||||
return (vertex_data.to_vec(), index_data.to_vec());
|
return (vertex_data.to_vec(), index_data.to_vec());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn create_quad(dimensions: Vec2) -> (Vec<Vertex>, Vec<u16>) {
|
||||||
|
let extent_x = dimensions.x() / 2.0;
|
||||||
|
let extent_y = dimensions.y() / 2.0;
|
||||||
|
create_quad_from_vertices(
|
||||||
|
vec2(-extent_x, extent_y),
|
||||||
|
vec2(extent_x, extent_y),
|
||||||
|
vec2(-extent_x, -extent_y),
|
||||||
|
vec2(extent_x, -extent_y),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn create_cube() -> (Vec<Vertex>, Vec<u16>) {
|
pub fn create_cube() -> (Vec<Vertex>, Vec<u16>) {
|
||||||
let vertex_data = [
|
let vertex_data = [
|
||||||
// top (0, 0, 1)
|
// top (0, 0, 1)
|
||||||
@ -115,11 +120,5 @@ pub fn create_cube() -> (Vec<Vertex>, Vec<u16>) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn create_plane(size: f32) -> (Vec<Vertex>, Vec<u16>) {
|
pub fn create_plane(size: f32) -> (Vec<Vertex>, Vec<u16>) {
|
||||||
let size_over_2 = size / 2.0;
|
create_quad(vec2(size, size))
|
||||||
create_quad(
|
|
||||||
vec2(-size_over_2, size_over_2),
|
|
||||||
vec2(size_over_2, size_over_2),
|
|
||||||
vec2(-size_over_2, -size_over_2),
|
|
||||||
vec2(size_over_2, -size_over_2),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
@ -23,14 +23,13 @@ layout(set = 0, binding = 1) uniform Lights {
|
|||||||
Light SceneLights[MAX_LIGHTS];
|
Light SceneLights[MAX_LIGHTS];
|
||||||
};
|
};
|
||||||
|
|
||||||
// TODO: this should be binding = 0 right?
|
layout(set = 2, binding = 0) uniform StandardMaterial_albedo {
|
||||||
layout(set = 2, binding = 1) uniform StandardMaterial_albedo {
|
|
||||||
vec4 Albedo;
|
vec4 Albedo;
|
||||||
};
|
};
|
||||||
|
|
||||||
# ifdef STANDARDMATERIAL_ALBEDO_TEXTURE
|
# ifdef STANDARDMATERIAL_ALBEDO_TEXTURE
|
||||||
layout(set = 3, binding = 1) uniform texture2D StandardMaterial_albedo_texture;
|
layout(set = 3, binding = 0) uniform texture2D StandardMaterial_albedo_texture;
|
||||||
layout(set = 3, binding = 2) uniform sampler StandardMaterial_albedo_texture_sampler;
|
layout(set = 3, binding = 1) uniform sampler StandardMaterial_albedo_texture_sampler;
|
||||||
# endif
|
# endif
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
|
Loading…
Reference in New Issue
Block a user