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 loadObj = require ( '../../lib/loadObj' ) ;
var obj2gltf = require ( '../../lib/obj2gltf' ) ;
2017-03-13 15:28:51 -04:00
2018-03-20 22:38:37 -04:00
var Cartesian3 = Cesium . Cartesian3 ;
var CesiumMath = Cesium . Math ;
2017-04-10 17:57:56 -04:00
var clone = Cesium . clone ;
2017-03-13 15:28:51 -04:00
var RuntimeError = Cesium . RuntimeError ;
2017-07-29 13:23:33 -04:00
var objPath = 'specs/data/box/box.obj' ;
var objNormalsPath = 'specs/data/box-normals/box-normals.obj' ;
var objUvsPath = 'specs/data/box-uvs/box-uvs.obj' ;
var objPositionsOnlyPath = 'specs/data/box-positions-only/box-positions-only.obj' ;
var objNegativeIndicesPath = 'specs/data/box-negative-indices/box-negative-indices.obj' ;
var objTrianglesPath = 'specs/data/box-triangles/box-triangles.obj' ;
var objObjectsPath = 'specs/data/box-objects/box-objects.obj' ;
var objGroupsPath = 'specs/data/box-groups/box-groups.obj' ;
var objObjectsGroupsPath = 'specs/data/box-objects-groups/box-objects-groups.obj' ;
2017-12-29 11:55:21 -05:00
var objObjectsGroupsMaterialsPath = 'specs/data/box-objects-groups-materials/box-objects-groups-materials.obj' ;
var objObjectsGroupsMaterialsPath2 = 'specs/data/box-objects-groups-materials-2/box-objects-groups-materials-2.obj' ;
2017-07-29 13:23:33 -04:00
var objUsemtlPath = 'specs/data/box-usemtl/box-usemtl.obj' ;
var objNoMaterialsPath = 'specs/data/box-no-materials/box-no-materials.obj' ;
var objMultipleMaterialsPath = 'specs/data/box-multiple-materials/box-multiple-materials.obj' ;
var objUncleanedPath = 'specs/data/box-uncleaned/box-uncleaned.obj' ;
var objMtllibPath = 'specs/data/box-mtllib/box-mtllib.obj' ;
2017-11-17 11:53:04 -05:00
var objMtllibSpacesPath = 'specs/data/box-mtllib-spaces/box mtllib.obj' ;
2017-07-29 13:23:33 -04:00
var objMissingMtllibPath = 'specs/data/box-missing-mtllib/box-missing-mtllib.obj' ;
2018-03-06 19:22:06 -05:00
var objMissingUsemtlPath = 'specs/data/box-missing-usemtl/box-missing-usemtl.obj' ;
2017-07-29 13:23:33 -04:00
var objExternalResourcesPath = 'specs/data/box-external-resources/box-external-resources.obj' ;
2017-11-17 15:07:52 -05:00
var objResourcesInRootPath = 'specs/data/box-resources-in-root/box-resources-in-root.obj' ;
2017-11-29 14:21:59 -05:00
var objExternalResourcesInRootPath = 'specs/data/box-external-resources-in-root/box-external-resources-in-root.obj' ;
2017-07-29 13:23:33 -04:00
var objTexturedPath = 'specs/data/box-textured/box-textured.obj' ;
var objMissingTexturePath = 'specs/data/box-missing-texture/box-missing-texture.obj' ;
var objSubdirectoriesPath = 'specs/data/box-subdirectories/box-textured.obj' ;
var objInvalidContentsPath = 'specs/data/box/box.mtl' ;
2017-08-10 09:52:19 -04:00
var objConcavePath = 'specs/data/concave/concave.obj' ;
2018-03-20 22:38:37 -04:00
var objUnnormalizedPath = 'specs/data/box-unnormalized/box-unnormalized.obj' ;
2017-07-29 13:23:33 -04:00
var objInvalidPath = 'invalid.obj' ;
2017-03-13 15:28:51 -04:00
function getMeshes ( data ) {
var meshes = [ ] ;
var nodes = data . nodes ;
var nodesLength = nodes . length ;
for ( var i = 0 ; i < nodesLength ; ++ i ) {
meshes = meshes . concat ( nodes [ i ] . meshes ) ;
}
return meshes ;
}
function getPrimitives ( data ) {
var primitives = [ ] ;
var nodes = data . nodes ;
var nodesLength = nodes . length ;
for ( var i = 0 ; i < nodesLength ; ++ i ) {
var meshes = nodes [ i ] . meshes ;
var meshesLength = meshes . length ;
for ( var j = 0 ; j < meshesLength ; ++ j ) {
primitives = primitives . concat ( meshes [ j ] . primitives ) ;
}
}
return primitives ;
}
2017-07-29 13:23:33 -04:00
var options ;
2017-04-10 17:57:56 -04:00
2017-04-19 15:48:07 -04:00
describe ( 'loadObj' , function ( ) {
2017-04-25 13:02:14 -04:00
beforeEach ( function ( ) {
2017-07-29 13:23:33 -04:00
options = clone ( obj2gltf . defaults ) ;
options . overridingTextures = { } ;
options . logger = function ( ) { } ;
2017-04-25 13:02:14 -04:00
} ) ;
2017-03-13 15:28:51 -04:00
it ( 'loads obj with positions, normals, and uvs' , function ( done ) {
2017-07-29 13:23:33 -04:00
expect ( loadObj ( objPath , options )
2017-03-13 15:28:51 -04:00
. then ( function ( data ) {
var materials = data . materials ;
var nodes = data . nodes ;
2017-07-29 13:23:33 -04:00
var name = data . name ;
2017-03-13 15:28:51 -04:00
var meshes = getMeshes ( data ) ;
var primitives = getPrimitives ( data ) ;
2017-07-29 13:23:33 -04:00
expect ( name ) . toBe ( 'box' ) ;
2017-05-04 17:58:13 -04:00
expect ( materials . length ) . toBe ( 1 ) ;
2017-03-13 15:28:51 -04:00
expect ( nodes . length ) . toBe ( 1 ) ;
expect ( meshes . length ) . toBe ( 1 ) ;
expect ( primitives . length ) . toBe ( 1 ) ;
var node = nodes [ 0 ] ;
var mesh = meshes [ 0 ] ;
var primitive = primitives [ 0 ] ;
expect ( node . name ) . toBe ( 'Cube' ) ;
expect ( mesh . name ) . toBe ( 'Cube-Mesh' ) ;
expect ( mesh . positions . length / 3 ) . toBe ( 24 ) ;
expect ( mesh . normals . length / 3 ) . toBe ( 24 ) ;
expect ( mesh . uvs . length / 2 ) . toBe ( 24 ) ;
expect ( primitive . indices . length ) . toBe ( 36 ) ;
expect ( primitive . material ) . toBe ( 'Material' ) ;
} ) , done ) . toResolve ( ) ;
} ) ;
it ( 'loads obj with normals' , function ( done ) {
2017-07-29 13:23:33 -04:00
expect ( loadObj ( objNormalsPath , options )
2017-03-13 15:28:51 -04:00
. then ( function ( data ) {
var mesh = getMeshes ( data ) [ 0 ] ;
expect ( mesh . positions . length / 3 ) . toBe ( 24 ) ;
expect ( mesh . normals . length / 3 ) . toBe ( 24 ) ;
expect ( mesh . uvs . length / 2 ) . toBe ( 0 ) ;
} ) , done ) . toResolve ( ) ;
} ) ;
2018-03-20 22:38:37 -04:00
it ( 'normalizes normals' , function ( done ) {
expect ( loadObj ( objUnnormalizedPath , options )
. then ( function ( data ) {
var scratchNormal = new Cesium . Cartesian3 ( ) ;
var mesh = getMeshes ( data ) [ 0 ] ;
var normals = mesh . normals ;
var normalsLength = normals . length / 3 ;
for ( var i = 0 ; i < normalsLength ; ++ i ) {
var normalX = normals . get ( i * 3 ) ;
var normalY = normals . get ( i * 3 + 1 ) ;
var normalZ = normals . get ( i * 3 + 2 ) ;
var normal = Cartesian3 . fromElements ( normalX , normalY , normalZ , scratchNormal ) ;
expect ( Cartesian3 . magnitude ( normal ) ) . toEqualEpsilon ( 1.0 , CesiumMath . EPSILON5 ) ;
}
} ) , done ) . toResolve ( ) ;
} ) ;
2017-03-13 15:28:51 -04:00
it ( 'loads obj with uvs' , function ( done ) {
2017-07-29 13:23:33 -04:00
expect ( loadObj ( objUvsPath , options )
2017-03-13 15:28:51 -04:00
. then ( function ( data ) {
var mesh = getMeshes ( data ) [ 0 ] ;
expect ( mesh . positions . length / 3 ) . toBe ( 20 ) ;
expect ( mesh . normals . length / 3 ) . toBe ( 0 ) ;
expect ( mesh . uvs . length / 2 ) . toBe ( 20 ) ;
} ) , done ) . toResolve ( ) ;
} ) ;
it ( 'loads obj with negative indices' , function ( done ) {
expect ( Promise . all ( [
2017-07-29 13:23:33 -04:00
loadObj ( objPositionsOnlyPath , options ) ,
loadObj ( objNegativeIndicesPath , options )
2017-03-13 15:28:51 -04:00
] )
. then ( function ( results ) {
var positionsReference = getMeshes ( results [ 0 ] ) [ 0 ] . positions . toFloatBuffer ( ) ;
var positions = getMeshes ( results [ 1 ] ) [ 0 ] . positions . toFloatBuffer ( ) ;
expect ( positions ) . toEqual ( positionsReference ) ;
} ) , done ) . toResolve ( ) ;
} ) ;
it ( 'loads obj with triangle faces' , function ( done ) {
2017-07-29 13:23:33 -04:00
expect ( loadObj ( objTrianglesPath , options )
2017-03-13 15:28:51 -04:00
. then ( function ( data ) {
var mesh = getMeshes ( data ) [ 0 ] ;
var primitive = getPrimitives ( data ) [ 0 ] ;
expect ( mesh . positions . length / 3 ) . toBe ( 24 ) ;
expect ( primitive . indices . length ) . toBe ( 36 ) ;
} ) , done ) . toResolve ( ) ;
} ) ;
it ( 'loads obj with objects' , function ( done ) {
2017-07-29 13:23:33 -04:00
expect ( loadObj ( objObjectsPath , options )
2017-03-13 15:28:51 -04:00
. then ( function ( data ) {
var nodes = data . nodes ;
expect ( nodes . length ) . toBe ( 3 ) ;
expect ( nodes [ 0 ] . name ) . toBe ( 'CubeBlue' ) ;
expect ( nodes [ 1 ] . name ) . toBe ( 'CubeGreen' ) ;
expect ( nodes [ 2 ] . name ) . toBe ( 'CubeRed' ) ;
var primitives = getPrimitives ( data ) ;
expect ( primitives . length ) . toBe ( 3 ) ;
expect ( primitives [ 0 ] . material ) . toBe ( 'Blue' ) ;
expect ( primitives [ 1 ] . material ) . toBe ( 'Green' ) ;
expect ( primitives [ 2 ] . material ) . toBe ( 'Red' ) ;
} ) , done ) . toResolve ( ) ;
} ) ;
it ( 'loads obj with groups' , function ( done ) {
2017-07-29 13:23:33 -04:00
expect ( loadObj ( objGroupsPath , options )
2017-03-13 15:28:51 -04:00
. then ( function ( data ) {
var nodes = data . nodes ;
expect ( nodes . length ) . toBe ( 3 ) ;
expect ( nodes [ 0 ] . name ) . toBe ( 'CubeBlue' ) ;
expect ( nodes [ 1 ] . name ) . toBe ( 'CubeGreen' ) ;
expect ( nodes [ 2 ] . name ) . toBe ( 'CubeRed' ) ;
var primitives = getPrimitives ( data ) ;
expect ( primitives . length ) . toBe ( 3 ) ;
expect ( primitives [ 0 ] . material ) . toBe ( 'Blue' ) ;
expect ( primitives [ 1 ] . material ) . toBe ( 'Green' ) ;
expect ( primitives [ 2 ] . material ) . toBe ( 'Red' ) ;
} ) , done ) . toResolve ( ) ;
} ) ;
it ( 'loads obj with objects and groups' , function ( done ) {
2017-07-29 13:23:33 -04:00
expect ( loadObj ( objObjectsGroupsPath , options )
2017-03-13 15:28:51 -04:00
. then ( function ( data ) {
var nodes = data . nodes ;
expect ( nodes . length ) . toBe ( 3 ) ;
expect ( nodes [ 0 ] . name ) . toBe ( 'CubeBlue' ) ;
expect ( nodes [ 1 ] . name ) . toBe ( 'CubeGreen' ) ;
expect ( nodes [ 2 ] . name ) . toBe ( 'CubeRed' ) ;
var meshes = getMeshes ( data ) ;
expect ( meshes . length ) . toBe ( 3 ) ;
expect ( meshes [ 0 ] . name ) . toBe ( 'CubeBlue_CubeBlue_Blue' ) ;
expect ( meshes [ 1 ] . name ) . toBe ( 'CubeGreen_CubeGreen_Green' ) ;
expect ( meshes [ 2 ] . name ) . toBe ( 'CubeRed_CubeRed_Red' ) ;
var primitives = getPrimitives ( data ) ;
expect ( primitives . length ) . toBe ( 3 ) ;
expect ( primitives [ 0 ] . material ) . toBe ( 'Blue' ) ;
expect ( primitives [ 1 ] . material ) . toBe ( 'Green' ) ;
expect ( primitives [ 2 ] . material ) . toBe ( 'Red' ) ;
} ) , done ) . toResolve ( ) ;
} ) ;
2017-12-29 11:55:21 -05:00
function loadsObjWithObjectsGroupsAndMaterials ( data ) {
var nodes = data . nodes ;
expect ( nodes . length ) . toBe ( 1 ) ;
expect ( nodes [ 0 ] . name ) . toBe ( 'Cube' ) ;
var meshes = getMeshes ( data ) ;
expect ( meshes . length ) . toBe ( 3 ) ;
expect ( meshes [ 0 ] . name ) . toBe ( 'Blue' ) ;
expect ( meshes [ 1 ] . name ) . toBe ( 'Green' ) ;
expect ( meshes [ 2 ] . name ) . toBe ( 'Red' ) ;
var primitives = getPrimitives ( data ) ;
expect ( primitives . length ) . toBe ( 6 ) ;
expect ( primitives [ 0 ] . material ) . toBe ( 'Blue' ) ;
expect ( primitives [ 1 ] . material ) . toBe ( 'Green' ) ;
expect ( primitives [ 2 ] . material ) . toBe ( 'Green' ) ;
expect ( primitives [ 3 ] . material ) . toBe ( 'Red' ) ;
expect ( primitives [ 4 ] . material ) . toBe ( 'Red' ) ;
expect ( primitives [ 5 ] . material ) . toBe ( 'Blue' ) ;
}
it ( 'loads obj with objects, groups, and materials' , function ( done ) {
expect ( loadObj ( objObjectsGroupsMaterialsPath , options )
. then ( function ( data ) {
loadsObjWithObjectsGroupsAndMaterials ( data ) ;
} ) , done ) . toResolve ( ) ;
} ) ;
it ( 'loads obj with objects, groups, and materials (2)' , function ( done ) {
// The usemtl lines are placed in an unordered fashion but
// should produce the same result as the previous test
expect ( loadObj ( objObjectsGroupsMaterialsPath2 , options )
. then ( function ( data ) {
loadsObjWithObjectsGroupsAndMaterials ( data ) ;
} ) , done ) . toResolve ( ) ;
} ) ;
2017-06-14 16:55:03 -04:00
it ( 'loads obj with concave face containing 5 vertices' , function ( done ) {
2017-08-10 09:52:19 -04:00
expect ( loadObj ( objConcavePath , options )
2017-06-14 16:55:03 -04:00
. then ( function ( data ) {
var mesh = getMeshes ( data ) [ 0 ] ;
var primitive = getPrimitives ( data ) [ 0 ] ;
expect ( mesh . positions . length / 3 ) . toBe ( 30 ) ;
expect ( primitive . indices . length ) . toBe ( 48 ) ;
} ) , done ) . toResolve ( ) ;
} ) ;
2017-03-13 15:28:51 -04:00
it ( 'loads obj with usemtl only' , function ( done ) {
2017-07-29 13:23:33 -04:00
expect ( loadObj ( objUsemtlPath , options )
2017-03-13 15:28:51 -04:00
. then ( function ( data ) {
var nodes = data . nodes ;
expect ( nodes . length ) . toBe ( 1 ) ;
expect ( nodes [ 0 ] . name ) . toBe ( 'Node' ) ; // default name
var meshes = getMeshes ( data ) ;
expect ( meshes . length ) . toBe ( 1 ) ;
expect ( meshes [ 0 ] . name ) . toBe ( 'Node-Mesh' ) ;
var primitives = getPrimitives ( data ) ;
expect ( primitives . length ) . toBe ( 3 ) ;
expect ( primitives [ 0 ] . material ) . toBe ( 'Blue' ) ;
expect ( primitives [ 1 ] . material ) . toBe ( 'Green' ) ;
expect ( primitives [ 2 ] . material ) . toBe ( 'Red' ) ;
} ) , done ) . toResolve ( ) ;
} ) ;
it ( 'loads obj with no materials' , function ( done ) {
2017-07-29 13:23:33 -04:00
expect ( loadObj ( objNoMaterialsPath , options )
2017-03-13 15:28:51 -04:00
. then ( function ( data ) {
var nodes = data . nodes ;
expect ( nodes . length ) . toBe ( 1 ) ;
expect ( nodes [ 0 ] . name ) . toBe ( 'Node' ) ; // default name
var primitives = getPrimitives ( data ) ;
expect ( primitives . length ) . toBe ( 1 ) ;
} ) , done ) . toResolve ( ) ;
} ) ;
it ( 'loads obj with multiple materials' , function ( done ) {
// The usemtl markers are interleaved, but should condense to just three primitives
2017-07-29 13:23:33 -04:00
expect ( loadObj ( objMultipleMaterialsPath , options )
2017-03-13 15:28:51 -04:00
. then ( function ( data ) {
var nodes = data . nodes ;
expect ( nodes . length ) . toBe ( 1 ) ;
var primitives = getPrimitives ( data ) ;
expect ( primitives . length ) . toBe ( 3 ) ;
expect ( primitives [ 0 ] . indices . length ) . toBe ( 12 ) ;
expect ( primitives [ 1 ] . indices . length ) . toBe ( 12 ) ;
expect ( primitives [ 2 ] . indices . length ) . toBe ( 12 ) ;
expect ( primitives [ 0 ] . material ) . toBe ( 'Red' ) ;
expect ( primitives [ 1 ] . material ) . toBe ( 'Green' ) ;
expect ( primitives [ 2 ] . material ) . toBe ( 'Blue' ) ;
} ) , done ) . toResolve ( ) ;
} ) ;
it ( 'loads obj uncleaned' , function ( done ) {
// Obj with extraneous o, g, and usemtl lines
// Also tests handling of o and g lines with the same names
2017-07-29 13:23:33 -04:00
expect ( loadObj ( objUncleanedPath , options )
2017-03-13 15:28:51 -04:00
. then ( function ( data ) {
var nodes = data . nodes ;
var meshes = getMeshes ( data ) ;
var primitives = getPrimitives ( data ) ;
expect ( nodes . length ) . toBe ( 1 ) ;
expect ( meshes . length ) . toBe ( 1 ) ;
expect ( primitives . length ) . toBe ( 1 ) ;
expect ( nodes [ 0 ] . name ) . toBe ( 'Cube' ) ;
expect ( meshes [ 0 ] . name ) . toBe ( 'Cube_1' ) ;
} ) , done ) . toResolve ( ) ;
} ) ;
it ( 'loads obj with multiple mtllibs' , function ( done ) {
2017-07-29 13:23:33 -04:00
expect ( loadObj ( objMtllibPath , options )
2017-03-13 15:28:51 -04:00
. then ( function ( data ) {
var materials = data . materials ;
2017-05-04 17:58:13 -04:00
expect ( materials . length ) . toBe ( 3 ) ;
// .mtl files are loaded in an arbitrary order, so sort for testing purposes
materials . sort ( function ( a , b ) {
return a . name . localeCompare ( b . name ) ;
2017-11-17 11:53:04 -05:00
} ) ;
expect ( materials [ 0 ] . name ) . toBe ( 'Blue' ) ;
expect ( materials [ 0 ] . pbrMetallicRoughness . baseColorFactor ) . toEqual ( [ 0.0 , 0.0 , 0.64 , 1.0 ] ) ;
expect ( materials [ 1 ] . name ) . toBe ( 'Green' ) ;
expect ( materials [ 1 ] . pbrMetallicRoughness . baseColorFactor ) . toEqual ( [ 0.0 , 0.64 , 0.0 , 1.0 ] ) ;
expect ( materials [ 2 ] . name ) . toBe ( 'Red' ) ;
expect ( materials [ 2 ] . pbrMetallicRoughness . baseColorFactor ) . toEqual ( [ 0.64 , 0.0 , 0.0 , 1.0 ] ) ;
} ) , done ) . toResolve ( ) ;
} ) ;
it ( 'loads obj with mtllib paths with spaces' , function ( done ) {
expect ( loadObj ( objMtllibSpacesPath , options )
. then ( function ( data ) {
var materials = data . materials ;
expect ( materials . length ) . toBe ( 3 ) ;
// .mtl files are loaded in an arbitrary order, so sort for testing purposes
materials . sort ( function ( a , b ) {
return a . name . localeCompare ( b . name ) ;
2017-05-04 17:58:13 -04:00
} ) ;
expect ( materials [ 0 ] . name ) . toBe ( 'Blue' ) ;
2017-07-29 13:23:33 -04:00
expect ( materials [ 0 ] . pbrMetallicRoughness . baseColorFactor ) . toEqual ( [ 0.0 , 0.0 , 0.64 , 1.0 ] ) ;
2017-05-04 17:58:13 -04:00
expect ( materials [ 1 ] . name ) . toBe ( 'Green' ) ;
2017-07-29 13:23:33 -04:00
expect ( materials [ 1 ] . pbrMetallicRoughness . baseColorFactor ) . toEqual ( [ 0.0 , 0.64 , 0.0 , 1.0 ] ) ;
2017-05-04 17:58:13 -04:00
expect ( materials [ 2 ] . name ) . toBe ( 'Red' ) ;
2017-07-29 13:23:33 -04:00
expect ( materials [ 2 ] . pbrMetallicRoughness . baseColorFactor ) . toEqual ( [ 0.64 , 0.0 , 0.0 , 1.0 ] ) ;
2017-03-13 15:28:51 -04:00
} ) , done ) . toResolve ( ) ;
} ) ;
it ( 'loads obj with missing mtllib' , function ( done ) {
2017-07-29 13:23:33 -04:00
var spy = jasmine . createSpy ( 'logger' ) ;
options . logger = spy ;
expect ( loadObj ( objMissingMtllibPath , options )
2017-03-13 15:28:51 -04:00
. then ( function ( data ) {
2017-05-04 17:58:13 -04:00
expect ( data . materials . length ) . toBe ( 0 ) ;
2017-12-21 22:19:52 -05:00
expect ( spy . calls . argsFor ( 0 ) [ 0 ] . indexOf ( 'ENOENT' ) >= 0 ) . toBe ( true ) ;
expect ( spy . calls . argsFor ( 1 ) [ 0 ] . indexOf ( 'Attempting to read the material file from within the obj directory instead.' ) >= 0 ) . toBe ( true ) ;
expect ( spy . calls . argsFor ( 2 ) [ 0 ] . indexOf ( 'ENOENT' ) >= 0 ) . toBe ( true ) ;
expect ( spy . calls . argsFor ( 3 ) [ 0 ] . indexOf ( 'Could not read material file' ) >= 0 ) . toBe ( true ) ;
2017-04-04 16:45:21 -04:00
} ) , done ) . toResolve ( ) ;
} ) ;
2018-03-06 19:22:06 -05:00
it ( 'loads obj with missing usemtl' , function ( done ) {
expect ( loadObj ( objMissingUsemtlPath , options )
. then ( function ( data ) {
expect ( data . materials . length ) . toBe ( 1 ) ;
expect ( data . nodes [ 0 ] . meshes [ 0 ] . primitives [ 0 ] . material ) . toBe ( 'Material' ) ;
} ) , done ) . toResolve ( ) ;
} ) ;
2017-07-29 13:23:33 -04:00
it ( 'loads .mtl outside of the obj directory' , function ( done ) {
expect ( loadObj ( objExternalResourcesPath , options )
2017-04-04 16:45:21 -04:00
. then ( function ( data ) {
2017-05-04 17:58:13 -04:00
var materials = data . materials ;
expect ( materials . length ) . toBe ( 2 ) ;
// .mtl files are loaded in an arbitrary order, so find the "MaterialTextured" material
var materialTextured = materials [ 0 ] . name === 'MaterialTextured' ? materials [ 0 ] : materials [ 1 ] ;
2017-07-29 13:23:33 -04:00
var baseColorTexture = materialTextured . pbrMetallicRoughness . baseColorTexture ;
expect ( baseColorTexture . source ) . toBeDefined ( ) ;
expect ( baseColorTexture . name ) . toEqual ( 'cesium' ) ;
2017-04-04 16:45:21 -04:00
} ) , done ) . toResolve ( ) ;
} ) ;
2017-07-29 13:23:33 -04:00
it ( 'does not load .mtl outside of the obj directory when secure is true' , function ( done ) {
var spy = jasmine . createSpy ( 'logger' ) ;
options . logger = spy ;
2017-04-10 17:57:56 -04:00
options . secure = true ;
2017-07-29 13:23:33 -04:00
expect ( loadObj ( objExternalResourcesPath , options )
2017-04-04 16:45:21 -04:00
. then ( function ( data ) {
2017-05-04 17:58:13 -04:00
expect ( data . materials . length ) . toBe ( 1 ) ; // obj references 2 materials, one of which is outside the input directory
2017-12-21 22:19:52 -05:00
expect ( spy . calls . argsFor ( 0 ) [ 0 ] . indexOf ( 'The material file is outside of the obj directory and the secure flag is true. Attempting to read the material file from within the obj directory instead.' ) >= 0 ) . toBe ( true ) ;
expect ( spy . calls . argsFor ( 1 ) [ 0 ] . indexOf ( 'ENOENT' ) >= 0 ) . toBe ( true ) ;
expect ( spy . calls . argsFor ( 2 ) [ 0 ] . indexOf ( 'Could not read material file' ) >= 0 ) . toBe ( true ) ;
2017-03-13 15:28:51 -04:00
} ) , done ) . toResolve ( ) ;
} ) ;
2017-11-17 15:07:52 -05:00
it ( 'loads .mtl from root directory when the .mtl path does not exist' , function ( done ) {
expect ( loadObj ( objResourcesInRootPath , options )
. then ( function ( data ) {
var baseColorTexture = data . materials [ 0 ] . pbrMetallicRoughness . baseColorTexture ;
expect ( baseColorTexture . name ) . toBe ( 'cesium' ) ;
expect ( baseColorTexture . source ) . toBeDefined ( ) ;
} ) , done ) . toResolve ( ) ;
} ) ;
2017-11-29 14:21:59 -05:00
it ( 'loads .mtl from root directory when the .mtl path is outside of the obj directory and secure is true' , function ( done ) {
options . secure = true ;
expect ( loadObj ( objExternalResourcesInRootPath , options )
. then ( function ( data ) {
var materials = data . materials ;
expect ( materials . length ) . toBe ( 2 ) ;
// .mtl files are loaded in an arbitrary order, so find the "MaterialTextured" material
var materialTextured = materials [ 0 ] . name === 'MaterialTextured' ? materials [ 0 ] : materials [ 1 ] ;
var baseColorTexture = materialTextured . pbrMetallicRoughness . baseColorTexture ;
expect ( baseColorTexture . source ) . toBeDefined ( ) ;
expect ( baseColorTexture . name ) . toEqual ( 'cesium' ) ;
2017-03-13 15:28:51 -04:00
} ) , done ) . toResolve ( ) ;
} ) ;
it ( 'loads obj with texture' , function ( done ) {
2017-07-29 13:23:33 -04:00
expect ( loadObj ( objTexturedPath , options )
2017-03-13 15:28:51 -04:00
. then ( function ( data ) {
2017-07-29 13:23:33 -04:00
var baseColorTexture = data . materials [ 0 ] . pbrMetallicRoughness . baseColorTexture ;
expect ( baseColorTexture . name ) . toBe ( 'cesium' ) ;
expect ( baseColorTexture . source ) . toBeDefined ( ) ;
2017-03-13 15:28:51 -04:00
} ) , done ) . toResolve ( ) ;
} ) ;
it ( 'loads obj with missing texture' , function ( done ) {
2017-07-29 13:23:33 -04:00
var spy = jasmine . createSpy ( 'logger' ) ;
options . logger = spy ;
2017-03-13 15:28:51 -04:00
2017-07-29 13:23:33 -04:00
expect ( loadObj ( objMissingTexturePath , options )
2017-03-13 15:28:51 -04:00
. then ( function ( data ) {
2017-07-29 13:23:33 -04:00
var baseColorTexture = data . materials [ 0 ] . pbrMetallicRoughness . baseColorTexture ;
expect ( baseColorTexture ) . toBeUndefined ( ) ;
2017-12-21 22:19:52 -05:00
expect ( spy . calls . argsFor ( 0 ) [ 0 ] . indexOf ( 'ENOENT' ) >= 0 ) . toBe ( true ) ;
expect ( spy . calls . argsFor ( 1 ) [ 0 ] . indexOf ( 'Attempting to read the texture file from within the obj directory instead.' ) >= 0 ) . toBe ( true ) ;
expect ( spy . calls . argsFor ( 2 ) [ 0 ] . indexOf ( 'ENOENT' ) >= 0 ) . toBe ( true ) ;
expect ( spy . calls . argsFor ( 3 ) [ 0 ] . indexOf ( 'Could not read texture file' ) >= 0 ) . toBe ( true ) ;
2017-03-13 15:28:51 -04:00
} ) , done ) . toResolve ( ) ;
} ) ;
2017-07-29 13:23:33 -04:00
it ( 'loads obj with subdirectories' , function ( done ) {
expect ( loadObj ( objSubdirectoriesPath , options )
2017-04-20 14:41:39 -04:00
. then ( function ( data ) {
2017-07-29 13:23:33 -04:00
var baseColorTexture = data . materials [ 0 ] . pbrMetallicRoughness . baseColorTexture ;
expect ( baseColorTexture . name ) . toBe ( 'cesium' ) ;
expect ( baseColorTexture . source ) . toBeDefined ( ) ;
2017-04-20 14:41:39 -04:00
} ) , done ) . toResolve ( ) ;
} ) ;
it ( 'throws when file has invalid contents' , function ( done ) {
2017-07-29 13:23:33 -04:00
expect ( loadObj ( objInvalidContentsPath , options ) , done ) . toRejectWith ( RuntimeError ) ;
2017-03-13 15:28:51 -04:00
} ) ;
it ( 'throw when reading invalid file' , function ( done ) {
2017-07-29 13:23:33 -04:00
expect ( loadObj ( objInvalidPath , options ) , done ) . toRejectWith ( Error ) ;
2017-03-13 15:28:51 -04:00
} ) ;
} ) ;