diff --git a/src/Assets/AssetManager.cpp b/src/Assets/AssetManager.cpp index 1476b4a6..0ca523cc 100644 --- a/src/Assets/AssetManager.cpp +++ b/src/Assets/AssetManager.cpp @@ -194,10 +194,9 @@ AssetManager::getAvailableAssetsTreeFilteredRecursive(const AvailableAssetsNode * @return material that should be used. */ std::shared_ptr AssetManager::registerMaterial(std::shared_ptr material) { - auto foundIt = materials.find(material->getHash()); + const auto foundIt = materials.find(material->getOriginalHash()); if(foundIt != materials.end()) { //std::cerr << "Material found, return "<< material->getName() << std::endl; - foundIt->second.second++; return foundIt->second.first; } @@ -208,7 +207,7 @@ std::shared_ptr AssetManager::registerMaterial(std::shared_ptr material) { - auto materialIt = materials.find(material->getHash()); + auto materialIt = materials.find(material->getOriginalHash()); if(materialIt == materials.end()) { std::cerr << "Unregister for non existent material found!" << std::endl; } diff --git a/src/Assets/ModelAsset.cpp b/src/Assets/ModelAsset.cpp index cdad4f96..d4c3c098 100755 --- a/src/Assets/ModelAsset.cpp +++ b/src/Assets/ModelAsset.cpp @@ -309,9 +309,9 @@ std::shared_ptr ModelAsset::loadMaterials(const aiScene *scene, unsign } newMaterial->setMaps(maps); - std::string requestedName = newMaterial->getName(); - + newMaterial->calculateOriginalHash(); newMaterial = assetManager->registerMaterial(newMaterial); + std::string requestedName = newMaterial->getName(); materialMap[requestedName] = newMaterial; } else { newMaterial = materialMap[property.C_Str()]; diff --git a/src/Editor/Editor.cpp b/src/Editor/Editor.cpp index 35b6500c..1de5e8a8 100644 --- a/src/Editor/Editor.cpp +++ b/src/Editor/Editor.cpp @@ -22,6 +22,8 @@ #include "AI/AIMovementGrid.h" +std::shared_ptr EditorNS::selectedMaterial = nullptr; + Editor::Editor(World *world) : world(world){ backgroundRenderStage = std::make_unique(world->graphicsWrapper, 640,480,"","",true,true,true,false,false); colorTexture = std::make_shared(world->graphicsWrapper, GraphicsInterface::TextureTypes::T2D, GraphicsInterface::InternalFormatTypes::RGBA, GraphicsInterface::FormatTypes::RGBA, GraphicsInterface::DataTypes::UNSIGNED_BYTE, 640, 480); @@ -671,11 +673,15 @@ void Editor::renderEditor() { if(ImGui::CollapsingHeader("List materials")) { //listing static size_t selectedHash = 0; + if (EditorNS::selectedMaterial != nullptr) { + selectedHash = EditorNS::selectedMaterial->getHash(); + EditorNS::selectedMaterial = nullptr; + } bool isSelected = false; auto allMaterials = world->assetManager->getMaterials(); ImGui::Text("Total material count is %llu", allMaterials.size()); ImGui::BeginListBox("Materials"); - for (auto it = allMaterials.begin(); it != allMaterials.end(); it++) { + for (auto it = allMaterials.begin(); it != allMaterials.end(); ++it) { isSelected = selectedHash == it->first; if (ImGui::Selectable((it->second.first->getName() + " -> " + std::to_string(it->first)).c_str(), isSelected)) { selectedHash = it->first; @@ -684,7 +690,6 @@ void Editor::renderEditor() { ImGui::EndListBox(); auto selectedMaterialIt = allMaterials.find(selectedHash); if(selectedMaterialIt != allMaterials.end()) { - selectedMaterialIt->second.first->addImGuiEditorElements(*world->request); } } diff --git a/src/Editor/Editor.h b/src/Editor/Editor.h index 073532ec..e3de27c4 100644 --- a/src/Editor/Editor.h +++ b/src/Editor/Editor.h @@ -16,7 +16,12 @@ class GraphicsPipelineStage; class Model; class GraphicsProgram; class ImGuiImageWrapper; +class Material; +namespace EditorNS { + //This is used as a global variable store. For multiple windows, ImGui doesn't provide anything else + extern std::shared_ptr selectedMaterial; +} class Editor { World* world; std::shared_ptr colorTexture; diff --git a/src/GameObjects/Model.cpp b/src/GameObjects/Model.cpp index b31f8569..f29b470d 100644 --- a/src/GameObjects/Model.cpp +++ b/src/GameObjects/Model.cpp @@ -8,6 +8,7 @@ #include "GamePlay/APISerializer.h" #include "Utils/HardCodedTags.h" #include +#include #ifdef CEREAL_SUPPORT #include @@ -410,13 +411,21 @@ ImGuiResult Model::addImGuiEditorElements(const ImGuiRequest &request) { } } //add material listing - static uint32_t selectedIndex = 0; + static int32_t selectedIndex = -1; + static uint32_t selectedModel = 0; + if (this->getWorldObjectID() != selectedModel) { + selectedIndex = -1; + selectedModel = this->getWorldObjectID(); + } bool isSelected = false; if(ImGui::BeginListBox("Meshes##GODWHY")) { for (size_t i = 0; i < meshMetaData.size(); ++i) { - isSelected = selectedIndex == i; + isSelected = selectedIndex == static_cast(i); if (ImGui::Selectable((meshMetaData[i]->mesh->getName() + " -> " + meshMetaData[i]->material->getName()).c_str(), isSelected)) { - selectedIndex = i; + if (selectedIndex != static_cast(i)) { //means selection changed, trigger material change on main window + EditorNS::selectedMaterial = meshMetaData[i]->mesh->getMaterial(); + } + selectedIndex = static_cast(i); } } ImGui::EndListBox(); diff --git a/src/Material.cpp b/src/Material.cpp index 3db46211..39fc8083 100644 --- a/src/Material.cpp +++ b/src/Material.cpp @@ -78,3 +78,7 @@ size_t Material::getHash() const { std::hash hashGenerator; return hashGenerator(*this); } + +size_t Material::getOriginalHash() const { + return originalHash; +} diff --git a/src/Material.h b/src/Material.h index c6b253e9..78c19c10 100644 --- a/src/Material.h +++ b/src/Material.h @@ -19,22 +19,24 @@ class Material { private: - AssetManager *assetManager; std::string name; - float specularExponent = 0; - uint32_t materialIndex; - uint32_t maps = 0; - bool deserialized = false; glm::vec3 ambientColor; glm::vec3 diffuseColor; glm::vec3 specularColor; + + uint32_t materialIndex; + uint32_t maps = 0; + AssetManager *assetManager; + size_t originalHash = 0;//With this, we can still match assets loaded after the material is changed + float specularExponent = 0; + float refractionIndex = 0; + bool deserialized = false; bool isAmbientMap = false; bool isDiffuseMap = false; bool isSpecularMap = false; bool isNormalMap = false; bool isOpacityMap = false; - float refractionIndex = 0; /** * This is a list of texture names. @@ -62,23 +64,23 @@ class Material { public: Material(AssetManager *assetManager, const std::string &name, uint32_t materialIndex, float specularExponent, const glm::vec3 &ambientColor, const glm::vec3 &diffuseColor, const glm::vec3 &specularColor, float refractionIndex) - : assetManager(assetManager), - name(name), - specularExponent(specularExponent), - materialIndex(materialIndex), + : name(name), ambientColor(ambientColor), diffuseColor(diffuseColor), specularColor(specularColor), + materialIndex(materialIndex), + assetManager(assetManager), + specularExponent(specularExponent), refractionIndex(refractionIndex) { } Material(AssetManager *assetManager, const std::string &name, uint32_t materialIndex) - : assetManager(assetManager), - name(name), - materialIndex(materialIndex), + : name(name), ambientColor(glm::vec3(0,0,0)), diffuseColor(glm::vec3(0,0,0)), - specularColor(glm::vec3(0,0,0)) { } + specularColor(glm::vec3(0,0,0)), + materialIndex(materialIndex), + assetManager(assetManager) { } void loadGPUSide(AssetManager *assetManager); @@ -260,6 +262,11 @@ class Material { } size_t getHash() const; + size_t getOriginalHash() const; + void calculateOriginalHash() { + this->originalHash = getHash(); + } + ImGuiResult addImGuiEditorElements(const ImGuiRequest &request);