Add a toon shading option box to Preferences Widget [skip ci]

master
huxingyi 2020-04-08 19:39:16 +09:30
parent 63cbfa83a5
commit 62e411d4e5
13 changed files with 126 additions and 5 deletions

View File

@ -135,6 +135,9 @@ include(thirdparty/qtsingleapplication/src/qtsingleapplication.pri)
INCLUDEPATH += src
SOURCES += src/toonline.cpp
HEADERS += src/toonline.h
SOURCES += src/meshstroketifier.cpp
HEADERS += src/meshstroketifier.h

View File

@ -537,11 +537,13 @@ vec4 metalRoughFunction(const in vec4 baseColor,
else
cLinear = hsv2rgb(vec3(hsv.r, hsv.g, hsv.b * 0.1));
if (toonEdgeEnabled == 1) {
if (toonEdgeEnabled > 0) {
float depthEdge = depthEdgeSobel();
float normalEdge = normalEdgeSobel();
if (depthEdge >= 0.009 || normalEdge >= 0.6) {
cLinear = hsv2rgb(vec3(hsv.r, hsv.g, hsv.b * 0.02));
} else if (toonEdgeEnabled == 2) {
return vec4(0.0, 0.0, 0.0, 0.0);
}
}
}

View File

@ -425,11 +425,13 @@ vec4 metalRoughFunction(const in vec4 baseColor,
else
cLinear = hsv2rgb(vec3(hsv.r, hsv.g, hsv.b * 0.1));
if (toonEdgeEnabled == 1) {
if (toonEdgeEnabled > 0) {
float depthEdge = depthEdgeSobel();
float normalEdge = normalEdgeSobel();
if (depthEdge >= 0.009 || normalEdge >= 0.6) {
cLinear = hsv2rgb(vec3(hsv.r, hsv.g, hsv.b * 0.02));
} else if (toonEdgeEnabled == 2) {
return vec4(0.0, 0.0, 0.0, 0.0);
}
}
}

View File

@ -2135,7 +2135,8 @@ void DocumentWindow::delayedGenerateNormalAndDepthMaps()
{
if (!Preferences::instance().toonShading())
return;
if (ToonLine::WithoutLine == Preferences::instance().toonLine())
return;
generateNormalAndDepthMaps();
}

View File

@ -8,6 +8,7 @@
#include <QSurfaceFormat>
#include "modelmeshbinder.h"
#include "ddsfile.h"
#include "preferences.h"
ModelMeshBinder::ModelMeshBinder(bool toolEnabled) :
m_toolEnabled(toolEnabled)
@ -321,7 +322,7 @@ void ModelMeshBinder::paint(ModelShaderProgram *program)
if (nullptr != m_toonNormalMap && nullptr != m_toonDepthMap) {
m_toonNormalMap->bind(5);
m_toonDepthMap->bind(6);
program->setUniformValue(program->toonEdgeEnabledLoc(), 1);
program->setUniformValue(program->toonEdgeEnabledLoc(), (int)Preferences::instance().toonLine());
}
f->glDrawArrays(GL_TRIANGLES, 0, m_renderTriangleVertexCount);
}

View File

@ -180,7 +180,9 @@ QImage ModelOffscreenRender::toImage(const QSize &size)
qDebug() << "bindDefault end";
delete renderFbo;
qDebug() << "doneCurrent begin...";
m_context->doneCurrent();
qDebug() << "doneCurrent end";
delete m_context;
m_context = nullptr;

View File

@ -53,6 +53,7 @@ ModelWidget::ModelWidget(QWidget *parent) :
zoom(200);
connect(&Preferences::instance(), &Preferences::toonShadingChanged, this, &ModelWidget::reRender);
connect(&Preferences::instance(), &Preferences::toonLineChanged, this, &ModelWidget::reRender);
}
void ModelWidget::reRender()

View File

@ -47,8 +47,13 @@ NormalAndDepthMapsGenerator::~NormalAndDepthMapsGenerator()
void NormalAndDepthMapsGenerator::generate()
{
qDebug() << "Normap map generation begin...";
m_normalMap = new QImage(m_normalMapRender->toImage(m_viewPortSize));
qDebug() << "Normap map generation done";
qDebug() << "Depth map generation begin...";
m_depthMap = new QImage(m_depthMapRender->toImage(m_viewPortSize));
qDebug() << "Depth map generation done";
}
void NormalAndDepthMapsGenerator::process()

View File

@ -16,6 +16,7 @@ void Preferences::loadDefault()
m_partColor = Qt::white;
m_flatShading = false;
m_toonShading = false;
m_toonLine = ToonLine::WithoutLine;
m_textureSize = 1024;
}
@ -46,6 +47,11 @@ Preferences::Preferences()
else
m_toonShading = isTrueValueString(value);
}
{
QString value = m_settings.value("toonLine").toString();
if (!value.isEmpty())
m_toonLine = ToonLineFromString(value.toUtf8().constData());
}
{
QString value = m_settings.value("textureSize").toString();
if (!value.isEmpty())
@ -73,6 +79,11 @@ bool Preferences::toonShading() const
return m_toonShading;
}
ToonLine Preferences::toonLine() const
{
return m_toonLine;
}
int Preferences::textureSize() const
{
return m_textureSize;
@ -114,6 +125,15 @@ void Preferences::setToonShading(bool toonShading)
emit toonShadingChanged();
}
void Preferences::setToonLine(ToonLine toonLine)
{
if (m_toonLine == toonLine)
return;
m_toonLine = toonLine;
m_settings.setValue("toonLine", ToonLineToString(m_toonLine));
emit toonLineChanged();
}
void Preferences::setTextureSize(int textureSize)
{
if (m_textureSize == textureSize)
@ -141,5 +161,6 @@ void Preferences::reset()
emit partColorChanged();
emit flatShadingChanged();
emit toonShadingChanged();
emit toonLineChanged();
emit textureSizeChanged();
}

