Merging models #691
-
Hi, I'm having some issues creating a single GLTF by merging multiple documents. I'm looping through
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 6 replies
-
Hi @raducostica! You'll need to merge(...) the Document instances, then write the Document to a file only once at the end. Merging N Documents results in a Document with N buffers and N scenes — in most cases you'll also want to consolidate to a single buffer and a single scene. See https://stackoverflow.com/a/68032114/1314762 if you choose to go this route. However, it looks like you might be trying to create a scene containing N mesh primitives? In that case there's no point in generating N documents and scenes along the way. You can create one document, append N meshes or mesh primitives to it, and then write the result: import { Document, NodeIO } from '@gltf-transform/core';
const doc = new Document();
const buffer = doc.createBuffer();
const mesh = doc.createMesh();
const node = doc.createNode().setMesh(mesh);
const scene = doc.createScene().addChild(node);
for (const coord of coords) {
const indices: number[]: getIndices(coord);
const positions: number[]: getPositions(coord);
const gltfPosition = doc
.createAccessor()
.setArray(new Float32Array(positions))
.setType("VEC3")
.setBuffer(buffer);
const gltfIndices = doc
.createAccessor()
.setArray(new Uint32Array(indices))
.setType("SCALAR")
.setBuffer(buffer);
const prim = doc
.createPrimitive()
.setAttribute("POSITION", gltfPosition)
.setIndices(gltfIndices);
mesh.addPrimitive(prim);
}
const io = new NodeIO();
// (a) write to byte array
const glb = await io.writeBinary(document); // → Uint8Array (.glb)
// (b) write to disk
await io.write('./path/to/scene.glb', document); |
Beta Was this translation helpful? Give feedback.
-
I'm having a hard time figuring out how to exactly use the API from the example above, especially how coords would work. Here is my scenario:
I want to create model3.gltf that aggregates model1 at pos1 and model2 at pos2. Would be really great if you could provide a generic example based on those inputs, so all content of the models get merged into a single resulting scene in model3. Thanks a bunch. |
Beta Was this translation helpful? Give feedback.
Hi @raducostica! You'll need to merge(...) the Document instances, then write the Document to a file only once at the end. Merging N Documents results in a Document with N buffers and N scenes — in most cases you'll also want to consolidate to a single buffer and a single scene. See https://stackoverflow.com/a/68032114/1314762 if you choose to go this route.
However, it looks like you might be trying to create a scene containing N mesh primitives? In that case there's no point in generating N documents and scenes along the way. You can create one document, append N meshes or mesh primitives to it, and then write the result: