use std::collections::HashMap;
use serde_json::{Map, Value, json};
use crate::document::Document;
use crate::mesh::srgb_to_linear;
use crate::num::{f32_of, usize_of};
use crate::source::{Axis, Fact, Handedness, SourceInfo};
use crate::{Context as _, ErrorKind, Result};
pub(crate) fn is_v1(json: &Map<String, Value>) -> bool {
if ["meshes", "nodes", "accessors"]
.iter()
.any(|k| json.get(*k).is_some_and(Value::is_array))
{
return false;
}
match json.get("asset").and_then(|a| a.get("version")) {
Some(Value::String(v)) => v.starts_with('1') || v.starts_with("0."),
Some(Value::Number(n)) => n.as_f64().is_some_and(|v| v < 2.0),
Some(_) => false,
None => ["meshes", "nodes", "accessors"]
.iter()
.any(|k| json.get(*k).is_some_and(Value::is_object)),
}
}
#[derive(Default)]
pub(crate) struct Fixups {
joints: Vec<usize>,
bind_shapes: Vec<(usize, [f32; 16])>,
bounds: Vec<usize>,
}
struct Kind {
list: Vec<(String, Value)>,
at: HashMap<String, usize>,
}
impl Kind {
fn take(json: &mut Map<String, Value>, key: &str) -> Self {
let mut list: Vec<(String, Value)> = match json.remove(key) {
Some(Value::Object(m)) => m.into_iter().collect(),
Some(Value::Array(a)) => a.into_iter().enumerate().map(|(i, v)| (i.to_string(), v)).collect(),
_ => Vec::new(),
};
if key == "buffers"
&& let Some(i) = list.iter().position(|(id, _)| id == BINARY)
{
let b = list.remove(i);
list.insert(0, b);
}
let at = list.iter().enumerate().map(|(i, (id, _))| (id.clone(), i)).collect();
Self { list, at }
}
fn index(&self, v: Option<&Value>) -> Option<usize> {
self.at.get(v?.as_str()?).copied()
}
fn ids<'a>(&'a self, v: Option<&'a Value>) -> impl Iterator<Item = usize> + 'a {
v.and_then(Value::as_array)
.into_iter()
.flatten()
.filter_map(|id| self.index(Some(id)))
}
}
const BINARY: &str = "binary_glTF";
fn object(id: &str, v: &Value, source: Map<String, Value>) -> Map<String, Value> {
let mut o = Map::new();
o.insert("name".into(), v.get("name").cloned().unwrap_or_else(|| json!(id)));
let mut extras = v.get("extras").and_then(Value::as_object).cloned().unwrap_or_default();
if !source.is_empty() {
extras.insert("source".into(), Value::Object(source));
}
if !extras.is_empty() {
o.insert("extras".into(), Value::Object(extras));
}
o
}
fn copy(from: &Value, to: &mut Map<String, Value>, keys: &[&str]) {
for k in keys {
if let Some(v) = from.get(*k) {
to.insert((*k).into(), v.clone());
}
}
}
fn components(kind: &str) -> usize {
match kind {
"VEC2" => 2,
"VEC3" => 3,
"VEC4" | "MAT2" => 4,
"MAT3" => 9,
"MAT4" => 16,
_ => 1,
}
}
fn component_size(ctype: u64) -> usize {
match ctype {
5120 | 5121 => 1,
5122 | 5123 => 2,
_ => 4,
}
}
fn semantic(s: &str, kind: &str) -> String {
let (base, n) = match s.rsplit_once('_') {
Some((b, n)) if n.parse::<u32>().is_ok() => (b, n),
_ => (s, "0"),
};
match (base, n) {
("POSITION" | "NORMAL", "0") => base.into(),
("TANGENT", "0") if kind == "VEC4" => base.into(),
("TEXCOORD", _) => format!("TEXCOORD_{n}"),
("COLOR", _) => format!("COLOR_{n}"),
("JOINT" | "JOINTS", _) => format!("JOINTS_{n}"),
("WEIGHT" | "WEIGHTS", _) => format!("WEIGHTS_{n}"),
_ => format!("_{s}"),
}
}
#[allow(clippy::too_many_lines, reason = "one pass over each kind of object, in order")]
pub(crate) fn upgrade(mut v1: Map<String, Value>) -> Result<(Map<String, Value>, Fixups)> {
let buffers = Kind::take(&mut v1, "buffers");
let views = Kind::take(&mut v1, "bufferViews");
let accessors = Kind::take(&mut v1, "accessors");
let meshes = Kind::take(&mut v1, "meshes");
let nodes = Kind::take(&mut v1, "nodes");
let skins = Kind::take(&mut v1, "skins");
let cameras = Kind::take(&mut v1, "cameras");
let images = Kind::take(&mut v1, "images");
let samplers = Kind::take(&mut v1, "samplers");
let textures = Kind::take(&mut v1, "textures");
let materials = Kind::take(&mut v1, "materials");
let techniques = Kind::take(&mut v1, "techniques");
let animations = Kind::take(&mut v1, "animations");
let scenes = Kind::take(&mut v1, "scenes");
let mut fix = Fixups::default();
let mut used: Vec<&str> = Vec::new();
let mut source = Map::new();
let mut asset = match v1.remove("asset") {
Some(Value::Object(a)) => a,
_ => Map::new(),
};
let version = match asset.remove("version") {
Some(Value::String(v)) => v,
Some(Value::Number(n)) => n.to_string(),
_ => "1.0".to_owned(),
};
let text = |k: &str| asset.get(k).and_then(Value::as_str).map(str::to_owned);
SourceInfo {
format: Some("glTF".into()),
version: Some(version),
meters_per_unit: Fact::Declared(1.0),
unit: Some("meter".into()),
up_axis: Fact::Assumed(Axis::PosY),
handedness: Fact::Assumed(Handedness::Right),
tool: text("generator"),
copyright: text("copyright"),
..SourceInfo::default()
}
.write(&mut source);
for k in ["premultipliedAlpha", "profile"] {
if let Some(v) = asset.remove(k) {
source.insert(k.into(), v);
}
}
if !techniques.list.is_empty() {
source.insert(
"techniques".into(),
Value::Object(techniques.list.iter().cloned().collect()),
);
}
for k in [
"programs",
"shaders",
"extensionsUsed",
"extensionsRequired",
"glExtensionsUsed",
] {
if let Some(v) = v1.remove(k) {
source.insert(k.into(), v);
}
}
let mut root_ext = match v1.remove("extensions") {
Some(Value::Object(e)) => e,
_ => Map::new(),
};
let mut out = Map::new();
out.insert(
"buffers".into(),
buffers
.list
.iter()
.map(|(id, b)| {
let mut o = object(id, b, Map::new());
copy(b, &mut o, &["byteLength"]);
if id != BINARY {
copy(b, &mut o, &["uri"]);
}
Value::Object(o)
})
.collect(),
);
let mut out_views: Vec<Value> = views
.list
.iter()
.map(|(id, v)| {
let mut o = object(id, v, Map::new());
o.insert("buffer".into(), json!(buffers.index(v.get("buffer"))));
copy(v, &mut o, &["byteOffset", "byteLength", "target"]);
Value::Object(o)
})
.collect();
for (i, (id, v)) in views.list.iter().enumerate() {
ensure!(
out_views[i]["buffer"].is_u64(),
Corrupt,
"glTF 1.0 bufferView {id:?} names no buffer there is ({})",
v.get("buffer").unwrap_or(&Value::Null)
);
}
let mut strided: HashMap<(usize, u64), usize> = HashMap::new();
let mut out_accessors = Vec::with_capacity(accessors.list.len());
for (id, a) in &accessors.list {
let mut o = object(id, a, Map::new());
copy(
a,
&mut o,
&["byteOffset", "componentType", "count", "type", "max", "min"],
);
if let Some(view) = views.index(a.get("bufferView")) {
let kind = a.get("type").and_then(Value::as_str).unwrap_or("SCALAR");
let ctype = a.get("componentType").and_then(Value::as_u64).unwrap_or(5126);
let packed = (components(kind) * component_size(ctype)) as u64;
let stride = a.get("byteStride").and_then(Value::as_u64).unwrap_or(0);
let view = if stride == 0 || stride == packed {
view
} else {
*strided.entry((view, stride)).or_insert_with(|| {
let mut copy = out_views[view].clone();
copy["byteStride"] = json!(stride);
out_views.push(copy);
out_views.len() - 1
})
};
o.insert("bufferView".into(), json!(view));
}
out_accessors.push(Value::Object(o));
}
let mut out_meshes: Vec<Value> = Vec::with_capacity(meshes.list.len());
for (id, m) in &meshes.list {
let mut o = object(id, m, Map::new());
let prims: Vec<Value> = m
.get("primitives")
.and_then(Value::as_array)
.into_iter()
.flatten()
.map(|p| primitive(p, &accessors, &materials, &mut out_accessors, &mut fix))
.collect();
o.insert("primitives".into(), Value::Array(prims));
out_meshes.push(Value::Object(o));
}
let mut merged: HashMap<Vec<usize>, usize> = HashMap::new();
let mut children: Vec<Vec<usize>> = vec![Vec::new(); nodes.list.len()];
for (i, (_, n)) in nodes.list.iter().enumerate() {
children[i] = nodes.ids(n.get("children")).collect();
}
let joint_name = |i: usize| nodes.list[i].1.get("jointName").and_then(Value::as_str);
let find_joint = |roots: &[usize], name: &str| -> Option<usize> {
let mut stack: Vec<usize> = roots.to_vec();
if stack.is_empty() {
stack = (0..nodes.list.len()).rev().collect();
}
while let Some(i) = stack.pop() {
if joint_name(i) == Some(name) {
return Some(i);
}
stack.extend(children[i].iter().rev());
}
None
};
let mut out_skins: Vec<Value> = Vec::new();
let mut skin_of: HashMap<(usize, Vec<usize>), Option<usize>> = HashMap::new();
let mut out_nodes = Vec::with_capacity(nodes.list.len());
let mut lights_used = false;
for (i, (id, n)) in nodes.list.iter().enumerate() {
let mut src = Map::new();
for k in ["jointName", "skeletons", "skin", "instanceSkin"] {
if let Some(v) = n.get(k) {
src.insert(k.into(), v.clone());
}
}
let mut o = object(id, n, src);
if !children[i].is_empty() {
o.insert("children".into(), json!(children[i]));
}
copy(n, &mut o, &["matrix", "translation", "rotation", "scale"]);
if let Some(c) = cameras.index(n.get("camera")) {
o.insert("camera".into(), json!(c));
}
let skinned = n.get("instanceSkin").unwrap_or(n);
let list: Vec<usize> = meshes.ids(skinned.get("meshes").or_else(|| n.get("meshes"))).collect();
let mesh = match list.as_slice() {
[] => None,
[m] => Some(*m),
_ => Some(*merged.entry(list.clone()).or_insert_with(|| {
let prims: Vec<Value> = list
.iter()
.flat_map(|&m| out_meshes[m]["primitives"].as_array().cloned().unwrap_or_default())
.collect();
let name = list
.iter()
.filter_map(|&m| out_meshes[m]["name"].as_str())
.collect::<Vec<_>>()
.join("+");
out_meshes.push(json!({ "name": name, "primitives": prims }));
out_meshes.len() - 1
})),
};
if let Some(m) = mesh {
o.insert("mesh".into(), json!(m));
if let Some(s) = skins.index(skinned.get("skin")) {
let roots: Vec<usize> = nodes.ids(skinned.get("skeletons")).collect();
let skin = *skin_of.entry((s, roots.clone())).or_insert_with(|| {
let skin = &skins.list[s].1;
let joints: Option<Vec<usize>> = skin
.get("jointNames")
.and_then(Value::as_array)
.into_iter()
.flatten()
.map(|name| find_joint(&roots, name.as_str()?))
.collect();
let joints = joints.filter(|j| !j.is_empty())?;
let mut src = Map::new();
copy(skin, &mut src, &["bindShapeMatrix", "jointNames"]);
let mut o = object(&skins.list[s].0, skin, src);
if let Some(ibm) = accessors.index(skin.get("inverseBindMatrices")) {
o.insert("inverseBindMatrices".into(), json!(ibm));
}
if let [root, ..] = roots.as_slice() {
o.insert("skeleton".into(), json!(root));
}
o.insert("joints".into(), json!(joints));
out_skins.push(Value::Object(o));
let at = out_skins.len() - 1;
if let Some(m) = matrix(skin.get("bindShapeMatrix"))
&& differs(&m, &IDENTITY)
{
fix.bind_shapes.push((at, m));
}
Some(at)
});
if let Some(skin) = skin {
o.insert("skin".into(), json!(skin));
}
}
}
if let Some(l) = n
.get("extensions")
.and_then(|e| e.get("KHR_materials_common"))
.and_then(|c| c.get("light"))
.and_then(Value::as_str)
{
o.insert("extensions".into(), json!({ "KHR_lights_punctual": { "light": l } }));
lights_used = true;
}
out_nodes.push(Value::Object(o));
}
if let Some(common) = root_ext.remove("KHR_materials_common") {
source.insert("KHR_materials_common".into(), common.clone());
let lights: Vec<(String, Value)> = common
.get("lights")
.and_then(Value::as_object)
.map(|l| l.iter().map(|(k, v)| (k.clone(), v.clone())).collect())
.unwrap_or_default();
let mut at = HashMap::new();
let mut out_lights = Vec::new();
for (id, l) in &lights {
if let Some(light) = light(id, l) {
at.insert(id.clone(), out_lights.len());
out_lights.push(light);
}
}
for n in &mut out_nodes {
let idx = n
.pointer("/extensions/KHR_lights_punctual/light")
.and_then(Value::as_str)
.map(|id| at.get(id).copied());
match idx {
Some(Some(i)) => n["extensions"]["KHR_lights_punctual"]["light"] = json!(i),
Some(None) => {
n.as_object_mut().expect("a node").remove("extensions");
}
None => {}
}
}
if !out_lights.is_empty() {
used.push("KHR_lights_punctual");
out.insert(
"extensions".into(),
json!({ "KHR_lights_punctual": { "lights": out_lights } }),
);
}
} else if lights_used {
for n in &mut out_nodes {
n.as_object_mut().expect("a node").remove("extensions");
}
}
if !root_ext.is_empty() {
source.insert("extensions".into(), Value::Object(root_ext));
}
out.insert(
"cameras".into(),
cameras
.list
.iter()
.map(|(id, c)| {
let mut o = object(id, c, Map::new());
copy(c, &mut o, &["type", "perspective", "orthographic"]);
Value::Object(o)
})
.collect(),
);
out.insert(
"images".into(),
images
.list
.iter()
.map(|(id, img)| {
let mut o = object(id, img, Map::new());
if let Some(b) = img.get("extensions").and_then(|e| e.get("KHR_binary_glTF")) {
if let Some(v) = views.index(b.get("bufferView")) {
o.insert("bufferView".into(), json!(v));
}
copy(b, &mut o, &["mimeType"]);
} else {
copy(img, &mut o, &["uri"]);
}
Value::Object(o)
})
.collect(),
);
out.insert(
"samplers".into(),
samplers
.list
.iter()
.map(|(id, s)| {
let mut o = object(id, s, Map::new());
copy(s, &mut o, &["magFilter", "minFilter", "wrapS", "wrapT"]);
Value::Object(o)
})
.collect(),
);
out.insert(
"textures".into(),
textures
.list
.iter()
.map(|(id, t)| {
let mut src = Map::new();
copy(t, &mut src, &["format", "internalFormat", "target", "type"]);
let mut o = object(id, t, src);
if let Some(s) = samplers.index(t.get("sampler")) {
o.insert("sampler".into(), json!(s));
}
if let Some(s) = images.index(t.get("source")) {
o.insert("source".into(), json!(s));
}
Value::Object(o)
})
.collect(),
);
let mut unlit = false;
out.insert(
"materials".into(),
materials
.list
.iter()
.map(|(id, m)| {
let (o, is_unlit) = material(id, m, &techniques, &textures);
unlit |= is_unlit;
o
})
.collect(),
);
if unlit {
used.push("KHR_materials_unlit");
}
let mut out_animations = Vec::new();
for (id, a) in &animations.list {
let mut src = Map::new();
copy(a, &mut src, &["parameters"]);
let mut o = object(id, a, src);
let params = a.get("parameters");
let param = |name: Option<&Value>| accessors.index(params?.get(name?.as_str()?));
let sampler_ids: Vec<(String, Value)> = match a.get("samplers") {
Some(Value::Object(s)) => s.iter().map(|(k, v)| (k.clone(), v.clone())).collect(),
_ => Vec::new(),
};
let mut at = HashMap::new();
let mut out_samplers = Vec::new();
for (sid, s) in &sampler_ids {
if let (Some(input), Some(output)) = (param(s.get("input")), param(s.get("output"))) {
at.insert(sid.as_str(), out_samplers.len());
let interpolation = s.get("interpolation").cloned().unwrap_or_else(|| json!("LINEAR"));
out_samplers.push(json!({ "input": input, "output": output, "interpolation": interpolation }));
}
}
let channels: Vec<Value> = a
.get("channels")
.and_then(Value::as_array)
.into_iter()
.flatten()
.filter_map(|c| {
let sampler = at.get(c.get("sampler")?.as_str()?)?;
let target = c.get("target")?;
let node = nodes.index(target.get("id").or_else(|| target.get("node")))?;
let path = target.get("path")?.as_str()?;
matches!(path, "translation" | "rotation" | "scale")
.then(|| json!({ "sampler": sampler, "target": { "node": node, "path": path } }))
})
.collect();
if channels.is_empty() {
continue;
}
o.insert("samplers".into(), Value::Array(out_samplers));
o.insert("channels".into(), Value::Array(channels));
out_animations.push(Value::Object(o));
}
out.insert(
"scenes".into(),
scenes
.list
.iter()
.map(|(id, s)| {
let mut o = object(id, s, Map::new());
o.insert("nodes".into(), json!(nodes.ids(s.get("nodes")).collect::<Vec<_>>()));
Value::Object(o)
})
.collect(),
);
if let Some(s) = scenes.index(v1.get("scene")) {
out.insert("scene".into(), json!(s));
}
out.insert("bufferViews".into(), Value::Array(out_views));
out.insert("accessors".into(), Value::Array(out_accessors));
out.insert("meshes".into(), Value::Array(out_meshes));
out.insert("nodes".into(), Value::Array(out_nodes));
out.insert("skins".into(), Value::Array(out_skins));
out.insert("animations".into(), Value::Array(out_animations));
out.retain(|_, v| !v.as_array().is_some_and(Vec::is_empty));
if !used.is_empty() {
out.insert("extensionsUsed".into(), json!(used));
}
if let Some(e) = v1.remove("extras") {
out.insert("extras".into(), e);
}
asset.insert("version".into(), json!("2.0"));
let mut extras = asset
.remove("extras")
.and_then(|e| e.as_object().cloned())
.unwrap_or_default();
extras.insert("source".into(), Value::Object(source));
asset.insert("extras".into(), Value::Object(extras));
out.insert("asset".into(), Value::Object(asset));
Ok((out, fix))
}
fn primitive(p: &Value, accessors: &Kind, materials: &Kind, out_accessors: &mut [Value], fix: &mut Fixups) -> Value {
let mut o = Map::new();
let mut attrs = Map::new();
for (name, id) in p.get("attributes").and_then(Value::as_object).into_iter().flatten() {
let Some(a) = accessors.index(Some(id)) else { continue };
let acc = &mut out_accessors[a];
let kind = acc.get("type").and_then(Value::as_str).unwrap_or("SCALAR").to_owned();
let ctype = acc.get("componentType").and_then(Value::as_u64).unwrap_or(5126);
let s = semantic(name, &kind);
if s.starts_with("JOINTS_") && ctype == 5126 && !fix.joints.contains(&a) {
fix.joints.push(a);
}
if s.starts_with("COLOR_") && ctype != 5126 {
acc["normalized"] = json!(true);
}
if s == "POSITION" && (acc.get("min").is_none() || acc.get("max").is_none()) && !fix.bounds.contains(&a) {
fix.bounds.push(a);
}
attrs.insert(s, json!(a));
}
o.insert("attributes".into(), Value::Object(attrs));
if let Some(i) = accessors.index(p.get("indices")) {
o.insert("indices".into(), json!(i));
}
if let Some(m) = materials.index(p.get("material")) {
o.insert("material".into(), json!(m));
}
if let Some(m) = p.get("mode").or_else(|| p.get("primitive")) {
o.insert("mode".into(), m.clone());
}
Value::Object(o)
}
fn differs(a: &[f32], b: &[f32]) -> bool {
a.iter().zip(b).any(|(x, y)| (x - y).abs() > 1e-6)
}
const IDENTITY: [f32; 16] = [
1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0,
];
fn matrix(v: Option<&Value>) -> Option<[f32; 16]> {
let a = v?.as_array()?;
let mut m = [0.0; 16];
for (o, x) in m.iter_mut().zip(a) {
*o = f32_of(x.as_f64()?);
}
(a.len() == 16).then_some(m)
}
fn material(id: &str, m: &Value, techniques: &Kind, textures: &Kind) -> (Value, bool) {
let common = m.get("extensions").and_then(|e| e.get("KHR_materials_common"));
let technique = techniques.index(m.get("technique")).map(|t| &techniques.list[t].1);
let mut values = Map::new();
let kind;
let mut double_sided = false;
let mut blend = false;
if let Some(c) = common {
kind = c.get("technique").and_then(Value::as_str).unwrap_or("BLINN").to_owned();
values.extend(c.get("values").and_then(Value::as_object).cloned().unwrap_or_default());
double_sided = c.get("doubleSided").and_then(Value::as_bool).unwrap_or(false);
blend = c.get("transparent").and_then(Value::as_bool).unwrap_or(false);
} else {
kind = "TECHNIQUE".to_owned();
if let Some(t) = technique {
for (name, p) in t.get("parameters").and_then(Value::as_object).into_iter().flatten() {
if let Some(v) = p.get("value") {
values.insert(name.clone(), v.clone());
}
}
let enabled = t.pointer("/states/enable").and_then(Value::as_array);
if let Some(enabled) = enabled {
double_sided = !enabled.iter().any(|e| e.as_u64() == Some(2884));
blend = enabled.iter().any(|e| e.as_u64() == Some(3042));
}
}
values.extend(m.get("values").and_then(Value::as_object).cloned().unwrap_or_default());
}
let mut src = Map::new();
copy(m, &mut src, &["technique", "values"]);
if let Some(c) = common {
src.insert("KHR_materials_common".into(), c.clone());
}
let mut o = object(id, m, src);
let color = |v: &Value| -> Option<[f32; 4]> {
let a = v.as_array()?;
let mut c = [0.0, 0.0, 0.0, 1.0];
for (i, x) in a.iter().take(4).enumerate() {
let x = f32_of(x.as_f64()?);
c[i] = if i < 3 {
srgb_to_linear(x.clamp(0.0, 1.0))
} else {
x.clamp(0.0, 1.0)
};
}
(a.len() >= 3).then_some(c)
};
let texture = |v: &Value| textures.index(Some(v)).map(|t| json!({ "index": t }));
let unlit = kind == "CONSTANT";
let mut pbr = Map::new();
let mut base = [1.0, 1.0, 1.0, 1.0];
let main = if unlit {
values.get("emission").or_else(|| values.get("diffuse"))
} else {
values.get("diffuse")
};
if let Some(d) = main {
if let Some(c) = color(d) {
base = c;
} else if let Some(t) = texture(d) {
pbr.insert("baseColorTexture".into(), t);
}
}
if let Some(t) = values.get("transparency").and_then(Value::as_f64) {
base[3] *= f32_of(t).clamp(0.0, 1.0);
}
if differs(&base, &[1.0; 4]) {
pbr.insert("baseColorFactor".into(), json!(base));
}
pbr.insert("metallicFactor".into(), json!(0.0));
let shininess = values
.get("shininess")
.and_then(|s| s.as_f64().or_else(|| s.get(0)?.as_f64()));
let roughness = match (kind.as_str(), shininess) {
("LAMBERT", _) | (_, None) => 1.0,
(_, Some(n)) => (1.0 - f32_of(n.max(0.0)).sqrt() / 10.0).clamp(0.0, 1.0),
};
pbr.insert("roughnessFactor".into(), json!(roughness));
o.insert("pbrMetallicRoughness".into(), Value::Object(pbr));
if unlit {
o.insert("extensions".into(), json!({ "KHR_materials_unlit": {} }));
} else if let Some(e) = values.get("emission") {
if let Some(c) = color(e) {
if c[..3] != [0.0; 3] {
o.insert("emissiveFactor".into(), json!(c[..3]));
}
} else if let Some(t) = texture(e) {
o.insert("emissiveTexture".into(), t);
o.insert("emissiveFactor".into(), json!([1.0, 1.0, 1.0]));
}
}
if blend || base[3] < 1.0 {
o.insert("alphaMode".into(), json!("BLEND"));
}
if double_sided {
o.insert("doubleSided".into(), json!(true));
}
(Value::Object(o), unlit)
}
fn light(id: &str, l: &Value) -> Option<Value> {
let kind = l.get("type")?.as_str()?;
let body = l.get(kind);
let color = body
.and_then(|b| b.get("color"))
.and_then(Value::as_array)
.filter(|c| c.len() >= 3)
.map_or(json!([1.0, 1.0, 1.0]), |c| json!(c[..3]));
let mut o = json!({ "name": l.get("name").cloned().unwrap_or_else(|| json!(id)), "color": color });
match kind {
"directional" => o["type"] = json!("directional"),
"point" => o["type"] = json!("point"),
"spot" => {
o["type"] = json!("spot");
let falloff = body.and_then(|b| b.get("falloffAngle")).and_then(Value::as_f64);
let outer = falloff.map_or(std::f64::consts::FRAC_PI_4, |a| {
(a / 2.0).clamp(1e-3, std::f64::consts::FRAC_PI_2)
});
o["spot"] = json!({ "innerConeAngle": 0.0, "outerConeAngle": outer });
}
_ => return None,
}
if let Some(b) = body.and_then(Value::as_object) {
let mut extras = b.clone();
extras.remove("color");
if !extras.is_empty() {
o["extras"] = json!({ "source": extras });
}
}
Some(o)
}
pub(crate) fn finish(doc: &mut Document, fix: &Fixups) -> Result<()> {
for &a in &fix.joints {
let (floats, n) = floats(doc, a)?;
let joints: Vec<u16> = floats
.iter()
.map(|&j| {
#[allow(
clippy::cast_possible_truncation,
clippy::cast_sign_loss,
reason = "rounded and clamped"
)]
let j = j.round().clamp(0.0, f32::from(u16::MAX)) as u16;
j
})
.collect();
let bytes: Vec<u8> = joints.iter().flat_map(|j| j.to_le_bytes()).collect();
let view = push(doc, bytes);
let acc = doc.json_mut()["accessors"][a].as_object_mut().expect("an accessor");
acc.insert("bufferView".into(), json!(view));
acc.insert("byteOffset".into(), json!(0));
acc.insert("componentType".into(), json!(5123));
acc.insert("count".into(), json!(n));
acc.remove("min");
acc.remove("max");
}
for &a in &fix.bounds {
let (p, _) = floats(doc, a)?;
let mut min = [f32::INFINITY; 3];
let mut max = [f32::NEG_INFINITY; 3];
for v in p.chunks_exact(3) {
for k in 0..3 {
min[k] = min[k].min(v[k]);
max[k] = max[k].max(v[k]);
}
}
if min[0].is_finite() {
let acc = &mut doc.json_mut()["accessors"][a];
acc["min"] = json!(min);
acc["max"] = json!(max);
}
}
for &(s, bsm) in &fix.bind_shapes {
let skin = &doc.base["skins"][s];
let joints = skin["joints"].as_array().map_or(0, Vec::len);
let ibms: Vec<f32> = match skin.get("inverseBindMatrices").and_then(Value::as_u64) {
Some(a) => floats(doc, usize_of(a))?.0,
None => IDENTITY.repeat(joints),
};
let mut baked = Vec::with_capacity(ibms.len() * 4);
for ibm in ibms.chunks_exact(16) {
for c in 0..4 {
for r in 0..4 {
let x: f32 = (0..4).map(|k| ibm[k * 4 + r] * bsm[c * 4 + k]).sum();
baked.extend_from_slice(&x.to_le_bytes());
}
}
}
let count = baked.len() / 64;
let view = push(doc, baked);
let accessors = doc
.json_mut()
.entry("accessors")
.or_insert_with(|| json!([]))
.as_array_mut()
.expect("glTF accessors is a list");
accessors.push(json!({ "bufferView": view, "componentType": 5126, "count": count, "type": "MAT4" }));
let a = accessors.len() - 1;
doc.base_mut()["skins"][s]["inverseBindMatrices"] = json!(a);
}
Ok(())
}
fn push(doc: &mut Document, bytes: Vec<u8>) -> usize {
let len = bytes.len();
let b = doc.push_buffer(bytes);
doc.push_view(json!({ "buffer": b, "byteLength": len }))
}
fn floats(doc: &Document, a: usize) -> Result<(Vec<f32>, usize)> {
let acc = &doc.json()["accessors"][a];
let get = |v: &Value, k: &str| v.get(k).and_then(Value::as_u64).unwrap_or(0);
ensure!(
get(acc, "componentType") == 5126,
Corrupt,
"glTF 1.0 accessor {a} isn't float"
);
let count = usize_of(get(acc, "count"));
let n = components(acc.get("type").and_then(Value::as_str).unwrap_or("SCALAR"));
let Some(v) = acc.get("bufferView").and_then(Value::as_u64) else {
return Ok((vec![0.0; count * n], count));
};
let view = &doc.json()["bufferViews"][usize_of(v)];
let stride = match usize_of(get(view, "byteStride")) {
0 => n * 4,
s => s,
};
let start = usize_of(get(view, "byteOffset") + get(acc, "byteOffset"));
let buf = doc.buffer(usize_of(get(view, "buffer")))?;
let mut out = Vec::with_capacity(count * n);
for i in 0..count {
for k in 0..n {
let at = start + i * stride + k * 4;
let b = buf.get(at..at + 4).with_context(ErrorKind::Truncated, || {
format!("glTF 1.0 accessor {a} runs past the end of its buffer")
})?;
out.push(f32::from_le_bytes([b[0], b[1], b[2], b[3]]));
}
}
Ok((out, count))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn semantics_take_2_0_names() {
for (a, b) in [
("POSITION", "POSITION"),
("NORMAL", "NORMAL"),
("TEXCOORD", "TEXCOORD_0"),
("TEXCOORD_1", "TEXCOORD_1"),
("COLOR", "COLOR_0"),
("JOINT", "JOINTS_0"),
("WEIGHT", "WEIGHTS_0"),
("TANGENT", "_TANGENT"),
("BATCHID", "_BATCHID"),
] {
assert_eq!(semantic(a, "VEC3"), b);
}
}
#[test]
fn versions() {
let v = |j: Value| is_v1(j.as_object().unwrap());
assert!(v(json!({ "asset": { "version": "1.0" } })));
assert!(v(json!({ "asset": { "version": "1.0.1" } })));
assert!(v(json!({ "asset": { "version": 1 } })));
assert!(v(json!({ "meshes": {} })));
assert!(!v(json!({ "asset": { "version": "2.0" } })));
assert!(!v(json!({ "meshes": [] })));
}
#[test]
fn the_source_is_1_0s() {
let v1 = json!({
"asset": { "version": "1.0.1", "generator": "Exporter 2", "copyright": "Ann" },
"meshes": {},
});
let (out, _) = upgrade(v1.as_object().unwrap().clone()).unwrap();
assert_eq!(out["asset"]["version"], "2.0");
assert_eq!(out["asset"]["generator"], "Exporter 2");
let info = SourceInfo::from_json(out["asset"]["extras"]["source"].as_object().unwrap());
assert_eq!(info.format.as_deref(), Some("glTF"));
assert_eq!(info.version.as_deref(), Some("1.0.1"));
assert_eq!(info.meters_per_unit, Fact::Declared(1.0));
assert_eq!(info.unit.as_deref(), Some("meter"));
assert_eq!(info.up_axis, Fact::Assumed(Axis::PosY));
assert_eq!(info.tool.as_deref(), Some("Exporter 2"));
assert_eq!(info.copyright.as_deref(), Some("Ann"));
assert_eq!(info.converted, crate::source::Converted::default());
}
}