Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
donmccurdy committed Oct 10, 2024
1 parent 4a3e76a commit 5f7118d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
18 changes: 16 additions & 2 deletions packages/functions/src/uninstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ const UNINSTANCE_DEFAULTS: Required<UninstanceOptions> = {};
export function uninstance(_options: UninstanceOptions = UNINSTANCE_DEFAULTS): Transform {
return createTransform(NAME, async (document: Document): Promise<void> => {
const logger = document.getLogger();
const root = document.getRoot();

const instanceAttributes = new Set<Accessor>();

for (const srcNode of document.getRoot().listNodes()) {
const batch = srcNode.getExtension<InstancedMesh>('EXT_mesh_gpu_instancing');
Expand All @@ -46,8 +49,19 @@ export function uninstance(_options: UninstanceOptions = UNINSTANCE_DEFAULTS): T
srcNode.addChild(instanceNode);
}

// Remove Mesh and Extension from source Node.
srcNode.setMesh(null).setExtension('EXT_mesh_gpu_instancing', null);
for (const instanceAttribute of batch.listAttributes()) {
instanceAttributes.add(instanceAttribute);
}

srcNode.setMesh(null);
batch.dispose();
}

// Clean up unused instance attributes.
for (const attribute of instanceAttributes) {
if (attribute.listParents().every((parent) => parent === root)) {
attribute.dispose();
}
}

// Remove Extension from Document.
Expand Down
2 changes: 2 additions & 0 deletions packages/functions/test/uninstance.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,6 @@ test('basic', async (t) => {
[{ _INSTANCE_ID: 100 }, { _INSTANCE_ID: 101 }, { _INSTANCE_ID: 102 }],
'sets instance extras',
);
t.is(translation.isDisposed(), true, 'disposes translation attribute');
t.is(id.isDisposed(), true, 'disposes id attribute');
});

0 comments on commit 5f7118d

Please sign in to comment.