Skip to content

Commit

Permalink
Update bevy to 0.10
Browse files Browse the repository at this point in the history
- Update bevy to 0.10 and update all other bevy crates to their respective versions
  • Loading branch information
caelwarner committed Apr 8, 2023
1 parent c3e3ccf commit 6b40c4b
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 53 deletions.
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
bevy = "0.8.1"
bevy_embedded_assets = "0.4.0"
bevy-inspector-egui = "0.13.0"
bevy-inspector-egui-derive = "0.13.0"
bevy = "0.10.1"
bevy_embedded_assets = "0.7.0"
bevy-inspector-egui = "0.18.3"
bevy-inspector-egui-derive = "0.18.1"
bytemuck = "1.12.2"
rand = "0.8.5"
serde = { version = "1.0.159", features = ["derive"] }
Expand Down
47 changes: 27 additions & 20 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::fs;
use bevy::asset::AssetPlugin;
use bevy::DefaultPlugins;
use bevy::prelude::*;
use bevy::window::{PresentMode, WindowMode, WindowResized};
use bevy::window::{PresentMode, WindowMode, WindowResized, WindowResolution};
use bevy_embedded_assets::EmbeddedAssetPlugin;
use serde::{Deserialize, Serialize};

Expand All @@ -28,31 +28,38 @@ fn main() {
},
};

let mut window_resolution = WindowResolution::new(
config.window.width as f32,
config.window.height as f32,
);
window_resolution.set_scale_factor_override(if config.window.override_scale_factor { Some(1.0) } else { None });

App::new()
.insert_resource(ClearColor(Color::BLACK))
.insert_resource(WindowDescriptor {
title: String::from("Slime Simulation"),
width: config.window.width as f32,
height: config.window.height as f32,
resizable: config.window.resizable,
mode: if config.window.fullscreen { WindowMode::Fullscreen } else { WindowMode::Windowed },
present_mode: if config.window.vsync { PresentMode::AutoVsync } else { PresentMode::AutoNoVsync },
scale_factor_override: if config.window.override_scale_factor { Some(1.0) } else { None },
..default()
})
.insert_resource(config)
.add_plugins_with(DefaultPlugins, |group| group.add_before::<AssetPlugin, _>(EmbeddedAssetPlugin))
.add_plugin(SlimeSimulationPlugin)
.add_startup_system_to_stage(
StartupStage::PostStartup,
setup,
.insert_resource(config.clone())
.add_plugins(DefaultPlugins
.set(WindowPlugin {
primary_window: Some(Window {
title: String::from("Slime Simulation"),
resolution: window_resolution,
resizable: config.window.resizable,
mode: if config.window.fullscreen { WindowMode::Fullscreen } else { WindowMode::Windowed },
present_mode: if config.window.vsync { PresentMode::AutoVsync } else { PresentMode::AutoNoVsync },
..default()
}),
..default()
})
.build()
.add_before::<AssetPlugin, _>(EmbeddedAssetPlugin),
)
.add_plugin(SlimeSimulationPlugin)
.add_startup_system(setup.in_base_set(StartupSet::PostStartup))
.add_system(on_window_resize)
.run();
}

