Upgraded to Bevy 0.15

This commit is contained in:
Silas Bartha 2024-12-10 16:06:06 -05:00
parent 3c711f2012
commit 3c229ff55b
6 changed files with 3117 additions and 39 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
target/

3075
Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,15 +1,15 @@
[package] [package]
name = "bevy_headless_render" name = "bevy_headless_render"
version = "0.1.2" version = "0.2.0"
edition = "2021" edition = "2021"
description = "A plugin for the bevy engine which enables headless rendering to an image for use in the main world." description = "A plugin for the bevy engine which enables headless rendering to an image for use in the main world."
license = "0BSD OR MIT OR Apache-2.0" license = "0BSD OR MIT OR Apache-2.0"
[dependencies] [dependencies]
oneshot = "0.1.6" oneshot = "0.1"
pollster = "0.3.0" pollster = "0.4"
[dependencies.bevy] [dependencies.bevy]
version = "0.14" version = "0.15"
default-features = false default-features = false
features = ["bevy_render", "bevy_asset"] features = ["bevy_render", "bevy_asset", "png", "bevy_pbr"]

View File

@ -3,7 +3,6 @@
[![Crates](https://img.shields.io/crates/v/bevy_headless_render)](https://crates.io/crates/bevy_headless_render) [![Crates](https://img.shields.io/crates/v/bevy_headless_render)](https://crates.io/crates/bevy_headless_render)
![License](https://img.shields.io/badge/license-MIT%2FApache-blue.svg) ![License](https://img.shields.io/badge/license-MIT%2FApache-blue.svg)
![Tag](https://img.shields.io/github/v/tag/exvacuum/bevy_headless_render) ![Tag](https://img.shields.io/github/v/tag/exvacuum/bevy_headless_render)
![Build](https://img.shields.io/github/actions/workflow/status/exvacuum/bevy_headless_render/rust.yml)
[![Docs](https://img.shields.io/docsrs/bevy_headless_render)](https://exvacuum.github.io/bevy_headless_render) [![Docs](https://img.shields.io/docsrs/bevy_headless_render)](https://exvacuum.github.io/bevy_headless_render)
A plugin for the [Bevy](https://bevyengine.org) engine which allows for headless rendering. A plugin for the [Bevy](https://bevyengine.org) engine which allows for headless rendering.
@ -15,19 +14,20 @@ Every frame will be copied from `HeadlessRenderSource` render textures into `Hea
| Crate Version | Bevy Version | | Crate Version | Bevy Version |
|--- |--- | |--- |--- |
| 0.1 | 0.14 | | 0.1 | 0.14 |
| 0.2 | 0.15 |
## Installation ## Installation
### crates.io ### crates.io
```toml ```toml
[dependencies] [dependencies]
bevy_headless_render = "0.1" bevy_headless_render = "0.2"
``` ```
### Using git URL in Cargo.toml ### Using git URL in Cargo.toml
```toml ```toml
[dependencies.bevy_headless_render] [dependencies.bevy_headless_render]
git = "https://github.com/exvacuum/bevy_headless_render.git" git = "https://git.exvacuum.dev/bevy_headless_render"
``` ```
## Usage ## Usage
@ -41,9 +41,6 @@ fn main() {
App::new() App::new()
.add_plugins(( .add_plugins((
DefaultPlugins DefaultPlugins
.build()
.disable::<WinitPlugin>(),
ScheduleRunnerPlugin::run_loop(Duration::from_secs_f32(1.0 / TICK_RATE)),
bevy_headless_render::HeadlessRenderPlugin, bevy_headless_render::HeadlessRenderPlugin,
)) ))
.run(); .run();
@ -63,7 +60,7 @@ let mut image = Image {
label: None, label: None,
size, size,
dimension: TextureDimension::D2, dimension: TextureDimension::D2,
format: TextureFormat::R8Unorm, format: TextureFormat::Rgba8UnormSrgb,
mip_level_count: 1, mip_level_count: 1,
sample_count: 1, sample_count: 1,
usage: TextureUsages::TEXTURE_BINDING usage: TextureUsages::TEXTURE_BINDING
@ -76,20 +73,15 @@ let mut image = Image {
image.resize(size); image.resize(size);
let image_handle = images.add(image); // ResMut<Assets<Image>> let image_handle = asset_server.add(image);
commands.spawn(( commands.spawn((
Camera3dBundle { HeadlessRenderSource::new(&asset_server, image_handle.clone()),
camera: Camera { Camera3d::default(),
target: image_handle.clone().into(); Camera {
..Default::default() target: image_handle.into(),
},
..Default::default() ..Default::default()
}, },
bevy_headless_render::HeadlessRenderBundle {
source: headless_render_sources.add(HeadlessRenderSource(image_handle.clone())), // ResMut<Assets<HeadlessRenderSource>>
destination: HeadlessRenderDestination::default(),
},
)); ));
``` ```

View File

@ -1,19 +1,19 @@
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex, MutexGuard, PoisonError};
use bevy::{ecs::query::QueryItem, prelude::*, render::extract_component::ExtractComponent}; use bevy::{ecs::query::QueryItem, prelude::*, render::extract_component::ExtractComponent};
use crate::render_assets::HeadlessRenderSource; use crate::render_assets;
/// Headless render destination. Contains the image which the rendered frame is copied to. /// Headless render destination. Contains the image which the rendered frame is copied to.
#[derive(Component, Default, Clone)] #[derive(Component, Default, Clone)]
pub struct HeadlessRenderDestination(pub Arc<Mutex<Image>>); pub struct HeadlessRenderDestination(pub Arc<Mutex<Image>>);
impl ExtractComponent for HeadlessRenderDestination { impl ExtractComponent for HeadlessRenderDestination {
type QueryData = (&'static Self, &'static Handle<HeadlessRenderSource>); type QueryData = (&'static Self, &'static HeadlessRenderSource);
type QueryFilter = (); type QueryFilter = ();
type Out = (Self, Handle<HeadlessRenderSource>); type Out = (Self, HeadlessRenderSource);
fn extract_component( fn extract_component(
(destination, source_handle): QueryItem<'_, Self::QueryData>, (destination, source_handle): QueryItem<'_, Self::QueryData>,
@ -22,11 +22,21 @@ impl ExtractComponent for HeadlessRenderDestination {
} }
} }
/// Bundle containing both a source and destination for headless rendering. impl HeadlessRenderDestination {
#[derive(Bundle)] /// Get lock on this destination's image
pub struct HeadlessRenderBundle { pub fn image(&self) -> Result<MutexGuard<Image>, PoisonError<MutexGuard<Image>>> {
/// Source self.0.lock()
pub source: Handle<HeadlessRenderSource>, }
/// Destination }
pub dest: HeadlessRenderDestination,
/// Headless render source
#[derive(Component, Debug, Clone, DerefMut, Deref)]
#[require(HeadlessRenderDestination, Camera3d)]
pub struct HeadlessRenderSource(pub Handle<render_assets::HeadlessRenderSource>);
impl HeadlessRenderSource {
/// Create a new headless render source from the provided image
pub fn new(asset_server: &AssetServer, image: Handle<Image>) -> Self {
Self(asset_server.add(render_assets::HeadlessRenderSource(image)))
}
} }

View File

@ -9,18 +9,18 @@ use bevy::{
use pollster::FutureExt; use pollster::FutureExt;
use crate::{components::HeadlessRenderDestination, render_assets::{HeadlessRenderSource, GpuHeadlessRenderSource}}; use crate::{
components::{HeadlessRenderDestination, HeadlessRenderSource},
render_assets::GpuHeadlessRenderSource,
};
pub fn copy_buffers( pub fn copy_buffers(
mut headless_render_query: Query<( mut headless_render_query: Query<(&HeadlessRenderSource, &mut HeadlessRenderDestination)>,
&Handle<HeadlessRenderSource>,
&mut HeadlessRenderDestination,
)>,
sources: Res<RenderAssets<GpuHeadlessRenderSource>>, sources: Res<RenderAssets<GpuHeadlessRenderSource>>,
device: Res<RenderDevice>, device: Res<RenderDevice>,
) { ) {
for (source_handle, destination_handle) in headless_render_query.iter_mut() { for (source_handle, destination_handle) in headless_render_query.iter_mut() {
let Some(gpu_source) = sources.get(source_handle) else { let Some(gpu_source) = sources.get(source_handle.id()) else {
continue; continue;
}; };