Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't delete nodes that have used descendants #533

Merged
merged 1 commit into from
Mar 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions lib/removeUnusedElements.js
Original file line number Diff line number Diff line change
Expand Up @@ -431,27 +431,24 @@ getListOfElementsIdsInUse.mesh = function(gltf) {

// Check if node is empty. It is considered empty if neither referencing
// mesh, camera, extensions and has no children
function nodeIsEmpty(gltf, node) {
function nodeIsEmpty(gltf, nodeId, usedNodeIds) {
const node = gltf.nodes[nodeId];
if (defined(node.mesh) || defined(node.camera) || defined(node.skin)
|| defined(node.weights) || defined(node.extras)
|| (defined(node.extensions) && node.extensions.length !== 0)) {
|| (defined(node.extensions) && Object.keys(node.extensions).length !== 0)
|| defined(usedNodeIds[nodeId])) {
return false;
}

// Empty if no children or children are all empty nodes
return !defined(node.children)
|| node.children.filter(function(n) {
return !nodeIsEmpty(gltf, gltf.nodes[n]);
return !nodeIsEmpty(gltf, n, usedNodeIds);
}).length === 0;
}

getListOfElementsIdsInUse.node = function(gltf) {
const usedNodeIds = {};
ForEach.node(gltf, function(node, nodeId) {
if (!nodeIsEmpty(gltf, node)) {
usedNodeIds[nodeId] = true;
}
});
ForEach.skin(gltf, function(skin) {
if (defined(skin.skeleton)) {
usedNodeIds[skin.skeleton] = true;
Expand All @@ -475,6 +472,11 @@ getListOfElementsIdsInUse.node = function(gltf) {
}
});
});
ForEach.node(gltf, function(node, nodeId) {
if (!nodeIsEmpty(gltf, nodeId, usedNodeIds)) {
usedNodeIds[nodeId] = true;
}
});

return usedNodeIds;
};
Expand Down
100 changes: 100 additions & 0 deletions specs/lib/removeUnusedElementsSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,106 @@ describe('removeUnusedElements', () => {
expect(remaining['lights']).toContain(element.name);
});
});

it('keeps ancestor nodes of used nodes', () => {
const gltf = {
nodes: [
{
name: 'skeleton-root',
skin: 0,
mesh: 0,
translation: [0.0, 0.0, 0.0]
},
{
name: 'joint-parent',
translation: [0.0, 0.0, 0.0],
children: [2]
},
{
name: 'joint',
translation: [0.0, 0.0, 0.0]
},
{
name: 'unused'
}
],
buffers: [
{
name: 'mesh',
byteLength: 246,
uri: 'data0.bin'
},
{
name: 'other',
byteLength: 80,
uri: 'data1.bin'
}
],
bufferViews: [
{
name: 'positions',
buffer: 0,
byteOffset: 0,
byteLength: 36
},
{
name: 'indices',
buffer: 0,
byteOffset: 240,
byteLength: 6
}
],
accessors: [
{
name: 'positions',
bufferView: 0,
byteOffset: 0,
componentType: WebGLConstants.FLOAT,
count: 3,
type: 'VEC3',
min: [-1.0, -1.0, -1.0],
max: [1.0, 1.0, 1.0]
},
{
name: 'indices',
bufferView: 7,
byteOffset: 240,
componentType: WebGLConstants.UNSIGNED_SHORT,
count: 3,
type: 'SCALAR'
}
],
meshes: [
{
name: 'mesh0',
primitives: [
{
attributes: {
POSITION: 0
},
indices: 1,
mode: WebGLConstants.TRIANGLES
}
]
}
],
skins: [
{
skeleton: 0,
joints: [0, 2]
}
],
scenes: [
{
nodes: [0]
}
]
};

removeUnusedElements(gltf);

expect(gltf.nodes.length).toEqual(3);
});
});

describe('removes unused materials, textures, images, samplers', () => {
Expand Down