Use temp directory

This commit is contained in:
Sean Lilley 2017-04-20 10:07:01 -04:00
parent a6ff230fc3
commit d7a354b313
3 changed files with 27 additions and 20 deletions

View File

@ -2,8 +2,10 @@
var Cesium = require('cesium'); var Cesium = require('cesium');
var fsExtra = require('fs-extra'); var fsExtra = require('fs-extra');
var GltfPipeline = require('gltf-pipeline').Pipeline; var GltfPipeline = require('gltf-pipeline').Pipeline;
var os = require('os');
var path = require('path'); var path = require('path');
var Promise = require('bluebird'); var Promise = require('bluebird');
var uuid = require('uuid');
var createGltf = require('./createGltf'); var createGltf = require('./createGltf');
var loadObj = require('./loadObj'); var loadObj = require('./loadObj');
var writeUris = require('./writeUris'); var writeUris = require('./writeUris');
@ -73,7 +75,6 @@ function obj2gltf(objPath, gltfPath, options) {
} }
var extension = path.extname(gltfPath).toLowerCase(); var extension = path.extname(gltfPath).toLowerCase();
var basePath = path.dirname(gltfPath);
var modelName = path.basename(gltfPath, path.extname(gltfPath)); var modelName = path.basename(gltfPath, path.extname(gltfPath));
if (extension === '.glb') { if (extension === '.glb') {
binary = true; binary = true;
@ -84,13 +85,14 @@ function obj2gltf(objPath, gltfPath, options) {
} }
gltfPath = path.join(path.dirname(gltfPath), modelName + extension); gltfPath = path.join(path.dirname(gltfPath), modelName + extension);
var resourcesDirectory = options.bypassPipeline ? path.dirname(gltfPath) : getTempDirectory();
var aoOptions = ao ? {} : undefined; var aoOptions = ao ? {} : undefined;
var kmcOptions = kmc ? {} : undefined; var kmcOptions = kmc ? {} : undefined;
var pipelineOptions = { var pipelineOptions = {
createDirectory : false, createDirectory : false,
basePath : basePath, basePath : resourcesDirectory,
binary : binary, binary : binary,
embed : !separate, embed : !separate,
embedImage : !separateTextures, embedImage : !separateTextures,
@ -110,7 +112,7 @@ function obj2gltf(objPath, gltfPath, options) {
return createGltf(objData); return createGltf(objData);
}) })
.then(function(gltf) { .then(function(gltf) {
return writeUris(gltf, gltfPath, options); return writeUris(gltf, gltfPath, resourcesDirectory, options);
}) })
.then(function(gltf) { .then(function(gltf) {
if (bypassPipeline) { if (bypassPipeline) {
@ -120,20 +122,22 @@ function obj2gltf(objPath, gltfPath, options) {
} }
}) })
.then(function() { .then(function() {
return cleanup(gltfPath, options); return cleanup(resourcesDirectory, options);
}); });
} }
function cleanup(gltfPath, options) { function cleanup(resourcesDirectory, options) {
// gltf-pipeline also saves out a buffer so remove the one generated by obj2gltf
if (!options.bypassPipeline && options.separate) { if (!options.bypassPipeline && options.separate) {
var bufferName = path.basename(gltfPath, path.extname(gltfPath)); return fsExtraRemove(resourcesDirectory);
var bufferUri = bufferName + '.bin';
var bufferPath = path.join(path.dirname(gltfPath), bufferUri);
return fsExtraRemove(bufferPath);
} }
} }
function getTempDirectory() {
var tempDirectory = os.tmpdir();
var randomId = uuid.v4();
return path.join(tempDirectory, randomId);
}
/** /**
* Default values that will be used when calling obj2gltf(options) unless specified in the options object. * Default values that will be used when calling obj2gltf(options) unless specified in the options object.
*/ */

View File

