Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/shanecelis/bevy_mod_plotters
A plotter material for bevy
https://github.com/shanecelis/bevy_mod_plotters
bevy plot shader
Last synced: 26 days ago
JSON representation
A plotter material for bevy
- Host: GitHub
- URL: https://github.com/shanecelis/bevy_mod_plotters
- Owner: shanecelis
- License: apache-2.0
- Created: 2024-10-27T03:30:55.000Z (3 months ago)
- Default Branch: main
- Last Pushed: 2024-10-29T00:13:18.000Z (2 months ago)
- Last Synced: 2024-11-16T18:43:51.985Z (about 2 months ago)
- Topics: bevy, plot, shader
- Language: Rust
- Homepage:
- Size: 83 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# bevy_mod_plotters
![Maintenance](https://img.shields.io/badge/maintenance-actively--developed-brightgreen.svg)
[![CI](https://github.com/shanecelis/bevy_mod_plotters/actions/workflows/rust.yml/badge.svg)](https://github.com/shanecelis/bevy_mod_plotters/actions)
[![crates-io](https://img.shields.io/crates/v/bevy_mod_plotters.svg)](https://crates.io/crates/bevy_mod_plotters)
[![api-docs](https://docs.rs/bevy_mod_plotters/badge.svg)](https://docs.rs/bevy_mod_plotters)A [bevy game engine](https://bevyengine.org) material to display the [plotters](https://github.com/plotters-rs/plotters) BGRX texture format.
![Example anim_graph of sine wave.](https://github.com/user-attachments/assets/4c953a1b-a95a-4b72-8d18-efdeab4b79b9)
# Motivation
The BGRX texture used by plotters is like a BGRA texture, but the plotters crate
does not does not abstain from overwriting the 'X' byte, which for BGRA is the
alpha channel, so one cannot rely on setting alpha once in the texture data but
must assume it was changed after any render from plotters. This crate exists
principally to avoid that alpha resetting problem and provide convenience
methods.# Install
Install the crate.
```sh
cargo add bevy_mod_plotters
```# Usage
## Add Plugin to App
```rust,no_run
use bevy::prelude::*;fn main() {
App::new()
.add_plugins(bevy_mod_plotters::PlottersPlugin)
.run();
}
```## Create Image and Plot
``` rust,no_run
# use bevy::{
# prelude::*,
# render::{
# render_asset::RenderAssetUsages,
# render_resource::{Extent3d, TextureDimension, TextureFormat},
# texture::Image,
# },
# };
# use plotters::coord::Shift;
use bevy_mod_plotters::prelude::*;
use plotters::prelude::*;
fn setup() -> Image {
let mut image = Image::new_fill(
Extent3d {
width: 500,
height: 500,
depth_or_array_layers: 1,
},
TextureDimension::D2,
&[0x00, 0x00, 0x00, 0x00],
TextureFormat::Bgra8UnormSrgb,
RenderAssetUsages::MAIN_WORLD | RenderAssetUsages::RENDER_WORLD,
);
{
let root = try_as_backend(&mut image)
.unwrap()
.into_drawing_area();
plot(&root)
.unwrap();
}
image
}fn plot(
root: &DrawingArea,
) -> Result<(), DrawingAreaErrorKind> {let mut chart = ChartBuilder::on(&root)
.caption("Hello, World!", ("sans-serif", 40).into_font())
.build_cartesian_2d(-1.0..1.0, -1.0..1.0)?;let points = vec![(0.0, 0.0), (5.0, 5.0), (8.0, 7.0)];
chart.draw_series(LineSeries::new(points, &RED))?;
root.present()
}
```## Add Image as a `PlotUiMaterial`
```rust,compile
# use bevy::{
# prelude::*,
# color::palettes::basic,
# pbr::ExtendedMaterial,
# };
# use bevy_mod_plotters::*;
fn add_image(
In(image): In,
mut commands: Commands,
mut images: ResMut>,
mut ui_materials: ResMut>,
) {
commands.spawn(Camera2dBundle::default());
commands
.spawn(NodeBundle {
style: Style {
width: Val::Percent(100.),
height: Val::Percent(100.),
justify_content: JustifyContent::Center,
..Default::default()
},
..Default::default()
})
.with_children(|parent| {
parent.spawn(MaterialNodeBundle {
style: Style {
width: Val::Px(500.0),
height: Val::Px(500.0),
border: UiRect::all(Val::Px(20.0)),
..default()
},
material: ui_materials.add(PlotUiMaterial {
color: LinearRgba::WHITE,
texture: images.add(image),
}),
..default()
});
});
}
```# Examples
Run the "draw_graph" example like so:
```sh
cargo run --example draw_graph
```This will show a plot of three points and a red line.
* `draw_graph` - Plot three points and a line; hit 'Space' to update.
* `anim_graph` - Animate a sinusoidal wave.# TODO
Consider adding a plotters backend specifically for the BGRA8 formats that will
not overwrite the alpha channel. A backend like that might be slightly slower for writing, but it would allow one to produce plots with a transparent background.# Compatibility
| bevy_mod_plotters | bevy |
|-------------------|------|
| 0.1 | 0.14 |# License
This crate is licensed under the MIT License or the Apache License 2.0. The
examples are licensed under the CC0 license.# Acknowlegments
* Thanks to [Claire V. Hammond](https://github.com/cvhammond) for the [bevy_plotters_test](https://github.com/cvhammond/bevy_plotters_test) repository.