Skip to content

Commit

Permalink
[FIX] Handle case where pbrMetallicRoughness is not present (#7198)
Browse files Browse the repository at this point in the history
* [FIX] Handle case where pbrMetallicRoughness is not present

* Use correct glTF spec default for roughness
  • Loading branch information
willeastcott authored Dec 18, 2024
1 parent 68d7141 commit 53a574b
Showing 1 changed file with 9 additions and 16 deletions.
25 changes: 9 additions & 16 deletions src/framework/parsers/glb-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -1112,17 +1112,22 @@ const extensionIridescence = (data, material, textures) => {
const createMaterial = (gltfMaterial, textures) => {
const material = new StandardMaterial();

if (gltfMaterial.hasOwnProperty('name')) {
material.name = gltfMaterial.name;
}

// glTF doesn't define how to occlude specular
material.occludeSpecular = SPECOCC_AO;

material.diffuseVertexColor = true;

material.specularTint = true;
material.specularVertexColor = true;

if (gltfMaterial.hasOwnProperty('name')) {
material.name = gltfMaterial.name;
}
// Set glTF spec defaults
material.specular.set(1, 1, 1);
material.gloss = 0;
material.glossInvert = true;
material.useMetalness = true;

let color, texture;
if (gltfMaterial.hasOwnProperty('pbrMetallicRoughness')) {
Expand All @@ -1133,9 +1138,6 @@ const createMaterial = (gltfMaterial, textures) => {
// Convert from linear space to sRGB space
material.diffuse.set(Math.pow(color[0], 1 / 2.2), Math.pow(color[1], 1 / 2.2), Math.pow(color[2], 1 / 2.2));
material.opacity = color[3];
} else {
material.diffuse.set(1, 1, 1);
material.opacity = 1;
}
if (pbrData.hasOwnProperty('baseColorTexture')) {
const baseColorTexture = pbrData.baseColorTexture;
Expand All @@ -1148,19 +1150,12 @@ const createMaterial = (gltfMaterial, textures) => {

extractTextureTransform(baseColorTexture, material, ['diffuse', 'opacity']);
}
material.useMetalness = true;
material.specular.set(1, 1, 1);
if (pbrData.hasOwnProperty('metallicFactor')) {
material.metalness = pbrData.metallicFactor;
} else {
material.metalness = 1;
}
if (pbrData.hasOwnProperty('roughnessFactor')) {
material.gloss = pbrData.roughnessFactor;
} else {
material.gloss = 1;
}
material.glossInvert = true;
if (pbrData.hasOwnProperty('metallicRoughnessTexture')) {
const metallicRoughnessTexture = pbrData.metallicRoughnessTexture;
material.metalnessMap = material.glossMap = textures[metallicRoughnessTexture.index];
Expand Down Expand Up @@ -1195,8 +1190,6 @@ const createMaterial = (gltfMaterial, textures) => {
color = gltfMaterial.emissiveFactor;
// Convert from linear space to sRGB space
material.emissive.set(Math.pow(color[0], 1 / 2.2), Math.pow(color[1], 1 / 2.2), Math.pow(color[2], 1 / 2.2));
} else {
material.emissive.set(0, 0, 0);
}

if (gltfMaterial.hasOwnProperty('emissiveTexture')) {
Expand Down

0 comments on commit 53a574b

Please sign in to comment.