mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-16 23:42:27 +00:00
Merge pull request #1777 from storytold/fredapps/i778-fbx-batch
mesh: land the FBX import batch (#1719 #1717 #1703 #1704 #1705 #1701 #1714 #1706 #1711)
This commit is contained in:
5 files changed
+1028
-155
No files matched your search
Generated
+1
@@ -2168,6 +2168,7 @@ version = "0.1.0"
|
||||
dependencies = [
|
||||
"craft-opc",
|
||||
"craft-xml",
|
||||
"craft-zip",
|
||||
"miniz_oxide 0.9.1",
|
||||
"serde_json",
|
||||
]
|
||||
|
||||
@@ -16,13 +16,14 @@ stl = []
|
||||
obj = []
|
||||
ply = []
|
||||
gltf = ["dep:serde_json"]
|
||||
fbx = ["dep:miniz_oxide"]
|
||||
fbx = ["dep:miniz_oxide", "dep:craft-zip"]
|
||||
threemf = ["dep:craft-opc", "dep:craft-xml"]
|
||||
|
||||
[dependencies]
|
||||
serde_json = { workspace = true, optional = true }
|
||||
# zlib-compressed arrays in binary FBX.
|
||||
miniz_oxide = { version = "0.9", optional = true }
|
||||
craft-zip = { workspace = true, optional = true }
|
||||
# 3MF is an OPC package of XML parts.
|
||||
craft-opc = { workspace = true, optional = true }
|
||||
craft-xml = { workspace = true, optional = true }
|
||||
+519
-154
@@ -39,21 +39,23 @@
|
||||
//! name is listed in [`Scene::external`]. The names come from the file: a resolver must not trust
|
||||
//! them as paths.
|
||||
//!
|
||||
//! Not read: FBX 5 and older, animation stacks, skin deformers, blend shapes, cameras and
|
||||
//! lights, NURBS and patches.
|
||||
//! Not read: FBX 5 and older, animation stacks, skin deformers, blend shapes, lights, NURBS and patches.
|
||||
//!
|
||||
//! [`Limits::max_data_bytes`] bounds what one file may keep in memory: its parsed values (an
|
||||
//! array element counts 8 bytes whatever its type in the file), the meshes built from them and
|
||||
//! the image bytes.
|
||||
|
||||
use std::cell::Cell;
|
||||
use std::collections::HashMap;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
|
||||
use crate::{
|
||||
AlphaMode, Corner, IDENTITY, Image, Limits, Mat4, Material, Mesh, NONE, Node, Part, Resolver, Result, Scene, TextureRef, base64_decode,
|
||||
count_warning, malformed, mat_mul, srgb_to_linear, too_large,
|
||||
AlphaMode, Camera, Corner, IDENTITY, Image, Limits, Mat4, Material, Mesh, NONE, Node, Part, Projection, Resolver, Result, Scene, TextureRef,
|
||||
base64_decode, count_warning, malformed, mat_mul, srgb_to_linear, too_large,
|
||||
};
|
||||
|
||||
/// Bounded ZIP packages containing an FBX and its texture files.
|
||||
pub mod package;
|
||||
|
||||
const KIND: &str = "FBX";
|
||||
|
||||
/// The binary file's magic (`Kaydara FBX Binary`, two spaces, NUL, 0x1A, 0x00).
|
||||
@@ -89,7 +91,8 @@ impl Budget {
|
||||
}
|
||||
|
||||
/// Bytes the values under `records` keep. Used for ASCII files, which hold array elements as one
|
||||
/// [`Prop`] each and are parsed before they can be counted (the text itself bounds them).
|
||||
/// [`Prop`] each.
|
||||
#[cfg(test)]
|
||||
pub(crate) fn value_bytes(records: &[Record]) -> Option<usize> {
|
||||
records.iter().try_fold(0usize, |acc, n| {
|
||||
let own = n.props.iter().try_fold(0usize, |a, p| {
|
||||
@@ -109,7 +112,13 @@ pub(crate) fn value_bytes(records: &[Record]) -> Option<usize> {
|
||||
/// Bytes a mesh keeps.
|
||||
pub(crate) fn mesh_bytes(m: &Mesh) -> Option<usize> {
|
||||
let uvs = m.uv_sets.iter().try_fold(0usize, |a, s| a.checked_add(s.len().checked_mul(8)?))?;
|
||||
let pools = m.positions.len().checked_mul(24)?.checked_add(m.normals.len().checked_mul(24)?)?.checked_add(uvs)?;
|
||||
let pools = m
|
||||
.positions
|
||||
.len()
|
||||
.checked_mul(24)?
|
||||
.checked_add(m.normals.len().checked_mul(24)?)?
|
||||
.checked_add(uvs)?
|
||||
.checked_add(m.colors.len().checked_mul(16)?)?;
|
||||
m.parts.iter().try_fold(pools, |a, p| a.checked_add(p.corners.len().checked_mul(12)?)?.checked_add(p.face_sizes.len().checked_mul(4)?))
|
||||
}
|
||||
|
||||
@@ -229,13 +238,29 @@ pub fn is_binary(bytes: &[u8]) -> bool {
|
||||
|
||||
/// Read an FBX file (binary or ASCII) into its records.
|
||||
pub fn read_document(bytes: &[u8], limits: &Limits) -> Result<Document> {
|
||||
read_document_impl(bytes, limits, false)
|
||||
}
|
||||
|
||||
/// Read records strictly, checking ASCII delimiters and the binary closing record.
|
||||
pub fn read_document_strict(bytes: &[u8], limits: &Limits) -> Result<Document> {
|
||||
read_document_impl(bytes, limits, true)
|
||||
}
|
||||
|
||||
fn read_document_impl(bytes: &[u8], limits: &Limits, strict: bool) -> Result<Document> {
|
||||
let budget = Budget::new(limits.max_data_bytes);
|
||||
let (version, records) = if is_binary(bytes) {
|
||||
read_binary(bytes, &budget)?
|
||||
read_binary(bytes, &budget, strict)?
|
||||
} else {
|
||||
let (version, records) = read_ascii(&String::from_utf8_lossy(bytes))?;
|
||||
budget.take(value_bytes(&records), "its values")?;
|
||||
(version, records)
|
||||
let text = match std::str::from_utf8(bytes) {
|
||||
Ok(text) => std::borrow::Cow::Borrowed(text),
|
||||
Err(_) => {
|
||||
if bytes.len() > limits.max_data_bytes {
|
||||
return Err(too_large(KIND, "invalid UTF-8 text exceeds the memory limit"));
|
||||
}
|
||||
String::from_utf8_lossy(bytes)
|
||||
}
|
||||
};
|
||||
read_ascii(&text, &budget, strict)?
|
||||
};
|
||||
Ok(Document { version, records, left: budget.left.get() })
|
||||
}
|
||||
@@ -254,6 +279,7 @@ struct Bin<'a> {
|
||||
/// 64-bit record offsets (version 7500 and later).
|
||||
wide: bool,
|
||||
budget: &'a Budget,
|
||||
records_budget: &'a Budget,
|
||||
}
|
||||
|
||||
impl Bin<'_> {
|
||||
@@ -313,6 +339,8 @@ impl Bin<'_> {
|
||||
if end <= start || end > limit.min(self.d.len()) {
|
||||
return Err(malformed(KIND, format!("bad record end {end} at byte {start}")));
|
||||
}
|
||||
// Bound record and property-vector allocations before copying names or reserving storage.
|
||||
self.records_budget.take(std::mem::size_of::<Record>().checked_add(nlen.saturating_mul(3)), "its records")?;
|
||||
let name = String::from_utf8_lossy(self.take(pos, nlen)?).into_owned();
|
||||
let pstart = *pos;
|
||||
let pend =
|
||||
@@ -321,7 +349,9 @@ impl Bin<'_> {
|
||||
if nprops > plen as u64 {
|
||||
return Err(malformed(KIND, format!("`{name}`: {nprops} properties in {plen} bytes")));
|
||||
}
|
||||
let mut props = Vec::with_capacity(nprops as usize);
|
||||
let count = usize::try_from(nprops).map_err(|_| too_large(KIND, "too many properties"))?;
|
||||
self.records_budget.take(count.checked_mul(std::mem::size_of::<Prop>()), "its properties")?;
|
||||
let mut props = Vec::with_capacity(count);
|
||||
for _ in 0..nprops {
|
||||
props.push(self.prop(pos, pend)?);
|
||||
}
|
||||
@@ -332,7 +362,12 @@ impl Bin<'_> {
|
||||
}
|
||||
|
||||
fn prop(&self, pos: &mut usize, pend: usize) -> Result<Prop> {
|
||||
let sub = Bin { d: self.d.get(..pend).ok_or_else(|| malformed(KIND, "truncated property"))?, wide: self.wide, budget: self.budget };
|
||||
let sub = Bin {
|
||||
d: self.d.get(..pend).ok_or_else(|| malformed(KIND, "truncated property"))?,
|
||||
wide: self.wide,
|
||||
budget: self.budget,
|
||||
records_budget: self.records_budget,
|
||||
};
|
||||
let t = sub.arr::<1>(pos)?[0];
|
||||
Ok(match t {
|
||||
b'Y' => Prop::Int(i64::from(i16::from_le_bytes(sub.arr(pos)?))),
|
||||
@@ -343,10 +378,12 @@ impl Bin<'_> {
|
||||
b'D' => Prop::Float(f64::from_le_bytes(sub.arr(pos)?)),
|
||||
b'S' => {
|
||||
let n = sub.u32(pos)? as usize;
|
||||
sub.budget.take(n.checked_mul(3), "a string")?;
|
||||
Prop::Str(object_name(sub.take(pos, n)?))
|
||||
}
|
||||
b'R' => {
|
||||
let n = sub.u32(pos)? as usize;
|
||||
sub.budget.take(Some(n), "binary data")?;
|
||||
Prop::Bytes(sub.take(pos, n)?.to_vec())
|
||||
}
|
||||
b'f' | b'd' | b'l' | b'i' | b'b' => {
|
||||
@@ -398,143 +435,178 @@ fn object_name(b: &[u8]) -> String {
|
||||
}
|
||||
}
|
||||
|
||||
fn read_binary(bytes: &[u8], budget: &Budget) -> Result<(u32, Vec<Record>)> {
|
||||
fn read_binary(bytes: &[u8], budget: &Budget, strict: bool) -> Result<(u32, Vec<Record>)> {
|
||||
let mut pos = BINARY_MAGIC.len();
|
||||
let version = Bin { d: bytes, wide: false, budget }.u32(&mut pos)?;
|
||||
let bin = Bin { d: bytes, wide: version >= 7500, budget };
|
||||
let records_budget = Budget::new(budget.limit);
|
||||
let version = Bin { d: bytes, wide: false, budget, records_budget: &records_budget }.u32(&mut pos)?;
|
||||
let bin = Bin { d: bytes, wide: version >= 7500, budget, records_budget: &records_budget };
|
||||
let mut records = vec![];
|
||||
let mut closed = false;
|
||||
while pos < bytes.len() {
|
||||
match bin.record(&mut pos, bytes.len(), 0) {
|
||||
Ok(Some(n)) => records.push(n),
|
||||
Ok(None) => break,
|
||||
Ok(None) => {
|
||||
closed = true;
|
||||
break;
|
||||
}
|
||||
// A damaged tail (footer, padding) after complete sections is ignored.
|
||||
Err(crate::Error::Malformed(_)) if records.iter().any(|n| n.name == "Objects") => break,
|
||||
Err(crate::Error::Malformed(_)) if !strict && records.iter().any(|n| n.name == "Objects") => break,
|
||||
Err(e) => return Err(e),
|
||||
}
|
||||
}
|
||||
if strict && !closed {
|
||||
return Err(malformed(KIND, "missing binary closing record"));
|
||||
}
|
||||
Ok((version, records))
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------------------------
|
||||
// ASCII
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
enum Tok {
|
||||
Key(String),
|
||||
Val(Prop),
|
||||
/// `*N` array length (the values follow in an `a:` child).
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
enum Tok<'a> {
|
||||
Key(&'a str),
|
||||
Value(&'a str, bool),
|
||||
Count,
|
||||
Comma,
|
||||
Open,
|
||||
Close,
|
||||
}
|
||||
|
||||
fn tokenize(text: &str) -> Vec<Tok> {
|
||||
let mut out = vec![];
|
||||
let mut it = text.char_indices().peekable();
|
||||
while let Some(&(i, c)) = it.peek() {
|
||||
match c {
|
||||
c if c.is_whitespace() => {
|
||||
it.next();
|
||||
/// Scan on demand so punctuation and ignored input cannot build an unbounded token vector.
|
||||
struct Ascii<'a> {
|
||||
text: &'a str,
|
||||
pos: usize,
|
||||
lookahead: Option<Tok<'a>>,
|
||||
strict: bool,
|
||||
unterminated: bool,
|
||||
}
|
||||
|
||||
impl<'a> Ascii<'a> {
|
||||
fn next_token(&mut self) -> Option<Tok<'a>> {
|
||||
loop {
|
||||
let rest = self.text.get(self.pos..)?;
|
||||
let c = rest.chars().next()?;
|
||||
if c.is_whitespace() {
|
||||
self.pos += c.len_utf8();
|
||||
continue;
|
||||
}
|
||||
';' => {
|
||||
for (_, c) in it.by_ref() {
|
||||
if c == '\n' {
|
||||
break;
|
||||
}
|
||||
if c == ';' {
|
||||
self.pos += rest.find('\n').map_or(rest.len(), |i| i + 1);
|
||||
continue;
|
||||
}
|
||||
match c {
|
||||
',' | '{' | '}' => {
|
||||
self.pos += 1;
|
||||
return Some(match c {
|
||||
',' => Tok::Comma,
|
||||
'{' => Tok::Open,
|
||||
_ => Tok::Close,
|
||||
});
|
||||
}
|
||||
}
|
||||
',' => {
|
||||
it.next();
|
||||
out.push(Tok::Comma);
|
||||
}
|
||||
'{' => {
|
||||
it.next();
|
||||
out.push(Tok::Open);
|
||||
}
|
||||
'}' => {
|
||||
it.next();
|
||||
out.push(Tok::Close);
|
||||
}
|
||||
'"' => {
|
||||
it.next();
|
||||
let start = i + 1;
|
||||
let mut end = text.len();
|
||||
for (j, c) in it.by_ref() {
|
||||
if c == '"' {
|
||||
end = j;
|
||||
break;
|
||||
}
|
||||
'"' => {
|
||||
let tail = rest.get(1..).unwrap_or("");
|
||||
let end = tail.find('"').unwrap_or(tail.len());
|
||||
self.unterminated |= end == tail.len();
|
||||
self.pos += 1 + end + usize::from(end < tail.len());
|
||||
return Some(Tok::Value(tail.get(..end).unwrap_or(""), true));
|
||||
}
|
||||
out.push(Tok::Val(Prop::Str(text.get(start..end).unwrap_or("").replace(""", "\""))));
|
||||
}
|
||||
_ => {
|
||||
let start = i;
|
||||
let mut end = text.len();
|
||||
while let Some(&(j, c)) = it.peek() {
|
||||
if c.is_whitespace() || matches!(c, ',' | '{' | '}' | '"' | ';' | ':') {
|
||||
end = j;
|
||||
break;
|
||||
_ => {
|
||||
let end = rest.find(|c: char| c.is_whitespace() || matches!(c, ',' | '{' | '}' | '"' | ';' | ':')).unwrap_or(rest.len());
|
||||
let word = rest.get(..end).unwrap_or("");
|
||||
self.pos += end;
|
||||
if rest.as_bytes().get(end) == Some(&b':') {
|
||||
self.pos += 1;
|
||||
return Some(Tok::Key(word));
|
||||
}
|
||||
if word.starts_with('*') {
|
||||
return Some(Tok::Count);
|
||||
}
|
||||
if !word.is_empty() {
|
||||
return Some(Tok::Value(word, false));
|
||||
}
|
||||
it.next();
|
||||
}
|
||||
let word = text.get(start..end).unwrap_or("");
|
||||
if it.peek().is_some_and(|(_, c)| *c == ':') {
|
||||
it.next();
|
||||
out.push(Tok::Key(word.to_string()));
|
||||
} else if word.starts_with('*') {
|
||||
out.push(Tok::Count);
|
||||
} else if let Ok(i) = word.parse::<i64>() {
|
||||
out.push(Tok::Val(Prop::Int(i)));
|
||||
} else if let Ok(f) = word.parse::<f64>() {
|
||||
out.push(Tok::Val(Prop::Float(f)));
|
||||
} else if !word.is_empty() {
|
||||
out.push(Tok::Val(Prop::Str(word.to_string())));
|
||||
} else {
|
||||
// A stray `:` with no key.
|
||||
it.next();
|
||||
self.pos += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
out
|
||||
|
||||
fn peek(&mut self) -> Option<Tok<'a>> {
|
||||
if self.lookahead.is_none() {
|
||||
self.lookahead = self.next_token();
|
||||
}
|
||||
self.lookahead
|
||||
}
|
||||
|
||||
fn take(&mut self) -> Option<Tok<'a>> {
|
||||
self.lookahead.take().or_else(|| self.next_token())
|
||||
}
|
||||
}
|
||||
|
||||
fn ascii_list(toks: &[Tok], i: &mut usize, depth: usize) -> Result<Vec<Record>> {
|
||||
fn ascii_list(scan: &mut Ascii<'_>, budget: &Budget, records_budget: &Budget, depth: usize) -> Result<Vec<Record>> {
|
||||
if depth > MAX_DEPTH {
|
||||
return Err(too_large(KIND, "records nested too deeply"));
|
||||
}
|
||||
let mut out = vec![];
|
||||
while let Some(t) = toks.get(*i) {
|
||||
*i += 1;
|
||||
while let Some(t) = scan.take() {
|
||||
match t {
|
||||
Tok::Close if depth > 0 => return Ok(out),
|
||||
Tok::Close if scan.strict => return Err(malformed(KIND, "unbalanced closing brace")),
|
||||
Tok::Key(name) => {
|
||||
let mut record = Record { name: name.clone(), ..Record::default() };
|
||||
records_budget.take(std::mem::size_of::<Record>().checked_add(name.len()), "its records")?;
|
||||
let mut record = Record { name: name.to_string(), ..Record::default() };
|
||||
let mut needs_comma = false;
|
||||
loop {
|
||||
match toks.get(*i) {
|
||||
Some(Tok::Val(v)) => record.props.push(v.clone()),
|
||||
Some(Tok::Count | Tok::Comma) => {}
|
||||
match scan.peek() {
|
||||
Some(Tok::Value(..) | Tok::Count) if scan.strict && needs_comma => break,
|
||||
Some(Tok::Value(value, quoted)) => {
|
||||
let number = if quoted {
|
||||
None
|
||||
} else {
|
||||
value.parse::<i64>().ok().map(Prop::Int).or_else(|| value.parse::<f64>().ok().map(Prop::Float))
|
||||
};
|
||||
let size = std::mem::size_of::<Prop>().checked_add(if number.is_some() { 0 } else { value.len() });
|
||||
// Charge before copying a string or growing the property vector.
|
||||
budget.take(size, "its values")?;
|
||||
record
|
||||
.props
|
||||
.push(number.unwrap_or_else(|| Prop::Str(if quoted { value.replace(""", "\"") } else { value.to_string() })));
|
||||
}
|
||||
Some(Tok::Count) => {}
|
||||
Some(Tok::Comma) => {
|
||||
needs_comma = false;
|
||||
scan.take();
|
||||
continue;
|
||||
}
|
||||
_ => break,
|
||||
}
|
||||
*i += 1;
|
||||
needs_comma = true;
|
||||
scan.take();
|
||||
}
|
||||
if toks.get(*i) == Some(&Tok::Open) {
|
||||
*i += 1;
|
||||
record.children = ascii_list(toks, i, depth + 1)?;
|
||||
if scan.peek() == Some(Tok::Open) {
|
||||
scan.take();
|
||||
record.children = ascii_list(scan, budget, records_budget, depth + 1)?;
|
||||
}
|
||||
out.push(record);
|
||||
}
|
||||
_ if scan.strict => return Err(malformed(KIND, "expected a property name")),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
if scan.strict && depth > 0 {
|
||||
return Err(malformed(KIND, "missing closing brace"));
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
fn read_ascii(text: &str) -> Result<(u32, Vec<Record>)> {
|
||||
let toks = tokenize(text);
|
||||
let mut i = 0;
|
||||
let records = ascii_list(&toks, &mut i, 0)?;
|
||||
fn read_ascii(text: &str, budget: &Budget, strict: bool) -> Result<(u32, Vec<Record>)> {
|
||||
let mut scan = Ascii { text, pos: 0, lookahead: None, strict, unterminated: false };
|
||||
let records_budget = Budget::new(budget.limit);
|
||||
let records = ascii_list(&mut scan, budget, &records_budget, 0)?;
|
||||
if strict && scan.unterminated {
|
||||
return Err(malformed(KIND, "unterminated string"));
|
||||
}
|
||||
let version = records
|
||||
.iter()
|
||||
.find(|n| n.name == "FBXHeaderExtension")
|
||||
@@ -814,53 +886,244 @@ impl Document {
|
||||
}
|
||||
}
|
||||
|
||||
// One mesh per Model with Mesh geometry (material slots are per Model). Models instancing
|
||||
// the same geometry with the same materials share one mesh rather than each building a
|
||||
// copy.
|
||||
let mut mesh_of: HashMap<(Key, Vec<usize>), usize> = HashMap::new();
|
||||
for (id, o) in &models {
|
||||
// FBX cameras look along +X; a child turns the neutral camera's -Z onto +X.
|
||||
// Keep this separate from the Model so its children retain their original transforms.
|
||||
for (id, model) in &models {
|
||||
let class_index = if matches!(model.props.first(), Some(Prop::Int(_))) { 2 } else { 1 };
|
||||
if model.str_at(class_index) != Some("Camera") {
|
||||
continue;
|
||||
}
|
||||
let name = short_name(model.first_str().unwrap_or(""));
|
||||
if name.starts_with("Producer ") {
|
||||
continue;
|
||||
}
|
||||
let Some(ni) = node_of.get(id).copied() else { continue };
|
||||
let Some((gid, geom)) = b.geometry_of(*id) else { continue };
|
||||
let mut mats: Vec<usize> = vec![];
|
||||
let lens = b.children_of(*id, "NodeAttribute").last().and_then(|(key, _)| b.by_key.get(key)).copied().unwrap_or(model);
|
||||
let props = Props::of(lens);
|
||||
let positive = |key| props.f(key).filter(|v| *v > 0.0);
|
||||
let projection = if props.f("CameraProjectionType") == Some(1.0) {
|
||||
let mag = positive("OrthoZoom").unwrap_or(1.0);
|
||||
Projection::Orthographic { xmag: mag, ymag: mag, znear: 0.01, zfar: 1000.0 }
|
||||
} else {
|
||||
let yfov = match positive("FocalLength") {
|
||||
Some(focal) => 2.0 * (positive("FilmHeight").unwrap_or(0.612) * 25.4 / (2.0 * focal)).atan(),
|
||||
None => positive("FieldOfViewY").or_else(|| positive("FieldOfView")).unwrap_or(40.0).to_radians(),
|
||||
};
|
||||
Projection::Perspective { yfov, aspect: None, znear: 0.01, zfar: None }
|
||||
};
|
||||
if b.scene.cameras.len() >= limits.max_extras {
|
||||
return Err(too_large(KIND, "too many cameras"));
|
||||
}
|
||||
b.node_room()?;
|
||||
let ci = b.scene.cameras.len();
|
||||
b.scene.cameras.push(Camera { name: name.to_string(), projection });
|
||||
let child = b.scene.nodes.len();
|
||||
b.scene.nodes.push(Node { name: format!("{name} camera"), transform: rotate(1, -90.0), camera: Some(ci), ..Node::default() });
|
||||
if let Some(node) = b.scene.nodes.get_mut(ni) {
|
||||
node.children.push(child);
|
||||
}
|
||||
}
|
||||
|
||||
// One mesh per geometry and material-slot combination, shared by instances.
|
||||
let mut mesh_of: HashMap<(Key, Vec<usize>), usize> = HashMap::new();
|
||||
let mut placed = HashSet::new();
|
||||
for (id, model) in &models {
|
||||
let Some(ni) = node_of.get(id).copied() else { continue };
|
||||
let geometries = b.geometries_of(*id);
|
||||
if geometries.is_empty() {
|
||||
continue;
|
||||
}
|
||||
let mut mats = Vec::new();
|
||||
for (mid, _) in b.children_of(*id, "Material") {
|
||||
mats.extend(b.material(mid)?);
|
||||
}
|
||||
let name = b.scene.nodes.get(ni).map(|n| n.name.clone()).unwrap_or_default();
|
||||
let mesh_index = match mesh_of.get(&(gid, mats.clone())) {
|
||||
Some(i) => *i,
|
||||
None => {
|
||||
let Some(mesh) = b.mesh(&name, geom, &mats)? else { continue };
|
||||
b.budget.take(mesh_bytes(&mesh), &format!("mesh `{name}`"))?;
|
||||
if b.scene.meshes.len() >= limits.max_meshes {
|
||||
return Err(too_large(KIND, format!("more than {} meshes", limits.max_meshes)));
|
||||
}
|
||||
b.scene.meshes.push(mesh);
|
||||
mesh_of.insert((gid, mats), b.scene.meshes.len() - 1);
|
||||
b.scene.meshes.len() - 1
|
||||
for (gid, geom) in geometries {
|
||||
if !placed.insert((gid, Some(*id))) {
|
||||
continue;
|
||||
}
|
||||
};
|
||||
match geometric(&Props::of(o)) {
|
||||
Some(transform) => {
|
||||
let mesh_index = match mesh_of.get(&(gid, mats.clone())) {
|
||||
Some(i) => *i,
|
||||
None => {
|
||||
let Some(mesh) = b.mesh(&name, geom, &mats)? else { continue };
|
||||
b.budget.take(mesh_bytes(&mesh), &format!("mesh `{name}`"))?;
|
||||
if b.scene.meshes.len() >= limits.max_meshes {
|
||||
return Err(too_large(KIND, format!("more than {} meshes", limits.max_meshes)));
|
||||
}
|
||||
b.scene.meshes.push(mesh);
|
||||
mesh_of.insert((gid, mats.clone()), b.scene.meshes.len() - 1);
|
||||
b.scene.meshes.len() - 1
|
||||
}
|
||||
};
|
||||
let transform = geometric(&Props::of(model));
|
||||
let occupied = b.scene.nodes.get(ni).is_some_and(|n| n.mesh.is_some());
|
||||
if transform.is_some() || occupied {
|
||||
b.node_room()?;
|
||||
let gi = b.scene.nodes.len();
|
||||
b.scene.nodes.push(Node { name: format!("{name} geometry"), transform, mesh: Some(mesh_index), ..Node::default() });
|
||||
if let Some(n) = b.scene.nodes.get_mut(ni) {
|
||||
n.children.push(gi);
|
||||
}
|
||||
}
|
||||
None => {
|
||||
if let Some(n) = b.scene.nodes.get_mut(ni) {
|
||||
n.mesh = Some(mesh_index);
|
||||
let child = b.scene.nodes.len();
|
||||
b.scene.nodes.push(Node {
|
||||
name: format!("{name} geometry"),
|
||||
transform: transform.unwrap_or(IDENTITY),
|
||||
mesh: Some(mesh_index),
|
||||
..Node::default()
|
||||
});
|
||||
if let Some(node) = b.scene.nodes.get_mut(ni) {
|
||||
node.children.push(child);
|
||||
}
|
||||
} else if let Some(node) = b.scene.nodes.get_mut(ni) {
|
||||
node.mesh = Some(mesh_index);
|
||||
}
|
||||
}
|
||||
}
|
||||
// Geometry with no Model connection still contributes a mesh at the origin.
|
||||
let placed_ids: HashSet<Key<'_>> = placed.iter().map(|(gid, _)| *gid).collect();
|
||||
for geom in objects.children.iter().filter(|o| o.name == "Geometry" && o.child("Vertices").is_some()) {
|
||||
let Some(gid) = key(geom.props.first()) else { continue };
|
||||
if placed_ids.contains(&gid) {
|
||||
continue;
|
||||
}
|
||||
let Some(mesh) = b.mesh("", geom, &[])? else { continue };
|
||||
b.budget.take(mesh_bytes(&mesh), "unplaced mesh")?;
|
||||
if b.scene.meshes.len() >= limits.max_meshes {
|
||||
return Err(too_large(KIND, format!("more than {} meshes", limits.max_meshes)));
|
||||
}
|
||||
b.node_room()?;
|
||||
let mesh_index = b.scene.meshes.len();
|
||||
b.scene.meshes.push(mesh);
|
||||
b.scene.roots.push(b.scene.nodes.len());
|
||||
b.scene.nodes.push(Node { mesh: Some(mesh_index), ..Node::default() });
|
||||
}
|
||||
let mut scene = b.scene;
|
||||
count_warning(&mut scene.warnings, b.skipped, "polygons with fewer than three corners or a vertex that is not there were left out");
|
||||
Ok(scene)
|
||||
}
|
||||
}
|
||||
|
||||
/// Diffuse material values as stored in FBX, before color-space conversion.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct DiffuseMaterial {
|
||||
pub name: String,
|
||||
pub factor: [f32; 4],
|
||||
/// Encoded embedded or resolved image bytes, when available.
|
||||
pub image: Option<Vec<u8>>,
|
||||
}
|
||||
|
||||
impl Document {
|
||||
/// Read all FBX diffuse materials, including unconnected materials. The resolver receives
|
||||
/// the relative path followed by the absolute path's leaf, in preference order.
|
||||
pub fn diffuse_materials(&self, limits: &Limits, resolve: &mut dyn FnMut(&[&str]) -> Option<Vec<u8>>) -> Result<Vec<DiffuseMaterial>> {
|
||||
let objects = self.top("Objects").ok_or_else(|| malformed(KIND, "no Objects section"))?;
|
||||
let mut materials = HashMap::new();
|
||||
let mut textures = HashMap::new();
|
||||
let mut videos = HashMap::new();
|
||||
for record in &objects.children {
|
||||
let Some(id) = key(record.props.first()) else { continue };
|
||||
match record.name.as_str() {
|
||||
"Material" => {
|
||||
materials.insert(id, record);
|
||||
}
|
||||
"Texture" => {
|
||||
textures.insert(id, record);
|
||||
}
|
||||
"Video" => {
|
||||
videos.insert(id, record);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
if materials.len() > limits.max_materials {
|
||||
return Err(too_large(KIND, "too many materials"));
|
||||
}
|
||||
let mut video_of = HashMap::new();
|
||||
let mut diffuse = HashMap::new();
|
||||
for link in self.top("Connections").into_iter().flat_map(|c| c.children.iter()).filter(|c| c.name == "C" || c.name == "Connect") {
|
||||
let (Some(child), Some(parent)) = (key(link.props.get(1)), key(link.props.get(2))) else { continue };
|
||||
if videos.contains_key(&child) && textures.contains_key(&parent) {
|
||||
video_of.insert(parent, child);
|
||||
} else if textures.contains_key(&child) && materials.contains_key(&parent) {
|
||||
match (link.str_at(0), link.str_at(3)) {
|
||||
(Some("OP"), Some("DiffuseColor" | "Maya|baseColor")) => {
|
||||
diffuse.insert(parent, child);
|
||||
}
|
||||
(Some("OO"), _) => {
|
||||
diffuse.entry(parent).or_insert(child);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
let budget = Budget { left: Cell::new(self.left.min(limits.max_data_bytes)), limit: limits.max_data_bytes };
|
||||
let mut out = Vec::new();
|
||||
for (id, material) in materials {
|
||||
let tex = diffuse.get(&id).and_then(|t| textures.get(t)).copied();
|
||||
let video = diffuse.get(&id).and_then(|t| video_of.get(t)).and_then(|v| videos.get(v)).copied();
|
||||
let mut image = None;
|
||||
if let Some(content) = video.or(tex).and_then(|n| n.child("Content")) {
|
||||
match content.props.first() {
|
||||
Some(Prop::Bytes(bytes)) if !bytes.is_empty() => {
|
||||
budget.take(Some(bytes.len()), "an embedded image")?;
|
||||
image = Some(bytes.clone());
|
||||
}
|
||||
Some(Prop::Str(_)) => {
|
||||
let size = content.props.iter().filter_map(Prop::as_str).try_fold(0usize, |n, s| n.checked_add(s.len()));
|
||||
// Reserve a conservative bound before concatenating and decoding.
|
||||
budget.take(size, "an embedded image")?;
|
||||
let text: String = content.props.iter().filter_map(Prop::as_str).collect();
|
||||
image = base64_decode(text.trim()).filter(|b| !b.is_empty());
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
if image.is_none() {
|
||||
for record in [tex, video].into_iter().flatten() {
|
||||
let relative = record.child("RelativeFilename").and_then(|n| n.str_at(0)).unwrap_or("");
|
||||
let absolute = ["FileName", "Filename"]
|
||||
.into_iter()
|
||||
.find_map(|n| record.child(n).and_then(|v| v.str_at(0)).filter(|s| !s.is_empty()))
|
||||
.unwrap_or("");
|
||||
let leaf = absolute.rsplit(['/', '\\']).next().unwrap_or("");
|
||||
let mut paths = Vec::new();
|
||||
for path in [relative, leaf] {
|
||||
if !path.is_empty() && !paths.contains(&path) {
|
||||
paths.push(path);
|
||||
}
|
||||
}
|
||||
if !paths.is_empty()
|
||||
&& let Some(bytes) = resolve(&paths)
|
||||
{
|
||||
budget.take(Some(bytes.len()), "a resolved image")?;
|
||||
image = Some(bytes);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
let props = Props::of(material);
|
||||
let values = props.values("DiffuseColor");
|
||||
let factor = match values.as_slice() {
|
||||
[r, g, b, ..] => {
|
||||
let f = props.f("DiffuseFactor").unwrap_or(1.0);
|
||||
[(*r * f) as f32, (*g * f) as f32, (*b * f) as f32, 1.0]
|
||||
}
|
||||
_ => [1.0; 4],
|
||||
};
|
||||
if factor.iter().any(|v| !v.is_finite()) || (image.is_none() && values.len() < 3) {
|
||||
continue;
|
||||
}
|
||||
let raw_name = short_name(material.first_str().unwrap_or(""));
|
||||
let name = if raw_name.is_empty() {
|
||||
match id {
|
||||
Key::Id(i) => format!("Material_{i}"),
|
||||
Key::Name(s) => format!("Material_{s}"),
|
||||
}
|
||||
} else {
|
||||
raw_name.to_string()
|
||||
};
|
||||
out.push(DiffuseMaterial { name, factor, image });
|
||||
}
|
||||
out.sort_by(|a, b| a.name.cmp(&b.name));
|
||||
Ok(out)
|
||||
}
|
||||
}
|
||||
|
||||
/// What connections name an object by: its id, or its `Class::Name`.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
||||
enum Key<'a> {
|
||||
@@ -1011,14 +1274,18 @@ impl<'a> Build<'a, '_> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// The mesh geometry of Model `id`, and what it is shared by.
|
||||
fn geometry_of(&self, id: Key<'a>) -> Option<(Key<'a>, &'a Record)> {
|
||||
// 6.x: the Model holds its own mesh.
|
||||
/// The geometries connected to a Model, including its own FBX 6 mesh.
|
||||
fn geometries_of(&self, id: Key<'a>) -> Vec<(Key<'a>, &'a Record)> {
|
||||
let mut geometries = Vec::new();
|
||||
if let Some(model) = self.by_key.get(&id).copied().filter(|o| o.child("Vertices").is_some()) {
|
||||
return Some((id, model));
|
||||
geometries.push((id, model));
|
||||
}
|
||||
let (gid, _) = self.children_of(id, "Geometry").into_iter().next()?;
|
||||
Some((gid, self.by_key.get(&gid).copied()?))
|
||||
for (gid, _) in self.children_of(id, "Geometry") {
|
||||
if let Some(geometry) = self.by_key.get(&gid).copied().filter(|o| o.child("Vertices").is_some()) {
|
||||
geometries.push((gid, geometry));
|
||||
}
|
||||
}
|
||||
geometries
|
||||
}
|
||||
|
||||
/// The image of Texture `tid`.
|
||||
@@ -1086,7 +1353,18 @@ impl<'a> Build<'a, '_> {
|
||||
return Ok(Some(*m));
|
||||
}
|
||||
let Some(record) = self.by_key.get(&mid).copied() else { return Ok(None) };
|
||||
let mut m = material(short_name(record.first_str().unwrap_or("")), record);
|
||||
let name = short_name(record.first_str().unwrap_or(""));
|
||||
let fallback;
|
||||
let name = if name.is_empty() {
|
||||
fallback = match mid {
|
||||
Key::Id(i) => format!("Material_{i}"),
|
||||
Key::Name(s) => format!("Material_{s}"),
|
||||
};
|
||||
&fallback
|
||||
} else {
|
||||
name
|
||||
};
|
||||
let mut m = material(name, record);
|
||||
for (tid, prop) in self.children_of(mid, "Texture") {
|
||||
let slot = match prop {
|
||||
Some("DiffuseColor") => &mut m.base_color_texture,
|
||||
@@ -1128,13 +1406,14 @@ impl<'a> Build<'a, '_> {
|
||||
return Err(malformed(KIND, "mesh array contains a non-numeric, non-finite or fractional index value"));
|
||||
}
|
||||
for (element, names) in [
|
||||
("LayerElementNormal", &["Normals", "NormalsIndex"][..]),
|
||||
("LayerElementNormal", &["Normals", "NormalsIndex", "NormalIndex"][..]),
|
||||
("LayerElementUV", &["UV", "UVIndex"][..]),
|
||||
("LayerElementMaterial", &["Materials"][..]),
|
||||
("LayerElementColor", &["Colors", "ColorIndex"][..]),
|
||||
("LayerElementColor", &["Colors", "ColorIndex", "ColorsIndex"][..]),
|
||||
] {
|
||||
if let Some(layer) = geom.children_named(element).min_by_key(|e| e.props.first().and_then(Prop::as_i64) != Some(0))
|
||||
&& names.iter().filter_map(|n| layer.child(n)).any(|r| !valid(r, false))
|
||||
// index arrays and material ids are integers; the data arrays are floats
|
||||
&& names.iter().filter_map(|n| layer.child(n).map(|r| (r, n.ends_with("Index") || *n == "Materials"))).any(|(r, integers)| !valid(r, integers))
|
||||
{
|
||||
return Err(malformed(KIND, "mesh array contains a non-numeric or non-finite value"));
|
||||
}
|
||||
@@ -1143,13 +1422,30 @@ impl<'a> Build<'a, '_> {
|
||||
let pool = |what: &str, n: usize| {
|
||||
if n > limits.max_vertices { Err(too_large(KIND, format!("more than {} {what}", limits.max_vertices))) } else { Ok(()) }
|
||||
};
|
||||
let positions: Vec<[f64; 3]> = vertices.floats().as_chunks::<3>().0.iter().map(|c| c.map(finite)).collect();
|
||||
let mut positions: Vec<[f64; 3]> = vertices.floats().as_chunks::<3>().0.iter().map(|c| c.map(finite)).collect();
|
||||
pool("vertices", positions.len())?;
|
||||
let original_positions = positions.len();
|
||||
let normals = Layer::of(geom, "LayerElementNormal", "Normals", &["NormalsIndex", "NormalIndex"], 3);
|
||||
let uvs = Layer::of(geom, "LayerElementUV", "UV", &["UVIndex"], 2);
|
||||
let slots = geom.child("LayerElementMaterial").and_then(|e| {
|
||||
let colors = Layer::of(geom, "LayerElementColor", "Colors", &["ColorIndex", "ColorsIndex"], 4);
|
||||
if colors.is_some() && positions.len().checked_mul(16).is_none_or(|n| n > self.budget.left.get()) {
|
||||
return Err(too_large(KIND, "vertex colors exceed the memory limit"));
|
||||
}
|
||||
let mut vertex_colors = colors.as_ref().map(|_| vec![[1.0; 4]; positions.len()]).unwrap_or_default();
|
||||
let mut color_indices = HashMap::<(u32, u32), u32>::new();
|
||||
let mut first_color = HashMap::<u32, u32>::new();
|
||||
let mut colors_complete = colors.is_some();
|
||||
let slots = geom.children_named("LayerElementMaterial").min_by_key(|e| e.props.first().and_then(Prop::as_i64) != Some(0)).and_then(|e| {
|
||||
let mapping = e.child("MappingInformationType").and_then(|m| m.str_at(0)).unwrap_or("AllSame");
|
||||
Some((mapping == "ByPolygon", e.child("Materials")?.ints()))
|
||||
// a polygon goes to one part, so per-corner and per-vertex IDs use its first corner
|
||||
let by = match mapping {
|
||||
"ByPolygon" => Mapping::Polygon,
|
||||
"ByPolygonVertex" => Mapping::PolygonVertex,
|
||||
"ByVertex" | "ByVertice" | "ByControlPoint" => Mapping::Vertex,
|
||||
"AllSame" => Mapping::AllSame,
|
||||
_ => return None,
|
||||
};
|
||||
Some((by, e.child("Materials")?.ints()))
|
||||
});
|
||||
let (normal_layer, uv_layer) = (normals.as_ref().map(|l| &l.0), uvs.as_ref().map(|l| &l.0));
|
||||
|
||||
@@ -1159,15 +1455,58 @@ impl<'a> Build<'a, '_> {
|
||||
let (mut broken, mut poly) = (false, 0usize);
|
||||
let polygon_indices = pvi.ints();
|
||||
let index_count = polygon_indices.len();
|
||||
for (pv, raw) in polygon_indices.into_iter().enumerate() {
|
||||
let mut poly_start = 0usize;
|
||||
for (pv, raw) in polygon_indices.iter().copied().enumerate() {
|
||||
// The last corner of a polygon is stored as `-index - 1`.
|
||||
let (v, last) = if raw < 0 { (!raw, true) } else { (raw, self.strict_geometry && pv + 1 == index_count) };
|
||||
match u32::try_from(v).ok().filter(|v| (*v as usize) < positions.len()) {
|
||||
Some(p) => face.push(Corner {
|
||||
position: p,
|
||||
normal: Layer::at(normal_layer, pv, p as usize, poly),
|
||||
uv: Layer::at(uv_layer, pv, p as usize, poly),
|
||||
}),
|
||||
match u32::try_from(v).ok().filter(|v| (*v as usize) < original_positions) {
|
||||
Some(p) => {
|
||||
let normal = Layer::at(normal_layer, pv, p as usize, poly);
|
||||
let uv = Layer::at(uv_layer, pv, p as usize, poly);
|
||||
let mut position = p;
|
||||
if let Some((layer, values)) = &colors {
|
||||
let color = Layer::at(Some(layer), pv, p as usize, poly);
|
||||
let rgba = usize::try_from(color)
|
||||
.ok()
|
||||
.and_then(|i| i.checked_mul(4))
|
||||
.and_then(|i| i.checked_add(4).and_then(|end| values.get(i..end)));
|
||||
if let Some(rgba) = rgba {
|
||||
if let Some(existing) = color_indices.get(&(p, color)) {
|
||||
position = *existing;
|
||||
} else {
|
||||
// Color seams need two maps as well as the output pools; do not
|
||||
// grow them past the remaining file budget on hostile corner lists.
|
||||
let staging = color_indices
|
||||
.len()
|
||||
.checked_add(1)
|
||||
.and_then(|n| n.checked_mul(64))
|
||||
.and_then(|n| positions.len().checked_mul(40).and_then(|pool| n.checked_add(pool)));
|
||||
if staging.is_none_or(|n| n > self.budget.left.get()) {
|
||||
return Err(too_large(KIND, "color seams exceed the memory limit"));
|
||||
}
|
||||
if first_color.insert(p, color).is_some() {
|
||||
pool("vertices", positions.len().saturating_add(1))?;
|
||||
if positions.len().saturating_add(1).checked_mul(40).is_none_or(|n| n > self.budget.left.get()) {
|
||||
return Err(too_large(KIND, "colored vertices exceed the memory limit"));
|
||||
}
|
||||
position = u32::try_from(positions.len()).map_err(|_| too_large(KIND, "too many vertices"))?;
|
||||
let Some(original) = positions.get(p as usize).copied() else { continue };
|
||||
positions.push(original);
|
||||
vertex_colors.push([1.0; 4]);
|
||||
}
|
||||
if let Some(dst) = vertex_colors.get_mut(position as usize)
|
||||
&& let [r, g, b, a] = rgba
|
||||
{
|
||||
*dst = [finite(*r) as f32, finite(*g) as f32, finite(*b) as f32, finite(*a) as f32];
|
||||
}
|
||||
color_indices.insert((p, color), position);
|
||||
}
|
||||
} else {
|
||||
colors_complete = false;
|
||||
}
|
||||
}
|
||||
face.push(Corner { position, normal, uv });
|
||||
}
|
||||
None if self.strict_geometry => return Err(malformed(KIND, format!("polygon vertex index {raw} out of range"))),
|
||||
None => broken = true,
|
||||
}
|
||||
@@ -1178,6 +1517,7 @@ impl<'a> Build<'a, '_> {
|
||||
continue;
|
||||
}
|
||||
let (corners, p_index, was_broken) = (std::mem::take(&mut face), poly, std::mem::take(&mut broken));
|
||||
let first_pv = std::mem::replace(&mut poly_start, pv + 1);
|
||||
poly += 1;
|
||||
if was_broken || corners.len() < 3 {
|
||||
self.skipped += 1;
|
||||
@@ -1187,11 +1527,22 @@ impl<'a> Build<'a, '_> {
|
||||
if self.triangles > limits.max_triangles {
|
||||
return Err(too_large(KIND, format!("more than {} triangles", limits.max_triangles)));
|
||||
}
|
||||
let slot = slots.as_ref().and_then(|(by_poly, ids)| {
|
||||
let k = if *by_poly { *ids.get(p_index)? } else { *ids.first()? };
|
||||
materials.get(usize::try_from(k).ok()?).copied()
|
||||
});
|
||||
let slot = slot.or_else(|| materials.first().copied());
|
||||
let slot = match &slots {
|
||||
Some((by, ids)) => {
|
||||
let k = match by {
|
||||
Mapping::Polygon => ids.get(p_index),
|
||||
Mapping::PolygonVertex => ids.get(first_pv),
|
||||
Mapping::Vertex => polygon_indices
|
||||
.get(first_pv)
|
||||
.map(|&r| if r < 0 { !r } else { r })
|
||||
.and_then(|v| usize::try_from(v).ok())
|
||||
.and_then(|v| ids.get(v)),
|
||||
Mapping::AllSame => ids.first(),
|
||||
};
|
||||
k.and_then(|k| usize::try_from(*k).ok()).and_then(|k| materials.get(k)).copied()
|
||||
}
|
||||
None => materials.first().copied(),
|
||||
};
|
||||
let at = match part_of.get(&slot) {
|
||||
Some(at) => *at,
|
||||
None => {
|
||||
@@ -1221,6 +1572,20 @@ impl<'a> Build<'a, '_> {
|
||||
let uvs: Option<Vec<[f32; 2]>> =
|
||||
uvs.map(|(_, d)| d.as_chunks::<2>().0.iter().map(|c| [finite(c[0]) as f32, 1.0 - finite(c[1]) as f32]).collect());
|
||||
pool("UVs", uvs.as_ref().map_or(0, Vec::len))?;
|
||||
Ok(Some(Mesh { name: name.to_string(), positions, normals, uv_sets: uvs.into_iter().collect(), parts, ..Mesh::default() }))
|
||||
if !colors_complete {
|
||||
vertex_colors.clear();
|
||||
if colors.is_some() {
|
||||
self.scene.warnings.push("FBX: incomplete vertex color layer was omitted".into());
|
||||
}
|
||||
}
|
||||
Ok(Some(Mesh {
|
||||
name: name.to_string(),
|
||||
positions,
|
||||
normals,
|
||||
uv_sets: uvs.into_iter().collect(),
|
||||
colors: vertex_colors,
|
||||
parts,
|
||||
..Mesh::default()
|
||||
}))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,301 @@
|
||||
//! FBX and referenced texture files in a ZIP container.
|
||||
use super::{Document, KIND};
|
||||
use crate::{Error, Limits, Result, malformed, too_large};
|
||||
|
||||
const ZIP_MAGIC: &[u8] = b"PK\x03\x04";
|
||||
|
||||
/// Whether bytes begin with a ZIP local-file header.
|
||||
pub fn is_package(bytes: &[u8]) -> bool {
|
||||
bytes.starts_with(ZIP_MAGIC)
|
||||
}
|
||||
|
||||
/// Limits for an FBX package and each referenced texture.
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub struct PackageLimits {
|
||||
pub max_entries: u64,
|
||||
pub max_model_bytes: u64,
|
||||
pub max_texture_bytes: u64,
|
||||
pub max_total_bytes: u64,
|
||||
}
|
||||
|
||||
/// An open FBX package. Reads are charged to its cumulative decompression budget.
|
||||
pub struct Package<'a> {
|
||||
zip: craft_zip::Archive<'a>,
|
||||
limits: PackageLimits,
|
||||
name: String,
|
||||
/// Textures read so far by name: a texture several materials share is read, and charged, once.
|
||||
textures: std::collections::HashMap<String, Vec<u8>>,
|
||||
}
|
||||
|
||||
impl<'a> Package<'a> {
|
||||
pub fn open(bytes: &'a [u8], limits: PackageLimits) -> Result<Self> {
|
||||
let zip = craft_zip::Archive::open(bytes, craft_zip::Limits { max_entries: limits.max_entries, max_total_bytes: limits.max_total_bytes })
|
||||
.map_err(|e| malformed(KIND, format!("package: {e}")))?;
|
||||
let name = zip
|
||||
.names()
|
||||
.find(|n| n.rsplit('.').next().is_some_and(|ext| ext.eq_ignore_ascii_case("fbx")))
|
||||
.map(|n| n.into_owned())
|
||||
.ok_or_else(|| malformed(KIND, "the package contains no FBX file"))?;
|
||||
Ok(Self { zip, limits, name, textures: std::collections::HashMap::new() })
|
||||
}
|
||||
|
||||
pub fn model(&mut self) -> Result<Vec<u8>> {
|
||||
let bytes = self
|
||||
.zip
|
||||
.read(&self.name, self.limits.max_model_bytes)
|
||||
.map_err(|e| malformed(KIND, format!("package: {e}")))?
|
||||
.ok_or_else(|| malformed(KIND, "package FBX is missing"))?;
|
||||
if is_package(&bytes) {
|
||||
return Err(malformed(KIND, "the package holds another package"));
|
||||
}
|
||||
Ok(bytes)
|
||||
}
|
||||
|
||||
/// Resolve candidate names within the package; outside paths are never read.
|
||||
pub fn texture(&mut self, paths: &[&str]) -> Option<Vec<u8>> {
|
||||
for name in paths.iter().filter_map(|p| below(p)) {
|
||||
if let Some(data) = self.textures.get(&name) {
|
||||
return Some(data.clone());
|
||||
}
|
||||
if let Some(data) = self.zip.read(&name, self.limits.max_texture_bytes).ok().flatten() {
|
||||
self.textures.insert(name, data.clone());
|
||||
return Some(data);
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// Resolve a safe relative path inside a package.
|
||||
fn below(path: &str) -> Option<String> {
|
||||
let path = path.replace('\\', "/");
|
||||
if path.starts_with('/') || path.contains(':') {
|
||||
return None;
|
||||
}
|
||||
let mut parts = Vec::new();
|
||||
for part in path.split('/') {
|
||||
match part {
|
||||
"" | "." => {}
|
||||
".." => {
|
||||
parts.pop()?;
|
||||
}
|
||||
part => parts.push(part),
|
||||
}
|
||||
}
|
||||
(!parts.is_empty()).then(|| parts.join("/"))
|
||||
}
|
||||
|
||||
/// Package the FBX and its externally resolved diffuse textures, if any. Returns unresolved paths.
|
||||
pub fn write(
|
||||
doc: &Document,
|
||||
file_name: &str,
|
||||
bytes: &[u8],
|
||||
limits: &Limits,
|
||||
package_limits: PackageLimits,
|
||||
found: &mut dyn FnMut(&str) -> Option<Vec<u8>>,
|
||||
) -> Result<(Option<Vec<u8>>, Vec<String>)> {
|
||||
if is_package(bytes) {
|
||||
return Ok((None, Vec::new()));
|
||||
}
|
||||
let mut files: Vec<(String, Vec<u8>)> = Vec::new();
|
||||
let mut by_name = std::collections::HashMap::<String, usize>::new();
|
||||
let mut too_many = false;
|
||||
let mut missing = Vec::new();
|
||||
let mut missing_seen = std::collections::HashSet::<String>::new();
|
||||
let mut total = bytes.len() as u64;
|
||||
if total > package_limits.max_total_bytes || total > package_limits.max_model_bytes {
|
||||
return Err(too_large(KIND, "the FBX and its textures exceed the package limit"));
|
||||
}
|
||||
let mut exceeded = false;
|
||||
doc.diffuse_materials(limits, &mut |paths| {
|
||||
// candidates in order of preference: an earlier one that exists wins over a later cached one
|
||||
for path in paths {
|
||||
let Some(name) = below(path) else { continue };
|
||||
if let Some(data) = by_name.get(&name).and_then(|&i| files.get(i)).map(|(_, d)| d.clone()) {
|
||||
return Some(data);
|
||||
}
|
||||
// once a resolved texture has not fit, the package fails: stop calling the resolver
|
||||
if too_many || exceeded {
|
||||
return None;
|
||||
}
|
||||
if let Some(data) = found(path).filter(|d| d.len() as u64 <= package_limits.max_texture_bytes) {
|
||||
// the model is an entry too; only a texture that resolves needs a new entry
|
||||
if files.len() as u64 + 1 >= package_limits.max_entries {
|
||||
too_many = true;
|
||||
return None;
|
||||
}
|
||||
total = total.saturating_add(data.len() as u64);
|
||||
if total > package_limits.max_total_bytes {
|
||||
exceeded = true;
|
||||
return None;
|
||||
}
|
||||
by_name.insert(name.clone(), files.len());
|
||||
files.push((name, data.clone()));
|
||||
return Some(data);
|
||||
}
|
||||
}
|
||||
if let Some(first) = paths.first().filter(|p| missing_seen.insert((**p).to_string())) {
|
||||
missing.push((*first).to_string());
|
||||
}
|
||||
None
|
||||
})?;
|
||||
if exceeded {
|
||||
return Err(too_large(KIND, "the FBX and its textures exceed the package limit"));
|
||||
}
|
||||
if too_many {
|
||||
return Err(too_large(KIND, "too many package entries"));
|
||||
}
|
||||
if files.is_empty() {
|
||||
return Ok((None, missing));
|
||||
}
|
||||
let leaf = file_name.rsplit(['/', '\\']).next().filter(|n| !n.is_empty()).unwrap_or("mesh.fbx");
|
||||
// the package is found by its `.fbx` entry, so the model always carries that extension
|
||||
let leaf = if leaf.rsplit_once('.').is_some_and(|(_, ext)| ext.eq_ignore_ascii_case("fbx")) { leaf.to_string() } else { format!("{leaf}.fbx") };
|
||||
let leaf = leaf.as_str();
|
||||
let mut writer = craft_zip::Writer::new();
|
||||
for (name, data) in std::iter::once((leaf, bytes)).chain(files.iter().map(|(n, b)| (n.as_str(), b.as_slice()))) {
|
||||
writer.add(name, data, craft_zip::Compression::Stored).map_err(|e| Error::Malformed(format!("FBX package: {e}")))?;
|
||||
}
|
||||
Ok((Some(writer.finish().map_err(|e| Error::Malformed(format!("FBX package: {e}")))?), missing))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::fbx::read_document;
|
||||
|
||||
const MODEL: &[u8] = b"FBXHeaderExtension: { FBXVersion: 7400 } Objects: { Material: 1, \"Material::M\", \"\" { } Texture: 2, \"Texture::T\", \"\" { RelativeFilename: \"tex/a.png\" } } Connections: { C: \"OP\",2,1,\"DiffuseColor\" }";
|
||||
fn bounds() -> PackageLimits {
|
||||
PackageLimits { max_entries: 4, max_model_bytes: 4096, max_texture_bytes: 32, max_total_bytes: 8192 }
|
||||
}
|
||||
#[test]
|
||||
fn package_roundtrip_and_limits() {
|
||||
let limits = Limits::default();
|
||||
let doc = read_document(MODEL, &limits).unwrap();
|
||||
let (bytes, missing) = write(&doc, "asset.fbx", MODEL, &limits, bounds(), &mut |p| (p == "tex/a.png").then(|| b"image".to_vec())).unwrap();
|
||||
assert!(missing.is_empty());
|
||||
let bytes = bytes.unwrap();
|
||||
let mut package = Package::open(&bytes, bounds()).unwrap();
|
||||
assert_eq!(package.model().unwrap(), MODEL);
|
||||
assert_eq!(package.texture(&["../tex/a.png", "tex/a.png"]), Some(b"image".to_vec()));
|
||||
assert!(package.texture(&["../tex/a.png"]).is_none());
|
||||
let small = PackageLimits { max_total_bytes: MODEL.len() as u64 + 4, ..bounds() };
|
||||
assert!(matches!(write(&doc, "a.fbx", MODEL, &limits, small, &mut |_| Some(b"image".to_vec())), Err(Error::TooLarge(_))));
|
||||
assert!(Package::open(&bytes, PackageLimits { max_entries: 1, ..bounds() }).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_earlier_candidate_wins_over_a_cached_later_one() {
|
||||
let model = b"FBXHeaderExtension: { FBXVersion: 7400 } Objects: { Material: 1, \"Material::A\", \"\" { } Texture: 2, \"Texture::A\", \"\" { RelativeFilename: \"base.png\" } Material: 3, \"Material::B\", \"\" { } Texture: 4, \"Texture::B\", \"\" { RelativeFilename: \"maps/base.png\" FileName: \"C:/art/base.png\" } } Connections: { C: \"OP\",2,1,\"DiffuseColor\" C: \"OP\",4,3,\"DiffuseColor\" }";
|
||||
let limits = Limits::default();
|
||||
let doc = read_document(model, &limits).unwrap();
|
||||
let mut found = |p: &str| match p {
|
||||
"base.png" => Some(b"flat".to_vec()),
|
||||
"maps/base.png" => Some(b"maps".to_vec()),
|
||||
_ => None,
|
||||
};
|
||||
let (bytes, missing) = write(&doc, "asset.fbx", model, &limits, bounds(), &mut found).unwrap();
|
||||
assert!(missing.is_empty());
|
||||
let bytes = bytes.unwrap();
|
||||
let mut package = Package::open(&bytes, bounds()).unwrap();
|
||||
assert_eq!(package.texture(&["maps/base.png"]), Some(b"maps".to_vec()));
|
||||
assert_eq!(package.texture(&["base.png"]), Some(b"flat".to_vec()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_model_entry_always_has_an_fbx_extension() {
|
||||
let limits = Limits::default();
|
||||
let doc = read_document(MODEL, &limits).unwrap();
|
||||
let (bytes, _) = write(&doc, "dir/asset", MODEL, &limits, bounds(), &mut |_| Some(b"image".to_vec())).unwrap();
|
||||
let bytes = bytes.unwrap();
|
||||
let mut package = Package::open(&bytes, bounds()).unwrap();
|
||||
assert_eq!(package.model().unwrap(), MODEL);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn texture_resolution_stops_at_the_entry_limit() {
|
||||
let mut model = String::from("FBXHeaderExtension: { FBXVersion: 7400 } Objects: {");
|
||||
let mut links = String::new();
|
||||
for i in 0..20 {
|
||||
model.push_str(&format!(
|
||||
" Material: {m}, \"Material::M{i}\", \"\" {{ }} Texture: {t}, \"Texture::T{i}\", \"\" {{ RelativeFilename: \"t{i}.png\" }}",
|
||||
m = 2 * i + 1,
|
||||
t = 2 * i + 2
|
||||
));
|
||||
links.push_str(&format!(" C: \"OP\",{t},{m},\"DiffuseColor\"", m = 2 * i + 1, t = 2 * i + 2));
|
||||
}
|
||||
model.push_str(&format!(" }} Connections: {{{links} }}"));
|
||||
let limits = Limits::default();
|
||||
let doc = read_document(model.as_bytes(), &limits).unwrap();
|
||||
let mut calls = 0;
|
||||
let result = write(&doc, "a.fbx", model.as_bytes(), &limits, bounds(), &mut |_| {
|
||||
calls += 1;
|
||||
Some(b"image".to_vec())
|
||||
});
|
||||
assert!(matches!(result, Err(Error::TooLarge(_))));
|
||||
assert!(calls <= bounds().max_entries as usize, "{calls} resolver calls past the entry limit");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn resolution_stops_once_the_byte_limit_is_passed() {
|
||||
let mut model = String::from("FBXHeaderExtension: { FBXVersion: 7400 } Objects: {");
|
||||
let mut links = String::new();
|
||||
for i in 0..10 {
|
||||
model.push_str(&format!(
|
||||
" Material: {m}, \"Material::M{i}\", \"\" {{ }} Texture: {t}, \"Texture::T{i}\", \"\" {{ RelativeFilename: \"big.png\" }}",
|
||||
m = 2 * i + 1,
|
||||
t = 2 * i + 2
|
||||
));
|
||||
links.push_str(&format!(" C: \"OP\",{t},{m},\"DiffuseColor\"", m = 2 * i + 1, t = 2 * i + 2));
|
||||
}
|
||||
model.push_str(&format!(" }} Connections: {{{links} }}"));
|
||||
let limits = Limits::default();
|
||||
let doc = read_document(model.as_bytes(), &limits).unwrap();
|
||||
let tight = PackageLimits { max_total_bytes: model.len() as u64 + 4, max_model_bytes: 1 << 20, ..bounds() };
|
||||
let mut calls = 0;
|
||||
let result = write(&doc, "a.fbx", model.as_bytes(), &limits, tight, &mut |_| {
|
||||
calls += 1;
|
||||
Some(b"image".to_vec())
|
||||
});
|
||||
assert!(matches!(result, Err(Error::TooLarge(_))));
|
||||
assert_eq!(calls, 1, "one texture past the limit ends resolution");
|
||||
// a model past its own limit is refused before any texture is resolved
|
||||
let small = PackageLimits { max_model_bytes: 8, ..bounds() };
|
||||
let mut calls = 0;
|
||||
assert!(matches!(
|
||||
write(&doc, "a.fbx", model.as_bytes(), &limits, small, &mut |_| {
|
||||
calls += 1;
|
||||
None
|
||||
}),
|
||||
Err(Error::TooLarge(_))
|
||||
));
|
||||
assert_eq!(calls, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_entry_limit_counts_only_textures_that_resolve() {
|
||||
let limits = Limits::default();
|
||||
let doc = read_document(MODEL, &limits).unwrap();
|
||||
// the model alone fills a one-entry package, so a resolved texture cannot fit
|
||||
let one = PackageLimits { max_entries: 1, ..bounds() };
|
||||
assert!(matches!(write(&doc, "a.fbx", MODEL, &limits, one, &mut |_| Some(b"image".to_vec())), Err(Error::TooLarge(_))));
|
||||
// an unresolved texture adds no entry, so it is reported missing rather than overflowing
|
||||
let (bytes, missing) = write(&doc, "a.fbx", MODEL, &limits, one, &mut |_| None).unwrap();
|
||||
assert!(bytes.is_none());
|
||||
assert_eq!(missing, vec!["tex/a.png".to_string()]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_shared_texture_is_read_and_charged_once() {
|
||||
let limits = Limits::default();
|
||||
let doc = read_document(MODEL, &limits).unwrap();
|
||||
let (bytes, _) = write(&doc, "asset.fbx", MODEL, &limits, bounds(), &mut |_| Some(b"image".to_vec())).unwrap();
|
||||
let bytes = bytes.unwrap();
|
||||
// room for the model and one read of the texture
|
||||
let tight = PackageLimits { max_total_bytes: MODEL.len() as u64 + 5, ..bounds() };
|
||||
let mut package = Package::open(&bytes, tight).unwrap();
|
||||
package.model().unwrap();
|
||||
assert_eq!(package.texture(&["tex/a.png"]), Some(b"image".to_vec()));
|
||||
assert_eq!(package.texture(&["tex/a.png"]), Some(b"image".to_vec()), "served from the cache");
|
||||
}
|
||||
}
|
||||
@@ -682,3 +682,208 @@ fn object_only_hierarchy_ignores_property_connections() {
|
||||
assert_eq!(scene.roots.len(), 2);
|
||||
assert_eq!(doc.scene(&Limits::default(), &mut no_external).unwrap().roots.len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn binary_scalar_properties_are_bounded_before_reserving() {
|
||||
let mut bytes = BINARY_MAGIC.to_vec();
|
||||
bytes.extend(7400u32.to_le_bytes());
|
||||
let count = 1000u32;
|
||||
let end = bytes.len() + 13 + 1 + count as usize * 2;
|
||||
bytes.extend((end as u32).to_le_bytes());
|
||||
bytes.extend(count.to_le_bytes());
|
||||
bytes.extend((count * 2).to_le_bytes());
|
||||
bytes.push(1);
|
||||
bytes.push(b'A');
|
||||
for _ in 0..count {
|
||||
bytes.extend([b'C', 1]);
|
||||
}
|
||||
let limits = Limits { max_data_bytes: 1024, ..Limits::default() };
|
||||
assert!(matches!(read_document(&bytes, &limits), Err(Error::TooLarge(_))));
|
||||
assert!(read_document(&bytes, &Limits::default()).is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fbx_invalid_material_slot_is_unassigned_and_unnamed_material_has_id() {
|
||||
let text = ASCII.replace("a: 0,1\n", "a: 0,99\n").replace("\"Material::Red\"", "\"\"");
|
||||
let scene = parse(text.as_bytes()).unwrap();
|
||||
assert!(scene.materials.iter().any(|m| m.name == "Material_400"));
|
||||
let parts = &scene.meshes[0].parts;
|
||||
assert!(parts.iter().any(|p| p.material.is_none()));
|
||||
assert!(parts.iter().any(|p| p.material.is_some()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cameras_keep_lens_and_model_hierarchy() {
|
||||
let text = r#"FBXHeaderExtension: { FBXVersion: 7400 }
|
||||
Objects: {
|
||||
Model: 1, "Model::Rig", "Null" { Properties70: { P: "Lcl Translation", "", "", "", 1,2,3 } }
|
||||
Model: 2, "Model::View", "Camera" { }
|
||||
NodeAttribute: 3, "NodeAttribute::Lens", "Camera" { Properties70: {
|
||||
P: "FocalLength", "", "", "", 50
|
||||
P: "FilmHeight", "", "", "", 1
|
||||
} }
|
||||
Model: 4, "Model::Ortho", "Camera" { Properties70: {
|
||||
P: "CameraProjectionType", "", "", "", 1
|
||||
P: "OrthoZoom", "", "", "", 3
|
||||
} }
|
||||
Model: 5, "Model::Producer Perspective", "Camera" { }
|
||||
}
|
||||
Connections: {
|
||||
C: "OO",2,1
|
||||
C: "OO",3,2
|
||||
}
|
||||
"#;
|
||||
let scene = parse(text.as_bytes()).unwrap();
|
||||
assert_eq!(scene.cameras.len(), 2);
|
||||
let crate::Projection::Perspective { yfov, .. } = scene.cameras[0].projection else { panic!("perspective") };
|
||||
assert!((yfov - 2.0 * (25.4_f64 / 100.0).atan()).abs() < 1e-12);
|
||||
assert!(matches!(scene.cameras[1].projection, crate::Projection::Orthographic { ymag: 3.0, .. }));
|
||||
let camera_node = scene.nodes.iter().find(|n| n.camera == Some(0)).unwrap();
|
||||
let forward = crate::transform_point(&camera_node.transform, [0.0, 0.0, -1.0]);
|
||||
assert!((forward[0] - 1.0).abs() < 1e-12);
|
||||
let rig = scene.nodes.iter().find(|n| n.name == "Rig").unwrap();
|
||||
assert!(rig.children.iter().any(|i| scene.nodes[*i].name == "View"));
|
||||
let limits = Limits { max_extras: 1, ..Limits::default() };
|
||||
assert!(matches!(read(text.as_bytes(), &limits, &mut no_external), Err(Error::TooLarge(_))));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn all_connected_and_unplaced_geometries_are_instances() {
|
||||
let geometry = |id| format!("Geometry: {id}, \"Geometry::Part{id}\", \"Mesh\" {{ Vertices: 0,0,0,1,0,0,0,1,0 PolygonVertexIndex: 0,1,-3 }}\n");
|
||||
let text = format!(
|
||||
"FBXHeaderExtension: {{ FBXVersion: 7400 }}\nObjects: {{\n{}{}{} Model: 10, \"Model::M\", \"Mesh\" {{}} }}\nConnections: {{ C: \"OO\",1,10 C: \"OO\",2,10 C: \"OO\",2,10 }}",
|
||||
geometry(1),
|
||||
geometry(2),
|
||||
geometry(3)
|
||||
);
|
||||
let scene = parse(text.as_bytes()).unwrap();
|
||||
assert_eq!(scene.meshes.len(), 3);
|
||||
assert_eq!(scene.instances(&Limits::default()).unwrap().len(), 3);
|
||||
assert_eq!(scene.roots.len(), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fbx_corner_colors_keep_seams_and_incomplete_layers_are_omitted() {
|
||||
let layer = r#"LayerElementColor: 0 {
|
||||
MappingInformationType: "ByPolygonVertex"
|
||||
ReferenceInformationType: "Direct"
|
||||
Colors: 1,0,0,1, 0,1,0,1, 0,0,1,1, 1,1,0,1, 0,1,0,1, 0,0,1,1
|
||||
}"#;
|
||||
let text = ASCII.replace("LayerElementNormal: 0 {", &format!("{layer}\n LayerElementNormal: 0 {{"));
|
||||
// Add a color for every corner of the original quad and triangle.
|
||||
let text = text.replace("0,0,1,1\n}", "0,0,1,1, 1,0,1,1\n}");
|
||||
let scene = parse(text.as_bytes()).unwrap();
|
||||
let mesh = &scene.meshes[0];
|
||||
let colors: Vec<_> = mesh.parts.iter().flat_map(|part| part.corners.iter()).map(|corner| mesh.colors[corner.position as usize]).collect();
|
||||
assert_eq!(colors.len(), 7);
|
||||
assert!(colors.contains(&[1.0, 0.0, 0.0, 1.0]));
|
||||
assert!(colors.contains(&[0.0, 1.0, 0.0, 1.0]));
|
||||
assert!(colors.contains(&[1.0, 0.0, 1.0, 1.0]));
|
||||
let incomplete = text.replace(", 1,0,1,1\n}", "\n}");
|
||||
assert!(parse(incomplete.as_bytes()).unwrap().meshes[0].colors.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ascii_budget_applies_while_reading_values_and_records() {
|
||||
let limits = Limits { max_data_bytes: 512, ..Limits::default() };
|
||||
let many_values = format!("Objects: {{\n A: {}\n}}", "1,".repeat(1000));
|
||||
assert!(matches!(read_document(many_values.as_bytes(), &limits), Err(Error::TooLarge(_))));
|
||||
let many_records = format!("Objects: {{\n{}\n}}", "A: {}\n".repeat(1000));
|
||||
assert!(matches!(read_document(many_records.as_bytes(), &limits), Err(Error::TooLarge(_))));
|
||||
let giant_string = format!("Objects: {{ A: \"{}\" }}", "x".repeat(1024));
|
||||
assert!(matches!(read_document(giant_string.as_bytes(), &limits), Err(Error::TooLarge(_))));
|
||||
// Empty punctuation and comments do not consume memory in proportion to their length.
|
||||
let ignored = format!(";{}\nObjects: {{}}", ",".repeat(10_000));
|
||||
assert!(read_document(ignored.as_bytes(), &limits).is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn strict_ascii_reports_unbalanced_and_unterminated_records() {
|
||||
let strict = |s: &str| crate::fbx::read_document_strict(s.as_bytes(), &Limits::default());
|
||||
assert!(strict(ASCII).is_ok());
|
||||
for s in ["Objects: { A: 1", "Objects: { } }", "Objects: { A: \"unterminated", "Objects: { A: 1 2 }"] {
|
||||
assert!(strict(s).is_err(), "{s}");
|
||||
}
|
||||
assert!(read_document(b"Objects: { A: 1", &Limits::default()).is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn strict_binary_requires_a_closing_record() {
|
||||
let doc = document(ASCII.as_bytes());
|
||||
let bytes = write_binary(&doc.records, 7400, false);
|
||||
assert!(crate::fbx::read_document_strict(&bytes, &Limits::default()).is_ok());
|
||||
let truncated = &bytes[..bytes.len() - 29];
|
||||
assert!(read_document(truncated, &Limits::default()).is_ok());
|
||||
assert!(crate::fbx::read_document_strict(truncated, &Limits::default()).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn diffuse_materials_preserve_factors_and_texture_candidates() {
|
||||
let text = with(
|
||||
"Texture: 600, \"Texture::Base\", \"\" { RelativeFilename: \"maps/base.png\" FileName: \"C:/a/base.png\" }\n",
|
||||
"C: \"OP\",600,400,\"Maya|baseColor\"\n",
|
||||
);
|
||||
let doc = document(text.as_bytes());
|
||||
let mut asked = Vec::new();
|
||||
let materials = doc
|
||||
.diffuse_materials(&Limits::default(), &mut |paths| {
|
||||
asked.push(paths.iter().map(|s| s.to_string()).collect::<Vec<_>>());
|
||||
Some(b"image".to_vec())
|
||||
})
|
||||
.unwrap();
|
||||
assert_eq!(asked, vec![vec!["maps/base.png", "base.png"]]);
|
||||
let red = materials.iter().find(|m| m.name == "Red").unwrap();
|
||||
assert_eq!(red.factor, [1.0, 0.0, 0.0, 1.0]);
|
||||
assert_eq!(red.image.as_deref(), Some(&b"image"[..]));
|
||||
let small = Limits { max_data_bytes: 4, ..Limits::default() };
|
||||
assert!(matches!(doc.diffuse_materials(&small, &mut |_| Some(b"image".to_vec())), Err(Error::TooLarge(_))));
|
||||
let embedded_text = with(
|
||||
&format!("Texture: 600, \"Texture::Base\", \"\" {{}}\nVideo: 700, \"Video::Base\", \"Clip\" {{ Content: \"{}\" }}\n", b64(b"embedded")),
|
||||
"C: \"OP\",600,400,\"DiffuseColor\"\nC: \"OO\",700,600\n",
|
||||
);
|
||||
let embedded = document(embedded_text.as_bytes()).diffuse_materials(&Limits::default(), &mut |_| None).unwrap();
|
||||
assert_eq!(embedded.iter().find(|m| m.name == "Red").unwrap().image.as_deref(), Some(&b"embedded"[..]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn per_corner_and_per_vertex_material_ids_use_each_polygons_first_corner() {
|
||||
let by_corner = ASCII.replace("MappingInformationType: \"ByPolygon\"\n\t\t\tReferenceInformationType: \"IndexToDirect\"\n\t\t\tMaterials: *2 {\n\t\t\t\ta: 0,1", "MappingInformationType: \"ByPolygonVertex\"\n\t\t\tReferenceInformationType: \"IndexToDirect\"\n\t\t\tMaterials: *7 {\n\t\t\t\ta: 0,0,0,0,1,1,1");
|
||||
assert_ne!(by_corner, ASCII);
|
||||
let parts = &parse(by_corner.as_bytes()).unwrap().meshes[0].parts;
|
||||
assert_eq!(parts.len(), 2, "the triangle takes material 1 from its first corner");
|
||||
// The triangle starts at control point 4, whose material is 1.
|
||||
let by_vertex = ASCII.replace("a: 0,1,2,-4,0,1,-5", "a: 0,1,2,-4,4,0,-2").replace(
|
||||
"MappingInformationType: \"ByPolygon\"\n\t\t\tReferenceInformationType: \"IndexToDirect\"\n\t\t\tMaterials: *2 {\n\t\t\t\ta: 0,1",
|
||||
"MappingInformationType: \"ByControlPoint\"\n\t\t\tReferenceInformationType: \"IndexToDirect\"\n\t\t\tMaterials: *5 {\n\t\t\t\ta: 0,0,0,0,1",
|
||||
);
|
||||
let scene = parse(by_vertex.as_bytes()).unwrap();
|
||||
assert_eq!(scene.meshes[0].parts.len(), 2);
|
||||
assert!(scene.meshes[0].parts.iter().all(|p| p.material.is_some()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn strict_colors_validate_and_read_the_same_layer() {
|
||||
let good = "LayerElementColor: 0 { MappingInformationType: \"AllSame\" ReferenceInformationType: \"Direct\" Colors: 0,1,0,1 }\n";
|
||||
let bad = "LayerElementColor: 1 { MappingInformationType: \"AllSame\" ReferenceInformationType: \"Direct\" Colors: \"bad\",1,0,0,1 }\n";
|
||||
let text = ASCII.replace("LayerElementNormal: 0 {", &format!("{bad} {good} LayerElementNormal: 0 {{"));
|
||||
let options = crate::fbx::SceneOptions { strict_geometry: true, ..Default::default() };
|
||||
let scene = document(text.as_bytes()).scene_with_options(&Limits::default(), &mut no_external, &options).unwrap();
|
||||
assert!(scene.meshes[0].colors.iter().all(|c| *c == [0.0, 1.0, 0.0, 1.0]));
|
||||
assert!(!scene.meshes[0].colors.is_empty());
|
||||
let only_bad = ASCII.replace("LayerElementNormal: 0 {", &format!("{} LayerElementNormal: 0 {{", bad.replace(": 1 {", ": 0 {")));
|
||||
assert!(document(only_bad.as_bytes()).scene_with_options(&Limits::default(), &mut no_external, &options).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn strict_geometry_validates_the_colors_index_alias() {
|
||||
let layer = |index: &str| {
|
||||
format!(
|
||||
"LayerElementColor: 0 {{ MappingInformationType: \"AllSame\" ReferenceInformationType: \"IndexToDirect\" Colors: 0,1,0,1 ColorsIndex: {index} }}\n"
|
||||
)
|
||||
};
|
||||
let with = |index: &str| ASCII.replace("LayerElementNormal: 0 {", &format!("{} LayerElementNormal: 0 {{", layer(index)));
|
||||
let options = crate::fbx::SceneOptions { strict_geometry: true, ..Default::default() };
|
||||
assert!(document(with("0").as_bytes()).scene_with_options(&Limits::default(), &mut no_external, &options).is_ok());
|
||||
assert!(document(with("\"bad\",0").as_bytes()).scene_with_options(&Limits::default(), &mut no_external, &options).is_err());
|
||||
assert!(document(with("0.5").as_bytes()).scene_with_options(&Limits::default(), &mut no_external, &options).is_err(), "a fractional index");
|
||||
}
|
||||
Reference in new issue
Block a user