obj2gltf/specs/lib/obj2gltfSpec.js

188 lines
6.4 KiB
JavaScript
Raw Normal View History

2016-07-11 12:27:15 -04:00
'use strict';
2017-04-25 13:02:14 -04:00
var fsExtra = require('fs-extra');
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
var texturedObjPath = 'specs/data/box-textured/box-textured.obj';
var complexObjPath = 'specs/data/box-complex-material/box-complex-material.obj';
var missingMtllibObjPath = 'specs/data/box-missing-mtllib/box-missing-mtllib.obj';
var outputDirectory = 'output';
2017-04-04 17:21:10 -04:00
2017-07-28 16:56:28 -04:00
var textureUrl = 'specs/data/box-textured/cesium.png';
2017-04-12 16:55:03 -04:00
describe('obj2gltf', function() {
2017-04-25 13:02:14 -04:00
beforeEach(function() {
spyOn(fsExtra, 'outputFile').and.returnValue(Promise.resolve());
2017-04-25 13:02:14 -04:00
});
it('converts obj to gltf', function(done) {
expect(obj2gltf(texturedObjPath)
.then(function(gltf) {
2017-03-13 15:28:51 -04:00
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
}), done).toResolve();
});
it('converts obj to glb', function(done) {
2017-03-13 15:28:51 -04:00
var options = {
binary : true
2017-03-13 15:28:51 -04:00
};
expect(obj2gltf(texturedObjPath, options)
.then(function(glb) {
var magic = glb.toString('utf8', 0, 4);
expect(magic).toBe('glTF');
2017-03-13 15:28:51 -04:00
}), done).toResolve();
});
it('convert obj to gltf with separate resources', function(done) {
var options = {
separate : true,
separateTextures : true,
outputDirectory : outputDirectory
};
expect(obj2gltf(texturedObjPath, options)
2017-03-13 15:28:51 -04:00
.then(function() {
expect(fsExtra.outputFile.calls.count()).toBe(2); // Saves out .png and .bin
2017-03-13 15:28:51 -04:00
}), done).toResolve();
});
it('converts obj to glb with separate resources', function(done) {
2017-03-13 15:28:51 -04:00
var options = {
separate : true,
separateTextures : true,
outputDirectory : outputDirectory,
binary : true
2017-03-13 15:28:51 -04:00
};
expect(obj2gltf(texturedObjPath, options)
2017-03-13 15:28:51 -04:00
.then(function() {
expect(fsExtra.outputFile.calls.count()).toBe(2); // Saves out .png and .bin
2017-03-13 15:28:51 -04:00
}), done).toResolve();
});
it('converts obj with multiple textures', function(done) {
var options = {
separateTextures : true,
outputDirectory : outputDirectory
};
expect(obj2gltf(complexObjPath, options)
.then(function() {
expect(fsExtra.outputFile.calls.count()).toBe(5); // baseColor, metallicRoughness, occlusion, emission, normal
}), done).toResolve();
});
it('sets overriding textures (1)', function(done) {
2017-07-28 16:56:28 -04:00
var options = {
overridingTextures : {
2017-07-28 16:56:28 -04:00
metallicRoughnessOcclusionTexture : textureUrl,
normalTexture : textureUrl,
baseColorTexture : textureUrl,
2018-01-03 20:41:25 -05:00
emissiveTexture : textureUrl,
alphaTexture : textureUrl
2017-07-28 16:56:28 -04:00
},
separateTextures : true,
outputDirectory : outputDirectory
};
expect(obj2gltf(complexObjPath, options)
.then(function() {
var args = fsExtra.outputFile.calls.allArgs();
var length = args.length;
for (var i = 0; i < length; ++i) {
expect(path.basename(args[i][0])).toBe(path.basename(textureUrl));
}
}), done).toResolve();
});
it('sets overriding textures (2)', function(done) {
var options = {
overridingTextures : {
specularGlossinessTexture : textureUrl,
occlusionTexture : textureUrl,
normalTexture : textureUrl,
baseColorTexture : textureUrl,
2018-01-03 20:41:25 -05:00
emissiveTexture : textureUrl,
alphaTexture : textureUrl
},
separateTextures : true,
outputDirectory : outputDirectory
2017-07-28 16:56:28 -04:00
};
expect(obj2gltf(complexObjPath, options)
2017-07-28 16:56:28 -04:00
.then(function() {
var args = fsExtra.outputFile.calls.allArgs();
var length = args.length;
for (var i = 0; i < length; ++i) {
expect(path.basename(args[i][0])).toBe(path.basename(textureUrl));
}
}), done).toResolve();
});
it('uses a custom logger', function(done) {
var lastMessage;
var options = {
logger : function(message) {
lastMessage = message;
}
};
expect(obj2gltf(missingMtllibObjPath, options)
.then(function() {
2017-12-21 22:19:52 -05:00
expect(lastMessage.indexOf('Could not read material file') >= 0).toBe(true);
}), done).toResolve();
});
it('uses a custom writer', function(done) {
var filePaths = [];
var fileContents = [];
var options = {
separate : true,
writer : function(relativePath, contents) {
filePaths.push(relativePath);
fileContents.push(contents);
}
};
expect(obj2gltf(texturedObjPath, options)
.then(function() {
expect(filePaths).toEqual(['box-textured.bin', 'cesium.png']);
expect(fileContents[0]).toBeDefined();
expect(fileContents[1]).toBeDefined();
}), done).toResolve();
});
2017-04-10 17:57:56 -04:00
it('throws if objPath is undefined', function() {
expect(function() {
obj2gltf(undefined);
2017-04-10 17:57:56 -04:00
}).toThrowDeveloperError();
2017-03-13 15:28:51 -04:00
});
it('throws if both options.writer and options.outputDirectory are undefined when writing separate resources', function() {
var options = {
separateTextures : true
};
2017-04-10 17:57:56 -04:00
expect(function() {
obj2gltf(texturedObjPath, options);
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
2017-07-24 18:21:01 -04:00
it('throws if more than one material type is set', function() {
2017-05-04 17:58:13 -04:00
var options = {
metallicRoughness : true,
specularGlossiness : true
};
2017-07-24 18:21:01 -04:00
expect(function() {
obj2gltf(texturedObjPath, options);
2017-07-24 18:21:01 -04:00
}).toThrowDeveloperError();
});
it('throws if metallicRoughnessOcclusionTexture and specularGlossinessTexture are both defined', function() {
var options = {
overridingTextures : {
metallicRoughnessOcclusionTexture : textureUrl,
specularGlossinessTexture : textureUrl
}
2017-07-24 18:21:01 -04:00
};
expect(function() {
obj2gltf(texturedObjPath, options);
2017-07-24 18:21:01 -04:00
}).toThrowDeveloperError();
2017-05-04 17:58:13 -04:00
});
2016-07-11 12:27:15 -04:00
});