Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Write gltf changes #95

Merged
merged 1 commit into from
Jun 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/getBinaryGltf.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function updateBinaryObject(gltf, pipelineExtras, name, state) {
state.currentBinaryView++;
}

var objectSource = object.extras._pipeline.source;
var objectSource = new Buffer(object.extras._pipeline.source);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Buffer.concat below didn't like concatting a shader string with a buffer, so I just always wrap the source in a Buffer.

var bufferViewId = 'binary_bufferView' + state.currentBinaryView;
KHR_binary_glTF.bufferView = bufferViewId; //Create bufferview
bufferViews[bufferViewId] = {
Expand Down
21 changes: 14 additions & 7 deletions lib/writeBinaryGltf.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,25 @@ function writeBinaryGltf(gltf, outputPath, createDirectory, callback) {
// Correct output path extension if necessary
var outputExtension = path.extname(outputPath);
if (outputExtension !== '.glb') {
outputPath = path.basename(outputPath, outputExtension) + '.glb';
outputPath = path.join(path.dirname(outputPath), path.basename(outputPath) + '.glb');
}
// Create the output directory if specified
if (createDirectory) {
outputPath = path.join(path.dirname(outputPath), 'output', path.basename(outputPath));
mkdirp.sync(path.dirname(outputPath));
}
mkdirp.sync(path.dirname(outputPath));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change includes all the deleted specs. I thought it was weird that the pipeline won't export a gltf file unless createDirectory is true, especially since there are cases where you don't want the file to be put in an output folder.


var glb = getBinaryGltf(gltf, callback);
fs.writeFile(outputPath, glb, function (err) {
if (err) {
callback(err);
}
var glb = getBinaryGltf(gltf, function(header, scene, body) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This callback was throwing off callbacks that expect an error in the first argument, but instead got the header, scene, body instead.

fs.writeFile(outputPath, glb, function (err) {
if (err) {
if (callback) {
callback(err);
} else {
throw err;
}
} else if (callback) {
callback(undefined, header, scene, body);
}
});
});
}
2 changes: 1 addition & 1 deletion lib/writeGltf.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ function writeGltf(gltf, outputPath, embed, embedImage, createDirectory, callbac
//Create the output directory if specified
if (createDirectory) {
outputPath = path.join(path.dirname(outputPath), 'output', path.basename(outputPath));
mkdirp.sync(path.dirname(outputPath));
}
mkdirp.sync(path.dirname(outputPath));
var basePath = path.dirname(outputPath);

async.each(['buffers', 'images', 'shaders'], function(name, asyncCallback) {
Expand Down
12 changes: 3 additions & 9 deletions specs/lib/writeBinaryGltfSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ describe('writeBinaryGltf', function() {
});

it('writes a valid binary gltf header', function() {
writeBinaryGltf(clone(testData.gltf), outputPath, true, function(header, scene, body) {
writeBinaryGltf(clone(testData.gltf), outputPath, true, function(err, header, scene, body) {
expect(header.toString('utf8', 0, 4)).toEqual('glTF');
expect(header.readUInt32LE(4)).toEqual(1);
expect(header.readUInt32LE(8)).toEqual(17706);
Expand All @@ -79,21 +79,15 @@ describe('writeBinaryGltf', function() {
});

it('writes the correct binary scene', function() {
writeBinaryGltf(clone(testData.gltf), outputPath, true, function(header, scene, body) {
writeBinaryGltf(clone(testData.gltf), outputPath, true, function(err, header, scene, body) {
expect(JSON.parse(scene.toString())).toEqual(testData.scene);
});
});

it('writes the correct binary body', function() {
writeBinaryGltf(clone(testData.gltf), outputPath, true, function(header, scene, body) {
writeBinaryGltf(clone(testData.gltf), outputPath, true, function(err, header, scene, body) {
var binaryBody = Buffer.concat([testData.buffer, testData.fragmentShader, testData.vertexShader, testData.image]);
expect(bufferEqual(binaryBody, body)).toBe(true);
})
});

it('throws an error', function() {
writeBinaryGltf(clone(testData.gltf), './specs/errorFilePath/output.gltf', false, function(err) {
expect(err).toBeDefined();
});
});
});
9 changes: 0 additions & 9 deletions specs/lib/writeBuffersSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,4 @@ describe('writeBuffers', function() {
done();
});
});

it('throws an error', function(done) {
var gltf = clone(testGltf);

writeGltf(gltf, './specs/errorFilePath/output.gltf', false, false, false, function(err) {
expect(err).toBeDefined();
done();
});
});
});
9 changes: 0 additions & 9 deletions specs/lib/writeImagesSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,4 @@ describe('writeImages', function() {
done();
});
});

it('throws an error', function(done) {
var gltf = clone(testGltf);

writeGltf(gltf, './specs/errorFilePath/output.gltf', false, false, false, function(err) {
expect(err).toBeDefined();
done();
});
});
});
9 changes: 0 additions & 9 deletions specs/lib/writeShadersSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,4 @@ describe('writeShaders', function() {
done();
});
});

it('throws an error', function(done) {
var gltf = clone(testGltf);

writeGltf(gltf, './specs/errorFilePath/output.gltf', false, false, false, function(err) {
expect(err).toBeDefined();
done();
});
});
});