Files
effectcraft/crates/model/src/gltf.rs
T
Dylan M. Taylor 63fda85a51 3D models: bound and share decoded textures, share skin matrices, index OBJ materials
* fix(model): share decoded textures and skin matrices, bound texture pixels, index OBJ materials

Texture::decode expanded any image to float RGBA without a size check. Repeated mtllib lines
reparsed the library and decoded its textures again; glTF textures with a non-default sampler
each copied their image's pixels; every instance of a skin built its own joint matrices. OBJ
usemtl and faces scanned the growing material and primitive lists, and contour grouping kept
every ancestor of every contour.

* address review: keep largest contours and dedupe mtllib by set

* address review: contour depth counts containers, glTF wrap copies and FBX textures share the import pixel budget

* address review: holes go to the innermost outline; glTF wrap variants keyed by decoded image

* address review: create every outline slot before attaching holes

* Import: a model's left-out parts reach the user as an import warning (Model::warnings, Importer::probe_noting)

* Models: left-out textures are import warnings; the import texture budget is 2^29 pixels

* 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 move to #2955 (it reworks the same code)
2026-11-12 07:00:15 -05:00

27 lines
1.3 KiB
Rust

//! glTF 2.0 import (`.gltf` with external or embedded buffers, `.glb`), read with `craft-mesh`
//! (`craft_mesh::gltf`): meshes, PBR metallic-roughness materials, the node hierarchy, skins,
//! animations, cameras and `KHR_lights_punctual` lights. External buffers and images are read
//! through the caller's resolver by their percent-decoded URI. Primitives without normals get
//! flat normals (glTF 2.0 §3.7.2.1); normal-mapped ones get tangents from their UVs.
use crate::scene::{MissingNormals, error, limits, to_model};
use crate::{Model, Result};
pub use craft_mesh::base64_decode;
pub use craft_mesh::gltf::{split_glb, uri_decode};
/// Parse a `.gltf` (JSON) or `.glb` file; `resolve(uri)` reads external buffers and images.
pub fn parse(bytes: &[u8], resolve: &dyn Fn(&str) -> Option<Vec<u8>>) -> Result<Model> {
let mut buffers = |uri: &str| resolve(uri);
let scene = craft_mesh::gltf::read(bytes, &limits(), &mut buffers).map_err(error)?;
to_model(scene, MissingNormals::Flat, &|im| {
let uri = im.uri.as_deref()?;
match uri.strip_prefix("data:") {
Some(_) => None,
// craft-mesh decodes external image URIs while constructing the scene. Decode only
// once here; a second pass changes literal percent escapes in valid filenames.
None => resolve(uri),
}
})
}