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