mirror of
https://github.com/storytold/texturecraft.git
synced 2026-11-21 06:50:43 +01:00
Preserve invalid ASCII FBX array values
This commit is contained in:
1 parent
277bbf3cf1
commit
6f0ec1189e
1 file changed
+85
-2
@@ -130,14 +130,15 @@ impl Node {
|
||||
if let Some(a) = self.child("a") {
|
||||
return a.arr();
|
||||
}
|
||||
Cow::Owned(self.props.iter().filter_map(Prop::num).collect())
|
||||
Cow::Owned(self.props.iter().map(|p| p.num().unwrap_or(f64::NAN)).collect())
|
||||
}
|
||||
|
||||
/// The numbers of entry `name` in Properties70 (`P: name, type, subtype, flags, values`) or
|
||||
/// Properties60 (`Property: name, type, flags, values`).
|
||||
fn property(&self, name: &str) -> Option<Vec<f64>> {
|
||||
let list = self.child("Properties70").or_else(|| self.child("Properties60"))?;
|
||||
list.children.iter().find(|p| p.str_at(0) == name).map(|p| p.props.iter().filter_map(Prop::num).collect())
|
||||
let first_value = if list.name == "Properties70" { 4 } else { 3 };
|
||||
list.children.iter().find(|p| p.str_at(0) == name).map(|p| p.props.iter().skip(first_value).map(|v| v.num().unwrap_or(f64::NAN)).collect())
|
||||
}
|
||||
|
||||
fn vec3(&self, name: &str, default: Vec3) -> Vec3 {
|
||||
@@ -797,6 +798,14 @@ fn add_geometry(sink: &mut PolygonSink, geometry: &Node, transform: Mat4, slots:
|
||||
let normals = Layer::read(geometry, "LayerElementNormal", "Normals", "NormalsIndex");
|
||||
let uvs = Layer::read(geometry, "LayerElementUV", "UV", "UVIndex");
|
||||
let materials = Layer::read(geometry, "LayerElementMaterial", "Materials", "");
|
||||
if vertices.iter().chain(polygon_indices.iter()).any(|v| !v.is_finite())
|
||||
|| [&normals, &uvs, &materials]
|
||||
.into_iter()
|
||||
.flatten()
|
||||
.any(|layer| layer.data.iter().any(|v| !v.is_finite()) || layer.index.as_ref().is_some_and(|index| index.iter().any(|v| !v.is_finite())))
|
||||
{
|
||||
return Err(malformed(KIND, "mesh array contains a non-numeric or non-finite value"));
|
||||
}
|
||||
let normal_transform = Mat3::from_mat4(transform).inverse().transpose();
|
||||
let flip = transform.determinant() < 0.0;
|
||||
let (mut corners, mut polygon_index, mut bad_normals, mut bad_uvs) = (Vec::new(), 0usize, 0usize, 0usize);
|
||||
@@ -850,6 +859,80 @@ fn add_geometry(sink: &mut PolygonSink, geometry: &Node, transform: Mat4, slots:
|
||||
mod instance_budget_tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn ascii_array_keeps_invalid_values_in_place() {
|
||||
let (nodes, _) = parse_ascii(
|
||||
br#"FBXHeaderExtension: { FBXVersion: 7400 }
|
||||
Vertices: 1, 1.#INF, 3, "x", 5
|
||||
"#,
|
||||
)
|
||||
.unwrap();
|
||||
let values = nodes[1].arr();
|
||||
assert_eq!(values.len(), 5);
|
||||
assert!(values[1].is_nan() && values[3].is_nan());
|
||||
assert_eq!((values[0], values[2], values[4]), (1.0, 3.0, 5.0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ascii_bad_vertex_is_rejected() {
|
||||
let (nodes, _) = parse_ascii(
|
||||
br#"FBXHeaderExtension: { FBXVersion: 7400 }
|
||||
Geometry: { Vertices: 0, 0, 0, 1, -1.#IND, 0, 0, 1, 0
|
||||
PolygonVertexIndex: 0, 1, -3 }
|
||||
"#,
|
||||
)
|
||||
.unwrap();
|
||||
let mut remaining = usize::MAX;
|
||||
let result = add_geometry(&mut PolygonSink::default(), &nodes[1], Mat4::IDENTITY, &[], &mut remaining);
|
||||
assert!(result.is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ascii_bad_index_and_uv_are_rejected() {
|
||||
for payload in [
|
||||
"PolygonVertexIndex: 0, oops, -3",
|
||||
"PolygonVertexIndex: 0, 1, -3\nLayerElementUV: 0 { MappingInformationType: \"ByPolygonVertex\"\nReferenceInformationType: \"Direct\"\nUV: 0, 0, oops, 1, 1, 0 }",
|
||||
] {
|
||||
let source = format!("FBXHeaderExtension: {{ FBXVersion: 7400 }}\nGeometry: {{ Vertices: 0, 0, 0, 1, 0, 0, 0, 1, 0\n{payload} }}");
|
||||
let (nodes, _) = parse_ascii(source.as_bytes()).unwrap();
|
||||
let mut remaining = usize::MAX;
|
||||
assert!(add_geometry(&mut PolygonSink::default(), &nodes[1], Mat4::IDENTITY, &[], &mut remaining).is_err());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ascii_transform_metadata_does_not_shift_values() {
|
||||
let (nodes, _) = parse_ascii(
|
||||
br#"FBXHeaderExtension: { FBXVersion: 7400 }
|
||||
Model: { Properties70: { P: "Lcl Translation", "Lcl Translation", "", "A", 1, 2, 3 } }
|
||||
"#,
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(nodes[1].property("Lcl Translation"), Some(vec![1.0, 2.0, 3.0]));
|
||||
let (nodes, _) = parse_ascii(
|
||||
br#"FBXHeaderExtension: { FBXVersion: 7400 }
|
||||
Model: { Properties60: { Property: "Lcl Translation", "Lcl Translation", "A", 1, 2, 3 } }
|
||||
"#,
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(nodes[1].property("Lcl Translation"), Some(vec![1.0, 2.0, 3.0]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn malformed_transform_value_keeps_its_position_in_both_property_formats() {
|
||||
for (list, entry) in [
|
||||
("Properties70", "P: \"Lcl Translation\", \"Lcl Translation\", \"\", \"A\", 1, oops, 3"),
|
||||
("Properties60", "Property: \"Lcl Translation\", \"Lcl Translation\", \"A\", 1, oops, 3"),
|
||||
] {
|
||||
let source = format!("FBXHeaderExtension: {{ FBXVersion: 7400 }}\nModel: {{ {list}: {{ {entry} }} }}");
|
||||
let (nodes, _) = parse_ascii(source.as_bytes()).unwrap();
|
||||
let values = nodes[1].property("Lcl Translation").unwrap();
|
||||
assert_eq!(values.len(), 3);
|
||||
assert_eq!((values[0], values[2]), (1.0, 3.0));
|
||||
assert!(values[1].is_nan());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn instance_points_are_charged_before_transforming_each_copy() {
|
||||
let geometry = Node {
|
||||
|
||||
Reference in new issue
Block a user