From d2f7d0e270156509f18390244fa17fae65d92712 Mon Sep 17 00:00:00 2001 From: Par Winzell Date: Fri, 29 Mar 2019 09:59:57 -0700 Subject: [PATCH] Generally repair old broken logic around aoMetRough texture. --- src/gltf/Raw2Gltf.cpp | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/src/gltf/Raw2Gltf.cpp b/src/gltf/Raw2Gltf.cpp index a221f05..fb195da 100644 --- a/src/gltf/Raw2Gltf.cpp +++ b/src/gltf/Raw2Gltf.cpp @@ -261,13 +261,19 @@ ModelData* Raw2Gltf( * Other values translate directly. */ RawMetRoughMatProps* props = (RawMetRoughMatProps*)material.info.get(); - - // If either a metallic or roughness map has been provided, generate the aoMetRoughTex. + + // determine if we need to generate a combined map bool hasMetallicMap = material.textures[RAW_TEXTURE_USAGE_METALLIC] >= 0; bool hasRoughnessMap = material.textures[RAW_TEXTURE_USAGE_ROUGHNESS] >= 0; - if (hasMetallicMap || hasRoughnessMap) { - // merge metallic into the blue channel and roughness into the green, of a new combinatory - // texture + bool hasOcclusionMap = material.textures[RAW_TEXTURE_USAGE_OCCLUSION] >= 0; + aoMetRoughTex = hasMetallicMap + ? simpleTex(RAW_TEXTURE_USAGE_METALLIC) + : (hasRoughnessMap + ? simpleTex(RAW_TEXTURE_USAGE_ROUGHNESS) + : (hasOcclusionMap ? simpleTex(RAW_TEXTURE_USAGE_OCCLUSION) : nullptr)); + if (aoMetRoughTex == nullptr) { + // merge occlusion into the red channel, metallic into blue channel, and + // roughness into the green, of a new combinatory texture aoMetRoughTex = textureBuilder.combine( { material.textures[RAW_TEXTURE_USAGE_OCCLUSION], @@ -275,14 +281,16 @@ ModelData* Raw2Gltf( material.textures[RAW_TEXTURE_USAGE_ROUGHNESS], }, "ao_met_rough", - [&](const std::vector pixels) -> TextureBuilder::pixel { + [&](const std::vector pixels) + -> TextureBuilder::pixel { const float occlusion = (*pixels[0])[0]; const float metallic = (*pixels[1])[0] * (hasMetallicMap ? 1 : props->metallic); - const float roughness = (*pixels[2])[0] * (hasRoughnessMap ? 1 : props->roughness); + const float roughness = + (*pixels[2])[0] * (hasRoughnessMap ? 1 : props->roughness); return {{occlusion, - props->invertRoughnessMap ? 1.0f - roughness : roughness, - metallic, - 1}}; + props->invertRoughnessMap ? 1.0f - roughness : roughness, + metallic, + 1}}; }, false); } @@ -292,11 +300,8 @@ ModelData* Raw2Gltf( roughness = props->roughness; emissiveFactor = props->emissiveFactor; emissiveIntensity = props->emissiveIntensity; - // add the occlusion texture only if actual occlusion pixels exist in the aoNetRough - // texture. - if (material.textures[RAW_TEXTURE_USAGE_OCCLUSION] >= 0) { - occlusionTexture = aoMetRoughTex.get(); - } + // this will set occlusionTexture to null, if no actual occlusion map exists + occlusionTexture = aoMetRoughTex.get(); } else { /** * Traditional FBX Material -> PBR Met/Rough glTF.