Skip to content

Commit

Permalink
Merge pull request #89 from AnalyticalGraphicsInc/keep-diffuse
Browse files Browse the repository at this point in the history
Keep diffuse intact so that shader generation for generateNormals works
  • Loading branch information
mramato authored Jun 23, 2017
2 parents 604e273 + d2eee3e commit 813290a
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 24 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Change Log
### Next release

* 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

Expand Down
8 changes: 4 additions & 4 deletions lib/createGltf.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module.exports = createGltf;
*
* @private
*/
function createGltf(objData) {
function createGltf(objData, options) {
var nodes = objData.nodes;
var materials = objData.materials;
var images = objData.images;
Expand Down Expand Up @@ -67,7 +67,7 @@ function createGltf(objData) {
return 'texture_' + getImageId(imagePath);
}

function createMaterial(material, hasNormals) {
function createMaterial(material, hasNormals, options) {
var ambient = defaultValue(defaultValue(getTextureId(material.ambientTexture), material.ambientColor));
var diffuse = defaultValue(defaultValue(getTextureId(material.diffuseTexture), material.diffuseColor));
var emission = defaultValue(defaultValue(getTextureId(material.emissionTexture), material.emissionColor));
Expand Down Expand Up @@ -95,7 +95,7 @@ function createGltf(objData) {

var doubleSided = transparent;

if (!hasNormals) {
if (!hasNormals && !options.generateNormals) {
// Constant technique only factors in ambient and emission sources - set emission to diffuse
emission = diffuse;
diffuse = [0, 0, 0, 1];
Expand Down Expand Up @@ -303,7 +303,7 @@ function createGltf(objData) {
}

if (!defined(gltfMaterial)) {
gltf.materials[materialId] = createMaterial(material, hasNormals);
gltf.materials[materialId] = createMaterial(material, hasNormals, options);
}

gltfMeshPrimitives.push({
Expand Down
3 changes: 2 additions & 1 deletion lib/obj2gltf.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ function obj2gltf(objPath, gltfPath, options) {
var outputUpAxis = defaultValue(options.outputUpAxis, defaults.outputUpAxis);
var logger = defaultValue(options.logger, defaults.logger);

options.generateNormals = generateNormals;
options.separate = separate;
options.separateTextures = separateTextures;
options.checkTransparency = checkTransparency;
Expand Down Expand Up @@ -112,7 +113,7 @@ function obj2gltf(objPath, gltfPath, options) {

return loadObj(objPath, options)
.then(function(objData) {
return createGltf(objData);
return createGltf(objData, options);
})
.then(function(gltf) {
return writeUris(gltf, gltfPath, resourcesDirectory, options);
Expand Down
38 changes: 19 additions & 19 deletions specs/lib/createGltfSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,15 @@ describe('createGltf', function() {
});

it('simple gltf', function(done) {
var gltf = createGltf(boxObjData);
var gltf = createGltf(boxObjData, defaultOptions);
expect(writeUris(gltf, boxGltfUrl, path.dirname(boxGltfUrl), defaultOptions)
.then(function() {
expect(gltf).toEqual(boxGltf);
}), done).toResolve();
});

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)
.then(function() {
Expand All @@ -97,7 +97,7 @@ describe('createGltf', function() {
it('sets default material values', function() {
boxObjData.materials.Material = new Material();

var gltf = createGltf(boxObjData);
var gltf = createGltf(boxObjData, defaultOptions);
var material = gltf.materials.Material;
var kmc = material.extensions.KHR_materials_common;
var values = kmc.values;
Expand All @@ -116,7 +116,7 @@ describe('createGltf', function() {
boxObjData.materials.Material = material;
boxObjData.images[diffuseTextureUrl] = diffuseTexture;

var gltf = createGltf(boxObjData);
var gltf = createGltf(boxObjData, defaultOptions);
var kmc = gltf.materials.Material.extensions.KHR_materials_common;
var texture = gltf.textures.texture_cesium;
var image = gltf.images.cesium;
Expand Down Expand Up @@ -154,7 +154,7 @@ describe('createGltf', function() {
material.alpha = 0.4;
boxObjData.materials.Material = material;

var gltf = createGltf(boxObjData);
var gltf = createGltf(boxObjData, defaultOptions);
var kmc = gltf.materials.Material.extensions.KHR_materials_common;

expect(kmc.values.diffuse).toEqual([0.5, 0.5, 0.5, 0.4]);
Expand All @@ -171,7 +171,7 @@ describe('createGltf', function() {

boxObjData.images[diffuseTextureUrl] = diffuseTexture;

var gltf = createGltf(boxObjData);
var gltf = createGltf(boxObjData, defaultOptions);
var kmc = gltf.materials.Material.extensions.KHR_materials_common;

expect(kmc.values.diffuse).toEqual('texture_cesium');
Expand All @@ -187,7 +187,7 @@ describe('createGltf', function() {

boxObjData.images[transparentDiffuseTextureUrl] = transparentDiffuseTexture;

var gltf = createGltf(boxObjData);
var gltf = createGltf(boxObjData, defaultOptions);
var kmc = gltf.materials.Material.extensions.KHR_materials_common;

expect(kmc.values.diffuse).toBe('texture_diffuse');
Expand All @@ -202,7 +202,7 @@ describe('createGltf', function() {
material.specularShininess = 0.1;
boxObjData.materials.Material = material;

var gltf = createGltf(boxObjData);
var gltf = createGltf(boxObjData, defaultOptions);
var kmc = gltf.materials.Material.extensions.KHR_materials_common;

expect(kmc.technique).toBe('PHONG');
Expand All @@ -219,7 +219,7 @@ describe('createGltf', function() {

boxObjData.images[diffuseTextureUrl] = diffuseTexture;

var gltf = createGltf(boxObjData);
var gltf = createGltf(boxObjData, defaultOptions);
var kmc = gltf.materials.Material.extensions.KHR_materials_common;

expect(kmc.technique).toBe('CONSTANT');
Expand All @@ -231,7 +231,7 @@ describe('createGltf', function() {
material.diffuseTexture = diffuseTextureUrl;
boxObjData.materials.Material = material;

var gltf = createGltf(boxObjData);
var gltf = createGltf(boxObjData, defaultOptions);
var kmc = gltf.materials.Material.extensions.KHR_materials_common;

expect(kmc.values.diffuse).toEqual([0.5, 0.5, 0.5, 1.0]);
Expand All @@ -241,7 +241,7 @@ describe('createGltf', function() {
boxObjData.nodes[0].meshes[0].primitives[0].material = undefined;

// Creates a material called "default"
var gltf = createGltf(boxObjData);
var gltf = createGltf(boxObjData, defaultOptions);
expect(gltf.materials.default).toBeDefined();
var kmc = gltf.materials.default.extensions.KHR_materials_common;
expect(kmc.values.diffuse).toEqual([0.5, 0.5, 0.5, 1.0]);
Expand All @@ -251,7 +251,7 @@ describe('createGltf', function() {
boxObjData.materials = {};

// 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;

expect(kmc.values.diffuse).toEqual([0.5, 0.5, 0.5, 1.0]);
Expand All @@ -262,7 +262,7 @@ describe('createGltf', function() {
boxObjData.nodes.push(duplicateBoxObjData.nodes[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 kmc2 = gltf.materials.Material_constant.extensions.KHR_materials_common;

Expand All @@ -275,7 +275,7 @@ describe('createGltf', function() {
boxObjData.nodes.push(duplicateBoxObjData.nodes[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 kmc2 = gltf.materials.Material_shaded.extensions.KHR_materials_common;

Expand All @@ -286,7 +286,7 @@ describe('createGltf', function() {
it('runs without normals', function() {
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;
expect(attributes.POSITION).toBeDefined();
expect(attributes.NORMAL).toBeUndefined();
Expand All @@ -296,7 +296,7 @@ describe('createGltf', function() {
it('runs without uvs', function() {
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;
expect(attributes.POSITION).toBeDefined();
expect(attributes.NORMAL).toBeDefined();
Expand All @@ -307,7 +307,7 @@ describe('createGltf', function() {
boxObjData.nodes[0].meshes[0].normals.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;
expect(attributes.POSITION).toBeDefined();
expect(attributes.NORMAL).toBeUndefined();
Expand Down Expand Up @@ -347,7 +347,7 @@ describe('createGltf', function() {
var indicesLength = mesh.primitives[0].indices.length;
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 indicesAccessor = gltf.accessors[primitive.indices];
expect(indicesAccessor.count).toBe(indicesLength);
Expand All @@ -361,7 +361,7 @@ describe('createGltf', 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];

var gltf = createGltf(boxObjData);
var gltf = createGltf(boxObjData, defaultOptions);
var ambient = gltf.materials.Material.extensions.KHR_materials_common.values.ambient;

expect(ambient).toEqual([0.0, 0.0, 0.0, 1.0]);
Expand Down

0 comments on commit 813290a

Please sign in to comment.