Skip to content

Commit

Permalink
Merge pull request #253 from AnalyticalGraphicsInc/glbSpecialChars
Browse files Browse the repository at this point in the history
updated getBinaryGltf for gltfs with special characters
  • Loading branch information
lilleyse authored Apr 6, 2017
2 parents bf401ef + fd4ae6a commit 6914f5f
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions lib/getBinaryGltf.js
Original file line number Diff line number Diff line change
Expand Up @@ -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] = {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 6914f5f

Please sign in to comment.