nodes: made changes to the node system to allow for context sensitive shader code
Primarily implemented for the math node so a new shader can be generated and compiled based on what inputs the node is receiving.
This commit is contained in:
+87
-3
@@ -42,7 +42,91 @@ void MathNode::Retranslate()
|
||||
{
|
||||
Node::Retranslate();
|
||||
|
||||
method_in_->set_name(tr(""));
|
||||
param_a_in_->set_name(tr(""));
|
||||
param_b_in_->set_name(tr(""));
|
||||
method_in_->set_name(tr("Method"));
|
||||
param_a_in_->set_name(tr("Value"));
|
||||
param_b_in_->set_name(tr("Value"));
|
||||
}
|
||||
|
||||
Node::Capabilities MathNode::GetCapabilities(const NodeValueDatabase &input) const
|
||||
{
|
||||
if (input[param_a_in_].Has(NodeParam::kTexture) || input[param_b_in_].Has(NodeParam::kTexture)) {
|
||||
return kShader;
|
||||
} else if (input[param_a_in_].Has(NodeParam::kSamples) || input[param_b_in_].Has(NodeParam::kSamples)) {
|
||||
return kSampleProcessor;
|
||||
} else {
|
||||
return kNormal;
|
||||
}
|
||||
}
|
||||
|
||||
QString MathNode::ShaderID(const NodeValueDatabase &input) const
|
||||
{
|
||||
// FIXME: Hardcoded ADD operation
|
||||
QString method = QString::number(0);
|
||||
|
||||
QString type_a = QString::number(GuessTypeFromTable(input[param_a_in_]));
|
||||
QString type_b = QString::number(GuessTypeFromTable(input[param_b_in_]));
|
||||
|
||||
return id().append(method).append(type_a).append(type_b);
|
||||
}
|
||||
|
||||
QString MathNode::ShaderFragmentCode(const NodeValueDatabase &input) const
|
||||
{
|
||||
NodeParam::DataType type_a = GuessTypeFromTable(input[param_a_in_]);
|
||||
NodeParam::DataType type_b = GuessTypeFromTable(input[param_b_in_]);
|
||||
|
||||
return QStringLiteral("#version 110\n"
|
||||
"\n"
|
||||
"varying vec2 ove_texcoord;\n"
|
||||
"\n"
|
||||
"uniform %1 %3;\n"
|
||||
"uniform %2 %4;\n"
|
||||
"\n"
|
||||
"void main(void) {\n"
|
||||
" gl_FragColor = %5 + %6;\n"
|
||||
"}\n").arg(GetUniformTypeFromType(type_a),
|
||||
GetUniformTypeFromType(type_b),
|
||||
param_a_in_->id(),
|
||||
param_b_in_->id(),
|
||||
GetVariableCall(param_a_in_->id(), type_a),
|
||||
GetVariableCall(param_b_in_->id(), type_b));
|
||||
}
|
||||
|
||||
NodeValue MathNode::InputValueFromTable(NodeInput *input, const NodeValueTable &table) const
|
||||
{
|
||||
if (input->IsConnected()
|
||||
&& (input == param_a_in_ || input == param_b_in_)
|
||||
&& table.Has(NodeParam::kTexture)) {
|
||||
return table.GetWithMeta(NodeParam::kTexture);
|
||||
}
|
||||
|
||||
return Node::InputValueFromTable(input, table);
|
||||
}
|
||||
|
||||
NodeParam::DataType MathNode::GuessTypeFromTable(const NodeValueTable &table)
|
||||
{
|
||||
if (table.Has(NodeParam::kTexture)) {
|
||||
return NodeParam::kTexture;
|
||||
} else if (table.Has(NodeParam::kSamples)) {
|
||||
return NodeParam::kSamples;
|
||||
} else {
|
||||
return NodeParam::kFloat;
|
||||
}
|
||||
}
|
||||
|
||||
QString MathNode::GetUniformTypeFromType(const NodeParam::DataType &type)
|
||||
{
|
||||
if (type == NodeParam::kTexture) {
|
||||
return QStringLiteral("sampler2D");
|
||||
}
|
||||
|
||||
return QStringLiteral("float");
|
||||
}
|
||||
|
||||
QString MathNode::GetVariableCall(const QString &input_id, const NodeParam::DataType &type)
|
||||
{
|
||||
if (type == NodeParam::kTexture) {
|
||||
return QStringLiteral("texture2D(%1, ove_texcoord)").arg(input_id);
|
||||
}
|
||||
|
||||
return input_id;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user