bevy/examples/3d/texture.rs

104 lines
3.4 KiB
Rust
Raw Normal View History

2020-07-17 02:34:18 +00:00
use bevy::prelude::*;
2020-01-14 02:06:06 +00:00
2020-08-01 00:10:29 +00:00
/// This example shows various ways to configure texture materials in 3D
2020-01-14 02:06:06 +00:00
fn main() {
App::build()
.add_default_plugins()
2020-05-14 00:31:56 +00:00
.add_startup_system(setup.system())
.run();
2020-01-14 02:06:06 +00:00
}
/// sets up a scene with textured entities
2020-05-14 00:31:56 +00:00
fn setup(
2020-07-10 04:18:35 +00:00
mut commands: Commands,
2020-05-16 02:30:02 +00:00
asset_server: Res<AssetServer>,
2020-05-14 00:52:47 +00:00
mut meshes: ResMut<Assets<Mesh>>,
mut textures: ResMut<Assets<Texture>>,
mut materials: ResMut<Assets<StandardMaterial>>,
2020-05-14 00:31:56 +00:00
) {
2020-08-01 00:10:29 +00:00
// load a texture and retrieve its aspect ratio
2020-05-16 07:27:30 +00:00
let texture_handle = asset_server
.load_sync(&mut textures, "assets/branding/bevy_logo_dark_big.png")
.unwrap();
2020-05-16 02:30:02 +00:00
let texture = textures.get(&texture_handle).unwrap();
2020-05-04 08:22:25 +00:00
let aspect = texture.aspect();
2020-01-14 02:06:06 +00:00
// create a new quad mesh. this is what we will apply the texture to
2020-03-30 22:44:29 +00:00
let quad_width = 8.0;
let quad_handle = meshes.add(Mesh::from(shape::Quad::new(Vec2::new(
quad_width,
quad_width * aspect,
))));
2020-03-30 22:44:29 +00:00
// this material renders the texture normally
2020-05-13 23:42:27 +00:00
let material_handle = materials.add(StandardMaterial {
albedo_texture: Some(texture_handle),
shaded: false,
..Default::default()
});
// this material modulates the texture to make it red (and slightly transparent)
let red_material_handle = materials.add(StandardMaterial {
albedo: Color::rgba(1.0, 0.0, 0.0, 0.5),
albedo_texture: Some(texture_handle),
shaded: false,
..Default::default()
});
// and lets make this one blue! (and also slightly transparent)
let blue_material_handle = materials.add(StandardMaterial {
albedo: Color::rgba(0.0, 0.0, 1.0, 0.5),
albedo_texture: Some(texture_handle),
shaded: false,
..Default::default()
});
// add entities to the world
2020-07-10 04:18:35 +00:00
commands
// textured quad - normal
2020-07-10 04:18:35 +00:00
.spawn(PbrComponents {
2020-03-30 22:44:29 +00:00
mesh: quad_handle,
material: material_handle,
translation: Translation::new(0.0, 0.0, 1.5),
rotation: Rotation(Quat::from_rotation_x(-std::f32::consts::PI / 5.0)),
draw: Draw {
is_transparent: true,
..Default::default()
},
..Default::default()
})
// textured quad - modulated
2020-07-10 04:18:35 +00:00
.spawn(PbrComponents {
mesh: quad_handle,
material: red_material_handle,
2020-03-30 22:44:29 +00:00
translation: Translation::new(0.0, 0.0, 0.0),
rotation: Rotation(Quat::from_rotation_x(-std::f32::consts::PI / 5.0)),
draw: Draw {
is_transparent: true,
..Default::default()
},
..Default::default()
})
// textured quad - modulated
2020-07-10 04:18:35 +00:00
.spawn(PbrComponents {
2020-03-30 22:44:29 +00:00
mesh: quad_handle,
material: blue_material_handle,
translation: Translation::new(0.0, 0.0, -1.5),
rotation: Rotation(Quat::from_rotation_x(-std::f32::consts::PI / 5.0)),
draw: Draw {
is_transparent: true,
..Default::default()
},
2020-02-18 03:06:12 +00:00
..Default::default()
2020-02-18 02:36:31 +00:00
})
// camera
.spawn(Camera3dComponents {
transform: Transform::new_sync_disabled(Mat4::face_toward(
Vec3::new(3.0, 5.0, 8.0),
2020-01-14 02:06:06 +00:00
Vec3::new(0.0, 0.0, 0.0),
Vec3::new(0.0, 1.0, 0.0),
2020-01-14 02:06:06 +00:00
)),
2020-03-22 04:55:33 +00:00
..Default::default()
2020-04-07 20:25:01 +00:00
});
2020-01-14 02:06:06 +00:00
}