obj2gltf/specs/lib/createGltfSpec.js

210 lines
8.1 KiB
JavaScript
Raw Normal View History

2017-03-13 15:28:51 -04:00
'use strict';
var Cesium = require('cesium');
var Promise = require('bluebird');
2017-04-12 16:55:03 -04:00
var obj2gltf = require('../../lib/obj2gltf');
var createGltf = require('../../lib/createGltf');
var loadObj = require('../../lib/loadObj');
2017-04-10 17:57:56 -04:00
var clone = Cesium.clone;
2017-03-13 15:28:51 -04:00
var WebGLConstants = Cesium.WebGLConstants;
var boxObjPath = 'specs/data/box/box.obj';
var groupObjPath = 'specs/data/box-objects-groups-materials/box-objects-groups-materials.obj';
var complexObjPath = 'specs/data/box-complex-material/box-complex-material.obj';
var noMaterialsObjPath = 'specs/data/box-no-materials/box-no-materials.obj';
2017-03-13 15:28:51 -04:00
var options;
2017-05-04 17:58:13 -04:00
2017-04-18 11:56:08 -04:00
describe('createGltf', function() {
2017-03-13 15:28:51 -04:00
var boxObjData;
var groupObjData;
var complexObjData;
var noMaterialsObjData;
2017-03-13 15:28:51 -04:00
2017-04-10 17:57:56 -04:00
beforeEach(function(done) {
options = clone(obj2gltf.defaults);
options.overridingTextures = {};
options.logger = function() {};
2017-03-13 15:28:51 -04:00
return Promise.all([
loadObj(boxObjPath, options)
2017-03-13 15:28:51 -04:00
.then(function(data) {
boxObjData = data;
}),
loadObj(groupObjPath, options)
2017-03-13 15:28:51 -04:00
.then(function(data) {
groupObjData = data;
}),
loadObj(complexObjPath, options)
.then(function(data) {
complexObjData = data;
}),
loadObj(noMaterialsObjPath, options)
.then(function(data) {
noMaterialsObjData = data;
2017-03-13 15:28:51 -04:00
})
]).then(done);
});
2017-07-11 11:32:04 -04:00
it('simple gltf', function() {
var gltf = createGltf(boxObjData, options);
2017-07-11 11:32:04 -04:00
2017-07-17 14:38:20 -04:00
expect(gltf.materials.length).toBe(1);
expect(gltf.scene).toBe(0);
expect(gltf.scenes[0].nodes[0]).toBe(0);
2017-07-17 14:38:20 -04:00
expect(gltf.nodes.length).toBe(1);
expect(gltf.meshes.length).toBe(1);
2017-07-11 11:32:04 -04:00
2017-07-17 14:38:20 -04:00
var primitives = gltf.meshes[0].primitives;
2017-07-11 11:32:04 -04:00
var primitive = primitives[0];
var attributes = primitive.attributes;
var positionAccessor = gltf.accessors[attributes.POSITION];
var normalAccessor = gltf.accessors[attributes.NORMAL];
var uvAccessor = gltf.accessors[attributes.TEXCOORD_0];
var indexAccessor = gltf.accessors[primitive.indices];
expect(primitives.length).toBe(1);
expect(positionAccessor.count).toBe(24);
expect(normalAccessor.count).toBe(24);
expect(uvAccessor.count).toBe(24);
expect(indexAccessor.count).toBe(36);
2017-03-13 15:28:51 -04:00
});
2017-07-11 11:32:04 -04:00
it('multiple nodes, meshes, and primitives', function() {
var gltf = createGltf(groupObjData, options);
2017-03-13 15:28:51 -04:00
2017-07-17 14:38:20 -04:00
expect(gltf.materials.length).toBe(3);
expect(gltf.scene).toBe(0);
expect(gltf.scenes[0].nodes[0]).toBe(0);
2017-07-17 14:38:20 -04:00
expect(gltf.nodes.length).toBe(4);
expect(gltf.nodes[0].mesh).toBeUndefined();
expect(gltf.nodes[0].children.length).toBe(3);
expect(gltf.meshes.length).toBe(3);
2017-07-11 11:32:04 -04:00
// Check for two primitives in each mesh
2017-07-17 14:38:20 -04:00
var length = gltf.meshes.length;
for (var i = 0; i < length; ++i) {
var mesh = gltf.meshes[i];
expect(mesh.primitives.length).toBe(2);
2017-07-11 11:32:04 -04:00
}
2017-03-13 15:28:51 -04:00
});
it('multiple textures', function() {
var gltf = createGltf(complexObjData, options);
var material = gltf.materials[0];
var pbr = material.pbrMetallicRoughness;
var textures = [pbr.metallicRoughnessTexture, pbr.baseColorTexture, material.emissiveTexture, material.normalTexture, material.occlusionTexture];
expect(textures.map(function(texture) {
return texture.index;
}).sort()).toEqual([0, 1, 2, 3, 4]);
expect(gltf.samplers[0]).toBeDefined();
});
it('creates default material', function() {
var gltf = createGltf(noMaterialsObjData, options);
var material = gltf.materials[0];
var pbr = material.pbrMetallicRoughness;
expect(material.name).toBe('default');
expect(pbr.baseColorTexture).toBeUndefined();
expect(pbr.metallicRoughnessTexture).toBeUndefined();
expect(pbr.baseColorFactor).toEqual([0.5, 0.5, 0.5, 1.0]);
expect(pbr.metallicFactor).toBe(0.0); // No metallic
expect(pbr.roughnessFactor).toBe(1.0); // Fully rough
expect(material.emissiveTexture).toBeUndefined();
expect(material.normalTexture).toBeUndefined();
expect(material.ambientTexture).toBeUndefined();
expect(material.emissiveFactor).toEqual([0.0, 0.0, 0.0]);
expect(material.alphaMode).toBe('OPAQUE');
expect(material.doubleSided).toBe(false);
});
it('adds KHR_materials_pbrSpecularGlossiness extension when specularGlossiness is set', function() {
options.specularGlossiness = true;
var gltf = createGltf(noMaterialsObjData, options);
expect(gltf.extensionsUsed).toEqual(['KHR_materials_pbrSpecularGlossiness']);
expect(gltf.extensionsRequired).toEqual(['KHR_materials_pbrSpecularGlossiness']);
});
it('adds KHR_materials_common extension when materialsCommon is set', function() {
options.materialsCommon = true;
var gltf = createGltf(noMaterialsObjData, options);
expect(gltf.extensionsUsed).toEqual(['KHR_materials_common']);
expect(gltf.extensionsRequired).toEqual(['KHR_materials_common']);
});
2017-03-13 15:28:51 -04:00
it('runs without normals', function() {
boxObjData.nodes[0].meshes[0].primitives[0].normals.length = 0;
2017-03-13 15:28:51 -04:00
var gltf = createGltf(boxObjData, options);
var attributes = gltf.meshes[0].primitives[0].attributes;
2017-03-13 15:28:51 -04:00
expect(attributes.POSITION).toBeDefined();
expect(attributes.NORMAL).toBeUndefined();
expect(attributes.TEXCOORD_0).toBeDefined();
});
it('runs without uvs', function() {
boxObjData.nodes[0].meshes[0].primitives[0].uvs.length = 0;
2017-03-13 15:28:51 -04:00
var gltf = createGltf(boxObjData, options);
var attributes = gltf.meshes[0].primitives[0].attributes;
2017-03-13 15:28:51 -04:00
expect(attributes.POSITION).toBeDefined();
expect(attributes.NORMAL).toBeDefined();
expect(attributes.TEXCOORD_0).toBeUndefined();
});
it('runs without uvs and normals', function() {
boxObjData.nodes[0].meshes[0].primitives[0].normals.length = 0;
boxObjData.nodes[0].meshes[0].primitives[0].uvs.length = 0;
2017-03-13 15:28:51 -04:00
var gltf = createGltf(boxObjData, options);
var attributes = gltf.meshes[0].primitives[0].attributes;
2017-03-13 15:28:51 -04:00
expect(attributes.POSITION).toBeDefined();
expect(attributes.NORMAL).toBeUndefined();
expect(attributes.TEXCOORD_0).toBeUndefined();
});
function expandObjData(objData, duplicatesLength) {
var primitive = objData.nodes[0].meshes[0].primitives[0];
var indices = primitive.indices;
var positions = primitive.positions;
var normals = primitive.normals;
var uvs = primitive.uvs;
2017-03-13 15:28:51 -04:00
var indicesLength = indices.length;
var vertexCount = positions.length / 3;
for (var i = 1; i < duplicatesLength; ++i) {
for (var j = 0; j < vertexCount; ++j) {
positions.push(0.0);
positions.push(0.0);
positions.push(0.0);
normals.push(0.0);
normals.push(0.0);
normals.push(0.0);
uvs.push(0.0);
uvs.push(0.0);
}
for (var k = 0; k < indicesLength; ++k) {
indices.push(indices.get(k) + vertexCount * i);
}
}
}
it('detects need to use uint32 indices', function() {
2017-04-10 17:57:56 -04:00
expandObjData(boxObjData, 2731); // Right above 65536 limit
var primitive = boxObjData.nodes[0].meshes[0].primitives[0];
var indicesLength = primitive.indices.length;
var vertexCount = primitive.positions.length / 3;
2017-03-13 15:28:51 -04:00
var gltf = createGltf(boxObjData, options);
primitive = gltf.meshes[0].primitives[0];
2017-03-13 15:28:51 -04:00
var indicesAccessor = gltf.accessors[primitive.indices];
expect(indicesAccessor.count).toBe(indicesLength);
expect(indicesAccessor.max[0]).toBe(vertexCount - 1);
expect(indicesAccessor.componentType).toBe(WebGLConstants.UNSIGNED_INT);
var positionAccessor = gltf.accessors[primitive.attributes.POSITION];
expect(positionAccessor.count).toBe(vertexCount);
});
});