Skip to content

Instantly share code, notes, and snippets.

@jornvandebeek
Created January 2, 2021 11:28
Show Gist options
  • Save jornvandebeek/306c4d063c14a1fbe2bc8d1f26d6c5e0 to your computer and use it in GitHub Desktop.
Save jornvandebeek/306c4d063c14a1fbe2bc8d1f26d6c5e0 to your computer and use it in GitHub Desktop.
Texturehandle replacement in 0.4.0
use bevy::{
prelude::*,
render::texture::{Extent3d, Texture, TextureDimension, TextureFormat},
};
struct MyTimer {
timer: Timer,
}
struct LogoHandle(Option<Handle<StandardMaterial>>);
/// This example shows various ways to configure texture materials in 3D
fn main() {
App::build()
.add_plugins(DefaultPlugins)
.add_startup_system(setup.system())
.add_resource(MyTimer {
// a five second non-repeating timer
timer: Timer::from_seconds(1.0, false),
})
.add_resource(LogoHandle(None))
.add_system(timer_system.system())
.run();
}
fn timer_system(
material_handle: Res<LogoHandle>,
time: Res<Time>,
mut my_timer: ResMut<MyTimer>,
mut materials: ResMut<Assets<StandardMaterial>>,
mut textures: ResMut<Assets<Texture>>,
) {
if my_timer.timer.tick(time.delta_seconds()).just_finished() {
let material_handle = material_handle.0.as_ref().unwrap();
let old_m = materials.get_mut(material_handle).unwrap();
let tex_h = textures.add(white_texture());
old_m.albedo_texture = Some(tex_h);
println!("Replaced texture with white");
}
}
fn white_texture() -> Texture {
let mut rvec = vec![];
rvec.resize((200 * 200 * 4) as usize, 255);
Texture::new(
Extent3d::new(200, 200, 1),
TextureDimension::D2,
rvec,
TextureFormat::Rgba8UnormSrgb,
)
}
/// sets up a scene with textured entities
fn setup(
commands: &mut Commands,
asset_server: Res<AssetServer>,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
mut logohandle: ResMut<LogoHandle>,
) {
// load a texture and retrieve its aspect ratio
let texture_handle = asset_server.load("branding/bevy_logo_dark_big.png");
// let texture_handle = textures.add(white_texture());
let aspect = 0.25;
// create a new quad mesh. this is what we will apply the texture to
let quad_width = 8.0;
let quad_handle = meshes.add(Mesh::from(shape::Quad::new(Vec2::new(
quad_width,
quad_width * aspect,
))));
// this material renders the texture normally
let material_handle = materials.add(StandardMaterial {
albedo_texture: Some(texture_handle.clone()),
shaded: false,
..Default::default()
});
*logohandle = LogoHandle(Some(material_handle.clone()));
// add entities to the world
commands
// textured quad - normal
.spawn(PbrBundle {
mesh: quad_handle.clone(),
material: material_handle.clone(),
transform: Transform {
translation: Vec3::new(0.0, 0.0, 1.5),
rotation: Quat::from_rotation_x(-std::f32::consts::PI / 5.0),
..Default::default()
},
visible: Visible {
is_transparent: true,
..Default::default()
},
..Default::default()
})
// textured quad - modulated
.spawn(PbrBundle {
mesh: quad_handle.clone(),
material: material_handle.clone(),
transform: Transform {
translation: Vec3::new(0.0, 0.0, 0.0),
rotation: Quat::from_rotation_x(-std::f32::consts::PI / 5.0),
..Default::default()
},
visible: Visible {
is_transparent: true,
..Default::default()
},
..Default::default()
})
// textured quad - modulated
.spawn(PbrBundle {
mesh: quad_handle,
material: material_handle,
transform: Transform {
translation: Vec3::new(0.0, 0.0, -1.5),
rotation: Quat::from_rotation_x(-std::f32::consts::PI / 5.0),
..Default::default()
},
visible: Visible {
is_transparent: true,
..Default::default()
},
..Default::default()
})
// camera
.spawn(Camera3dBundle {
transform: Transform::from_translation(Vec3::new(3.0, 5.0, 8.0))
.looking_at(Vec3::default(), Vec3::unit_y()),
..Default::default()
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment