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

Web3d quantized attributes #56

Merged
merged 40 commits into from
Jun 10, 2016
Merged
Show file tree
Hide file tree
Changes from 38 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
0960548
Added quantize attributes function and test skeleton
lasalvavida May 13, 2016
78caba2
A few tweaks
lasalvavida Jun 1, 2016
14b5491
Merge branch 'master' of github.com:AnalyticalGraphicsInc/gltf-pipeli…
lasalvavida Jun 3, 2016
ff81e8f
Added getBinaryGltf to return buffer
lasalvavida Jun 3, 2016
f83a5df
Made writeBinaryGltf use getBinaryGltf
lasalvavida Jun 3, 2016
21ab7a6
Fixed some jsHint errors
lasalvavida Jun 3, 2016
8588875
Split updateBinaryObject out into its own function
lasalvavida Jun 6, 2016
4b83b4d
Body referenced form pipelineExtras
lasalvavida Jun 6, 2016
0f66720
Split getQuantizableAttributes into its own function
lasalvavida Jun 6, 2016
8eaedab
Added addExtensionsUsed utility
lasalvavida Jun 6, 2016
36e5e07
Lots of cleanup for quantizeAttributes
lasalvavida Jun 7, 2016
8596b8e
Dropped unnecessary else
lasalvavida Jun 7, 2016
9a893aa
Added packBuffers for minimizing buffer space
lasalvavida Jun 7, 2016
4b2c2b5
Reverted removeUnusedVertices
lasalvavida Jun 7, 2016
c5c4845
Made decode matrix 0's floats and simplified options
lasalvavida Jun 7, 2016
9183718
More updates
lasalvavida Jun 7, 2016
92280c8
Fixed jsHint errors
lasalvavida Jun 7, 2016
cc98b05
Added tests for packBuffers
lasalvavida Jun 7, 2016
9ad9112
Updated packBuffers
lasalvavida Jun 7, 2016
e045dd3
Added quantized attribute tests
lasalvavida Jun 7, 2016
d0569ce
Tweaks
lasalvavida Jun 7, 2016
6a17276
Removed quantized data directory - it isn't needed
lasalvavida Jun 7, 2016
0397d54
Fixes to precision and test
lasalvavida Jun 7, 2016
f4be044
Merge branch 'master' of github.com:AnalyticalGraphicsInc/gltf-pipeli…
lasalvavida Jun 7, 2016
466a802
Updates
lasalvavida Jun 8, 2016
d0c4214
Updated packBuffers references
lasalvavida Jun 8, 2016
f134385
Removed gltf from example header
lasalvavida Jun 8, 2016
be8790e
Use process.nextTick
lasalvavida Jun 8, 2016
e228777
Missed a change
lasalvavida Jun 8, 2016
09d39da
See if re-ordering requires fixes CI failure
lasalvavida Jun 8, 2016
deb1c54
Updated travis node to v4
lasalvavida Jun 8, 2016
5d7e99f
Pulled tweaks from removeUnusedAttributes branch
lasalvavida Jun 8, 2016
c4d3626
Merge branch 'master' of github.com:AnalyticalGraphicsInc/gltf-pipeli…
lasalvavida Jun 9, 2016
6071c9e
Added quantized option to command line
lasalvavida Jun 9, 2016
84dbc41
jsHint fix
lasalvavida Jun 9, 2016
24dca8b
Removed packBuffers
lasalvavida Jun 9, 2016
2ab58d3
Merge branch 'master' of github.com:AnalyticalGraphicsInc/gltf-pipeli…
lasalvavida Jun 9, 2016
74f46e8
Removed old packBufferSpec file
lasalvavida Jun 9, 2016
539fc23
Make sure SCALAR is supported
lasalvavida Jun 10, 2016
05d8ad5
Updated README
lasalvavida Jun 10, 2016
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
7 changes: 5 additions & 2 deletions bin/gltf-pipeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ if (process.argv.length < 3 || defined(argv.h) || defined(argv.help)) {
' -i, input=PATH Read unoptimized glTF from the specified file.\n ' +
' -o, output=PATH write optimized glTF to the specified file.\n' +
' -b, write binary glTF file.\n' +
' -s, writes out separate geometry/animation data files, shader files and textures instead of embedding them in the glTF file.\n ';
' -s, writes out separate geometry/animation data files, shader files and textures instead of embedding them in the glTF file.\n' +
' -q, quantize the attributes of this model.\n';
Copy link
Contributor

Choose a reason for hiding this comment

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

Also update README.md please.

process.stdout.write(help);
return;
}
Expand All @@ -31,6 +32,7 @@ var filePath = path.dirname(gltfPath);
var outputPath = defaultValue(argv._[1], argv.o);
var isSeparate = defaultValue(argv.s, false);
var isBinary = defaultValue(argv.b, false);
var isQuantized = defaultValue(argv.q, false);

