#![allow(
clippy::cast_possible_truncation,
reason = "indices into a glTF document in memory fit in usize"
)]
use serde_json::{Map, Value, json};
use crate::document::Document;
pub(super) struct Placed {
first: usize,
count: usize,
roots: Vec<usize>,
pub shareable: bool,
}
fn len(json: &Map<String, Value>, key: &str) -> usize {
json.get(key).and_then(Value::as_array).map_or(0, Vec::len)
}
fn lights(json: &Map<String, Value>) -> usize {
json.get("extensions")
.and_then(|e| e.get("KHR_lights_punctual"))
.and_then(|l| l.get("lights"))
.and_then(Value::as_array)
.map_or(0, Vec::len)
}
#[derive(Clone, Copy, Default)]
struct Offsets {
nodes: usize,
meshes: usize,
materials: usize,
textures: usize,
images: usize,
samplers: usize,
accessors: usize,
views: usize,
buffers: usize,
skins: usize,
cameras: usize,
lights: usize,
variants: usize,
}
impl Offsets {
fn of(json: &Map<String, Value>) -> Self {
Self {
nodes: len(json, "nodes"),
meshes: len(json, "meshes"),
materials: len(json, "materials"),
textures: len(json, "textures"),
images: len(json, "images"),
samplers: len(json, "samplers"),
accessors: len(json, "accessors"),
views: len(json, "bufferViews"),
buffers: len(json, "buffers"),
skins: len(json, "skins"),
cameras: len(json, "cameras"),
lights: lights(json),
variants: json
.get("extensions")
.and_then(|e| e.get("KHR_materials_variants"))
.and_then(|v| v.get("variants"))
.and_then(Value::as_array)
.map_or(0, Vec::len),
}
}
}
fn bump(v: &mut Value, key: &str, by: usize) {
if let Some(x) = v.get_mut(key)
&& let Some(i) = x.as_u64()
{
*x = json!(i as usize + by);
}
}
fn bump_all(v: &mut Value, key: &str, by: usize) {
if let Some(Value::Array(list)) = v.get_mut(key) {
for x in list {
if let Some(i) = x.as_u64() {
*x = json!(i as usize + by);
}
}
}
}
fn bump_values(v: &mut Value, by: usize) {
if let Some(o) = v.as_object_mut() {
for x in o.values_mut() {
if let Some(i) = x.as_u64() {
*x = json!(i as usize + by);
}
}
}
}
fn bump_textures(v: &mut Value, by: usize) {
match v {
Value::Object(o) => {
for (k, x) in o.iter_mut() {
if k.ends_with("Texture") && x.is_object() {
bump(x, "index", by);
}
bump_textures(x, by);
}
}
Value::Array(list) => list.iter_mut().for_each(|x| bump_textures(x, by)),
_ => {}
}
}
fn pointer(p: &str, o: &Offsets) -> String {
let rules: [(&str, usize); 6] = [
("/nodes/", o.nodes),
("/materials/", o.materials),
("/meshes/", o.meshes),
("/cameras/", o.cameras),
("/textures/", o.textures),
("/extensions/KHR_lights_punctual/lights/", o.lights),
];
for (prefix, by) in rules {
if let Some(rest) = p.strip_prefix(prefix) {
let (num, tail) = rest.split_at(rest.find('/').unwrap_or(rest.len()));
if let Ok(i) = num.parse::<usize>() {
return format!("{prefix}{}{tail}", i + by);
}
}
}
p.to_owned()
}
fn append(dst: &mut Map<String, Value>, src: &mut Map<String, Value>, key: &str, mut f: impl FnMut(&mut Value)) {
let Some(Value::Array(mut list)) = src.remove(key) else {
return;
};
if list.is_empty() {
return;
}
list.iter_mut().for_each(&mut f);
if let Value::Array(d) = dst.entry(key).or_insert_with(|| json!([])) {
d.extend(list);
}
}
fn adopt(json: &mut Map<String, Value>, parent: usize, children: &[usize]) {
if children.is_empty() {
return;
}
if let Some(Value::Array(nodes)) = json.get_mut("nodes")
&& let Some(Value::Object(p)) = nodes.get_mut(parent)
&& let Value::Array(c) = p.entry("children").or_insert_with(|| json!([]))
{
c.extend(children.iter().map(|&i| json!(i)));
}
}
pub(super) fn graft(dst: &mut Document, src: Document, parent: usize, materials: Option<&[usize]>) -> Placed {
let o = Offsets::of(&dst.json);
let Document {
json: mut s,
buffers,
refs,
lines_and_points,
..
} = src;
let materials = materials.filter(|m| m.len() == len(&s, "materials"));
let map_material = |i: usize| materials.map_or(i + o.materials, |m| m[i]);
let shareable = len(&s, "skins") == 0 && len(&s, "animations") == 0;
let scene = s.get("scene").and_then(Value::as_u64).unwrap_or(0) as usize;
let roots: Vec<usize> = s
.get("scenes")
.and_then(|v| v.get(scene))
.and_then(|v| v.get("nodes"))
.and_then(Value::as_array)
.map(|r| {
r.iter()
.filter_map(Value::as_u64)
.map(|i| i as usize + o.nodes)
.collect()
})
.unwrap_or_default();
let count = len(&s, "nodes");
let d = &mut dst.json;
append(d, &mut s, "nodes", |n| {
bump_all(n, "children", o.nodes);
bump(n, "mesh", o.meshes);
bump(n, "skin", o.skins);
bump(n, "camera", o.cameras);
if let Some(e) = n.get_mut("extensions") {
if let Some(l) = e.get_mut("KHR_lights_punctual") {
bump(l, "light", o.lights);
}
if let Some(a) = e
.get_mut("EXT_mesh_gpu_instancing")
.and_then(|i| i.get_mut("attributes"))
{
bump_values(a, o.accessors);
}
}
});
append(d, &mut s, "meshes", |m| {
let Some(Value::Array(prims)) = m.get_mut("primitives") else {
return;
};
for p in prims {
if let Some(a) = p.get_mut("attributes") {
bump_values(a, o.accessors);
}
bump(p, "indices", o.accessors);
if let Some(x) = p.get_mut("material")
&& let Some(i) = x.as_u64()
{
*x = json!(map_material(i as usize));
}
if let Some(Value::Array(targets)) = p.get_mut("targets") {
for t in targets.iter_mut() {
bump_values(t, o.accessors);
}
}
if let Some(e) = p.get_mut("extensions") {
if let Some(draco) = e.get_mut("KHR_draco_mesh_compression") {
bump(draco, "bufferView", o.views);
}
if let Some(Value::Array(maps)) =
e.get_mut("KHR_materials_variants").and_then(|v| v.get_mut("mappings"))
{
for map in maps {
if let Some(x) = map.get_mut("material")
&& let Some(i) = x.as_u64()
{
*x = json!(map_material(i as usize));
}
bump_all(map, "variants", o.variants);
}
}
}
}
});
if materials.is_some() {
s.remove("materials");
} else {
append(d, &mut s, "materials", |m| bump_textures(m, o.textures));
}
append(d, &mut s, "textures", |t| {
bump(t, "source", o.images);
bump(t, "sampler", o.samplers);
if let Some(Value::Object(e)) = t.get_mut("extensions") {
e.values_mut().for_each(|x| bump(x, "source", o.images));
}
});
append(d, &mut s, "images", |i| bump(i, "bufferView", o.views));
append(d, &mut s, "samplers", |_| {});
append(d, &mut s, "accessors", |a| {
bump(a, "bufferView", o.views);
if let Some(sp) = a.get_mut("sparse") {
for part in ["indices", "values"] {
if let Some(x) = sp.get_mut(part) {
bump(x, "bufferView", o.views);
}
}
}
});
append(d, &mut s, "bufferViews", |v| {
bump(v, "buffer", o.buffers);
if let Some(Value::Object(e)) = v.get_mut("extensions") {
e.values_mut().for_each(|x| bump(x, "buffer", o.buffers));
}
});
append(d, &mut s, "buffers", |_| {});
append(d, &mut s, "skins", |k| {
bump(k, "inverseBindMatrices", o.accessors);
bump(k, "skeleton", o.nodes);
bump_all(k, "joints", o.nodes);
});
append(d, &mut s, "cameras", |_| {});
append(d, &mut s, "animations", |a| {
if let Some(Value::Array(samplers)) = a.get_mut("samplers") {
for x in samplers {
bump(x, "input", o.accessors);
bump(x, "output", o.accessors);
}
}
if let Some(Value::Array(channels)) = a.get_mut("channels") {
for c in channels {
let Some(t) = c.get_mut("target") else { continue };
bump(t, "node", o.nodes);
if let Some(Value::String(p)) = t.pointer_mut("/extensions/KHR_animation_pointer/pointer") {
*p = pointer(p, &o);
}
}
}
});
if let Some(Value::Object(mut ext)) = s.remove("extensions") {
for (name, list) in [
("KHR_lights_punctual", "lights"),
("KHR_materials_variants", "variants"),
] {
let Some(Value::Array(items)) = ext.remove(name).and_then(|mut e| e.get_mut(list).map(Value::take)) else {
continue;
};
let de = d.entry("extensions").or_insert_with(|| json!({}));
if let Some(de) = de.as_object_mut() {
let entry = de
.entry(name)
.or_insert_with(|| Value::Object(Map::from_iter([(list.to_owned(), json!([]))])));
if let Some(Value::Array(dl)) = entry.get_mut(list) {
dl.extend(items);
}
}
}
}
for key in ["extensionsUsed", "extensionsRequired"] {
if let Some(Value::Array(names)) = s.remove(key)
&& let Value::Array(dl) = d.entry(key).or_insert_with(|| json!([]))
{
for n in names {
if !dl.contains(&n) {
dl.push(n);
}
}
}
}
adopt(d, parent, &roots);
dst.buffers.extend(buffers);
dst.refs.extend(refs.into_iter().map(|(i, r)| (i + o.images, r)));
dst.lines_and_points |= lines_and_points;
Placed {
first: o.nodes,
count,
roots,
shareable,
}
}
pub(super) fn repeat(dst: &mut Document, placed: &Placed, parent: usize) {
let at = len(&dst.json, "nodes");
let Some(Value::Array(nodes)) = dst.json.get_mut("nodes") else {
return;
};
let Some(copies) = nodes
.get(placed.first..placed.first + placed.count)
.map(<[Value]>::to_vec)
else {
return;
};
for mut n in copies {
if let Some(Value::Array(c)) = n.get_mut("children") {
for x in c {
if let Some(i) = x.as_u64() {
*x = json!(i as usize - placed.first + at);
}
}
}
nodes.push(n);
}
let roots: Vec<usize> = placed.roots.iter().map(|r| r - placed.first + at).collect();
adopt(&mut dst.json, parent, &roots);
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn moves_pointers() {
let o = Offsets {
nodes: 3,
materials: 2,
lights: 1,
..Offsets::default()
};
assert_eq!(pointer("/nodes/0/translation", &o), "/nodes/3/translation");
assert_eq!(
pointer("/materials/1/pbrMetallicRoughness/baseColorFactor", &o),
"/materials/3/pbrMetallicRoughness/baseColorFactor"
);
assert_eq!(
pointer("/extensions/KHR_lights_punctual/lights/0/color", &o),
"/extensions/KHR_lights_punctual/lights/1/color"
);
let mut m = json!({ "normalTexture": { "index": 0 }, "extensions": { "KHR_materials_clearcoat": { "clearcoatTexture": { "index": 1 } } } });
bump_textures(&mut m, 5);
assert_eq!(m["normalTexture"]["index"], 5);
assert_eq!(
m["extensions"]["KHR_materials_clearcoat"]["clearcoatTexture"]["index"],
6
);
}
}