Improve editor workflows and thumbnail resilience
This commit is contained in:
1 parent
b5e561904c
commit
cc1fa6ea3b
9 files changed
+465
-135
No files matched your search
@@ -6,7 +6,7 @@ use bevy::prelude::*;
|
||||
use bevy_egui::{egui, EguiPrimaryContextPass, EguiTextureHandle, EguiUserTextures};
|
||||
use egui_phosphor_icons::icons;
|
||||
|
||||
use super::sources::gltf::gltf_base_color_texture_path;
|
||||
use super::sources::gltf::{gltf_base_color_texture_path, validate_gltf_dependencies};
|
||||
use super::studio::{model_file_exists, ThumbnailStudio};
|
||||
use super::ThumbnailJobSource;
|
||||
use crate::assets::{
|
||||
@@ -100,13 +100,12 @@ impl AssetThumbnailCache {
|
||||
return;
|
||||
}
|
||||
|
||||
if let Some(texture_path) = gltf_base_color_texture_path(&model_path) {
|
||||
self.request_texture(key, texture_path, asset_server);
|
||||
if !self.model_source_ready(&key, &model_path) {
|
||||
return;
|
||||
}
|
||||
|
||||
if !model_file_exists(&model_path) {
|
||||
self.mark_studio_failed(&key, "file not found", false);
|
||||
if let Some(texture_path) = gltf_base_color_texture_path(&model_path) {
|
||||
self.request_texture(key, texture_path, asset_server);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -130,8 +129,7 @@ impl AssetThumbnailCache {
|
||||
{
|
||||
return;
|
||||
}
|
||||
if !model_file_exists(&model_path) {
|
||||
self.mark_studio_failed(&key, "file not found", false);
|
||||
if !self.model_source_ready(&key, &model_path) {
|
||||
return;
|
||||
}
|
||||
if studio.enqueue_source(
|
||||
@@ -160,8 +158,7 @@ impl AssetThumbnailCache {
|
||||
{
|
||||
return;
|
||||
}
|
||||
if !model_file_exists(&model_path) {
|
||||
self.mark_studio_failed(&key, "file not found", false);
|
||||
if !self.model_source_ready(&key, &model_path) {
|
||||
return;
|
||||
}
|
||||
if studio.enqueue_source(
|
||||
@@ -206,6 +203,18 @@ impl AssetThumbnailCache {
|
||||
}
|
||||
}
|
||||
|
||||
fn model_source_ready(&mut self, key: &str, model_path: &str) -> bool {
|
||||
if !model_file_exists(model_path) {
|
||||
self.mark_studio_failed(key, "file not found", false);
|
||||
return false;
|
||||
}
|
||||
if let Err(reason) = validate_gltf_dependencies(model_path) {
|
||||
self.mark_studio_failed(key, &reason, false);
|
||||
return false;
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
pub(crate) fn complete_studio_thumbnail(
|
||||
&mut self,
|
||||
key: &str,
|
||||
|
||||
@@ -29,6 +29,41 @@ pub fn gltf_base_color_texture_path(model_asset_path: &str) -> Option<String> {
|
||||
None
|
||||
}
|
||||
|
||||
/// Verifies local external buffers and images before Bevy starts an async glTF load.
|
||||
pub fn validate_gltf_dependencies(model_asset_path: &str) -> Result<(), String> {
|
||||
let model_path = Path::new(model_asset_path);
|
||||
if !is_gltf_path(model_path) {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let gltf = gltf::Gltf::open(model_path).map_err(|error| format!("invalid glTF: {error}"))?;
|
||||
let mut uris = Vec::new();
|
||||
for buffer in gltf.buffers() {
|
||||
if let gltf::buffer::Source::Uri(uri) = buffer.source() {
|
||||
uris.push(uri);
|
||||
}
|
||||
}
|
||||
for image in gltf.images() {
|
||||
if let gltf::image::Source::Uri { uri, .. } = image.source() {
|
||||
uris.push(uri);
|
||||
}
|
||||
}
|
||||
|
||||
let model_dir = model_path.parent().unwrap_or_else(|| Path::new(""));
|
||||
for uri in uris {
|
||||
if uri.starts_with("data:") || uri.contains("://") {
|
||||
continue;
|
||||
}
|
||||
let dependency = model_dir.join(uri);
|
||||
if !dependency.is_file() {
|
||||
let display = normalize_asset_path(&dependency)
|
||||
.unwrap_or_else(|| dependency.to_string_lossy().replace('\\', "/"));
|
||||
return Err(format!("missing dependency: {display}"));
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub struct GltfThumbnailSource;
|
||||
|
||||
fn is_gltf_path(path: &Path) -> bool {
|
||||
@@ -61,7 +96,9 @@ fn normalize_asset_path(path: &Path) -> Option<String> {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::fs;
|
||||
use std::path::PathBuf;
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
|
||||
#[test]
|
||||
fn normalize_asset_path_keeps_assets_prefix() {
|
||||
@@ -79,4 +116,25 @@ mod tests {
|
||||
Some("assets/models/textures/chair_albedo.png".into())
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dependency_validation_reports_missing_external_buffer() {
|
||||
let nonce = SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.unwrap()
|
||||
.as_nanos();
|
||||
let directory = std::env::temp_dir().join(format!("blacksite-gltf-{nonce}"));
|
||||
fs::create_dir_all(&directory).unwrap();
|
||||
let model = directory.join("missing-buffer.gltf");
|
||||
fs::write(
|
||||
&model,
|
||||
r#"{"asset":{"version":"2.0"},"buffers":[{"uri":"missing.bin","byteLength":4}]}"#,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let error = validate_gltf_dependencies(model.to_str().unwrap()).unwrap_err();
|
||||
assert!(error.contains("missing.bin"));
|
||||
|
||||
fs::remove_dir_all(directory).unwrap();
|
||||
}
|
||||
}
|
||||
@@ -315,6 +315,9 @@ fn process_thumbnail_studio(
|
||||
ThumbnailStudioLayer,
|
||||
RenderLayers::layer(THUMBNAIL_LAYER),
|
||||
Transform::default(),
|
||||
Visibility::Visible,
|
||||
InheritedVisibility::default(),
|
||||
ViewVisibility::default(),
|
||||
))
|
||||
.id();
|
||||
|
||||
|
||||
@@ -1,5 +1,12 @@
|
||||
//! Runtime game crate: FPS player, rendering, and starter world.
|
||||
#![allow(clippy::type_complexity)]
|
||||
#![cfg_attr(
|
||||
feature = "hot-reload",
|
||||
allow(
|
||||
clippy::too_many_arguments,
|
||||
reason = "hot-lib-reloader wrappers preserve Bevy system signatures and macro spans"
|
||||
)
|
||||
)]
|
||||
|
||||
pub mod editor_ext;
|
||||
pub mod launch;
|
||||
@@ -62,8 +69,8 @@ mod systems {
|
||||
mod hot {
|
||||
use avian3d::prelude::*;
|
||||
use bevy::input::mouse::AccumulatedMouseMotion;
|
||||
use bevy::light::atmosphere::ScatteringMedium;
|
||||
use bevy::light::CascadeShadowConfig;
|
||||
use bevy::pbr::ScatteringMedium;
|
||||
use bevy::prelude::*;
|
||||
use bevy::time::Fixed;
|
||||
use bevy::window::{CursorOptions, PrimaryWindow};
|
||||
|
||||
Reference in new issue
Block a user