You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

167 lines
5.1 KiB
Rust

use std::f32::consts::PI;
use bevy::{
input::common_conditions::input_just_pressed,
prelude::*,
render::{
camera::RenderTarget,
render_resource::{
Extent3d, TextureDescriptor, TextureDimension, TextureFormat, TextureUsages,
},
},
};
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_systems(Startup, add_texture)
.add_systems(
Update,
rotate_mesh.run_if(input_just_pressed(KeyCode::Space)),
)
.run();
}
fn add_texture(
mut images: ResMut<Assets<Image>>,
mut materials: ResMut<Assets<StandardMaterial>>,
mut meshes: ResMut<Assets<Mesh>>,
mut commands: Commands,
) {
let image_handle = {
let size = Extent3d {
width: 256,
height: 256 * 6,
..default()
};
let mut image = Image {
texture_descriptor: TextureDescriptor {
label: None,
size,
dimension: TextureDimension::D2,
format: TextureFormat::Bgra8UnormSrgb,
mip_level_count: 1,
sample_count: 1,
usage: TextureUsages::TEXTURE_BINDING
| TextureUsages::COPY_DST
| TextureUsages::RENDER_ATTACHMENT,
view_formats: &[],
},
..default()
};
image.resize(size);
images.add(image)
};
let texture_camera = commands
.spawn(Camera2dBundle {
camera: Camera {
order: -1,
target: RenderTarget::Image(image_handle.clone()),
..default()
},
..default()
})
.id();
commands
.spawn((
NodeBundle {
style: Style {
width: Val::Percent(100.0),
height: Val::Percent(100.0),
flex_direction: FlexDirection::Column,
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,
..default()
},
background_color: BackgroundColor(Color::GOLD),
..default()
},
TargetCamera(texture_camera),
))
.with_children(|parent| {
["Up", "Down", "Left", "Right", "Top", "Bottom"]
.into_iter()
.for_each(|text| {
parent
.spawn(NodeBundle {
style: Style {
height: Val::Percent(100.0 / 6.0),
width: Val::Percent(100.0),
justify_content: JustifyContent::Center,
justify_items: JustifyItems::Center,
align_items: AlignItems::Center,
align_content: AlignContent::Center,
..default()
},
..default()
})
.with_children(|parent| {
parent.spawn(TextBundle {
text: Text::from_section(
text,
TextStyle {
color: Color::BLACK,
font_size: 64.0,
..default()
},
),
..default()
});
});
});
});
commands.spawn(DirectionalLightBundle { ..default() });
{
commands.spawn(ImageBundle {
style: Style {
top: Val::Px(0.0),
left: Val::Px(0.0),
position_type: PositionType::Absolute,
max_height: Val::Percent(100.0),
..default()
},
image: UiImage {
texture: image_handle.clone(),
..default()
},
..default()
});
}
{
let material = materials.add(StandardMaterial {
base_color_texture: Some(image_handle),
..default()
});
let transform =
Transform::from_xyz(0.0, 0.0, 1.5).with_rotation(Quat::from_rotation_x(-PI / 5.0));
commands.spawn(PbrBundle {
mesh,
material,
transform,
..default()
});
}
{
let transform = Transform::from_xyz(0.0, 0.0, 15.0).looking_at(Vec3::ZERO, Vec3::Y);
commands.spawn(Camera3dBundle {
transform,
..default()
});
}
}
fn rotate_mesh(mut query: Query<&mut Transform, With<Handle<Mesh>>>, time: Res<Time>) {
query.iter_mut().for_each(|mut t| {
t.rotate_x(time.delta_seconds() * 0.5);
t.rotate_y(time.delta_seconds() * 0.5);
t.rotate_z(time.delta_seconds() * 0.5);
});
}