mirror of
https://github.com/storytold/effectcraft.git
synced 2026-11-16 15:22:43 +00:00
* fix(model): FBX records stay within their parent, and textures, materials and links are indexed A binary child record could end past its parent: the parent's end was then restored and the child's tail parsed again. A failed array decompression printed the decompressed bytes in the error. Texture objects aliasing one embedded Video or file each decoded and kept their own pixels, outside the import budget. Each polygon scanned the primitive groups for its material, and each object scanned every connection for its children. * address review: FBX textures: key image cache by the source used, charge decoded size before decoding, propagate budget errors * Import: a model's left-out parts reach the user as an import warning (Model::warnings, Importer::probe_noting) * Import: a model's left-out parts reach the user as an import warning * FBX: textures past the limits are left out with an import warning, not a failed import; 2^29-pixel allowance
36 lines
1.7 KiB
Rust
36 lines
1.7 KiB
Rust
//! FBX 6.x and 7.x scenes, binary and ASCII, read with `craft-mesh` (`craft_mesh::fbx`):
|
|
//! `Geometry` meshes, `Model` nodes with the SDK transform and geometric transforms, `Material`
|
|
//! (Lambert/Phong mapped onto metallic-roughness), embedded or relative `Texture` images,
|
|
//! joined by `Connections`. A root node turns the file's `GlobalSettings` axes to +Y up; units
|
|
//! stay the file's own (like OBJ). Meshes without normals get smooth ones.
|
|
|
|
use crate::scene::{MissingNormals, error, limits, mat4, to_model};
|
|
use crate::{Model, ModelError, Node, Result};
|
|
|
|
pub use craft_mesh::fbx::BINARY_MAGIC;
|
|
|
|
/// Parse an FBX file; `resolve(path)` reads texture files the file names but does not embed.
|
|
pub fn parse(bytes: &[u8], resolve: &dyn Fn(&str) -> Option<Vec<u8>>) -> Result<Model> {
|
|
let limits = limits();
|
|
let doc = craft_mesh::fbx::read_document(bytes, &limits).map_err(error)?;
|
|
let mut textures = |path: &str| resolve(path);
|
|
let scene = doc.scene(&limits, &mut textures).map_err(error)?;
|
|
if scene.meshes.is_empty() {
|
|
return Err(ModelError::Parse("FBX file without meshes".into()));
|
|
}
|
|
let mut m = to_model(scene, MissingNormals::Smooth, &|_| None)?;
|
|
// The axes node goes first: every node index moves up by one.
|
|
for n in &mut m.nodes {
|
|
n.children.iter_mut().for_each(|c| *c += 1);
|
|
}
|
|
m.skins.iter_mut().flat_map(|s| s.joints.iter_mut()).for_each(|j| *j += 1);
|
|
m.animations.iter_mut().flat_map(|a| a.channels.iter_mut()).for_each(|c| c.node += 1);
|
|
let roots = m.roots.iter().map(|r| r + 1).collect();
|
|
m.nodes.insert(0, Node { name: "FBX axes".into(), matrix: Some(mat4(&doc.axes_to_y_up())), children: roots, ..Node::default() });
|
|
m.roots = vec![0];
|
|
Ok(m)
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests;
|