2017-03-13 15:28:51 -04:00
'use strict' ;
2019-02-05 20:59:09 -05:00
const Cesium = require ( 'cesium' ) ;
const fsExtra = require ( 'fs-extra' ) ;
const loadMtl = require ( '../../lib/loadMtl' ) ;
const loadTexture = require ( '../../lib/loadTexture' ) ;
const obj2gltf = require ( '../../lib/obj2gltf' ) ;
const clone = Cesium . clone ;
const coloredMaterialPath = 'specs/data/box/box.mtl' ;
const texturedMaterialPath = 'specs/data/box-complex-material/box-complex-material.mtl' ;
const texturedWithOptionsMaterialPath = 'specs/data/box-texture-options/box-texture-options.mtl' ;
const multipleMaterialsPath = 'specs/data/box-multiple-materials/box-multiple-materials.mtl' ;
const externalMaterialPath = 'specs/data/box-external-resources/box-external-resources.mtl' ;
const resourcesInRootMaterialPath = 'specs/data/box-resources-in-root/box-resources-in-root.mtl' ;
const externalInRootMaterialPath = 'specs/data/box-external-resources-in-root/box-external-resources-in-root.mtl' ;
const transparentMaterialPath = 'specs/data/box-transparent/box-transparent.mtl' ;
2019-10-26 19:03:32 -04:00
const sharedTexturesMaterialPath = 'specs/data/box-shared-textures/box-shared-textures.mtl' ;
const sharedTexturesMaterial2Path = 'specs/data/box-shared-textures-2/box-shared-textures-2.mtl' ;
2019-02-05 20:59:09 -05:00
const diffuseTexturePath = 'specs/data/box-textured/cesium.png' ;
const transparentDiffuseTexturePath = 'specs/data/box-complex-material/diffuse.png' ;
const alphaTexturePath = 'specs/data/box-complex-material-alpha/alpha.png' ;
const ambientTexturePath = 'specs/data/box-complex-material/ambient.gif' ;
const normalTexturePath = 'specs/data/box-complex-material/bump.png' ;
const emissiveTexturePath = 'specs/data/box-complex-material/emission.jpg' ;
const specularTexturePath = 'specs/data/box-complex-material/specular.jpeg' ;
const specularShininessTexturePath = 'specs/data/box-complex-material/shininess.png' ;
let diffuseTexture ;
let transparentDiffuseTexture ;
let alphaTexture ;
let ambientTexture ;
let normalTexture ;
let emissiveTexture ;
let specularTexture ;
let specularShininessTexture ;
const checkTransparencyOptions = {
2017-07-29 13:23:33 -04:00
checkTransparency : true
} ;
2019-02-05 20:59:09 -05:00
const decodeOptions = {
2017-07-29 13:23:33 -04:00
decode : true
} ;
2019-02-05 20:59:09 -05:00
let options ;
describe ( 'loadMtl' , ( ) => {
beforeAll ( async ( ) => {
diffuseTexture = await loadTexture ( diffuseTexturePath , decodeOptions ) ;
transparentDiffuseTexture = await loadTexture ( transparentDiffuseTexturePath , checkTransparencyOptions ) ;
alphaTexture = await loadTexture ( alphaTexturePath , decodeOptions ) ;
ambientTexture = await loadTexture ( ambientTexturePath ) ;
normalTexture = await loadTexture ( normalTexturePath ) ;
emissiveTexture = await loadTexture ( emissiveTexturePath ) ;
specularTexture = await loadTexture ( specularTexturePath , decodeOptions ) ;
specularShininessTexture = await loadTexture ( specularShininessTexturePath , decodeOptions ) ;
2017-07-29 13:23:33 -04:00
} ) ;
2019-02-05 20:59:09 -05:00
beforeEach ( ( ) => {
2017-07-29 13:23:33 -04:00
options = clone ( obj2gltf . defaults ) ;
options . overridingTextures = { } ;
2019-02-05 20:59:09 -05:00
options . logger = ( ) => { } ;
2017-07-29 13:23:33 -04:00
} ) ;
2019-02-05 20:59:09 -05:00
it ( 'loads mtl' , async ( ) => {
2017-07-29 13:23:33 -04:00
options . metallicRoughness = true ;
2019-02-05 20:59:09 -05:00
const materials = await loadMtl ( coloredMaterialPath , options ) ;
expect ( materials . length ) . toBe ( 1 ) ;
const material = materials [ 0 ] ;
const pbr = material . pbrMetallicRoughness ;
expect ( pbr . baseColorTexture ) . toBeUndefined ( ) ;
expect ( pbr . metallicRoughnessTexture ) . toBeUndefined ( ) ;
expect ( pbr . baseColorFactor ) . toEqual ( [ 0.64 , 0.64 , 0.64 , 1.0 ] ) ;
expect ( pbr . metallicFactor ) . toBe ( 0.5 ) ;
expect ( pbr . roughnessFactor ) . toBe ( 96.078431 ) ;
expect ( material . name ) . toBe ( 'Material' ) ;
expect ( material . emissiveTexture ) . toBeUndefined ( ) ;
expect ( material . normalTexture ) . toBeUndefined ( ) ;
expect ( material . ambientTexture ) . toBeUndefined ( ) ;
expect ( material . emissiveFactor ) . toEqual ( [ 0.0 , 0.0 , 0.1 ] ) ;
expect ( material . alphaMode ) . toBe ( 'OPAQUE' ) ;
expect ( material . doubleSided ) . toBe ( false ) ;
2017-07-29 13:23:33 -04:00
} ) ;
2019-02-05 20:59:09 -05:00
it ( 'loads mtl with textures' , async ( ) => {
2017-07-29 13:23:33 -04:00
options . metallicRoughness = true ;
2019-02-05 20:59:09 -05:00
const materials = await loadMtl ( texturedMaterialPath , options ) ;
expect ( materials . length ) . toBe ( 1 ) ;
const material = materials [ 0 ] ;
const pbr = material . pbrMetallicRoughness ;
expect ( pbr . baseColorTexture ) . toBeDefined ( ) ;
expect ( pbr . metallicRoughnessTexture ) . toBeDefined ( ) ;
expect ( pbr . baseColorFactor ) . toEqual ( [ 1.0 , 1.0 , 1.0 , 0.9 ] ) ;
expect ( pbr . metallicFactor ) . toBe ( 1.0 ) ;
expect ( pbr . roughnessFactor ) . toBe ( 1.0 ) ;
expect ( material . name ) . toBe ( 'Material' ) ;
expect ( material . emissiveTexture ) . toBeDefined ( ) ;
expect ( material . normalTexture ) . toBeDefined ( ) ;
expect ( material . occlusionTexture ) . toBeDefined ( ) ;
expect ( material . emissiveFactor ) . toEqual ( [ 1.0 , 1.0 , 1.0 ] ) ;
expect ( material . alphaMode ) . toBe ( 'BLEND' ) ;
expect ( material . doubleSided ) . toBe ( true ) ;
2017-10-10 13:23:09 +02:00
} ) ;
2019-02-05 20:59:09 -05:00
it ( 'loads mtl with textures having options' , async ( ) => {
2017-10-10 13:23:09 +02:00
options . metallicRoughness = true ;
2019-02-05 20:59:09 -05:00
const materials = await loadMtl ( texturedWithOptionsMaterialPath , options ) ;
expect ( materials . length ) . toBe ( 1 ) ;
const material = materials [ 0 ] ;
const pbr = material . pbrMetallicRoughness ;
expect ( pbr . baseColorTexture ) . toBeDefined ( ) ;
expect ( pbr . metallicRoughnessTexture ) . toBeDefined ( ) ;
expect ( pbr . baseColorFactor ) . toEqual ( [ 1.0 , 1.0 , 1.0 , 0.9 ] ) ;
expect ( pbr . metallicFactor ) . toBe ( 1.0 ) ;
expect ( pbr . roughnessFactor ) . toBe ( 1.0 ) ;
expect ( material . name ) . toBe ( 'Material' ) ;
expect ( material . emissiveTexture ) . toBeDefined ( ) ;
expect ( material . normalTexture ) . toBeDefined ( ) ;
expect ( material . occlusionTexture ) . toBeDefined ( ) ;
expect ( material . emissiveFactor ) . toEqual ( [ 1.0 , 1.0 , 1.0 ] ) ;
expect ( material . alphaMode ) . toBe ( 'BLEND' ) ;
expect ( material . doubleSided ) . toBe ( true ) ;
2017-03-13 15:28:51 -04:00
} ) ;
2019-02-05 20:59:09 -05:00
it ( 'loads mtl with multiple materials' , async ( ) => {
2017-07-29 13:23:33 -04:00
options . metallicRoughness = true ;
2019-02-05 20:59:09 -05:00
const materials = await loadMtl ( multipleMaterialsPath , options ) ;
expect ( materials . length ) . toBe ( 3 ) ;
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 ] ) ;
2017-07-29 13:23:33 -04:00
} ) ;
2019-02-05 20:59:09 -05:00
it ( 'sets overriding textures' , async ( ) => {
2017-07-29 13:23:33 -04:00
spyOn ( fsExtra , 'readFile' ) . and . callThrough ( ) ;
options . overridingTextures = {
metallicRoughnessOcclusionTexture : alphaTexturePath ,
baseColorTexture : alphaTexturePath ,
emissiveTexture : emissiveTexturePath
} ;
2019-02-05 20:59:09 -05:00
const materials = await loadMtl ( texturedMaterialPath , options ) ;
const material = materials [ 0 ] ;
const pbr = material . pbrMetallicRoughness ;
expect ( pbr . baseColorTexture . name ) . toBe ( 'alpha' ) ;
expect ( pbr . metallicRoughnessTexture . name ) . toBe ( 'alpha' ) ;
expect ( material . emissiveTexture . name ) . toBe ( 'emission' ) ;
expect ( material . normalTexture . name ) . toBe ( 'bump' ) ;
expect ( fsExtra . readFile . calls . count ( ) ) . toBe ( 3 ) ;
2017-07-29 13:23:33 -04:00
} ) ;
2019-02-05 20:59:09 -05:00
it ( 'loads texture outside of the mtl directory' , async ( ) => {
const materials = await loadMtl ( externalMaterialPath , options ) ;
const material = materials [ 0 ] ;
const baseColorTexture = material . pbrMetallicRoughness . baseColorTexture ;
expect ( baseColorTexture . source ) . toBeDefined ( ) ;
expect ( baseColorTexture . name ) . toBe ( 'cesium' ) ;
2017-07-29 13:23:33 -04:00
} ) ;
2019-02-05 20:59:09 -05:00
it ( 'does not load texture outside of the mtl directory when secure is true' , async ( ) => {
const spy = jasmine . createSpy ( 'logger' ) ;
2017-07-29 13:23:33 -04:00
options . logger = spy ;
options . secure = true ;
2019-02-05 20:59:09 -05:00
const materials = await loadMtl ( externalMaterialPath , options ) ;
const material = materials [ 0 ] ;
const baseColorTexture = material . pbrMetallicRoughness . baseColorTexture ;
expect ( baseColorTexture ) . toBeUndefined ( ) ;
expect ( spy . calls . argsFor ( 0 ) [ 0 ] . indexOf ( 'Texture file is outside of the mtl directory and the secure flag is true. Attempting to read the texture 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 texture file' ) >= 0 ) . toBe ( true ) ;
2017-03-13 15:28:51 -04:00
} ) ;
2017-07-29 13:23:33 -04:00
2019-02-05 20:59:09 -05:00
it ( 'loads textures from root directory when the texture paths do not exist' , async ( ) => {
const materials = await loadMtl ( resourcesInRootMaterialPath , options ) ;
const material = materials [ 0 ] ;
const baseColorTexture = material . pbrMetallicRoughness . baseColorTexture ;
expect ( baseColorTexture . source ) . toBeDefined ( ) ;
expect ( baseColorTexture . name ) . toBe ( 'cesium' ) ;
2017-11-29 14:21:59 -05:00
} ) ;
2019-02-05 20:59:09 -05:00
it ( 'loads textures from root directory when texture is outside of the mtl directory and secure is true' , async ( ) => {
2017-11-29 14:21:59 -05:00
options . secure = true ;
2019-02-05 20:59:09 -05:00
const materials = await loadMtl ( externalInRootMaterialPath , options ) ;
const material = materials [ 0 ] ;
const baseColorTexture = material . pbrMetallicRoughness . baseColorTexture ;
expect ( baseColorTexture . source ) . toBeDefined ( ) ;
expect ( baseColorTexture . name ) . toBe ( 'cesium' ) ;
2017-11-17 15:07:52 -05:00
} ) ;
2019-02-05 20:59:09 -05:00
it ( 'alpha of 0.0 is treated as 1.0' , async ( ) => {
const materials = await loadMtl ( transparentMaterialPath , options ) ;
expect ( materials . length ) . toBe ( 1 ) ;
const material = materials [ 0 ] ;
const pbr = material . pbrMetallicRoughness ;
expect ( pbr . baseColorTexture ) . toBeUndefined ( ) ;
expect ( pbr . metallicRoughnessTexture ) . toBeUndefined ( ) ;
expect ( pbr . baseColorFactor [ 3 ] ) . toEqual ( 1.0 ) ;
expect ( material . alphaMode ) . toBe ( 'OPAQUE' ) ;
expect ( material . doubleSided ) . toBe ( false ) ;
2017-09-19 13:09:58 -04:00
} ) ;
2019-02-05 20:59:09 -05:00
it ( 'ambient texture is ignored if it is the same as the diffuse texture' , async ( ) => {
2019-10-26 19:03:32 -04:00
const materials = await loadMtl ( sharedTexturesMaterialPath , options ) ;
2019-02-05 20:59:09 -05:00
expect ( materials . length ) . toBe ( 1 ) ;
const material = materials [ 0 ] ;
const pbr = material . pbrMetallicRoughness ;
expect ( pbr . baseColorTexture ) . toBeDefined ( ) ;
expect ( pbr . occlusionTexture ) . toBeUndefined ( ) ;
2018-01-19 11:02:14 -05:00
} ) ;
2019-10-26 19:03:32 -04:00
it ( 'texture referenced by specular is decoded' , async ( ) => {
const materials = await loadMtl ( sharedTexturesMaterialPath , options ) ;
expect ( materials . length ) . toBe ( 1 ) ;
const material = materials [ 0 ] ;
const pbr = material . pbrMetallicRoughness ;
expect ( pbr . baseColorTexture . pixels ) . toBeDefined ( ) ;
expect ( pbr . baseColorTexture . source ) . toBeDefined ( ) ;
expect ( pbr . metallicRoughnessTexture . pixels ) . toBeDefined ( ) ;
expect ( pbr . metallicRoughnessTexture . source ) . toBeUndefined ( ) ;
} ) ;
2019-10-27 13:28:36 -04:00
it ( 'texture referenced by diffuse and emissive is not decoded' , async ( ) => {
2019-10-26 19:03:32 -04:00
const materials = await loadMtl ( sharedTexturesMaterial2Path , options ) ;
expect ( materials . length ) . toBe ( 1 ) ;
const material = materials [ 0 ] ;
const pbr = material . pbrMetallicRoughness ;
expect ( pbr . baseColorTexture ) . toBe ( material . emissiveTexture ) ;
expect ( pbr . baseColorTexture . pixels ) . toBeUndefined ( ) ;
expect ( pbr . baseColorTexture . source ) . toBeDefined ( ) ;
} ) ;
2019-02-05 20:59:09 -05:00
describe ( 'metallicRoughness' , ( ) => {
it ( 'creates default material' , ( ) => {
const material = loadMtl . _createMaterial ( undefined , options ) ;
const pbr = material . pbrMetallicRoughness ;
2017-07-29 13:23:33 -04:00
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 ) ;
} ) ;
2019-02-05 20:59:09 -05:00
it ( 'creates material with textures' , ( ) => {
2017-07-29 13:23:33 -04:00
options . metallicRoughness = true ;
2019-02-05 20:59:09 -05:00
const material = loadMtl . _createMaterial ( {
2017-07-29 13:23:33 -04:00
diffuseTexture : diffuseTexture ,
ambientTexture : ambientTexture ,
normalTexture : normalTexture ,
emissiveTexture : emissiveTexture ,
specularTexture : specularTexture ,
specularShininessTexture : specularShininessTexture
} , options ) ;
2019-02-05 20:59:09 -05:00
const pbr = material . pbrMetallicRoughness ;
2017-07-29 13:23:33 -04:00
expect ( pbr . baseColorTexture ) . toBeDefined ( ) ;
expect ( pbr . metallicRoughnessTexture ) . toBeDefined ( ) ;
expect ( pbr . baseColorFactor ) . toEqual ( [ 1.0 , 1.0 , 1.0 , 1.0 ] ) ;
expect ( pbr . metallicFactor ) . toBe ( 1.0 ) ;
expect ( pbr . roughnessFactor ) . toBe ( 1.0 ) ;
expect ( material . emissiveTexture ) . toBeDefined ( ) ;
expect ( material . normalTexture ) . toBeDefined ( ) ;
expect ( material . occlusionTexture ) . toBeDefined ( ) ;
expect ( material . emissiveFactor ) . toEqual ( [ 1.0 , 1.0 , 1.0 ] ) ;
expect ( material . alphaMode ) . toBe ( 'OPAQUE' ) ;
expect ( material . doubleSided ) . toBe ( false ) ;
} ) ;
2019-02-05 20:59:09 -05:00
it ( 'packs occlusion in metallic roughness texture' , ( ) => {
2017-07-29 13:23:33 -04:00
options . metallicRoughness = true ;
options . packOcclusion = true ;
2019-02-05 20:59:09 -05:00
const material = loadMtl . _createMaterial ( {
2017-07-29 13:23:33 -04:00
ambientTexture : alphaTexture ,
specularTexture : specularTexture ,
specularShininessTexture : specularShininessTexture
} , options ) ;
2019-02-05 20:59:09 -05:00
const pbr = material . pbrMetallicRoughness ;
2017-07-29 13:23:33 -04:00
expect ( pbr . metallicRoughnessTexture ) . toBeDefined ( ) ;
expect ( pbr . metallicRoughnessTexture ) . toBe ( material . occlusionTexture ) ;
} ) ;
2019-02-05 20:59:09 -05:00
it ( 'does not create metallic roughness texture if decoded texture data is not available' , ( ) => {
2017-07-29 13:23:33 -04:00
options . metallicRoughness = true ;
options . packOcclusion = true ;
2019-02-05 20:59:09 -05:00
const material = loadMtl . _createMaterial ( {
2017-07-29 13:23:33 -04:00
ambientTexture : ambientTexture , // Is a .gif which can't be decoded
specularTexture : specularTexture ,
specularShininessTexture : specularShininessTexture
} , options ) ;
2019-02-05 20:59:09 -05:00
const pbr = material . pbrMetallicRoughness ;
2017-07-29 13:23:33 -04:00
expect ( pbr . metallicRoughnessTexture ) . toBeUndefined ( ) ;
expect ( material . occlusionTexture ) . toBeUndefined ( ) ;
} ) ;
2019-02-05 20:59:09 -05:00
it ( 'sets material for transparent diffuse texture' , ( ) => {
2017-07-29 13:23:33 -04:00
options . metallicRoughness = true ;
2019-02-05 20:59:09 -05:00
const material = loadMtl . _createMaterial ( {
2017-07-29 13:23:33 -04:00
diffuseTexture : transparentDiffuseTexture
} , options ) ;
expect ( material . alphaMode ) . toBe ( 'BLEND' ) ;
expect ( material . doubleSided ) . toBe ( true ) ;
} ) ;
2018-01-03 20:41:25 -05:00
2019-02-05 20:59:09 -05:00
it ( 'packs alpha texture in base color texture' , ( ) => {
2018-01-03 20:41:25 -05:00
options . metallicRoughness = true ;
2019-02-05 20:59:09 -05:00
const material = loadMtl . _createMaterial ( {
2018-01-03 20:41:25 -05:00
diffuseTexture : diffuseTexture ,
alphaTexture : alphaTexture
} , options ) ;
2019-02-05 20:59:09 -05:00
const pbr = material . pbrMetallicRoughness ;
2018-01-03 20:41:25 -05:00
expect ( pbr . baseColorTexture ) . toBeDefined ( ) ;
2019-02-05 20:59:09 -05:00
let hasBlack = false ;
let hasWhite = false ;
const pixels = pbr . baseColorTexture . pixels ;
const pixelsLength = pixels . length / 4 ;
for ( let i = 0 ; i < pixelsLength ; ++ i ) {
const alpha = pixels [ i * 4 + 3 ] ;
2018-01-03 20:41:25 -05:00
hasBlack = hasBlack || ( alpha === 0 ) ;
hasWhite = hasWhite || ( alpha === 255 ) ;
}
expect ( hasBlack ) . toBe ( true ) ;
expect ( hasWhite ) . toBe ( true ) ;
expect ( pbr . baseColorFactor [ 3 ] ) . toEqual ( 1 ) ;
expect ( material . alphaMode ) . toBe ( 'BLEND' ) ;
expect ( material . doubleSided ) . toBe ( true ) ;
} ) ;
2019-07-19 13:15:38 -04:00
2019-07-22 18:39:30 -04:00
it ( 'uses diffuse texture if diffuse and alpha are the same' , ( ) => {
2019-07-19 13:15:38 -04:00
options . metallicRoughness = true ;
const material = loadMtl . _createMaterial ( {
2019-07-22 18:39:30 -04:00
diffuseTexture : diffuseTexture ,
2019-07-19 13:15:38 -04:00
alphaTexture : diffuseTexture
} , options ) ;
const pbr = material . pbrMetallicRoughness ;
2019-07-22 18:39:30 -04:00
expect ( pbr . baseColorTexture ) . toBe ( diffuseTexture ) ;
2019-07-19 14:52:12 -04:00
expect ( material . alphaMode ) . toBe ( 'BLEND' ) ;
expect ( material . doubleSided ) . toBe ( true ) ;
2019-07-19 13:15:38 -04:00
} ) ;
2017-07-29 13:23:33 -04:00
} ) ;
2019-02-05 20:59:09 -05:00
describe ( 'specularGlossiness' , ( ) => {
it ( 'creates default material' , ( ) => {
2017-07-29 13:23:33 -04:00
options . specularGlossiness = true ;
2019-02-05 20:59:09 -05:00
const material = loadMtl . _createMaterial ( undefined , options ) ;
const pbr = material . extensions . KHR _materials _pbrSpecularGlossiness ;
2017-07-29 13:23:33 -04:00
expect ( pbr . diffuseTexture ) . toBeUndefined ( ) ;
expect ( pbr . specularGlossinessTexture ) . toBeUndefined ( ) ;
expect ( pbr . diffuseFactor ) . toEqual ( [ 0.5 , 0.5 , 0.5 , 1.0 ] ) ;
expect ( pbr . specularFactor ) . toEqual ( [ 0.0 , 0.0 , 0.0 ] ) ; // No specular color
expect ( pbr . glossinessFactor ) . toEqual ( 0.0 ) ; // Rough surface
expect ( material . emissiveTexture ) . toBeUndefined ( ) ;
expect ( material . normalTexture ) . toBeUndefined ( ) ;
expect ( material . occlusionTexture ) . toBeUndefined ( ) ;
expect ( material . emissiveFactor ) . toEqual ( [ 0.0 , 0.0 , 0.0 ] ) ;
expect ( material . alphaMode ) . toBe ( 'OPAQUE' ) ;
expect ( material . doubleSided ) . toBe ( false ) ;
} ) ;
2019-02-05 20:59:09 -05:00
it ( 'creates material with textures' , ( ) => {
2017-07-29 13:23:33 -04:00
options . specularGlossiness = true ;
2019-02-05 20:59:09 -05:00
const material = loadMtl . _createMaterial ( {
2017-07-29 13:23:33 -04:00
diffuseTexture : diffuseTexture ,
ambientTexture : ambientTexture ,
normalTexture : normalTexture ,
emissiveTexture : emissiveTexture ,
specularTexture : specularTexture ,
specularShininessTexture : specularShininessTexture
} , options ) ;
2019-02-05 20:59:09 -05:00
const pbr = material . extensions . KHR _materials _pbrSpecularGlossiness ;
2017-07-29 13:23:33 -04:00
expect ( pbr . diffuseTexture ) . toBeDefined ( ) ;
expect ( pbr . specularGlossinessTexture ) . toBeDefined ( ) ;
expect ( pbr . diffuseFactor ) . toEqual ( [ 1.0 , 1.0 , 1.0 , 1.0 ] ) ;
expect ( pbr . specularFactor ) . toEqual ( [ 1.0 , 1.0 , 1.0 ] ) ;
expect ( pbr . glossinessFactor ) . toEqual ( 1.0 ) ;
expect ( material . emissiveTexture ) . toBeDefined ( ) ;
expect ( material . normalTexture ) . toBeDefined ( ) ;
expect ( material . occlusionTexture ) . toBeDefined ( ) ;
expect ( material . emissiveFactor ) . toEqual ( [ 1.0 , 1.0 , 1.0 ] ) ;
expect ( material . alphaMode ) . toBe ( 'OPAQUE' ) ;
expect ( material . doubleSided ) . toBe ( false ) ;
} ) ;
2019-02-05 20:59:09 -05:00
it ( 'does not create specular glossiness texture if decoded texture data is not available' , ( ) => {
2017-07-29 13:23:33 -04:00
options . specularGlossiness = true ;
2019-02-05 20:59:09 -05:00
const material = loadMtl . _createMaterial ( {
2017-07-29 13:23:33 -04:00
specularTexture : ambientTexture , // Is a .gif which can't be decoded
specularShininessTexture : specularShininessTexture
} , options ) ;
2019-02-05 20:59:09 -05:00
const pbr = material . extensions . KHR _materials _pbrSpecularGlossiness ;
2017-07-29 13:23:33 -04:00
expect ( pbr . specularGlossinessTexture ) . toBeUndefined ( ) ;
} ) ;
2019-02-05 20:59:09 -05:00
it ( 'sets material for transparent diffuse texture' , ( ) => {
2017-07-29 13:23:33 -04:00
options . specularGlossiness = true ;
2019-02-05 20:59:09 -05:00
const material = loadMtl . _createMaterial ( {
2017-07-29 13:23:33 -04:00
diffuseTexture : transparentDiffuseTexture
} , options ) ;
expect ( material . alphaMode ) . toBe ( 'BLEND' ) ;
expect ( material . doubleSided ) . toBe ( true ) ;
} ) ;
2018-01-03 20:41:25 -05:00
2019-02-05 20:59:09 -05:00
it ( 'packs alpha texture in diffuse texture' , ( ) => {
2018-01-03 20:41:25 -05:00
options . specularGlossiness = true ;
2019-02-05 20:59:09 -05:00
const material = loadMtl . _createMaterial ( {
2018-01-03 20:41:25 -05:00
diffuseTexture : diffuseTexture ,
alphaTexture : alphaTexture
} , options ) ;
2019-02-05 20:59:09 -05:00
const pbr = material . extensions . KHR _materials _pbrSpecularGlossiness ;
2018-01-03 20:41:25 -05:00
expect ( pbr . diffuseTexture ) . toBeDefined ( ) ;
2019-02-05 20:59:09 -05:00
let hasBlack = false ;
let hasWhite = false ;
const pixels = pbr . diffuseTexture . pixels ;
const pixelsLength = pixels . length / 4 ;
for ( let i = 0 ; i < pixelsLength ; ++ i ) {
const alpha = pixels [ i * 4 + 3 ] ;
2018-01-03 20:41:25 -05:00
hasBlack = hasBlack || ( alpha === 0 ) ;
hasWhite = hasWhite || ( alpha === 255 ) ;
}
expect ( hasBlack ) . toBe ( true ) ;
expect ( hasWhite ) . toBe ( true ) ;
expect ( pbr . diffuseFactor [ 3 ] ) . toEqual ( 1 ) ;
expect ( material . alphaMode ) . toBe ( 'BLEND' ) ;
expect ( material . doubleSided ) . toBe ( true ) ;
} ) ;
2019-07-19 14:52:12 -04:00
2019-07-22 18:39:30 -04:00
it ( 'uses diffuse texture if diffuse and alpha are the same' , ( ) => {
2019-07-19 14:52:12 -04:00
options . specularGlossiness = true ;
const material = loadMtl . _createMaterial ( {
2019-07-22 18:39:30 -04:00
diffuseTexture : diffuseTexture ,
2019-07-19 14:52:12 -04:00
alphaTexture : diffuseTexture
} , options ) ;
const pbr = material . extensions . KHR _materials _pbrSpecularGlossiness ;
2019-07-22 18:39:30 -04:00
expect ( pbr . diffuseTexture ) . toEqual ( diffuseTexture ) ;
2019-07-19 14:52:12 -04:00
expect ( material . alphaMode ) . toBe ( 'BLEND' ) ;
expect ( material . doubleSided ) . toBe ( true ) ;
} ) ;
2017-07-29 13:23:33 -04:00
} ) ;
2017-03-13 15:28:51 -04:00
} ) ;