From ee166bafeddf2516b34f8027f946889424c22099 Mon Sep 17 00:00:00 2001 From: "W2.Wizard" Date: Mon, 25 Jan 2021 00:51:47 +0100 Subject: [PATCH 1/5] Added initial remap node --- Source/Editor/Surface/Archetypes/Math.cs | 23 +++++++++++++++++++++++ Source/Engine/Visject/ShaderGraph.cpp | 16 ++++++++++++++++ Source/Engine/Visject/VisjectGraph.cpp | 16 ++++++++++++++++ 3 files changed, 55 insertions(+) diff --git a/Source/Editor/Surface/Archetypes/Math.cs b/Source/Editor/Surface/Archetypes/Math.cs index 92ae69a12..7416ea498 100644 --- a/Source/Editor/Surface/Archetypes/Math.cs +++ b/Source/Editor/Surface/Archetypes/Math.cs @@ -404,6 +404,29 @@ namespace FlaxEditor.Surface.Archetypes NodeElementArchetype.Factory.Output(0, "A | B", null, 2), } }, + new NodeArchetype + { + TypeID = 48, + Title = "Remap", + Description = "Remaps a value from one range to another, so for example having 25 in a range of 0 to 100 being remapped to 0 to 1 would return 0.25", + Flags = NodeFlags.AllGraphs, + Size = new Vector2(175, 75), + DefaultValues = new object[] + { + 25.0f, + new Vector2(0.0f, 100.0f), + new Vector2(0.0f, 1.0f), + false + }, + Elements = new[] + { + NodeElementArchetype.Factory.Input(0, "Value", true, typeof(float), 0, 0), + NodeElementArchetype.Factory.Input(1, "In Range", true, typeof(Vector2), 1, 1), + NodeElementArchetype.Factory.Input(2, "Out Range", true, typeof(Vector2), 2, 2), + NodeElementArchetype.Factory.Input(3, "Clamp", true, typeof(bool), 3, 3), + NodeElementArchetype.Factory.Output(0, string.Empty, typeof(float), 4), + } + }, }; } } diff --git a/Source/Engine/Visject/ShaderGraph.cpp b/Source/Engine/Visject/ShaderGraph.cpp index 7b3c51f3e..7545e7c96 100644 --- a/Source/Engine/Visject/ShaderGraph.cpp +++ b/Source/Engine/Visject/ShaderGraph.cpp @@ -401,6 +401,22 @@ void ShaderGenerator::ProcessGroupMath(Box* box, Node* node, Value& value) { value = writeFunction1(node, tryGetValue(node->GetBox(0), Value::Zero), TEXT("radians")); break; + } + // Remap + case 48: + { + auto inVal = tryGetValue(node->GetBox(0), node->Values[0]); + auto rangeA = tryGetValue(node->GetBox(1), node->Values[1]); + auto rangeB = tryGetValue(node->GetBox(2), node->Values[2]); + + // Clamp value? + if (node->Values[3].AsBool) + { + value = writeLocal(ValueType::Float, String::Format(TEXT("clamp({2}.x + ({0} - {1}.x) * ({2}.y - {2}.x) / ({1}.y - {1}.x), {2}.x, {2}.y)"), inVal.Value, rangeA.Value, rangeB.Value), node); + break; + } + value = writeLocal(ValueType::Float, String::Format(TEXT("{2}.x + ({0} - {1}.x) * ({2}.y - {2}.x) / ({1}.y - {1}.x)"), inVal.Value, rangeA.Value, rangeB.Value), node); + break; } default: break; diff --git a/Source/Engine/Visject/VisjectGraph.cpp b/Source/Engine/Visject/VisjectGraph.cpp index eb2b8bf61..dfd5c9fdf 100644 --- a/Source/Engine/Visject/VisjectGraph.cpp +++ b/Source/Engine/Visject/VisjectGraph.cpp @@ -371,6 +371,22 @@ void VisjectExecutor::ProcessGroupMath(Box* box, Node* node, Value& value) if (value.Type.Type == VariantType::Enum) value.AsUint64 = value.AsUint64 | (uint64)tryGetValue(node->GetBox(1), Value::Zero); break; + + case 48: + { + const float inVal = tryGetValue(node->GetBox(0), node->Values[0]).AsFloat; + const Vector2 rangeA = tryGetValue(node->GetBox(1), node->Values[1]).AsVector2(); + const Vector2 rangeB = tryGetValue(node->GetBox(2), node->Values[2]).AsVector2(); + + // Use clamp? + if (node->Values[3].AsBool) + { + value = Math::Clamp(rangeB.X + (inVal - rangeA.X) * (rangeB.Y - rangeB.X) / (rangeA.Y - rangeA.X), rangeB.X, rangeB.Y); + break; + } + value = rangeB.X + (inVal - rangeA.X) * (rangeB.Y - rangeB.X) / (rangeA.Y - rangeA.X); + break; + } default: break; } From c0c2da34d738458fc92fe50f15793f61a3d2d92b Mon Sep 17 00:00:00 2001 From: "W2.Wizard" Date: Mon, 25 Jan 2021 12:37:42 +0100 Subject: [PATCH 2/5] Specified variants --- Source/Engine/Visject/ShaderGraph.cpp | 6 +++--- Source/Engine/Visject/VisjectGraph.cpp | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Source/Engine/Visject/ShaderGraph.cpp b/Source/Engine/Visject/ShaderGraph.cpp index 7545e7c96..5e133eb8c 100644 --- a/Source/Engine/Visject/ShaderGraph.cpp +++ b/Source/Engine/Visject/ShaderGraph.cpp @@ -405,9 +405,9 @@ void ShaderGenerator::ProcessGroupMath(Box* box, Node* node, Value& value) // Remap case 48: { - auto inVal = tryGetValue(node->GetBox(0), node->Values[0]); - auto rangeA = tryGetValue(node->GetBox(1), node->Values[1]); - auto rangeB = tryGetValue(node->GetBox(2), node->Values[2]); + auto inVal = tryGetValue(node->GetBox(0), node->Values[0].AsFloat); + auto rangeA = tryGetValue(node->GetBox(1), node->Values[1].AsVector2()); + auto rangeB = tryGetValue(node->GetBox(2), node->Values[2].AsVector2()); // Clamp value? if (node->Values[3].AsBool) diff --git a/Source/Engine/Visject/VisjectGraph.cpp b/Source/Engine/Visject/VisjectGraph.cpp index dfd5c9fdf..e4e8f69cf 100644 --- a/Source/Engine/Visject/VisjectGraph.cpp +++ b/Source/Engine/Visject/VisjectGraph.cpp @@ -378,7 +378,7 @@ void VisjectExecutor::ProcessGroupMath(Box* box, Node* node, Value& value) const Vector2 rangeA = tryGetValue(node->GetBox(1), node->Values[1]).AsVector2(); const Vector2 rangeB = tryGetValue(node->GetBox(2), node->Values[2]).AsVector2(); - // Use clamp? + // Clamp value? if (node->Values[3].AsBool) { value = Math::Clamp(rangeB.X + (inVal - rangeA.X) * (rangeB.Y - rangeB.X) / (rangeA.Y - rangeA.X), rangeB.X, rangeB.Y); From f331191e3deb848547de738d78649ef25feab96e Mon Sep 17 00:00:00 2001 From: "W2.Wizard" Date: Mon, 25 Jan 2021 19:31:12 +0100 Subject: [PATCH 3/5] Refactored --- Source/Engine/Visject/ShaderGraph.cpp | 15 +++++---------- Source/Engine/Visject/VisjectGraph.cpp | 9 +++------ 2 files changed, 8 insertions(+), 16 deletions(-) diff --git a/Source/Engine/Visject/ShaderGraph.cpp b/Source/Engine/Visject/ShaderGraph.cpp index 5e133eb8c..c447961c2 100644 --- a/Source/Engine/Visject/ShaderGraph.cpp +++ b/Source/Engine/Visject/ShaderGraph.cpp @@ -405,17 +405,12 @@ void ShaderGenerator::ProcessGroupMath(Box* box, Node* node, Value& value) // Remap case 48: { - auto inVal = tryGetValue(node->GetBox(0), node->Values[0].AsFloat); - auto rangeA = tryGetValue(node->GetBox(1), node->Values[1].AsVector2()); - auto rangeB = tryGetValue(node->GetBox(2), node->Values[2].AsVector2()); + const auto inVal = tryGetValue(node->GetBox(0), node->Values[0].AsFloat); + const auto rangeA = tryGetValue(node->GetBox(1), node->Values[1].AsVector2()); + const auto rangeB = tryGetValue(node->GetBox(2), node->Values[2].AsVector2()); - // Clamp value? - if (node->Values[3].AsBool) - { - value = writeLocal(ValueType::Float, String::Format(TEXT("clamp({2}.x + ({0} - {1}.x) * ({2}.y - {2}.x) / ({1}.y - {1}.x), {2}.x, {2}.y)"), inVal.Value, rangeA.Value, rangeB.Value), node); - break; - } - value = writeLocal(ValueType::Float, String::Format(TEXT("{2}.x + ({0} - {1}.x) * ({2}.y - {2}.x) / ({1}.y - {1}.x)"), inVal.Value, rangeA.Value, rangeB.Value), node); + const auto mapFunc = String::Format(TEXT("{2}.x + ({0} - {1}.x) * ({2}.y - {2}.x) / ({1}.y - {1}.x)"), inVal.Value, rangeA.Value, rangeB.Value); + value = writeLocal(ValueType::Float, node->Values[3].AsBool ? String::Format(TEXT("clamp({0}, {1}.x, {1}.y)"), mapFunc, rangeB.Value) : mapFunc, node); break; } default: diff --git a/Source/Engine/Visject/VisjectGraph.cpp b/Source/Engine/Visject/VisjectGraph.cpp index e4e8f69cf..b5ee3e29d 100644 --- a/Source/Engine/Visject/VisjectGraph.cpp +++ b/Source/Engine/Visject/VisjectGraph.cpp @@ -378,13 +378,10 @@ void VisjectExecutor::ProcessGroupMath(Box* box, Node* node, Value& value) const Vector2 rangeA = tryGetValue(node->GetBox(1), node->Values[1]).AsVector2(); const Vector2 rangeB = tryGetValue(node->GetBox(2), node->Values[2]).AsVector2(); + auto mapFunc = rangeB.X + (inVal - rangeA.X) * (rangeB.Y - rangeB.X) / (rangeA.Y - rangeA.X); + // Clamp value? - if (node->Values[3].AsBool) - { - value = Math::Clamp(rangeB.X + (inVal - rangeA.X) * (rangeB.Y - rangeB.X) / (rangeA.Y - rangeA.X), rangeB.X, rangeB.Y); - break; - } - value = rangeB.X + (inVal - rangeA.X) * (rangeB.Y - rangeB.X) / (rangeA.Y - rangeA.X); + value = node->Values[3].AsBool ? Math::Clamp(mapFunc, rangeB.X, rangeB.Y) : mapFunc; break; } default: From 44d636520bc50dacd655f9a7d2ebdcfdec173226 Mon Sep 17 00:00:00 2001 From: "W2.Wizard" Date: Tue, 26 Jan 2021 00:04:04 +0100 Subject: [PATCH 4/5] Applied review changes --- Source/Engine/Visject/VisjectGraph.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Source/Engine/Visject/VisjectGraph.cpp b/Source/Engine/Visject/VisjectGraph.cpp index b5ee3e29d..939013ebd 100644 --- a/Source/Engine/Visject/VisjectGraph.cpp +++ b/Source/Engine/Visject/VisjectGraph.cpp @@ -371,17 +371,18 @@ void VisjectExecutor::ProcessGroupMath(Box* box, Node* node, Value& value) if (value.Type.Type == VariantType::Enum) value.AsUint64 = value.AsUint64 | (uint64)tryGetValue(node->GetBox(1), Value::Zero); break; - + // Remap case 48: { const float inVal = tryGetValue(node->GetBox(0), node->Values[0]).AsFloat; const Vector2 rangeA = tryGetValue(node->GetBox(1), node->Values[1]).AsVector2(); const Vector2 rangeB = tryGetValue(node->GetBox(2), node->Values[2]).AsVector2(); + const bool clamp = tryGetValue(node->GetBox(3), node->Values[3]).AsBool; auto mapFunc = rangeB.X + (inVal - rangeA.X) * (rangeB.Y - rangeB.X) / (rangeA.Y - rangeA.X); // Clamp value? - value = node->Values[3].AsBool ? Math::Clamp(mapFunc, rangeB.X, rangeB.Y) : mapFunc; + value = clamp ? Math::Clamp(mapFunc, rangeB.X, rangeB.Y) : mapFunc; break; } default: From c703064add51ea88080fabb5bbd89a690d4b26c1 Mon Sep 17 00:00:00 2001 From: "W2.Wizard" Date: Tue, 26 Jan 2021 00:12:43 +0100 Subject: [PATCH 5/5] Added also tryGetValue to ShaderGraph Since @mafiesto4 requested in the review to use tryGetValye in the VisjectGraph to get the bool param I also did it to the shader one --- Source/Engine/Visject/ShaderGraph.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Source/Engine/Visject/ShaderGraph.cpp b/Source/Engine/Visject/ShaderGraph.cpp index c447961c2..c8a8782e9 100644 --- a/Source/Engine/Visject/ShaderGraph.cpp +++ b/Source/Engine/Visject/ShaderGraph.cpp @@ -408,9 +408,10 @@ void ShaderGenerator::ProcessGroupMath(Box* box, Node* node, Value& value) const auto inVal = tryGetValue(node->GetBox(0), node->Values[0].AsFloat); const auto rangeA = tryGetValue(node->GetBox(1), node->Values[1].AsVector2()); const auto rangeB = tryGetValue(node->GetBox(2), node->Values[2].AsVector2()); + const auto clamp = tryGetValue(node->GetBox(3), node->Values[3]).AsBool(); const auto mapFunc = String::Format(TEXT("{2}.x + ({0} - {1}.x) * ({2}.y - {2}.x) / ({1}.y - {1}.x)"), inVal.Value, rangeA.Value, rangeB.Value); - value = writeLocal(ValueType::Float, node->Values[3].AsBool ? String::Format(TEXT("clamp({0}, {1}.x, {1}.y)"), mapFunc, rangeB.Value) : mapFunc, node); + value = writeLocal(ValueType::Float, String::Format(TEXT("{2} ? clamp({0}, {1}.x, {1}.y) : {0}"), mapFunc, rangeB.Value, clamp.Value), node); break; } default: