This commit is contained in:
1 parent
f29712d158
commit
3e053a81ab
123 files changed
+31930
-1404
No files matched your search
@@ -8,6 +8,7 @@ use egui_phosphor_icons::icons;
|
||||
|
||||
use super::sources::gltf::gltf_base_color_texture_path;
|
||||
use super::studio::{model_file_exists, ThumbnailStudio};
|
||||
use super::ThumbnailJobSource;
|
||||
use crate::assets::{
|
||||
asset_cache_key, asset_server_path, EditorAsset, EditorAssetKind, EditorAssets,
|
||||
};
|
||||
@@ -114,6 +115,97 @@ impl AssetThumbnailCache {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn request_mesh_subasset(
|
||||
&mut self,
|
||||
key: String,
|
||||
model_path: String,
|
||||
mesh_label: String,
|
||||
material_label: Option<String>,
|
||||
studio: &mut ThumbnailStudio,
|
||||
) {
|
||||
if matches!(
|
||||
self.state(&key),
|
||||
Some(ThumbnailState::Ready | ThumbnailState::Pending)
|
||||
) || self.failed.contains_key(&key)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if !model_file_exists(&model_path) {
|
||||
self.mark_studio_failed(&key, "file not found", false);
|
||||
return;
|
||||
}
|
||||
if studio.enqueue_source(
|
||||
key.clone(),
|
||||
ThumbnailJobSource::MeshSubAsset {
|
||||
model_path,
|
||||
mesh_label,
|
||||
material_label,
|
||||
},
|
||||
) {
|
||||
self.studio_pending.insert(key);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn request_source_material(
|
||||
&mut self,
|
||||
key: String,
|
||||
model_path: String,
|
||||
material_label: String,
|
||||
studio: &mut ThumbnailStudio,
|
||||
) {
|
||||
if matches!(
|
||||
self.state(&key),
|
||||
Some(ThumbnailState::Ready | ThumbnailState::Pending)
|
||||
) || self.failed.contains_key(&key)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if !model_file_exists(&model_path) {
|
||||
self.mark_studio_failed(&key, "file not found", false);
|
||||
return;
|
||||
}
|
||||
if studio.enqueue_source(
|
||||
key.clone(),
|
||||
ThumbnailJobSource::SourceMaterial {
|
||||
model_path,
|
||||
material_label,
|
||||
},
|
||||
) {
|
||||
self.studio_pending.insert(key);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn request_material_asset(
|
||||
&mut self,
|
||||
key: String,
|
||||
path: String,
|
||||
studio: &mut ThumbnailStudio,
|
||||
) {
|
||||
if matches!(
|
||||
self.state(&key),
|
||||
Some(ThumbnailState::Ready | ThumbnailState::Pending)
|
||||
) || self.failed.contains_key(&key)
|
||||
{
|
||||
return;
|
||||
}
|
||||
let material = match shared::MaterialAsset::load_from_path(&path) {
|
||||
Ok(asset) => asset,
|
||||
Err(error) => {
|
||||
self.mark_studio_failed(&key, &error, true);
|
||||
return;
|
||||
}
|
||||
};
|
||||
if studio.enqueue_source(
|
||||
key.clone(),
|
||||
ThumbnailJobSource::MaterialAsset {
|
||||
label: material.label.clone(),
|
||||
material: material.material,
|
||||
},
|
||||
) {
|
||||
self.studio_pending.insert(key);
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn complete_studio_thumbnail(
|
||||
&mut self,
|
||||
key: &str,
|
||||
@@ -297,6 +389,18 @@ pub struct ThumbnailCacheSnapshot {
|
||||
}
|
||||
|
||||
impl ThumbnailCacheSnapshot {
|
||||
pub fn texture_for_key(&self, key: &str) -> Option<egui::TextureId> {
|
||||
self.texture_ids.get(key).copied()
|
||||
}
|
||||
|
||||
pub fn is_pending_key(&self, key: &str) -> bool {
|
||||
self.pending_keys.iter().any(|pending| pending == key)
|
||||
}
|
||||
|
||||
pub fn is_failed_key(&self, key: &str) -> bool {
|
||||
self.failed_keys.iter().any(|failed| failed == key)
|
||||
}
|
||||
|
||||
pub fn texture_for(&self, asset: &EditorAsset) -> Option<egui::TextureId> {
|
||||
self.texture_ids.get(&asset_cache_key(asset)).copied()
|
||||
}
|
||||
@@ -331,7 +435,7 @@ pub fn prefetch_folder_thumbnails(world: &mut World, folder: &str) {
|
||||
.filter(|asset| {
|
||||
matches!(
|
||||
asset.kind,
|
||||
EditorAssetKind::Texture | EditorAssetKind::Model
|
||||
EditorAssetKind::Texture | EditorAssetKind::Model | EditorAssetKind::Material
|
||||
)
|
||||
})
|
||||
.filter_map(|asset| {
|
||||
@@ -352,6 +456,9 @@ pub fn prefetch_folder_thumbnails(world: &mut World, folder: &str) {
|
||||
EditorAssetKind::Model => {
|
||||
cache.request_model(key, path, &asset_server, &mut studio)
|
||||
}
|
||||
EditorAssetKind::Material => {
|
||||
cache.request_material_asset(key, path, &mut studio)
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,40 @@
|
||||
//! Thumbnail generation job descriptor.
|
||||
|
||||
use shared::MaterialDesc;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ThumbnailJob {
|
||||
pub cache_key: String,
|
||||
pub model_path: String,
|
||||
pub source: ThumbnailJobSource,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum ThumbnailJobSource {
|
||||
Model {
|
||||
model_path: String,
|
||||
},
|
||||
MeshSubAsset {
|
||||
model_path: String,
|
||||
mesh_label: String,
|
||||
material_label: Option<String>,
|
||||
},
|
||||
SourceMaterial {
|
||||
model_path: String,
|
||||
material_label: String,
|
||||
},
|
||||
MaterialAsset {
|
||||
label: String,
|
||||
material: MaterialDesc,
|
||||
},
|
||||
}
|
||||
|
||||
impl ThumbnailJobSource {
|
||||
pub fn label(&self) -> &str {
|
||||
match self {
|
||||
Self::Model { model_path }
|
||||
| Self::MeshSubAsset { model_path, .. }
|
||||
| Self::SourceMaterial { model_path, .. } => model_path,
|
||||
Self::MaterialAsset { label, .. } => label,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -11,7 +11,7 @@ pub use cache::{
|
||||
draw_asset_cell_with, invalidate_on_catalog_refresh, kind_icon, prefetch_folder_thumbnails,
|
||||
AssetThumbnailCache, AssetThumbnailsPlugin, ThumbnailCacheSnapshot, ThumbnailState,
|
||||
};
|
||||
pub use job::ThumbnailJob;
|
||||
pub use job::{ThumbnailJob, ThumbnailJobSource};
|
||||
pub use sources::{FbxThumbnailSource, GltfThumbnailSource, ThumbnailModelSource};
|
||||
pub use studio::{
|
||||
model_file_exists, model_scene_asset_path, ThumbnailStudio, ThumbnailStudioPlugin,
|
||||
|
||||
@@ -12,10 +12,10 @@ use bevy::prelude::*;
|
||||
use bevy::render::render_resource::TextureFormat;
|
||||
use bevy::scene::SceneRoot;
|
||||
use bevy_egui::EguiUserTextures;
|
||||
use shared::ModelRef;
|
||||
use shared::{material_from_desc, ModelRef};
|
||||
|
||||
use super::cache::AssetThumbnailCache;
|
||||
use super::job::ThumbnailJob;
|
||||
use super::job::{ThumbnailJob, ThumbnailJobSource};
|
||||
use super::sources::{source_for_extension, uses_scene_root};
|
||||
use crate::assets::asset_server_path;
|
||||
use crate::infra::EditorOnly;
|
||||
@@ -52,7 +52,8 @@ pub struct ThumbnailStudio {
|
||||
|
||||
struct ActiveModelThumbnail {
|
||||
cache_key: String,
|
||||
model_path: String,
|
||||
label: String,
|
||||
render_image: Handle<Image>,
|
||||
root: Entity,
|
||||
meshes_ready: bool,
|
||||
mesh_warmup_frames: u8,
|
||||
@@ -75,6 +76,10 @@ impl Plugin for ThumbnailStudioPlugin {
|
||||
|
||||
impl ThumbnailStudio {
|
||||
pub fn enqueue(&mut self, cache_key: String, model_path: String) -> bool {
|
||||
self.enqueue_source(cache_key, ThumbnailJobSource::Model { model_path })
|
||||
}
|
||||
|
||||
pub fn enqueue_source(&mut self, cache_key: String, source: ThumbnailJobSource) -> bool {
|
||||
if self
|
||||
.active
|
||||
.as_ref()
|
||||
@@ -85,10 +90,7 @@ impl ThumbnailStudio {
|
||||
if self.queue.iter().any(|job| job.cache_key == cache_key) {
|
||||
return false;
|
||||
}
|
||||
self.queue.push_back(ThumbnailJob {
|
||||
cache_key,
|
||||
model_path,
|
||||
});
|
||||
self.queue.push_back(ThumbnailJob { cache_key, source });
|
||||
true
|
||||
}
|
||||
|
||||
@@ -122,12 +124,7 @@ pub fn model_scene_asset_path(path: &str, scene_index: usize) -> String {
|
||||
}
|
||||
|
||||
fn setup_thumbnail_studio(mut commands: Commands, mut images: ResMut<Assets<Image>>) {
|
||||
let render_image = images.add(Image::new_target_texture(
|
||||
THUMB_SIZE,
|
||||
THUMB_SIZE,
|
||||
TextureFormat::Rgba8UnormSrgb,
|
||||
None,
|
||||
));
|
||||
let render_image = images.add(studio_render_image());
|
||||
|
||||
let camera = commands
|
||||
.spawn((
|
||||
@@ -200,8 +197,10 @@ fn process_thumbnail_studio(
|
||||
mut textures: ResMut<EguiUserTextures>,
|
||||
mut materials: ResMut<Assets<StandardMaterial>>,
|
||||
mut mesh_storage: ResMut<Assets<Mesh>>,
|
||||
mut images: ResMut<Assets<Image>>,
|
||||
asset_server: Res<AssetServer>,
|
||||
mut cameras: Query<&mut Camera, With<ThumbnailStudioCamera>>,
|
||||
mut camera_targets: Query<&mut RenderTarget, With<ThumbnailStudioCamera>>,
|
||||
mut camera_transforms: Query<&mut Transform, With<ThumbnailStudioCamera>>,
|
||||
children: Query<&Children>,
|
||||
transforms: Query<&GlobalTransform>,
|
||||
@@ -240,13 +239,14 @@ fn process_thumbnail_studio(
|
||||
if active.wait_frames >= LOAD_TIMEOUT_FRAMES {
|
||||
warn!(
|
||||
"Model thumbnail timed out framing {} (meshes may still be loading)",
|
||||
active.model_path
|
||||
active.label
|
||||
);
|
||||
fail_active_thumbnail(
|
||||
&mut commands,
|
||||
&mut studio,
|
||||
&mut cache,
|
||||
&mut cameras,
|
||||
&mut camera_targets,
|
||||
active,
|
||||
"load timeout",
|
||||
true,
|
||||
@@ -277,10 +277,16 @@ fn process_thumbnail_studio(
|
||||
} else {
|
||||
cache.complete_studio_thumbnail(
|
||||
&active.cache_key,
|
||||
studio.render_image.clone(),
|
||||
active.render_image.clone(),
|
||||
&mut textures,
|
||||
);
|
||||
finish_active_thumbnail(&mut commands, &mut studio, &mut cameras, active);
|
||||
finish_active_thumbnail(
|
||||
&mut commands,
|
||||
&mut studio,
|
||||
&mut cameras,
|
||||
&mut camera_targets,
|
||||
active,
|
||||
);
|
||||
}
|
||||
return;
|
||||
}
|
||||
@@ -294,43 +300,45 @@ fn process_thumbnail_studio(
|
||||
return;
|
||||
};
|
||||
|
||||
let mut root_entity = commands.spawn((
|
||||
EditorOnly,
|
||||
ThumbnailSceneRoot,
|
||||
ThumbnailStudioLayer,
|
||||
RenderLayers::layer(THUMBNAIL_LAYER),
|
||||
Transform::default(),
|
||||
));
|
||||
|
||||
if uses_scene_root(&job.model_path) {
|
||||
root_entity.insert(SceneRoot(
|
||||
asset_server.load(model_scene_asset_path(&job.model_path, 0)),
|
||||
));
|
||||
let render_image = images.add(studio_render_image());
|
||||
if let Ok(mut target) = camera_targets.get_mut(studio.camera) {
|
||||
*target = RenderTarget::Image(render_image.clone().into());
|
||||
}
|
||||
if let Ok(mut camera) = cameras.get_mut(studio.camera) {
|
||||
camera.is_active = false;
|
||||
}
|
||||
|
||||
let root = root_entity.id();
|
||||
let root = commands
|
||||
.spawn((
|
||||
EditorOnly,
|
||||
ThumbnailSceneRoot,
|
||||
ThumbnailStudioLayer,
|
||||
RenderLayers::layer(THUMBNAIL_LAYER),
|
||||
Transform::default(),
|
||||
))
|
||||
.id();
|
||||
|
||||
let content_spawned = if let Some(source) = source_for_extension(&job.model_path) {
|
||||
source.spawn_preview(
|
||||
&mut commands,
|
||||
&mut mesh_storage,
|
||||
&mut materials,
|
||||
root,
|
||||
&job.model_path,
|
||||
)
|
||||
} else {
|
||||
uses_scene_root(&job.model_path)
|
||||
};
|
||||
let label = job.source.label().to_string();
|
||||
let content_spawned = spawn_thumbnail_job_content(
|
||||
&mut commands,
|
||||
&mut mesh_storage,
|
||||
&mut materials,
|
||||
&asset_server,
|
||||
root,
|
||||
&job.source,
|
||||
);
|
||||
|
||||
if source_for_extension(&job.model_path).is_some() && !content_spawned {
|
||||
if !content_spawned {
|
||||
fail_active_thumbnail(
|
||||
&mut commands,
|
||||
&mut studio,
|
||||
&mut cache,
|
||||
&mut cameras,
|
||||
&mut camera_targets,
|
||||
ActiveModelThumbnail {
|
||||
cache_key: job.cache_key,
|
||||
model_path: job.model_path,
|
||||
label,
|
||||
render_image,
|
||||
root,
|
||||
meshes_ready: false,
|
||||
mesh_warmup_frames: 0,
|
||||
@@ -348,7 +356,8 @@ fn process_thumbnail_studio(
|
||||
|
||||
studio.active = Some(ActiveModelThumbnail {
|
||||
cache_key: job.cache_key,
|
||||
model_path: job.model_path,
|
||||
label,
|
||||
render_image,
|
||||
root,
|
||||
meshes_ready: false,
|
||||
mesh_warmup_frames: 0,
|
||||
@@ -360,13 +369,127 @@ fn process_thumbnail_studio(
|
||||
});
|
||||
}
|
||||
|
||||
fn spawn_thumbnail_job_content(
|
||||
commands: &mut Commands,
|
||||
mesh_storage: &mut Assets<Mesh>,
|
||||
materials: &mut Assets<StandardMaterial>,
|
||||
asset_server: &AssetServer,
|
||||
root: Entity,
|
||||
source: &ThumbnailJobSource,
|
||||
) -> bool {
|
||||
match source {
|
||||
ThumbnailJobSource::Model { model_path } => {
|
||||
if uses_scene_root(model_path) {
|
||||
commands.entity(root).insert(SceneRoot(
|
||||
asset_server.load(model_scene_asset_path(model_path, 0)),
|
||||
));
|
||||
}
|
||||
if let Some(source) = source_for_extension(model_path) {
|
||||
source.spawn_preview(commands, mesh_storage, materials, root, model_path)
|
||||
} else {
|
||||
uses_scene_root(model_path)
|
||||
}
|
||||
}
|
||||
ThumbnailJobSource::MeshSubAsset {
|
||||
model_path,
|
||||
mesh_label,
|
||||
material_label,
|
||||
} => spawn_mesh_subasset_preview(
|
||||
commands,
|
||||
materials,
|
||||
asset_server,
|
||||
root,
|
||||
model_path,
|
||||
mesh_label,
|
||||
material_label.as_deref(),
|
||||
),
|
||||
ThumbnailJobSource::SourceMaterial {
|
||||
model_path,
|
||||
material_label,
|
||||
} => spawn_material_sphere(
|
||||
commands,
|
||||
mesh_storage,
|
||||
root,
|
||||
asset_server.load(labeled_asset_path(
|
||||
&asset_server_path(model_path),
|
||||
material_label,
|
||||
)),
|
||||
),
|
||||
ThumbnailJobSource::MaterialAsset { material, .. } => {
|
||||
let material = materials.add(material_from_desc(asset_server, material));
|
||||
spawn_material_sphere(commands, mesh_storage, root, material)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn spawn_mesh_subasset_preview(
|
||||
commands: &mut Commands,
|
||||
materials: &mut Assets<StandardMaterial>,
|
||||
asset_server: &AssetServer,
|
||||
root: Entity,
|
||||
model_path: &str,
|
||||
mesh_label: &str,
|
||||
material_label: Option<&str>,
|
||||
) -> bool {
|
||||
let source_path = asset_server_path(model_path);
|
||||
let mesh: Handle<Mesh> = asset_server.load(labeled_asset_path(&source_path, mesh_label));
|
||||
let material = if let Some(material_label) = material_label {
|
||||
asset_server.load(labeled_asset_path(&source_path, material_label))
|
||||
} else {
|
||||
materials.add(StandardMaterial {
|
||||
base_color: Color::srgb(0.72, 0.72, 0.75),
|
||||
perceptual_roughness: 0.65,
|
||||
..default()
|
||||
})
|
||||
};
|
||||
commands.entity(root).with_children(|parent| {
|
||||
parent.spawn((
|
||||
RenderLayers::layer(THUMBNAIL_LAYER),
|
||||
Mesh3d(mesh),
|
||||
MeshMaterial3d(material),
|
||||
Transform::default(),
|
||||
));
|
||||
});
|
||||
true
|
||||
}
|
||||
|
||||
fn spawn_material_sphere(
|
||||
commands: &mut Commands,
|
||||
mesh_storage: &mut Assets<Mesh>,
|
||||
root: Entity,
|
||||
material: Handle<StandardMaterial>,
|
||||
) -> bool {
|
||||
let mesh = mesh_storage.add(Sphere::new(0.65).mesh().uv(48, 24));
|
||||
commands.entity(root).with_children(|parent| {
|
||||
parent.spawn((
|
||||
RenderLayers::layer(THUMBNAIL_LAYER),
|
||||
Mesh3d(mesh),
|
||||
MeshMaterial3d(material),
|
||||
Transform::default(),
|
||||
));
|
||||
});
|
||||
true
|
||||
}
|
||||
|
||||
fn labeled_asset_path(path: &str, label: &str) -> String {
|
||||
format!("{path}#{label}")
|
||||
}
|
||||
|
||||
fn studio_render_image() -> Image {
|
||||
Image::new_target_texture(THUMB_SIZE, THUMB_SIZE, TextureFormat::Rgba8UnormSrgb, None)
|
||||
}
|
||||
|
||||
fn finish_active_thumbnail(
|
||||
commands: &mut Commands,
|
||||
studio: &mut ThumbnailStudio,
|
||||
cameras: &mut Query<&mut Camera, With<ThumbnailStudioCamera>>,
|
||||
camera_targets: &mut Query<&mut RenderTarget, With<ThumbnailStudioCamera>>,
|
||||
active: ActiveModelThumbnail,
|
||||
) {
|
||||
despawn_thumbnail_root(commands, active.root);
|
||||
if let Ok(mut target) = camera_targets.get_mut(studio.camera) {
|
||||
*target = RenderTarget::Image(studio.render_image.clone().into());
|
||||
}
|
||||
deactivate_studio_camera(cameras, studio.camera);
|
||||
studio.cooldown_frames = COOLDOWN_FRAMES;
|
||||
}
|
||||
@@ -376,12 +499,13 @@ fn fail_active_thumbnail(
|
||||
studio: &mut ThumbnailStudio,
|
||||
cache: &mut AssetThumbnailCache,
|
||||
cameras: &mut Query<&mut Camera, With<ThumbnailStudioCamera>>,
|
||||
camera_targets: &mut Query<&mut RenderTarget, With<ThumbnailStudioCamera>>,
|
||||
active: ActiveModelThumbnail,
|
||||
reason: &str,
|
||||
retryable: bool,
|
||||
) {
|
||||
cache.mark_studio_failed(&active.cache_key, reason, retryable);
|
||||
finish_active_thumbnail(commands, studio, cameras, active);
|
||||
finish_active_thumbnail(commands, studio, cameras, camera_targets, active);
|
||||
}
|
||||
|
||||
fn apply_thumbnail_studio_layers(
|
||||
|
||||
Reference in new issue
Block a user