Expand asset browser subasset workflow
CI / Format, lint, test, build (push) Waiting to run

This commit is contained in:
Rbanh committed 2026-06-06 01:38:27 -04:00
1 parent f29712d158
commit 3e053a81ab
123 files changed
+31930 -1404

No files matched your search

+160 -2
View File
@@ -11,7 +11,8 @@ use shared::{
use walkdir::WalkDir;
use crate::asset_db::{
find_asset_by_path, find_asset_mut_by_path, AssetRegistry, ModelPlacementMode,
find_asset_by_path, find_asset_mut_by_path, AssetRegistry, MaterialImportPolicy,
ModelPlacementMode,
};
use crate::assets::static_mesh::{
load_static_mesh_manifest, refresh_static_mesh_artifact, renderer_from_manifest,
@@ -21,10 +22,24 @@ use crate::history::{spawn_with_history, EditorEntitySnapshot};
pub const BUILTINS_FOLDER: &str = "__builtins__";
pub const ASSETS_ROOT: &str = "assets";
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum AssetSubAssetKind {
Mesh,
Material,
Texture,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum AssetSelection {
Builtin(String),
File(String),
SubAsset {
parent_path: String,
sub_asset_id: String,
label: String,
kind: AssetSubAssetKind,
source_path: Option<String>,
},
}
impl AssetSelection {
@@ -34,6 +49,21 @@ impl AssetSelection {
None => AssetSelection::Builtin(asset.label.clone()),
}
}
pub fn parent_path(&self) -> Option<&str> {
match self {
AssetSelection::Builtin(_) => None,
AssetSelection::File(path) => Some(path),
AssetSelection::SubAsset { parent_path, .. } => Some(parent_path),
}
}
pub fn display_label(&self) -> &str {
match self {
AssetSelection::Builtin(label) | AssetSelection::File(label) => label,
AssetSelection::SubAsset { label, .. } => label,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
@@ -137,6 +167,10 @@ impl EditorAssets {
.assets
.iter()
.find(|asset| asset.path.as_deref() == Some(path.as_str())),
AssetSelection::SubAsset { parent_path, .. } => self
.assets
.iter()
.find(|asset| asset.path.as_deref() == Some(parent_path.as_str())),
}
}
@@ -152,10 +186,20 @@ impl EditorAssets {
.and_then(|selection| self.asset_for_selection(selection))
}
pub fn dragging_selection(&self) -> Option<&AssetSelection> {
self.dragging.as_ref()
}
pub fn select(&mut self, selection: AssetSelection) {
if let Some(asset) = self.asset_for_selection(&selection).cloned() {
let selected_label = match &selection {
AssetSelection::SubAsset { label, kind, .. } => {
format!("{kind:?}: {label}")
}
_ => asset.label.clone(),
};
self.selected = Some(selection);
self.status = format!("Selected {}", asset.label);
self.status = format!("Selected {selected_label}");
}
}
@@ -302,6 +346,7 @@ fn should_skip_asset_path(path: &Path) -> bool {
let normalized = normalize_asset_path(path);
normalized == "assets/project.ron"
|| normalized.starts_with("assets/.index/")
|| normalized.starts_with("assets/.trash/")
|| normalized.starts_with(crate::assets::static_mesh::STATIC_MESH_ARTIFACT_DIR)
}
@@ -543,6 +588,101 @@ pub fn spawn_asset_at(world: &mut World, asset: &EditorAsset, translation: Vec3)
Some(spawn_with_history(world, snapshot))
}
pub fn spawn_subasset_at(
world: &mut World,
selection: &AssetSelection,
translation: Vec3,
) -> Option<Entity> {
let AssetSelection::SubAsset {
parent_path,
sub_asset_id,
label,
kind: AssetSubAssetKind::Mesh,
..
} = selection
else {
return None;
};
let record = world
.get_resource::<AssetRegistry>()
.and_then(|registry| find_asset_by_path(registry, parent_path))?;
let manifest_path = record
.import_settings
.static_mesh_manifest_path
.as_deref()?;
let manifest = load_static_mesh_manifest(manifest_path).ok()?;
let part = manifest
.parts
.iter()
.find(|part| part_effective_id_for_selection(part) == *sub_asset_id)?;
let mesh_ref = shared::EditorAssetRef::new(
manifest.asset_id.clone(),
sub_asset_id.clone(),
label.clone(),
);
let material = matches!(
record.import_settings.material_policy,
MaterialImportPolicy::SourceMaterials
)
.then(|| {
part_effective_material_id_for_selection(part).map(|id| {
shared::EditorAssetRef::new(
manifest.asset_id.clone(),
id,
part.material_slot_name.clone(),
)
})
})
.flatten();
let slot = shared::StaticMeshRendererEntry {
id: shared::ComponentInstanceId::new("slot:0"),
name: label.clone(),
mesh: mesh_ref.clone(),
material,
local_transform: part.local_transform,
visible: true,
cast_shadows: true,
receive_shadows: true,
};
let mut snapshot = EditorEntitySnapshot {
actor_id: None,
actor_kind: ActorKind::StaticMesh,
actor_name: None,
name: Some(label.clone()),
transform: Transform::from_translation(translation + Vec3::Y * 0.1),
primitive: None,
static_mesh_renderer: Some(shared::StaticMeshRenderer::single(slot)),
material: None,
material_override: None,
rigid_body: None,
collider: None,
physics: None,
light: None,
player_spawn: false,
model: None,
prefab: None,
prefab_instance: None,
weapon_spawn: None,
trigger_volume: None,
post_process_volume: None,
team_spawn: None,
objective: None,
hierarchy_sibling_index: 0,
editor_visibility: EditorVisibility::default(),
children: Vec::new(),
};
snapshot.transform.scale *= record.import_settings.scale;
if record.import_settings.generate_collider {
snapshot.rigid_body = Some(RigidBodyDesc::default());
snapshot.collider = Some(ColliderDesc::static_mesh(vec![mesh_ref]));
}
Some(spawn_with_history(world, snapshot))
}
fn apply_static_mesh_placement_mode(
snapshot: &mut EditorEntitySnapshot,
renderer: shared::StaticMeshRenderer,
@@ -619,6 +759,24 @@ fn static_mesh_collider_for_renderer(renderer: &shared::StaticMeshRenderer) -> C
)
}
fn part_effective_id_for_selection(part: &crate::assets::static_mesh::StaticMeshPart) -> String {
if part.id.trim().is_empty() {
crate::assets::static_mesh::part_id_from_label(&part.mesh_label)
} else {
part.id.clone()
}
}
fn part_effective_material_id_for_selection(
part: &crate::assets::static_mesh::StaticMeshPart,
) -> Option<String> {
part.material_id.clone().or_else(|| {
part.material_label
.as_ref()
.map(|label| crate::assets::static_mesh::material_id_from_label(label))
})
}
fn static_mesh_renderer_for_asset(
world: &mut World,
path: &str,
+1 -1
View File
@@ -11,5 +11,5 @@ pub mod thumbnails;
pub use catalog::*;
pub use thumbnails::{
draw_asset_cell_with, invalidate_on_catalog_refresh, kind_icon, prefetch_folder_thumbnails,
AssetThumbnailCache, ThumbnailCacheSnapshot, ThumbnailState, ThumbnailsPlugin,
AssetThumbnailCache, ThumbnailCacheSnapshot, ThumbnailState, ThumbnailStudio, ThumbnailsPlugin,
};
+108 -1
View File
@@ -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)
}
_ => {}
}
}
+34 -1
View File
@@ -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,
}
}
}
+1 -1
View File
@@ -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,
+168 -44
View File
@@ -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(