From c4639d77ae62a3fa4436738b0060bf65dc7573bb Mon Sep 17 00:00:00 2001 From: Jeremy Hu Date: Thu, 27 Feb 2020 19:27:09 +0930 Subject: [PATCH] Fix alphaMode of glb exporting Only export alphaMode as BLEND when there are transparent parts. --- src/document.cpp | 3 +++ src/document.h | 1 + src/documentwindow.cpp | 1 + src/glbfile.cpp | 4 +++- src/glbfile.h | 1 + src/texturegenerator.cpp | 12 +++++++++++- src/texturegenerator.h | 2 ++ 7 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/document.cpp b/src/document.cpp index d937d73e..c6cd59cd 100644 --- a/src/document.cpp +++ b/src/document.cpp @@ -37,6 +37,7 @@ Document::Document() : textureMetalnessImage(nullptr), textureRoughnessImage(nullptr), textureAmbientOcclusionImage(nullptr), + textureHasTransparencySettings(false), rigType(RigType::None), weldEnabled(true), polyCount(PolyCount::Original), @@ -2087,6 +2088,8 @@ void Document::textureReady() delete m_resultTextureMesh; m_resultTextureMesh = m_textureGenerator->takeResultMesh(); + textureHasTransparencySettings = m_textureGenerator->hasTransparencySettings(); + //addToolToMesh(m_resultTextureMesh); m_textureImageUpdateVersion++; diff --git a/src/document.h b/src/document.h index ca18ed06..16f27f6d 100644 --- a/src/document.h +++ b/src/document.h @@ -534,6 +534,7 @@ public: // need initialize QImage *textureMetalnessImage; QImage *textureRoughnessImage; QImage *textureAmbientOcclusionImage; + bool textureHasTransparencySettings; RigType rigType; bool weldEnabled; PolyCount polyCount; diff --git a/src/documentwindow.cpp b/src/documentwindow.cpp index 47746852..d50a000c 100644 --- a/src/documentwindow.cpp +++ b/src/documentwindow.cpp @@ -1703,6 +1703,7 @@ void DocumentWindow::exportGlbToFilename(const QString &filename) exportMotions.push_back({motion->name, motion->jointNodeTrees}); } GlbFileWriter glbFileWriter(skeletonResult, m_document->resultRigBones(), m_document->resultRigWeights(), filename, + m_document->textureHasTransparencySettings, m_document->textureImage, m_document->textureNormalImage, m_document->textureMetalnessRoughnessAmbientOcclusionImage, exportMotions.empty() ? nullptr : &exportMotions); glbFileWriter.save(); QApplication::restoreOverrideCursor(); diff --git a/src/glbfile.cpp b/src/glbfile.cpp index 8c4616ae..2381a8cb 100644 --- a/src/glbfile.cpp +++ b/src/glbfile.cpp @@ -26,6 +26,7 @@ GlbFileWriter::GlbFileWriter(Outcome &outcome, const std::vector *resultRigBones, const std::map *resultRigWeights, const QString &filename, + bool textureHasTransparencySettings, QImage *textureImage, QImage *normalImage, QImage *ormImage, @@ -169,7 +170,8 @@ GlbFileWriter::GlbFileWriter(Outcome &outcome, m_json["materials"][primitiveIndex]["pbrMetallicRoughness"]["baseColorTexture"]["index"] = textureIndex++; m_json["materials"][primitiveIndex]["pbrMetallicRoughness"]["metallicFactor"] = MeshLoader::m_defaultMetalness; m_json["materials"][primitiveIndex]["pbrMetallicRoughness"]["roughnessFactor"] = MeshLoader::m_defaultRoughness; - m_json["materials"][primitiveIndex]["alphaMode"] = "BLEND"; + if (textureHasTransparencySettings) + m_json["materials"][primitiveIndex]["alphaMode"] = "BLEND"; if (normalImage) { m_json["materials"][primitiveIndex]["normalTexture"]["index"] = textureIndex++; } diff --git a/src/glbfile.h b/src/glbfile.h index cd5154f1..71cb721d 100644 --- a/src/glbfile.h +++ b/src/glbfile.h @@ -19,6 +19,7 @@ public: const std::vector *resultRigBones, const std::map *resultRigWeights, const QString &filename, + bool textureHasTransparencySettings, QImage *textureImage=nullptr, QImage *normalImage=nullptr, QImage *ormImage=nullptr, diff --git a/src/texturegenerator.cpp b/src/texturegenerator.cpp index 112cf3d9..ca587df7 100644 --- a/src/texturegenerator.cpp +++ b/src/texturegenerator.cpp @@ -24,7 +24,8 @@ TextureGenerator::TextureGenerator(const Outcome &outcome, Snapshot *snapshot) : m_resultTextureMetalnessImage(nullptr), m_resultTextureAmbientOcclusionImage(nullptr), m_resultMesh(nullptr), - m_snapshot(snapshot) + m_snapshot(snapshot), + m_hasTransparencySettings(false) { m_outcome = new Outcome(); *m_outcome = outcome; @@ -210,6 +211,11 @@ void TextureGenerator::prepare() } } +bool TextureGenerator::hasTransparencySettings() +{ + return m_hasTransparencySettings; +} + void TextureGenerator::generate() { m_resultMesh = new MeshLoader(*m_outcome); @@ -240,6 +246,10 @@ void TextureGenerator::generate() std::map, const OutcomeNode *> nodeMap; std::map partColorSolubilityMap; for (const auto &item: m_outcome->nodes) { + if (!m_hasTransparencySettings) { + if (!qFuzzyCompare(1.0, item.color.alphaF())) + m_hasTransparencySettings = true; + } nodeMap.insert({{item.partId, item.nodeId}, &item}); partColorMap.insert({item.partId, item.color}); partColorSolubilityMap.insert({item.partId, item.colorSolubility}); diff --git a/src/texturegenerator.h b/src/texturegenerator.h index 09498b51..eb72c4a0 100644 --- a/src/texturegenerator.h +++ b/src/texturegenerator.h @@ -26,6 +26,7 @@ public: QImage *takeResultTextureAmbientOcclusionImage(); Outcome *takeOutcome(); MeshLoader *takeResultMesh(); + bool hasTransparencySettings(); void addPartColorMap(QUuid partId, const QImage *image, float tileScale); void addPartNormalMap(QUuid partId, const QImage *image, float tileScale); void addPartMetalnessMap(QUuid partId, const QImage *image, float tileScale); @@ -60,6 +61,7 @@ private: std::map> m_partAmbientOcclusionTextureMap; std::set m_countershadedPartIds; Snapshot *m_snapshot; + bool m_hasTransparencySettings; }; #endif