bevy/crates/bevy_ecs
Robert Swain b1a91a823f bevy_pbr2: Add support for most of the StandardMaterial textures (#4)
* bevy_pbr2: Add support for most of the StandardMaterial textures

Normal maps are not included here as they require tangents in a vertex attribute.

* bevy_pbr2: Ensure RenderCommandQueue is ready for PbrShaders init

* texture_pipelined: Add a light to the scene so we can see stuff

* WIP bevy_pbr2: back to front sorting hack

* bevy_pbr2: Uniform control flow for texture sampling in pbr.frag

From 'fintelia' on the Bevy Render Rework Round 2 discussion:

"My understanding is that GPUs these days never use the "execute both branches
and select the result" strategy. Rather, what they do is evaluate the branch
condition on all threads of a warp, and jump over it if all of them evaluate to
false. If even a single thread needs to execute the if statement body, however,
then the remaining threads are paused until that is completed."

* bevy_pbr2: Simplify texture and sampler names

The StandardMaterial_ prefix is no longer needed

* bevy_pbr2: Match default 'AmbientColor' of current bevy_pbr for now

* bevy_pbr2: Convert from non-linear to linear sRGB for the color uniform

* bevy_pbr2: Add pbr_pipelined example

* Fix view vector in pbr frag to work in ortho

* bevy_pbr2: Use a 90 degree y fov and light range projection for lights

* bevy_pbr2: Add AmbientLight resource

* bevy_pbr2: Convert PointLight color to linear sRGB for use in fragment shader

* bevy_pbr2: pbr.frag: Rename PointLight.projection to view_projection

The uniform contains the view_projection matrix so this was incorrect.

* bevy_pbr2: PointLight is an OmniLight as it has a radius

* bevy_pbr2: Factoring out duplicated code

* bevy_pbr2: Implement RenderAsset for StandardMaterial

* Remove unnecessary texture and sampler clones

* fix comment formatting

* remove redundant Buffer:from

* Don't extract meshes when their material textures aren't ready

* make missing textures in the queue step an error

Co-authored-by: Aevyrie <aevyrie@gmail.com>
Co-authored-by: Carter Anderson <mcanders1@gmail.com>
2021-07-24 16:43:37 -07:00
..
examples Add a readme to bevy_ecs (#2028) 2021-06-08 01:57:24 +00:00
macros bevy_render now uses wgpu directly 2021-07-24 16:43:37 -07:00
src bevy_pbr2: Add support for most of the StandardMaterial textures (#4) 2021-07-24 16:43:37 -07:00
Cargo.toml Relicense Bevy under the dual MIT or Apache-2.0 license (#2509) 2021-07-23 21:11:51 +00:00
README.md use discord vanity link (#2420) 2021-07-01 20:41:42 +00:00

Bevy ECS

Crates.io license Discord

What is Bevy ECS?

Bevy ECS is an Entity Component System custom-built for the Bevy game engine. It aims to be simple to use, ergonomic, fast, massively parallel, opinionated, and featureful. It was created specifically for Bevy's needs, but it can easily be used as a standalone crate in other projects.

ECS

All app logic in Bevy uses the Entity Component System paradigm, which is often shortened to ECS. ECS is a software pattern that involves breaking your program up into Entities, Components, and Systems. Entities are unique "things" that are assigned groups of Components, which are then processed using Systems.

For example, one entity might have a Position and Velocity component, whereas another entity might have a Position and UI component. You might have a movement system that runs on all entities with a Position and Velocity component.

The ECS pattern encourages clean, decoupled designs by forcing you to break up your app data and logic into its core components. It also helps make your code faster by optimizing memory access patterns and making parallelism easier.

Concepts

Bevy ECS is Bevy's implementation of the ECS pattern. Unlike other Rust ECS implementations, which often require complex lifetimes, traits, builder patterns, or macros, Bevy ECS uses normal Rust data types for all of these concepts:

Components

Components are normal Rust structs. They are data stored in a World and specific instances of Components correlate to Entities.

struct Position { x: f32, y: f32 }

Worlds

Entities, Components, and Resources are stored in a World. Worlds, much like Rust std collections like HashSet and Vec, expose operations to insert, read, write, and remove the data they store.

let world = World::default();

Entities

Entities are unique identifiers that correlate to zero or more Components.

let entity = world.spawn()
    .insert(Position { x: 0.0, y: 0.0 })
    .insert(Velocity { x: 1.0, y: 0.0 })
    .id();

let entity_ref = world.entity(entity);
let position = entity_ref.get::<Position>().unwrap();
let velocity = entity_ref.get::<Velocity>().unwrap();

Systems

Systems are normal Rust functions. Thanks to the Rust type system, Bevy ECS can use function parameter types to determine what data needs to be sent to the system. It also uses this "data access" information to determine what Systems can run in parallel with each other.

fn print_position(query: Query<(Entity, &Position)>) {
    for (entity, position) in query.iter() {
        println!("Entity {:?} is at position: x {}, y {}", entity, position.x, position.y);
    }
}

Resources

Apps often require unique resources, such as asset collections, renderers, audio servers, time, etc. Bevy ECS makes this pattern a first class citizen. Resource is a special kind of component that does not belong to any entity. Instead, it is identified uniquely by its type:

#[derive(Default)]
struct Time {
    seconds: f32,
}

world.insert_resource(Time::default());

let time = world.get_resource::<Time>().unwrap();

// You can also access resources from Systems
fn print_time(time: Res<Time>) {
    println!("{}", time.seconds);
}

The resources.rs example illustrates how to read and write a Counter resource from Systems.

Schedules

Schedules consist of zero or more Stages, which run a set of Systems according to some execution strategy. Bevy ECS provides a few built in Stage implementations (ex: parallel, serial), but you can also implement your own! Schedules run Stages one-by-one in an order defined by the user.

The built in "parallel stage" considers dependencies between systems and (by default) run as many of them in parallel as possible. This maximizes performance, while keeping the system execution safe. You can also define explicit dependencies between systems.

Using Bevy ECS

Bevy ECS should feel very natural for those familiar with Rust syntax:

use bevy_ecs::prelude::*;

struct Velocity {
    x: f32,
    y: f32,
}

struct Position {
    x: f32,
    y: f32,
}

// This system moves each entity with a Position and Velocity component
fn movement(query: Query<(&mut Position, &Velocity)>) {
    for (mut position, velocity) in query.iter_mut() {
        position.x += velocity.x;
        position.y += velocity.y;
    }
}

fn main() {
    // Create a new empty World to hold our Entities and Components
    let mut world = World::new();

    // Spawn an entity with Position and Velocity components
    world.spawn()
        .insert(Position { x: 0.0, y: 0.0 })
        .insert(Velocity { x: 1.0, y: 0.0 });

    // Create a new Schedule, which defines an execution strategy for Systems
    let mut schedule = Schedule::default();

    // Add a Stage to our schedule. Each Stage in a schedule runs all of its systems
    // before moving on to the next Stage
    schedule.add_stage("update", SystemStage::parallel()
        .with_system(movement.system())
    );

    // Run the schedule once. If your app has a "loop", you would run this once per loop
    schedule.run(&mut world);
}

Features

Query Filters

// Gets the Position component of all Entities with Player component and without the RedTeam component
fn system(query: Query<&Position, (With<Player>, Without<RedTeam>)>) {
    for position in query.iter() {
    }
}

Change Detection

Bevy ECS tracks all changes to Components and Resources.

Queries can filter for changed Components:

// Gets the Position component of all Entities whose Velocity has changed since the last run of the System
fn system(query: Query<&Position, Changed<Velocity>>) {
    for position in query.iter() {
    }
}

// Gets the Position component of all Entities that had a Velocity component added since the last run of the System
fn system(query: Query<&Position, Added<Velocity>>) {
    for position in query.iter() {
    }
}

Resources also expose change state:

// Prints "time changed!" if the Time resource has changed since the last run of the System
fn system(time: Res<Time>) {
    if time.is_changed() {
        println!("time changed!");
    }
}

The change_detection.rs example shows how to query only for updated entities and react on changes in resources.

Component Storage

Bevy ECS supports multiple component storage types.

Components can be stored in:

  • Tables: Fast and cache friendly iteration, but slower adding and removing of components. This is the default storage type.
  • Sparse Sets: Fast adding and removing of components, but slower iteration.

Component storage types are configurable, and they default to table storage if the storage is not manually defined. The component_storage.rs example shows how to configure the storage type for a component.

// store Position components in Sparse Sets
world.register_component(ComponentDescriptor::new::<Position>(StorageType::SparseSet));

Component Bundles

Define sets of Components that should be added together.

#[derive(Bundle, Default)]
struct PlayerBundle {
    player: Player,
    position: Position,
    velocity: Velocity,
}

// Spawn a new entity and insert the default PlayerBundle
world.spawn().insert_bundle(PlayerBundle::default());

// Bundles play well with Rust's struct update syntax
world.spawn().insert_bundle(PlayerBundle {
    position: Position { x: 1.0, y: 1.0 },
    ..Default::default()
});

Events

Events offer a communication channel between one or more systems. Events can be sent using the system parameter EventWriter and received with EventReader.

struct MyEvent {
    message: String,
}

fn writer(mut writer: EventWriter<MyEvent>) {
    writer.send(MyEvent {
        message: "hello!".to_string(),
    });
}

fn reader(mut reader: EventReader<MyEvent>) {
    for event in reader.iter() {
    }
}

A minimal set up using events can be seen in events.rs.