obj2gltf/lib/convert.js

55 lines
1.6 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;
function convert(objFile, outputPath, options, done) {
var binary = defaultValue(options.binary, false);
var embed = defaultValue(options.embed, true);
2016-06-24 14:04:12 -04:00
var quantize = defaultValue(options.quantize, 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);
parseObj(objFile, inputPath, function(data) {
createGltf(data, inputPath, modelName, function(gltf) {
2016-06-09 13:33:08 -04:00
var options = {
binary : binary,
embed : embed,
2016-06-24 14:04:12 -04:00
quantize : quantize,
createDirectory : false,
basePath : inputPath
2016-06-09 13:33:08 -04:00
};
gltfPipeline.processJSONToDisk(gltf, gltfFile, options, function(error) {
if (error) {
throw error;
}
done();
});
});
});
}