mirror of
https://github.com/CesiumGS/obj2gltf.git
synced 2025-01-31 03:53:13 -05:00
Rounded out materialCommon, pbrMetallicRoughness, and pbrSpecularGlosiness tests
This commit is contained in:
parent
df20748d60
commit
608234dcc4
@ -184,7 +184,9 @@ function addTexture(gltf, image) {
|
|||||||
source : imageIndex
|
source : imageIndex
|
||||||
});
|
});
|
||||||
|
|
||||||
return textureIndex;
|
return {
|
||||||
|
index : textureIndex
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function getTexture(gltf, image) {
|
function getTexture(gltf, image) {
|
||||||
@ -279,18 +281,14 @@ function getMinimumDimensions(images, options) {
|
|||||||
var length = images.length;
|
var length = images.length;
|
||||||
for (i = 0; i < length; ++i) {
|
for (i = 0; i < length; ++i) {
|
||||||
image = images[i];
|
image = images[i];
|
||||||
if (defined(image)) {
|
width = Math.min(image.width, width);
|
||||||
width = Math.min(image.width, width);
|
height = Math.min(image.height, height);
|
||||||
height = Math.min(image.height, height);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for (i = 0; i < length; ++i) {
|
for (i = 0; i < length; ++i) {
|
||||||
image = images[i];
|
image = images[i];
|
||||||
if (defined(image)) {
|
if (image.width !== width || image.height !== height) {
|
||||||
if (image.width !== width || image.height !== height) {
|
options.logger('Image ' + image.path + ' will be scaled from ' + image.width + 'x' + image.height + ' to ' + width + 'x' + height + '.');
|
||||||
options.logger('Image ' + image.path + ' will be scaled from ' + image.width + 'x' + image.height + ' to ' + width + 'x' + height + '.');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -347,7 +345,11 @@ function createMetallicRoughnessTexture(gltf, materialName, metallicImage, rough
|
|||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
var dimensions = getMinimumDimensions([metallicImage, roughnessImage, occlusionImage], options);
|
var packedImages = [metallicImage, roughnessImage, occlusionImage].filter(function(image) {
|
||||||
|
return defined(image) && defined(image.decoded);
|
||||||
|
});
|
||||||
|
|
||||||
|
var dimensions = getMinimumDimensions(packedImages, options);
|
||||||
var width = dimensions[0];
|
var width = dimensions[0];
|
||||||
var height = dimensions[1];
|
var height = dimensions[1];
|
||||||
var pixelsLength = width * height;
|
var pixelsLength = width * height;
|
||||||
@ -406,7 +408,11 @@ function createSpecularGlossinessTexture(gltf, materialName, specularImage, glos
|
|||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
var dimensions = getMinimumDimensions([specularImage, glossinessImage], options);
|
var packedImages = [specularImage, glossinessImage].filter(function(image) {
|
||||||
|
return defined(image) && defined(image.decoded);
|
||||||
|
});
|
||||||
|
|
||||||
|
var dimensions = getMinimumDimensions(packedImages, options);
|
||||||
var width = dimensions[0];
|
var width = dimensions[0];
|
||||||
var height = dimensions[1];
|
var height = dimensions[1];
|
||||||
var pixelsLength = width * height;
|
var pixelsLength = width * height;
|
||||||
@ -460,7 +466,7 @@ function createSpecularGlossinessMaterial(gltf, images, material, options) {
|
|||||||
|
|
||||||
var emissiveFactor = getEmissiveFactor(material);
|
var emissiveFactor = getEmissiveFactor(material);
|
||||||
var diffuseFactor = material.diffuseColor;
|
var diffuseFactor = material.diffuseColor;
|
||||||
var specularFactor = material.specularColor;
|
var specularFactor = material.specularColor.slice(0, 3);
|
||||||
var glossinessFactor = material.specularShininess;
|
var glossinessFactor = material.specularShininess;
|
||||||
|
|
||||||
if (defined(emissiveTexture)) {
|
if (defined(emissiveTexture)) {
|
||||||
@ -472,7 +478,7 @@ function createSpecularGlossinessMaterial(gltf, images, material, options) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (defined(specularImage)) {
|
if (defined(specularImage)) {
|
||||||
specularFactor = 1.0;
|
specularFactor = [1.0, 1.0, 1.0];
|
||||||
}
|
}
|
||||||
|
|
||||||
if (defined(glossinessImage)) {
|
if (defined(glossinessImage)) {
|
||||||
@ -484,7 +490,7 @@ function createSpecularGlossinessMaterial(gltf, images, material, options) {
|
|||||||
|
|
||||||
var transparent = alpha < 1.0;
|
var transparent = alpha < 1.0;
|
||||||
if (defined(diffuseImage)) {
|
if (defined(diffuseImage)) {
|
||||||
transparent |= diffuseImage.transparent;
|
transparent = transparent || diffuseImage.transparent;
|
||||||
}
|
}
|
||||||
|
|
||||||
var doubleSided = transparent;
|
var doubleSided = transparent;
|
||||||
@ -528,7 +534,7 @@ function createMetallicRoughnessMaterial(gltf, images, material, options) {
|
|||||||
var baseColorTexture = getTexture(gltf, baseColorImage);
|
var baseColorTexture = getTexture(gltf, baseColorImage);
|
||||||
var metallicRoughnessTexture = createMetallicRoughnessTexture(gltf, materialName, metallicImage, roughnessImage, occlusionImage, options);
|
var metallicRoughnessTexture = createMetallicRoughnessTexture(gltf, materialName, metallicImage, roughnessImage, occlusionImage, options);
|
||||||
|
|
||||||
var packOcclusion = defined(occlusionImage) || options.packOcclusion;
|
var packOcclusion = defined(occlusionImage) && options.packOcclusion;
|
||||||
var occlusionTexture = packOcclusion ? metallicRoughnessTexture : getTexture(gltf, occlusionImage);
|
var occlusionTexture = packOcclusion ? metallicRoughnessTexture : getTexture(gltf, occlusionImage);
|
||||||
|
|
||||||
var emissiveFactor = getEmissiveFactor(material);
|
var emissiveFactor = getEmissiveFactor(material);
|
||||||
@ -557,7 +563,7 @@ function createMetallicRoughnessMaterial(gltf, images, material, options) {
|
|||||||
|
|
||||||
var transparent = alpha < 1.0;
|
var transparent = alpha < 1.0;
|
||||||
if (defined(baseColorImage)) {
|
if (defined(baseColorImage)) {
|
||||||
transparent |= baseColorImage.transparent;
|
transparent = transparent || baseColorImage.transparent;
|
||||||
}
|
}
|
||||||
|
|
||||||
var doubleSided = transparent;
|
var doubleSided = transparent;
|
||||||
@ -706,6 +712,7 @@ function getMaterial(gltf, materials, images, materialName, hasNormals, options)
|
|||||||
for (i = 0; i < materialsLength; ++i) {
|
for (i = 0; i < materialsLength; ++i) {
|
||||||
if (materials[i].name === materialName) {
|
if (materials[i].name === materialName) {
|
||||||
material = materials[i];
|
material = materials[i];
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,8 +6,6 @@ var path = require('path');
|
|||||||
var PNG = require('pngjs').PNG;
|
var PNG = require('pngjs').PNG;
|
||||||
|
|
||||||
var defaultValue = Cesium.defaultValue;
|
var defaultValue = Cesium.defaultValue;
|
||||||
var defined = Cesium.defined;
|
|
||||||
var WebGLConstants = Cesium.WebGLConstants;
|
|
||||||
|
|
||||||
module.exports = loadImage;
|
module.exports = loadImage;
|
||||||
|
|
||||||
|
@ -14,10 +14,17 @@ var boxObjUrl = 'specs/data/box/box.obj';
|
|||||||
var groupObjUrl = 'specs/data/box-objects-groups-materials/box-objects-groups-materials.obj';
|
var groupObjUrl = 'specs/data/box-objects-groups-materials/box-objects-groups-materials.obj';
|
||||||
var diffuseTextureUrl = 'specs/data/box-textured/cesium.png';
|
var diffuseTextureUrl = 'specs/data/box-textured/cesium.png';
|
||||||
var transparentDiffuseTextureUrl = 'specs/data/box-complex-material/diffuse.png';
|
var transparentDiffuseTextureUrl = 'specs/data/box-complex-material/diffuse.png';
|
||||||
|
var ambientTextureUrl = 'specs/data/box-complex-material/ambient.gif';
|
||||||
|
var normalTextureUrl = 'specs/data/box-complex-material/bump.png';
|
||||||
|
var emissiveTextureUrl = 'specs/data/box-complex-material/emission.jpg';
|
||||||
|
var metallicTextureUrl = 'specs/data/box-complex-material/specular.jpeg';
|
||||||
|
var roughnessTextureUrl = 'specs/data/box-complex-material/shininess.png';
|
||||||
|
|
||||||
var defaultOptions = obj2gltf.defaults;
|
var defaultOptions = obj2gltf.defaults;
|
||||||
var checkTransparencyOptions = clone(defaultOptions);
|
var checkTransparencyOptions = clone(defaultOptions);
|
||||||
checkTransparencyOptions.checkTransparency = true;
|
checkTransparencyOptions.checkTransparency = true;
|
||||||
|
var decodeOptions = clone(defaultOptions);
|
||||||
|
decodeOptions.decode = true;
|
||||||
|
|
||||||
function setDefaultMaterial(objData) {
|
function setDefaultMaterial(objData) {
|
||||||
var originalMaterial = objData.materials[0];
|
var originalMaterial = objData.materials[0];
|
||||||
@ -32,24 +39,50 @@ describe('createGltf', function() {
|
|||||||
var groupObjData;
|
var groupObjData;
|
||||||
var diffuseTexture;
|
var diffuseTexture;
|
||||||
var transparentDiffuseTexture;
|
var transparentDiffuseTexture;
|
||||||
|
var ambientTexture;
|
||||||
|
var normalTexture;
|
||||||
|
var emissiveTexture;
|
||||||
|
var metallicTexture;
|
||||||
|
var roughnessTexture;
|
||||||
|
|
||||||
beforeEach(function(done) {
|
beforeEach(function(done) {
|
||||||
|
spyOn(console, 'log');
|
||||||
return Promise.all([
|
return Promise.all([
|
||||||
loadObj(boxObjUrl, defaultOptions)
|
loadObj(boxObjUrl, decodeOptions)
|
||||||
.then(function(data) {
|
.then(function(data) {
|
||||||
boxObjData = data;
|
boxObjData = data;
|
||||||
}),
|
}),
|
||||||
loadObj(groupObjUrl, defaultOptions)
|
loadObj(groupObjUrl, decodeOptions)
|
||||||
.then(function(data) {
|
.then(function(data) {
|
||||||
groupObjData = data;
|
groupObjData = data;
|
||||||
}),
|
}),
|
||||||
loadImage(diffuseTextureUrl, defaultOptions)
|
loadImage(diffuseTextureUrl, decodeOptions)
|
||||||
.then(function(image) {
|
.then(function(image) {
|
||||||
diffuseTexture = image;
|
diffuseTexture = image;
|
||||||
}),
|
}),
|
||||||
loadImage(transparentDiffuseTextureUrl, checkTransparencyOptions)
|
loadImage(transparentDiffuseTextureUrl, checkTransparencyOptions)
|
||||||
.then(function(image) {
|
.then(function(image) {
|
||||||
transparentDiffuseTexture = image;
|
transparentDiffuseTexture = image;
|
||||||
|
}),
|
||||||
|
loadImage(ambientTextureUrl, decodeOptions)
|
||||||
|
.then(function(image) {
|
||||||
|
ambientTexture = image;
|
||||||
|
}),
|
||||||
|
loadImage(normalTextureUrl, decodeOptions)
|
||||||
|
.then(function(image) {
|
||||||
|
normalTexture = image;
|
||||||
|
}),
|
||||||
|
loadImage(emissiveTextureUrl, decodeOptions)
|
||||||
|
.then(function(image) {
|
||||||
|
emissiveTexture = image;
|
||||||
|
}),
|
||||||
|
loadImage(metallicTextureUrl, decodeOptions)
|
||||||
|
.then(function(image) {
|
||||||
|
metallicTexture = image;
|
||||||
|
}),
|
||||||
|
loadImage(roughnessTextureUrl, decodeOptions)
|
||||||
|
.then(function(image) {
|
||||||
|
roughnessTexture = image;
|
||||||
})
|
})
|
||||||
]).then(done);
|
]).then(done);
|
||||||
});
|
});
|
||||||
@ -93,46 +126,6 @@ describe('createGltf', function() {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
it('sets default material values', function() {
|
|
||||||
// Will convert traditional material to metallic-roughness
|
|
||||||
setDefaultMaterial(boxObjData);
|
|
||||||
|
|
||||||
var gltf = createGltf(boxObjData, defaultOptions);
|
|
||||||
var material = gltf.materials[0];
|
|
||||||
var pbr = material.pbrMetallicRoughness;
|
|
||||||
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).toBe(undefined);
|
|
||||||
expect(material.normalTexture).toBe(undefined);
|
|
||||||
expect(material.occlusionTexture).toBe(undefined);
|
|
||||||
expect(material.emissiveFactor).toEqual([0.0, 0.0, 0.0]);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('sets default material values for metallicRoughness', function() {
|
|
||||||
// No conversion applied when metallicRoughness flag is set
|
|
||||||
var options = clone(defaultOptions);
|
|
||||||
options.metallicRoughness = true;
|
|
||||||
|
|
||||||
var defaultMaterial = setDefaultMaterial(boxObjData);
|
|
||||||
defaultMaterial.specularShininess = 1.0; // This is the default set in loadMtl
|
|
||||||
|
|
||||||
var gltf = createGltf(boxObjData, options);
|
|
||||||
var material = gltf.materials[0];
|
|
||||||
var pbr = material.pbrMetallicRoughness;
|
|
||||||
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).toBe(undefined);
|
|
||||||
expect(material.normalTexture).toBe(undefined);
|
|
||||||
expect(material.occlusionTexture).toBe(undefined);
|
|
||||||
expect(material.emissiveFactor).toEqual([0.0, 0.0, 0.0]);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('runs without normals', function() {
|
it('runs without normals', function() {
|
||||||
boxObjData.nodes[0].meshes[0].normals.length = 0;
|
boxObjData.nodes[0].meshes[0].normals.length = 0;
|
||||||
|
|
||||||
@ -208,6 +201,201 @@ describe('createGltf', function() {
|
|||||||
expect(positionAccessor.count).toBe(vertexCount);
|
expect(positionAccessor.count).toBe(vertexCount);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('metallicRoughness', function() {
|
||||||
|
it('sets default material values', function() {
|
||||||
|
// Will convert traditional material to metallic-roughness
|
||||||
|
setDefaultMaterial(boxObjData);
|
||||||
|
|
||||||
|
var gltf = createGltf(boxObjData, defaultOptions);
|
||||||
|
var material = gltf.materials[0];
|
||||||
|
var pbr = material.pbrMetallicRoughness;
|
||||||
|
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).toBe(undefined);
|
||||||
|
expect(material.normalTexture).toBe(undefined);
|
||||||
|
expect(material.occlusionTexture).toBe(undefined);
|
||||||
|
expect(material.emissiveFactor).toEqual([0.0, 0.0, 0.0]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('sets default material values for metallicRoughness', function() {
|
||||||
|
// No conversion applied when metallicRoughness flag is set
|
||||||
|
var options = clone(defaultOptions);
|
||||||
|
options.metallicRoughness = true;
|
||||||
|
|
||||||
|
var defaultMaterial = setDefaultMaterial(boxObjData);
|
||||||
|
defaultMaterial.specularShininess = 1.0; // This is the default set in loadMtl
|
||||||
|
|
||||||
|
var gltf = createGltf(boxObjData, options);
|
||||||
|
var material = gltf.materials[0];
|
||||||
|
var pbr = material.pbrMetallicRoughness;
|
||||||
|
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).toBe(undefined);
|
||||||
|
expect(material.normalTexture).toBe(undefined);
|
||||||
|
expect(material.occlusionTexture).toBe(undefined);
|
||||||
|
expect(material.emissiveFactor).toEqual([0.0, 0.0, 0.0]);
|
||||||
|
expect(material.alphaMode).toBe('OPAQUE');
|
||||||
|
expect(material.doubleSided).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('complex material', function() {
|
||||||
|
var options = clone(defaultOptions);
|
||||||
|
options.metallicRoughness = true;
|
||||||
|
|
||||||
|
var defaultMaterial = setDefaultMaterial(boxObjData);
|
||||||
|
defaultMaterial.diffuseTexture = diffuseTextureUrl;
|
||||||
|
defaultMaterial.ambientTexture = ambientTextureUrl;
|
||||||
|
defaultMaterial.normalTexture = normalTextureUrl;
|
||||||
|
defaultMaterial.emissiveTexture = emissiveTextureUrl;
|
||||||
|
defaultMaterial.specularTexture = metallicTextureUrl;
|
||||||
|
defaultMaterial.specularShininessTexture = roughnessTextureUrl;
|
||||||
|
boxObjData.images.push(diffuseTexture, ambientTexture, normalTexture, emissiveTexture, metallicTexture, roughnessTexture);
|
||||||
|
|
||||||
|
var gltf = createGltf(boxObjData, options);
|
||||||
|
var material = gltf.materials[0];
|
||||||
|
var pbr = material.pbrMetallicRoughness;
|
||||||
|
var textureIndexes = [pbr.baseColorTexture.index, pbr.metallicRoughnessTexture.index, material.occlusionTexture.index, material.emissiveTexture.index, material.normalTexture.index].sort();
|
||||||
|
expect(textureIndexes).toEqual([0, 1, 2, 3, 4]);
|
||||||
|
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.emissiveFactor).toEqual([1.0, 1.0, 1.0]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('packs occlusion in metallic roughness texture', function() {
|
||||||
|
var options = clone(defaultOptions);
|
||||||
|
options.metallicRoughness = true;
|
||||||
|
options.packOcclusion = true;
|
||||||
|
|
||||||
|
var defaultMaterial = setDefaultMaterial(boxObjData);
|
||||||
|
defaultMaterial.ambientTexture = diffuseTextureUrl;
|
||||||
|
defaultMaterial.specularTexture = metallicTextureUrl;
|
||||||
|
defaultMaterial.specularShininessTexture = roughnessTextureUrl;
|
||||||
|
boxObjData.images.push(diffuseTexture, metallicTexture, roughnessTexture);
|
||||||
|
|
||||||
|
var gltf = createGltf(boxObjData, options);
|
||||||
|
var material = gltf.materials[0];
|
||||||
|
var pbr = material.pbrMetallicRoughness;
|
||||||
|
expect(pbr.metallicRoughnessTexture).toEqual({index : 0});
|
||||||
|
expect(material.occlusionTexture).toEqual({index : 0});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('does not create metallic roughness texture if decoded image data is not available', function() {
|
||||||
|
var options = clone(defaultOptions);
|
||||||
|
options.metallicRoughness = true;
|
||||||
|
options.packOcclusion = true;
|
||||||
|
|
||||||
|
var defaultMaterial = setDefaultMaterial(boxObjData);
|
||||||
|
defaultMaterial.ambientTexture = ambientTextureUrl; // is a .gif which can't be decoded
|
||||||
|
defaultMaterial.specularTexture = metallicTextureUrl;
|
||||||
|
defaultMaterial.specularShininessTexture = roughnessTextureUrl;
|
||||||
|
boxObjData.images.push(ambientTexture, metallicTexture, roughnessTexture);
|
||||||
|
|
||||||
|
var gltf = createGltf(boxObjData, options);
|
||||||
|
var material = gltf.materials[0];
|
||||||
|
var pbr = material.pbrMetallicRoughness;
|
||||||
|
expect(pbr.metallicRoughnessTexture).toBeUndefined();
|
||||||
|
expect(material.occlusionTexture).toBeUndefined();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('sets material for transparent diffuse texture', function() {
|
||||||
|
var options = clone(defaultOptions);
|
||||||
|
options.metallicRoughness = true;
|
||||||
|
|
||||||
|
var defaultMaterial = setDefaultMaterial(boxObjData);
|
||||||
|
defaultMaterial.diffuseTexture = transparentDiffuseTextureUrl;
|
||||||
|
boxObjData.images.push(transparentDiffuseTexture);
|
||||||
|
|
||||||
|
var gltf = createGltf(boxObjData, options);
|
||||||
|
var material = gltf.materials[0];
|
||||||
|
expect(material.alphaMode).toBe('BLEND');
|
||||||
|
expect(material.doubleSided).toBe(true);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('specularGlosiness', function() {
|
||||||
|
it('sets default material values for specularGlossiness', function() {
|
||||||
|
var options = clone(defaultOptions);
|
||||||
|
options.specularGlossiness = true;
|
||||||
|
|
||||||
|
setDefaultMaterial(boxObjData);
|
||||||
|
|
||||||
|
var gltf = createGltf(boxObjData, options);
|
||||||
|
var material = gltf.materials[0];
|
||||||
|
var pbr = material.extensions.KHR_materials_pbrSpecularGlossiness;
|
||||||
|
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).toBe(undefined);
|
||||||
|
expect(material.normalTexture).toBe(undefined);
|
||||||
|
expect(material.occlusionTexture).toBe(undefined);
|
||||||
|
expect(material.emissiveFactor).toEqual([0.0, 0.0, 0.0]);
|
||||||
|
expect(material.alphaMode).toBe('OPAQUE');
|
||||||
|
expect(material.doubleSided).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('complex material', function() {
|
||||||
|
var options = clone(defaultOptions);
|
||||||
|
options.specularGlossiness = true;
|
||||||
|
|
||||||
|
var defaultMaterial = setDefaultMaterial(boxObjData);
|
||||||
|
defaultMaterial.diffuseTexture = diffuseTextureUrl;
|
||||||
|
defaultMaterial.ambientTexture = ambientTextureUrl;
|
||||||
|
defaultMaterial.normalTexture = normalTextureUrl;
|
||||||
|
defaultMaterial.emissiveTexture = emissiveTextureUrl;
|
||||||
|
defaultMaterial.specularTexture = metallicTextureUrl;
|
||||||
|
defaultMaterial.specularShininessTexture = roughnessTextureUrl;
|
||||||
|
boxObjData.images.push(diffuseTexture, ambientTexture, normalTexture, emissiveTexture, metallicTexture, roughnessTexture);
|
||||||
|
|
||||||
|
var gltf = createGltf(boxObjData, options);
|
||||||
|
var material = gltf.materials[0];
|
||||||
|
var pbr = material.extensions.KHR_materials_pbrSpecularGlossiness;
|
||||||
|
var textureIndexes = [pbr.diffuseTexture.index, pbr.specularGlossinessTexture.index, material.occlusionTexture.index, material.emissiveTexture.index, material.normalTexture.index].sort();
|
||||||
|
expect(textureIndexes).toEqual([0, 1, 2, 3, 4]);
|
||||||
|
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.emissiveFactor).toEqual([1.0, 1.0, 1.0]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('does not create metallic roughness texture if decoded image data is not available', function() {
|
||||||
|
var options = clone(defaultOptions);
|
||||||
|
options.specularGlossiness = true;
|
||||||
|
|
||||||
|
var defaultMaterial = setDefaultMaterial(boxObjData);
|
||||||
|
defaultMaterial.specularTexture = ambientTextureUrl; // is a .gif which can't be decoded;
|
||||||
|
defaultMaterial.specularShininessTexture = roughnessTextureUrl;
|
||||||
|
boxObjData.images.push(ambientTexture, roughnessTexture);
|
||||||
|
|
||||||
|
var gltf = createGltf(boxObjData, options);
|
||||||
|
var material = gltf.materials[0];
|
||||||
|
var pbr = material.extensions.KHR_materials_pbrSpecularGlossiness;
|
||||||
|
expect(pbr.specularGlossinessTexture).toBeUndefined();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('sets material for transparent diffuse texture', function() {
|
||||||
|
var options = clone(defaultOptions);
|
||||||
|
options.specularGlossiness = true;
|
||||||
|
|
||||||
|
var defaultMaterial = setDefaultMaterial(boxObjData);
|
||||||
|
defaultMaterial.diffuseTexture = transparentDiffuseTextureUrl;
|
||||||
|
boxObjData.images.push(transparentDiffuseTexture);
|
||||||
|
|
||||||
|
var gltf = createGltf(boxObjData, options);
|
||||||
|
var material = gltf.materials[0];
|
||||||
|
expect(material.alphaMode).toBe('BLEND');
|
||||||
|
expect(material.doubleSided).toBe(true);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('materialsCommon', function() {
|
describe('materialsCommon', function() {
|
||||||
it('sets default material values for materialsCommon', function() {
|
it('sets default material values for materialsCommon', function() {
|
||||||
var options = clone(defaultOptions);
|
var options = clone(defaultOptions);
|
||||||
@ -237,26 +425,23 @@ describe('createGltf', function() {
|
|||||||
|
|
||||||
var defaultMaterial = setDefaultMaterial(boxObjData);
|
var defaultMaterial = setDefaultMaterial(boxObjData);
|
||||||
defaultMaterial.diffuseTexture = diffuseTextureUrl;
|
defaultMaterial.diffuseTexture = diffuseTextureUrl;
|
||||||
boxObjData.images[diffuseTextureUrl] = diffuseTexture;
|
boxObjData.images.push(diffuseTexture);
|
||||||
|
|
||||||
var gltf = createGltf(boxObjData, options);
|
var gltf = createGltf(boxObjData, options);
|
||||||
var kmc = gltf.materials[0].extensions.KHR_materials_common;
|
var kmc = gltf.materials[0].extensions.KHR_materials_common;
|
||||||
var texture = gltf.textures.texture_cesium;
|
var texture = gltf.textures[0];
|
||||||
var image = gltf.images.cesium;
|
var image = gltf.images[0];
|
||||||
|
|
||||||
expect(kmc.technique).toBe('LAMBERT');
|
expect(kmc.technique).toBe('LAMBERT');
|
||||||
expect(kmc.values.diffuse).toEqual('texture_cesium');
|
expect(kmc.values.diffuse).toEqual({index : 0});
|
||||||
expect(kmc.values.transparency).toBe(1.0);
|
expect(kmc.values.transparency).toBe(1.0);
|
||||||
expect(kmc.values.transparent).toBe(false);
|
expect(kmc.values.transparent).toBe(false);
|
||||||
expect(kmc.values.doubleSided).toBe(false);
|
expect(kmc.values.doubleSided).toBe(false);
|
||||||
|
|
||||||
expect(texture).toEqual({
|
expect(texture).toEqual({
|
||||||
format : WebGLConstants.RGB,
|
name : 'cesium',
|
||||||
internalFormat : WebGLConstants.RGB,
|
sampler : 0,
|
||||||
sampler : 'sampler',
|
source : 0
|
||||||
source : 'cesium',
|
|
||||||
target : WebGLConstants.TEXTURE_2D,
|
|
||||||
type : WebGLConstants.UNSIGNED_BYTE
|
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(image).toBeDefined();
|
expect(image).toBeDefined();
|
||||||
@ -264,7 +449,7 @@ describe('createGltf', function() {
|
|||||||
expect(image.extras._obj2gltf.source).toBeDefined();
|
expect(image.extras._obj2gltf.source).toBeDefined();
|
||||||
expect(image.extras._obj2gltf.extension).toBe('.png');
|
expect(image.extras._obj2gltf.extension).toBe('.png');
|
||||||
|
|
||||||
expect(gltf.samplers.sampler).toEqual({
|
expect(gltf.samplers[0]).toEqual({
|
||||||
magFilter : WebGLConstants.LINEAR,
|
magFilter : WebGLConstants.LINEAR,
|
||||||
minFilter : WebGLConstants.NEAREST_MIPMAP_LINEAR,
|
minFilter : WebGLConstants.NEAREST_MIPMAP_LINEAR,
|
||||||
wrapS : WebGLConstants.REPEAT,
|
wrapS : WebGLConstants.REPEAT,
|
||||||
@ -276,12 +461,11 @@ describe('createGltf', function() {
|
|||||||
var options = clone(defaultOptions);
|
var options = clone(defaultOptions);
|
||||||
options.materialsCommon = true;
|
options.materialsCommon = true;
|
||||||
|
|
||||||
var material = new Material();
|
var defaultMaterial = setDefaultMaterial(boxObjData);
|
||||||
material.alpha = 0.4;
|
defaultMaterial.alpha = 0.4;
|
||||||
boxObjData.materials[0] = material;
|
|
||||||
|
|
||||||
var gltf = createGltf(boxObjData, options);
|
var gltf = createGltf(boxObjData, options);
|
||||||
var kmc = gltf.materials.Material.extensions.KHR_materials_common;
|
var kmc = gltf.materials[0].extensions.KHR_materials_common;
|
||||||
|
|
||||||
expect(kmc.values.diffuse).toEqual([0.5, 0.5, 0.5, 0.4]);
|
expect(kmc.values.diffuse).toEqual([0.5, 0.5, 0.5, 0.4]);
|
||||||
expect(kmc.values.transparency).toBe(1.0);
|
expect(kmc.values.transparency).toBe(1.0);
|
||||||
@ -293,17 +477,15 @@ describe('createGltf', function() {
|
|||||||
var options = clone(defaultOptions);
|
var options = clone(defaultOptions);
|
||||||
options.materialsCommon = true;
|
options.materialsCommon = true;
|
||||||
|
|
||||||
var material = new Material();
|
var defaultMaterial = setDefaultMaterial(boxObjData);
|
||||||
material.diffuseTexture = diffuseTextureUrl;
|
defaultMaterial.diffuseTexture = diffuseTextureUrl;
|
||||||
material.alpha = 0.4;
|
defaultMaterial.alpha = 0.4;
|
||||||
boxObjData.materials[0] = material;
|
boxObjData.images.push(diffuseTexture);
|
||||||
|
|
||||||
boxObjData.images[diffuseTextureUrl] = diffuseTexture;
|
|
||||||
|
|
||||||
var gltf = createGltf(boxObjData, options);
|
var gltf = createGltf(boxObjData, options);
|
||||||
var kmc = gltf.materials.Material.extensions.KHR_materials_common;
|
var kmc = gltf.materials[0].extensions.KHR_materials_common;
|
||||||
|
|
||||||
expect(kmc.values.diffuse).toEqual('texture_cesium');
|
expect(kmc.values.diffuse).toEqual({index : 0});
|
||||||
expect(kmc.values.transparency).toBe(0.4);
|
expect(kmc.values.transparency).toBe(0.4);
|
||||||
expect(kmc.values.transparent).toBe(true);
|
expect(kmc.values.transparent).toBe(true);
|
||||||
expect(kmc.values.doubleSided).toBe(true);
|
expect(kmc.values.doubleSided).toBe(true);
|
||||||
@ -313,16 +495,14 @@ describe('createGltf', function() {
|
|||||||
var options = clone(defaultOptions);
|
var options = clone(defaultOptions);
|
||||||
options.materialsCommon = true;
|
options.materialsCommon = true;
|
||||||
|
|
||||||
var material = new Material();
|
var defaultMaterial = setDefaultMaterial(boxObjData);
|
||||||
material.diffuseTexture = transparentDiffuseTextureUrl;
|
defaultMaterial.diffuseTexture = transparentDiffuseTextureUrl;
|
||||||
boxObjData.materials[0] = material;
|
boxObjData.images.push(transparentDiffuseTexture);
|
||||||
|
|
||||||
boxObjData.images[transparentDiffuseTextureUrl] = transparentDiffuseTexture;
|
|
||||||
|
|
||||||
var gltf = createGltf(boxObjData, options);
|
var gltf = createGltf(boxObjData, options);
|
||||||
var kmc = gltf.materials.Material.extensions.KHR_materials_common;
|
var kmc = gltf.materials[0].extensions.KHR_materials_common;
|
||||||
|
|
||||||
expect(kmc.values.diffuse).toBe('texture_diffuse');
|
expect(kmc.values.diffuse).toEqual({index : 0});
|
||||||
expect(kmc.values.transparency).toBe(1.0);
|
expect(kmc.values.transparency).toBe(1.0);
|
||||||
expect(kmc.values.transparent).toBe(true);
|
expect(kmc.values.transparent).toBe(true);
|
||||||
expect(kmc.values.doubleSided).toBe(true);
|
expect(kmc.values.doubleSided).toBe(true);
|
||||||
@ -332,13 +512,12 @@ describe('createGltf', function() {
|
|||||||
var options = clone(defaultOptions);
|
var options = clone(defaultOptions);
|
||||||
options.materialsCommon = true;
|
options.materialsCommon = true;
|
||||||
|
|
||||||
var material = new Material();
|
var defaultMaterial = setDefaultMaterial(boxObjData);
|
||||||
material.specularColor = [0.1, 0.1, 0.2, 1];
|
defaultMaterial.specularColor = [0.1, 0.1, 0.2, 1];
|
||||||
material.specularShininess = 0.1;
|
defaultMaterial.specularShininess = 0.1;
|
||||||
boxObjData.materials[0] = material;
|
|
||||||
|
|
||||||
var gltf = createGltf(boxObjData, options);
|
var gltf = createGltf(boxObjData, options);
|
||||||
var kmc = gltf.materials.Material.extensions.KHR_materials_common;
|
var kmc = gltf.materials[0].extensions.KHR_materials_common;
|
||||||
|
|
||||||
expect(kmc.technique).toBe('PHONG');
|
expect(kmc.technique).toBe('PHONG');
|
||||||
expect(kmc.values.specular).toEqual([0.1, 0.1, 0.2, 1]);
|
expect(kmc.values.specular).toEqual([0.1, 0.1, 0.2, 1]);
|
||||||
@ -351,26 +530,23 @@ describe('createGltf', function() {
|
|||||||
|
|
||||||
boxObjData.nodes[0].meshes[0].normals.length = 0;
|
boxObjData.nodes[0].meshes[0].normals.length = 0;
|
||||||
|
|
||||||
var material = new Material();
|
var defaultMaterial = setDefaultMaterial(boxObjData);
|
||||||
material.diffuseTexture = diffuseTextureUrl;
|
defaultMaterial.diffuseTexture = diffuseTextureUrl;
|
||||||
boxObjData.materials[0] = material;
|
boxObjData.images.push(diffuseTexture);
|
||||||
|
|
||||||
boxObjData.images[diffuseTextureUrl] = diffuseTexture;
|
|
||||||
|
|
||||||
var gltf = createGltf(boxObjData, options);
|
var gltf = createGltf(boxObjData, options);
|
||||||
var kmc = gltf.materials.Material.extensions.KHR_materials_common;
|
var kmc = gltf.materials[0].extensions.KHR_materials_common;
|
||||||
|
|
||||||
expect(kmc.technique).toBe('CONSTANT');
|
expect(kmc.technique).toBe('CONSTANT');
|
||||||
expect(kmc.values.emission).toEqual('texture_cesium');
|
expect(kmc.values.emission).toEqual({index : 0});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('sets default material when texture is missing', function() {
|
it('sets default material when texture is missing', function() {
|
||||||
var options = clone(defaultOptions);
|
var options = clone(defaultOptions);
|
||||||
options.materialsCommon = true;
|
options.materialsCommon = true;
|
||||||
|
|
||||||
var material = new Material();
|
var defaultMaterial = setDefaultMaterial(boxObjData);
|
||||||
material.diffuseTexture = diffuseTextureUrl;
|
defaultMaterial.diffuseTexture = diffuseTextureUrl;
|
||||||
boxObjData.materials[0] = material;
|
|
||||||
|
|
||||||
var gltf = createGltf(boxObjData, options);
|
var gltf = createGltf(boxObjData, options);
|
||||||
var kmc = gltf.materials[0].extensions.KHR_materials_common;
|
var kmc = gltf.materials[0].extensions.KHR_materials_common;
|
||||||
@ -408,10 +584,10 @@ describe('createGltf', function() {
|
|||||||
var options = clone(defaultOptions);
|
var options = clone(defaultOptions);
|
||||||
options.materialsCommon = true;
|
options.materialsCommon = true;
|
||||||
|
|
||||||
boxObjData.materials.Material.ambientColor = [1.0, 1.0, 1.0, 1.0];
|
boxObjData.materials[0].ambientColor = [1.0, 1.0, 1.0, 1.0];
|
||||||
|
|
||||||
var gltf = createGltf(boxObjData, options);
|
var gltf = createGltf(boxObjData, options);
|
||||||
var ambient = gltf.materials.Material.extensions.KHR_materials_common.values.ambient;
|
var ambient = gltf.materials[0].extensions.KHR_materials_common.values.ambient;
|
||||||
|
|
||||||
expect(ambient).toEqual([0.0, 0.0, 0.0, 1.0]);
|
expect(ambient).toEqual([0.0, 0.0, 0.0, 1.0]);
|
||||||
});
|
});
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
var obj2gltf = require('../../lib/obj2gltf');
|
|
||||||
var loadImage = require('../../lib/loadImage');
|
var loadImage = require('../../lib/loadImage');
|
||||||
|
|
||||||
var pngImage = 'specs/data/box-complex-material/shininess.png';
|
var pngImage = 'specs/data/box-complex-material/shininess.png';
|
||||||
|
@ -313,7 +313,6 @@ describe('loadObj', function() {
|
|||||||
|
|
||||||
expect(loadObj(objExternalResourcesUrl, options)
|
expect(loadObj(objExternalResourcesUrl, options)
|
||||||
.then(function(data) {
|
.then(function(data) {
|
||||||
var imagePath = getImagePath(objMissingTextureUrl, 'cesium.png');
|
|
||||||
expect(data.images.length).toBe(0); // obj references an image file that is outside the input directory
|
expect(data.images.length).toBe(0); // obj references an image file that is outside the input directory
|
||||||
expect(data.materials.length).toBe(1); // obj references 2 materials, one of which is outside the input directory
|
expect(data.materials.length).toBe(1); // obj references 2 materials, one of which is outside the input directory
|
||||||
expect(console.log.calls.argsFor(0)[0].indexOf('Could not read mtl file') >= 0).toBe(true);
|
expect(console.log.calls.argsFor(0)[0].indexOf('Could not read mtl file') >= 0).toBe(true);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user