@ -16,6 +16,7 @@ module.exports = writeUris;
* *
* @param {Object} gltf The glTF asset. * @param {Object} gltf The glTF asset.
* @param {String} gltfPath Path where the glTF will be saved. * @param {String} gltfPath Path where the glTF will be saved.
* @param {String} resourcesDirectory Path where separate resources will be saved.
* @param {Object} options An object with the following properties: * @param {Object} options An object with the following properties:
* @param {Boolean} options.separate Writes out separate buffers. * @param {Boolean} options.separate Writes out separate buffers.
* @param {Boolean} options.separateTextures Write out separate textures only. * @param {Boolean} options.separateTextures Write out separate textures only.
@ -23,7 +24,7 @@ module.exports = writeUris;
* *
* @private * @private
*/ */
function writeUris(gltf, gltfPath, options) { function writeUris(gltf, gltfPath, resourcesDirectory, options) {
var separate = options.separate; var separate = options.separate;
var separateTextures = options.separateTextures; var separateTextures = options.separateTextures;
@ -44,17 +45,19 @@ function writeUris(gltf, gltfPath, options) {
var exceedsMaximum = (texturesByteLength + bufferByteLength > 201326580); var exceedsMaximum = (texturesByteLength + bufferByteLength > 201326580);
if (exceedsMaximum && !separate) { if (exceedsMaximum && !separate) {
return Promise.reject(new RuntimeError('Buffers and textures are too large to encode in the glTF, saving as separate resources.')); return Promise.reject(new RuntimeError('Buffers and textures are too large to encode in the glTF. Use the --separate flag instead.'));
} }
var name = path.basename(gltfPath, path.extname(gltfPath));
if (separate) { if (separate) {
promises.push(writeSeparateBuffer(gltf, gltfPath)); promises.push(writeSeparateBuffer(gltf, resourcesDirectory, name));
} else { } else {
writeEmbeddedBuffer(gltf); writeEmbeddedBuffer(gltf);
} }
if (separateTextures) { if (separateTextures) {
promises.push(writeSeparateTextures(gltf, gltfPath)); promises.push(writeSeparateTextures(gltf, resourcesDirectory));
} else { } else {
writeEmbeddedTextures(gltf); writeEmbeddedTextures(gltf);
} }
@ -79,24 +82,23 @@ function deleteExtras(gltf) {
} }
} }
function writeSeparateBuffer(gltf, gltfPath) { function writeSeparateBuffer(gltf, resourcesDirectory, name) {
var buffer = gltf.buffers[Object.keys(gltf.buffers)[0]]; var buffer = gltf.buffers[Object.keys(gltf.buffers)[0]];
var source = buffer.extras._obj2gltf.source; var source = buffer.extras._obj2gltf.source;
var bufferName = path.basename(gltfPath, path.extname(gltfPath)); var bufferUri = name + '.bin';
var bufferUri = bufferName + '.bin';
buffer.uri = bufferUri; buffer.uri = bufferUri;
var bufferPath = path.join(path.dirname(gltfPath), bufferUri); var bufferPath = path.join(resourcesDirectory, bufferUri);
return writeUris._outputFile(bufferPath, source); return writeUris._outputFile(bufferPath, source);
} }
function writeSeparateTextures(gltf, gltfPath) { function writeSeparateTextures(gltf, resourcesDirectory) {
var images = gltf.images; var images = gltf.images;
return Promise.map(Object.keys(images), function(id) { return Promise.map(Object.keys(images), function(id) {
var image = images[id]; var image = images[id];
var extras = image.extras._obj2gltf; var extras = image.extras._obj2gltf;
var imageUri = image.name + extras.extension; var imageUri = image.name + extras.extension;
image.uri = imageUri; image.uri = imageUri;
var imagePath = path.join(path.dirname(gltfPath), imageUri); var imagePath = path.join(resourcesDirectory, imageUri);
return writeUris._outputFile(imagePath, extras.source); return writeUris._outputFile(imagePath, extras.source);
}, {concurrency : 10}); }, {concurrency : 10});
} }

View File

@ -33,6 +33,7 @@
"gltf-pipeline": "^0.1.0-alpha11", "gltf-pipeline": "^0.1.0-alpha11",
"mime": "^1.3.4", "mime": "^1.3.4",
"pngjs": "^3.0.1", "pngjs": "^3.0.1",
"uuid": "^3.0.1",
"yargs": "^7.0.1" "yargs": "^7.0.1"
}, },
"devDependencies": { "devDependencies": {