125 lines
3.5 KiB
Rust
125 lines
3.5 KiB
Rust
use std::f32::consts::PI;
|
|
use std::time::Duration;
|
|
|
|
use bevy::app::ScheduleRunnerPlugin;
|
|
use bevy::camera::RenderTarget;
|
|
use bevy::color::Color;
|
|
use bevy::prelude::*;
|
|
use wgpu::{TextureFormat, TextureUsages};
|
|
|
|
mod post_process;
|
|
|
|
use crate::post_process::PostProcessSettings;
|
|
use crate::renderer::{G13Resource, ImageExport, ImageExportPlugin, ImageExportSource};
|
|
use crate::shared::Spinner;
|
|
|
|
mod renderer;
|
|
mod shared;
|
|
|
|
fn main() {
|
|
App::new()
|
|
.add_plugins((
|
|
DefaultPlugins.set(WindowPlugin {
|
|
primary_window: Some(Window {
|
|
resolution: bevy::window::WindowResolution::new(160 * 4, 43 * 4)
|
|
.with_scale_factor_override(4.0),
|
|
..default()
|
|
}),
|
|
..default()
|
|
}),
|
|
ScheduleRunnerPlugin::run_loop(Duration::from_secs_f64(1. / 30.)),
|
|
post_process::PostProcessPlugin,
|
|
ImageExportPlugin,
|
|
))
|
|
.insert_resource(ClearColor(Color::linear_rgba(0.0, 0.0, 0.0, 0.0)))
|
|
.add_systems(Startup, setup_scene_system)
|
|
.add_systems(PostStartup, spawn_in_ui)
|
|
.add_systems(Update, rotate_cube)
|
|
.run();
|
|
}
|
|
|
|
fn setup_scene_system(
|
|
mut commands: Commands,
|
|
meshes: ResMut<Assets<Mesh>>,
|
|
materials: ResMut<Assets<StandardMaterial>>,
|
|
mut images: ResMut<Assets<Image>>,
|
|
mut export_sources: ResMut<Assets<ImageExportSource>>,
|
|
) {
|
|
shared::spawn_3d_scene(commands.reborrow(), meshes, materials);
|
|
|
|
let mut image = Image::new_target_texture(
|
|
160,
|
|
43,
|
|
TextureFormat::Rgba8Unorm,
|
|
Some(TextureFormat::Rgba8UnormSrgb),
|
|
);
|
|
|
|
image.texture_descriptor.usage = TextureUsages::TEXTURE_BINDING
|
|
| TextureUsages::COPY_SRC
|
|
| TextureUsages::COPY_DST
|
|
| TextureUsages::RENDER_ATTACHMENT;
|
|
|
|
let image_handle = images.add(image);
|
|
|
|
commands.spawn((
|
|
Camera3d::default(),
|
|
Camera::default(),
|
|
RenderTarget::Image(image_handle.clone().into()),
|
|
Transform::from_xyz(0.0, 1.0, 2.5).looking_at(Vec3::ZERO, Vec3::Y),
|
|
UiAntiAlias::Off,
|
|
PostProcessSettings { scale: 1.0 },
|
|
));
|
|
|
|
commands.spawn((ImageExport(export_sources.add(image_handle)),));
|
|
}
|
|
|
|
fn spawn_in_ui(mut commands: Commands, assets: Res<AssetServer>, camera: Query<(Entity, &Camera)>) {
|
|
let font: Handle<Font> = assets.load("fonts/Roboto-Bold.ttf");
|
|
|
|
let text_font = TextFont::from(font.clone())
|
|
.with_font_size(12.)
|
|
.with_font_smoothing(bevy::text::FontSmoothing::None);
|
|
|
|
let Ok(main_camera) = camera.single() else {
|
|
return; // no camera... yet?
|
|
};
|
|
|
|
commands.spawn((
|
|
Node {
|
|
position_type: PositionType::Absolute,
|
|
top: Val::Px(5.0),
|
|
left: Val::Px(5.0),
|
|
..default()
|
|
},
|
|
Text::new("Hello World!"),
|
|
text_font.clone(),
|
|
TextColor(Color::WHITE),
|
|
UiTargetCamera(main_camera.0),
|
|
));
|
|
}
|
|
|
|
fn rotate_cube(
|
|
mut cubes: Query<(&mut Transform, &Spinner)>,
|
|
timer: Res<Time>,
|
|
g13: Res<G13Resource>,
|
|
) {
|
|
const DEADZONE: i32 = 35;
|
|
|
|
let g13 = g13.g13.state();
|
|
let mut x = g13.x as f32 / 512.0;
|
|
let mut y = g13.y as f32 / 512.0;
|
|
|
|
if g13.x > -DEADZONE && g13.x < DEADZONE {
|
|
x = 0.0;
|
|
}
|
|
|
|
if g13.y > -DEADZONE && g13.y < DEADZONE {
|
|
y = 0.0;
|
|
}
|
|
|
|
for (mut transform, _cube) in &mut cubes {
|
|
transform.rotate_y(x * PI * timer.delta_secs());
|
|
transform.rotate_x(y * PI * timer.delta_secs());
|
|
}
|
|
}
|