2016-07-11 12:27:15 -04:00
|
|
|
'use strict';
|
2017-05-04 17:58:13 -04:00
|
|
|
var Cesium = require('Cesium');
|
2017-04-25 13:02:14 -04:00
|
|
|
var fsExtra = require('fs-extra');
|
2016-08-25 13:48:34 -04:00
|
|
|
var GltfPipeline = require('gltf-pipeline').Pipeline;
|
2017-04-25 13:02:14 -04:00
|
|
|
var os = require('os');
|
2016-07-11 12:27:15 -04:00
|
|
|
var path = require('path');
|
2017-04-25 13:02:14 -04:00
|
|
|
var Promise = require('bluebird');
|
2017-04-12 16:55:03 -04:00
|
|
|
var obj2gltf = require('../../lib/obj2gltf');
|
2016-07-11 12:27:15 -04:00
|
|
|
|
2017-05-04 17:58:13 -04:00
|
|
|
var RuntimeError = Cesium.RuntimeError;
|
|
|
|
|
2017-03-13 15:28:51 -04:00
|
|
|
var objPath = 'specs/data/box-textured/box-textured.obj';
|
|
|
|
var gltfPath = 'specs/data/box-textured/box-textured.gltf';
|
|
|
|
var glbPath = 'specs/data/box-textured/box-textured.glb';
|
2017-04-05 10:44:28 -04:00
|
|
|
var objPathNonExistent = 'specs/data/non-existent.obj';
|
2017-04-04 17:21:10 -04:00
|
|
|
|
2017-04-12 16:55:03 -04:00
|
|
|
describe('obj2gltf', function() {
|
2017-04-25 13:02:14 -04:00
|
|
|
var tempDirectory;
|
|
|
|
|
|
|
|
beforeAll(function() {
|
|
|
|
expect(obj2gltf._getTempDirectory()).toContain(os.tmpdir());
|
|
|
|
tempDirectory = path.join(os.tmpdir(), 'testPath');
|
|
|
|
spyOn(obj2gltf, '_getTempDirectory').and.returnValue(tempDirectory);
|
Update npm dependencies
A few npm dependencies were major versions behind, so this updates `yargs`,
`fs-extra`, and `jasmin-spec-reporter` to their latest versions.
The major change here is `fs-extra`, which now has promise implementations
of all functions by default, this means there's no reason to manually
`Promisify` a function any more, the result is less code overall.
There is one important edge case, `fs-extra` uses built-in native Node
promises, which do not have a `finally` function. If you start a promise
change with an `fs-extra` function, you need to wrap it in `Promise.resolve`
in order to make use of finally at the end (assuming you are using finally
at all, if not you don't need to worry about it. The upside is that your
code will always error if you forget to do this.
2017-05-19 11:37:33 -04:00
|
|
|
spyOn(fsExtra, 'outputJson');
|
|
|
|
spyOn(fsExtra, 'outputFile');
|
2017-04-25 13:02:14 -04:00
|
|
|
spyOn(fsExtra, 'remove');
|
|
|
|
});
|
|
|
|
|
|
|
|
beforeEach(function() {
|
|
|
|
spyOn(GltfPipeline, 'processJSONToDisk').and.returnValue(Promise.resolve());
|
|
|
|
});
|
|
|
|
|
2016-07-11 12:27:15 -04:00
|
|
|
it('converts an obj to gltf', function(done) {
|
2017-04-12 16:55:03 -04:00
|
|
|
expect(obj2gltf(objPath, gltfPath)
|
2016-07-22 14:09:13 -04:00
|
|
|
.then(function() {
|
2017-04-25 13:02:14 -04:00
|
|
|
var args = GltfPipeline.processJSONToDisk.calls.first().args;
|
2017-03-13 15:28:51 -04:00
|
|
|
var gltf = args[0];
|
|
|
|
var outputPath = args[1];
|
2017-04-25 13:02:14 -04:00
|
|
|
var options = args[2];
|
2017-03-13 15:28:51 -04:00
|
|
|
expect(path.normalize(outputPath)).toEqual(path.normalize(gltfPath));
|
|
|
|
expect(gltf).toBeDefined();
|
2017-05-04 17:58:13 -04:00
|
|
|
expect(gltf.images.length).toBe(1);
|
2017-03-13 15:28:51 -04:00
|
|
|
expect(options).toEqual({
|
2017-04-25 13:02:14 -04:00
|
|
|
basePath : tempDirectory,
|
2017-03-13 15:28:51 -04:00
|
|
|
createDirectory : false,
|
|
|
|
binary : false,
|
|
|
|
embed : true,
|
|
|
|
embedImage : true,
|
|
|
|
encodeNormals : false,
|
|
|
|
quantize : false,
|
|
|
|
compressTextureCoordinates : false,
|
|
|
|
aoOptions : undefined,
|
|
|
|
smoothNormals : false,
|
|
|
|
optimizeForCesium : false,
|
|
|
|
textureCompressionOptions : undefined,
|
|
|
|
preserve : true
|
|
|
|
});
|
|
|
|
}), done).toResolve();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('sets options', function(done) {
|
|
|
|
var textureCompressionOptions = {
|
|
|
|
format : 'dxt1',
|
|
|
|
quality : 10
|
|
|
|
};
|
|
|
|
var options = {
|
|
|
|
binary : true,
|
|
|
|
separate : true,
|
|
|
|
separateTextures : true,
|
|
|
|
compress : true,
|
|
|
|
optimize : true,
|
2017-04-10 17:57:56 -04:00
|
|
|
optimizeForCesium : true,
|
2017-03-13 15:28:51 -04:00
|
|
|
generateNormals : true,
|
|
|
|
ao : true,
|
2017-04-10 17:57:56 -04:00
|
|
|
textureCompressionOptions : textureCompressionOptions,
|
|
|
|
checkTransparency : true,
|
|
|
|
secure : true,
|
2017-04-20 14:41:39 -04:00
|
|
|
inputUpAxis : 'Z',
|
|
|
|
outputUpAxis : 'X',
|
2017-04-12 16:55:03 -04:00
|
|
|
logger : obj2gltf.defaults.logger
|
2017-03-13 15:28:51 -04:00
|
|
|
};
|
|
|
|
|
2017-04-12 16:55:03 -04:00
|
|
|
expect(obj2gltf(objPath, gltfPath, options)
|
2017-03-13 15:28:51 -04:00
|
|
|
.then(function() {
|
2017-04-25 13:02:14 -04:00
|
|
|
var args = GltfPipeline.processJSONToDisk.calls.first().args;
|
2017-03-13 15:28:51 -04:00
|
|
|
var options = args[2];
|
|
|
|
expect(options).toEqual({
|
2017-04-25 13:02:14 -04:00
|
|
|
basePath : tempDirectory,
|
2017-03-13 15:28:51 -04:00
|
|
|
createDirectory : false,
|
|
|
|
binary : true,
|
|
|
|
embed : false,
|
|
|
|
embedImage : false,
|
|
|
|
encodeNormals : true,
|
|
|
|
quantize : true,
|
|
|
|
compressTextureCoordinates : true,
|
|
|
|
aoOptions : {},
|
|
|
|
smoothNormals : true,
|
|
|
|
optimizeForCesium : true,
|
|
|
|
textureCompressionOptions : textureCompressionOptions,
|
|
|
|
preserve : false
|
|
|
|
});
|
Update npm dependencies
A few npm dependencies were major versions behind, so this updates `yargs`,
`fs-extra`, and `jasmin-spec-reporter` to their latest versions.
The major change here is `fs-extra`, which now has promise implementations
of all functions by default, this means there's no reason to manually
`Promisify` a function any more, the result is less code overall.
There is one important edge case, `fs-extra` uses built-in native Node
promises, which do not have a `finally` function. If you start a promise
change with an `fs-extra` function, you need to wrap it in `Promise.resolve`
in order to make use of finally at the end (assuming you are using finally
at all, if not you don't need to worry about it. The upside is that your
code will always error if you forget to do this.
2017-05-19 11:37:33 -04:00
|
|
|
expect(fsExtra.outputFile.calls.count()).toBe(2); // Saves out .png and .bin
|
2017-03-13 15:28:51 -04:00
|
|
|
}), done).toResolve();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('saves as binary if gltfPath has a .glb extension', function(done) {
|
2017-04-12 16:55:03 -04:00
|
|
|
expect(obj2gltf(objPath, glbPath)
|
2017-03-13 15:28:51 -04:00
|
|
|
.then(function() {
|
2017-04-25 13:02:14 -04:00
|
|
|
var args = GltfPipeline.processJSONToDisk.calls.first().args;
|
2017-03-13 15:28:51 -04:00
|
|
|
var options = args[2];
|
|
|
|
expect(options.binary).toBe(true);
|
|
|
|
}), done).toResolve();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('bypassPipeline flag bypasses gltf-pipeline', function(done) {
|
|
|
|
var options = {
|
|
|
|
bypassPipeline : true
|
|
|
|
};
|
2017-04-12 16:55:03 -04:00
|
|
|
expect(obj2gltf(objPath, gltfPath, options)
|
2017-03-13 15:28:51 -04:00
|
|
|
.then(function() {
|
Update npm dependencies
A few npm dependencies were major versions behind, so this updates `yargs`,
`fs-extra`, and `jasmin-spec-reporter` to their latest versions.
The major change here is `fs-extra`, which now has promise implementations
of all functions by default, this means there's no reason to manually
`Promisify` a function any more, the result is less code overall.
There is one important edge case, `fs-extra` uses built-in native Node
promises, which do not have a `finally` function. If you start a promise
change with an `fs-extra` function, you need to wrap it in `Promise.resolve`
in order to make use of finally at the end (assuming you are using finally
at all, if not you don't need to worry about it. The upside is that your
code will always error if you forget to do this.
2017-05-19 11:37:33 -04:00
|
|
|
expect(fsExtra.outputJson).toHaveBeenCalled();
|
2017-04-10 17:57:56 -04:00
|
|
|
expect(GltfPipeline.processJSONToDisk).not.toHaveBeenCalled();
|
2017-03-13 15:28:51 -04:00
|
|
|
}), done).toResolve();
|
|
|
|
});
|
|
|
|
|
2017-04-10 17:57:56 -04:00
|
|
|
it('rejects if obj path does not exist', function(done) {
|
2017-04-12 16:55:03 -04:00
|
|
|
expect(obj2gltf(objPathNonExistent, gltfPath), done).toRejectWith(Error);
|
2017-04-05 10:44:28 -04:00
|
|
|
});
|
|
|
|
|
2017-04-10 17:57:56 -04:00
|
|
|
it('throws if objPath is undefined', function() {
|
|
|
|
expect(function() {
|
2017-04-12 16:55:03 -04:00
|
|
|
obj2gltf(undefined, gltfPath);
|
2017-04-10 17:57:56 -04:00
|
|
|
}).toThrowDeveloperError();
|
2017-03-13 15:28:51 -04:00
|
|
|
});
|
|
|
|
|
2017-05-04 17:58:13 -04:00
|
|
|
it('throws if gltfPath is undefined', function() {
|
2017-04-10 17:57:56 -04:00
|
|
|
expect(function() {
|
2017-04-12 16:55:03 -04:00
|
|
|
obj2gltf(objPath, undefined);
|
2017-04-10 17:57:56 -04:00
|
|
|
}).toThrowDeveloperError();
|
2017-03-13 15:28:51 -04:00
|
|
|
});
|
2017-05-04 17:58:13 -04:00
|
|
|
|
|
|
|
it('rejects if both bpypassPipeline and binary are true', function(done) {
|
|
|
|
var options = {
|
|
|
|
bypassPipeline : true,
|
|
|
|
binary : true
|
|
|
|
};
|
|
|
|
expect(obj2gltf(objPath, gltfPath, options), done).toRejectWith(RuntimeError);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('rejects if more than one material type is set', function(done) {
|
|
|
|
var options = {
|
|
|
|
metallicRoughness : true,
|
|
|
|
specularGlossiness : true
|
|
|
|
};
|
|
|
|
expect(obj2gltf(objPath, gltfPath, options), done).toRejectWith(RuntimeError);
|
|
|
|
});
|
2016-07-11 12:27:15 -04:00
|
|
|
});
|