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