From cef6deeda3883338ec26e1ae58b53a175ffa20db Mon Sep 17 00:00:00 2001 From: huxingyi Date: Mon, 7 Dec 2020 19:47:25 +0930 Subject: [PATCH] Add preference: Interpolation enabling toggle Disabling interpolation would remove the auto generated intermediate nodes, then reduce the generated triangles. --- src/document.cpp | 7 +++++++ src/document.h | 1 + src/meshgenerator.cpp | 9 +++++++-- src/meshgenerator.h | 2 ++ src/preferences.cpp | 23 +++++++++++++++++++++++ src/preferences.h | 4 ++++ src/preferenceswidget.cpp | 8 ++++++++ 7 files changed, 52 insertions(+), 2 deletions(-) diff --git a/src/document.cpp b/src/document.cpp index 1990d7de..3003558e 100644 --- a/src/document.cpp +++ b/src/document.cpp @@ -85,6 +85,7 @@ Document::Document() : connect(&Preferences::instance(), &Preferences::partColorChanged, this, &Document::applyPreferencePartColorChange); connect(&Preferences::instance(), &Preferences::flatShadingChanged, this, &Document::applyPreferenceFlatShadingChange); connect(&Preferences::instance(), &Preferences::textureSizeChanged, this, &Document::applyPreferenceTextureSizeChange); + connect(&Preferences::instance(), &Preferences::interpolationEnabledChanged, this, &Document::applyPreferenceInterpolationChange); } void Document::applyPreferencePartColorChange() @@ -103,6 +104,11 @@ void Document::applyPreferenceTextureSizeChange() generateTexture(); } +void Document::applyPreferenceInterpolationChange() +{ + regenerateMesh(); +} + Document::~Document() { delete m_resultMesh; @@ -2055,6 +2061,7 @@ void Document::generateMesh() m_meshGenerator = new MeshGenerator(snapshot); m_meshGenerator->setId(m_nextMeshGenerationId++); m_meshGenerator->setDefaultPartColor(Preferences::instance().partColor()); + m_meshGenerator->setInterpolationEnabled(Preferences::instance().interpolationEnabled()); if (nullptr == m_generatedCacheContext) m_generatedCacheContext = new GeneratedCacheContext; m_meshGenerator->setGeneratedCacheContext(m_generatedCacheContext); diff --git a/src/document.h b/src/document.h index a99d7d1a..3698b950 100644 --- a/src/document.h +++ b/src/document.h @@ -633,6 +633,7 @@ public slots: void applyPreferencePartColorChange(); void applyPreferenceFlatShadingChange(); void applyPreferenceTextureSizeChange(); + void applyPreferenceInterpolationChange(); void initScript(const QString &script); void updateScript(const QString &script); void runScript(); diff --git a/src/meshgenerator.cpp b/src/meshgenerator.cpp index a7cabee9..e5f8afef 100644 --- a/src/meshgenerator.cpp +++ b/src/meshgenerator.cpp @@ -1021,11 +1021,11 @@ MeshCombiner::Mesh *MeshGenerator::combineComponentMesh(const QString &component QString partIdString = valueOfKeyInMapOrEmpty(*component, "linkData"); bool hasError = false; bool retryable = true; - mesh = combinePartMesh(partIdString, &hasError, &retryable); + mesh = combinePartMesh(partIdString, &hasError, &retryable, m_interpolationEnabled); if (hasError) { delete mesh; mesh = nullptr; - if (retryable) { + if (retryable && m_interpolationEnabled) { hasError = false; qDebug() << "Try combine part again without adding intermediate nodes"; mesh = combinePartMesh(partIdString, &hasError, &retryable, false); @@ -1411,6 +1411,11 @@ void MeshGenerator::setSmoothShadingThresholdAngleDegrees(float degrees) m_smoothShadingThresholdAngleDegrees = degrees; } +void MeshGenerator::setInterpolationEnabled(bool interpolationEnabled) +{ + m_interpolationEnabled = interpolationEnabled; +} + void MeshGenerator::process() { generate(); diff --git a/src/meshgenerator.h b/src/meshgenerator.h index d284d687..01f62f09 100644 --- a/src/meshgenerator.h +++ b/src/meshgenerator.h @@ -96,6 +96,7 @@ public: void generate(); void setGeneratedCacheContext(GeneratedCacheContext *cacheContext); void setSmoothShadingThresholdAngleDegrees(float degrees); + void setInterpolationEnabled(bool interpolationEnabled); void setDefaultPartColor(const QColor &color); void setId(quint64 id); void setWeldEnabled(bool enabled); @@ -130,6 +131,7 @@ private: std::vector m_clothCollisionVertices; std::vector> m_clothCollisionTriangles; bool m_weldEnabled = true; + bool m_interpolationEnabled = true; void collectParts(); void collectIncombinableComponentMeshes(const QString &componentIdString); diff --git a/src/preferences.cpp b/src/preferences.cpp index 5ae5a0dc..362f9b80 100644 --- a/src/preferences.cpp +++ b/src/preferences.cpp @@ -19,6 +19,7 @@ void Preferences::loadDefault() m_toonLine = ToonLine::WithoutLine; m_textureSize = 1024; m_scriptEnabled = false; + m_interpolationEnabled = true; } Preferences::Preferences() @@ -65,6 +66,13 @@ Preferences::Preferences() else m_scriptEnabled = isTrueValueString(value); } + { + QString value = m_settings.value("interpolationEnabled").toString(); + if (value.isEmpty()) + m_interpolationEnabled = true; + else + m_interpolationEnabled = isTrueValueString(value); + } } CombineMode Preferences::componentCombineMode() const @@ -87,6 +95,11 @@ bool Preferences::scriptEnabled() const return m_scriptEnabled; } +bool Preferences::interpolationEnabled() const +{ + return m_interpolationEnabled; +} + bool Preferences::toonShading() const { return m_toonShading; @@ -138,6 +151,15 @@ void Preferences::setScriptEnabled(bool enabled) emit scriptEnabledChanged(); } +void Preferences::setInterpolationEnabled(bool enabled) +{ + if (m_interpolationEnabled == enabled) + return; + m_interpolationEnabled = enabled; + m_settings.setValue("interpolationEnabled", enabled ? "true" : "false"); + emit interpolationEnabledChanged(); +} + void Preferences::setToonShading(bool toonShading) { if (m_toonShading == toonShading) @@ -186,4 +208,5 @@ void Preferences::reset() emit toonLineChanged(); emit textureSizeChanged(); emit scriptEnabledChanged(); + emit interpolationEnabledChanged(); } diff --git a/src/preferences.h b/src/preferences.h index 4810d91b..8bcf063f 100644 --- a/src/preferences.h +++ b/src/preferences.h @@ -16,6 +16,7 @@ public: const QColor &partColor() const; bool flatShading() const; bool scriptEnabled() const; + bool interpolationEnabled() const; bool toonShading() const; ToonLine toonLine() const; QSize documentWindowSize() const; @@ -28,6 +29,7 @@ signals: void toonShadingChanged(); void toonLineChanged(); void textureSizeChanged(); + void interpolationEnabledChanged(); void scriptEnabledChanged(); public slots: void setComponentCombineMode(CombineMode mode); @@ -37,6 +39,7 @@ public slots: void setToonLine(ToonLine toonLine); void setTextureSize(int textureSize); void setScriptEnabled(bool enabled); + void setInterpolationEnabled(bool enabled); void reset(); private: CombineMode m_componentCombineMode; @@ -47,6 +50,7 @@ private: QSettings m_settings; int m_textureSize; bool m_scriptEnabled; + bool m_interpolationEnabled; private: void loadDefault(); }; diff --git a/src/preferenceswidget.cpp b/src/preferenceswidget.cpp index 52b5a375..7b762ed4 100644 --- a/src/preferenceswidget.cpp +++ b/src/preferenceswidget.cpp @@ -85,6 +85,12 @@ PreferencesWidget::PreferencesWidget(const Document *document, QWidget *parent) toonShadingLayout->addWidget(toonLineSelectBox); toonShadingLayout->addStretch(); + QCheckBox *interpolationEnabledBox = new QCheckBox(); + Theme::initCheckbox(interpolationEnabledBox); + connect(interpolationEnabledBox, &QCheckBox::stateChanged, this, [=]() { + Preferences::instance().setInterpolationEnabled(interpolationEnabledBox->isChecked()); + }); + QComboBox *textureSizeSelectBox = new QComboBox; textureSizeSelectBox->addItem("512"); textureSizeSelectBox->addItem("1024"); @@ -104,6 +110,7 @@ PreferencesWidget::PreferencesWidget(const Document *document, QWidget *parent) formLayout->addRow(tr("Part color:"), colorLayout); formLayout->addRow(tr("Combine mode:"), combineModeSelectBox); formLayout->addRow(tr("Flat shading:"), flatShadingBox); + formLayout->addRow(tr("Interpolation:"), interpolationEnabledBox); formLayout->addRow(tr("Toon shading:"), toonShadingLayout); formLayout->addRow(tr("Texture size:"), textureSizeSelectBox); formLayout->addRow(tr("Script:"), scriptEnabledBox); @@ -117,6 +124,7 @@ PreferencesWidget::PreferencesWidget(const Document *document, QWidget *parent) textureSizeSelectBox->setCurrentIndex( textureSizeSelectBox->findText(QString::number(Preferences::instance().textureSize())) ); + interpolationEnabledBox->setChecked(Preferences::instance().interpolationEnabled()); scriptEnabledBox->setChecked(Preferences::instance().scriptEnabled()); };