2017-03-13 15:28:51 -04:00
|
|
|
'use strict';
|
|
|
|
var Cesium = require('cesium');
|
|
|
|
var fsExtra = require('fs-extra');
|
2016-08-25 13:48:34 -04:00
|
|
|
var GltfPipeline = require('gltf-pipeline').Pipeline;
|
2017-03-13 15:28:51 -04:00
|
|
|
var path = require('path');
|
|
|
|
var Promise = require('bluebird');
|
2016-06-09 13:33:08 -04:00
|
|
|
var createGltf = require('./gltf');
|
2017-03-13 15:28:51 -04:00
|
|
|
var loadObj = require('./obj');
|
2017-03-17 15:44:01 -04:00
|
|
|
var writeUris = require('./writeUris');
|
2017-03-13 15:28:51 -04:00
|
|
|
|
|
|
|
var fsExtraOutputJson = Promise.promisify(fsExtra.outputJson);
|
|
|
|
|
2016-06-09 13:33:08 -04:00
|
|
|
var defaultValue = Cesium.defaultValue;
|
2017-03-13 15:28:51 -04:00
|
|
|
var defined = Cesium.defined;
|
|
|
|
var DeveloperError = Cesium.DeveloperError;
|
2016-06-09 13:33:08 -04:00
|
|
|
|
|
|
|
module.exports = convert;
|
|
|
|
|
2017-03-13 15:28:51 -04:00
|
|
|
/**
|
|
|
|
* Converts an obj file to a glTF file.
|
|
|
|
*
|
|
|
|
* @param {String} objPath Path to the obj file.
|
|
|
|
* @param {String} gltfPath Path of the converted glTF file.
|
|
|
|
* @param {Object} [options] An object with the following properties:
|
|
|
|
* @param {Boolean} [options.binary=false] Save as binary glTF.
|
2017-03-14 16:42:42 -04:00
|
|
|
* @param {Boolean} [options.separate=false] Writes out separate geometry data files, shader files, and textures instead of embedding them in the glTF.
|
2017-03-13 15:28:51 -04:00
|
|
|
* @param {Boolean} [options.separateTextures=false] Write out separate textures only.
|
|
|
|
* @param {Boolean} [options.compress=false] Quantize positions, compress texture coordinates, and oct-encode normals.
|
|
|
|
* @param {Boolean} [options.optimize=false] Use the optimization stages in the glTF pipeline.
|
|
|
|
* @param {Boolean} [options.optimizeForCesium=false] Optimize the glTF for Cesium by using the sun as a default light source.
|
|
|
|
* @param {Boolean} [options.generateNormals=false] Generate normals if they are missing.
|
|
|
|
* @param {Boolean} [options.ao=false] Apply ambient occlusion to the converted model.
|
|
|
|
* @param {Boolean} [options.textureCompressionOptions] Options sent to the compressTextures stage of gltf-pipeline.
|
|
|
|
* @param {Boolean} [options.bypassPipeline=false] Bypass the gltf-pipeline for debugging purposes. This option overrides many of the options above and will save the glTF with the KHR_materials_common extension.
|
2017-03-21 14:37:52 -04:00
|
|
|
* @param {Boolean} [options.hasTransparency=false] Do a more exhaustive check for texture transparency by looking at the alpha channel of each pixel.
|
2017-03-13 15:28:51 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
function convert(objPath, gltfPath, options) {
|
|
|
|
options = defaultValue(options, defaultValue.EMPTY_OBJECT);
|
2016-06-09 13:33:08 -04:00
|
|
|
var binary = defaultValue(options.binary, false);
|
2017-03-13 15:28:51 -04:00
|
|
|
var separate = defaultValue(options.separate, false);
|
2017-03-14 16:42:42 -04:00
|
|
|
var separateTextures = defaultValue(options.separateTextures, false) || separate;
|
2016-08-25 13:48:34 -04:00
|
|
|
var compress = defaultValue(options.compress, false);
|
2017-03-13 15:28:51 -04:00
|
|
|
var optimize = defaultValue(options.optimize, false);
|
2016-07-25 22:12:41 -04:00
|
|
|
var optimizeForCesium = defaultValue(options.optimizeForCesium, false);
|
2017-03-13 15:28:51 -04:00
|
|
|
var generateNormals = defaultValue(options.generateNormals, false);
|
|
|
|
var ao = defaultValue(options.ao, false);
|
|
|
|
var textureCompressionOptions = options.textureCompressionOptions;
|
|
|
|
var bypassPipeline = defaultValue(options.bypassPipeline, false);
|
2016-06-09 13:33:08 -04:00
|
|
|
|
2017-03-13 15:28:51 -04:00
|
|
|
if (!defined(objPath)) {
|
|
|
|
throw new DeveloperError('objPath is required');
|
2016-06-09 13:33:08 -04:00
|
|
|
}
|
|
|
|
|
2017-03-13 15:28:51 -04:00
|
|
|
if (!defined(gltfPath)) {
|
|
|
|
throw new DeveloperError('gltfPath is required');
|
2016-06-09 13:33:08 -04:00
|
|
|
}
|
|
|
|
|
2017-03-21 11:29:02 -04:00
|
|
|
var basePath = path.dirname(gltfPath);
|
2017-03-13 15:28:51 -04:00
|
|
|
var modelName = path.basename(objPath, path.extname(objPath));
|
|
|
|
var extension = path.extname(gltfPath);
|
|
|
|
if (extension === '.glb') {
|
|
|
|
binary = true;
|
2017-03-21 14:37:52 -04:00
|
|
|
if (bypassPipeline) {
|
|
|
|
console.log('--bypassPipeline does not convert to binary glTF, saving as .gltf');
|
|
|
|
extension = '.gltf';
|
|
|
|
}
|
2016-06-09 13:33:08 -04:00
|
|
|
}
|
2017-03-13 15:28:51 -04:00
|
|
|
gltfPath = path.join(path.dirname(gltfPath), modelName + extension);
|
|
|
|
|
|
|
|
var aoOptions = ao ? {} : undefined;
|
2016-06-09 13:33:08 -04:00
|
|
|
|
2017-03-13 15:28:51 -04:00
|
|
|
var pipelineOptions = {
|
|
|
|
createDirectory : false,
|
|
|
|
basePath : basePath,
|
|
|
|
binary : binary,
|
|
|
|
embed : !separate,
|
2017-03-14 16:42:42 -04:00
|
|
|
embedImage : !separateTextures,
|
2017-03-13 15:28:51 -04:00
|
|
|
quantize : compress,
|
|
|
|
compressTextureCoordinates : compress,
|
|
|
|
encodeNormals : compress,
|
|
|
|
preserve : !optimize,
|
|
|
|
optimizeForCesium : optimizeForCesium,
|
|
|
|
smoothNormals : generateNormals,
|
|
|
|
aoOptions : aoOptions,
|
|
|
|
textureCompressionOptions : textureCompressionOptions
|
|
|
|
};
|
2016-06-09 13:33:08 -04:00
|
|
|
|
2017-03-17 16:05:51 -04:00
|
|
|
return loadObj(objPath, options)
|
2017-03-13 15:28:51 -04:00
|
|
|
.then(function(objData) {
|
|
|
|
return createGltf(objData);
|
2016-07-22 14:09:13 -04:00
|
|
|
})
|
|
|
|
.then(function(gltf) {
|
2017-03-17 15:44:01 -04:00
|
|
|
return writeUris(gltf, gltfPath, separate, separateTextures);
|
2017-03-13 15:28:51 -04:00
|
|
|
})
|
|
|
|
.then(function(gltf) {
|
|
|
|
if (bypassPipeline) {
|
|
|
|
return convert._outputJson(gltfPath, gltf);
|
|
|
|
} else {
|
|
|
|
return GltfPipeline.processJSONToDisk(gltf, gltfPath, pipelineOptions);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-03-14 16:42:42 -04:00
|
|
|
/**
|
|
|
|
* Exposed for testing
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
convert._outputJson = fsExtraOutputJson;
|