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 fsExtra = require('fs-extra');
var GltfPipeline = require('gltf-pipeline').Pipeline;
var os = require('os');
var path = require('path');
var Promise = require('bluebird');
var uuid = require('uuid');
var createGltf = require('./createGltf');
var loadObj = require('./loadObj');
var writeUris = require('./writeUris');
@ -73,7 +75,6 @@ function obj2gltf(objPath, gltfPath, options) {
}
var extension = path.extname(gltfPath).toLowerCase();
var basePath = path.dirname(gltfPath);
var modelName = path.basename(gltfPath, path.extname(gltfPath));
if (extension === '.glb') {
binary = true;
@ -84,13 +85,14 @@ function obj2gltf(objPath, gltfPath, options) {
}
gltfPath = path.join(path.dirname(gltfPath), modelName + extension);
var resourcesDirectory = options.bypassPipeline ? path.dirname(gltfPath) : getTempDirectory();
var aoOptions = ao ? {} : undefined;
var kmcOptions = kmc ? {} : undefined;
var pipelineOptions = {
createDirectory : false,
basePath : basePath,
basePath : resourcesDirectory,
binary : binary,
embed : !separate,
embedImage : !separateTextures,
@ -110,7 +112,7 @@ function obj2gltf(objPath, gltfPath, options) {
return createGltf(objData);
})
.then(function(gltf) {
return writeUris(gltf, gltfPath, options);
return writeUris(gltf, gltfPath, resourcesDirectory, options);
})
.then(function(gltf) {
if (bypassPipeline) {
@ -120,20 +122,22 @@ function obj2gltf(objPath, gltfPath, options) {
}
})
.then(function() {
return cleanup(gltfPath, options);
return cleanup(resourcesDirectory, options);
});
}
function cleanup(gltfPath, options) {
// gltf-pipeline also saves out a buffer so remove the one generated by obj2gltf
function cleanup(resourcesDirectory, options) {
if (!options.bypassPipeline && options.separate) {
var bufferName = path.basename(gltfPath, path.extname(gltfPath));
var bufferUri = bufferName + '.bin';
var bufferPath = path.join(path.dirname(gltfPath), bufferUri);
return fsExtraRemove(bufferPath);
return fsExtraRemove(resourcesDirectory);
}
}
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.
*/

View File

@ -16,6 +16,7 @@ module.exports = writeUris;
*
* @param {Object} gltf The glTF asset.
* @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 {Boolean} options.separate Writes out separate buffers.
* @param {Boolean} options.separateTextures Write out separate textures only.
@ -23,7 +24,7 @@ module.exports = writeUris;
*
* @private
*/
function writeUris(gltf, gltfPath, options) {
function writeUris(gltf, gltfPath, resourcesDirectory, options) {
var separate = options.separate;
var separateTextures = options.separateTextures;
@ -44,17 +45,19 @@ function writeUris(gltf, gltfPath, options) {
var exceedsMaximum = (texturesByteLength + bufferByteLength > 201326580);
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) {
promises.push(writeSeparateBuffer(gltf, gltfPath));
promises.push(writeSeparateBuffer(gltf, resourcesDirectory, name));
} else {
writeEmbeddedBuffer(gltf);
}
if (separateTextures) {
promises.push(writeSeparateTextures(gltf, gltfPath));
promises.push(writeSeparateTextures(gltf, resourcesDirectory));
} else {
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 source = buffer.extras._obj2gltf.source;
var bufferName = path.basename(gltfPath, path.extname(gltfPath));
var bufferUri = bufferName + '.bin';
var bufferUri = name + '.bin';
buffer.uri = bufferUri;
var bufferPath = path.join(path.dirname(gltfPath), bufferUri);
var bufferPath = path.join(resourcesDirectory, bufferUri);
return writeUris._outputFile(bufferPath, source);
}
function writeSeparateTextures(gltf, gltfPath) {
function writeSeparateTextures(gltf, resourcesDirectory) {
var images = gltf.images;
return Promise.map(Object.keys(images), function(id) {
var image = images[id];
var extras = image.extras._obj2gltf;
var imageUri = image.name + extras.extension;
image.uri = imageUri;
var imagePath = path.join(path.dirname(gltfPath), imageUri);
var imagePath = path.join(resourcesDirectory, imageUri);
return writeUris._outputFile(imagePath, extras.source);
}, {concurrency : 10});
}

View File

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