Merge branch 'W2Wizard-RemapMaterialNode'

This commit is contained in:
Wojtek Figat
2021-01-26 00:16:17 +01:00
3 changed files with 49 additions and 0 deletions

View File

@@ -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),
}
},
};
}
}

View File

@@ -401,6 +401,18 @@ void ShaderGenerator::ProcessGroupMath(Box* box, Node* node, Value& value)
{
value = writeFunction1(node, tryGetValue(node->GetBox(0), Value::Zero), TEXT("radians"));
break;
}
// Remap
case 48:
{
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, String::Format(TEXT("{2} ? clamp({0}, {1}.x, {1}.y) : {0}"), mapFunc, rangeB.Value, clamp.Value), node);
break;
}
default:
break;

View File

@@ -371,6 +371,20 @@ 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 = clamp ? Math::Clamp(mapFunc, rangeB.X, rangeB.Y) : mapFunc;
break;
}
default:
break;
}