Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic assets preview and preview caching #151

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,5 @@ Cargo.lock

# mdbook generated files
design-book/book

crates/bevy_editor_launcher/assets/Bevy Assets
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ unused_qualifications = "warn"
[workspace.dependencies]
bevy = { git = "https://github.com/bevyengine/bevy.git", rev = "015f2c69ca2a2c009fd04eadada282482deaf469", features = ["wayland"] }
bevy_derive = { git = "https://github.com/bevyengine/bevy.git", rev = "015f2c69ca2a2c009fd04eadada282482deaf469" }
crossbeam-channel = "0.5"
thiserror = "1"
serde = { version = "1", features = ["derive"] }
atomicow = "1.0.0"
Expand Down
1 change: 1 addition & 0 deletions bevy_editor_panes/bevy_asset_browser/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ bevy_pane_layout.workspace = true
bevy_scroll_box.workspace = true
bevy_context_menu.workspace = true
atomicow.workspace = true
bevy_asset_preview.workspace = true

[lints]
workspace = true
10 changes: 9 additions & 1 deletion bevy_editor_panes/bevy_asset_browser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use bevy::{
},
prelude::*,
};
use bevy_asset_preview::AssetPreviewPlugin;
use bevy_pane_layout::PaneRegistry;
use bevy_scroll_box::ScrollBoxPlugin;
use ui::{top_bar::location_as_changed, AssetBrowserNode};
Expand All @@ -19,14 +20,21 @@ mod io;
mod ui;

/// The bevy asset browser plugin
pub struct AssetBrowserPanePlugin;
pub struct AssetBrowserPanePlugin {
/// Enable asset preview or not.
pub preview: bool,
}