if (!defined(gltfPath)) {
throw new DeveloperError('Input path is undefined.');
Expand All @@ -47,7 +49,8 @@ if (!defined(outputPath)) {

var options = {
isBinary : isBinary,
isEmbedded : !isSeparate
isEmbedded : !isSeparate,
isQuantized : isQuantized
};

processFileToDisk(gltfPath, outputPath, options);
16 changes: 16 additions & 0 deletions lib/addExtensionsUsed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';
var Cesium = require('cesium');
var defined = Cesium.defined;

module.exports = addExtensionsUsed;

function addExtensionsUsed(gltf, extension) {
var extensionsUsed = gltf.extensionsUsed;
if (!defined(extensionsUsed)) {
extensionsUsed = [];
gltf.extensionsUsed = extensionsUsed;
}
if (extensionsUsed.indexOf(extension) < 0) {
extensionsUsed.push(extension);
}
}
109 changes: 109 additions & 0 deletions lib/getBinaryGltf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
'use strict';
var Cesium = require('cesium');
var defined = Cesium.defined;
var defaultValue = Cesium.defaultValue;
var sizeOf = require('image-size');
var mime = require('mime');
var mergeBuffers = require('./mergeBuffers');
var removePipelineExtras = require('./removePipelineExtras');

module.exports = getBinaryGltf;

//Update object with KHR_binary_glTF properties and add to body and bufferViews
function updateBinaryObject(gltf, pipelineExtras, name, state) {
var bufferViews = gltf.bufferViews;
var objects = gltf[name];
if (defined(objects)) {
for (var objectId in objects) {
if (objects.hasOwnProperty(objectId)) {
var object = objects[objectId];

//Update object with binary format
object.uri = 'data:,';
object.extensions = defaultValue(object.extensions, {});
object.extensions.KHR_binary_glTF = defaultValue(object.extensions.KHR_binary_glTF, {});
var KHR_binary_glTF = object.extensions.KHR_binary_glTF;

//Create a bufferView based on the byte length and current offset
var bufferViewKeys = Object.keys(bufferViews);
while (bufferViewKeys.indexOf('binary_bufferView' + state.currentBinaryView) != -1) {
state.currentBinaryView++;
}

var objectSource = object.extras._pipeline.source;
var bufferViewId = 'binary_bufferView' + state.currentBinaryView;
KHR_binary_glTF.bufferView = bufferViewId; //Create bufferview
bufferViews[bufferViewId] = {
buffer : 'binary_glTF',
byteLength : objectSource.length,
byteOffset : state.offset
};
state.offset += objectSource.length;

//Append the object source to the binary body
pipelineExtras.source = Buffer.concat([pipelineExtras.source, objectSource]);

//Add additional properties for images
if (name === 'images') {
KHR_binary_glTF.mimeType = mime.lookup(object.extras._pipeline.extension);

var dimensions = sizeOf(object.extras._pipeline.source);
KHR_binary_glTF.width = dimensions.width;
KHR_binary_glTF.height = dimensions.height;
}
}
}
}
}

function getBinaryGltf(gltf, callback) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Are there unit tests for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There are unit tests for writeBinaryGltf which uses this directly.

Copy link
Contributor

Choose a reason for hiding this comment

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

Sounds good, can you double check the coverage and update if needed?

//Create the special binary buffer from the existing buffers
gltf.bufferViews = defaultValue(gltf.bufferViews, {});
gltf.buffers = defaultValue(gltf.buffers, {});
mergeBuffers(gltf, 'binary_glTF');

var buffers = gltf.buffers;
var currentOffset = buffers.binary_glTF.byteLength;
var pipelineExtras = buffers.binary_glTF.extras._pipeline;
var state = {
offset : currentOffset,
currentBinaryView : 0
};

updateBinaryObject(gltf, pipelineExtras, 'shaders', state);
updateBinaryObject(gltf, pipelineExtras, 'images', state);

var body = buffers.binary_glTF.extras._pipeline.source;
buffers.binary_glTF.byteLength = state.offset;

//Remove extras objects before writing
removePipelineExtras(gltf);

//Create padded binary scene string 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 bodyOffset = 20 + sceneLength;
var glbLength = bodyOffset + body.length;

//Write binary glTF header (magic, version, length, sceneLength, sceneFormat)
var header = new Buffer(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);
var glb = Buffer.concat([header, scene, body], glbLength);

if (callback) {
process.nextTick(function () {
callback(header, scene, body);
});
}
return glb;
}
5 changes: 5 additions & 0 deletions lib/gltfPipeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ var writeBinaryGltf = require('./writeBinaryGltf');
var readGltf = require('./readGltf');
var removePipelineExtras = require('./removePipelineExtras');
var loadGltfUris = require('./loadGltfUris');
var quantizeAttributes = require('./quantizeAttributes');
var Cesium = require('cesium');
var defaultValue = Cesium.defaultValue;
var defined = Cesium.defined;
Expand Down Expand Up @@ -72,6 +73,10 @@ function processJSONWithExtras(gltfWithExtras, options, callback) {
//Run removeUnused stage again after all pipeline stages have been run to remove objects that become unused
removeUnused(gltfWithExtras);

if (options.isQuantized) {
quantizeAttributes(gltfWithExtras);
}

callback(gltfWithExtras);
}

Expand Down
Loading