obj2gltf/lib/writeUris.js

128 lines
3.9 KiB
JavaScript
Raw Normal View History

'use strict';
2017-04-10 17:57:56 -04:00
var Cesium = require('cesium');
var fsExtra = require('fs-extra');
var mime = require('mime');
var path = require('path');
var Promise = require('bluebird');
var fsExtraOutputFile = Promise.promisify(fsExtra.outputFile);
2017-04-10 17:57:56 -04:00
var RuntimeError = Cesium.RuntimeError;
module.exports = writeUris;
/**
* Write glTF resources as embedded data uris or external files.
*
* @param {Object} gltf The glTF asset.
* @param {String} gltfPath Path where the glTF will be saved.
2017-04-10 17:57:56 -04:00
* @param {Object} options An object with the following properties:
* @param {Boolean} options.separate Writes out separate buffers.
* @param {Boolean} options.separateTextures Write out separate textures only.
* @returns {Promise} A promise that resolves to the glTF asset.
*
* @private
*/
2017-04-10 17:57:56 -04:00
function writeUris(gltf, gltfPath, options) {
var separate = options.separate;
var separateTextures = options.separateTextures;
var promises = [];
2017-04-18 11:56:08 -04:00
var buffer = gltf.buffers[0];
var bufferByteLength = buffer.extras._obj2gltf.source.length;
var texturesByteLength = 0;
var images = gltf.images;
2017-04-18 11:56:08 -04:00
var imagesLength = images.length;
for (var i = 0; i < imagesLength; ++i) {
texturesByteLength += images[i].extras._obj2gltf.source.length;
}
// Buffers larger than ~192MB cannot be base64 encoded due to a NodeJS limitation. Source: https://github.com/nodejs/node/issues/4266
var exceedsMaximum = (texturesByteLength + bufferByteLength > 201326580);
2017-04-10 17:57:56 -04:00
if (exceedsMaximum && !separate) {
return Promise.reject(new RuntimeError('Buffers and textures are too large to encode in the glTF, saving as separate resources.'));
}
2017-04-10 17:57:56 -04:00
if (separate) {
promises.push(writeSeparateBuffer(gltf, gltfPath));
} else {
writeEmbeddedBuffer(gltf);
}
2017-04-10 17:57:56 -04:00
if (separateTextures) {
promises.push(writeSeparateTextures(gltf, gltfPath));
} else {
writeEmbeddedTextures(gltf);
}
return Promise.all(promises)
.then(function() {
2017-03-21 14:37:52 -04:00
deleteExtras(gltf);
return gltf;
});
}
function deleteExtras(gltf) {
2017-04-18 11:56:08 -04:00
var buffer = gltf.buffers[0];
delete buffer.extras;
var images = gltf.images;
2017-04-18 11:56:08 -04:00
var imagesLength = images.length;
for (var i = 0; i < imagesLength; ++i) {
delete images[i].extras;
}
var materials = gltf.materials;
var materialsLength = materials.length;
for (var j = 0; j < materialsLength; ++j) {
delete materials[j].extras;
}
}
function writeSeparateBuffer(gltf, gltfPath) {
2017-04-18 11:56:08 -04:00
var buffer = gltf.buffers[0];
var source = buffer.extras._obj2gltf.source;
var bufferName = path.basename(gltfPath, path.extname(gltfPath));
var bufferUri = bufferName + '.bin';
buffer.uri = bufferUri;
var bufferPath = path.join(path.dirname(gltfPath), bufferUri);
return writeUris._outputFile(bufferPath, source);
}
function writeSeparateTextures(gltf, gltfPath) {
var images = gltf.images;
2017-04-18 11:56:08 -04:00
return Promise.map(images, function(image) {
2017-04-10 17:57:56 -04:00
var extras = image.extras._obj2gltf;
var imageUri = image.name + extras.extension;
image.uri = imageUri;
var imagePath = path.join(path.dirname(gltfPath), imageUri);
return writeUris._outputFile(imagePath, extras.source);
}, {concurrency : 10});
}
function writeEmbeddedBuffer(gltf) {
2017-04-18 11:56:08 -04:00
var buffer = gltf.buffers[0];
var source = buffer.extras._obj2gltf.source;
buffer.uri = 'data:application/octet-stream;base64,' + source.toString('base64');
}
function writeEmbeddedTextures(gltf) {
var images = gltf.images;
2017-04-18 11:56:08 -04:00
var imagesLength = images.length;
for (var i = 0; i < imagesLength; ++i) {
var image = images[i];
var extras = image.extras._obj2gltf;
image.uri = 'data:' + mime.lookup(extras.extension) + ';base64,' + extras.source.toString('base64');
}
}
/**
2017-04-04 11:32:18 -04:00
* Exposed for testing.
*
* @private
*/
writeUris._outputFile = fsExtraOutputFile;