View File

@ -4,6 +4,7 @@
#include <QColor>
#include <QSize>
#include "combinemode.h"
#include "toonline.h"
class Preferences : public QObject
{
@ -15,6 +16,7 @@ public:
const QColor &partColor() const;
bool flatShading() const;
bool toonShading() const;
ToonLine toonLine() const;
QSize documentWindowSize() const;
void setDocumentWindowSize(const QSize&);
int textureSize() const;
@ -23,12 +25,14 @@ signals:
void partColorChanged();
void flatShadingChanged();
void toonShadingChanged();
void toonLineChanged();
void textureSizeChanged();
public slots:
void setComponentCombineMode(CombineMode mode);
void setPartColor(const QColor &color);
void setFlatShading(bool flatShading);
void setToonShading(bool toonShading);
void setToonLine(ToonLine toonLine);
void setTextureSize(int textureSize);
void reset();
private:
@ -36,6 +40,7 @@ private:
QColor m_partColor;
bool m_flatShading;
bool m_toonShading;
ToonLine m_toonLine;
QSettings m_settings;
int m_textureSize;
private:

View File

@ -71,6 +71,20 @@ PreferencesWidget::PreferencesWidget(const Document *document, QWidget *parent)
Preferences::instance().setToonShading(toonShadingBox->isChecked());
});
QComboBox *toonLineSelectBox = new QComboBox;
for (size_t i = 0; i < (size_t)ToonLine::Count; ++i) {
ToonLine toonLine = (ToonLine)i;
toonLineSelectBox->addItem(ToonLineToDispName(toonLine));
}
connect(toonLineSelectBox, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this, [=](int index) {
Preferences::instance().setToonLine((ToonLine)index);
});
QHBoxLayout *toonShadingLayout = new QHBoxLayout;
toonShadingLayout->addWidget(toonShadingBox);
toonShadingLayout->addWidget(toonLineSelectBox);
toonShadingLayout->addStretch();
QComboBox *textureSizeSelectBox = new QComboBox;
textureSizeSelectBox->addItem("512");
textureSizeSelectBox->addItem("1024");
@ -84,13 +98,14 @@ 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("Toon shading:"), toonShadingBox);
formLayout->addRow(tr("Toon shading:"), toonShadingLayout);
formLayout->addRow(tr("Texture size:"), textureSizeSelectBox);
auto loadFromPreferences = [=]() {
updatePickButtonColor();
combineModeSelectBox->setCurrentIndex((int)Preferences::instance().componentCombineMode());
flatShadingBox->setChecked(Preferences::instance().flatShading());
toonLineSelectBox->setCurrentIndex((int)Preferences::instance().toonLine());
toonShadingBox->setChecked(Preferences::instance().toonShading());
textureSizeSelectBox->setCurrentIndex(
textureSizeSelectBox->findText(QString::number(Preferences::instance().textureSize()))

6
src/toonline.cpp Normal file
View File

@ -0,0 +1,6 @@
#include <QObject>
#include "toonline.h"
IMPL_ToonLineToString
IMPL_ToonLineFromString
IMPL_ToonLineToDispName

57
src/toonline.h Normal file
View File

@ -0,0 +1,57 @@
#ifndef DUST3D_TOON_LINE_H
#define DUST3D_TOON_LINE_H
#include <QString>
enum class ToonLine
{
WithoutLine = 0,
WithLine,
LineOnly,
Count
};
ToonLine ToonLineFromString(const char *lineString);
#define IMPL_ToonLineFromString \
ToonLine ToonLineFromString(const char *lineString) \
{ \
QString line = lineString; \
if (line == "WithoutLine") \
return ToonLine::WithoutLine; \
if (line == "WithLine") \
return ToonLine::WithLine; \
if (line == "LineOnly") \
return ToonLine::LineOnly; \
return ToonLine::WithoutLine; \
}
QString ToonLineToString(ToonLine toonLine);
#define IMPL_ToonLineToString \
QString ToonLineToString(ToonLine toonLine) \
{ \
switch (toonLine) { \
case ToonLine::WithoutLine: \
return "WithoutLine"; \
case ToonLine::WithLine: \
return "WithLine"; \
case ToonLine::LineOnly: \
return "LineOnly"; \
default: \
return ""; \
} \
}
QString ToonLineToDispName(ToonLine toonLine);
#define IMPL_ToonLineToDispName \
QString ToonLineToDispName(ToonLine toonLine) \
{ \
switch (toonLine) { \
case ToonLine::WithoutLine: \
return QObject::tr("Without Line"); \
case ToonLine::WithLine: \
return QObject::tr("With Line"); \
case ToonLine::LineOnly: \
return QObject::tr("Line Only"); \
default: \
return QObject::tr("Without Line"); \
} \
}
#endif