dtcristo / bevy_pixels

Bevy plugin that uses Pixels (a tiny pixel buffer) for rendering
https://docs.rs/bevy_pixels
Apache License 2.0
85 stars 12 forks source link

bevy_pixels

Bevy plugin that uses Pixels (a tiny pixel buffer) for rendering

crates.io Bevy tracking

Usage

Add bevy and bevy_pixels to Cargo.toml. Be sure to disable bevy's render and bevy_wgpu features (with default-features = false) as they will conflict with rendering provided by bevy_pixels.

[dependencies]
bevy = { version = "0.13", default_features = false }
bevy_pixels = "0.13"

Add PixelsPlugin to your Bevy project.

use bevy::prelude::*;
use bevy_pixels::prelude::*;

fn main() {
    App::new()
        .add_plugins((DefaultPlugins, PixelsPlugin::default()))
        // Add systems that draw to the buffer to `Draw` schedule
        // to ensure they are rendered in the current frame.
        .add_systems(Draw, draw)
        .run();
}

Use PixelsWrapper in your systems.

fn draw(mut wrapper_query: Query<&mut PixelsWrapper>) {
    // Query the `PixelsWrapper` component that owns an instance of `Pixels` for the given window.
    let Ok(mut wrapper) = wrapper_query.get_single_mut() else { return };

    // Get a mutable slice for the pixel buffer.
    let frame: &mut [u8] = wrapper.pixels.frame_mut();

    // Fill frame with pixel data.
    // ...
}

Bevy and Pixels version mapping

bevy_pixels bevy pixels
0.13 0.13 0.13
0.12 0.12 0.13
0.11 0.11 0.13
0.9-0.10 0.10 0.12
0.8 0.9 0.11
0.7 0.9 0.10
0.6 0.8 0.10
0.5 0.7 0.9
0.3-0.4 0.6 0.9
0.2 0.5 0.8
0.1 0.5 0.3

Examples

minimal

This example demonstrates rendering a solid color to the pixel buffer.

multiple_windows

This example demonstrate usage of multiple windows each with their own pixel buffer.

custom_render

This example demonstrate usage of a custom render system. Default render cargo feature must be disabled before defining a custom render system. Use default_features = "false" in Cargo.toml.

bounce

More advanced example based off the minimal-winit example from the pixels project. It demonstrates rendering dynamic content to the pixel buffer as well as custom configuration for PixelsPlugin and PixelsOptions on the primary window.

bounce example

Running examples natively

Build and run example with just. See Justfile for more details. Install just with cargo install just.

just run example_name

Running examples in web browser

Install dependencies.

rustup target add wasm32-unknown-unknown
cargo install wasm-bindgen-cli miniserve

Build and serve example for web.

just serve-web example_name

Open localhost:8080 in your web browser to run the example.

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you shall be dual licensed as above, without any additional terms or conditions.