Merge pull request #527 from storytold/codex/shared-fbx-consumer

Use shared FBX scene and package readers
This commit is contained in:
FredApps authored and GitHub committed 2026-11-16 10:25:43 +01:00
commit 893d6fbb1b
8 files changed
+252 -935

No files matched your search

Generated
+18 -4
View File
@@ -829,7 +829,7 @@ name = "craft-mcmat"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=c376774f15ae7cd3b06a7ac664f01a8645af4b83#c376774f15ae7cd3b06a7ac664f01a8645af4b83"
dependencies = [
"craft-zip",
"craft-zip 0.1.0 (git+https://github.com/storytold/craft-libs?rev=c376774f15ae7cd3b06a7ac664f01a8645af4b83)",
"serde",
"serde_json",
"thiserror 2.0.21",
@@ -851,7 +851,11 @@ dependencies = [
[[package]]
name = "craft-mesh"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=d6ed8c20f12188c1f39c89b3a8436bd0d09c3748#d6ed8c20f12188c1f39c89b3a8436bd0d09c3748"
source = "git+https://github.com/storytold/craft-libs?rev=4f9c718985182ae8e26fc1bdf3adce81161b99f9#4f9c718985182ae8e26fc1bdf3adce81161b99f9"
dependencies = [
"craft-zip 0.1.0 (git+https://github.com/storytold/craft-libs?rev=4f9c718985182ae8e26fc1bdf3adce81161b99f9)",
"miniz_oxide 0.9.1",
]
[[package]]
name = "craft-quit"
@@ -869,7 +873,7 @@ name = "craft-tcraft"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=c376774f15ae7cd3b06a7ac664f01a8645af4b83#c376774f15ae7cd3b06a7ac664f01a8645af4b83"
dependencies = [
"craft-zip",
"craft-zip 0.1.0 (git+https://github.com/storytold/craft-libs?rev=c376774f15ae7cd3b06a7ac664f01a8645af4b83)",
"serde",
"serde_json",
"thiserror 2.0.21",
@@ -953,6 +957,16 @@ dependencies = [
"thiserror 2.0.21",
]
[[package]]
name = "craft-zip"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=4f9c718985182ae8e26fc1bdf3adce81161b99f9#4f9c718985182ae8e26fc1bdf3adce81161b99f9"
dependencies = [
"flate2",
"thiserror 2.0.21",
"zip",
]
[[package]]
name = "craft-zip"
version = "0.1.0"
@@ -4459,7 +4473,7 @@ dependencies = [
"craft-mesh",
"craft-tcraft",
"craft-usd",
"craft-zip",
"craft-zip 0.1.0 (git+https://github.com/storytold/craft-libs?rev=c376774f15ae7cd3b06a7ac664f01a8645af4b83)",
"exr",
"flate2",
"gltf",
+1 -1
View File
@@ -72,7 +72,7 @@ craft-ui = { git = "https://github.com/storytold/craft-libs", rev = "390fe98b0dd
craft-materialx = { git = "https://github.com/storytold/craft-libs", rev = "390fe98b0dd3fb84db7c3af7cbd958db4e8cc585" }
craft-tcraft = { git = "https://github.com/storytold/craft-libs", rev = "c376774f15ae7cd3b06a7ac664f01a8645af4b83" }
craft-zip = { git = "https://github.com/storytold/craft-libs", rev = "c376774f15ae7cd3b06a7ac664f01a8645af4b83" }
craft-mesh = { git = "https://github.com/storytold/craft-libs", rev = "d6ed8c20f12188c1f39c89b3a8436bd0d09c3748", default-features = false, features = ["stl", "ply"] }
craft-mesh = { git = "https://github.com/storytold/craft-libs", rev = "4f9c718985182ae8e26fc1bdf3adce81161b99f9", default-features = false, features = ["stl", "ply", "fbx"] }
craft-mcmat = { git = "https://github.com/storytold/craft-libs", rev = "c376774f15ae7cd3b06a7ac664f01a8645af4b83" }
# 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"] }
+1 -1
View File
@@ -554,7 +554,7 @@ impl Walk<'_, '_> {
Vec2::ZERO
}
});
corners.push(Corner { p: *p, n, uv });
corners.push(Corner { p: *p, n, uv, color: None });
}
k = end;
if !keep_order {
+1
View File
@@ -356,6 +356,7 @@ fn add_geometry(d: &mut Doc, sink: &mut PolygonSink, geom: &El, m: Mat4, bind: &
p: m.transform_point3(Vec3::from(pos)),
n: n.map(|n| nm * Vec3::from(n)),
uv: t.map(|[u, v]| Vec2::new(u, 1.0 - v)),
color: None,
});
}
next = next.saturating_add(size);
File diff suppressed because it is too large. Load diff
+22 -3
View File
@@ -2,7 +2,7 @@
//! world-space [`Mesh`] with one submesh per material and part (glTF node, OBJ object, FBX
//! model, COLLADA node, USD Mesh prim, Alembic PolyMesh; unnamed parts take the file stem), materials contiguous,
//! glTF UV convention (origin top-left, v down),
//! normals (computed when missing), vertex colors (glTF, OBJ, PLY), UVs (auto-unwrapped when
//! normals (computed when missing), vertex colors (glTF, FBX, OBJ, PLY), UVs (auto-unwrapped when
//! missing) and MikkTSpace tangents. glTF, FBX, COLLADA and USD cameras come along as the
//! project's camera list.
@@ -182,10 +182,11 @@ pub(crate) struct Corner {
pub p: Vec3,
pub n: Option<Vec3>,
pub uv: Option<Vec2>,
pub color: Option<Vec4>,
}
/// Collects polygons into a [`RawMesh`]: fan triangulation, one vertex per unique corner, and
/// normals / UVs kept only when every corner has them (else computed / unwrapped).
/// normals / UVs / colors kept only when every corner has them (else computed / unwrapped / white).
#[derive(Default)]
pub(crate) struct PolygonSink {
/// Part name of the polygons that follow.
@@ -193,10 +194,13 @@ pub(crate) struct PolygonSink {
raw: RawMesh,
normals: Vec<Vec3>,
uvs: Vec<Vec2>,
/// Allocated by the first coloured corner, so a scene without colors pays nothing for them.
colors: Option<Vec<Vec4>>,
missing_normals: bool,
missing_uvs: bool,
some_uvs: bool,
ids: HashMap<[u32; 8], u32>,
missing_colors: bool,
ids: HashMap<[u32; 12], u32>,
polygon: Vec<u32>,
triangles: usize,
}
@@ -213,13 +217,24 @@ impl PolygonSink {
self.missing_normals |= c.n.is_none();
self.missing_uvs |= c.uv.is_none();
self.some_uvs |= c.uv.is_some();
self.missing_colors |= c.color.is_none();
let (n, uv) = (c.n.unwrap_or_default(), c.uv.unwrap_or_default());
// A corner without a color must not merge with one that has it.
let color_key = c.color.map_or([u32::MAX; 4], |v| v.to_array().map(f32::to_bits));
let key = [c.p.x, c.p.y, c.p.z, n.x, n.y, n.z, uv.x, uv.y].map(f32::to_bits);
let key = [key[0], key[1], key[2], key[3], key[4], key[5], key[6], key[7], color_key[0], color_key[1], color_key[2], color_key[3]];
let id = match self.ids.entry(key) {
Entry::Occupied(e) => *e.get(),
Entry::Vacant(e) => {
let id = self.raw.positions.len();
check_vertices(id + 1)?;
// White stands in for the corners before the first coloured one.
if c.color.is_some() && self.colors.is_none() {
self.colors = Some(vec![Vec4::ONE; id]);
}
if let Some(colors) = self.colors.as_mut() {
colors.push(c.color.unwrap_or(Vec4::ONE));
}
self.raw.positions.push(c.p);
self.normals.push(n);
self.uvs.push(uv);
@@ -242,8 +257,12 @@ impl PolygonSink {
if self.missing_uvs && self.some_uvs {
self.warn("some polygons have no UVs; the whole mesh was unwrapped automatically".into());
}
if self.missing_colors && self.colors.is_some() {
self.warn("some polygon corners have no color in the color layer; vertex colors were dropped".into());
}
self.raw.normals = (!self.missing_normals).then_some(self.normals);
self.raw.uvs = (!self.missing_uvs).then_some(self.uvs);
self.raw.colors = if self.missing_colors { None } else { self.colors.take() };
self.raw
}
}
+28 -1
View File
@@ -3,7 +3,7 @@
use std::io::Write as _;
use texturecraft_geom::glam::{Vec2, Vec3};
use texturecraft_geom::glam::{Vec2, Vec3, Vec4};
use super::tests::hammer;
use super::*;
@@ -298,6 +298,33 @@ fn fbx_binary_32_and_64_bit_compressed_or_not() {
}
}
/// [`scene`] with a per-polygon-vertex color layer: corner `i` gets `(i/8, 1 - i/8, 0.25, 1)`.
fn colored_scene() -> Vec<N> {
let mut nodes = scene(false);
let objects = nodes.iter_mut().find(|node| node.name == "Objects").unwrap();
let geometry = objects.children.iter_mut().find(|node| node.name == "Geometry").unwrap();
let colors: Vec<f64> = (0..7).flat_map(|i| [i as f64 / 8.0, 1.0 - i as f64 / 8.0, 0.25, 1.0]).collect();
geometry.children.push(layer(
"LayerElementColor",
"ByPolygonVertex",
"IndexToDirect",
vec![n("Colors", vec![P::Doubles(colors)], vec![]), n("ColorIndex", vec![P::Ints((0..7).collect())], vec![])],
));
nodes
}
#[test]
fn fbx_vertex_colors_reach_the_mesh_and_high_polys() {
let nodes = colored_scene();
let expected: Vec<Vec4> = (0..7).map(|i| Vec4::new(i as f32 / 8.0, 1.0 - i as f32 / 8.0, 0.25, 1.0)).collect();
for (label, bytes) in [("binary", binary_fbx(&nodes, 7400, false)), ("ascii", ascii_fbx(&nodes).into_bytes())] {
let imported = import_mesh_bytes("colors.fbx", &bytes).unwrap_or_else(|e| panic!("{label}: {e}"));
assert_eq!(imported.mesh.colors, expected, "{label}: the color layer reaches the mesh");
let high = import_high_poly_bytes("colors.fbx", &bytes).unwrap_or_else(|e| panic!("{label}: {e}"));
assert_eq!(high.mesh.colors, expected, "{label}: and the high poly used for ID bakes");
}
}
/// [`scene`] with a diffuse color on Red and an embedded diffuse texture on Blue.
#[test]
fn fbx_camera_models_are_capped_during_import() {
@@ -576,6 +576,7 @@ impl<'a> Stage<'a> {
p: m.transform_point3(Vec3::from(p)),
n: normals.as_ref().and_then(|pv| pv.get::<3>(at)).map(|v| nm * Vec3::from(v)),
uv: uvs.as_ref().and_then(|pv| pv.get::<2>(at)).map(|[u, v]| Vec2::new(u, 1.0 - v)),
color: None,
});
}
if flip {