From d7a354b313930ce1e8869665a047bf967fcfc532 Mon Sep 17 00:00:00 2001 From: Sean Lilley Date: Thu, 20 Apr 2017 10:07:01 -0400 Subject: [PATCH] Use temp directory --- lib/obj2gltf.js | 24 ++++++++++++++---------- lib/writeUris.js | 22 ++++++++++++---------- package.json | 1 + 3 files changed, 27 insertions(+), 20 deletions(-) diff --git a/lib/obj2gltf.js b/lib/obj2gltf.js index d6c74fc..3d4386b 100644 --- a/lib/obj2gltf.js +++ b/lib/obj2gltf.js @@ -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. */ diff --git a/lib/writeUris.js b/lib/writeUris.js index 53e23c3..1a578fd 100644 --- a/lib/writeUris.js +++ b/lib/writeUris.js @@ -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}); } diff --git a/package.json b/package.json index 74fd41f..6a45bfd 100644 --- a/package.json +++ b/package.json @@ -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": {