This commit is contained in:
commit
f29712d158
259 files changed
+52069
No files matched your search
@@ -0,0 +1,123 @@
|
||||
//! Direct FBX mesh extraction for thumbnail studio (no texture deps).
|
||||
|
||||
use bevy::asset::RenderAssetUsages;
|
||||
use bevy::camera::visibility::RenderLayers;
|
||||
use bevy::mesh::{Indices, PrimitiveTopology};
|
||||
use bevy::prelude::*;
|
||||
use bevy_ufbx::mesh::group_faces_by_material;
|
||||
use bevy_ufbx::utils::convert_matrix;
|
||||
|
||||
use super::ThumbnailModelSource;
|
||||
use crate::assets::thumbnails::studio::THUMBNAIL_LAYER;
|
||||
|
||||
pub struct FbxThumbnailSource;
|
||||
|
||||
impl ThumbnailModelSource for FbxThumbnailSource {
|
||||
fn spawn_preview(
|
||||
&self,
|
||||
commands: &mut Commands,
|
||||
meshes: &mut Assets<Mesh>,
|
||||
materials: &mut Assets<StandardMaterial>,
|
||||
root: Entity,
|
||||
catalog_path: &str,
|
||||
) -> bool {
|
||||
let Ok(bytes) = std::fs::read(catalog_path) else {
|
||||
warn!("FBX thumbnail: could not read {catalog_path}");
|
||||
return false;
|
||||
};
|
||||
|
||||
let scene = match ufbx::load_memory(
|
||||
&bytes,
|
||||
ufbx::LoadOpts {
|
||||
target_unit_meters: 1.0,
|
||||
target_axes: ufbx::CoordinateAxes::right_handed_y_up(),
|
||||
..Default::default()
|
||||
},
|
||||
) {
|
||||
Ok(scene) => scene,
|
||||
Err(err) => {
|
||||
warn!("FBX thumbnail: failed to parse {catalog_path}: {err:?}");
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
let material = materials.add(StandardMaterial {
|
||||
base_color: Color::srgb(0.72, 0.72, 0.75),
|
||||
..default()
|
||||
});
|
||||
|
||||
let mut spawned = false;
|
||||
for node in scene.nodes.as_ref().iter() {
|
||||
let Some(mesh_ref) = node.mesh.as_ref() else {
|
||||
continue;
|
||||
};
|
||||
let ufbx_mesh = mesh_ref.as_ref();
|
||||
if ufbx_mesh.num_vertices == 0 || ufbx_mesh.faces.as_ref().is_empty() {
|
||||
continue;
|
||||
}
|
||||
|
||||
for (_material_idx, indices) in group_faces_by_material(ufbx_mesh) {
|
||||
if indices.is_empty() {
|
||||
continue;
|
||||
}
|
||||
|
||||
let mesh_handle = meshes.add(build_mesh(ufbx_mesh, &indices));
|
||||
let transform = Transform::from_matrix(convert_matrix(&node.geometry_to_world));
|
||||
|
||||
commands.entity(root).with_children(|parent| {
|
||||
parent.spawn((
|
||||
RenderLayers::layer(THUMBNAIL_LAYER),
|
||||
Mesh3d(mesh_handle),
|
||||
MeshMaterial3d(material.clone()),
|
||||
transform,
|
||||
));
|
||||
});
|
||||
spawned = true;
|
||||
}
|
||||
}
|
||||
|
||||
if !spawned {
|
||||
warn!("FBX thumbnail: no renderable meshes in {catalog_path}");
|
||||
}
|
||||
spawned
|
||||
}
|
||||
}
|
||||
|
||||
fn build_mesh(ufbx_mesh: &ufbx::Mesh, indices: &[u32]) -> Mesh {
|
||||
let mut mesh = Mesh::new(
|
||||
PrimitiveTopology::TriangleList,
|
||||
RenderAssetUsages::default(),
|
||||
);
|
||||
|
||||
let positions: Vec<[f32; 3]> = ufbx_mesh
|
||||
.vertex_position
|
||||
.values
|
||||
.as_ref()
|
||||
.iter()
|
||||
.map(|v| [v.x as f32, v.y as f32, v.z as f32])
|
||||
.collect();
|
||||
mesh.insert_attribute(Mesh::ATTRIBUTE_POSITION, positions);
|
||||
|
||||
if ufbx_mesh.vertex_normal.exists {
|
||||
let normals: Vec<[f32; 3]> = (0..ufbx_mesh.num_vertices)
|
||||
.map(|i| {
|
||||
let n = ufbx_mesh.vertex_normal[i];
|
||||
[n.x as f32, n.y as f32, n.z as f32]
|
||||
})
|
||||
.collect();
|
||||
mesh.insert_attribute(Mesh::ATTRIBUTE_NORMAL, normals);
|
||||
}
|
||||
|
||||
if ufbx_mesh.vertex_uv.exists {
|
||||
let uvs: Vec<[f32; 2]> = (0..ufbx_mesh.num_vertices)
|
||||
.map(|i| {
|
||||
let uv = ufbx_mesh.vertex_uv[i];
|
||||
[uv.x as f32, uv.y as f32]
|
||||
})
|
||||
.collect();
|
||||
mesh.insert_attribute(Mesh::ATTRIBUTE_UV_0, uvs);
|
||||
}
|
||||
|
||||
mesh.insert_indices(Indices::U32(indices.to_vec()));
|
||||
mesh
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
//! glTF/GLB fast-path texture extraction for asset browser thumbnails.
|
||||
|
||||
use std::path::Path;
|
||||
|
||||
/// Returns an asset-server path for the first base-color texture referenced by a glTF/GLB file.
|
||||
///
|
||||
/// Embedded GLB buffers are skipped so callers can fall back to the render studio.
|
||||
pub fn gltf_base_color_texture_path(model_asset_path: &str) -> Option<String> {
|
||||
let model_path = Path::new(model_asset_path);
|
||||
if !is_gltf_path(model_path) {
|
||||
return None;
|
||||
}
|
||||
|
||||
let (document, ..) = gltf::import(model_path).ok()?;
|
||||
for material in document.materials() {
|
||||
let Some(tex_info) = material.pbr_metallic_roughness().base_color_texture() else {
|
||||
continue;
|
||||
};
|
||||
let texture = document.textures().nth(tex_info.texture().index())?;
|
||||
let image = document.images().nth(texture.source().index())?;
|
||||
let uri = match image.source() {
|
||||
gltf::image::Source::Uri { uri, .. } => uri,
|
||||
gltf::image::Source::View { .. } => continue,
|
||||
};
|
||||
if let Some(path) = resolve_texture_uri(model_path, uri) {
|
||||
return Some(path);
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
pub struct GltfThumbnailSource;
|
||||
|
||||
fn is_gltf_path(path: &Path) -> bool {
|
||||
path.extension()
|
||||
.and_then(|ext| ext.to_str())
|
||||
.is_some_and(|ext| matches!(ext.to_ascii_lowercase().as_str(), "gltf" | "glb"))
|
||||
}
|
||||
|
||||
fn resolve_texture_uri(model_path: &Path, uri: &str) -> Option<String> {
|
||||
if uri.starts_with("data:") {
|
||||
return None;
|
||||
}
|
||||
let model_dir = model_path.parent()?;
|
||||
let texture_path = model_dir.join(uri);
|
||||
normalize_asset_path(&texture_path)
|
||||
}
|
||||
|
||||
fn normalize_asset_path(path: &Path) -> Option<String> {
|
||||
let raw = path.to_string_lossy().replace('\\', "/");
|
||||
if raw.starts_with("assets/") {
|
||||
return Some(raw);
|
||||
}
|
||||
let file_name = path.file_name()?.to_str()?;
|
||||
if file_name.is_empty() {
|
||||
return None;
|
||||
}
|
||||
Some(format!("assets/{file_name}"))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::path::PathBuf;
|
||||
|
||||
#[test]
|
||||
fn normalize_asset_path_keeps_assets_prefix() {
|
||||
assert_eq!(
|
||||
normalize_asset_path(Path::new("assets/textures/albedo.png")),
|
||||
Some("assets/textures/albedo.png".into())
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn resolve_texture_uri_relative_to_model() {
|
||||
let model = PathBuf::from("assets/models/chair.gltf");
|
||||
assert_eq!(
|
||||
resolve_texture_uri(&model, "textures/chair_albedo.png"),
|
||||
Some("assets/models/textures/chair_albedo.png".into())
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
//! Model file sources for the thumbnail render studio.
|
||||
|
||||
mod fbx;
|
||||
pub(crate) mod gltf;
|
||||
|
||||
pub use fbx::FbxThumbnailSource;
|
||||
pub use gltf::GltfThumbnailSource;
|
||||
|
||||
use bevy::prelude::*;
|
||||
|
||||
/// Spawns untextured or scene-root preview content under `root` for the thumbnail studio.
|
||||
pub trait ThumbnailModelSource: Send + Sync {
|
||||
fn spawn_preview(
|
||||
&self,
|
||||
commands: &mut Commands,
|
||||
meshes: &mut Assets<Mesh>,
|
||||
materials: &mut Assets<StandardMaterial>,
|
||||
root: Entity,
|
||||
catalog_path: &str,
|
||||
) -> bool;
|
||||
}
|
||||
|
||||
pub fn source_for_extension(path: &str) -> Option<&'static dyn ThumbnailModelSource> {
|
||||
if path.ends_with(".fbx") {
|
||||
Some(&FbxThumbnailSource)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
pub fn uses_scene_root(path: &str) -> bool {
|
||||
!path.ends_with(".fbx")
|
||||
}
|
||||
Reference in new issue
Block a user