Merge pull request #89 from AnalyticalGraphicsInc/keep-diffuse

Keep diffuse intact so that shader generation for generateNormals works
This commit is contained in:
Matthew Amato 2017-06-23 10:34:53 -04:00 committed by GitHub
commit 813290af6c
4 changed files with 26 additions and 24 deletions

View File

@ -4,6 +4,7 @@ Change Log
### Next release ### Next release
* Change texture sampling to use NEAREST_MIPMAP_LINEAR by default [#83](https://github.com/AnalyticalGraphicsInc/obj2gltf/pull/83). * Change texture sampling to use NEAREST_MIPMAP_LINEAR by default [#83](https://github.com/AnalyticalGraphicsInc/obj2gltf/pull/83).
* Fixed normal generation.
### 1.1.1 2017-04-25 ### 1.1.1 2017-04-25

View File

@ -17,7 +17,7 @@ module.exports = createGltf;
* *
* @private * @private
*/ */
function createGltf(objData) { function createGltf(objData, options) {
var nodes = objData.nodes; var nodes = objData.nodes;
var materials = objData.materials; var materials = objData.materials;
var images = objData.images; var images = objData.images;
@ -67,7 +67,7 @@ function createGltf(objData) {
return 'texture_' + getImageId(imagePath); return 'texture_' + getImageId(imagePath);
} }
function createMaterial(material, hasNormals) { function createMaterial(material, hasNormals, options) {
var ambient = defaultValue(defaultValue(getTextureId(material.ambientTexture), material.ambientColor)); var ambient = defaultValue(defaultValue(getTextureId(material.ambientTexture), material.ambientColor));
var diffuse = defaultValue(defaultValue(getTextureId(material.diffuseTexture), material.diffuseColor)); var diffuse = defaultValue(defaultValue(getTextureId(material.diffuseTexture), material.diffuseColor));
var emission = defaultValue(defaultValue(getTextureId(material.emissionTexture), material.emissionColor)); var emission = defaultValue(defaultValue(getTextureId(material.emissionTexture), material.emissionColor));
@ -95,7 +95,7 @@ function createGltf(objData) {
var doubleSided = transparent; var doubleSided = transparent;
if (!hasNormals) { if (!hasNormals && !options.generateNormals) {
// Constant technique only factors in ambient and emission sources - set emission to diffuse // Constant technique only factors in ambient and emission sources - set emission to diffuse
emission = diffuse; emission = diffuse;
diffuse = [0, 0, 0, 1]; diffuse = [0, 0, 0, 1];
@ -303,7 +303,7 @@ function createGltf(objData) {
} }
if (!defined(gltfMaterial)) { if (!defined(gltfMaterial)) {
gltf.materials[materialId] = createMaterial(material, hasNormals); gltf.materials[materialId] = createMaterial(material, hasNormals, options);
} }
gltfMeshPrimitives.push({ gltfMeshPrimitives.push({

View File

@ -61,6 +61,7 @@ function obj2gltf(objPath, gltfPath, options) {
var outputUpAxis = defaultValue(options.outputUpAxis, defaults.outputUpAxis); var outputUpAxis = defaultValue(options.outputUpAxis, defaults.outputUpAxis);
var logger = defaultValue(options.logger, defaults.logger); var logger = defaultValue(options.logger, defaults.logger);
options.generateNormals = generateNormals;
options.separate = separate; options.separate = separate;
options.separateTextures = separateTextures; options.separateTextures = separateTextures;
options.checkTransparency = checkTransparency; options.checkTransparency = checkTransparency;
@ -112,7 +113,7 @@ function obj2gltf(objPath, gltfPath, options) {
return loadObj(objPath, options) return loadObj(objPath, options)
.then(function(objData) { .then(function(objData) {
return createGltf(objData); return createGltf(objData, options);
}) })
.then(function(gltf) { .then(function(gltf) {
return writeUris(gltf, gltfPath, resourcesDirectory, options); return writeUris(gltf, gltfPath, resourcesDirectory, options);

View File

@ -67,7 +67,7 @@ describe('createGltf', function() {
}); });
it('simple gltf', function(done) { it('simple gltf', function(done) {
var gltf = createGltf(boxObjData); var gltf = createGltf(boxObjData, defaultOptions);
expect(writeUris(gltf, boxGltfUrl, path.dirname(boxGltfUrl), defaultOptions) expect(writeUris(gltf, boxGltfUrl, path.dirname(boxGltfUrl), defaultOptions)
.then(function() { .then(function() {
expect(gltf).toEqual(boxGltf); expect(gltf).toEqual(boxGltf);
@ -75,7 +75,7 @@ describe('createGltf', function() {
}); });
it('multiple nodes, meshes, and primitives', function(done) { it('multiple nodes, meshes, and primitives', function(done) {
var gltf = createGltf(groupObjData); var gltf = createGltf(groupObjData, defaultOptions);
expect(writeUris(gltf, groupGltfUrl, path.dirname(groupGltfUrl), defaultOptions) expect(writeUris(gltf, groupGltfUrl, path.dirname(groupGltfUrl), defaultOptions)
.then(function() { .then(function() {
@ -97,7 +97,7 @@ describe('createGltf', function() {
it('sets default material values', function() { it('sets default material values', function() {
boxObjData.materials.Material = new Material(); boxObjData.materials.Material = new Material();
var gltf = createGltf(boxObjData); var gltf = createGltf(boxObjData, defaultOptions);
var material = gltf.materials.Material; var material = gltf.materials.Material;
var kmc = material.extensions.KHR_materials_common; var kmc = material.extensions.KHR_materials_common;
var values = kmc.values; var values = kmc.values;
@ -116,7 +116,7 @@ describe('createGltf', function() {
boxObjData.materials.Material = material; boxObjData.materials.Material = material;
boxObjData.images[diffuseTextureUrl] = diffuseTexture; boxObjData.images[diffuseTextureUrl] = diffuseTexture;
var gltf = createGltf(boxObjData); var gltf = createGltf(boxObjData, defaultOptions);
var kmc = gltf.materials.Material.extensions.KHR_materials_common; var kmc = gltf.materials.Material.extensions.KHR_materials_common;
var texture = gltf.textures.texture_cesium; var texture = gltf.textures.texture_cesium;
var image = gltf.images.cesium; var image = gltf.images.cesium;
@ -154,7 +154,7 @@ describe('createGltf', function() {
material.alpha = 0.4; material.alpha = 0.4;
boxObjData.materials.Material = material; boxObjData.materials.Material = material;
var gltf = createGltf(boxObjData); var gltf = createGltf(boxObjData, defaultOptions);
var kmc = gltf.materials.Material.extensions.KHR_materials_common; var kmc = gltf.materials.Material.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]);
@ -171,7 +171,7 @@ describe('createGltf', function() {
boxObjData.images[diffuseTextureUrl] = diffuseTexture; boxObjData.images[diffuseTextureUrl] = diffuseTexture;
var gltf = createGltf(boxObjData); var gltf = createGltf(boxObjData, defaultOptions);
var kmc = gltf.materials.Material.extensions.KHR_materials_common; var kmc = gltf.materials.Material.extensions.KHR_materials_common;
expect(kmc.values.diffuse).toEqual('texture_cesium'); expect(kmc.values.diffuse).toEqual('texture_cesium');
@ -187,7 +187,7 @@ describe('createGltf', function() {
boxObjData.images[transparentDiffuseTextureUrl] = transparentDiffuseTexture; boxObjData.images[transparentDiffuseTextureUrl] = transparentDiffuseTexture;
var gltf = createGltf(boxObjData); var gltf = createGltf(boxObjData, defaultOptions);
var kmc = gltf.materials.Material.extensions.KHR_materials_common; var kmc = gltf.materials.Material.extensions.KHR_materials_common;
expect(kmc.values.diffuse).toBe('texture_diffuse'); expect(kmc.values.diffuse).toBe('texture_diffuse');
@ -202,7 +202,7 @@ describe('createGltf', function() {
material.specularShininess = 0.1; material.specularShininess = 0.1;
boxObjData.materials.Material = material; boxObjData.materials.Material = material;
var gltf = createGltf(boxObjData); var gltf = createGltf(boxObjData, defaultOptions);
var kmc = gltf.materials.Material.extensions.KHR_materials_common; var kmc = gltf.materials.Material.extensions.KHR_materials_common;
expect(kmc.technique).toBe('PHONG'); expect(kmc.technique).toBe('PHONG');
@ -219,7 +219,7 @@ describe('createGltf', function() {
boxObjData.images[diffuseTextureUrl] = diffuseTexture; boxObjData.images[diffuseTextureUrl] = diffuseTexture;
var gltf = createGltf(boxObjData); var gltf = createGltf(boxObjData, defaultOptions);
var kmc = gltf.materials.Material.extensions.KHR_materials_common; var kmc = gltf.materials.Material.extensions.KHR_materials_common;
expect(kmc.technique).toBe('CONSTANT'); expect(kmc.technique).toBe('CONSTANT');
@ -231,7 +231,7 @@ describe('createGltf', function() {
material.diffuseTexture = diffuseTextureUrl; material.diffuseTexture = diffuseTextureUrl;
boxObjData.materials.Material = material; boxObjData.materials.Material = material;
var gltf = createGltf(boxObjData); var gltf = createGltf(boxObjData, defaultOptions);
var kmc = gltf.materials.Material.extensions.KHR_materials_common; var kmc = gltf.materials.Material.extensions.KHR_materials_common;
expect(kmc.values.diffuse).toEqual([0.5, 0.5, 0.5, 1.0]); expect(kmc.values.diffuse).toEqual([0.5, 0.5, 0.5, 1.0]);
@ -241,7 +241,7 @@ describe('createGltf', function() {
boxObjData.nodes[0].meshes[0].primitives[0].material = undefined; boxObjData.nodes[0].meshes[0].primitives[0].material = undefined;
// Creates a material called "default" // Creates a material called "default"
var gltf = createGltf(boxObjData); var gltf = createGltf(boxObjData, defaultOptions);
expect(gltf.materials.default).toBeDefined(); expect(gltf.materials.default).toBeDefined();
var kmc = gltf.materials.default.extensions.KHR_materials_common; var kmc = gltf.materials.default.extensions.KHR_materials_common;
expect(kmc.values.diffuse).toEqual([0.5, 0.5, 0.5, 1.0]); expect(kmc.values.diffuse).toEqual([0.5, 0.5, 0.5, 1.0]);
@ -251,7 +251,7 @@ describe('createGltf', function() {
boxObjData.materials = {}; boxObjData.materials = {};
// Uses the original name of the material // Uses the original name of the material
var gltf = createGltf(boxObjData); var gltf = createGltf(boxObjData, defaultOptions);
var kmc = gltf.materials.Material.extensions.KHR_materials_common; var kmc = gltf.materials.Material.extensions.KHR_materials_common;
expect(kmc.values.diffuse).toEqual([0.5, 0.5, 0.5, 1.0]); expect(kmc.values.diffuse).toEqual([0.5, 0.5, 0.5, 1.0]);
@ -262,7 +262,7 @@ describe('createGltf', function() {
boxObjData.nodes.push(duplicateBoxObjData.nodes[0]); boxObjData.nodes.push(duplicateBoxObjData.nodes[0]);
boxObjData.nodes[1].meshes[0].normals.length = 0; boxObjData.nodes[1].meshes[0].normals.length = 0;
var gltf = createGltf(boxObjData); var gltf = createGltf(boxObjData, defaultOptions);
var kmc1 = gltf.materials.Material.extensions.KHR_materials_common; var kmc1 = gltf.materials.Material.extensions.KHR_materials_common;
var kmc2 = gltf.materials.Material_constant.extensions.KHR_materials_common; var kmc2 = gltf.materials.Material_constant.extensions.KHR_materials_common;
@ -275,7 +275,7 @@ describe('createGltf', function() {
boxObjData.nodes.push(duplicateBoxObjData.nodes[0]); boxObjData.nodes.push(duplicateBoxObjData.nodes[0]);
boxObjData.nodes[0].meshes[0].normals.length = 0; boxObjData.nodes[0].meshes[0].normals.length = 0;
var gltf = createGltf(boxObjData); var gltf = createGltf(boxObjData, defaultOptions);
var kmc1 = gltf.materials.Material.extensions.KHR_materials_common; var kmc1 = gltf.materials.Material.extensions.KHR_materials_common;
var kmc2 = gltf.materials.Material_shaded.extensions.KHR_materials_common; var kmc2 = gltf.materials.Material_shaded.extensions.KHR_materials_common;
@ -286,7 +286,7 @@ describe('createGltf', function() {
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;
var gltf = createGltf(boxObjData); var gltf = createGltf(boxObjData, defaultOptions);
var attributes = gltf.meshes[Object.keys(gltf.meshes)[0]].primitives[0].attributes; var attributes = gltf.meshes[Object.keys(gltf.meshes)[0]].primitives[0].attributes;
expect(attributes.POSITION).toBeDefined(); expect(attributes.POSITION).toBeDefined();
expect(attributes.NORMAL).toBeUndefined(); expect(attributes.NORMAL).toBeUndefined();
@ -296,7 +296,7 @@ describe('createGltf', function() {
it('runs without uvs', function() { it('runs without uvs', function() {
boxObjData.nodes[0].meshes[0].uvs.length = 0; boxObjData.nodes[0].meshes[0].uvs.length = 0;
var gltf = createGltf(boxObjData); var gltf = createGltf(boxObjData, defaultOptions);
var attributes = gltf.meshes[Object.keys(gltf.meshes)[0]].primitives[0].attributes; var attributes = gltf.meshes[Object.keys(gltf.meshes)[0]].primitives[0].attributes;
expect(attributes.POSITION).toBeDefined(); expect(attributes.POSITION).toBeDefined();
expect(attributes.NORMAL).toBeDefined(); expect(attributes.NORMAL).toBeDefined();
@ -307,7 +307,7 @@ describe('createGltf', function() {
boxObjData.nodes[0].meshes[0].normals.length = 0; boxObjData.nodes[0].meshes[0].normals.length = 0;
boxObjData.nodes[0].meshes[0].uvs.length = 0; boxObjData.nodes[0].meshes[0].uvs.length = 0;
var gltf = createGltf(boxObjData); var gltf = createGltf(boxObjData, defaultOptions);
var attributes = gltf.meshes[Object.keys(gltf.meshes)[0]].primitives[0].attributes; var attributes = gltf.meshes[Object.keys(gltf.meshes)[0]].primitives[0].attributes;
expect(attributes.POSITION).toBeDefined(); expect(attributes.POSITION).toBeDefined();
expect(attributes.NORMAL).toBeUndefined(); expect(attributes.NORMAL).toBeUndefined();
@ -347,7 +347,7 @@ describe('createGltf', function() {
var indicesLength = mesh.primitives[0].indices.length; var indicesLength = mesh.primitives[0].indices.length;
var vertexCount = mesh.positions.length / 3; var vertexCount = mesh.positions.length / 3;
var gltf = createGltf(boxObjData); var gltf = createGltf(boxObjData, defaultOptions);
var primitive = gltf.meshes[Object.keys(gltf.meshes)[0]].primitives[0]; var primitive = gltf.meshes[Object.keys(gltf.meshes)[0]].primitives[0];
var indicesAccessor = gltf.accessors[primitive.indices]; var indicesAccessor = gltf.accessors[primitive.indices];
expect(indicesAccessor.count).toBe(indicesLength); expect(indicesAccessor.count).toBe(indicesLength);
@ -361,7 +361,7 @@ describe('createGltf', function() {
it('ambient of [1, 1, 1] is treated as [0, 0, 0]', function() { it('ambient of [1, 1, 1] is treated as [0, 0, 0]', function() {
boxObjData.materials.Material.ambientColor = [1.0, 1.0, 1.0, 1.0]; boxObjData.materials.Material.ambientColor = [1.0, 1.0, 1.0, 1.0];
var gltf = createGltf(boxObjData); var gltf = createGltf(boxObjData, defaultOptions);
var ambient = gltf.materials.Material.extensions.KHR_materials_common.values.ambient; var ambient = gltf.materials.Material.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]);