fix(formats): preserve multiple COLLADA node cameras and optimize glTF material lookup (#715)

* fix(formats): preserve multiple COLLADA node cameras and avoid O(N) glTF material lookup

* perf(formats): index high-poly base colors by material name
This commit is contained in:
Dylan M. Taylor
2026-11-20 14:31:52 -05:00
committed by GitHub
parent 2bbd2625bf
commit cf37994744
6 changed files with 81 additions and 30 deletions
Generated
+48 -18
View File
@@ -729,6 +729,15 @@ dependencies = [
"crossbeam-utils",
]
[[package]]
name = "constant_time_eq"
version = "0.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9eb6feb175df65d0a3ccd3e73484e940d0ac664e8034f0519fd49a93db3071d9"
dependencies = [
"rustversion",
]
[[package]]
name = "core-foundation"
version = "0.9.4"
@@ -933,10 +942,10 @@ dependencies = [
[[package]]
name = "craft-mesh"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=3dff4793f1a5bd1f99a4fe3280d48f515158dff3#3dff4793f1a5bd1f99a4fe3280d48f515158dff3"
source = "git+https://github.com/storytold/craft-libs?rev=a20dfd1623709b0bd9830106d3a92a1a443902e3#a20dfd1623709b0bd9830106d3a92a1a443902e3"
dependencies = [
"craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=3dff4793f1a5bd1f99a4fe3280d48f515158dff3)",
"craft-zip 0.1.0 (git+https://github.com/storytold/craft-libs?rev=3dff4793f1a5bd1f99a4fe3280d48f515158dff3)",
"craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=a20dfd1623709b0bd9830106d3a92a1a443902e3)",
"craft-zip 0.1.0 (git+https://github.com/storytold/craft-libs?rev=a20dfd1623709b0bd9830106d3a92a1a443902e3)",
"miniz_oxide 0.9.1",
"serde_json",
]
@@ -1077,23 +1086,13 @@ dependencies = [
[[package]]
name = "craft-xml"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=3dff4793f1a5bd1f99a4fe3280d48f515158dff3#3dff4793f1a5bd1f99a4fe3280d48f515158dff3"
source = "git+https://github.com/storytold/craft-libs?rev=a20dfd1623709b0bd9830106d3a92a1a443902e3#a20dfd1623709b0bd9830106d3a92a1a443902e3"
dependencies = [
"quick-xml 0.41.0",
"serde",
"thiserror 2.0.21",
]
[[package]]
name = "craft-zip"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=3dff4793f1a5bd1f99a4fe3280d48f515158dff3#3dff4793f1a5bd1f99a4fe3280d48f515158dff3"
dependencies = [
"flate2",
"thiserror 2.0.21",
"zip",
]
[[package]]
name = "craft-zip"
version = "0.1.0"
@@ -1114,6 +1113,16 @@ dependencies = [
"zip",
]
[[package]]
name = "craft-zip"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=a20dfd1623709b0bd9830106d3a92a1a443902e3#a20dfd1623709b0bd9830106d3a92a1a443902e3"
dependencies = [
"flate2",
"thiserror 2.0.21",
"zip",
]
[[package]]
name = "craft-zip"
version = "0.1.0"
@@ -1906,8 +1915,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "300e883d756b2e4ec94e02791f39b04b522276138852cfc41d9fb7e904106099"
dependencies = [
"cfg-if",
"js-sys",
"libc",
"r-efi 6.0.0",
"wasm-bindgen",
]
[[package]]
@@ -5086,9 +5097,9 @@ dependencies = [
[[package]]
name = "typed-path"
version = "0.12.4"
version = "0.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "54ec2755506471f54baf5e4bb5499d8e510cc1caf271270e778a736bd2d7336c"
checksum = "9ce898823d8cd0f028662d2f349694b1af0a5cc9be83f1d213a00d3147445e1e"
[[package]]
name = "typeid"
@@ -6390,6 +6401,20 @@ name = "zeroize"
version = "1.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e13084392c5e4bc371903e2935a5eaeed24905a7511356b883835e18a78f6879"
dependencies = [
"zeroize_derive",
]
[[package]]
name = "zeroize_derive"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3c50655cbb0fe3fc43170059e702f1ce5e19b84cec58dc87b037a09935c2f328"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.119",
]
[[package]]
name = "zerotrie"
@@ -6430,16 +6455,21 @@ dependencies = [
[[package]]
name = "zip"
version = "9.0.5"
version = "9.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6e04976da629d3b04204ba8105b396a193af8ad2583b2c79bfb3ab8af580a11e"
checksum = "a9534624d547e3f516ecde0ed9a70d37788f01d340efcfdd03bf3ea8e163c1d0"
dependencies = [
"constant_time_eq",
"crc32fast",
"flate2",
"getrandom 0.4.3",
"indexmap",
"libc",
"memchr",
"nix",
"typed-path",
"windows-sys 0.60.2",
"zeroize",
"zopfli",
]
+1 -1
View File
@@ -75,7 +75,7 @@ craft-tcraft = { git = "https://github.com/storytold/craft-libs", rev = "c376774
# Isolated aligned writer; existing archive readers retain their pin and behavior.
craft-zip-usdz = { package = "craft-zip", git = "https://github.com/storytold/craft-libs", rev = "5631e99fe126cffb74c673eb458db04299f2bb9b" }
craft-zip = { git = "https://github.com/storytold/craft-libs", rev = "c376774f15ae7cd3b06a7ac664f01a8645af4b83" }
craft-mesh = { git = "https://github.com/storytold/craft-libs", rev = "3dff4793f1a5bd1f99a4fe3280d48f515158dff3", default-features = false, features = ["stl", "ply", "fbx", "obj", "gltf", "dae", "abc"] }
craft-mesh = { git = "https://github.com/storytold/craft-libs", rev = "a20dfd1623709b0bd9830106d3a92a1a443902e3", default-features = false, features = ["stl", "ply", "fbx", "obj", "gltf", "dae", "abc"] }
craft-mcmat = { git = "https://github.com/storytold/craft-libs", rev = "7a7a18cb40ac4cd5fca29c0dc224ac5178d40a89" }
# Who draws the window frame, and the app-drawn strip as the title bar (crates/ui-egui/src/titlebar.rs).
craft-window = { git = "https://github.com/storytold/craft-libs", rev = "390fe98b0dd3fb84db7c3af7cbd958db4e8cc585", features = ["egui"] }
+9 -8
View File
@@ -36,14 +36,15 @@ pub(crate) fn read(bytes: &[u8]) -> Result<RawMesh> {
fn read_inner(bytes: &[u8]) -> Result<RawMesh> {
let lim = limits();
let doc = craft_mesh::dae::read_document(bytes, &lim).map_err(convert)?;
let cameras_doc = craft_mesh::dae::read_document_with_cameras(bytes, &lim).map_err(convert)?;
let doc = cameras_doc.document();
let to_mat = |m: craft_mesh::Mat4| Mat4::from_cols_array(&m.map(|v| v as f32));
let global = to_mat(doc.global_transform());
let scene = doc.scene;
let scene = &doc.scene;
let mut sink = PolygonSink::default();
for w in scene.warnings {
sink.warn(w);
for w in &scene.warnings {
sink.warn(w.clone());
}
let mut cameras = Vec::new();
let mut stack: Vec<(usize, Mat4, usize)> = scene.roots.iter().rev().map(|&r| (r, global, 0)).collect();
@@ -56,10 +57,10 @@ fn read_inner(bytes: &[u8]) -> Result<RawMesh> {
let node = scene.nodes.get(id).ok_or_else(|| malformed(KIND, "node index out of range"))?;
let world = parent * to_mat(node.transform);
if let Some(cam_idx) = node.camera
&& cameras.len() < super::MAX_CAMERAS
&& let Some(cam) = scene.cameras.get(cam_idx)
{
for cam in cameras_doc.cameras_for_node(id) {
if cameras.len() >= super::MAX_CAMERAS {
break;
}
let (fov, ortho) = match cam.projection {
craft_mesh::Projection::Perspective { yfov, .. } => (yfov.to_degrees() as f32, None),
craft_mesh::Projection::Orthographic { ymag, .. } => (40.0, Some(ymag as f32)),
+3 -2
View File
@@ -1,6 +1,6 @@
//! glTF 2.0 / GLB: the default scene's node tree, flattened to world space.
use std::collections::HashMap;
use std::collections::{HashMap, HashSet};
use texturecraft_geom::glam::{Mat3, Mat4, Vec2, Vec3, Vec4};
@@ -107,6 +107,7 @@ struct Attrs {
/// White where a primitive has no COLOR_0.
colors: Vec<Vec4>,
color_seen: bool,
seen_materials: HashSet<String>,
}
fn add_part(
@@ -133,7 +134,7 @@ fn add_part(
Some(i) => scene.materials.get(i).filter(|m| !m.name.is_empty()).map(|m| m.name.clone()).unwrap_or_else(|| format!("Material_{i}")),
};
if let Some(i) = part.material
&& !raw.base_colors.iter().any(|c| c.material == mat_name)
&& attrs.seen_materials.insert(mat_name.clone())
&& let Some(mat) = scene.materials.get(i)
{
base_color(raw, scene, mat, &mat_name);
+5 -1
View File
@@ -335,11 +335,15 @@ pub fn import_high_poly_bytes(file_name: &str, bytes: &[u8]) -> Result<HighPolyM
mesh.tangents = vec![Vec4::new(1.0, 0.0, 0.0, 1.0); n];
mesh.validate()?;
let mut budget = BASE_COLOR_TEXELS;
let mut color_by_material: HashMap<&str, &RawBaseColor> = HashMap::with_capacity(colors.len());
for color in &colors {
color_by_material.entry(color.material.as_str()).or_insert(color);
}
let base_color = mesh
.materials
.iter()
.map(|m| {
let c = colors.iter().find(|c| c.material == *m)?;
let c = color_by_material.get(m.as_str()).copied()?;
let (width, height, texels) = match c.image.as_deref().map(|b| decode_base_color(b, &mut budget)) {
Some(Ok(t)) => t,
Some(Err(e)) => {
@@ -851,6 +851,21 @@ fn dae_empty_material_binding_stays_distinct_from_named_default() {
assert_eq!(material_for("other"), Some("Default"));
}
#[test]
fn dae_preserves_multiple_cameras_on_one_node() {
let dae = DAE
.replace(
r#"<camera id="cam" name="Overview"><optics><technique_common><perspective><yfov>30</yfov></perspective></technique_common></optics></camera>"#,
r#"<camera id="cam" name="Overview"><optics><technique_common><perspective><yfov>30</yfov></perspective></technique_common></optics></camera>
<camera id="cam2" name="Detail"><optics><technique_common><orthographic><ymag>2</ymag></orthographic></technique_common></optics></camera>"#,
)
.replace(r##"<instance_camera url="#cam"/>"##, r##"<instance_camera url="#cam"/><instance_camera url="#cam2"/>"##);
let r = import_mesh_bytes("scene.dae", dae.as_bytes()).unwrap();
let names: Vec<&str> = r.cameras.iter().map(|c| c.name.as_str()).collect();
assert_eq!(names, ["Overview", "Detail"]);
assert!(!r.cameras[0].ortho && r.cameras[1].ortho);
}
#[test]
fn glb_export_round_trips_with_textures() {
let mesh = texturecraft_geom::primitives::material_ball();