diff --git a/lib/getBinaryGltf.js b/lib/getBinaryGltf.js index 366c99f2..5f248961 100644 --- a/lib/getBinaryGltf.js +++ b/lib/getBinaryGltf.js @@ -32,7 +32,7 @@ function updateBinaryObject(gltf, objects, name, pipelineExtras, state) { state.currentBinaryView++; } - var objectSource = new Buffer(object.extras._pipeline.source); + var objectSource = Buffer.from(object.extras._pipeline.source); var bufferViewId = 'binary_bufferView' + state.currentBinaryView; KHR_binary_glTF.bufferView = bufferViewId; //Create bufferview bufferViews[bufferViewId] = { @@ -118,25 +118,27 @@ function getBinaryGltf(gltf, embed, embedImage) { // Remove extras objects before writing removePipelineExtras(gltf); - // Create padded binary scene string and calculate total length + // Create padded binary scene buffer and calculate total length var sceneString = JSON.stringify(gltf); - var sceneLength = Buffer.byteLength(sceneString); - sceneLength += 4 - (sceneLength % 4); - var padding = new Array(sceneLength + 1).join(' '); - sceneString = (sceneString + padding).substring(0, sceneLength); + var sceneBuffer = Buffer.from(sceneString); + var sceneLength = sceneBuffer.length; + var paddingLength = 4 - (sceneLength % 4); // pad out to 4 bytes as per glb specification + sceneLength += paddingLength; + var paddingBuffer = Buffer.alloc(paddingLength, ' '); + var scene = Buffer.concat([sceneBuffer, paddingBuffer]); + var bodyOffset = 20 + sceneLength; var glbLength = bodyOffset + body.length; // Write binary glTF header (magic, version, length, sceneLength, sceneFormat) - var header = new Buffer(20); + var header = Buffer.alloc(20); header.write('glTF', 0); header.writeUInt32LE(1, 4); header.writeUInt32LE(glbLength, 8); header.writeUInt32LE(sceneLength, 12); header.writeUInt32LE(0, 16); - // Create scene buffer and overall buffer - var scene = new Buffer(sceneString); + // Create overall buffer var glb = Buffer.concat([header, scene, body], glbLength); return {