From 9752f39736b940f5f4e363db26739b2563c1fdd0 Mon Sep 17 00:00:00 2001 From: Paul Sajna Date: Wed, 21 Jun 2023 01:17:41 -0700 Subject: [PATCH] headless bevy --- crates/programs/bevy-experiments/Cargo.lock | 133 ++++- crates/programs/bevy-experiments/Cargo.toml | 11 +- crates/programs/bevy-experiments/src/main.rs | 561 ++++--------------- 3 files changed, 242 insertions(+), 463 deletions(-) diff --git a/crates/programs/bevy-experiments/Cargo.lock b/crates/programs/bevy-experiments/Cargo.lock index 869b154820..bb13c40d93 100644 --- a/crates/programs/bevy-experiments/Cargo.lock +++ b/crates/programs/bevy-experiments/Cargo.lock @@ -273,6 +273,15 @@ dependencies = [ "bevy_internal", ] +[[package]] +name = "bevy-experiments" +version = "0.1.0" +dependencies = [ + "bevy", + "bevy_framepace", + "bevy_image_export", +] + [[package]] name = "bevy_a11y" version = "0.10.1" @@ -468,6 +477,16 @@ dependencies = [ "encase_derive_impl", ] +[[package]] +name = "bevy_framepace" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1825c5a485aa2a2ffd91cc1faa1f4d621c804a893ead88cafedbe1ed6c51d34" +dependencies = [ + "bevy", + "spin_sleep", +] + [[package]] name = "bevy_gilrs" version = "0.10.1" @@ -525,6 +544,20 @@ dependencies = [ "smallvec", ] +[[package]] +name = "bevy_image_export" +version = "0.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bdcb6aae8af3981628eb120cad4cc348f7012c13323b089e184dbc8d87c4dfcd" +dependencies = [ + "bevy", + "bytemuck", + "futures", + "futures-lite", + "image", + "wgpu", +] + [[package]] name = "bevy_input" version = "0.10.1" @@ -1442,12 +1475,48 @@ dependencies = [ "libc", ] +[[package]] +name = "futures" +version = "0.3.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23342abe12aba583913b2e62f22225ff9c950774065e4bfb61a19cd9770fec40" +dependencies = [ + "futures-channel", + "futures-core", + "futures-executor", + "futures-io", + "futures-sink", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-channel" +version = "0.3.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "955518d47e09b25bbebc7a18df10b81f0c766eaf4c4f1cccef2fca5f2a4fb5f2" +dependencies = [ + "futures-core", + "futures-sink", +] + [[package]] name = "futures-core" version = "0.3.28" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4bca583b7e26f571124fe5b7561d49cb2868d79116cfa0eefce955557c6fee8c" +[[package]] +name = "futures-executor" +version = "0.3.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ccecee823288125bd88b4d7f565c9e58e41858e47ab72e8ea2d64e93624386e0" +dependencies = [ + "futures-core", + "futures-task", + "futures-util", +] + [[package]] name = "futures-io" version = "0.3.28" @@ -1469,6 +1538,47 @@ dependencies = [ "waker-fn", ] +[[package]] +name = "futures-macro" +version = "0.3.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "89ca545a94061b6365f2c7355b4b32bd20df3ff95f02da9329b34ccc3bd6ee72" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.18", +] + +[[package]] +name = "futures-sink" +version = "0.3.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f43be4fe21a13b9781a69afa4985b0f6ee0e1afab2c6f454a8cf30e2b2237b6e" + +[[package]] +name = "futures-task" +version = "0.3.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "76d3d132be6c0e6aa1534069c705a74a5997a356c0dc2f86a47765e5617c5b65" + +[[package]] +name = "futures-util" +version = "0.3.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26b01e40b772d54cf6c6d721c1d1abd0647a0106a12ecaa1c186273392a69533" +dependencies = [ + "futures-channel", + "futures-core", + "futures-io", + "futures-macro", + "futures-sink", + "futures-task", + "memchr", + "pin-project-lite", + "pin-utils", + "slab", +] + [[package]] name = "fxhash" version = "0.2.1" @@ -2388,6 +2498,12 @@ version = "0.2.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e0a7ae3ac2f1173085d398531c705756c94a4c56843785df85a60c1a0afac116" +[[package]] +name = "pin-utils" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" + [[package]] name = "pkg-config" version = "0.3.27" @@ -2673,6 +2789,16 @@ dependencies = [ "serde", ] +[[package]] +name = "spin_sleep" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cafa7900db085f4354dbc7025e25d7a839a14360ea13b5fc4fd717f2d3b23134" +dependencies = [ + "once_cell", + "winapi", +] + [[package]] name = "spirv" version = "0.2.0+1.5.4" @@ -2742,13 +2868,6 @@ dependencies = [ "slotmap", ] -[[package]] -name = "temp-bevy" -version = "0.1.0" -dependencies = [ - "bevy", -] - [[package]] name = "termcolor" version = "1.2.0" diff --git a/crates/programs/bevy-experiments/Cargo.toml b/crates/programs/bevy-experiments/Cargo.toml index eb06634e7c..7d28c9777d 100644 --- a/crates/programs/bevy-experiments/Cargo.toml +++ b/crates/programs/bevy-experiments/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "temp-bevy" +name = "bevy-experiments" version = "0.1.0" edition = "2021" @@ -7,7 +7,10 @@ edition = "2021" [dependencies] bevy = "0.10.1" +bevy_framepace = "0.12.1" +bevy_image_export = "0.7.2" + +#[profile.release] +#opt-level = "z" +#lto = "fat" -[profile.release] -opt-level = "z" -lto = "fat" diff --git a/crates/programs/bevy-experiments/src/main.rs b/crates/programs/bevy-experiments/src/main.rs index 2210f21930..55cbd68c89 100644 --- a/crates/programs/bevy-experiments/src/main.rs +++ b/crates/programs/bevy-experiments/src/main.rs @@ -1,235 +1,62 @@ -//! Loads and renders a glTF file as a scene. - -use std::f32::consts::*; use core::time::Duration; use bevy::{ pbr::{CascadeShadowConfigBuilder, DirectionalLightShadowMap}, prelude::*, - scene::SceneInstance, animation::{AnimationClip, AnimationPlayer}, }; - - use bevy::{ - input::mouse::MouseMotion, + render::{ + camera::RenderTarget, + render_resource::{ + Extent3d, TextureDescriptor, TextureDimension, TextureFormat, TextureUsages, + }, + }, }; +use bevy_image_export::{ImageExportPlugin, ImageExportSource, ImageExportBundle}; +use bevy::winit::WinitPlugin; +use bevy::app::ScheduleRunnerSettings; +use bevy::app::ScheduleRunnerPlugin; +use bevy::app::AppExit; + +// number of frames after which the application exits +const DESIRED_FRAME_COUNT: u32 = 120; + fn main() { + let export_plugin = ImageExportPlugin::default(); + let export_threads = export_plugin.threads.clone(); App::new() .insert_resource(AmbientLight { color: Color::WHITE, brightness: 1.0 / 5.0f32, }) .insert_resource(DirectionalLightShadowMap { size: 4096 }) - .add_plugins(DefaultPlugins) + .insert_resource(ScheduleRunnerSettings::run_loop(Duration::from_secs_f64(0.0))) + .add_plugins(DefaultPlugins.build().disable::()) + .add_plugin(ScheduleRunnerPlugin) + .add_plugin(export_plugin) .add_startup_system(setup) - .add_system(setup_scene_once_loaded) - //.add_system(animate_light_direction) - //.add_system(animate_model_direction) - .add_system(keyboard_animation_control) - .add_system(camera_controller) - .add_system(text_update_system) + .add_system(update) .run(); + export_threads.finish(); } #[derive(Resource)] struct Animations(Vec>); -#[derive(Component)] -struct PosText; - -#[derive(Component)] -struct RotText; - -#[derive(Component)] -struct MainCamera; - fn setup(mut commands: Commands, asset_server: Res) { - - commands.insert_resource(Animations(vec![ asset_server.load("Roko_Anim_Wave_noOptimization.glb#Animation0"), ])); - commands.spawn(( - Camera3dBundle { - transform: Transform::from_xyz(0.0, 1.5, 1.5), - //transform: Transform { - //translation: Vec3::new(0.0, -10.0, 5.0), - //rotation: Quat::from_xyzw(0.574, 0.0, 0.0, 0.819), - //scale: Vec3::new(1.0, 1.0, 1.0), - //}, - //projection: Projection::Orthographic(OrthographicProjection{ - //near: 0.1, - //far: 100.0, - //scale: 7.312, - //..default() - //} - //), - ..default() - }, - MainCamera, - CameraController::default(), - EnvironmentMapLight { - diffuse_map: asset_server.load("pisa_diffuse_rgb9e5_zstd.ktx2"), - specular_map: asset_server.load("pisa_specular_rgb9e5_zstd.ktx2"), - }, - )); - - commands.spawn(( - TextBundle::from_sections([ - TextSection::new( - "Position: ", - TextStyle { - font: asset_server.load("fonts/FiraSans-Bold.ttf"), - font_size: 20.0, - color: Color::WHITE, - }, - ), - TextSection::new( - "X: ", - TextStyle { - font: asset_server.load("fonts/FiraSans-Bold.ttf"), - font_size: 20.0, - color: Color::WHITE, - }, - ), - TextSection::from_style(TextStyle { - font: asset_server.load("fonts/FiraMono-Medium.ttf"), - font_size: 20.0, - color: Color::GOLD, - }), - TextSection::new( - "Y: ", - TextStyle { - font: asset_server.load("fonts/FiraSans-Bold.ttf"), - font_size: 20.0, - color: Color::WHITE, - }, - ), - TextSection::from_style(TextStyle { - font: asset_server.load("fonts/FiraMono-Medium.ttf"), - font_size: 20.0, - color: Color::GOLD, - }), - TextSection::new( - "Z: ", - TextStyle { - font: asset_server.load("fonts/FiraSans-Bold.ttf"), - font_size: 20.0, - color: Color::WHITE, - }, - ), - TextSection::from_style(TextStyle { - font: asset_server.load("fonts/FiraMono-Medium.ttf"), - font_size: 20.0, - color: Color::GOLD, - }), - - ]).with_style( - Style { - position_type: PositionType::Absolute, - position: UiRect { - right: Val::Px(00.0), - ..default() - }, - ..default() - } - ), - - PosText, - )); - - commands.spawn(( - TextBundle::from_sections([ - TextSection::new( - "Rotation: ", - TextStyle { - font: asset_server.load("fonts/FiraSans-Bold.ttf"), - font_size: 20.0, - color: Color::WHITE, - }, - ), - TextSection::new( - "X: ", - TextStyle { - font: asset_server.load("fonts/FiraSans-Bold.ttf"), - font_size: 20.0, - color: Color::WHITE, - }, - ), - TextSection::from_style(TextStyle { - font: asset_server.load("fonts/FiraMono-Medium.ttf"), - font_size: 20.0, - color: Color::GOLD, - }), - TextSection::new( - "Y: ", - TextStyle { - font: asset_server.load("fonts/FiraSans-Bold.ttf"), - font_size: 20.0, - color: Color::WHITE, - }, - ), - TextSection::from_style(TextStyle { - font: asset_server.load("fonts/FiraMono-Medium.ttf"), - font_size: 20.0, - color: Color::GOLD, - }), - TextSection::new( - "Z: ", - TextStyle { - font: asset_server.load("fonts/FiraSans-Bold.ttf"), - font_size: 20.0, - color: Color::WHITE, - }, - ), - TextSection::from_style(TextStyle { - font: asset_server.load("fonts/FiraMono-Medium.ttf"), - font_size: 20.0, - color: Color::GOLD, - }), - TextSection::new( - "W: ", - TextStyle { - font: asset_server.load("fonts/FiraSans-Bold.ttf"), - font_size: 20.0, - color: Color::WHITE, - }, - ), - TextSection::from_style(TextStyle { - font: asset_server.load("fonts/FiraMono-Medium.ttf"), - font_size: 20.0, - color: Color::GOLD, - }), - ]).with_style( - Style { - position_type: PositionType::Absolute, - position: UiRect { - top: Val::Px(20.0), - right: Val::Px(00.0), - ..default() - }, - ..default() - } - ), - RotText, - )); - - - commands.spawn(DirectionalLightBundle { directional_light: DirectionalLight { shadows_enabled: true, ..default() }, - // This is a relatively small scene, so use tighter shadow - // cascade bounds than the default for better quality. - // We also adjusted the shadow map to be larger since we're - // only using a single cascade. cascade_shadow_config: CascadeShadowConfigBuilder { num_cascades: 1, maximum_distance: 1.6, @@ -243,276 +70,106 @@ fn setup(mut commands: Commands, asset_server: Res) { }, ..default() }); - commands.spawn(SceneBundle { - scene: asset_server.load("Roko_nogamerig.glb#Scene0"), - transform: Transform::from_xyz(0.0, 0.0, 0.0), - //transform: Transform::from_rotation(Quat::from_rotation_x(3.14)), - ..default() - }); - //commands.spawn(AnimationPlayer::default()); + commands.spawn(( + SceneBundle { + scene: asset_server.load("Roko_nogamerig.glb#Scene0"), + transform: Transform::from_xyz(0.0, 0.0, 0.0), + ..default() + }, + Roko + )); } -fn animate_light_direction( - time: Res