From 70ca1996c5e4a9fc8204a3133baad7d3826fb3bc Mon Sep 17 00:00:00 2001 From: NoriteSC <53096989+NoriteSC@users.noreply.github.com> Date: Wed, 4 Oct 2023 10:00:03 +0200 Subject: [PATCH 1/9] added Ratangle Mask and FWidth --- Source/Editor/Surface/Archetypes/Material.cs | 38 +++++++++++++++++++ .../MaterialGenerator.Material.cpp | 19 ++++++++++ 2 files changed, 57 insertions(+) diff --git a/Source/Editor/Surface/Archetypes/Material.cs b/Source/Editor/Surface/Archetypes/Material.cs index 1a797c67d..064f0a771 100644 --- a/Source/Editor/Surface/Archetypes/Material.cs +++ b/Source/Editor/Surface/Archetypes/Material.cs @@ -882,6 +882,44 @@ namespace FlaxEditor.Surface.Archetypes NodeElementArchetype.Factory.Output(1, "Inv Size", typeof(Float2), 1), } }, + new NodeArchetype + { + TypeID = 40, + Title = "Ratangle Mask", + Description = "Creates a Ratangle mask", + Flags = NodeFlags.MaterialGraph, + Size = new Float2(150, 100), + ConnectionsHints = ConnectionsHint.Vector, + DefaultValues = new object[] + { + new Float2(0, 0), + new Float2(0.5f, 0.5f), + }, + Elements = new[] + { + NodeElementArchetype.Factory.Input(0, "UV", true, typeof(Float2), 0), + NodeElementArchetype.Factory.Input(1, "Ratangle", true, typeof(Float2), 1), + NodeElementArchetype.Factory.Output(0, string.Empty, typeof(float), 5), + } + }, + new NodeArchetype + { + TypeID = 41, + Title = "FWidth", + Description = "Creates a Partial Derivatives (fwidth)", + Flags = NodeFlags.MaterialGraph, + Size = new Float2(150, 100), + ConnectionsHints = ConnectionsHint.Vector, + DefaultValues = new object[] + { + 1 + }, + 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 8aa300731..b00477fdc 100644 --- a/Source/Engine/Tools/MaterialGenerator/MaterialGenerator.Material.cpp +++ b/Source/Engine/Tools/MaterialGenerator/MaterialGenerator.Material.cpp @@ -519,6 +519,25 @@ void MaterialGenerator::ProcessGroupMaterial(Box* box, Node* node, Value& value) } break; } + // Ratangle Mask + case 40: + { + const auto uv = tryGetValue(node->GetBox(0), getUVs).AsFloat2(); + const auto ratangle = 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::Float , String::Format(TEXT("1 - {0} / {1}"), d.Value, fwidth.Value), node); + value = writeLocal(ValueType::Float , String::Format(TEXT("saturate(min({0}.x, {0}.y))"), d2.Value), node); + break; + } + // FWidth + case 41: + { + const auto d = tryGetValue(node->GetBox(0), node->Values[0]).AsFloat(); + value = writeLocal(ValueType::Float, String::Format(TEXT("abs(ddx({0})) + abs(ddy({0}))"), d.Value), node); + break; + } default: break; } From 367eaf2f8966a04628d3dedb67e366c662abb6f2 Mon Sep 17 00:00:00 2001 From: NoriteSC <53096989+NoriteSC@users.noreply.github.com> Date: Wed, 4 Oct 2023 10:28:23 +0200 Subject: [PATCH 2/9] adjustment to nodes size --- Source/Editor/Surface/Archetypes/Material.cs | 4 ++-- .../Tools/MaterialGenerator/MaterialGenerator.Material.cpp | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Source/Editor/Surface/Archetypes/Material.cs b/Source/Editor/Surface/Archetypes/Material.cs index 064f0a771..a0aa7d29f 100644 --- a/Source/Editor/Surface/Archetypes/Material.cs +++ b/Source/Editor/Surface/Archetypes/Material.cs @@ -888,7 +888,7 @@ namespace FlaxEditor.Surface.Archetypes Title = "Ratangle Mask", Description = "Creates a Ratangle mask", Flags = NodeFlags.MaterialGraph, - Size = new Float2(150, 100), + Size = new Float2(150, 40), ConnectionsHints = ConnectionsHint.Vector, DefaultValues = new object[] { @@ -908,7 +908,7 @@ namespace FlaxEditor.Surface.Archetypes Title = "FWidth", Description = "Creates a Partial Derivatives (fwidth)", Flags = NodeFlags.MaterialGraph, - Size = new Float2(150, 100), + Size = new Float2(150, 20), ConnectionsHints = ConnectionsHint.Vector, DefaultValues = new object[] { diff --git a/Source/Engine/Tools/MaterialGenerator/MaterialGenerator.Material.cpp b/Source/Engine/Tools/MaterialGenerator/MaterialGenerator.Material.cpp index b00477fdc..c6d05ef48 100644 --- a/Source/Engine/Tools/MaterialGenerator/MaterialGenerator.Material.cpp +++ b/Source/Engine/Tools/MaterialGenerator/MaterialGenerator.Material.cpp @@ -527,7 +527,7 @@ void MaterialGenerator::ProcessGroupMaterial(Box* box, Node* node, Value& value) 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::Float , String::Format(TEXT("1 - {0} / {1}"), d.Value, fwidth.Value), node); + auto d2 = writeLocal(ValueType::Float2 , String::Format(TEXT("1 - {0} / {1}"), d.Value, fwidth.Value), node); value = writeLocal(ValueType::Float , String::Format(TEXT("saturate(min({0}.x, {0}.y))"), d2.Value), node); break; } 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 3/9] 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; } From 329910ae0d022445e34ede5fbe20401c683bd7b6 Mon Sep 17 00:00:00 2001 From: NoriteSC <53096989+NoriteSC@users.noreply.github.com> Date: Wed, 4 Oct 2023 13:18:42 +0200 Subject: [PATCH 4/9] missing input fix --- Source/Editor/Surface/Archetypes/Material.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Source/Editor/Surface/Archetypes/Material.cs b/Source/Editor/Surface/Archetypes/Material.cs index 8e5329d22..c725b0662 100644 --- a/Source/Editor/Surface/Archetypes/Material.cs +++ b/Source/Editor/Surface/Archetypes/Material.cs @@ -926,7 +926,7 @@ namespace FlaxEditor.Surface.Archetypes Title = "AAStep", Description = "Smooth version of step", Flags = NodeFlags.MaterialGraph, - Size = new Float2(150, 20), + Size = new Float2(150, 40), ConnectionsHints = ConnectionsHint.Vector, DefaultValues = new object[] { @@ -936,6 +936,7 @@ namespace FlaxEditor.Surface.Archetypes Elements = new[] { NodeElementArchetype.Factory.Input(0, "value", true, typeof(float), 0), + NodeElementArchetype.Factory.Input(1, "gradient", true, typeof(float), 0), NodeElementArchetype.Factory.Output(0, string.Empty, typeof(float), 5), } }, From 1736aaeb6a791005c978d6d57c1bd9f82c0fd53a Mon Sep 17 00:00:00 2001 From: NoriteSC <53096989+NoriteSC@users.noreply.github.com> Date: Fri, 6 Oct 2023 22:54:26 +0200 Subject: [PATCH 5/9] Update Source/Editor/Surface/Archetypes/Material.cs Co-authored-by: stefnotch --- Source/Editor/Surface/Archetypes/Material.cs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/Source/Editor/Surface/Archetypes/Material.cs b/Source/Editor/Surface/Archetypes/Material.cs index c725b0662..77bdb25c6 100644 --- a/Source/Editor/Surface/Archetypes/Material.cs +++ b/Source/Editor/Surface/Archetypes/Material.cs @@ -910,10 +910,6 @@ namespace FlaxEditor.Surface.Archetypes Flags = NodeFlags.MaterialGraph, Size = new Float2(150, 20), ConnectionsHints = ConnectionsHint.Vector, - DefaultValues = new object[] - { - 1 - }, Elements = new[] { NodeElementArchetype.Factory.Input(0, "value", true, typeof(float), 0), From 4e2870e90cdcc0b3c04713eefaf1f4b469ef997b Mon Sep 17 00:00:00 2001 From: NoriteSC <53096989+NoriteSC@users.noreply.github.com> Date: Fri, 6 Oct 2023 22:56:16 +0200 Subject: [PATCH 6/9] Update Source/Editor/Surface/Archetypes/Material.cs Co-authored-by: stefnotch --- Source/Editor/Surface/Archetypes/Material.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Source/Editor/Surface/Archetypes/Material.cs b/Source/Editor/Surface/Archetypes/Material.cs index 77bdb25c6..6fee3a978 100644 --- a/Source/Editor/Surface/Archetypes/Material.cs +++ b/Source/Editor/Surface/Archetypes/Material.cs @@ -909,7 +909,9 @@ namespace FlaxEditor.Surface.Archetypes Description = "Creates a partial derivative (fwidth)", Flags = NodeFlags.MaterialGraph, Size = new Float2(150, 20), - ConnectionsHints = ConnectionsHint.Vector, + ConnectionsHints = ConnectionsHint.Numeric, + IndependentBoxes = new[] { 0 }, + DependentBoxes = new[] { 1 }, Elements = new[] { NodeElementArchetype.Factory.Input(0, "value", true, typeof(float), 0), From d7095957d089972be84376a66bd43d981bfe5908 Mon Sep 17 00:00:00 2001 From: NoriteSC <53096989+NoriteSC@users.noreply.github.com> Date: Fri, 6 Oct 2023 23:07:00 +0200 Subject: [PATCH 7/9] Update Source/Editor/Surface/Archetypes/Material.cs Co-authored-by: stefnotch --- Source/Editor/Surface/Archetypes/Material.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Source/Editor/Surface/Archetypes/Material.cs b/Source/Editor/Surface/Archetypes/Material.cs index 6fee3a978..ee3270460 100644 --- a/Source/Editor/Surface/Archetypes/Material.cs +++ b/Source/Editor/Surface/Archetypes/Material.cs @@ -914,8 +914,8 @@ namespace FlaxEditor.Surface.Archetypes DependentBoxes = new[] { 1 }, Elements = new[] { - NodeElementArchetype.Factory.Input(0, "value", true, typeof(float), 0), - NodeElementArchetype.Factory.Output(0, string.Empty, typeof(float), 5), + NodeElementArchetype.Factory.Input(0, "value", true, null, 0), + NodeElementArchetype.Factory.Output(0, string.Empty, null, 1), } }, new NodeArchetype From 004e2ab5e80f8a91a3cafffdfb94208d7da646da Mon Sep 17 00:00:00 2001 From: NoriteSC <53096989+NoriteSC@users.noreply.github.com> Date: Fri, 6 Oct 2023 23:07:19 +0200 Subject: [PATCH 8/9] Update Source/Engine/Tools/MaterialGenerator/MaterialGenerator.Material.cpp Co-authored-by: stefnotch --- .../Tools/MaterialGenerator/MaterialGenerator.Material.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Source/Engine/Tools/MaterialGenerator/MaterialGenerator.Material.cpp b/Source/Engine/Tools/MaterialGenerator/MaterialGenerator.Material.cpp index 0c8d0780e..df594a02b 100644 --- a/Source/Engine/Tools/MaterialGenerator/MaterialGenerator.Material.cpp +++ b/Source/Engine/Tools/MaterialGenerator/MaterialGenerator.Material.cpp @@ -533,8 +533,8 @@ void MaterialGenerator::ProcessGroupMaterial(Box* box, Node* node, Value& value) // FWidth case 41: { - const auto d = tryGetValue(node->GetBox(0), node->Values[0]).AsFloat(); - value = writeLocal(ValueType::Float, String::Format(TEXT("abs(ddx({0})) + abs(ddy({0}))"), d.Value), node); + const auto inValue = tryGetValue(node->GetBox(0), 0, Value::Zero); + value = writeLocal(inValue.Type, String::Format(TEXT("fwidth({0})"), inValue.Value), node); break; } //AAStep (smooth version of step) From 137c82a38748666cfc954e91e4acb2ed2ac3e549 Mon Sep 17 00:00:00 2001 From: NoriteSC <53096989+NoriteSC@users.noreply.github.com> Date: Wed, 25 Oct 2023 06:21:19 +0200 Subject: [PATCH 9/9] Update Material.cs --- Source/Editor/Surface/Archetypes/Material.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/Editor/Surface/Archetypes/Material.cs b/Source/Editor/Surface/Archetypes/Material.cs index ee3270460..11237481d 100644 --- a/Source/Editor/Surface/Archetypes/Material.cs +++ b/Source/Editor/Surface/Archetypes/Material.cs @@ -934,7 +934,7 @@ namespace FlaxEditor.Surface.Archetypes Elements = new[] { NodeElementArchetype.Factory.Input(0, "value", true, typeof(float), 0), - NodeElementArchetype.Factory.Input(1, "gradient", true, typeof(float), 0), + NodeElementArchetype.Factory.Input(1, "gradient", true, typeof(float), 1), NodeElementArchetype.Factory.Output(0, string.Empty, typeof(float), 5), } },