Move tex alpha detection to cv method

This commit is contained in:
Zach Pomerantz 2016-03-23 14:01:11 -07:00
parent 16c5971a7f
commit fc8b34f8c7
2 changed files with 41 additions and 32 deletions

View file

@ -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<Schema>()._key = (uint32)_key._flags.to_ulong();
if (channel == MaterialKey::ALBEDO_MAP) {
resetOpacityMap();
}
_schemaBuffer.edit<Schema>()._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<Schema>()._key = (uint32)_key._flags.to_ulong();
}

View file

@ -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;