Use system fns for all setup code

This commit is contained in:
Carter Anderson 2020-05-13 17:31:56 -07:00
parent 70f122b92a
commit 06f3a4efb9
27 changed files with 162 additions and 165 deletions

View File

@ -71,7 +71,7 @@ impl<'a> WorldBuilder<'a> {
self
}
pub fn add_children(&mut self, build_children: impl Fn(&mut Self)) -> &mut Self {
pub fn add_children(&mut self, build_children: impl Fn(&mut Self) -> &mut Self) -> &mut Self {
self.parent_entity = self.current_entity;
self.current_entity = None;

View File

@ -27,15 +27,13 @@ impl ForwardPbrRenderGraphBuilder for RenderGraph {
fn add_pbr_graph(&mut self, resources: &Resources) -> &mut Self {
self.add_system_node_named(
node::LOCAL_TO_WORLD,
UniformNode::<LocalToWorld>::new(true),
resources,
UniformNode::<LocalToWorld>::new(true)
);
self.add_system_node_named(
node::STANDARD_MATERIAL,
AssetUniformNode::<StandardMaterial>::new(true),
resources,
AssetUniformNode::<StandardMaterial>::new(true)
);
self.add_system_node_named(node::LIGHTS, LightsNode::new(10), resources);
self.add_system_node_named(node::LIGHTS, LightsNode::new(10));
let mut shaders = resources.get_mut::<Assets<Shader>>().unwrap();
let mut pipelines = resources
.get_mut::<Assets<PipelineDescriptor>>()

View File

@ -21,7 +21,7 @@ impl AppPlugin for PbrPlugin {
fn build(&self, app: &mut AppBuilder) {
app.add_asset::<StandardMaterial>().add_system_to_stage(
stage::POST_UPDATE,
shader::asset_handle_shader_def_system::<StandardMaterial>.system(),
shader::asset_shader_def_system::<StandardMaterial>.system(),
);
let resources = app.resources();
let mut render_graph = resources.get_mut::<RenderGraph>().unwrap();

View File

@ -46,7 +46,7 @@ pub struct LightCount {
}
impl SystemNode for LightsNode {
fn get_system(&self, _resources: &Resources) -> Box<dyn Schedulable> {
fn get_system(&self) -> Box<dyn Schedulable> {
let mut light_buffer = None;
let mut lights_are_dirty = true;
// TODO: merge these

View File

@ -61,11 +61,11 @@ impl BaseRenderGraphBuilder for RenderGraph {
config: &BaseRenderGraphConfig,
) -> &mut Self {
if config.add_3d_camera {
self.add_system_node_named(node::CAMERA, CameraNode::default(), resources);
self.add_system_node_named(node::CAMERA, CameraNode::default());
}
if config.add_2d_camera {
self.add_system_node_named(node::CAMERA2D, Camera2dNode::default(), resources);
self.add_system_node_named(node::CAMERA2D, Camera2dNode::default());
}
if config.add_main_depth_texture {

View File

@ -1,5 +1,5 @@
use super::{Edge, Node, NodeId, NodeLabel, NodeState, RenderGraphError, SlotLabel, SystemNode};
use legion::prelude::{Executor, Resources, Schedulable};
use legion::prelude::{Executor, Schedulable};
use std::{borrow::Cow, collections::HashMap, fmt::Debug};
#[derive(Default)]
@ -33,11 +33,11 @@ impl RenderGraph {
id
}
pub fn add_system_node<T>(&mut self, node: T, resources: &mut Resources) -> NodeId
pub fn add_system_node<T>(&mut self, node: T) -> NodeId
where
T: SystemNode + 'static,
{
self.new_node_systems.push(node.get_system(resources));
self.new_node_systems.push(node.get_system());
self.add_node(node)
}
@ -45,12 +45,11 @@ impl RenderGraph {
&mut self,
name: impl Into<Cow<'static, str>>,
node: T,
resources: &Resources,
) -> NodeId
where
T: SystemNode + 'static,
{
self.new_node_systems.push(node.get_system(resources));
self.new_node_systems.push(node.get_system());
self.add_node_named(name, node)
}

View File

@ -36,7 +36,7 @@ pub trait Node: Downcast + Send + Sync + 'static {
impl_downcast!(Node);
pub trait SystemNode: Node {
fn get_system(&self, resources: &Resources) -> Box<dyn Schedulable>;
fn get_system(&self) -> Box<dyn Schedulable>;
}
pub struct Edges {

View File

@ -1,10 +1,12 @@
use bevy_app::{Events, GetEventReader};
use bevy_app::Events;
use bevy_window::WindowResized;
use crate::{
camera::{ActiveCamera2d, Camera},
render_graph::{CommandQueue, Node, ResourceSlots, SystemNode},
render_resource::{resource_name, BufferInfo, BufferUsage, RenderResourceAssignments, RenderResourceAssignment},
render_resource::{
resource_name, BufferInfo, BufferUsage, RenderResourceAssignment, RenderResourceAssignments,
},
renderer::{RenderContext, RenderResources},
};
@ -30,9 +32,9 @@ impl Node for Camera2dNode {
}
impl SystemNode for Camera2dNode {
fn get_system(&self, resources: &Resources) -> Box<dyn Schedulable> {
fn get_system(&self) -> Box<dyn Schedulable> {
let mut camera_buffer = None;
let mut window_resized_event_reader = resources.get_event_reader::<WindowResized>();
let mut window_resized_event_reader = None;
let mut command_queue = self.command_queue.clone();
SystemBuilder::new("camera_2d_resource_provider")
@ -69,7 +71,12 @@ impl SystemNode for Camera2dNode {
camera_buffer = Some(buffer);
}
if window_resized_event_reader.is_none() {
window_resized_event_reader = Some(window_resized_events.get_reader());
}
let primary_window_resized_event = window_resized_event_reader
.as_mut()
.unwrap()
.find_latest(&window_resized_events, |event| event.is_primary);
if let Some(_) = primary_window_resized_event {
let matrix_size = std::mem::size_of::<[[f32; 4]; 4]>();

View File

@ -7,7 +7,7 @@ use crate::{
ActiveCamera, Camera,
};
use bevy_app::{Events, GetEventReader};
use bevy_app::Events;
use bevy_transform::prelude::*;
use bevy_window::WindowResized;
use legion::prelude::*;
@ -32,9 +32,9 @@ impl Node for CameraNode {
}
impl SystemNode for CameraNode {
fn get_system(&self, resources: &Resources) -> Box<dyn Schedulable> {
fn get_system(&self) -> Box<dyn Schedulable> {
let mut camera_buffer = None;
let mut window_resized_event_reader = resources.get_event_reader::<WindowResized>();
let mut window_resized_event_reader = None;
let mut command_queue = self.command_queue.clone();
SystemBuilder::new("camera_resource_provider")
@ -70,8 +70,12 @@ impl SystemNode for CameraNode {
);
camera_buffer = Some(buffer);
}
if window_resized_event_reader.is_none() {
window_resized_event_reader = Some(window_resized_events.get_reader());
}
let primary_window_resized_event = window_resized_event_reader
.as_mut()
.unwrap()
.find_latest(&window_resized_events, |event| event.is_primary);
if let Some(_) = primary_window_resized_event {
let matrix_size = std::mem::size_of::<[[f32; 4]; 4]>();

View File

@ -389,12 +389,10 @@ impl<T> SystemNode for UniformNode<T>
where
T: AsUniforms,
{
fn get_system(&self, resources: &Resources) -> Box<dyn Schedulable> {
fn get_system(&self) -> Box<dyn Schedulable> {
let mut command_queue = self.command_queue.clone();
let mut uniform_buffer_arrays = UniformBufferArrays::<T>::new();
let mut vertex_buffer_descriptors = resources.get_mut::<VertexBufferDescriptors>().unwrap();
let dynamic_uniforms = self.dynamic_uniforms;
initialize_vertex_buffer_descriptor::<T>(&mut vertex_buffer_descriptors);
// TODO: maybe run "update" here
SystemBuilder::new(format!(
"uniform_resource_provider::<{}>",
@ -547,12 +545,10 @@ impl<T> SystemNode for AssetUniformNode<T>
where
T: AsUniforms,
{
fn get_system(&self, resources: &Resources) -> Box<dyn Schedulable> {
fn get_system(&self) -> Box<dyn Schedulable> {
let mut command_queue = self.command_queue.clone();
let mut uniform_buffer_arrays = UniformBufferArrays::<T>::new();
let mut vertex_buffer_descriptors = resources.get_mut::<VertexBufferDescriptors>().unwrap();
let dynamic_uniforms = self.dynamic_uniforms;
initialize_vertex_buffer_descriptor::<T>(&mut vertex_buffer_descriptors);
// TODO: maybe run "update" here
SystemBuilder::new("uniform_resource_provider")
.read_resource::<Assets<T>>()
@ -674,6 +670,7 @@ where
}
}
#[allow(dead_code)]
fn initialize_vertex_buffer_descriptor<T>(vertex_buffer_descriptors: &mut VertexBufferDescriptors)
where
T: AsUniforms,

View File

@ -33,7 +33,7 @@ where
}
}
pub fn asset_handle_shader_def_system<T>(
pub fn asset_shader_def_system<T>(
assets: Resource<Assets<T>>,
asset_handle: Ref<Handle<T>>,
mut renderable: RefMut<Renderable>,

View File

@ -22,7 +22,7 @@ use bevy_asset::{AddAsset, Assets, Handle};
use bevy_render::{
mesh::{shape::Quad, Mesh},
render_graph::RenderGraph,
shader::asset_handle_shader_def_system,
shader::asset_shader_def_system,
};
use glam::Vec2;
use legion::prelude::IntoSystem;
@ -38,7 +38,7 @@ impl AppPlugin for UiPlugin {
app.add_asset::<ColorMaterial>()
.add_system_to_stage(
stage::POST_UPDATE,
asset_handle_shader_def_system::<ColorMaterial>.system(),
asset_shader_def_system::<ColorMaterial>.system(),
)
.add_system_to_stage(stage::POST_UPDATE, sprite_system())
.add_system_to_stage(stage::POST_UPDATE, ui_update_system());

View File

@ -70,11 +70,10 @@ impl UiRenderGraphBuilder for RenderGraph {
self.add_system_node_named(
"color_material",
AssetUniformNode::<ColorMaterial>::new(false),
resources,
);
self.add_node_edge("color_material", base_render_graph::node::MAIN_PASS)
.unwrap();
self.add_system_node_named("rect", UniformNode::<Rect>::new(false), resources);
self.add_system_node_named("rect", UniformNode::<Rect>::new(false));
self.add_node_edge("rect", base_render_graph::node::MAIN_PASS)
.unwrap();
let mut pipelines = resources

View File

@ -3,18 +3,20 @@ use bevy::prelude::*;
fn main() {
App::build()
.add_default_plugins()
.add_startup_system(setup)
.add_startup_system(setup.system())
.run();
}
fn setup(world: &mut World, resources: &mut Resources) {
let mut textures = resources.get_mut::<Assets<Texture>>().unwrap();
fn setup(
command_buffer: &mut CommandBuffer,
mut textures: ResourceMut<Assets<Texture>>,
mut materials: ResourceMut<Assets<ColorMaterial>>,
) {
let texture_path = concat!(env!("CARGO_MANIFEST_DIR"), "/assets/branding/icon.png");
let texture = Texture::load(TextureType::Png(texture_path.to_string()));
let texture_handle = textures.add(texture);
let mut color_materials = resources.get_mut::<Assets<ColorMaterial>>().unwrap();
world
command_buffer
.build()
.add_entity(Camera2dEntity::default())
.add_entity(SpriteEntity {
@ -23,7 +25,7 @@ fn setup(world: &mut World, resources: &mut Resources) {
z_index: 0.5,
..Default::default()
},
material: color_materials.add(texture_handle.into()),
material: materials.add(texture_handle.into()),
..Default::default()
});
}

View File

@ -3,28 +3,28 @@ use bevy::{gltf, prelude::*};
fn main() {
App::build()
.add_default_plugins()
.add_startup_system(setup)
.add_startup_system(setup.system())
.run();
}
fn setup(world: &mut World, resources: &mut Resources) {
fn setup(
command_buffer: &mut CommandBuffer,
mut meshes: ResourceMut<Assets<Mesh>>,
mut materials: ResourceMut<Assets<StandardMaterial>>,
) {
// load the mesh
let model_path = concat!(env!("CARGO_MANIFEST_DIR"), "/assets/models/Monkey.gltf");
let mesh = gltf::load_gltf(&model_path).unwrap().unwrap();
let mut meshes = resources.get_mut::<Assets<Mesh>>().unwrap();
let mesh_handle = meshes.add(mesh);
// create a material for the mesh
let mut materials = resources
.get_mut::<Assets<StandardMaterial>>()
.unwrap();
let material_handle = materials.add(StandardMaterial {
albedo: Color::rgb(0.5, 0.4, 0.3),
..Default::default()
});
// add entities to the world
world
command_buffer
.build()
// mesh
.add_entity(MeshEntity {

View File

@ -5,7 +5,7 @@ struct Rotator;
fn main() {
App::build()
.add_default_plugins()
.add_startup_system(setup)
.add_startup_system(setup.system())
.add_system(rotator_system.system())
.run();
}
@ -16,19 +16,18 @@ fn rotator_system(time: Resource<Time>, _rotator: RefMut<Rotator>, mut rotation:
}
/// set up a simple scene with a "parent" cube and a "child" cube
fn setup(world: &mut World, resources: &mut Resources) {
let mut meshes = resources.get_mut::<Assets<Mesh>>().unwrap();
let mut material_storage = resources
.get_mut::<Assets<StandardMaterial>>()
.unwrap();
fn setup(
command_buffer: &mut CommandBuffer,
mut meshes: ResourceMut<Assets<Mesh>>,
mut materials: ResourceMut<Assets<StandardMaterial>>,
) {
let cube_handle = meshes.add(Mesh::from(shape::Cube));
let cube_material_handle = material_storage.add(StandardMaterial {
let cube_material_handle = materials.add(StandardMaterial {
albedo: Color::rgb(0.5, 0.4, 0.3),
..Default::default()
});
world
command_buffer
.build()
// parent cube
.add_entity(MeshEntity {
@ -45,7 +44,7 @@ fn setup(world: &mut World, resources: &mut Resources) {
material: cube_material_handle,
translation: Translation::new(0.0, 0.0, 3.0),
..Default::default()
});
})
})
// light
.add_entity(LightEntity {

View File

@ -3,21 +3,21 @@ use bevy::prelude::*;
fn main() {
App::build()
.add_default_plugins()
.add_startup_system(setup)
.add_startup_system(setup.system())
.run();
}
/// set up a simple scene
fn setup(world: &mut World, resources: &mut Resources) {
fn setup(
command_buffer: &mut CommandBuffer,
mut meshes: ResourceMut<Assets<Mesh>>,
mut materials: ResourceMut<Assets<StandardMaterial>>,
) {
// create a cube and a plane mesh
let mut meshes = resources.get_mut::<Assets<Mesh>>().unwrap();
let cube_handle = meshes.add(Mesh::from(shape::Cube));
let plane_handle = meshes.add(Mesh::from(shape::Plane { size: 10.0 }));
// create materials for our cube and plane
let mut materials = resources
.get_mut::<Assets<StandardMaterial>>()
.unwrap();
let cube_material_handle = materials.add(StandardMaterial {
albedo: Color::rgb(0.5, 0.4, 0.3),
..Default::default()
@ -28,7 +28,7 @@ fn setup(world: &mut World, resources: &mut Resources) {
});
// add entities to the world
world
command_buffer
.build()
// plane
.add_entity(MeshEntity {

View File

@ -9,12 +9,12 @@ fn main() {
.add_default_plugins()
.add_plugin(FrameTimeDiagnosticsPlugin::default())
.add_plugin(PrintDiagnosticsPlugin::default())
.add_startup_system(setup)
.add_system(move_system.system())
.add_startup_system(setup.system())
.add_system(move_cubes.system())
.run();
}
fn move_system(
fn move_cubes(
time: Resource<Time>,
mut materials: ResourceMut<Assets<StandardMaterial>>,
mut translation: RefMut<Translation>,
@ -25,11 +25,11 @@ fn move_system(
material.albedo += Color::rgb(-time.delta_seconds, -time.delta_seconds, time.delta_seconds);
}
fn setup(world: &mut World, resources: &mut Resources) {
let mut meshes = resources.get_mut::<Assets<Mesh>>().unwrap();
let mut materials = resources
.get_mut::<Assets<StandardMaterial>>()
.unwrap();
fn setup(
command_buffer: &mut CommandBuffer,
mut meshes: ResourceMut<Assets<Mesh>>,
mut materials: ResourceMut<Assets<StandardMaterial>>,
) {
let cube_handle = meshes.add(Mesh::from(shape::Cube));
let plane_handle = meshes.add(Mesh::from(shape::Plane { size: 10.0 }));
let cube_material_handle = materials.add(StandardMaterial {
@ -41,7 +41,7 @@ fn setup(world: &mut World, resources: &mut Resources) {
..Default::default()
});
let mut builder = world.build();
let mut builder = command_buffer.build();
builder
// plane
.add_entity(MeshEntity {

View File

@ -3,14 +3,18 @@ use bevy::prelude::*;
fn main() {
App::build()
.add_default_plugins()
.add_startup_system(setup)
.add_startup_system(setup.system())
.run();
}
/// sets up a scene with textured entities
fn setup(world: &mut World, resources: &mut Resources) {
fn setup(
command_buffer: &mut CommandBuffer,
mut meshes: ResourceMut<Assets<Mesh>>,
mut textures: ResourceMut<Assets<Texture>>,
mut materials: ResourceMut<Assets<StandardMaterial>>,
) {
// load a texture
let mut textures = resources.get_mut::<Assets<Texture>>().unwrap();
let texture_path = concat!(
env!("CARGO_MANIFEST_DIR"),
"/assets/branding/bevy_logo_dark_big.png"
@ -20,17 +24,11 @@ fn setup(world: &mut World, resources: &mut Resources) {
let texture_handle = textures.add(texture);
// create a new quad mesh. this is what we will apply the texture to
let mut meshes = resources.get_mut::<Assets<Mesh>>().unwrap();
let quad_width = 8.0;
let quad_handle = meshes.add(Mesh::from(shape::Quad {
size: Vec2::new(quad_width, quad_width * aspect),
}));
// create materials that use our new texture
let mut materials = resources
.get_mut::<Assets<StandardMaterial>>()
.unwrap();
// this material renders the texture normally
let material_handle = materials.add(StandardMaterial {
albedo_texture: Some(texture_handle),
@ -45,7 +43,7 @@ fn setup(world: &mut World, resources: &mut Resources) {
});
// add entities to the world
world
command_buffer
.build()
// textured quad - normal
.add_entity(MeshEntity {

View File

@ -5,22 +5,22 @@ pub struct ExamplePlugin;
impl AppPlugin for ExamplePlugin {
fn build(&self, app: &mut AppBuilder) {
app.add_startup_system(setup);
app.add_startup_system(setup.system());
}
}
pub fn setup(world: &mut World, resources: &mut Resources) {
let mut meshes = resources.get_mut::<Assets<Mesh>>().unwrap();
let mut materials = resources
.get_mut::<Assets<StandardMaterial>>()
.unwrap();
fn setup(
command_buffer: &mut CommandBuffer,
mut meshes: ResourceMut<Assets<Mesh>>,
mut materials: ResourceMut<Assets<StandardMaterial>>,
) {
let cube_handle = meshes.add(Mesh::from(shape::Cube));
let cube_material_handle = materials.add(StandardMaterial {
albedo: Color::rgb(0.5, 0.4, 0.3),
..Default::default()
});
world
command_buffer
.build()
// cube
.add_entity(MeshEntity {

View File

@ -7,9 +7,9 @@ fn main() {
App::build()
.add_default_plugins()
.init_resource::<State>()
.add_system(collect_input_system.system())
.add_system(move_system.system())
.add_startup_system(setup)
.add_startup_system(setup.system())
.add_system(collect_input.system())
.add_system(move_on_input.system())
.run();
}
@ -21,7 +21,7 @@ struct State {
}
/// adjusts move state based on keyboard input
fn collect_input_system(
fn collect_input(
mut state: ResourceMut<State>,
keyboard_input_events: Resource<Events<KeyboardInput>>,
) {
@ -47,7 +47,7 @@ fn collect_input_system(
}
/// moves our cube left when the "left" key is pressed. moves it right when the "right" key is pressed
fn move_system(
fn move_on_input(
state: Resource<State>,
time: Resource<Time>,
mut translation: RefMut<Translation>,
@ -63,18 +63,18 @@ fn move_system(
}
/// creates a simple scene
fn setup(world: &mut World, resources: &mut Resources) {
let mut meshes = resources.get_mut::<Assets<Mesh>>().unwrap();
let mut materials = resources
.get_mut::<Assets<StandardMaterial>>()
.unwrap();
fn setup(
command_buffer: &mut CommandBuffer,
mut meshes: ResourceMut<Assets<Mesh>>,
mut materials: ResourceMut<Assets<StandardMaterial>>,
) {
let cube_handle = meshes.add(Mesh::from(shape::Cube));
let cube_material_handle = materials.add(StandardMaterial {
albedo: Color::rgb(0.5, 0.4, 0.3),
..Default::default()
});
world
command_buffer
.build()
// cube
.add_entity(MeshEntity {

View File

@ -4,7 +4,7 @@ fn main() {
App::build()
.add_default_plugins()
.add_asset::<MyMaterial>()
.add_startup_system(setup)
.add_startup_system(setup.system())
.run();
}
@ -13,14 +13,16 @@ struct MyMaterial {
pub color: Color,
}
fn setup(world: &mut World, resources: &mut Resources) {
fn setup(
command_buffer: &mut CommandBuffer,
mut pipelines: ResourceMut<Assets<PipelineDescriptor>>,
mut shaders: ResourceMut<Assets<Shader>>,
mut meshes: ResourceMut<Assets<Mesh>>,
mut materials: ResourceMut<Assets<MyMaterial>>,
mut render_graph: ResourceMut<RenderGraph>,
) {
// create new shader pipeline and add to main pass in Render Graph
let pipeline_handle = {
let mut pipelines = resources
.get_mut::<Assets<PipelineDescriptor>>()
.unwrap();
let mut shaders = resources.get_mut::<Assets<Shader>>().unwrap();
let pipeline_handle = pipelines.add(PipelineDescriptor::default_config(ShaderStages {
vertex: shaders.add(Shader::from_glsl(
ShaderStage::Vertex,
@ -52,12 +54,9 @@ fn setup(world: &mut World, resources: &mut Resources) {
"#,
))),
}));
let mut render_graph = resources.get_mut::<RenderGraph>().unwrap();
render_graph.add_system_node_named(
"my_material",
AssetUniformNode::<MyMaterial>::new(true),
resources,
);
render_graph
.add_system_node_named("my_material", AssetUniformNode::<MyMaterial>::new(true));
let main_pass: &mut PassNode = render_graph.get_node_mut("main_pass").unwrap();
main_pass.add_pipeline(
pipeline_handle,
@ -67,15 +66,12 @@ fn setup(world: &mut World, resources: &mut Resources) {
};
// create materials
let mut materials = resources.get_mut::<Assets<MyMaterial>>().unwrap();
let material = materials.add(MyMaterial {
color: Color::rgb(0.0, 0.8, 0.0),
});
let mut meshes = resources.get_mut::<Assets<Mesh>>().unwrap();
let cube_handle = meshes.add(Mesh::from(shape::Cube));
world
command_buffer
.build()
// cube
.add_entity(MeshMaterialEntity::<MyMaterial> {

View File

@ -4,10 +4,10 @@ fn main() {
App::build()
.add_default_plugins()
.add_asset::<MyMaterial>()
.add_startup_system(setup)
.add_startup_system(setup.system())
.add_system_to_stage(
stage::POST_UPDATE,
shader::asset_handle_shader_def_system::<MyMaterial>.system(),
shader::asset_shader_def_system::<MyMaterial>.system(),
)
.run();
}
@ -19,14 +19,16 @@ struct MyMaterial {
pub always_red: bool,
}
fn setup(world: &mut World, resources: &mut Resources) {
fn setup(
command_buffer: &mut CommandBuffer,
mut pipelines: ResourceMut<Assets<PipelineDescriptor>>,
mut shaders: ResourceMut<Assets<Shader>>,
mut meshes: ResourceMut<Assets<Mesh>>,
mut materials: ResourceMut<Assets<MyMaterial>>,
mut render_graph: ResourceMut<RenderGraph>,
) {
// create new shader pipeline and add to main pass in Render Graph
let pipeline_handle = {
let mut pipelines = resources
.get_mut::<Assets<PipelineDescriptor>>()
.unwrap();
let mut shaders = resources.get_mut::<Assets<Shader>>().unwrap();
let pipeline_handle = pipelines.add(PipelineDescriptor::default_config(ShaderStages {
vertex: shaders.add(Shader::from_glsl(
ShaderStage::Vertex,
@ -62,12 +64,8 @@ fn setup(world: &mut World, resources: &mut Resources) {
"#,
))),
}));
let mut render_graph = resources.get_mut::<RenderGraph>().unwrap();
render_graph.add_system_node_named(
"my_material",
AssetUniformNode::<MyMaterial>::new(true),
resources,
);
render_graph
.add_system_node_named("my_material", AssetUniformNode::<MyMaterial>::new(true));
let main_pass: &mut PassNode = render_graph.get_node_mut("main_pass").unwrap();
main_pass.add_pipeline(
pipeline_handle,
@ -77,7 +75,6 @@ fn setup(world: &mut World, resources: &mut Resources) {
};
// create materials
let mut materials = resources.get_mut::<Assets<MyMaterial>>().unwrap();
let green_material = materials.add(MyMaterial {
color: Color::rgb(0.0, 0.8, 0.0),
always_red: false,
@ -88,10 +85,9 @@ fn setup(world: &mut World, resources: &mut Resources) {
always_red: true,
});
let mut meshes = resources.get_mut::<Assets<Mesh>>().unwrap();
let cube_handle = meshes.add(Mesh::from(shape::Cube));
world
command_buffer
.build()
// cube
.add_entity(MeshMaterialEntity::<MyMaterial> {

View File

@ -4,12 +4,15 @@ use std::{fs::File, io::Read};
fn main() {
App::build()
.add_default_plugins()
.add_startup_system(setup)
.add_startup_system(setup.system())
.run();
}
fn setup(world: &mut World, resources: &mut Resources) {
let mut textures = resources.get_mut::<Assets<Texture>>().unwrap();
fn setup(
command_buffer: &mut CommandBuffer,
mut textures: ResourceMut<Assets<Texture>>,
mut materials: ResourceMut<Assets<ColorMaterial>>,
) {
let font_path = concat!(
env!("CARGO_MANIFEST_DIR"),
"/assets/fonts/FiraSans-Bold.ttf"
@ -23,8 +26,7 @@ fn setup(world: &mut World, resources: &mut Resources) {
let half_width = texture.width as f32 / 2.0;
let half_height = texture.height as f32 / 2.0;
let texture_handle = textures.add(texture);
let mut color_materials = resources.get_mut::<Assets<ColorMaterial>>().unwrap();
world
command_buffer
.build()
// 2d camera
.add_entity(Camera2dEntity::default())
@ -35,7 +37,7 @@ fn setup(world: &mut World, resources: &mut Resources) {
Anchors::CENTER,
Margins::new(-half_width, half_width, -half_height, half_height),
),
material: color_materials.add(ColorMaterial::texture(texture_handle)),
material: materials.add(ColorMaterial::texture(texture_handle)),
..Default::default()
});
}

View File

@ -3,11 +3,15 @@ use bevy::prelude::*;
fn main() {
App::build()
.add_default_plugins()
.add_startup_system(setup)
.add_startup_system(setup.system())
.run();
}
fn setup(world: &mut World, resources: &mut Resources) {
fn setup(
command_buffer: &mut CommandBuffer,
mut textures: ResourceMut<Assets<Texture>>,
mut materials: ResourceMut<Assets<ColorMaterial>>,
) {
// TODO: "background" 3D temporarily disabled until depth mismatch is fixed
// let mut mesh_storage = resources.get_mut::<AssetStorage<Mesh>>().unwrap();
// let mut material_storage = resources
@ -19,7 +23,6 @@ fn setup(world: &mut World, resources: &mut Resources) {
// ..Default::default()
// });
let mut textures = resources.get_mut::<Assets<Texture>>().unwrap();
let texture_path = concat!(
env!("CARGO_MANIFEST_DIR"),
"/assets/branding/bevy_logo_dark_big.png"
@ -28,10 +31,9 @@ fn setup(world: &mut World, resources: &mut Resources) {
let aspect = texture.aspect();
let texture_handle = textures.add(texture);
let mut color_materials = resources.get_mut::<Assets<ColorMaterial>>().unwrap();
let blue_material_handle = color_materials.add(Color::rgb(0.6, 0.6, 1.0).into());
let blue_material_handle = materials.add(Color::rgb(0.6, 0.6, 1.0).into());
world
command_buffer
.build()
// // cube
// .add_entity(MeshEntity {
@ -63,7 +65,7 @@ fn setup(world: &mut World, resources: &mut Resources) {
Anchors::LEFT_FULL,
Margins::new(10.0, 200.0, 10.0, 10.0),
),
material: color_materials.add(Color::rgb(0.02, 0.02, 0.02).into()),
material: materials.add(Color::rgb(0.02, 0.02, 0.02).into()),
..Default::default()
})
// right vertical fill
@ -73,7 +75,7 @@ fn setup(world: &mut World, resources: &mut Resources) {
Anchors::RIGHT_FULL,
Margins::new(10.0, 100.0, 100.0, 100.0),
),
material: color_materials.add(Color::rgb(0.02, 0.02, 0.02).into()),
material: materials.add(Color::rgb(0.02, 0.02, 0.02).into()),
..Default::default()
})
// render order test: reddest in the back, whitest in the front
@ -83,7 +85,7 @@ fn setup(world: &mut World, resources: &mut Resources) {
Anchors::CENTER,
Margins::new(0.0, 100.0, 0.0, 100.0),
),
material: color_materials.add(Color::rgb(1.0, 0.0, 0.0).into()),
material: materials.add(Color::rgb(1.0, 0.0, 0.0).into()),
..Default::default()
})
.add_entity(UiEntity {
@ -92,7 +94,7 @@ fn setup(world: &mut World, resources: &mut Resources) {
Anchors::CENTER,
Margins::new(0.0, 100.0, 0.0, 100.0),
),
material: color_materials.add(Color::rgb(1.0, 0.3, 0.3).into()),
material: materials.add(Color::rgb(1.0, 0.3, 0.3).into()),
..Default::default()
})
.add_entity(UiEntity {
@ -101,7 +103,7 @@ fn setup(world: &mut World, resources: &mut Resources) {
Anchors::CENTER,
Margins::new(0.0, 100.0, 0.0, 100.0),
),
material: color_materials.add(Color::rgb(1.0, 0.5, 0.5).into()),
material: materials.add(Color::rgb(1.0, 0.5, 0.5).into()),
..Default::default()
})
.add_entity(UiEntity {
@ -110,7 +112,7 @@ fn setup(world: &mut World, resources: &mut Resources) {
Anchors::CENTER,
Margins::new(0.0, 100.0, 0.0, 100.0),
),
material: color_materials.add(Color::rgb(1.0, 0.7, 0.7).into()),
material: materials.add(Color::rgb(1.0, 0.7, 0.7).into()),
..Default::default()
})
// parenting
@ -120,7 +122,7 @@ fn setup(world: &mut World, resources: &mut Resources) {
Anchors::BOTTOM_LEFT,
Margins::new(0.0, 200.0, 10.0, 210.0),
),
material: color_materials.add(Color::rgb(0.1, 0.1, 1.0).into()),
material: materials.add(Color::rgb(0.1, 0.1, 1.0).into()),
..Default::default()
})
.add_children(|builder| {
@ -132,7 +134,7 @@ fn setup(world: &mut World, resources: &mut Resources) {
),
material: blue_material_handle,
..Default::default()
});
})
})
// alpha test
.add_entity(UiEntity {
@ -141,7 +143,7 @@ fn setup(world: &mut World, resources: &mut Resources) {
Anchors::CENTER,
Margins::new(0.0, 100.0, 0.0, 100.0),
),
material: color_materials.add(Color::rgba(1.0, 0.9, 0.9, 0.4).into()),
material: materials.add(Color::rgba(1.0, 0.9, 0.9, 0.4).into()),
..Default::default()
})
// texture
@ -151,7 +153,7 @@ fn setup(world: &mut World, resources: &mut Resources) {
Anchors::CENTER_TOP,
Margins::new(-250.0, 250.0, 510.0 * aspect, 10.0),
),
material: color_materials.add(ColorMaterial::texture(texture_handle)),
material: materials.add(ColorMaterial::texture(texture_handle)),
..Default::default()
});
}

View File

@ -3,7 +3,7 @@ use bevy::prelude::*;
fn main() {
App::build()
.add_default_plugins()
.add_startup_system(setup)
.add_startup_system(setup.system())
.add_system(placement_system.system())
.add_plugin(DiagnosticsPlugin::default())
.run();
@ -21,11 +21,10 @@ fn placement_system(
}
}
fn setup(world: &mut World, resources: &mut Resources) {
let mut builder = world.build();
fn setup(command_buffer: &mut CommandBuffer, mut materials: ResourceMut<Assets<ColorMaterial>>) {
let mut builder = command_buffer.build();
builder.add_entity(Camera2dEntity::default());
let mut materials = resources.get_mut::<Assets<ColorMaterial>>().unwrap();
let mut prev = Vec2::default();
let count = 1000;
for i in 0..count {

View File

@ -3,13 +3,12 @@ use bevy::{prelude::*, window::CreateWindow};
fn main() {
App::build()
.add_default_plugins()
.add_startup_system(setup)
.add_startup_system(setup.system())
.run();
}
fn setup(_world: &mut World, resources: &mut Resources) {
fn setup(mut create_window_events: ResourceMut<Events<CreateWindow>>) {
// sends out a "CreateWindow" event, which will be received by the windowing backend
let mut create_window_events = resources.get_mut::<Events<CreateWindow>>().unwrap();
create_window_events.send(CreateWindow {
descriptor: WindowDescriptor {
width: 800,