72 lines
1.5 KiB
JavaScript
72 lines
1.5 KiB
JavaScript
/**
|
|
* @module nodeUtils
|
|
* Utilities to process scene nodes.
|
|
*/
|
|
|
|
/**
|
|
* @typedef {Object} SceneNode
|
|
* @property {String} label Required
|
|
* @property {String} model
|
|
* @property {Boolean} isMain
|
|
* @property {Number|null} opacity
|
|
* @property {SceneNode[]|null} children
|
|
*/
|
|
|
|
/**
|
|
* @typedef {Object} NormalizedSceneNode
|
|
* @property {String} label Required
|
|
* @property {String} id
|
|
* @property {String} model
|
|
* @property {Boolean} isMain
|
|
* @property {Number|null} opacity
|
|
* @property {Boolean} active
|
|
*/
|
|
|
|
/**
|
|
*
|
|
* @param {SceneNode} node
|
|
* @param {Array} flatList
|
|
* @param {Number} depth
|
|
*/
|
|
function traverse(node, flatList, depth = 1) {
|
|
if (!node.label) {
|
|
console.error("Node missing label:", node);
|
|
return;
|
|
}
|
|
|
|
const normNode = {
|
|
...node,
|
|
id: node.id ?? node.label,
|
|
isMain: node.isMain ?? false,
|
|
active: true,
|
|
};
|
|
if (node.model) {
|
|
normNode.model = node.model;
|
|
}
|
|
flatList.push({
|
|
...normNode,
|
|
depth
|
|
});
|
|
if (node.children && Array.isArray(node.children)) {
|
|
for(let child of node.children) {
|
|
traverse(child, flatList, depth + 1);
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Create a flat list of nodes from
|
|
* the nested structure in config
|
|
* @param {Array} nodes
|
|
* @returns {NormalizedSceneNode[]} A flat list of nodes
|
|
**/
|
|
export function normalizeNodes (nodes) {
|
|
let flatList = [];
|
|
|
|
for (let node of nodes) {
|
|
traverse(node, flatList);
|
|
}
|
|
|
|
return flatList;
|
|
}
|