From 695e558851adbb7a12bae26a5ecc614e9cac1446 Mon Sep 17 00:00:00 2001
From: samcake <samuel.gateau@gmail.com>
Date: Mon, 21 Mar 2016 16:36:47 -0700
Subject: [PATCH] Trying to clena up the problem?

---
 .../src/model-networking/ModelCache.cpp       |  2 +-
 libraries/model/src/model/Material.cpp        | 18 +++++++++++-
 libraries/model/src/model/Material.slh        | 28 +++++++++----------
 .../render-utils/src/MaterialTextures.slh     |  4 +--
 4 files changed, 34 insertions(+), 18 deletions(-)

diff --git a/libraries/model-networking/src/model-networking/ModelCache.cpp b/libraries/model-networking/src/model-networking/ModelCache.cpp
index 7d5c6964e8..9c2725a919 100644
--- a/libraries/model-networking/src/model-networking/ModelCache.cpp
+++ b/libraries/model-networking/src/model-networking/ModelCache.cpp
@@ -151,7 +151,7 @@ bool NetworkGeometry::isLoadedWithTextures() const {
                 (material->lightmapTexture && !material->lightmapTexture->isLoaded())) {
                 return false;
             }
-            if (material->useAlbedoMapOpacity && material->albedoTexture && material->albedoTexture->getGPUTexture()) {
+            if (/*material->useAlbedoMapOpacity && */ material->albedoTexture && material->albedoTexture->getGPUTexture()) {
                 material->_material->setTextureMap(model::MaterialKey::ALBEDO_MAP, material->_material->getTextureMap(model::MaterialKey::ALBEDO_MAP));
                 // Reset the materialKey transparentTexture key only, as it is albedoTexture-dependent
                 const auto& usage = material->albedoTexture->getGPUTexture()->getUsage();
diff --git a/libraries/model/src/model/Material.cpp b/libraries/model/src/model/Material.cpp
index 5f5c93eb61..eaabda8f07 100755
--- a/libraries/model/src/model/Material.cpp
+++ b/libraries/model/src/model/Material.cpp
@@ -92,7 +92,23 @@ void Material::setTextureMap(MapChannel channel, const TextureMapPointer& textur
                         auto usage = textureMap->getTextureView()._texture->getUsage();
                         if (usage.isAlpha()) {
                             // Texture has alpha, is nut just a mask or a true transparent channel
-                            if (!usage.isAlphaMask()) {
+                            if (usage.isAlphaMask()) {
+                                _key.setOpacityMaskMap(true);
+                                _key.setTranslucentMap(false);
+                            } else {
+                                _key.setOpacityMaskMap(false);
+                                _key.setTranslucentMap(true);
+                            }
+                        }
+                    }
+                }
+            } else {
+                if (textureMap->isDefined()) {
+                    if (textureMap->getTextureView().isValid()) {
+                        auto usage = textureMap->getTextureView()._texture->getUsage();
+                        if (usage.isAlpha()) {
+                            // Texture has alpha, is nut just a mask or a true transparent channel
+                            if (usage.isAlphaMask()) {
                                 _key.setOpacityMaskMap(true);
                                 _key.setTranslucentMap(false);
                             } else {
diff --git a/libraries/model/src/model/Material.slh b/libraries/model/src/model/Material.slh
index e8c9d3d6bc..a5b58e2aa0 100644
--- a/libraries/model/src/model/Material.slh
+++ b/libraries/model/src/model/Material.slh
@@ -39,21 +39,21 @@ float getMaterialShininess(Material m) { return 1.0 - getMaterialRoughness(m); }
 
 int getMaterialKey(Material m) { return floatBitsToInt(m._spareKey.w); }
 
-const int EMISSIVE_VAL_BIT = 0x00000001;
-const int ALBEDO_VAL_BIT = 0x00000002;
-const int METALLIC_VAL_BIT = 0x00000004;
-const int GLOSSY_VAL_BIT = 0x00000008;
-const int OPACITY_VAL_BIT = 0x00000010;
+const int EMISSIVE_VAL_BIT              = 0x00000001;
+const int ALBEDO_VAL_BIT                = 0x00000002;
+const int METALLIC_VAL_BIT              = 0x00000004;
+const int GLOSSY_VAL_BIT                = 0x00000008;
+const int OPACITY_VAL_BIT               = 0x00000010;
 
-const int EMISSIVE_MAP_BIT = 0x00000020;
-const int ALBEDO_MAP_BIT = 0x00000040;
-const int OPACITY_MASK_MAP_BIT = 0x00000080;
-const int OPACITY_TRANSLUCENT_MAP_BIT = 0x00000100;
-const int METALLIC_MAP_BIT = 0x00000200;
-const int ROUGHNESS_MAP_BIT = 0x00000400;
-const int NORMAL_MAP_BIT = 0x00001000;
-const int OCCLUSION_MAP_BIT = 0x00002000;
-const int LIGHTMAP_MAP_BIT = 0x00004000;
+const int EMISSIVE_MAP_BIT              = 0x00000020;
+const int ALBEDO_MAP_BIT                = 0x00000040;
+const int OPACITY_MASK_MAP_BIT          = 0x00000080;
+const int OPACITY_TRANSLUCENT_MAP_BIT   = 0x00000100;
+const int METALLIC_MAP_BIT              = 0x00000200;
+const int ROUGHNESS_MAP_BIT             = 0x00000400;
+const int NORMAL_MAP_BIT                = 0x00000800;
+const int OCCLUSION_MAP_BIT             = 0x00001000;
+const int LIGHTMAP_MAP_BIT              = 0x00002000;
 
 
 <@endif@>
diff --git a/libraries/render-utils/src/MaterialTextures.slh b/libraries/render-utils/src/MaterialTextures.slh
index b25876af63..34bb7c429b 100644
--- a/libraries/render-utils/src/MaterialTextures.slh
+++ b/libraries/render-utils/src/MaterialTextures.slh
@@ -114,9 +114,9 @@ vec3 fetchLightmapMap(vec2 uv) {
 
 <@func evalMaterialOpacity(fetchedOpacity, materialOpacity, matKey, opacity)@>
 {
-    const float OPACITY_MASK_THRESHOLD = 0.95;
+    const float OPACITY_MASK_THRESHOLD = 0.5;
     <$opacity$> = (((<$matKey$> & (OPACITY_TRANSLUCENT_MAP_BIT | OPACITY_MASK_MAP_BIT)) != 0) ?
-                     (((<$matKey$> & OPACITY_MASK_MAP_BIT) != 0) ? step(<$fetchedOpacity$>, OPACITY_MASK_THRESHOLD) : <$fetchedOpacity$>) :
+                     (((<$matKey$> & OPACITY_MASK_MAP_BIT) != 0) ? step(OPACITY_MASK_THRESHOLD, <$fetchedOpacity$>) : <$fetchedOpacity$>) :
                      1.0) * <$materialOpacity$>;
 }
 <@endfunc@>