Loading a glb file with missing textures #1578
-
Hi gltf-tranform community! I am trying to use the gltf library embed textures in to glb files when our validator has given an error that textures are missing, but when using Here is the function in question: type EmbedFilesInput = {
glbBuffer: Buffer;
missingResources: {
bufferData: Buffer;
path: string;
}[];
};
export const embedFiles = async ({
glbBuffer,
missingResources,
}: EmbedFilesInput): Promise<Buffer> => {
const io = new NodeIO();
const document = await io.readBinary(new Uint8Array(glbBuffer));
const root = document.getRoot();
missingResources.forEach(({ bufferData, path }) => {
const resourceType = path.split('/')[1];
const resourceIndex = parseInt(path.split('/')[2], 10);
if (resourceType === 'buffers') {
const newBuffer = document
.createBuffer(`buffer-${resourceIndex}`)
.setURI(path);
root.listBuffers()[resourceIndex] = newBuffer;
} else if (resourceType === 'textures' || resourceType === 'images') {
const texture = root.listTextures()[resourceIndex];
if (!texture) {
console.log('no texture found at the given index');
throw new Error(
`Texture at index ${resourceIndex} does not exist in the glTF.`,
);
}
texture.setImage(new Uint8Array(bufferData));
} else {
console.log(`Unsupported resource type: ${resourceType}`);
throw new Error(`Unsupported resource type: ${resourceType}`);
}
});
const updatedGlb = await io.writeBinary(document);
return Buffer.from(updatedGlb);
}; |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Because glTF Transform does throw when trying to parse a .glb/.gltf with missing external resources, I think it would be necessary to parse the .glb header manually, copying the implementation here... glTF-Transform/packages/core/src/io/platform-io.ts Lines 262 to 291 in 7c9394b ... and then to manually embed the missing textures similar to this code... glTF-Transform/packages/core/src/io/platform-io.ts Lines 184 to 194 in 7c9394b Once you've added the missing entries to the |
Beta Was this translation helpful? Give feedback.
Because glTF Transform does throw when trying to parse a .glb/.gltf with missing external resources, I think it would be necessary to parse the .glb header manually, copying the implementation here...
glTF-Transform/packages/core/src/io/platform-io.ts
Lines 262 to 291 in 7c9394b