This commit is contained in:
commit
f29712d158
259 files changed
+52069
No files matched your search
@@ -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())
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user