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

feat(core): Add io.jsonToBinary() #1280

Closed
wants to merge 4 commits into from
Closed

Conversation

donmccurdy
Copy link
Owner

@donmccurdy donmccurdy commented Feb 21, 2024

Example:

const { json, resources } = await io.writeJSON(document);
const glb = await io.jsonToBinary({ json, resources });

Related:

Remaining:

  • More tests

@donmccurdy
Copy link
Owner Author

Current dependencies on/for this PR:

This stack of pull requests is managed by Graphite.

@donmccurdy donmccurdy added this to the v4.0 milestone Feb 21, 2024
@donmccurdy donmccurdy added feature New enhancement or request package:core labels Feb 21, 2024
@donmccurdy
Copy link
Owner Author

donmccurdy commented Feb 24, 2024

Testing on meshopt-compressed inputs with the script below:

import { NodeIO } from '@gltf-transform/core';
import { ALL_EXTENSIONS } from '@gltf-transform/extensions';
import { MeshoptDecoder } from 'meshoptimizer';
import fs from 'fs/promises';

await MeshoptDecoder.ready;

const io = new NodeIO()
  .registerExtensions(ALL_EXTENSIONS)
  .registerDependencies({ 'meshopt.decoder': MeshoptDecoder });

const jsonDoc = await io.readAsJSON('scene-meshopt.gltf');

console.log(JSON.stringify(jsonDoc.json.bufferViews, null, 2));
console.log('-------------');

const glb = await io.jsonToBinary(jsonDoc);

const jsonDoc2 = await io.binaryToJSON(glb);

console.log(JSON.stringify(jsonDoc2.json.bufferViews, null, 2));

await fs.writeFile('scene-packed.glb', glb);

@donmccurdy
Copy link
Owner Author

I've spent a while trying to provide this API...

const glb = await io.jsonToBinary(jsonDoc);

... but a fundamental problem with this approach seems to be, that if we don't parse the JSON into a Document, which I'd expect a method of that name to avoid, then extensions are never initialized and we have no way to understand extensions' references to buffers. And the buffers need to be repacked when writing a multi-buffer input to GLB. This shows up with Meshopt compression, but other extensions could theoretically do the same thing.

For now the best option remains to read the document, unpartition its buffers, and then write to GLB:

import { unpartition } from '@gltf-transform/functions';
const document = io.readJSON(jsonDoc);
await document.transform(unpartition());
const glb = await io.writeBinary(document);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New enhancement or request package:core
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support writing Document containing multiple Buffers as GLB Add JSONToBinary method
1 participant