Merge pull request #166 from ryanhlewis/main

GLTF Multiple Materials Fix
This commit is contained in:
Lucas Dower 2025-04-28 21:19:08 +01:00 committed by GitHub
commit 0813e8ddae
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -31,17 +31,22 @@ export class GltfLoader extends IImporter {
const meshTexcoords: UV[] = []; const meshTexcoords: UV[] = [];
const meshTriangles: Tri[] = []; const meshTriangles: Tri[] = [];
const meshMaterials: MaterialMap = new Map(); const meshMaterials: MaterialMap = new Map();
meshMaterials.set('NONE', { meshMaterials.set('NONE', {
type: MaterialType.solid, type: MaterialType.solid,
colour: RGBAUtil.copy(RGBAColours.WHITE), colour: RGBAUtil.copy(RGBAColours.WHITE),
needsAttention: false, needsAttention: false,
canBeTextured: false, canBeTextured: false,
}); });
let maxIndex = 0; let maxIndex = 0;
let materialIndex = 0; // New variable to create unique material identifiers
Object.values(gltf.meshes).forEach((mesh: any) => { Object.values(gltf.meshes).forEach((mesh: any) => {
Object.values(mesh.primitives).forEach((primitive: any) => { Object.values(mesh.primitives).forEach((primitive: any) => {
const attributes = primitive.attributes; const attributes = primitive.attributes;
// Handling vertices
if (attributes.POSITION !== undefined) { if (attributes.POSITION !== undefined) {
const positions = attributes.POSITION.value as Float32Array; const positions = attributes.POSITION.value as Float32Array;
for (let i = 0; i < positions.length; i += 3) { for (let i = 0; i < positions.length; i += 3) {
@ -52,6 +57,8 @@ export class GltfLoader extends IImporter {
)); ));
} }
} }
// Handling normals
if (attributes.NORMAL !== undefined) { if (attributes.NORMAL !== undefined) {
const normals = attributes.NORMAL.value as Float32Array; const normals = attributes.NORMAL.value as Float32Array;
for (let i = 0; i < normals.length; i += 3) { 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) { if (attributes.TEXCOORD_0 !== undefined) {
const texcoords = attributes.TEXCOORD_0.value as Float32Array; const texcoords = attributes.TEXCOORD_0.value as Float32Array;
for (let i = 0; i < texcoords.length; i += 2) { for (let i = 0; i < texcoords.length; i += 2) {
@ -71,125 +80,82 @@ export class GltfLoader extends IImporter {
)); ));
} }
} }
// Material // Material
let materialNameToUse = 'NONE'; let materialBaseName = 'NONE';
{ if (primitive.material) {
if (primitive.material) { materialBaseName = primitive.material.name || 'Material';
const materialName = primitive.material.name; }
let materialMade = false; const materialNameToUse = materialBaseName + '_' + materialIndex; // Unique material identifier
materialIndex++; // Increment material index
const pbr = primitive.material.pbrMetallicRoughness; // Handling materials
if (pbr !== undefined) { if (primitive.material) {
const diffuseTexture = pbr.baseColorTexture; const pbr = primitive.material.pbrMetallicRoughness;
if (diffuseTexture !== undefined) { if (pbr !== undefined) {
const imageData: Uint8Array = diffuseTexture.texture.source.bufferView.data; const diffuseTexture = pbr.baseColorTexture;
const mimeType: string = diffuseTexture.texture.source.mimeType; if (diffuseTexture !== undefined) {
const imageData: Uint8Array = diffuseTexture.texture.source.bufferView.data;
const mimeType: string = diffuseTexture.texture.source.mimeType;
try { try {
if (mimeType !== 'image/png' && mimeType !== 'image/jpeg') { if (mimeType !== 'image/png' && mimeType !== 'image/jpeg') {
StatusHandler.warning(LOC('import.unsupported_image_type', { file_name: diffuseTexture.texture.source.id, file_type: mimeType })); StatusHandler.warning(LOC('import.unsupported_image_type', { file_name: diffuseTexture.texture.source.id, file_type: mimeType }));
throw new Error('Unsupported image type'); throw new Error('Unsupported image type');
}
const base64 = btoa(
imageData.reduce((data, byte) => data + String.fromCharCode(byte), ''),
);
meshMaterials.set(materialName, {
type: MaterialType.textured,
diffuse: {
filetype: mimeType === 'image/jpeg' ? 'jpg' : 'png',
raw: (mimeType === 'image/jpeg' ? 'data:image/jpeg;base64,' : 'data:image/png;base64,') + base64,
},
extension: 'clamp',
interpolation: 'linear',
needsAttention: false,
transparency: { type: 'None' },
});
} catch {
meshMaterials.set(materialName, {
type: MaterialType.solid,
colour: RGBAUtil.copy(RGBAColours.WHITE),
needsAttention: false,
canBeTextured: true,
});
} }
const base64 = btoa(
imageData.reduce((data, byte) => data + String.fromCharCode(byte), ''),
);
/* meshMaterials.set(materialNameToUse, {
type: MaterialType.textured,
*/ diffuse: {
filetype: mimeType === 'image/jpeg' ? 'jpg' : 'png',
materialNameToUse = materialName; raw: (mimeType === 'image/jpeg' ? 'data:image/jpeg;base64,' : 'data:image/png;base64,') + base64,
materialMade = true; },
} else { extension: 'clamp',
const diffuseColour: (number[] | undefined) = pbr.baseColorFactor; interpolation: 'linear',
needsAttention: false,
if (diffuseColour !== undefined) { transparency: { type: 'None' },
meshMaterials.set(materialName, { });
type: MaterialType.solid, } catch {
colour: { meshMaterials.set(materialNameToUse, {
r: diffuseColour[0], type: MaterialType.solid,
g: diffuseColour[1], colour: RGBAUtil.copy(RGBAColours.WHITE),
b: diffuseColour[2], needsAttention: false,
a: diffuseColour[3], canBeTextured: true,
}, });
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 // Indices
{ const indices = primitive.indices.value as Uint16Array;
const indices = primitive.indices.value as Uint16Array; for (let i = 0; i < indices.length / 3; ++i) {
for (let i = 0; i < indices.length / 3; ++i) { meshTriangles.push({
meshTriangles.push({ material: materialNameToUse,
material: materialNameToUse, positionIndices: {
positionIndices: { x: maxIndex + indices[i * 3 + 0],
x: maxIndex + indices[i * 3 + 0], y: maxIndex + indices[i * 3 + 1],
y: maxIndex + indices[i * 3 + 1], z: maxIndex + indices[i * 3 + 2],
z: maxIndex + indices[i * 3 + 2], },
}, texcoordIndices: {
texcoordIndices: { x: maxIndex + indices[i * 3 + 0],
x: maxIndex + indices[i * 3 + 0], y: maxIndex + indices[i * 3 + 1],
y: maxIndex + indices[i * 3 + 1], z: maxIndex + indices[i * 3 + 2],
z: maxIndex + indices[i * 3 + 2], },
}, });
});
}
let localMax = 0;
for (let i = 0; i < indices.length; ++i) {
localMax = Math.max(localMax, indices[i]);
}
maxIndex += localMax + 1;
} }
let localMax = 0;
for (let i = 0; i < indices.length; ++i) {
localMax = Math.max(localMax, indices[i]);
}
maxIndex += localMax + 1;
}); });
}); });