fn setup(mut commands: Commands, images: Res<PipelineImages>, config: Res<AppConfig>) {
commands.spawn_bundle(SpriteBundle {
commands.spawn(SpriteBundle {
sprite: Sprite {
custom_size: Some(Vec2::new(
config.window.width as f32,
Expand All @@ -64,7 +71,7 @@ fn setup(mut commands: Commands, images: Res<PipelineImages>, config: Res<AppCon
..default()
});

commands.spawn_bundle(Camera2dBundle::default());
commands.spawn(Camera2dBundle::default());
}

fn on_window_resize(
Expand All @@ -80,7 +87,7 @@ fn on_window_resize(
}
}

#[derive(Clone, Default, Serialize, Deserialize)]
#[derive(Clone, Default, Serialize, Deserialize, Resource)]
pub struct AppConfig {
window: WindowConfig,
texture: TextureConfig,
Expand Down
9 changes: 6 additions & 3 deletions src/pipeline/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub mod fade;
pub mod recolor;
pub mod simulation;

#[derive(Resource)]
pub struct MainShaderPipeline {
sub_pipelines: Vec<Box<dyn SubShaderPipeline>>,
}
Expand Down Expand Up @@ -95,7 +96,8 @@ fn run_shader(
workgroup_size: WorkgroupSize,
) {
if let CachedPipelineState::Ok(_) = pipeline_cache.get_compute_pipeline_state(pipeline) {
let mut compute_pass = render_context.command_encoder
let mut compute_pass = render_context
.command_encoder()
.begin_compute_pass(&ComputePassDescriptor::default());

compute_pass.set_bind_group(0, bind_group.expect("bind group to exist"), &[]);
Expand Down Expand Up @@ -124,7 +126,8 @@ fn get_compute_pipeline_id(
.queue_compute_pipeline(
ComputePipelineDescriptor {
label: Some(Cow::from(label)),
layout: Some(vec![bind_group_layout]),
layout: vec![bind_group_layout],
push_constant_ranges: vec![],
shader,
shader_defs: vec![],
entry_point: Cow::from(entry_point),
Expand Down Expand Up @@ -163,7 +166,7 @@ impl<T> Default for PipelineData<T> {
}
}

#[derive(Clone, Deref, ExtractResource)]
#[derive(Clone, Deref, ExtractResource, Resource)]
pub struct PipelineImages(pub Vec<Handle<Image>>);

#[derive(Default)]
Expand Down
51 changes: 25 additions & 26 deletions src/plugin.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use bevy::prelude::*;
use bevy::render::{RenderApp, RenderStage};
use bevy::render::{RenderApp, RenderSet};
use bevy::render::extract_resource::{ExtractResource, ExtractResourcePlugin};
use bevy::render::main_graph::node::CAMERA_DRIVER;
use bevy::render::render_asset::RenderAssets;
use bevy::render::render_graph::RenderGraph;
use bevy::render::render_resource::{Extent3d, TextureDimension, TextureFormat, TextureUsages};
use bevy::render::renderer::{RenderDevice, RenderQueue};
use bevy_inspector_egui::{Inspectable, InspectorPlugin};
use bevy_inspector_egui::prelude::*;
use bevy_inspector_egui::quick::ResourceInspectorPlugin;

use crate::AppConfig;
use crate::pipeline::{MainShaderPipeline, PipelineImages, ShaderPipelineNode};
Expand All @@ -16,27 +17,23 @@ pub struct SlimeSimulationPlugin;
impl Plugin for SlimeSimulationPlugin {
fn build(&self, app: &mut App) {
app
.add_startup_system(create_images)
.add_plugin(InspectorPlugin::<SimulationSettings>::new())
.add_plugin(ExtractResourcePlugin::<PipelineImages>::default())
.init_resource::<SimulationSettings>()
.register_type::<SimulationSettings>()
.add_plugin(ResourceInspectorPlugin::<SimulationSettings>::default())
.add_plugin(ExtractResourcePlugin::<SimulationSettings>::default())
.add_plugin(ExtractResourcePlugin::<PluginTime>::default());
.add_plugin(ExtractResourcePlugin::<PipelineImages>::default())
.add_plugin(ExtractResourcePlugin::<PluginTime>::default())
.add_startup_system(create_images);

let app_config = app.world.get_resource::<AppConfig>().cloned().unwrap();
let render_app = app.sub_app_mut(RenderApp);

render_app.insert_resource(app_config);
render_app
.insert_resource(app_config)
.init_resource::<SimulationSettings>()
.init_resource::<MainShaderPipeline>()
.add_system_to_stage(
RenderStage::Queue,
queue_bind_groups,
)
.add_system_to_stage(
RenderStage::Prepare,
prepare_data,
);
.add_system(queue_bind_groups.in_set(RenderSet::Queue))
.add_system(prepare_data.in_set(RenderSet::Prepare));

let mut render_graph = render_app.world.resource_mut::<RenderGraph>();
render_graph.add_node(
Expand All @@ -46,7 +43,7 @@ impl Plugin for SlimeSimulationPlugin {
render_graph.add_node_edge(
"simulation",
CAMERA_DRIVER,
).unwrap();
);
}
}

Expand Down Expand Up @@ -94,29 +91,30 @@ fn prepare_data(
pipeline.prepare_data(render_queue.as_ref(), app_config.as_ref(), settings.as_ref(), time.as_ref());
}

#[derive(Clone, Inspectable, ExtractResource)]
#[derive(Clone, ExtractResource, InspectorOptions, Reflect, Resource)]
#[reflect(InspectorOptions, Resource)]
pub struct SimulationSettings {
pub pause: bool,
pub num_agents: u32,
#[inspectable(min = 0.1, max = 5.0)]
#[inspector(min = 0.1, max = 5.0)]
pub agent_speed: f32,
#[inspectable(min = 0.0, max = 2.0, speed = 0.05)]
#[inspector(min = 0.0, max = 2.0, speed = 0.05)]
pub agent_sense_angle_offset: f32,
#[inspectable(min = 0.0, max = 30.0)]
#[inspector(min = 0.0, max = 30.0)]
pub agent_sense_distance: f32,
pub agent_turn_speed: f32,
#[inspectable(min = 0.0, max = 2.0, speed = 0.05)]
#[inspector(min = 0.0, max = 2.0, speed = 0.05)]
pub agent_turn_randomness: f32,
pub color: Color,
pub has_trails: bool,
#[inspectable(min = 0.0, max = 5.0, speed = 0.005)]
#[inspector(min = 0.0, max = 5.0, speed = 0.005)]
pub fade_rate: f32,
#[inspectable(min = 0, max = 7)]
#[inspector(min = 0, max = 7)]
pub blur_radius: u32,
}

impl FromWorld for SimulationSettings {
fn from_world(_world: &mut World) -> Self {
impl Default for SimulationSettings {
fn default() -> Self {
Self {
pause: true,
num_agents: 1000000,
Expand All @@ -133,6 +131,7 @@ impl FromWorld for SimulationSettings {
}
}

#[derive(Resource)]
pub struct PluginTime {
pub delta_time: f32,
pub time: f32,
Expand All @@ -144,7 +143,7 @@ impl ExtractResource for PluginTime {
fn extract_resource(source: &Self::Source) -> Self {
Self {
delta_time: source.delta_seconds_f64() as f32,
time: source.seconds_since_startup() as f32,
time: source.elapsed_seconds_f64() as f32,
}
}
}

0 comments on commit 6b40c4b

Please sign in to comment.