bevy/examples/texture.rs

52 lines
1.5 KiB
Rust
Raw Normal View History

2020-01-14 03:20:58 +00:00
use bevy::{asset, prelude::*};
2020-01-14 02:06:06 +00:00
fn main() {
2020-02-18 02:36:31 +00:00
AppBuilder::new().add_defaults().setup_world(setup).run();
2020-01-14 02:06:06 +00:00
}
fn setup(world: &mut World) {
let cube_handle = {
let mut mesh_storage = world.resources.get_mut::<AssetStorage<Mesh>>().unwrap();
2020-02-18 02:36:31 +00:00
mesh_storage.add(Mesh::load(MeshType::Cube))
2020-01-14 02:06:06 +00:00
};
let texture_handle = {
2020-01-14 02:06:06 +00:00
let mut texture_storage = world.resources.get_mut::<AssetStorage<Texture>>().unwrap();
2020-01-14 03:20:58 +00:00
let texture = Texture::load(TextureType::Data(asset::create_texels(256)));
2020-02-12 03:10:38 +00:00
texture_storage.add(texture)
2020-01-14 02:06:06 +00:00
};
2020-02-18 02:36:31 +00:00
world
.build()
// cube
2020-02-18 03:06:12 +00:00
.add_archetype(MeshEntity {
2020-02-18 02:36:31 +00:00
mesh: cube_handle.clone(),
material: StandardMaterial {
albedo: texture_handle.into(),
2020-02-18 02:36:31 +00:00
},
translation: Translation::new(0.0, 0.0, 1.0),
..Default::default()
})
// light
.add_archetype(LightEntity {
translation: Translation::new(4.0, -4.0, 5.0),
2020-02-18 03:06:12 +00:00
..Default::default()
2020-02-18 02:36:31 +00:00
})
// camera
.add_archetype(CameraEntity {
camera: Camera::new(CameraType::Projection {
2020-01-14 02:06:06 +00:00
fov: std::f32::consts::PI / 4.0,
near: 1.0,
far: 1000.0,
aspect_ratio: 1.0,
}),
2020-02-18 02:36:31 +00:00
active_camera: ActiveCamera,
local_to_world: LocalToWorld(Mat4::look_at_rh(
2020-01-14 02:06:06 +00:00
Vec3::new(3.0, 8.0, 5.0),
Vec3::new(0.0, 0.0, 0.0),
Vec3::new(0.0, 0.0, 1.0),
)),
2020-02-18 02:36:31 +00:00
})
.build();
2020-01-14 02:06:06 +00:00
}