From d38737e2cb5a3bb9fcf43a37e9a87ddb2e355ec5 Mon Sep 17 00:00:00 2001 From: Jeremy Hu Date: Wed, 10 Jul 2019 21:39:46 +0930 Subject: [PATCH] Fix mesh preview flashback Before this commit, outdated textured preview will override the updated untextured preview. From user's perspective, sometimes, the mesh preview flashback for a short time span, although it will finally been updated to the latest. --- src/document.cpp | 5 ++++- src/document.h | 2 ++ src/documentwindow.cpp | 17 ++++++++++++++--- src/documentwindow.h | 1 + src/meshgenerator.cpp | 11 +++++++++++ src/meshgenerator.h | 3 +++ src/meshloader.cpp | 12 ++++++++++++ src/meshloader.h | 3 +++ src/outcome.h | 1 + 9 files changed, 51 insertions(+), 4 deletions(-) diff --git a/src/document.cpp b/src/document.cpp index e1afd4ce..c2ea7056 100644 --- a/src/document.cpp +++ b/src/document.cpp @@ -59,7 +59,9 @@ Document::Document() : m_posePreviewsGenerator(nullptr), m_currentRigSucceed(false), m_materialPreviewsGenerator(nullptr), - m_motionsGenerator(nullptr) + m_motionsGenerator(nullptr), + m_meshGenerationId(0), + m_nextMeshGenerationId(1) { connect(&Preferences::instance(), &Preferences::partColorChanged, this, &Document::applyPreferencePartColorChange); connect(&Preferences::instance(), &Preferences::flatShadingChanged, this, &Document::applyPreferenceFlatShadingChange); @@ -1754,6 +1756,7 @@ void Document::generateMesh() toSnapshot(snapshot); resetDirtyFlags(); m_meshGenerator = new MeshGenerator(snapshot); + m_meshGenerator->setId(m_nextMeshGenerationId++); m_meshGenerator->setDefaultPartColor(Preferences::instance().partColor()); m_meshGenerator->setGeneratedCacheContext(&m_generatedCacheContext); if (!m_smoothNormal) { diff --git a/src/document.h b/src/document.h index 89fe8a66..52bf7987 100644 --- a/src/document.h +++ b/src/document.h @@ -689,6 +689,8 @@ private: // need initialize bool m_currentRigSucceed; MaterialPreviewsGenerator *m_materialPreviewsGenerator; MotionsGenerator *m_motionsGenerator; + quint64 m_meshGenerationId; + quint64 m_nextMeshGenerationId; private: static unsigned long m_maxSnapshot; std::deque m_undoItems; diff --git a/src/documentwindow.cpp b/src/documentwindow.cpp index 9a6dd9dc..d330f309 100644 --- a/src/documentwindow.cpp +++ b/src/documentwindow.cpp @@ -118,7 +118,8 @@ DocumentWindow::DocumentWindow() : m_documentSaved(true), m_exportPreviewWidget(nullptr), m_preferencesWidget(nullptr), - m_isLastMeshGenerationSucceed(true) + m_isLastMeshGenerationSucceed(true), + m_currentUpdatedMeshId(0) { if (!g_logBrowser) { g_logBrowser = new LogBrowser; @@ -920,11 +921,21 @@ DocumentWindow::DocumentWindow() : connect(m_document, &Document::resultTextureChanged, [=]() { if (m_document->isMeshGenerating()) return; - m_modelRenderWidget->updateMesh(m_document->takeResultTextureMesh()); + auto resultTextureMesh = m_document->takeResultTextureMesh(); + if (nullptr != resultTextureMesh) { + if (resultTextureMesh->meshId() < m_currentUpdatedMeshId) { + delete resultTextureMesh; + return; + } + } + m_modelRenderWidget->updateMesh(resultTextureMesh); }); connect(m_document, &Document::resultMeshChanged, [=]() { - m_modelRenderWidget->updateMesh(m_document->takeResultMesh()); + auto resultMesh = m_document->takeResultMesh(); + if (nullptr != resultMesh) + m_currentUpdatedMeshId = resultMesh->meshId(); + m_modelRenderWidget->updateMesh(resultMesh); }); connect(m_document, &Document::posesChanged, m_document, &Document::generateMotions); diff --git a/src/documentwindow.h b/src/documentwindow.h index 360b0377..b650d269 100644 --- a/src/documentwindow.h +++ b/src/documentwindow.h @@ -80,6 +80,7 @@ private: PreferencesWidget *m_preferencesWidget; std::vector m_dialogs; bool m_isLastMeshGenerationSucceed; + quint64 m_currentUpdatedMeshId; private: QString m_currentFilename; diff --git a/src/meshgenerator.cpp b/src/meshgenerator.cpp index ec5bca8a..8eb8bb46 100644 --- a/src/meshgenerator.cpp +++ b/src/meshgenerator.cpp @@ -31,6 +31,16 @@ MeshGenerator::~MeshGenerator() delete m_nodesCutFaces; } +void MeshGenerator::setId(quint64 id) +{ + m_id = id; +} + +quint64 MeshGenerator::id() +{ + return m_id; +} + bool MeshGenerator::isSucceed() { return m_isSucceed; @@ -1056,6 +1066,7 @@ void MeshGenerator::generate() countTimeConsumed.start(); m_outcome = new Outcome; + m_outcome->meshId = m_id; //m_cutFaceTransforms = new std::map; //m_nodesCutFaces = new std::map>; diff --git a/src/meshgenerator.h b/src/meshgenerator.h index ce4c296e..34edf9c7 100644 --- a/src/meshgenerator.h +++ b/src/meshgenerator.h @@ -66,6 +66,8 @@ public: void setGeneratedCacheContext(GeneratedCacheContext *cacheContext); void setSmoothShadingThresholdAngleDegrees(float degrees); void setDefaultPartColor(const QColor &color); + void setId(quint64 id); + quint64 id(); signals: void finished(); public slots: @@ -91,6 +93,7 @@ private: float m_smoothShadingThresholdAngleDegrees = 60; std::map *m_cutFaceTransforms = nullptr; std::map> *m_nodesCutFaces = nullptr; + quint64 m_id = 0; void collectParts(); bool checkIsComponentDirty(const QString &componentIdString); diff --git a/src/meshloader.cpp b/src/meshloader.cpp index 8debbde5..8faaaf14 100644 --- a/src/meshloader.cpp +++ b/src/meshloader.cpp @@ -54,6 +54,7 @@ MeshLoader::MeshLoader(const MeshLoader &mesh) : this->m_faces = mesh.m_faces; this->m_triangulatedVertices = mesh.m_triangulatedVertices; this->m_triangulatedFaces = mesh.m_triangulatedFaces; + this->m_meshId = mesh.meshId(); } MeshLoader::MeshLoader(ShaderVertex *triangleVertices, int vertexNum) : @@ -106,6 +107,7 @@ MeshLoader::MeshLoader(Outcome &outcome) : m_edgeVertexCount(0), m_textureImage(nullptr) { + m_meshId = outcome.meshId; m_vertices = outcome.vertices; m_faces = outcome.triangleAndQuads; @@ -349,3 +351,13 @@ void MeshLoader::updateTool(ShaderVertex *toolVertices, int vertexNum) m_toolVertices = toolVertices; m_toolVertexCount = vertexNum; } + +quint64 MeshLoader::meshId() const +{ + return m_meshId; +} + +void MeshLoader::setMeshId(quint64 id) +{ + m_meshId = id; +} diff --git a/src/meshloader.h b/src/meshloader.h index 3ef9a1a1..7c0d6419 100644 --- a/src/meshloader.h +++ b/src/meshloader.h @@ -54,6 +54,8 @@ public: void exportAsObj(const QString &filename); void exportAsObj(QTextStream *textStream); void updateTool(ShaderVertex *toolVertices, int vertexNum); + quint64 meshId() const; + void setMeshId(quint64 id); private: ShaderVertex *m_triangleVertices = nullptr; int m_triangleVertexCount = 0; @@ -71,6 +73,7 @@ private: bool m_hasMetalnessInImage = false; bool m_hasRoughnessInImage = false; bool m_hasAmbientOcclusionInImage = false; + quint64 m_meshId = 0; }; #endif diff --git a/src/outcome.h b/src/outcome.h index 3714a1aa..e5b5ff92 100644 --- a/src/outcome.h +++ b/src/outcome.h @@ -35,6 +35,7 @@ public: std::vector> triangles; std::vector triangleNormals; std::vector triangleColors; + quint64 meshId = 0; const std::vector> *triangleSourceNodes() const {