obj2gltf/specs/lib/obj2gltfSpec.js

139 lines
5.2 KiB
JavaScript
Raw Normal View History

2016-07-11 12:27:15 -04:00
'use strict';
var GltfPipeline = require('gltf-pipeline').Pipeline;
2016-07-11 12:27:15 -04:00
var path = require('path');
2017-04-12 16:55:03 -04:00
var obj2gltf = require('../../lib/obj2gltf');
var writeUris = require('../../lib/writeUris');
2016-07-11 12:27:15 -04:00
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';
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() {
2016-07-11 12:27:15 -04:00
it('converts an obj to gltf', function(done) {
2017-03-13 15:28:51 -04:00
var spy = spyOn(GltfPipeline, 'processJSONToDisk');
2017-04-12 16:55:03 -04:00
expect(obj2gltf(objPath, gltfPath)
2016-07-22 14:09:13 -04:00
.then(function() {
var args = spy.calls.first().args;
2017-03-13 15:28:51 -04:00
var gltf = args[0];
var outputPath = args[1];
expect(path.normalize(outputPath)).toEqual(path.normalize(gltfPath));
expect(gltf).toBeDefined();
expect(gltf.images.cesium).toBeDefined();
2016-07-22 14:09:13 -04:00
}), done).toResolve();
2016-07-11 12:27:15 -04:00
});
2017-03-13 15:28:51 -04:00
it('uses default gltf-pipeline options', function(done) {
var spy = spyOn(GltfPipeline, 'processJSONToDisk');
2017-04-12 16:55:03 -04:00
expect(obj2gltf(objPath, gltfPath)
2017-03-13 15:28:51 -04:00
.then(function() {
var args = spy.calls.first().args;
var options = args[2];
2017-04-20 14:55:47 -04:00
expect(options.basePath).toBeDefined();
delete options.basePath; // This will be a random temp directory
2017-03-13 15:28:51 -04:00
expect(options).toEqual({
createDirectory : false,
binary : false,
embed : true,
embedImage : true,
encodeNormals : false,
quantize : false,
compressTextureCoordinates : false,
aoOptions : undefined,
2017-04-04 17:57:01 -04:00
kmcOptions : undefined,
2017-03-13 15:28:51 -04:00
smoothNormals : false,
optimizeForCesium : false,
textureCompressionOptions : undefined,
preserve : true
});
}), done).toResolve();
});
it('sets options', function(done) {
2017-04-10 17:57:56 -04:00
var spy = spyOn(GltfPipeline, 'processJSONToDisk');
spyOn(writeUris, '_outputFile');
2017-03-13 15:28:51 -04:00
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-04 17:57:01 -04:00
kmc : true,
2017-04-10 17:57:56 -04:00
textureCompressionOptions : textureCompressionOptions,
checkTransparency : true,
secure : true,
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-10 17:57:56 -04:00
var args = spy.calls.first().args;
2017-03-13 15:28:51 -04:00
var options = args[2];
2017-04-20 14:55:47 -04:00
expect(options.basePath).toBeDefined();
delete options.basePath; // This will be a random temp directory
2017-03-13 15:28:51 -04:00
expect(options).toEqual({
createDirectory : false,
binary : true,
embed : false,
embedImage : false,
encodeNormals : true,
quantize : true,
compressTextureCoordinates : true,
aoOptions : {},
2017-04-04 17:57:01 -04:00
kmcOptions : {},
2017-03-13 15:28:51 -04:00
smoothNormals : true,
optimizeForCesium : true,
textureCompressionOptions : textureCompressionOptions,
preserve : false
});
2017-04-10 17:57:56 -04:00
expect(writeUris._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) {
var spy = spyOn(GltfPipeline, 'processJSONToDisk');
2017-04-12 16:55:03 -04:00
expect(obj2gltf(objPath, glbPath)
2017-03-13 15:28:51 -04:00
.then(function() {
var args = spy.calls.first().args;
var options = args[2];
expect(options.binary).toBe(true);
}), done).toResolve();
});
it('bypassPipeline flag bypasses gltf-pipeline', function(done) {
2017-04-12 16:55:03 -04:00
spyOn(obj2gltf, '_outputJson');
2017-04-10 17:57:56 -04:00
spyOn(GltfPipeline, 'processJSONToDisk');
2017-03-13 15:28:51 -04:00
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() {
2017-04-12 16:55:03 -04:00
expect(obj2gltf._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-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-04-10 17:57:56 -04:00
it('rejects if gltfPath is undefined', function() {
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
});
2016-07-11 12:27:15 -04:00
});