mod anim;
mod deform;
mod geometry;
mod material;
mod records;
use std::collections::HashMap;
use std::io::Write;
use serde_json::{Map, Value};
use super::xform::{IDENTITY, M4, decompose, from_column_major, mul, rotation, scale, translate};
use crate::document::Document;
use crate::{Result, quat};
use records::{Node, p, p_f64, p_int, p_str, p_time, p_vec, props70};
type Trs = ([f64; 3], [f64; 4], [f64; 3]);
pub(super) const KTIME: f64 = 46_186_158_000.0;
pub(crate) fn write(doc: &Document, w: &mut dyn Write) -> Result<()> {
let mut s = Writer::new(doc);
s.scene()?;
let top = s.finish();
records::write_file(&top, w)
}
#[derive(Clone, Copy)]
struct Turn {
q: [f64; 4],
perm: [usize; 3],
}
const CAMERA_TURN: Turn = Turn {
q: [
0.0,
std::f64::consts::FRAC_1_SQRT_2,
0.0,
std::f64::consts::FRAC_1_SQRT_2,
],
perm: [2, 1, 0],
};
const LIGHT_TURN: Turn = Turn {
q: [
std::f64::consts::FRAC_1_SQRT_2,
0.0,
0.0,
std::f64::consts::FRAC_1_SQRT_2,
],
perm: [0, 2, 1],
};
struct Model {
id: i64,
name: String,
parent: Option<usize>,
t: [f64; 3],
r: [f64; 4],
s: [f64; 3],
world: M4,
turn: Option<Turn>,
back: Option<[f64; 4]>,
joint: bool,
}
impl Model {
fn fbx_trs(&self, t: [f64; 3], r: [f64; 4], s: [f64; 3]) -> Trs {
let (mut t, mut r, mut s) = (t, r, s);
if let Some(b) = self.back {
t = quat::rotate(b, t);
r = quat::mul(b, r);
}
if let Some(turn) = self.turn {
r = quat::mul(r, turn.q);
s = turn.perm.map(|i| s[i]);
}
(t, r, s)
}
}
struct Writer<'d> {
doc: &'d Document,
json: &'d Map<String, Value>,
next_id: i64,
objects: Vec<Node>,
links: Vec<Node>,
counts: Vec<(&'static str, usize)>,
models: Vec<Option<Model>>,
materials: HashMap<Option<usize>, i64>,
textures: HashMap<String, i64>,
videos: HashMap<usize, Option<i64>>,
shared_geometry: HashMap<usize, Option<geometry::Geom>>,
channels: HashMap<usize, Vec<i64>>,
takes: Vec<(String, i64, i64)>,
fps: f64,
span: Option<(i64, i64)>,
}
fn list<'a>(json: &'a Map<String, Value>, key: &str) -> &'a [Value] {
json.get(key).and_then(Value::as_array).map_or(&[], Vec::as_slice)
}
fn number(v: &Value, key: &str) -> Option<f64> {
v.get(key).and_then(Value::as_f64)
}
fn numbers<const N: usize>(v: &Value, key: &str, default: [f64; N]) -> [f64; N] {
v.get(key)
.and_then(Value::as_array)
.filter(|a| a.len() == N)
.map_or(default, |a| {
std::array::from_fn(|i| a[i].as_f64().filter(|x| x.is_finite()).unwrap_or(default[i]))
})
}
fn index(v: &Value, key: &str) -> Option<usize> {
v.get(key).and_then(Value::as_u64).and_then(|i| usize::try_from(i).ok())
}
fn euler(q: [f64; 4]) -> [f64; 3] {
let m = quat::matrix(q);
let at = |r: usize, c: usize| m[c * 3 + r];
let sy = (-at(2, 0)).clamp(-1.0, 1.0);
let y = sy.asin();
let (x, z) = if at(2, 0).abs() < 1.0 - 1e-12 {
(at(2, 1).atan2(at(2, 2)), at(1, 0).atan2(at(0, 0)))
} else {
((-at(1, 2)).atan2(at(1, 1)), 0.0)
};
[x, y, z].map(f64::to_degrees)
}
fn to_srgb(c: f64) -> f64 {
let c = c.clamp(0.0, 1.0);
if c <= 0.003_130_8 {
c * 12.92
} else {
1.055 * c.powf(1.0 / 2.4) - 0.055
}
}
fn fbx_name(name: &str, class: &str) -> String {
let name = name.replace('\0', "");
format!("{name}\0\x01{class}")
}
fn node_trs(node: &Value) -> Trs {
if let Some(m) = node.get("matrix").and_then(Value::as_array).filter(|m| m.len() == 16) {
let flat: [f64; 16] = std::array::from_fn(|i| m[i].as_f64().unwrap_or(0.0));
let d = decompose(&from_column_major(&flat));
return (d.t, d.r, d.s);
}
let r = numbers(node, "rotation", [0.0, 0.0, 0.0, 1.0]);
let r = quat::unit_above(r, 1e-12).unwrap_or(quat::IDENTITY);
(
numbers(node, "translation", [0.0; 3]),
r,
numbers(node, "scale", [1.0; 3]),
)
}
fn trs_matrix((t, r, s): Trs) -> M4 {
mul(&translate(t), &mul(&rotation(r), &scale(s)))
}
impl<'d> Writer<'d> {
fn new(doc: &'d Document) -> Self {
Self {
doc,
json: doc.json(),
next_id: 1_000_000,
objects: Vec::new(),
links: Vec::new(),
counts: Vec::new(),
models: Vec::new(),
materials: HashMap::new(),
textures: HashMap::new(),
videos: HashMap::new(),
shared_geometry: HashMap::new(),
channels: HashMap::new(),
takes: Vec::new(),
fps: 30.0,
span: None,
}
}
fn id(&mut self) -> i64 {
self.next_id += 1;
self.next_id
}
fn object(&mut self, class: &'static str, node: Node) {
match self.counts.iter_mut().find(|(c, _)| *c == class) {
Some((_, n)) => *n += 1,
None => self.counts.push((class, 1)),
}
self.objects.push(node);
}
fn oo(&mut self, child: i64, parent: i64) {
self.links.push(Node::new("C").str("OO").i64(child).i64(parent));
}
fn op(&mut self, child: i64, parent: i64, prop: &str) {
self.links
.push(Node::new("C").str("OP").i64(child).i64(parent).str(prop));
}
fn written_nodes(&self) -> Vec<bool> {
let nodes = list(self.json, "nodes");
let mut keep = vec![false; nodes.len()];
let scenes = list(self.json, "scenes");
let scene = self
.json
.get("scene")
.and_then(Value::as_u64)
.and_then(|s| usize::try_from(s).ok())
.or((!scenes.is_empty()).then_some(0));
let mut roots: Vec<usize> = if let Some(s) = scene.and_then(|s| scenes.get(s)) {
s.get("nodes")
.and_then(Value::as_array)
.into_iter()
.flatten()
.filter_map(|v| v.as_u64().and_then(|i| usize::try_from(i).ok()))
.collect()
} else {
let mut child = vec![false; nodes.len()];
for n in nodes {
for c in children(n) {
if let Some(c) = child.get_mut(c) {
*c = true;
}
}
}
(0..nodes.len()).filter(|&i| !child[i]).collect()
};
let mark = |roots: &[usize], keep: &mut Vec<bool>| {
let mut stack: Vec<usize> = roots.to_vec();
while let Some(n) = stack.pop() {
if n >= keep.len() || keep[n] {
continue;
}
keep[n] = true;
stack.extend(children(&nodes[n]));
}
};
mark(&roots, &mut keep);
let parents = parents(nodes);
let mut more = Vec::new();
for (i, n) in nodes.iter().enumerate() {
if !keep[i] {
continue;
}
let Some(skin) = index(n, "skin").and_then(|s| list(self.json, "skins").get(s)) else {
continue;
};
for j in skin.get("joints").and_then(Value::as_array).into_iter().flatten() {
let Some(mut j) = j.as_u64().and_then(|j| usize::try_from(j).ok()) else {
continue;
};
if j >= nodes.len() || keep[j] {
continue;
}
let mut steps = 0;
while let Some(&p) = parents.get(j).and_then(Option::as_ref) {
if keep[p] || steps > nodes.len() {
break;
}
j = p;
steps += 1;
}
more.push(j);
}
}
roots.clear();
mark(&more, &mut keep);
keep
}
fn scene(&mut self) -> Result<()> {
let nodes = list(self.json, "nodes");
let keep = self.written_nodes();
let parents = parents(nodes);
let mut joint = vec![false; nodes.len()];
for skin in list(self.json, "skins") {
for j in skin.get("joints").and_then(Value::as_array).into_iter().flatten() {
if let Some(j) = j.as_u64().and_then(|j| usize::try_from(j).ok())
&& let Some(f) = joint.get_mut(j)
{
*f = true;
}
}
}
self.models = (0..nodes.len()).map(|_| None).collect();
for (i, node) in nodes.iter().enumerate() {
if !keep[i] {
continue;
}
let (t, r, s) = node_trs(node);
let parent = parents[i].filter(|&p| keep[p]);
let id = self.id();
let name = node
.get("name")
.and_then(Value::as_str)
.filter(|n| !n.is_empty())
.map_or_else(|| format!("node_{i}"), str::to_owned);
self.models[i] = Some(Model {
id,
name,
parent,
t,
r,
s,
world: IDENTITY,
turn: self.turn_of(i, joint[i]),
back: None,
joint: joint[i],
});
}
let order = topological(&self.models);
for &i in &order {
let Some(m) = &self.models[i] else { continue };
let parent = m.parent;
let local = trs_matrix((m.t, m.r, m.s));
let (pw, back) = parent
.and_then(|p| self.models[p].as_ref())
.map_or((IDENTITY, None), |p| (p.world, p.turn.map(|t| quat::conjugate(t.q))));
let m = self.models[i].as_mut().expect("written above");
m.world = mul(&pw, &local);
m.back = back;
}
for i in 0..nodes.len() {
if self.models[i].is_some() {
self.model(i);
}
}
for i in 0..nodes.len() {
if self.models[i].is_some() {
self.content(i)?;
}
}
if nodes.is_empty() {
for (mesh, m) in list(self.json, "meshes").iter().enumerate() {
let name = m
.get("name")
.and_then(Value::as_str)
.filter(|n| !n.is_empty())
.map_or_else(|| format!("mesh_{mesh}"), str::to_owned);
if let Some(g) = self.geometry(mesh)? {
let model = self.helper(0, &name, "Mesh", ([0.0; 3], quat::IDENTITY, [1.0; 3]));
self.draw(&g, model, &name);
}
}
}
self.animations()?;
Ok(())
}
fn turn_of(&self, i: usize, joint: bool) -> Option<Turn> {
let node = &list(self.json, "nodes")[i];
if joint || node.get("mesh").is_some() || node.get("skin").is_some() {
return None;
}
if index(node, "camera").is_some_and(|c| c < list(self.json, "cameras").len()) {
Some(CAMERA_TURN)
} else if self.light_of(node).is_some() {
Some(LIGHT_TURN)
} else {
None
}
}
fn light_of(&self, node: &Value) -> Option<&'d Value> {
let l = node.pointer("/extensions/KHR_lights_punctual/light")?.as_u64()?;
self.json
.get("extensions")?
.get("KHR_lights_punctual")?
.get("lights")?
.as_array()?
.get(usize::try_from(l).ok()?)
}
fn model(&mut self, i: usize) {
let node = &list(self.json, "nodes")[i];
let m = self.models[i].as_ref().expect("a written node");
let kind = if m.joint {
"LimbNode"
} else if node.get("mesh").is_some() {
"Mesh"
} else if m.turn.is_some_and(|t| t.perm == CAMERA_TURN.perm) {
"Camera"
} else if m.turn.is_some() {
"Light"
} else {
"Null"
};
let (t, r, s) = m.fbx_trs(m.t, m.r, m.s);
let hidden = node.pointer("/extensions/KHR_node_visibility/visible") == Some(&Value::Bool(false));
let mut props = vec![
p_int("RotationActive", "bool", 1),
p_int("InheritType", "enum", 1),
p_vec("ScalingMax", "Vector3D", "Vector", "", [0.0; 3]),
p_int("DefaultAttributeIndex", "int", 0),
p_vec("Lcl Translation", "Lcl Translation", "", "A", t),
p_vec("Lcl Rotation", "Lcl Rotation", "", "A", euler(r)),
p_vec("Lcl Scaling", "Lcl Scaling", "", "A", s),
];
if hidden {
props.push(p_f64("Visibility", "Visibility", "A", 0.0));
}
if let Some(extras) = node.get("extras").and_then(Value::as_object) {
user_props(extras, &mut props);
}
let (id, name, parent) = (m.id, fbx_name(&m.name, "Model"), m.parent);
let model = Node::new("Model")
.i64(id)
.str(&name)
.str(kind)
.child("Version", |n| n.i32(232))
.with(props70(props))
.child("Shading", |n| n.bool(true))
.child("Culling", |n| n.str("CullingOff"));
self.object("Model", model);
let parent = parent.and_then(|p| self.models[p].as_ref()).map_or(0, |p| p.id);
self.oo(id, parent);
if self.models[i].as_ref().is_some_and(|m| m.joint) {
let attr = self.id();
let n = Node::new("NodeAttribute")
.i64(attr)
.str(&fbx_name(
&self.models[i].as_ref().expect("written").name,
"NodeAttribute",
))
.str("LimbNode")
.with(props70(vec![p_f64("Size", "double", "", 100.0)]))
.child("TypeFlags", |n| n.str("Skeleton"));
self.object("NodeAttribute", n);
self.oo(attr, id);
}
}
fn content(&mut self, i: usize) -> Result<()> {
let node = &list(self.json, "nodes")[i];
let model = self.models[i].as_ref().expect("a written node").id;
if let Some(mesh) = index(node, "mesh").filter(|&m| m < list(self.json, "meshes").len()) {
self.mesh(i, mesh, model)?;
}
self.attributes(i, model);
Ok(())
}
fn helper(&mut self, parent: i64, name: &str, kind: &str, (t, r, s): Trs) -> i64 {
let id = self.id();
let props = vec![
p_int("RotationActive", "bool", 1),
p_int("InheritType", "enum", 1),
p_int("DefaultAttributeIndex", "int", 0),
p_vec("Lcl Translation", "Lcl Translation", "", "A", t),
p_vec("Lcl Rotation", "Lcl Rotation", "", "A", euler(r)),
p_vec("Lcl Scaling", "Lcl Scaling", "", "A", s),
];
let model = Node::new("Model")
.i64(id)
.str(&fbx_name(name, "Model"))
.str(kind)
.child("Version", |n| n.i32(232))
.with(props70(props))
.child("Shading", |n| n.bool(true))
.child("Culling", |n| n.str("CullingOff"));
self.object("Model", model);
self.oo(id, parent);
id
}
fn mesh(&mut self, i: usize, mesh: usize, model: i64) -> Result<()> {
let node = &list(self.json, "nodes")[i];
let mesh_json = &list(self.json, "meshes")[mesh];
let skinned = index(node, "skin").is_some_and(|s| s < list(self.json, "skins").len());
let morphed = mesh_json
.get("primitives")
.and_then(Value::as_array)
.into_iter()
.flatten()
.any(|p| {
p.get("targets")
.and_then(Value::as_array)
.is_some_and(|t| !t.is_empty())
});
let geom = if skinned || morphed {
self.geometry(mesh)?
} else if let Some(g) = self.shared_geometry.get(&mesh) {
g.clone()
} else {
let g = self.geometry(mesh)?;
self.shared_geometry.insert(mesh, g.clone());
g
};
let Some(g) = geom else {
return Ok(());
};
let name = self.models[i].as_ref().expect("written").name.clone();
let hosts = match self.instances(node)? {
Some(instances) if !skinned => instances
.into_iter()
.enumerate()
.map(|(k, trs)| self.helper(model, &format!("{name}_instance_{k}"), "Mesh", trs))
.collect(),
_ => vec![model],
};
for &host in &hosts {
self.draw(&g, host, &name);
}
if g.id.is_some() {
if skinned {
self.skin(i, &g)?;
}
if morphed {
self.morphs(i, mesh, &g)?;
}
}
Ok(())
}
fn draw(&mut self, g: &geometry::Geom, host: i64, name: &str) {
if let Some(gid) = g.id {
self.oo(gid, host);
for slot in &g.slots {
let m = self.material(*slot);
self.oo(m, host);
}
}
if let Some((line, mat)) = g.lines {
let host = if g.id.is_some() {
self.helper(
host,
&format!("{name}_lines"),
"Mesh",
([0.0; 3], quat::IDENTITY, [1.0; 3]),
)
} else {
host
};
self.oo(line, host);
let m = self.material(mat);
self.oo(m, host);
}
}
fn instances(&self, node: &Value) -> Result<Option<Vec<Trs>>> {
let Some(attrs) = node.pointer("/extensions/EXT_mesh_gpu_instancing/attributes") else {
return Ok(None);
};
let read = |key: &str, width: usize| -> Result<Option<Vec<f64>>> {
let Some(a) = index(attrs, key) else { return Ok(None) };
let (w, v) = self.doc.read_floats(a)?;
Ok((w == width).then(|| v.into_iter().map(f64::from).collect()))
};
let (t, r, s) = (read("TRANSLATION", 3)?, read("ROTATION", 4)?, read("SCALE", 3)?);
let count = [
t.as_ref().map(|v| v.len() / 3),
r.as_ref().map(|v| v.len() / 4),
s.as_ref().map(|v| v.len() / 3),
]
.into_iter()
.flatten()
.min();
let Some(count) = count else { return Ok(None) };
let at = |v: &Option<Vec<f64>>, k: usize, default: &[f64]| -> Vec<f64> {
v.as_ref().map_or_else(
|| default.to_vec(),
|v| v[k * default.len()..(k + 1) * default.len()].to_vec(),
)
};
Ok(Some(
(0..count)
.map(|k| {
let t = at(&t, k, &[0.0; 3]);
let r = at(&r, k, &quat::IDENTITY);
let s = at(&s, k, &[1.0; 3]);
(
[t[0], t[1], t[2]],
quat::unit_above([r[0], r[1], r[2], r[3]], 0.0).unwrap_or(quat::IDENTITY),
[s[0], s[1], s[2]],
)
})
.collect(),
))
}
fn attributes(&mut self, i: usize, model: i64) {
let node = &list(self.json, "nodes")[i];
let m = self.models[i].as_ref().expect("a written node");
let (name, turn) = (m.name.clone(), m.turn.map(|t| t.perm));
if let Some(cam) = index(node, "camera").and_then(|c| list(self.json, "cameras").get(c)) {
let host = if turn == Some(CAMERA_TURN.perm) {
model
} else {
self.helper(
model,
&format!("{name}_camera"),
"Camera",
([0.0; 3], CAMERA_TURN.q, [1.0; 3]),
)
};
let attr = self.id();
self.object("NodeAttribute", camera(attr, own_name(cam, &name), cam));
self.oo(attr, host);
}
if let Some(light) = self.light_of(node) {
let host = if turn == Some(LIGHT_TURN.perm) {
model
} else {
self.helper(
model,
&format!("{name}_light"),
"Light",
([0.0; 3], LIGHT_TURN.q, [1.0; 3]),
)
};
let attr = self.id();
self.object("NodeAttribute", light_attribute(attr, own_name(light, &name), light));
self.oo(attr, host);
}
}
fn finish(mut self) -> Vec<Node> {
let mut top = vec![
self.header(7400),
Node::new("FileId").raw(&[
0x28, 0xb3, 0x2a, 0xeb, 0xb6, 0x24, 0xcc, 0xc2, 0xbf, 0xc8, 0xb0, 0x2a, 0xa9, 0x2b, 0xfc, 0xf1,
]),
Node::new("CreationTime").str("1970-01-01 10:00:00:000"),
Node::new("Creator").str(&creator()),
self.global_settings(),
self.documents(),
Node::new("References"),
self.definitions(),
Node::new("Objects"),
Node::new("Connections"),
self.takes_node(),
];
let objects = std::mem::take(&mut self.objects);
let links = std::mem::take(&mut self.links);
for o in objects {
top[8].push(o);
}
for l in links {
top[9].push(l);
}
if records::wide(&top) {
top[0] = self.header(7500);
}
top
}
fn header(&self, version: i32) -> Node {
let source = self.json.get("asset").and_then(|a| a.get("extras"));
let meta = |key: &str| {
source
.and_then(|s| s.get(key))
.and_then(Value::as_str)
.unwrap_or("")
.to_owned()
};
let creator = creator();
let app = env!("CARGO_PKG_VERSION");
let date = "01/01/1970 10:00:00.000";
Node::new("FBXHeaderExtension")
.child("FBXHeaderVersion", |n| n.i32(1003))
.child("FBXVersion", |n| n.i32(version))
.child("EncryptionType", |n| n.i32(0))
.with(
Node::new("CreationTimeStamp")
.child("Version", |n| n.i32(1000))
.child("Year", |n| n.i32(1970))
.child("Month", |n| n.i32(1))
.child("Day", |n| n.i32(1))
.child("Hour", |n| n.i32(10))
.child("Minute", |n| n.i32(0))
.child("Second", |n| n.i32(0))
.child("Millisecond", |n| n.i32(0)),
)
.child("Creator", |n| n.str(&creator))
.with(
Node::new("SceneInfo")
.str("GlobalInfo\0\x01SceneInfo")
.str("UserData")
.child("Type", |n| n.str("UserData"))
.child("Version", |n| n.i32(100))
.with(
Node::new("MetaData")
.child("Version", |n| n.i32(100))
.child("Title", |n| n.str(&meta("title")))
.child("Subject", |n| n.str(&meta("subject")))
.child("Author", |n| n.str(&meta("author")))
.child("Keywords", |n| n.str(&meta("keywords")))
.child("Revision", |n| n.str(&meta("revision")))
.child("Comment", |n| n.str(&meta("comment"))),
)
.with(props70(vec![
p("DocumentUrl", "KString", "Url", "").str("scene.fbx"),
p("SrcDocumentUrl", "KString", "Url", "").str("scene.fbx"),
p("Original", "Compound", "", ""),
p_str("Original|ApplicationVendor", ""),
p_str("Original|ApplicationName", "polysmelt"),
p_str("Original|ApplicationVersion", app),
p("Original|DateTime_GMT", "DateTime", "", "").str(date),
p_str("Original|FileName", "scene.fbx"),
p("LastSaved", "Compound", "", ""),
p_str("LastSaved|ApplicationVendor", ""),
p_str("LastSaved|ApplicationName", "polysmelt"),
p_str("LastSaved|ApplicationVersion", app),
p("LastSaved|DateTime_GMT", "DateTime", "", "").str(date),
])),
)
}
fn global_settings(&self) -> Node {
let (mode, custom) = anim::time_mode(self.fps);
let (start, stop) = self.span.unwrap_or((0, anim::ktime(10.0 / self.fps)));
Node::new("GlobalSettings")
.child("Version", |n| n.i32(1000))
.with(props70(vec![
p_int("UpAxis", "int", 1),
p_int("UpAxisSign", "int", 1),
p_int("FrontAxis", "int", 2),
p_int("FrontAxisSign", "int", 1),
p_int("CoordAxis", "int", 0),
p_int("CoordAxisSign", "int", 1),
p_int("OriginalUpAxis", "int", 1),
p_int("OriginalUpAxisSign", "int", 1),
p_f64("UnitScaleFactor", "double", "", 100.0),
p_f64("OriginalUnitScaleFactor", "double", "", 100.0),
p_vec("AmbientColor", "ColorRGB", "Color", "", [0.0; 3]),
p_str("DefaultCamera", "Producer Perspective"),
p_int("TimeMode", "enum", mode),
p_int("TimeProtocol", "enum", 2),
p_int("SnapOnFrameMode", "enum", 0),
p_time("TimeSpanStart", start),
p_time("TimeSpanStop", stop),
p_f64("CustomFrameRate", "double", "", custom),
p("TimeMarker", "Compound", "", ""),
p_int("CurrentTimeMarker", "int", -1),
]))
}
fn documents(&self) -> Node {
let active = self.takes.first().map_or("", |t| t.0.as_str());
Node::new("Documents").child("Count", |n| n.i32(1)).with(
Node::new("Document")
.i64(1_000_000)
.str("")
.str("Scene")
.with(props70(vec![
p("SourceObject", "object", "", ""),
p_str("ActiveAnimStackName", active),
]))
.child("RootNode", |n| n.i64(0)),
)
}
fn definitions(&self) -> Node {
let total: usize = self.counts.iter().map(|c| c.1).sum::<usize>() + 1;
let mut defs = Node::new("Definitions")
.child("Version", |n| n.i32(100))
.child("Count", |n| n.i32(i32::try_from(total).unwrap_or(i32::MAX)));
defs.push(
Node::new("ObjectType")
.str("GlobalSettings")
.child("Count", |n| n.i32(1)),
);
for &(class, count) in &self.counts {
let mut t = Node::new("ObjectType")
.str(class)
.child("Count", |n| n.i32(i32::try_from(count).unwrap_or(i32::MAX)));
if let Some((name, props)) = template(class) {
t.push(Node::new("PropertyTemplate").str(name).with(props70(props)));
}
defs.push(t);
}
defs
}
fn takes_node(&self) -> Node {
let mut n = Node::new("Takes").child("Current", |n| n.str(""));
for (name, start, stop) in &self.takes {
n.push(
Node::new("Take")
.str(name)
.child("FileName", |n| n.str(&format!("{}.tak", name.replace(' ', "_"))))
.child("LocalTime", |n| n.i64(*start).i64(*stop))
.child("ReferenceTime", |n| n.i64(*start).i64(*stop)),
);
}
n
}
}
fn camera(id: i64, name: &str, cam: &Value) -> Node {
const FILM_HEIGHT: f64 = 24.0 / 25.4;
let ortho = cam.get("type").and_then(Value::as_str) == Some("orthographic");
let o = cam.get("orthographic").unwrap_or(&Value::Null);
let p = cam.get("perspective").unwrap_or(&Value::Null);
let (aspect, near, far) = if ortho {
let (x, y) = (number(o, "xmag").unwrap_or(1.0), number(o, "ymag").unwrap_or(1.0));
let aspect = if x > 0.0 && y > 0.0 { x / y } else { 1.0 };
(
aspect,
number(o, "znear").unwrap_or(0.01),
number(o, "zfar").unwrap_or(1000.0),
)
} else {
let aspect = number(p, "aspectRatio").filter(|a| *a > 0.0).unwrap_or(16.0 / 9.0);
let near = number(p, "znear").filter(|n| *n > 0.0).unwrap_or(0.01);
(
aspect,
near,
number(p, "zfar").filter(|f| *f > near).unwrap_or(near * 1e7),
)
};
let yfov = number(p, "yfov")
.filter(|f| *f > 0.0 && *f < std::f64::consts::PI)
.unwrap_or(0.8);
let focal = FILM_HEIGHT * 25.4 / 2.0 / (yfov / 2.0).tan();
let mut props = vec![
p_int("CameraProjectionType", "enum", i32::from(ortho)),
p_int("ApertureMode", "enum", 2),
p_f64("FieldOfView", "FieldOfView", "A", yfov.to_degrees()),
p_f64("FocalLength", "Number", "A", focal),
p_f64("FilmWidth", "Number", "", FILM_HEIGHT * aspect),
p_f64("FilmHeight", "Number", "", FILM_HEIGHT),
p_f64("FilmAspectRatio", "double", "", aspect),
p_int("AspectRatioMode", "enum", 0),
p_f64("AspectWidth", "double", "", aspect),
p_f64("AspectHeight", "double", "", 1.0),
p_f64("NearPlane", "double", "", near),
p_f64("FarPlane", "double", "", far),
];
if ortho {
props.push(p_f64("OrthoZoom", "double", "", number(o, "ymag").unwrap_or(1.0)));
}
if let Some(extras) = cam.get("extras").and_then(Value::as_object) {
user_props(extras, &mut props);
}
Node::new("NodeAttribute")
.i64(id)
.str(&fbx_name(name, "NodeAttribute"))
.str("Camera")
.with(props70(props))
.child("TypeFlags", |n| n.str("Camera"))
.child("GeometryVersion", |n| n.i32(124))
.child("Position", |n| n.f64(0.0).f64(0.0).f64(0.0))
.child("Up", |n| n.f64(0.0).f64(1.0).f64(0.0))
.child("LookAt", |n| n.f64(1.0).f64(0.0).f64(0.0))
.child("ShowInfoOnMoving", |n| n.i32(1))
.child("ShowAudio", |n| n.i32(0))
.child("AudioColor", |n| n.f64(0.0).f64(1.0).f64(0.0))
.child("CameraOrthoZoom", |n| n.f64(1.0))
}
fn own_name<'a>(item: &'a Value, node: &'a str) -> &'a str {
item.get("name")
.and_then(Value::as_str)
.filter(|n| !n.is_empty())
.unwrap_or(node)
}
fn light_attribute(id: i64, name: &str, light: &Value) -> Node {
let kind = match light.get("type").and_then(Value::as_str) {
Some("directional") => 1,
Some("spot") => 2,
_ => 0,
};
let color = numbers(light, "color", [1.0; 3]).map(to_srgb);
let mut props = vec![
p_int("LightType", "enum", kind),
p_int("CastLight", "bool", 1),
p_vec("Color", "Color", "", "A", color),
p_f64(
"Intensity",
"Number",
"A",
number(light, "intensity").unwrap_or(1.0) * 100.0,
),
p_int("DecayType", "enum", 2),
p_int("CastShadows", "bool", 1),
];
if kind == 2 {
let spot = light.get("spot").unwrap_or(&Value::Null);
let outer = number(spot, "outerConeAngle").unwrap_or(std::f64::consts::FRAC_PI_4);
let inner = number(spot, "innerConeAngle").unwrap_or(0.0);
props.push(p_f64("InnerAngle", "Number", "A", (inner * 2.0).to_degrees()));
props.push(p_f64("OuterAngle", "Number", "A", (outer * 2.0).to_degrees()));
}
let mut user = Map::new();
if let Some(range) = light.get("range") {
user.insert("range".to_owned(), range.clone());
}
if let Some(extras) = light.get("extras").and_then(Value::as_object) {
user.extend(extras.iter().map(|(k, v)| (k.clone(), v.clone())));
}
user_props(&user, &mut props);
Node::new("NodeAttribute")
.i64(id)
.str(&fbx_name(name, "NodeAttribute"))
.str("Light")
.with(props70(props))
.child("TypeFlags", |n| n.str("Light"))
.child("GeometryVersion", |n| n.i32(124))
}
fn creator() -> String {
concat!("polysmelt ", env!("CARGO_PKG_VERSION")).to_owned()
}
fn children(node: &Value) -> impl Iterator<Item = usize> + '_ {
node.get("children")
.and_then(Value::as_array)
.into_iter()
.flatten()
.filter_map(|v| v.as_u64().and_then(|i| usize::try_from(i).ok()))
}
fn parents(nodes: &[Value]) -> Vec<Option<usize>> {
let mut out = vec![None; nodes.len()];
for (i, n) in nodes.iter().enumerate() {
for c in children(n) {
if c < out.len() && out[c].is_none() && c != i {
out[c] = Some(i);
}
}
}
out
}
fn topological(models: &[Option<Model>]) -> Vec<usize> {
let mut depth = vec![usize::MAX; models.len()];
for i in 0..models.len() {
let mut chain = Vec::new();
let mut at = Some(i);
while let Some(n) = at {
if models[n].is_none() || depth[n] != usize::MAX || chain.len() > models.len() {
break;
}
chain.push(n);
at = models[n].as_ref().and_then(|m| m.parent);
}
let mut d = at.map_or(0, |n| if depth[n] == usize::MAX { 0 } else { depth[n] + 1 });
for &n in chain.iter().rev() {
if depth[n] == usize::MAX {
depth[n] = d;
d += 1;
}
}
}
let mut order: Vec<usize> = (0..models.len()).filter(|&i| models[i].is_some()).collect();
order.sort_by_key(|&i| (depth[i], i));
order
}
fn user_props(extras: &Map<String, Value>, out: &mut Vec<Node>) {
for (k, v) in extras {
let entry = match v {
Value::Bool(b) => p(k, "Bool", "", "A+U").i32(i32::from(*b)),
Value::Number(n) => match n.as_i64().and_then(|i| i32::try_from(i).ok()) {
Some(i) => p(k, "Integer", "", "A+U").i32(i),
None => p(k, "Number", "", "A+U").f64(n.as_f64().unwrap_or(0.0)),
},
Value::String(s) => p(k, "KString", "", "U").str(s),
Value::Array(a) if !a.is_empty() && a.len() <= 16 && a.iter().all(Value::is_number) => {
let mut e = p(k, if a.len() == 3 { "Vector" } else { "Number" }, "", "A+U");
for x in a {
e = e.f64(x.as_f64().unwrap_or(0.0));
}
e
}
Value::Null => continue,
other => p(k, "KString", "", "U").str(&other.to_string()),
};
out.push(entry);
}
}
fn template(class: &str) -> Option<(&'static str, Vec<Node>)> {
Some(match class {
"Model" => (
"FbxNode",
vec![
p_int("QuaternionInterpolate", "enum", 0),
p_vec("RotationOffset", "Vector3D", "Vector", "", [0.0; 3]),
p_vec("RotationPivot", "Vector3D", "Vector", "", [0.0; 3]),
p_vec("ScalingOffset", "Vector3D", "Vector", "", [0.0; 3]),
p_vec("ScalingPivot", "Vector3D", "Vector", "", [0.0; 3]),
p_vec("PreRotation", "Vector3D", "Vector", "", [0.0; 3]),
p_vec("PostRotation", "Vector3D", "Vector", "", [0.0; 3]),
p_int("RotationOrder", "enum", 0),
p_int("RotationActive", "bool", 0),
p_int("InheritType", "enum", 0),
p_vec("GeometricTranslation", "Vector3D", "Vector", "", [0.0; 3]),
p_vec("GeometricRotation", "Vector3D", "Vector", "", [0.0; 3]),
p_vec("GeometricScaling", "Vector3D", "Vector", "", [1.0; 3]),
p_vec("Lcl Translation", "Lcl Translation", "", "A", [0.0; 3]),
p_vec("Lcl Rotation", "Lcl Rotation", "", "A", [0.0; 3]),
p_vec("Lcl Scaling", "Lcl Scaling", "", "A", [1.0; 3]),
p_f64("Visibility", "Visibility", "A", 1.0),
p_int("Visibility Inheritance", "Visibility Inheritance", 1),
],
),
"Geometry" => (
"FbxMesh",
vec![
p_vec("Color", "ColorRGB", "Color", "", [0.8; 3]),
p_vec("BBoxMin", "Vector3D", "Vector", "", [0.0; 3]),
p_vec("BBoxMax", "Vector3D", "Vector", "", [0.0; 3]),
p_int("Primary Visibility", "bool", 1),
p_int("Casts Shadows", "bool", 1),
p_int("Receive Shadows", "bool", 1),
],
),
"Material" => (
"FbxSurfacePhong",
vec![
p_str("ShadingModel", "Phong"),
p_int("MultiLayer", "bool", 0),
p_vec("EmissiveColor", "Color", "", "A", [0.0; 3]),
p_f64("EmissiveFactor", "Number", "A", 1.0),
p_vec("AmbientColor", "Color", "", "A", [0.2; 3]),
p_f64("AmbientFactor", "Number", "A", 1.0),
p_vec("DiffuseColor", "Color", "", "A", [0.8; 3]),
p_f64("DiffuseFactor", "Number", "A", 1.0),
p_vec("TransparentColor", "Color", "", "A", [0.0; 3]),
p_f64("TransparencyFactor", "Number", "A", 0.0),
p_vec("SpecularColor", "Color", "", "A", [0.2; 3]),
p_f64("SpecularFactor", "Number", "A", 1.0),
p_f64("Shininess", "Number", "A", 20.0),
p_f64("ShininessExponent", "Number", "A", 20.0),
p_vec("ReflectionColor", "Color", "", "A", [0.0; 3]),
p_f64("ReflectionFactor", "Number", "A", 1.0),
],
),
"Texture" => (
"FbxFileTexture",
vec![
p_int("TextureTypeUse", "enum", 0),
p_f64("Texture alpha", "Number", "A", 1.0),
p_int("CurrentMappingType", "enum", 0),
p_int("WrapModeU", "enum", 0),
p_int("WrapModeV", "enum", 0),
p_int("UVSwap", "bool", 0),
p_int("PremultiplyAlpha", "bool", 1),
p_vec("Translation", "Vector", "", "A", [0.0; 3]),
p_vec("Rotation", "Vector", "", "A", [0.0; 3]),
p_vec("Scaling", "Vector", "", "A", [1.0; 3]),
p_int("UseMaterial", "bool", 0),
p_int("UseMipMap", "bool", 0),
p_int("CurrentTextureBlendMode", "enum", 1),
p_str("UVSet", "default"),
],
),
"Video" => ("FbxVideo", vec![p_str("Path", ""), p_int("ImageSequence", "bool", 0)]),
"AnimationStack" => (
"FbxAnimStack",
vec![
p_str("Description", ""),
p_time("LocalStart", 0),
p_time("LocalStop", 0),
p_time("ReferenceStart", 0),
p_time("ReferenceStop", 0),
],
),
"AnimationLayer" => (
"FbxAnimLayer",
vec![
p_f64("Weight", "Number", "A", 100.0),
p_int("Mute", "bool", 0),
p_int("Solo", "bool", 0),
p_int("Lock", "bool", 0),
p_vec("Color", "ColorRGB", "Color", "", [0.8; 3]),
p_int("BlendMode", "enum", 0),
p_int("RotationAccumulationMode", "enum", 0),
p_int("ScaleAccumulationMode", "enum", 0),
p("BlendModeBypass", "ULongLong", "", "").i64(0),
],
),
"AnimationCurveNode" => ("FbxAnimCurveNode", vec![p("d", "Compound", "", "")]),
_ => return None,
})
}
#[cfg(test)]
mod tests;