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]
name = "bevy_headless_render"
version = "0.1.2"
version = "0.2.0"
edition = "2021"
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"
[dependencies]
oneshot = "0.1.6"
pollster = "0.3.0"
oneshot = "0.1"
pollster = "0.4"
[dependencies.bevy]
version = "0.14"
version = "0.15"
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)
![License](https://img.shields.io/badge/license-MIT%2FApache-blue.svg)
![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)
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 |
|--- |--- |
| 0.1 | 0.14 |
| 0.2 | 0.15 |
## Installation
### crates.io
```toml
[dependencies]
bevy_headless_render = "0.1"
bevy_headless_render = "0.2"
```
### Using git URL in Cargo.toml
```toml
[dependencies.bevy_headless_render]
git = "https://github.com/exvacuum/bevy_headless_render.git"
git = "https://git.exvacuum.dev/bevy_headless_render"
```
## Usage
@ -41,9 +41,6 @@ fn main() {
App::new()
.add_plugins((
DefaultPlugins
.build()
.disable::<WinitPlugin>(),
ScheduleRunnerPlugin::run_loop(Duration::from_secs_f32(1.0 / TICK_RATE)),
bevy_headless_render::HeadlessRenderPlugin,
))
.run();
@ -63,7 +60,7 @@ let mut image = Image {
label: None,
size,
dimension: TextureDimension::D2,
format: TextureFormat::R8Unorm,
format: TextureFormat::Rgba8UnormSrgb,
mip_level_count: 1,
sample_count: 1,
usage: TextureUsages::TEXTURE_BINDING
@ -76,20 +73,15 @@ let mut image = Image {
image.resize(size);
let image_handle = images.add(image); // ResMut<Assets<Image>>
let image_handle = asset_server.add(image);
commands.spawn((
Camera3dBundle {
camera: Camera {
target: image_handle.clone().into();
..Default::default()
},
HeadlessRenderSource::new(&asset_server, image_handle.clone()),
Camera3d::default(),
Camera {
target: image_handle.into(),
..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 crate::render_assets::HeadlessRenderSource;
use crate::render_assets;
/// Headless render destination. Contains the image which the rendered frame is copied to.
#[derive(Component, Default, Clone)]
pub struct HeadlessRenderDestination(pub Arc<Mutex<Image>>);
impl ExtractComponent for HeadlessRenderDestination {
type QueryData = (&'static Self, &'static Handle<HeadlessRenderSource>);
type QueryData = (&'static Self, &'static HeadlessRenderSource);
type QueryFilter = ();
type Out = (Self, Handle<HeadlessRenderSource>);
type Out = (Self, HeadlessRenderSource);
fn extract_component(
(destination, source_handle): QueryItem<'_, Self::QueryData>,
@ -22,11 +22,21 @@ impl ExtractComponent for HeadlessRenderDestination {
}
}
/// Bundle containing both a source and destination for headless rendering.
#[derive(Bundle)]
pub struct HeadlessRenderBundle {
/// Source
pub source: Handle<HeadlessRenderSource>,
/// Destination
pub dest: HeadlessRenderDestination,
impl HeadlessRenderDestination {
/// Get lock on this destination's image
pub fn image(&self) -> Result<MutexGuard<Image>, PoisonError<MutexGuard<Image>>> {
self.0.lock()
}
}
/// 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 crate::{components::HeadlessRenderDestination, render_assets::{HeadlessRenderSource, GpuHeadlessRenderSource}};
use crate::{
components::{HeadlessRenderDestination, HeadlessRenderSource},
render_assets::GpuHeadlessRenderSource,
};
pub fn copy_buffers(
mut headless_render_query: Query<(
&Handle<HeadlessRenderSource>,
&mut HeadlessRenderDestination,
)>,
mut headless_render_query: Query<(&HeadlessRenderSource, &mut HeadlessRenderDestination)>,
sources: Res<RenderAssets<GpuHeadlessRenderSource>>,
device: Res<RenderDevice>,
) {
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;
};