From d7b9056d943615767523980e47d4bffb07ab8932 Mon Sep 17 00:00:00 2001 From: NoriteSC <53096989+NoriteSC@users.noreply.github.com> Date: Wed, 4 Oct 2023 13:13:45 +0200 Subject: [PATCH] added AAStep and resolved stefnotch reviewe --- Source/Editor/Surface/Archetypes/Material.cs | 27 ++++++++++++++++--- .../MaterialGenerator.Material.cpp | 24 ++++++++++++++--- 2 files changed, 43 insertions(+), 8 deletions(-) diff --git a/Source/Editor/Surface/Archetypes/Material.cs b/Source/Editor/Surface/Archetypes/Material.cs index a0aa7d29f..8e5329d22 100644 --- a/Source/Editor/Surface/Archetypes/Material.cs +++ b/Source/Editor/Surface/Archetypes/Material.cs @@ -885,8 +885,8 @@ namespace FlaxEditor.Surface.Archetypes new NodeArchetype { TypeID = 40, - Title = "Ratangle Mask", - Description = "Creates a Ratangle mask", + Title = "Rectangle Mask", + Description = "Creates a Rectangle mask", Flags = NodeFlags.MaterialGraph, Size = new Float2(150, 40), ConnectionsHints = ConnectionsHint.Vector, @@ -898,7 +898,7 @@ namespace FlaxEditor.Surface.Archetypes Elements = new[] { NodeElementArchetype.Factory.Input(0, "UV", true, typeof(Float2), 0), - NodeElementArchetype.Factory.Input(1, "Ratangle", true, typeof(Float2), 1), + NodeElementArchetype.Factory.Input(1, "Rectangle", true, typeof(Float2), 1), NodeElementArchetype.Factory.Output(0, string.Empty, typeof(float), 5), } }, @@ -906,7 +906,7 @@ namespace FlaxEditor.Surface.Archetypes { TypeID = 41, Title = "FWidth", - Description = "Creates a Partial Derivatives (fwidth)", + Description = "Creates a partial derivative (fwidth)", Flags = NodeFlags.MaterialGraph, Size = new Float2(150, 20), ConnectionsHints = ConnectionsHint.Vector, @@ -920,6 +920,25 @@ namespace FlaxEditor.Surface.Archetypes NodeElementArchetype.Factory.Output(0, string.Empty, typeof(float), 5), } }, + new NodeArchetype + { + TypeID = 42, + Title = "AAStep", + Description = "Smooth version of step", + Flags = NodeFlags.MaterialGraph, + Size = new Float2(150, 20), + ConnectionsHints = ConnectionsHint.Vector, + DefaultValues = new object[] + { + 1, + 0 + }, + Elements = new[] + { + NodeElementArchetype.Factory.Input(0, "value", true, typeof(float), 0), + NodeElementArchetype.Factory.Output(0, string.Empty, typeof(float), 5), + } + }, }; } } diff --git a/Source/Engine/Tools/MaterialGenerator/MaterialGenerator.Material.cpp b/Source/Engine/Tools/MaterialGenerator/MaterialGenerator.Material.cpp index c6d05ef48..0c8d0780e 100644 --- a/Source/Engine/Tools/MaterialGenerator/MaterialGenerator.Material.cpp +++ b/Source/Engine/Tools/MaterialGenerator/MaterialGenerator.Material.cpp @@ -523,11 +523,10 @@ void MaterialGenerator::ProcessGroupMaterial(Box* box, Node* node, Value& value) case 40: { const auto uv = tryGetValue(node->GetBox(0), getUVs).AsFloat2(); - const auto ratangle = tryGetValue(node->GetBox(1), node->Values[1]).AsFloat2(); + const auto rectangle = tryGetValue(node->GetBox(1), node->Values[1]).AsFloat2(); - auto d = writeLocal(ValueType::Float2, String::Format(TEXT("abs({0} * 2 - 1) - {1}"),uv.Value, ratangle.Value), node); - auto fwidth = writeLocal(ValueType::Float , String::Format(TEXT("abs(ddx({0})) + abs(ddy({0}))"), d.Value), node); - auto d2 = writeLocal(ValueType::Float2 , String::Format(TEXT("1 - {0} / {1}"), d.Value, fwidth.Value), node); + auto d = writeLocal(ValueType::Float2, String::Format(TEXT("abs({0} * 2 - 1) - {1}"),uv.Value, rectangle.Value), node); + auto d2 = writeLocal(ValueType::Float2 , String::Format(TEXT("1 - {0} / fwidth({0})"), d.Value), node); value = writeLocal(ValueType::Float , String::Format(TEXT("saturate(min({0}.x, {0}.y))"), d2.Value), node); break; } @@ -538,6 +537,23 @@ void MaterialGenerator::ProcessGroupMaterial(Box* box, Node* node, Value& value) value = writeLocal(ValueType::Float, String::Format(TEXT("abs(ddx({0})) + abs(ddy({0}))"), d.Value), node); break; } + //AAStep (smooth version of step) + case 42: + { + //source https://www.ronja-tutorials.com/post/046-fwidth/#a-better-step + + const auto compValue = tryGetValue(node->GetBox(0), getUVs).AsFloat(); + const auto gradient = tryGetValue(node->GetBox(1), node->Values[1]).AsFloat(); + + auto change = writeLocal(ValueType::Float, String::Format(TEXT("fwidth({0})"), gradient.Value), node); + + //base the range of the inverse lerp on the change over two pixels + auto lowerEdge = writeLocal(ValueType::Float, String::Format(TEXT("{0} - {1})"), compValue.Value, change.Value), node); + auto upperEdge = writeLocal(ValueType::Float, String::Format(TEXT("{0} + {1})"), compValue.Value, change.Value), node); + + //do the inverse interpolation and saturate it + value = writeLocal(ValueType::Float, String::Format(TEXT("saturate((({0} - {1}) / ({2} - {1})))"), gradient.Value, lowerEdge.Value, upperEdge.Value), node); + } default: break; }