ShaderNode: Add possibility to set variable name (+ force variables)
This commit is contained in:
parent
eabb8a630d
commit
0a0dce4109
|
|
@ -4,5 +4,6 @@ inline FragmentOutput::FragmentOutput(ShaderGraph& graph) :
|
|||
ShaderNode(graph)
|
||||
{
|
||||
SetPreviewSize({ 128, 128 });
|
||||
DisableCustomVariableName();
|
||||
EnablePreview(true);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -21,6 +21,7 @@ ShaderNode(graph)
|
|||
m_currentInputText = firstInput.name;
|
||||
}
|
||||
|
||||
DisableCustomVariableName();
|
||||
UpdatePreview();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -3,12 +3,13 @@
|
|||
#include <QtWidgets/QComboBox>
|
||||
#include <QtWidgets/QFormLayout>
|
||||
#include <QtWidgets/QLabel>
|
||||
#include <QtWidgets/QLineEdit>
|
||||
|
||||
ShaderNode::ShaderNode(ShaderGraph& graph) :
|
||||
m_previewSize(64, 64),
|
||||
m_pixmapLabel(nullptr),
|
||||
m_graph(graph),
|
||||
m_forceVariable(false),
|
||||
m_enableCustomVariableName(true),
|
||||
m_isPreviewEnabled(false)
|
||||
{
|
||||
m_pixmapLabel = new QLabel;
|
||||
|
|
@ -51,6 +52,16 @@ void ShaderNode::BuildNodeEdition(QFormLayout* layout)
|
|||
});
|
||||
|
||||
layout->addRow(tr("Preview size"), previewSize);
|
||||
|
||||
if (m_enableCustomVariableName)
|
||||
{
|
||||
QLineEdit* lineEdit = new QLineEdit(QString::fromStdString(m_variableName));
|
||||
connect(lineEdit, &QLineEdit::textChanged, [&](const QString& text)
|
||||
{
|
||||
SetVariableName(text.toStdString());
|
||||
});
|
||||
layout->addRow(tr("Variable name"), lineEdit);
|
||||
}
|
||||
}
|
||||
|
||||
void ShaderNode::EnablePreview(bool enable)
|
||||
|
|
|
|||
|
|
@ -27,13 +27,16 @@ class ShaderNode : public QtNodes::NodeDataModel
|
|||
inline const ShaderGraph& GetGraph() const;
|
||||
inline const std::string& GetVariableName() const;
|
||||
|
||||
void SetPreviewSize(const Nz::Vector2i& size);
|
||||
inline void SetPreviewSize(const Nz::Vector2i& size);
|
||||
inline void SetVariableName(std::string variableName);
|
||||
|
||||
QWidget* embeddedWidget() final;
|
||||
|
||||
void setInData(std::shared_ptr<QtNodes::NodeData>, int) override;
|
||||
|
||||
protected:
|
||||
inline void DisableCustomVariableName();
|
||||
inline void EnableCustomVariableName(bool enable = true);
|
||||
void UpdatePreview();
|
||||
|
||||
private:
|
||||
|
|
@ -44,7 +47,7 @@ class ShaderNode : public QtNodes::NodeDataModel
|
|||
std::optional<QPixmap> m_pixmap;
|
||||
std::string m_variableName;
|
||||
ShaderGraph& m_graph;
|
||||
bool m_forceVariable;
|
||||
bool m_enableCustomVariableName;
|
||||
bool m_isPreviewEnabled;
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -24,3 +24,20 @@ inline void ShaderNode::SetPreviewSize(const Nz::Vector2i& size)
|
|||
embeddedWidgetSizeUpdated();
|
||||
}
|
||||
}
|
||||
|
||||
inline void ShaderNode::SetVariableName(std::string variableName)
|
||||
{
|
||||
m_variableName = std::move(variableName);
|
||||
}
|
||||
|
||||
inline void ShaderNode::DisableCustomVariableName()
|
||||
{
|
||||
return EnableCustomVariableName(false);
|
||||
}
|
||||
|
||||
inline void ShaderNode::EnableCustomVariableName(bool enable)
|
||||
{
|
||||
m_enableCustomVariableName = enable;
|
||||
if (!m_enableCustomVariableName)
|
||||
m_variableName.clear();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -22,6 +22,7 @@ ShaderNode(graph)
|
|||
m_currentTextureText = firstInput.name;
|
||||
}
|
||||
|
||||
DisableCustomVariableName();
|
||||
EnablePreview(true);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -16,6 +16,7 @@
|
|||
#include <nodes/FlowScene>
|
||||
#include <nodes/FlowView>
|
||||
#include <nodes/DataModelRegistry>
|
||||
#include <unordered_set>
|
||||
|
||||
namespace
|
||||
{
|
||||
|
|
@ -105,8 +106,6 @@ Nz::ShaderAst::StatementPtr ShaderGraph::ToAst()
|
|||
std::vector<Nz::ShaderAst::StatementPtr> statements;
|
||||
QHash<QUuid, unsigned int> usageCount;
|
||||
|
||||
unsigned int varCount = 0;
|
||||
|
||||
std::function<void(QtNodes::Node*)> DetectVariables;
|
||||
DetectVariables = [&](QtNodes::Node* node)
|
||||
{
|
||||
|
|
@ -135,6 +134,9 @@ Nz::ShaderAst::StatementPtr ShaderGraph::ToAst()
|
|||
|
||||
QHash<QUuid, Nz::ShaderAst::ExpressionPtr> variableExpressions;
|
||||
|
||||
unsigned int varCount = 0;
|
||||
std::unordered_set<std::string> usedVariableNames;
|
||||
|
||||
std::function<Nz::ShaderAst::ExpressionPtr(QtNodes::Node*)> HandleNode;
|
||||
HandleNode = [&](QtNodes::Node* node) -> Nz::ShaderAst::ExpressionPtr
|
||||
{
|
||||
|
|
@ -163,12 +165,24 @@ Nz::ShaderAst::StatementPtr ShaderGraph::ToAst()
|
|||
|
||||
auto expression = shaderNode->GetExpression(expressions.data(), expressions.size());
|
||||
|
||||
if (*it > 1)
|
||||
const std::string& variableName = shaderNode->GetVariableName();
|
||||
if (*it > 1 || !variableName.empty())
|
||||
{
|
||||
Nz::ShaderAst::ExpressionPtr varExpression;
|
||||
if (expression->GetExpressionCategory() == Nz::ShaderAst::ExpressionCategory::RValue)
|
||||
{
|
||||
auto variable = Nz::ShaderBuilder::Variable("var" + std::to_string(varCount++), expression->GetExpressionType());
|
||||
std::string name;
|
||||
if (variableName.empty())
|
||||
name = "var" + std::to_string(varCount++);
|
||||
else
|
||||
name = variableName;
|
||||
|
||||
if (usedVariableNames.find(name) != usedVariableNames.end())
|
||||
throw std::runtime_error("duplicate variable found: " + name);
|
||||
|
||||
usedVariableNames.insert(name);
|
||||
|
||||
auto variable = Nz::ShaderBuilder::Variable(std::move(name), expression->GetExpressionType());
|
||||
statements.emplace_back(Nz::ShaderBuilder::DeclareVariable(variable, expression));
|
||||
|
||||
varExpression = variable;
|
||||
|
|
|
|||
Loading…
Reference in New Issue