Skip to content
Snippets Groups Projects
Commit 9de227d1 authored by Marshall Quander's avatar Marshall Quander
Browse files

Clean up inflation algorithm a lot

parent b08febb7
No related branches found
No related tags found
No related merge requests found
...@@ -74,44 +74,34 @@ function cloneGltf(gltf) { ...@@ -74,44 +74,34 @@ function cloneGltf(gltf) {
return clone; return clone;
} }
const getBehaviorNodes = function(root, templates) { /// Walks the tree of three.js objects starting at the given node, using the GLTF data
const nodes = new Set(); /// and template data to construct A-Frame entities and components when necessary.
if (root.userData.components || root.name in templates) { /// (It's unnecessary to construct entities for subtrees that have no component data
nodes.add(root); /// or templates associated with any of their nodes.)
} ///
for (const child of root.children) { /// Returns the A-Frame entity associated with the given node, if one was constructed.
for (const other of getBehaviorNodes(child, templates)) { const inflateEntities = function(node, templates, gltfPath) {
nodes.add(other); // inflate subtrees first so that we can determine whether or not this node needs to be inflated
const childEntities = [];
const children = node.children.slice(0); // setObject3D mutates the node's parent, so we have to copy
for (const child of children) {
const el = inflateEntities(child, templates, gltfPath);
if (el) {
childEntities.push(el);
} }
} }
return nodes;
};
const markLeaves = function(root, behaviorNodes) { const nodeHasBehavior = node.userData.components || node.name in templates;
root.userData.leaf = !behaviorNodes.has(root); if (!nodeHasBehavior && !childEntities.length) {
for (const child of root.children) { return null; // we don't need an entity for this node
if (!markLeaves(child, behaviorNodes)) {
root.userData.leaf = false;
}
} }
return root.userData.leaf;
};
const inflateEntities = function(parentEl, node, gltfPath) {
if (node.userData.leaf) {
// we don't need an entity for this node
return null;
}
// setObject3D mutates the node's parent, so we have to copy
const children = node.children.slice(0);
const el = document.createElement("a-entity"); const el = document.createElement("a-entity");
el.append.apply(el, childEntities);
// Remove invalid CSS class name characters. // Remove invalid CSS class name characters.
const className = (node.name || node.uuid).replace(/[^\w-]/g, ""); const className = (node.name || node.uuid).replace(/[^\w-]/g, "");
el.classList.add(className); el.classList.add(className);
parentEl.appendChild(el);
// AFRAME rotation component expects rotations in YXZ, convert it // AFRAME rotation component expects rotations in YXZ, convert it
if (node.rotation.order !== "YXZ") { if (node.rotation.order !== "YXZ") {
...@@ -166,10 +156,6 @@ const inflateEntities = function(parentEl, node, gltfPath) { ...@@ -166,10 +156,6 @@ const inflateEntities = function(parentEl, node, gltfPath) {
} }
} }
children.forEach(childNode => {
inflateEntities(el, childNode, gltfPath);
});
return el; return el;
}; };
...@@ -286,9 +272,8 @@ AFRAME.registerComponent("gltf-model-plus", { ...@@ -286,9 +272,8 @@ AFRAME.registerComponent("gltf-model-plus", {
this.el.setObject3D("mesh", this.model); this.el.setObject3D("mesh", this.model);
if (this.data.inflate) { if (this.data.inflate) {
const behaviorNodes = getBehaviorNodes(this.model, this.templates); this.inflatedEl = inflateEntities(this.model, this.templates, gltfPath);
markLeaves(this.model, behaviorNodes); this.el.appendChild(this.inflatedEl);
this.inflatedEl = inflateEntities(this.el, this.model, gltfPath);
// TODO: Still don't fully understand the lifecycle here and how it differs between browsers, we should dig in more // TODO: Still don't fully understand the lifecycle here and how it differs between browsers, we should dig in more
// Wait one tick for the appended custom elements to be connected before attaching templates // Wait one tick for the appended custom elements to be connected before attaching templates
await nextTick(); await nextTick();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment