mirror of
https://github.com/CesiumGS/obj2gltf.git
synced 2024-11-23 08:34:14 -05:00
Merge pull request #173 from AnalyticalGraphicsInc/newmtl-without-name
Fix for material that doesn't have a name
This commit is contained in:
commit
ebd7d73a21
@ -93,7 +93,7 @@ function loadMtl(mtlPath, options) {
|
||||
|
||||
function parseLine(line) {
|
||||
line = line.trim();
|
||||
if (/^newmtl /i.test(line)) {
|
||||
if (/^newmtl/i.test(line)) {
|
||||
const name = line.substring(7).trim();
|
||||
createMaterial(name);
|
||||
} else if (/^Ka /i.test(line)) {
|
||||
|
@ -329,7 +329,7 @@ function loadObj(objPath, options) {
|
||||
} else if (/^g\s/i.test(line)) {
|
||||
const groupName = line.substring(2).trim();
|
||||
addMesh(groupName);
|
||||
} else if (/^usemtl\s/i.test(line)) {
|
||||
} else if (/^usemtl/i.test(line)) {
|
||||
const materialName = line.substring(7).trim();
|
||||
useMaterial(materialName);
|
||||
} else if (/^mtllib/i.test(line)) {
|
||||
@ -427,6 +427,7 @@ function finishLoading(nodes, mtlPaths, objPath, usesMaterials, options) {
|
||||
if (materials.length > 0 && !usesMaterials) {
|
||||
assignDefaultMaterial(nodes, materials, usesMaterials);
|
||||
}
|
||||
assignUnnamedMaterial(nodes, materials);
|
||||
return {
|
||||
nodes : nodes,
|
||||
materials : materials,
|
||||
@ -502,6 +503,34 @@ function assignDefaultMaterial(nodes, materials) {
|
||||
}
|
||||
}
|
||||
|
||||
function assignUnnamedMaterial(nodes, materials) {
|
||||
// If there is a material that doesn't have a name, assign that
|
||||
// material to any primitives whose material is undefined.
|
||||
const unnamedMaterial = materials.find(function(material) {
|
||||
return material.name.length === 0;
|
||||
});
|
||||
|
||||
if (!defined(unnamedMaterial)) {
|
||||
return;
|
||||
}
|
||||
|
||||
const nodesLength = nodes.length;
|
||||
for (let i = 0; i < nodesLength; ++i) {
|
||||
const meshes = nodes[i].meshes;
|
||||
const meshesLength = meshes.length;
|
||||
for (let j = 0; j < meshesLength; ++j) {
|
||||
const primitives = meshes[j].primitives;
|
||||
const primitivesLength = primitives.length;
|
||||
for (let k = 0; k < primitivesLength; ++k) {
|
||||
const primitive = primitives[k];
|
||||
if (!defined(primitive.material)) {
|
||||
primitive.material = unnamedMaterial.name;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function removeEmptyMeshes(meshes) {
|
||||
return meshes.filter(function(mesh) {
|
||||
// Remove empty primitives
|
||||
|
12
specs/data/box-unnamed-material/box-unnamed-material.mtl
Normal file
12
specs/data/box-unnamed-material/box-unnamed-material.mtl
Normal file
@ -0,0 +1,12 @@
|
||||
# Blender MTL File: 'None'
|
||||
# Material Count: 1
|
||||
|
||||
newmtl
|
||||
Ns 96.078431
|
||||
Ka 0.100000 0.000000 0.000000
|
||||
Kd 0.640000 0.640000 0.640000
|
||||
Ks 0.500000 0.500000 0.500000
|
||||
Ke 0.000000 0.000000 0.100000
|
||||
Ni 1.000000
|
||||
d 1.000000
|
||||
illum 2
|
46
specs/data/box-unnamed-material/box-unnamed-material.obj
Normal file
46
specs/data/box-unnamed-material/box-unnamed-material.obj
Normal file
@ -0,0 +1,46 @@
|
||||
# Blender v2.78 (sub 0) OBJ File: ''
|
||||
# www.blender.org
|
||||
mtllib box-unnamed-material.mtl
|
||||
o Cube
|
||||
v -1.000000 -1.000000 1.000000
|
||||
v -1.000000 1.000000 1.000000
|
||||
v -1.000000 -1.000000 -1.000000
|
||||
v -1.000000 1.000000 -1.000000
|
||||
v 1.000000 -1.000000 1.000000
|
||||
v 1.000000 1.000000 1.000000
|
||||
v 1.000000 -1.000000 -1.000000
|
||||
v 1.000000 1.000000 -1.000000
|
||||
vt 0.0000 0.0000
|
||||
vt 1.0000 0.0000
|
||||
vt 1.0000 1.0000
|
||||
vt 0.0000 1.0000
|
||||
vt 0.0000 0.0000
|
||||
vt 1.0000 0.0000
|
||||
vt 1.0000 1.0000
|
||||
vt 0.0000 1.0000
|
||||
vt 0.0000 0.0000
|
||||
vt 1.0000 0.0000
|
||||
vt 1.0000 1.0000
|
||||
vt 0.0000 1.0000
|
||||
vt 0.0000 0.0000
|
||||
vt 1.0000 0.0000
|
||||
vt 1.0000 1.0000
|
||||
vt 0.0000 1.0000
|
||||
vt 1.0000 0.0000
|
||||
vt 1.0000 1.0000
|
||||
vt 0.0000 0.0000
|
||||
vt 0.0000 1.0000
|
||||
vn -1.0000 0.0000 0.0000
|
||||
vn 0.0000 0.0000 -1.0000
|
||||
vn 1.0000 0.0000 0.0000
|
||||
vn 0.0000 0.0000 1.0000
|
||||
vn 0.0000 -1.0000 0.0000
|
||||
vn 0.0000 1.0000 0.0000
|
||||
usemtl
|
||||
s off
|
||||
f 1/1/1 2/2/1 4/3/1 3/4/1
|
||||
f 3/5/2 4/6/2 8/7/2 7/8/2
|
||||
f 7/9/3 8/10/3 6/11/3 5/12/3
|
||||
f 5/13/4 6/14/4 2/15/4 1/16/4
|
||||
f 3/5/5 7/17/5 5/18/5 1/16/5
|
||||
f 8/19/6 4/6/6 2/15/6 6/20/6
|
@ -28,6 +28,7 @@ const objMtllibPath = 'specs/data/box-mtllib/box-mtllib.obj';
|
||||
const objMtllibSpacesPath = 'specs/data/box-mtllib-spaces/box mtllib.obj';
|
||||
const objMissingMtllibPath = 'specs/data/box-missing-mtllib/box-missing-mtllib.obj';
|
||||
const objMissingUsemtlPath = 'specs/data/box-missing-usemtl/box-missing-usemtl.obj';
|
||||
const objUnnamedMaterialPath = 'specs/data/box-unnamed-material/box-unnamed-material.obj';
|
||||
const objExternalResourcesPath = 'specs/data/box-external-resources/box-external-resources.obj';
|
||||
const objResourcesInRootPath = 'specs/data/box-resources-in-root/box-resources-in-root.obj';
|
||||
const objExternalResourcesInRootPath = 'specs/data/box-external-resources-in-root/box-external-resources-in-root.obj';
|
||||
@ -359,6 +360,12 @@ describe('loadObj', () => {
|
||||
expect(data.nodes[0].meshes[0].primitives[0].material).toBe('Material');
|
||||
});
|
||||
|
||||
it('loads obj with unnamed material', async () => {
|
||||
const data = await loadObj(objUnnamedMaterialPath, options);
|
||||
expect(data.materials.length).toBe(1);
|
||||
expect(data.nodes[0].meshes[0].primitives[0].material).toBe('');
|
||||
});
|
||||
|
||||
it('loads .mtl outside of the obj directory', async () => {
|
||||
const data = await loadObj(objExternalResourcesPath, options);
|
||||
const materials = data.materials;
|
||||
|
Loading…
Reference in New Issue
Block a user