From 581bbea36ed3326d4d2ced740b49652908cba2df Mon Sep 17 00:00:00 2001
From: Zach Pomerantz <zach@highfidelity.io>
Date: Fri, 25 Mar 2016 11:57:02 -0700
Subject: [PATCH] Accomodate the caching Model

---
 .../src/model-networking/ModelCache.cpp       | 22 +++++++++++++++----
 .../src/model-networking/ModelCache.h         |  3 +++
 2 files changed, 21 insertions(+), 4 deletions(-)

diff --git a/libraries/model-networking/src/model-networking/ModelCache.cpp b/libraries/model-networking/src/model-networking/ModelCache.cpp
index 7c692f1565..d16fef040f 100644
--- a/libraries/model-networking/src/model-networking/ModelCache.cpp
+++ b/libraries/model-networking/src/model-networking/ModelCache.cpp
@@ -242,6 +242,18 @@ const QVariantMap Geometry::getTextures() const {
     return textures;
 }
 
+// FIXME: The materials should only be copied when modified, but the Model currently caches the original
+Geometry::Geometry(const Geometry& geometry) {
+    _geometry = geometry._geometry;
+    _meshes = geometry._meshes;
+    _shapes = geometry._shapes;
+
+    _materials.reserve(geometry._materials.size());
+    for (const auto& material : geometry._materials) {
+        _materials.push_back(std::make_shared<NetworkMaterial>(*material));
+    }
+}
+
 void Geometry::setTextures(const QVariantMap& textureMap) {
     if (_meshes->size() > 0) {
         for (auto& material : _materials) {
@@ -249,10 +261,12 @@ void Geometry::setTextures(const QVariantMap& textureMap) {
             if (std::any_of(material->_textures.cbegin(), material->_textures.cend(),
                 [&textureMap](const NetworkMaterial::Textures::value_type& it) { return it.texture && textureMap.contains(it.name); })) { 
 
-                if (material->isOriginal()) {
-                    // Copy the material to avoid mutating the cached version
-                    material = std::make_shared<NetworkMaterial>(*material);
-                }
+                // FIXME: The Model currently caches the materials (waste of space!)
+                //        so they must be copied in the Geometry copy-ctor
+                // if (material->isOriginal()) {
+                //    // Copy the material to avoid mutating the cached version
+                //    material = std::make_shared<NetworkMaterial>(*material);
+                //}
 
                 material->setTextures(textureMap);
                 _areTexturesLoaded = false;
diff --git a/libraries/model-networking/src/model-networking/ModelCache.h b/libraries/model-networking/src/model-networking/ModelCache.h
index 166ad128f2..dad7883a6a 100644
--- a/libraries/model-networking/src/model-networking/ModelCache.h
+++ b/libraries/model-networking/src/model-networking/ModelCache.h
@@ -56,6 +56,9 @@ class Geometry {
 public:
     using Pointer = std::shared_ptr<Geometry>;
 
+    Geometry() = default;
+    Geometry(const Geometry& geometry);
+
     // Immutable over lifetime
     using NetworkMeshes = std::vector<std::shared_ptr<const NetworkMesh>>;
     using NetworkShapes = std::vector<std::shared_ptr<const NetworkShape>>;