mirror of
https://github.com/LucasDower/ObjToSchematic.git
synced 2025-12-11 20:15:30 +01:00
Merge pull request #166 from ryanhlewis/main
GLTF Multiple Materials Fix
This commit is contained in:
commit
0813e8ddae
@ -31,17 +31,22 @@ export class GltfLoader extends IImporter {
|
||||
const meshTexcoords: UV[] = [];
|
||||
const meshTriangles: Tri[] = [];
|
||||
const meshMaterials: MaterialMap = new Map();
|
||||
|
||||
meshMaterials.set('NONE', {
|
||||
type: MaterialType.solid,
|
||||
colour: RGBAUtil.copy(RGBAColours.WHITE),
|
||||
needsAttention: false,
|
||||
canBeTextured: false,
|
||||
});
|
||||
|
||||
let maxIndex = 0;
|
||||
let materialIndex = 0; // New variable to create unique material identifiers
|
||||
|
||||
Object.values(gltf.meshes).forEach((mesh: any) => {
|
||||
Object.values(mesh.primitives).forEach((primitive: any) => {
|
||||
const attributes = primitive.attributes;
|
||||
|
||||
// Handling vertices
|
||||
if (attributes.POSITION !== undefined) {
|
||||
const positions = attributes.POSITION.value as Float32Array;
|
||||
for (let i = 0; i < positions.length; i += 3) {
|
||||
@ -52,6 +57,8 @@ export class GltfLoader extends IImporter {
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
// Handling normals
|
||||
if (attributes.NORMAL !== undefined) {
|
||||
const normals = attributes.NORMAL.value as Float32Array;
|
||||
for (let i = 0; i < normals.length; i += 3) {
|
||||
@ -62,6 +69,8 @@ export class GltfLoader extends IImporter {
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
// Handling texture coordinates
|
||||
if (attributes.TEXCOORD_0 !== undefined) {
|
||||
const texcoords = attributes.TEXCOORD_0.value as Float32Array;
|
||||
for (let i = 0; i < texcoords.length; i += 2) {
|
||||
@ -71,14 +80,18 @@ export class GltfLoader extends IImporter {
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
// Material
|
||||
let materialNameToUse = 'NONE';
|
||||
{
|
||||
let materialBaseName = 'NONE';
|
||||
if (primitive.material) {
|
||||
const materialName = primitive.material.name;
|
||||
materialBaseName = primitive.material.name || 'Material';
|
||||
}
|
||||
|
||||
let materialMade = false;
|
||||
const materialNameToUse = materialBaseName + '_' + materialIndex; // Unique material identifier
|
||||
materialIndex++; // Increment material index
|
||||
|
||||
// Handling materials
|
||||
if (primitive.material) {
|
||||
const pbr = primitive.material.pbrMetallicRoughness;
|
||||
if (pbr !== undefined) {
|
||||
const diffuseTexture = pbr.baseColorTexture;
|
||||
@ -96,7 +109,7 @@ export class GltfLoader extends IImporter {
|
||||
imageData.reduce((data, byte) => data + String.fromCharCode(byte), ''),
|
||||
);
|
||||
|
||||
meshMaterials.set(materialName, {
|
||||
meshMaterials.set(materialNameToUse, {
|
||||
type: MaterialType.textured,
|
||||
diffuse: {
|
||||
filetype: mimeType === 'image/jpeg' ? 'jpg' : 'png',
|
||||
@ -108,64 +121,18 @@ export class GltfLoader extends IImporter {
|
||||
transparency: { type: 'None' },
|
||||
});
|
||||
} catch {
|
||||
meshMaterials.set(materialName, {
|
||||
meshMaterials.set(materialNameToUse, {
|
||||
type: MaterialType.solid,
|
||||
colour: RGBAUtil.copy(RGBAColours.WHITE),
|
||||
needsAttention: false,
|
||||
canBeTextured: true,
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
|
||||
*/
|
||||
|
||||
materialNameToUse = materialName;
|
||||
materialMade = true;
|
||||
} else {
|
||||
const diffuseColour: (number[] | undefined) = pbr.baseColorFactor;
|
||||
|
||||
if (diffuseColour !== undefined) {
|
||||
meshMaterials.set(materialName, {
|
||||
type: MaterialType.solid,
|
||||
colour: {
|
||||
r: diffuseColour[0],
|
||||
g: diffuseColour[1],
|
||||
b: diffuseColour[2],
|
||||
a: diffuseColour[3],
|
||||
},
|
||||
needsAttention: false,
|
||||
canBeTextured: false,
|
||||
});
|
||||
}
|
||||
|
||||
materialNameToUse = materialName;
|
||||
materialMade = true;
|
||||
}
|
||||
}
|
||||
|
||||
const emissiveColour: (number[] | undefined) = primitive.material.pbr;
|
||||
if (!materialMade && emissiveColour !== undefined) {
|
||||
meshMaterials.set(materialName, {
|
||||
type: MaterialType.solid,
|
||||
colour: {
|
||||
r: emissiveColour[0],
|
||||
g: emissiveColour[1],
|
||||
b: emissiveColour[2],
|
||||
a: 1.0,
|
||||
},
|
||||
needsAttention: false,
|
||||
canBeTextured: false,
|
||||
});
|
||||
|
||||
materialNameToUse = materialName;
|
||||
materialMade = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Indices
|
||||
{
|
||||
const indices = primitive.indices.value as Uint16Array;
|
||||
for (let i = 0; i < indices.length / 3; ++i) {
|
||||
meshTriangles.push({
|
||||
@ -189,7 +156,6 @@ export class GltfLoader extends IImporter {
|
||||
}
|
||||
|
||||
maxIndex += localMax + 1;
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user