mirror of
https://github.com/storytold/texturecraft.git
synced 2026-11-28 09:09:25 +01:00
* Import glTF scenes that have no nodes array (#438) The gltf crate requires scene.nodes, but the glTF 2.0 schema lists no required properties for a scene. Blender writes such scenes for scenes with no exported objects, so a file whose default scene was fine failed with 'missing field nodes'. Treat a missing array as empty before the document is built. * Test GLB import with an empty scene --------- Co-authored-by: FredApps <FredApps@users.noreply.github.com>
This commit is contained in:
1 parent
73c9f8f705
commit
277bbf3cf1
2 files changed
+69
-1
No files matched your search
@@ -20,8 +20,34 @@ pub(crate) fn read(bytes: &[u8]) -> Result<RawMesh> {
|
||||
crate::guard(KIND, || read_inner(bytes))
|
||||
}
|
||||
|
||||
/// Loads and validates the document like `gltf::Gltf::from_slice`, except that a scene without a
|
||||
/// `nodes` array is an empty scene. The `gltf` crate declares the field as required, but the
|
||||
/// glTF 2.0 schema lists no required properties for a scene, and exporters write such scenes
|
||||
/// for scenes that contain no objects.
|
||||
fn parse(bytes: &[u8]) -> Result<(gltf::Document, Option<Vec<u8>>)> {
|
||||
let (json, blob) = if bytes.starts_with(b"glTF") {
|
||||
let mut glb = gltf::Glb::from_slice(bytes).map_err(|e| malformed(KIND, e))?;
|
||||
let blob = glb.bin.take().map(std::borrow::Cow::into_owned);
|
||||
(glb.json.into_owned(), blob)
|
||||
} else {
|
||||
(bytes.to_vec(), None)
|
||||
};
|
||||
let mut value: serde_json::Value = serde_json::from_slice(&json).map_err(|e| malformed(KIND, e))?;
|
||||
if let Some(scenes) = value.get_mut("scenes").and_then(serde_json::Value::as_array_mut) {
|
||||
for scene in scenes {
|
||||
if let Some(scene) = scene.as_object_mut() {
|
||||
scene.entry("nodes").or_insert_with(|| serde_json::Value::Array(Vec::new()));
|
||||
}
|
||||
}
|
||||
}
|
||||
let json = serde_json::to_vec(&value).map_err(|e| malformed(KIND, e))?;
|
||||
let root = gltf::json::Root::from_slice(&json).map_err(|e| malformed(KIND, e))?;
|
||||
let document = gltf::Document::from_json(root).map_err(|e| malformed(KIND, e))?;
|
||||
Ok((document, blob))
|
||||
}
|
||||
|
||||
fn read_inner(bytes: &[u8]) -> Result<RawMesh> {
|
||||
let gltf::Gltf { document, blob } = gltf::Gltf::from_slice(bytes).map_err(|e| malformed(KIND, e))?;
|
||||
let (document, blob) = parse(bytes)?;
|
||||
check_buffer_uris(&document)?;
|
||||
let buffers = gltf::import_buffers(&document, None, blob).map_err(|e| match e {
|
||||
gltf::Error::ExternalReferenceInSliceImport => FormatError::Unsupported(
|
||||
|
||||
@@ -118,6 +118,48 @@ fn glb_two_materials_mirrored_node() {
|
||||
m.validate().unwrap();
|
||||
}
|
||||
|
||||
/// A scene without a `nodes` array is legal glTF (Blender writes one for a scene with no exported
|
||||
/// objects); it must not stop the default scene from importing.
|
||||
#[test]
|
||||
fn gltf_scene_without_nodes_imports_the_default_scene() {
|
||||
let mut json: serde_json::Value = serde_json::from_slice(&embedded_gltf()).unwrap();
|
||||
json["scenes"] = serde_json::json!([{ "name": "EmptyReviewScene" }, { "nodes": [0] }]);
|
||||
json["scene"] = 1.into();
|
||||
let im = import_mesh_bytes("scene.gltf", &serde_json::to_vec(&json).unwrap()).unwrap();
|
||||
assert_eq!(im.report.triangles, 2);
|
||||
// Only an empty scene: nothing to import, but a clear error rather than a parse failure.
|
||||
json["scenes"] = serde_json::json!([{ "name": "EmptyReviewScene" }]);
|
||||
json["scene"] = 0.into();
|
||||
let err = import_mesh_bytes("scene.gltf", &serde_json::to_vec(&json).unwrap()).unwrap_err();
|
||||
assert!(!err.to_string().contains("missing field"), "{err}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn glb_scene_without_nodes_keeps_binary_mesh_in_default_scene() {
|
||||
let original = glb(true, true);
|
||||
let jlen = u32::from_le_bytes(original[12..16].try_into().unwrap()) as usize;
|
||||
let mut json: serde_json::Value = serde_json::from_slice(&original[20..20 + jlen]).unwrap();
|
||||
json["scenes"] = serde_json::json!([{ "name": "EmptyReviewScene" }, { "nodes": [0] }]);
|
||||
json["scene"] = 1.into();
|
||||
let mut js = serde_json::to_vec(&json).unwrap();
|
||||
while !js.len().is_multiple_of(4) {
|
||||
js.push(b' ');
|
||||
}
|
||||
let bin_chunk = &original[20 + jlen..];
|
||||
let mut glb = Vec::new();
|
||||
glb.extend(b"glTF");
|
||||
glb.extend(2u32.to_le_bytes());
|
||||
glb.extend(((20 + js.len() + bin_chunk.len()) as u32).to_le_bytes());
|
||||
glb.extend((js.len() as u32).to_le_bytes());
|
||||
glb.extend(b"JSON");
|
||||
glb.extend(js);
|
||||
glb.extend(bin_chunk);
|
||||
let im = import_mesh_bytes("scene.glb", &glb).unwrap();
|
||||
assert_eq!(im.report.triangles, 2);
|
||||
assert_eq!(im.mesh.materials, ["Red", "Material_1"]);
|
||||
assert_outward(&im.mesh);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gltf_embedded_buffers_and_missing_attributes() {
|
||||
let im = import_mesh_bytes("scene.gltf", &embedded_gltf()).unwrap();
|
||||
|
||||
Reference in new issue
Block a user