Skip to content

Instantly share code, notes, and snippets.

@timknip
Last active November 26, 2020 20:06
Show Gist options
  • Save timknip/4ae9dbeda56f9f110bb43a3a88192b46 to your computer and use it in GitHub Desktop.
Save timknip/4ae9dbeda56f9f110bb43a3a88192b46 to your computer and use it in GitHub Desktop.
import { Document, NodeIO } from '@gltf-transform/core';
import { DracoMeshCompression } from '@gltf-transform/extensions';
import * as draco3d from 'draco3dgltf';
export async function draco_encode_issue (useTimeout = false) {
const doc = new Document();
const buffer = doc.createBuffer('bin');
const accessor = doc.createAccessor('positions')
.setArray(new Float32Array([
0, 0, 0,
1, 0, 0,
1, 1, 0
]))
.setType(Accessor.Type.VEC3)
.setBuffer(buffer);
const indices = doc.createAccessor('indices')
.setArray(new Uint16Array([0, 1, 2]))
.setType(Accessor.Type.SCALAR)
.setBuffer(buffer);
const material = doc.createMaterial('material')
.setBaseColorFactor([1, 0, 0, 1]);
const primitive = doc.createPrimitive()
.setAttribute('POSITION', accessor)
.setIndices(indices)
.setMaterial(material);
const mesh = doc.createMesh('mesh')
.addPrimitive(primitive);
const node = doc.createNode('node')
.setMesh(mesh);
const scene = doc.createScene('scene')
.addChild(node);
doc.createExtension(DracoMeshCompression).setRequired(true);
const io = new NodeIO()
.registerExtensions([DracoMeshCompression])
.registerDependencies({
'draco3d.decoder': draco3d.createDecoderModule(),
'draco3d.encoder': draco3d.createEncoderModule(),
});
if (useTimeout) {
setTimeout(() => {
io.write('draco_extension_issue.glb', doc);
}, 1000);
} else {
// throws: "TypeError: Cannot read property 'apply' of undefined"
io.write('draco_extension_issue.glb', doc);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment