obj2gltf/lib/convert.js

58 lines
1.8 KiB
JavaScript
Raw Normal View History

2016-06-09 13:33:08 -04:00
"use strict";
var path = require('path');
var gltfPipeline = require('gltf-pipeline').gltfPipeline;
var parseObj = require('./obj');
var createGltf = require('./gltf');
var Cesium = require('cesium');
var defined = Cesium.defined;
var defaultValue = Cesium.defaultValue;
module.exports = convert;
2016-07-22 14:09:13 -04:00
function convert(objFile, outputPath, options) {
2016-07-20 14:48:49 -04:00
options = defaultValue(options, {});
2016-06-09 13:33:08 -04:00
var binary = defaultValue(options.binary, false);
var embed = defaultValue(options.embed, true);
var embedImage = defaultValue(options.embedImage, true);
2016-06-24 14:04:12 -04:00
var quantize = defaultValue(options.quantize, false);
2016-07-12 18:14:56 -04:00
var ao = defaultValue(options.ao, false);
2016-06-09 13:33:08 -04:00
if (!defined(objFile)) {
throw new Error('objFile is required');
}
if (!defined(outputPath)) {
outputPath = path.dirname(objFile);
}
var inputPath = path.dirname(objFile);
var modelName = path.basename(objFile, '.obj');
var extension = path.extname(outputPath);
if (extension !== '') {
modelName = path.basename(outputPath, extension);
outputPath = path.dirname(outputPath);
}
extension = binary ? '.glb' : '.gltf';
var gltfFile = path.join(outputPath, modelName + extension);
2016-07-22 14:09:13 -04:00
parseObj(objFile, inputPath)
.then(function(data) {
return createGltf(data, inputPath, modelName);
})
.then(function(gltf) {
2016-07-12 18:14:56 -04:00
var aoOptions = ao ? {} : undefined;
2016-06-09 13:33:08 -04:00
var options = {
2016-07-22 14:09:13 -04:00
binary: binary,
embed: embed,
embedImage: embedImage,
quantize: quantize,
aoOptions: aoOptions,
createDirectory: false,
basePath: inputPath
2016-06-09 13:33:08 -04:00
};
2016-07-22 14:09:13 -04:00
return gltfPipeline.processJSONToDisk(gltf, gltfFile, options);
2016-06-09 13:33:08 -04:00
});
}