impl Plugin for AssetBrowserPanePlugin {
fn build(&self, app: &mut App) {
embedded_asset!(app, "assets/directory_icon.png");
embedded_asset!(app, "assets/source_icon.png");
embedded_asset!(app, "assets/file_icon.png");

if self.preview {
app.add_plugins(AssetPreviewPlugin);
}

app.world_mut()
.get_resource_or_init::<PaneRegistry>()
.register("Asset Browser", |mut commands, pane_root| {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,7 @@ fn populate_directory_content(
.set_parent(parent_entity);
}
Entry::File(name) => {
spawn_file_node(commands, name.clone(), asset_server, theme)
.set_parent(parent_entity);
spawn_file_node(commands, name.clone(), location, theme).set_parent(parent_entity);
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions bevy_editor_panes/bevy_asset_browser/src/ui/nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use bevy::{
window::SystemCursorIcon,
winit::cursor::CursorIcon,
};
use bevy_asset_preview::PreviewAsset;
use bevy_context_menu::{ContextMenu, ContextMenuOption};
use bevy_editor_styles::Theme;

Expand Down Expand Up @@ -152,15 +153,15 @@ pub(crate) fn spawn_folder_node<'a>(
pub(crate) fn spawn_file_node<'a>(
commands: &'a mut Commands,
file_name: String,
asset_server: &Res<AssetServer>,
location: &Res<AssetBrowserLocation>,
theme: &Res<Theme>,
) -> EntityCommands<'a> {
let base_node = spawn_base_node(commands, theme).id();

// Icon
commands
.spawn((
UiImage::new(asset_server.load("embedded://bevy_asset_browser/assets/file_icon.png")),
PreviewAsset(location.path.join(&file_name)),
Node {
height: Val::Px(50.0),
..default()
Expand Down
4 changes: 3 additions & 1 deletion crates/bevy_asset_preview/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ version = "0.1.0"
edition = "2021"

[dependencies]
bevy.workspace = true
bevy.workspace = true
crossbeam-channel.workspace = true
image = "0.25"
93 changes: 93 additions & 0 deletions crates/bevy_asset_preview/src/io/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
use std::{
env,
io::{BufWriter, Cursor},
path::{Path, PathBuf},
};

use bevy::{
asset::{
io::{file::FileAssetWriter, AssetWriter},
AssetServer, Assets,
},
prelude::{Image, Res, ResMut},
render::{renderer::RenderDevice, texture::TextureFormatPixelInfo},
tasks::IoTaskPool,
};
use image::ImageEncoder;

use crate::render::{
receive::{MainWorldPreviewImageReceiver, PreviewImageCopies},
RenderedScenePreviews,
};

pub(crate) fn get_base_path() -> PathBuf {
if let Ok(manifest_dir) = env::var("BEVY_ASSET_ROOT") {
PathBuf::from(manifest_dir)
} else if let Ok(manifest_dir) = env::var("CARGO_MANIFEST_DIR") {
PathBuf::from(manifest_dir)
} else {
env::current_exe()
.map(|path| path.parent().map(ToOwned::to_owned).unwrap())
.unwrap()
}
}

pub fn receive_preview(
mut previews: ResMut<RenderedScenePreviews>,
asset_server: Res<AssetServer>,
mut images: ResMut<Assets<Image>>,
receiver: Res<MainWorldPreviewImageReceiver>,
mut image_copies: ResMut<PreviewImageCopies>,
) {
let thread_pool = IoTaskPool::get();

while let Ok((id, data)) = receiver.try_recv() {
let image = images.get_mut(id).unwrap();
let row_bytes = image.width() as usize * image.texture_descriptor.format.pixel_size();
let aligned_row_bytes = RenderDevice::align_copy_bytes_per_row(row_bytes);
if row_bytes == aligned_row_bytes {
image.data = data;
} else {
image.data = data
.chunks(aligned_row_bytes)
.take(image.height() as usize)
.flat_map(|row| &row[..row_bytes.min(row.len())])
.cloned()
.collect()
}

image_copies.remove(&id);

let Some(scene_handle) = previews.changed.remove(&id) else {
continue;
};
let Some(path) = asset_server.get_path(scene_handle) else {
continue;
};

let image_buffer = image.clone().try_into_dynamic().unwrap().into_rgba8();
let image_path =
Path::new("assets/cache/asset_preview").join(path.path().with_extension("png"));

thread_pool
.spawn(async move {
let image_path_full = get_base_path().join(&image_path);
FileAssetWriter::new("", true)
.write(&image_path)
.await
.unwrap();
image_buffer.save(image_path_full).unwrap();

// TODO use the following code once know why it fails sometimes.
// let mut writer = BufWriter::new(Cursor::new(Vec::new()));
// image_buffer
// .write_to(&mut writer, image::ImageFormat::Png)
// .unwrap();
// FileAssetWriter::new("", true)
// .write_bytes(&image_path, writer.buffer())
// .await
// .unwrap();
})
.detach();
}
}
63 changes: 61 additions & 2 deletions crates/bevy_asset_preview/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,28 @@
use bevy::prelude::*;
use std::path::PathBuf;

use bevy::{
app::{App, Last, Plugin, Update},
asset::{AssetPath, AssetServer, Handle},
gltf::GltfAssetLabel,
prelude::{Component, Deref, Image, IntoSystemConfigs},
render::{
extract_resource::ExtractResourcePlugin, graph::CameraDriverLabel,
render_graph::RenderGraph, Render, RenderApp, RenderSet,
},
scene::Scene,
};

use crate::render::{
receive::{
MainWorldPreviewImageReceiver, PreviewImageCopies, PreviewTextureToBufferLabel,
PreviewTextureToBufferNode, RenderWorldPreviewImageSender,
},
PreviewRendered, PreviewSceneState, PreviewSettings, RenderedScenePreviews,
};

mod io;
mod render;
mod ui;

/// This crate is a work in progress and is not yet ready for use.
/// The intention is to provide a way to load/render/unload assets in the background and provide previews of them in the Bevy Editor.
Expand All @@ -7,8 +31,43 @@ use bevy::prelude::*;
/// So long as the assets are unchanged, the previews will be cached and will not need to be re-rendered.
/// In theory this can be done passively in the background, and the previews will be ready when the user needs them.

#[derive(Component, Deref)]
pub struct PreviewAsset(pub PathBuf);

pub struct AssetPreviewPlugin;

impl Plugin for AssetPreviewPlugin {
fn build(&self, _app: &mut App) {}
fn build(&self, app: &mut App) {
let (s, r) = crossbeam_channel::unbounded();

app.add_plugins(ExtractResourcePlugin::<PreviewImageCopies>::default())
.add_event::<PreviewRendered>()
.add_systems(
Update,
(
render::update_queue,
render::update_preview_frames_counter,
ui::preview_handler,
io::receive_preview,
),
)
.add_systems(Last, render::change_render_layers)
.init_resource::<RenderedScenePreviews>()
.init_resource::<PreviewSettings>()
.init_resource::<PreviewSceneState>()
.init_resource::<PreviewImageCopies>()
.insert_resource(MainWorldPreviewImageReceiver(r));

let render_app = app.sub_app_mut(RenderApp);
render_app
.add_systems(
Render,
render::receive::receive_image_from_buffer.after(RenderSet::Render),
)
.insert_resource(RenderWorldPreviewImageSender(s));

let mut graph = render_app.world_mut().resource_mut::<RenderGraph>();
graph.add_node(PreviewTextureToBufferLabel, PreviewTextureToBufferNode);
graph.add_node_edge(CameraDriverLabel, PreviewTextureToBufferLabel);
}
}
Loading