ShaderNode: Add FloatValue

This commit is contained in:
Lynix 2020-06-04 18:30:40 +02:00
parent 25562a5856
commit 725ecc7606
5 changed files with 156 additions and 1 deletions

View File

@ -0,0 +1,106 @@
#include <ShaderNode/DataModels/FloatValue.hpp>
#include <ShaderNode/DataTypes/FloatData.hpp>
#include <Nazara/Renderer/ShaderBuilder.hpp>
#include <cassert>
FloatValue::FloatValue(ShaderGraph& graph) :
ShaderNode(graph),
m_value(1.f)
{
UpdatePreview();
}
QString FloatValue::caption() const
{
static QString caption = "Float constant";
return caption;
}
QString FloatValue::name() const
{
static QString name = "float_constant";
return name;
}
QtNodes::NodeDataType FloatValue::dataType(QtNodes::PortType portType, QtNodes::PortIndex portIndex) const
{
assert(portType == QtNodes::PortType::Out);
assert(portIndex == 0);
return FloatData::Type();
}
unsigned int FloatValue::nPorts(QtNodes::PortType portType) const
{
switch (portType)
{
case QtNodes::PortType::In: return 0;
case QtNodes::PortType::Out: return 1;
}
return 0;
}
std::shared_ptr<QtNodes::NodeData> FloatValue::outData(QtNodes::PortIndex port)
{
assert(port == 0);
auto out = std::make_shared<FloatData>();
out->preview.fill(ToColor());
return out;
}
void FloatValue::BuildNodeEdition(QFormLayout* layout)
{
ShaderNode::BuildNodeEdition(layout);
QDoubleSpinBox* spinbox = new QDoubleSpinBox;
spinbox->setDecimals(6);
spinbox->setValue(m_value);
connect(spinbox, qOverload<double>(&QDoubleSpinBox::valueChanged), [=](double)
{
m_value = spinbox->value();
Q_EMIT dataUpdated(0);
UpdatePreview();
});
layout->addRow(tr("Value"), spinbox);
}
Nz::ShaderAst::ExpressionPtr FloatValue::GetExpression(Nz::ShaderAst::ExpressionPtr* /*expressions*/, std::size_t count) const
{
assert(count == 0);
return Nz::ShaderBuilder::Constant(m_value);
}
bool FloatValue::ComputePreview(QPixmap& pixmap)
{
pixmap.fill(ToColor());
return true;
}
QColor FloatValue::ToColor() const
{
float value = std::clamp(m_value, 0.f, 1.f);
return QColor::fromRgbF(value, value, value, value);
}
void FloatValue::restore(const QJsonObject& data)
{
m_value = float(data["value"].toDouble(m_value));
ShaderNode::restore(data);
}
QJsonObject FloatValue::save() const
{
QJsonObject data = ShaderNode::save();
data["value"] = m_value;
return data;
}

View File

@ -0,0 +1,45 @@
#pragma once
#ifndef NAZARA_SHADERNODES_FLOATVALUE_HPP
#define NAZARA_SHADERNODES_FLOATVALUE_HPP
#include <QtGui/QImage>
#include <QtWidgets/QDoubleSpinBox>
#include <QtWidgets/QFormLayout>
#include <QtWidgets/QLabel>
#include <ShaderNode/DataModels/ShaderNode.hpp>
#include <ShaderNode/DataTypes/FloatData.hpp>
#include <array>
class FloatValue : public ShaderNode
{
public:
FloatValue(ShaderGraph& graph);
~FloatValue() = default;
QString caption() const override;
QString name() const override;
QtNodes::NodeDataType dataType(QtNodes::PortType portType, QtNodes::PortIndex portIndex) const override;
unsigned int nPorts(QtNodes::PortType portType) const override;
std::shared_ptr<QtNodes::NodeData> outData(QtNodes::PortIndex port) override;
void BuildNodeEdition(QFormLayout* layout) override;
Nz::ShaderAst::ExpressionPtr GetExpression(Nz::ShaderAst::ExpressionPtr* expressions, std::size_t count) const override;
private:
bool ComputePreview(QPixmap& pixmap) override;
QColor ToColor() const;
void restore(const QJsonObject& data) override;
QJsonObject save() const override;
float m_value;
};
#include <ShaderNode/DataModels/FloatValue.inl>
#endif

View File

@ -0,0 +1 @@
#include <ShaderNode/DataModels/FloatValue.hpp>

View File

@ -127,7 +127,9 @@ auto InputValue::dataType(QtNodes::PortType portType, QtNodes::PortIndex portInd
switch (inputEntry.type)
{
//case InputType::Bool: return Nz::ShaderAst::ExpressionType::Boolean;
//case InputType::Float1: return Nz::ShaderAst::ExpressionType::Float1;
case InOutType::Float1:
return FloatData::Type();
case InOutType::Float2:
case InOutType::Float3:
case InOutType::Float4:

View File

@ -262,6 +262,7 @@ std::shared_ptr<QtNodes::DataModelRegistry> ShaderGraph::BuildRegistry()
RegisterShaderNode<CastToVec2>(*this, registry, "Casts");
RegisterShaderNode<CastToVec3>(*this, registry, "Casts");
RegisterShaderNode<CastToVec4>(*this, registry, "Casts");
RegisterShaderNode<FloatValue>(*this, registry, "Constants");
RegisterShaderNode<InputValue>(*this, registry, "Inputs");
RegisterShaderNode<OutputValue>(*this, registry, "Outputs");
RegisterShaderNode<SampleTexture>(*this, registry, "Texture");