diff --git a/libraries/model/src/model/Material.cpp b/libraries/model/src/model/Material.cpp index 0a6a4bb695..5260143a7f 100755 --- a/libraries/model/src/model/Material.cpp +++ b/libraries/model/src/model/Material.cpp @@ -83,41 +83,48 @@ void Material::setMetallic(float metallic) { void Material::setTextureMap(MapChannel channel, const TextureMapPointer& textureMap) { if (textureMap) { _key.setMapChannel(channel, (true)); - - if (channel == MaterialKey::ALBEDO_MAP) { - // clear the previous flags whatever they were: - _key.setOpacityMaskMap(false); - _key.setTranslucentMap(false); - - if (textureMap->useAlphaChannel() && textureMap->isDefined() && textureMap->getTextureView().isValid()) { - auto usage = textureMap->getTextureView()._texture->getUsage(); - if (usage.isAlpha()) { - // Texture has alpha, is not just a mask or a true transparent channel - if (usage.isAlphaMask()) { - _key.setOpacityMaskMap(true); - _key.setTranslucentMap(false); - } else { - _key.setOpacityMaskMap(false); - _key.setTranslucentMap(true); - } - } - } - } - _textureMaps[channel] = textureMap; } else { _key.setMapChannel(channel, (false)); - - if (channel == MaterialKey::ALBEDO_MAP) { - _key.setOpacityMaskMap(false); - _key.setTranslucentMap(false); - } - _textureMaps.erase(channel); } _schemaBuffer.edit()._key = (uint32)_key._flags.to_ulong(); + if (channel == MaterialKey::ALBEDO_MAP) { + resetOpacityMap(); + } + + _schemaBuffer.edit()._key = (uint32)_key._flags.to_ulong(); + +} + +void Material::resetOpacityMap() const { + // Clear the previous flags + _key.setOpacityMaskMap(false); + _key.setTranslucentMap(false); + + const auto& textureMap = getTextureMap(MaterialKey::ALBEDO_MAP); + if (textureMap && + textureMap->useAlphaChannel() && + textureMap->isDefined() && + textureMap->getTextureView().isValid()) { + + auto usage = textureMap->getTextureView()._texture->getUsage(); + if (usage.isAlpha()) { + if (usage.isAlphaMask()) { + // Texture has alpha, but it is just a mask + _key.setOpacityMaskMap(true); + _key.setTranslucentMap(false); + } else { + // Texture has alpha, it is a true translucency channel + _key.setOpacityMaskMap(false); + _key.setTranslucentMap(true); + } + } + } + + _schemaBuffer.edit()._key = (uint32)_key._flags.to_ulong(); } diff --git a/libraries/model/src/model/Material.h b/libraries/model/src/model/Material.h index 5a7b919994..c500e9ec10 100755 --- a/libraries/model/src/model/Material.h +++ b/libraries/model/src/model/Material.h @@ -291,15 +291,17 @@ public: const TextureMaps& getTextureMaps() const { return _textureMaps; } const TextureMapPointer getTextureMap(MapChannel channel) const; + // Albedo maps cannot have opacity detected until they are loaded + // This method allows const changing of the key/schemaBuffer without touching the map + void resetOpacityMap() const; + // conversion from legacy material properties to PBR equivalent static float shininessToRoughness(float shininess) { return 1.0f - shininess / 100.0f; } -protected: - - MaterialKey _key; - UniformBufferView _schemaBuffer; +private: + mutable MaterialKey _key; + mutable UniformBufferView _schemaBuffer; TextureMaps _textureMaps; - }; typedef std::shared_ptr< Material > MaterialPointer;