mirror of
https://github.com/CesiumGS/obj2gltf.git
synced 2024-11-23 08:34:14 -05:00
Merge pull request #152 from OmarShehata/unlit
Replace materialsCommon with unlit
This commit is contained in:
commit
fd2407512f
@ -1,6 +1,11 @@
|
||||
Change Log
|
||||
==========
|
||||
|
||||
### 3.0.0 ????-??-??
|
||||
|
||||
* Breaking changes
|
||||
* The `--materialsCommon` flag has been removed. Use `--unlit` instead which uses the `KHR_materials_unlit` extension. [#152](https://github.com/AnalyticalGraphicsInc/obj2gltf/pull/152)
|
||||
|
||||
### 2.3.2 2018-11-02
|
||||
|
||||
* Improved handling of primitives with different attributes using the same material. Materials are now duplicated. [#162](https://github.com/AnalyticalGraphicsInc/obj2gltf/pull/162)
|
||||
|
39
README.md
39
README.md
@ -56,14 +56,13 @@ There are three shading models supported by `obj2gltf`:
|
||||
|
||||
* Metallic roughness PBR
|
||||
* Specular glossiness PBR (via `KHR_materials_pbrSpecularGlossiness` extension)
|
||||
* Materials common (via `KHR_materials_common` extension)
|
||||
* Unlit materials (via `KHR_materials_unlit` extension)
|
||||
|
||||
If the material type is known in advance, it should be specified with either the `metallicRoughness`, `specularGlossiness`, or `materialsCommon` flag.
|
||||
If the material type is known in advance, it should be specified with either the `metallicRoughness` or `specularGlossiness` flag.
|
||||
|
||||
In general, if a model is authored with traditional diffuse, specular, and shininess textures the `materialsCommon` flag should be passed in.
|
||||
The glTF will be saved with the `KHR_materials_common` extension and the Blinn-Phong shading model will be used.
|
||||
If lighting information is already present in the model, the `unlit` flag should be used. This will save the glTF with the `KHR_materials_unlit` extension.
|
||||
|
||||
However if the model is created with PBR textures, either the `metallicRoughness` or `specularGlossiness` flag should be passed in.
|
||||
If the model is created with PBR textures, either the `metallicRoughness` or `specularGlossiness` flag should be passed in.
|
||||
See the table below for more information about how to specify PBR values inside the .mtl file.
|
||||
|
||||
If none of these flags are provided, the .mtl is assumed to contain traditional Blinn-Phong materials which will be converted to metallic-roughness PBR.
|
||||
@ -74,21 +73,21 @@ As a convenience the PBR textures may be supplied directly to the command line.
|
||||
|
||||
**Mapping of mtl slots to shading models**
|
||||
|
||||
Slot | Metallic roughness | Specular glossiness | Materials common
|
||||
Slot | Metallic roughness | Specular glossiness
|
||||
--- | --- | --- | ---
|
||||
Ka | occlusion value | occlusion value | ambient color
|
||||
Ke | emissive color | emissive color | emissive color
|
||||
Kd | base color | diffuse color | diffuse color
|
||||
Ks | metallic value | specular color | specular color
|
||||
Ns | roughness value | glossiness value | specular shininess value
|
||||
d | alpha | alpha | alpha
|
||||
Tr | 1.0 - alpha | 1.0 - alpha | 1.0 - alpha
|
||||
map_Ka | occlusion texture | occlusion texture | ambient texture
|
||||
map_Ke | emissive texture | emissive texture | emissive texture
|
||||
map_Kd | base color texture | diffuse texture | diffuse texture
|
||||
map_Ks | metallic texture | specular texture | specular texture
|
||||
map_Ns | roughness texture | glossiness texture | specular shininess texture
|
||||
map_Bump | normal texture | normal texture | normal texture
|
||||
Ka | occlusion value | occlusion value
|
||||
Ke | emissive color | emissive color
|
||||
Kd | base color | diffuse color
|
||||
Ks | metallic value | specular color
|
||||
Ns | roughness value | glossiness value
|
||||
d | alpha | alpha
|
||||
Tr | 1.0 - alpha | 1.0 - alpha
|
||||
map_Ka | occlusion texture | occlusion texture
|
||||
map_Ke | emissive texture | emissive texture
|
||||
map_Kd | base color texture | diffuse texture
|
||||
map_Ks | metallic texture | specular texture
|
||||
map_Ns | roughness texture | glossiness texture
|
||||
map_Bump | normal texture | normal texture
|
||||
|
||||
## Usage
|
||||
|
||||
@ -107,7 +106,7 @@ map_Bump | normal texture | normal texture | normal texture
|
||||
|`--packOcclusion`|Pack the occlusion texture in the red channel of metallic-roughness texture.|No, default `false`|
|
||||
|`--metallicRoughness`|The values in the mtl file are already metallic-roughness PBR values and no conversion step should be applied. Metallic is stored in the Ks and map_Ks slots and roughness is stored in the Ns and map_Ns slots.|No, default `false`|
|
||||
|`--specularGlossiness`|The values in the mtl file are already specular-glossiness PBR values and no conversion step should be applied. Specular is stored in the Ks and map_Ks slots and glossiness is stored in the Ns and map_Ns slots. The glTF will be saved with the `KHR_materials_pbrSpecularGlossiness` extension.|No, default `false`|
|
||||
|`--materialsCommon`|The glTF will be saved with the KHR_materials_common extension.|No, default `false`|
|
||||
|`--unlit`|The glTF will be saved with the KHR_materials_unlit extension.|No, default `false`|
|
||||
|`--metallicRoughnessOcclusionTexture`|Path to the metallic-roughness-occlusion texture that should override textures in the .mtl file, where occlusion is stored in the red channel, roughness is stored in the green channel, and metallic is stored in the blue channel. The model will be saved with a pbrMetallicRoughness material. This is often convenient in workflows where the .mtl does not exist or is not set up to use PBR materials. Intended for models with a single material.|No|
|
||||
|`--specularGlossinessTexture`|Path to the specular-glossiness texture that should override textures in the .mtl file, where specular color is stored in the red, green, and blue channels and specular glossiness is stored in the alpha channel. The model will be saved with a material using the KHR_materials_pbrSpecularGlossiness extension.|No|
|
||||
|`--occlusionTexture`|Path to the occlusion texture that should override textures in the .mtl file.|No|
|
||||
|
@ -85,11 +85,6 @@ var argv = yargs
|
||||
type : 'boolean',
|
||||
default : defaults.specularGlossiness
|
||||
},
|
||||
materialsCommon : {
|
||||
describe : 'The glTF will be saved with the KHR_materials_common extension.',
|
||||
type : 'boolean',
|
||||
default : defaults.materialsCommon
|
||||
},
|
||||
metallicRoughnessOcclusionTexture : {
|
||||
describe : 'Path to the metallic-roughness-occlusion texture that should override textures in the .mtl file, where occlusion is stored in the red channel, roughness is stored in the green channel, and metallic is stored in the blue channel. The model will be saved with a pbrMetallicRoughness material. This is often convenient in workflows where the .mtl does not exist or is not set up to use PBR materials. Intended for models with a single material',
|
||||
type : 'string',
|
||||
@ -122,11 +117,16 @@ var argv = yargs
|
||||
},
|
||||
alphaTexture : {
|
||||
describe : 'Path to the alpha texture that should override textures in the .mtl file.'
|
||||
},
|
||||
unlit : {
|
||||
describe : 'The glTF will be saved with the KHR_materials_unlit extension.',
|
||||
type : 'boolean',
|
||||
default : defaults.unlit
|
||||
}
|
||||
}).parse(args);
|
||||
|
||||
if (argv.metallicRoughness + argv.specularGlossiness + argv.materialsCommon > 1) {
|
||||
console.error('Only one material type may be set from [--metallicRoughness, --specularGlossiness, --materialsCommon].');
|
||||
if (argv.metallicRoughness + argv.specularGlossiness > 1) {
|
||||
console.error('Only one material type may be set from [--metallicRoughness, --specularGlossiness].');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
@ -165,7 +165,7 @@ var options = {
|
||||
packOcclusion : argv.packOcclusion,
|
||||
metallicRoughness : argv.metallicRoughness,
|
||||
specularGlossiness : argv.specularGlossiness,
|
||||
materialsCommon : argv.materialsCommon,
|
||||
unlit : argv.unlit,
|
||||
overridingTextures : overridingTextures,
|
||||
outputDirectory : outputDirectory
|
||||
};
|
||||
|
@ -74,14 +74,14 @@ function createGltf(objData, options) {
|
||||
var meshIndex;
|
||||
|
||||
if (meshesLength === 1) {
|
||||
meshIndex = addMesh(gltf, materials, bufferState, uint32Indices, meshes[0]);
|
||||
meshIndex = addMesh(gltf, materials, bufferState, uint32Indices, meshes[0], options);
|
||||
addNode(gltf, node.name, meshIndex, undefined);
|
||||
} else {
|
||||
// Add meshes as child nodes
|
||||
var parentIndex = addNode(gltf, node.name);
|
||||
for (var j = 0; j < meshesLength; ++j) {
|
||||
var mesh = meshes[j];
|
||||
meshIndex = addMesh(gltf, materials, bufferState, uint32Indices, mesh);
|
||||
meshIndex = addMesh(gltf, materials, bufferState, uint32Indices, mesh, options);
|
||||
addNode(gltf, mesh.name, meshIndex, parentIndex);
|
||||
}
|
||||
}
|
||||
@ -101,9 +101,11 @@ function createGltf(objData, options) {
|
||||
if (options.specularGlossiness) {
|
||||
gltf.extensionsUsed.push('KHR_materials_pbrSpecularGlossiness');
|
||||
gltf.extensionsRequired.push('KHR_materials_pbrSpecularGlossiness');
|
||||
} else if (options.materialsCommon) {
|
||||
gltf.extensionsUsed.push('KHR_materials_common');
|
||||
gltf.extensionsRequired.push('KHR_materials_common');
|
||||
}
|
||||
|
||||
if (options.unlit) {
|
||||
gltf.extensionsUsed.push('KHR_materials_unlit');
|
||||
gltf.extensionsRequired.push('KHR_materials_unlit');
|
||||
}
|
||||
|
||||
return gltf;
|
||||
@ -236,9 +238,15 @@ function resolveTextures(gltf, material) {
|
||||
}
|
||||
}
|
||||
|
||||
function addGltfMaterial(gltf, material) {
|
||||
function addGltfMaterial(gltf, material, options) {
|
||||
resolveTextures(gltf, material);
|
||||
var materialIndex = gltf.materials.length;
|
||||
if (options.unlit) {
|
||||
if (!defined(material.extensions)) {
|
||||
material.extensions = {};
|
||||
}
|
||||
material.extensions.KHR_materials_unlit = {};
|
||||
}
|
||||
gltf.materials.push(material);
|
||||
return materialIndex;
|
||||
}
|
||||
@ -261,12 +269,12 @@ function getMaterialIndex(materials, materialName) {
|
||||
}
|
||||
}
|
||||
|
||||
function getOrCreateGltfMaterial(gltf, materials, materialName) {
|
||||
function getOrCreateGltfMaterial(gltf, materials, materialName, options) {
|
||||
var material = getMaterialByName(materials, materialName);
|
||||
var materialIndex = getMaterialIndex(gltf.materials, materialName);
|
||||
|
||||
if (!defined(materialIndex)) {
|
||||
materialIndex = addGltfMaterial(gltf, material);
|
||||
materialIndex = addGltfMaterial(gltf, material, options);
|
||||
}
|
||||
|
||||
return materialIndex;
|
||||
@ -389,7 +397,7 @@ function requiresUint32Indices(nodes) {
|
||||
return false;
|
||||
}
|
||||
|
||||
function addPrimitive(gltf, materials, bufferState, uint32Indices, mesh, primitive, index) {
|
||||
function addPrimitive(gltf, materials, bufferState, uint32Indices, mesh, primitive, index, options) {
|
||||
var hasPositions = primitive.positions.length > 0;
|
||||
var hasNormals = primitive.normals.length > 0;
|
||||
var hasUVs = primitive.uvs.length > 0;
|
||||
@ -426,7 +434,7 @@ function addPrimitive(gltf, materials, bufferState, uint32Indices, mesh, primiti
|
||||
primitive.uvs = undefined;
|
||||
primitive.indices = undefined;
|
||||
|
||||
var materialIndex = getOrCreateGltfMaterial(gltf, materials, primitive.material);
|
||||
var materialIndex = getOrCreateGltfMaterial(gltf, materials, primitive.material, options);
|
||||
|
||||
return {
|
||||
attributes : attributes,
|
||||
@ -436,12 +444,12 @@ function addPrimitive(gltf, materials, bufferState, uint32Indices, mesh, primiti
|
||||
};
|
||||
}
|
||||
|
||||
function addMesh(gltf, materials, bufferState, uint32Indices, mesh) {
|
||||
function addMesh(gltf, materials, bufferState, uint32Indices, mesh, options) {
|
||||
var gltfPrimitives = [];
|
||||
var primitives = mesh.primitives;
|
||||
var primitivesLength = primitives.length;
|
||||
for (var i = 0; i < primitivesLength; ++i) {
|
||||
gltfPrimitives.push(addPrimitive(gltf, materials, bufferState, uint32Indices, mesh, primitives[i], i));
|
||||
gltfPrimitives.push(addPrimitive(gltf, materials, bufferState, uint32Indices, mesh, primitives[i], i, options));
|
||||
}
|
||||
|
||||
var gltfMesh = {
|
||||
|
@ -25,7 +25,6 @@ module.exports = loadMtl;
|
||||
*
|
||||
* @param {String} mtlPath Path to the .mtl file.
|
||||
* @param {Object} options The options object passed along from lib/obj2gltf.js
|
||||
* @param {Boolean} options.hasNormals Whether the model has normals.
|
||||
* @returns {Promise} A promise resolving to an array of glTF materials with Texture objects stored in the texture slots.
|
||||
*
|
||||
* @private
|
||||
@ -50,7 +49,7 @@ function loadMtl(mtlPath, options) {
|
||||
var overridingAlphaTexture = overridingTextures.alphaTexture;
|
||||
|
||||
// Textures that are packed into PBR textures need to be decoded first
|
||||
var decodeOptions = options.materialsCommon ? undefined : {
|
||||
var decodeOptions = {
|
||||
decode : true
|
||||
};
|
||||
|
||||
@ -276,10 +275,7 @@ function convertMaterial(material, options) {
|
||||
return createSpecularGlossinessMaterial(material, options);
|
||||
} else if (options.metallicRoughness) {
|
||||
return createMetallicRoughnessMaterial(material, options);
|
||||
} else if (options.materialsCommon) {
|
||||
return createMaterialsCommonMaterial(material, options);
|
||||
}
|
||||
|
||||
// No material type specified, convert the material to metallic roughness
|
||||
convertTraditionalToMetallicRoughness(material);
|
||||
return createMetallicRoughnessMaterial(material, options);
|
||||
@ -737,64 +733,3 @@ function convertTraditionalToMetallicRoughness(material) {
|
||||
material.specularColor = [metallicFactor, metallicFactor, metallicFactor, 1.0];
|
||||
material.specularShininess = roughnessFactor;
|
||||
}
|
||||
|
||||
function createMaterialsCommonMaterial(material, options) {
|
||||
var diffuseAlphaTexture = createDiffuseAlphaTexture(material.diffuseTexture, material.alphaTexture, options);
|
||||
var ambient = defaultValue(material.ambientTexture, material.ambientColor);
|
||||
var diffuse = defaultValue(diffuseAlphaTexture, material.diffuseColor);
|
||||
var emission = defaultValue(material.emissiveTexture, material.emissiveColor);
|
||||
var specular = defaultValue(material.specularTexture, material.specularColor);
|
||||
|
||||
var alpha = material.alpha;
|
||||
var shininess = material.specularShininess;
|
||||
var hasSpecular = (shininess > 0.0) && (defined(material.specularTexture) || (specular[0] > 0.0 || specular[1] > 0.0 || specular[2] > 0.0));
|
||||
|
||||
var transparent;
|
||||
var transparency = 1.0;
|
||||
if (defined(material.alphaTexture)) {
|
||||
transparent = true;
|
||||
} else if (defined(material.diffuseTexture)) {
|
||||
transparency = alpha;
|
||||
transparent = material.diffuseTexture.transparent || (transparency < 1.0);
|
||||
} else {
|
||||
diffuse[3] = alpha;
|
||||
transparent = alpha < 1.0;
|
||||
}
|
||||
|
||||
if (!defined(material.ambientTexture)) {
|
||||
// If ambient color is [1, 1, 1] assume it is a multiplier and instead change to [0, 0, 0]
|
||||
if (ambient[0] === 1.0 && ambient[1] === 1.0 && ambient[2] === 1.0) {
|
||||
ambient = [0.0, 0.0, 0.0, 1.0];
|
||||
}
|
||||
}
|
||||
|
||||
var doubleSided = transparent;
|
||||
var technique = hasSpecular ? 'PHONG' : 'LAMBERT';
|
||||
|
||||
if (!options.hasNormals) {
|
||||
// Constant technique only factors in ambient and emission sources - set emission to diffuse
|
||||
emission = diffuse;
|
||||
technique = 'CONSTANT';
|
||||
}
|
||||
|
||||
return {
|
||||
name : material.name,
|
||||
extensions : {
|
||||
KHR_materials_common : {
|
||||
technique : technique,
|
||||
transparent : transparent,
|
||||
doubleSided : doubleSided,
|
||||
values : {
|
||||
ambient : ambient,
|
||||
diffuse : diffuse,
|
||||
emission : emission,
|
||||
specular : specular,
|
||||
shininess : shininess,
|
||||
transparency : transparency,
|
||||
transparent : transparent,
|
||||
doubleSided : doubleSided
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
@ -25,7 +25,7 @@ module.exports = obj2gltf;
|
||||
* @param {Boolean} [options.packOcclusion=false] Pack the occlusion texture in the red channel of the metallic-roughness texture.
|
||||
* @param {Boolean} [options.metallicRoughness=false] The values in the mtl file are already metallic-roughness PBR values and no conversion step should be applied. Metallic is stored in the Ks and map_Ks slots and roughness is stored in the Ns and map_Ns slots.
|
||||
* @param {Boolean} [options.specularGlossiness=false] The values in the mtl file are already specular-glossiness PBR values and no conversion step should be applied. Specular is stored in the Ks and map_Ks slots and glossiness is stored in the Ns and map_Ns slots. The glTF will be saved with the KHR_materials_pbrSpecularGlossiness extension.
|
||||
* @param {Boolean} [options.materialsCommon=false] The glTF will be saved with the KHR_materials_common extension.
|
||||
* @param {Boolean} [options.unlit=false] The glTF will be saved with the KHR_materials_unlit extension.
|
||||
* @param {Object} [options.overridingTextures] An object containing texture paths that override textures defined in the .mtl file. This is often convenient in workflows where the .mtl does not exist or is not set up to use PBR materials. Intended for models with a single material.
|
||||
* @param {String} [options.overridingTextures.metallicRoughnessOcclusionTexture] Path to the metallic-roughness-occlusion texture, where occlusion is stored in the red channel, roughness is stored in the green channel, and metallic is stored in the blue channel. The model will be saved with a pbrMetallicRoughness material.
|
||||
* @param {String} [options.overridingTextures.specularGlossinessTexture] Path to the specular-glossiness texture, where specular color is stored in the red, green, and blue channels and specular glossiness is stored in the alpha channel. The model will be saved with a material using the KHR_materials_pbrSpecularGlossiness extension.
|
||||
@ -50,7 +50,7 @@ function obj2gltf(objPath, options) {
|
||||
options.packOcclusion = defaultValue(options.packOcclusion, defaults.packOcclusion);
|
||||
options.metallicRoughness = defaultValue(options.metallicRoughness, defaults.metallicRoughness);
|
||||
options.specularGlossiness = defaultValue(options.specularGlossiness, defaults.specularGlossiness);
|
||||
options.materialsCommon = defaultValue(options.materialsCommon, defaults.materialsCommon);
|
||||
options.unlit = defaultValue(options.unlit, defaults.unlit);
|
||||
options.overridingTextures = defaultValue(options.overridingTextures, defaultValue.EMPTY_OBJECT);
|
||||
options.logger = defaultValue(options.logger, getDefaultLogger());
|
||||
options.writer = defaultValue(options.writer, getDefaultWriter(options.outputDirectory));
|
||||
@ -63,8 +63,8 @@ function obj2gltf(objPath, options) {
|
||||
throw new DeveloperError('Either options.writer or options.outputDirectory must be defined when writing separate resources.');
|
||||
}
|
||||
|
||||
if (options.metallicRoughness + options.specularGlossiness + options.materialsCommon > 1) {
|
||||
throw new DeveloperError('Only one material type may be set from [metallicRoughness, specularGlossiness, materialsCommon].');
|
||||
if (options.metallicRoughness + options.specularGlossiness + options.unlit > 1) {
|
||||
throw new DeveloperError('Only one material type may be set from [metallicRoughness, specularGlossiness, unlit].');
|
||||
}
|
||||
|
||||
if (defined(options.overridingTextures.metallicRoughnessOcclusionTexture) && defined(options.overridingTextures.specularGlossinessTexture)) {
|
||||
@ -74,14 +74,12 @@ function obj2gltf(objPath, options) {
|
||||
if (defined(options.overridingTextures.metallicRoughnessOcclusionTexture)) {
|
||||
options.metallicRoughness = true;
|
||||
options.specularGlossiness = false;
|
||||
options.materialsCommon = false;
|
||||
options.packOcclusion = true;
|
||||
}
|
||||
|
||||
if (defined(options.overridingTextures.specularGlossinessTexture)) {
|
||||
options.metallicRoughness = false;
|
||||
options.specularGlossiness = true;
|
||||
options.materialsCommon = false;
|
||||
}
|
||||
|
||||
return loadObj(objPath, options)
|
||||
@ -162,11 +160,11 @@ obj2gltf.defaults = {
|
||||
*/
|
||||
specularGlossiness : false,
|
||||
/**
|
||||
* Gets or sets whether the glTF will be saved with the KHR_materials_common extension.
|
||||
* Gets or sets whether the glTF will be saved with the KHR_materials_unlit extension.
|
||||
* @type Boolean
|
||||
* @default false
|
||||
*/
|
||||
materialsCommon : false
|
||||
unlit : false
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -133,11 +133,11 @@ describe('createGltf', function() {
|
||||
expect(gltf.extensionsRequired).toEqual(['KHR_materials_pbrSpecularGlossiness']);
|
||||
});
|
||||
|
||||
it('adds KHR_materials_common extension when materialsCommon is set', function() {
|
||||
options.materialsCommon = true;
|
||||
it('adds KHR_materials_unlit extension when unlit is set', function() {
|
||||
options.unlit = true;
|
||||
var gltf = createGltf(noMaterialsObjData, options);
|
||||
expect(gltf.extensionsUsed).toEqual(['KHR_materials_common']);
|
||||
expect(gltf.extensionsRequired).toEqual(['KHR_materials_common']);
|
||||
expect(gltf.extensionsUsed).toEqual(['KHR_materials_unlit']);
|
||||
expect(gltf.extensionsRequired).toEqual(['KHR_materials_unlit']);
|
||||
});
|
||||
|
||||
it('runs without normals', function() {
|
||||
|
@ -5,7 +5,6 @@ var fsExtra = require('fs-extra');
|
||||
var loadMtl = require('../../lib/loadMtl');
|
||||
var loadTexture = require('../../lib/loadTexture');
|
||||
var obj2gltf = require('../../lib/obj2gltf');
|
||||
var Texture = require('../../lib/Texture');
|
||||
|
||||
var clone = Cesium.clone;
|
||||
|
||||
@ -468,164 +467,4 @@ describe('loadMtl', function() {
|
||||
expect(material.doubleSided).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('materialsCommon', function() {
|
||||
it('creates default material', function() {
|
||||
options.materialsCommon = true;
|
||||
var material = loadMtl._createMaterial(undefined, options);
|
||||
var extension = material.extensions.KHR_materials_common;
|
||||
var values = extension.values;
|
||||
expect(extension.technique).toBe('LAMBERT');
|
||||
expect(extension.transparent).toBe(false);
|
||||
expect(extension.doubleSided).toBe(false);
|
||||
expect(values.ambient).toEqual([0.0, 0.0, 0.0, 1.0]);
|
||||
expect(values.diffuse).toEqual([0.5, 0.5, 0.5, 1.0]);
|
||||
expect(values.emission).toEqual([0.0, 0.0, 0.0, 1.0]);
|
||||
expect(values.specular).toEqual([0.0, 0.0, 0.0, 1.0]);
|
||||
expect(values.shininess).toEqual(0);
|
||||
expect(values.transparency).toBe(1.0);
|
||||
expect(values.transparent).toBe(false);
|
||||
expect(values.doubleSided).toBe(false);
|
||||
});
|
||||
|
||||
it('creates material with textures', function() {
|
||||
options.materialsCommon = true;
|
||||
|
||||
var material = loadMtl._createMaterial({
|
||||
diffuseTexture : diffuseTexture,
|
||||
ambientTexture : ambientTexture,
|
||||
normalTexture : normalTexture,
|
||||
emissiveTexture : emissiveTexture,
|
||||
specularTexture : specularTexture,
|
||||
specularShininessTexture : specularShininessTexture
|
||||
}, options);
|
||||
|
||||
var extension = material.extensions.KHR_materials_common;
|
||||
var values = extension.values;
|
||||
expect(extension.technique).toBe('LAMBERT');
|
||||
expect(extension.transparent).toBe(false);
|
||||
expect(extension.doubleSided).toBe(false);
|
||||
expect(values.ambient instanceof Texture).toBe(true);
|
||||
expect(values.diffuse instanceof Texture).toBe(true);
|
||||
expect(values.emission instanceof Texture).toBe(true);
|
||||
expect(values.specular instanceof Texture).toBe(true);
|
||||
expect(values.shininess).toEqual(0);
|
||||
expect(values.transparency).toBe(1.0);
|
||||
expect(values.transparent).toBe(false);
|
||||
expect(values.doubleSided).toBe(false);
|
||||
});
|
||||
|
||||
it('sets material for alpha less than 1', function() {
|
||||
options.materialsCommon = true;
|
||||
|
||||
var material = loadMtl._createMaterial({
|
||||
alpha : 0.4
|
||||
}, options);
|
||||
|
||||
var values = material.extensions.KHR_materials_common.values;
|
||||
expect(values.diffuse).toEqual([0.5, 0.5, 0.5, 0.4]);
|
||||
expect(values.transparency).toBe(1.0);
|
||||
expect(values.transparent).toBe(true);
|
||||
expect(values.doubleSided).toBe(true);
|
||||
});
|
||||
|
||||
it('sets material for diffuse texture and alpha less than 1', function() {
|
||||
options.materialsCommon = true;
|
||||
|
||||
var material = loadMtl._createMaterial({
|
||||
diffuseTexture : diffuseTexture,
|
||||
alpha : 0.4
|
||||
}, options);
|
||||
|
||||
var values = material.extensions.KHR_materials_common.values;
|
||||
|
||||
expect(values.diffuse instanceof Texture).toBe(true);
|
||||
expect(values.transparency).toBe(0.4);
|
||||
expect(values.transparent).toBe(true);
|
||||
expect(values.doubleSided).toBe(true);
|
||||
});
|
||||
|
||||
it('sets material for transparent diffuse texture', function() {
|
||||
options.materialsCommon = true;
|
||||
|
||||
var material = loadMtl._createMaterial({
|
||||
diffuseTexture : transparentDiffuseTexture
|
||||
}, options);
|
||||
|
||||
var values = material.extensions.KHR_materials_common.values;
|
||||
|
||||
expect(values.diffuse instanceof Texture).toBe(true);
|
||||
expect(values.transparency).toBe(1.0);
|
||||
expect(values.transparent).toBe(true);
|
||||
expect(values.doubleSided).toBe(true);
|
||||
});
|
||||
|
||||
it('sets material for specular', function() {
|
||||
options.materialsCommon = true;
|
||||
|
||||
var material = loadMtl._createMaterial({
|
||||
specularColor : [0.1, 0.1, 0.2, 1],
|
||||
specularShininess : 0.1
|
||||
}, options);
|
||||
|
||||
var extension = material.extensions.KHR_materials_common;
|
||||
var values = extension.values;
|
||||
|
||||
expect(extension.technique).toBe('PHONG');
|
||||
expect(values.specular).toEqual([0.1, 0.1, 0.2, 1]);
|
||||
expect(values.shininess).toEqual(0.1);
|
||||
});
|
||||
|
||||
it('sets CONSTANT technique when there are no normals', function() {
|
||||
options.materialsCommon = true;
|
||||
options.hasNormals = false;
|
||||
|
||||
var material = loadMtl._createMaterial({
|
||||
diffuseColor : [1.0, 1.0, 1.0, 1.0]
|
||||
}, options);
|
||||
|
||||
var extension = material.extensions.KHR_materials_common;
|
||||
var values = extension.values;
|
||||
|
||||
expect(extension.technique).toBe('CONSTANT');
|
||||
expect(values.emission).toEqual(values.diffuse);
|
||||
});
|
||||
|
||||
it('ambient of [1, 1, 1] is treated as [0, 0, 0]', function() {
|
||||
options.materialsCommon = true;
|
||||
|
||||
var material = loadMtl._createMaterial({
|
||||
ambientColor : [1.0, 1.0, 1.0, 1.0]
|
||||
}, options);
|
||||
|
||||
var values = material.extensions.KHR_materials_common.values;
|
||||
expect(values.ambient).toEqual([0.0, 0.0, 0.0, 1.0]);
|
||||
});
|
||||
|
||||
it('packs alpha texture in diffuse texture', function() {
|
||||
options.materialsCommon = true;
|
||||
|
||||
var material = loadMtl._createMaterial({
|
||||
diffuseTexture : diffuseTexture,
|
||||
alphaTexture : alphaTexture
|
||||
}, options);
|
||||
|
||||
var values = material.extensions.KHR_materials_common.values;
|
||||
|
||||
var hasBlack = false;
|
||||
var hasWhite = false;
|
||||
var pixels = values.diffuse.pixels;
|
||||
var pixelsLength = pixels.length / 4;
|
||||
for (var i = 0; i < pixelsLength; ++i) {
|
||||
var alpha = pixels[i * 4 + 3];
|
||||
hasBlack = hasBlack || (alpha === 0);
|
||||
hasWhite = hasWhite || (alpha === 255);
|
||||
}
|
||||
expect(hasBlack).toBe(true);
|
||||
expect(hasWhite).toBe(true);
|
||||
expect(values.transparency).toBe(1.0);
|
||||
expect(values.transparent).toBe(true);
|
||||
expect(values.doubleSided).toBe(true);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user