From bd18286fbd781879c796b45ac890829fae404dca Mon Sep 17 00:00:00 2001 From: "W2.Wizard" Date: Thu, 28 Jan 2021 01:26:57 +0100 Subject: [PATCH 1/5] Refactoring and Node implementation Refactored all recently added nodes to maintain consistency. --- Source/Editor/Surface/Archetypes/Material.cs | 62 +++++++++++++++++- .../MaterialGenerator.Material.cpp | 63 ++++++++++++++----- 2 files changed, 106 insertions(+), 19 deletions(-) diff --git a/Source/Editor/Surface/Archetypes/Material.cs b/Source/Editor/Surface/Archetypes/Material.cs index 6e828fca4..f8fc025a8 100644 --- a/Source/Editor/Surface/Archetypes/Material.cs +++ b/Source/Editor/Surface/Archetypes/Material.cs @@ -725,7 +725,7 @@ namespace FlaxEditor.Surface.Archetypes { TypeID = 30, Title = "DDX", - Description = "Returns the partial derivative of the specified value with respect to the screen-space x-coordinate.", + Description = "Returns the partial derivative of the specified value with respect to the screen-space x-coordinate", Flags = NodeFlags.MaterialGraph, Size = new Vector2(90, 25), ConnectionsHints = ConnectionsHint.Numeric, @@ -741,7 +741,7 @@ namespace FlaxEditor.Surface.Archetypes { TypeID = 31, Title = "DDY", - Description = "Returns the partial derivative of the specified value with respect to the screen-space y-coordinate.", + Description = "Returns the partial derivative of the specified value with respect to the screen-space y-coordinate", Flags = NodeFlags.MaterialGraph, Size = new Vector2(90, 25), ConnectionsHints = ConnectionsHint.Numeric, @@ -753,6 +753,64 @@ namespace FlaxEditor.Surface.Archetypes NodeElementArchetype.Factory.Output(0, string.Empty, null, 1), } }, + new NodeArchetype + { + TypeID = 32, + Title = "Sign", + Description = "Returns -1 if value is less than zero; 0 if value equals zero; and 1 if value is greater than zero", + Flags = NodeFlags.MaterialGraph, + Size = new Vector2(90, 25), + ConnectionsHints = ConnectionsHint.Numeric, + IndependentBoxes = new[] { 0 }, + Elements = new[] + { + NodeElementArchetype.Factory.Input(0, "Value", true, null, 0), + NodeElementArchetype.Factory.Output(0, string.Empty, typeof(float), 1), + } + }, + new NodeArchetype + { + TypeID = 33, + Title = "Any", + Description = "True if any components of value are non-zero; otherwise, false", + Flags = NodeFlags.MaterialGraph, + Size = new Vector2(90, 25), + ConnectionsHints = ConnectionsHint.Numeric, + IndependentBoxes = new[] { 0 }, + Elements = new[] + { + NodeElementArchetype.Factory.Input(0, "Value", true, null, 0), + NodeElementArchetype.Factory.Output(0, string.Empty, typeof(bool), 1), + } + }, + new NodeArchetype + { + TypeID = 34, + Title = "All", + Description = "Determines if all components of the specified value are non-zero", + Flags = NodeFlags.MaterialGraph, + Size = new Vector2(90, 25), + ConnectionsHints = ConnectionsHint.Numeric, + IndependentBoxes = new[] { 0 }, + Elements = new[] + { + NodeElementArchetype.Factory.Input(0, "Value", true, null, 0), + NodeElementArchetype.Factory.Output(0, string.Empty, typeof(bool), 1), + } + }, + new NodeArchetype + { + TypeID = 35, + Title = "Black Body", + Description = "Simulates black body radiation via a given temperature in kelvin", + Flags = NodeFlags.MaterialGraph, + Size = new Vector2(120, 25), + Elements = new[] + { + NodeElementArchetype.Factory.Input(0, "Temp", true, typeof(float), 0), + NodeElementArchetype.Factory.Output(0, string.Empty, typeof(Vector3), 1), + } + }, }; } } diff --git a/Source/Engine/Tools/MaterialGenerator/MaterialGenerator.Material.cpp b/Source/Engine/Tools/MaterialGenerator/MaterialGenerator.Material.cpp index 0d8a0e226..d851e4b86 100644 --- a/Source/Engine/Tools/MaterialGenerator/MaterialGenerator.Material.cpp +++ b/Source/Engine/Tools/MaterialGenerator/MaterialGenerator.Material.cpp @@ -351,15 +351,15 @@ void MaterialGenerator::ProcessGroupMaterial(Box* box, Node* node, Value& value) // Rotator case 27: { - auto uv = tryGetValue(node->GetBox(0), getUVs).AsVector2(); - auto center = tryGetValue(node->GetBox(1), Value::Zero).AsVector2(); - auto rotationAngle = tryGetValue(node->GetBox(2), Value::Zero).AsFloat(); + const auto uv = tryGetValue(node->GetBox(0), getUVs).AsVector2(); + const auto center = tryGetValue(node->GetBox(1), Value::Zero).AsVector2(); + const auto rotationAngle = tryGetValue(node->GetBox(2), Value::Zero).AsFloat(); - const auto x1 = writeLocal(ValueType::Vector2, String::Format(TEXT("({0} * -1) + {1}"), center.Value, uv.Value), node); - const auto raCosSin = writeLocal(ValueType::Vector2, String::Format(TEXT("float2(cos({0}), sin({0}))"), rotationAngle.Value), node); + auto x1 = writeLocal(ValueType::Vector2, String::Format(TEXT("({0} * -1) + {1}"), center.Value, uv.Value), node); + auto raCosSin = writeLocal(ValueType::Vector2, String::Format(TEXT("float2(cos({0}), sin({0}))"), rotationAngle.Value), node); - const auto dotB1 = writeLocal(ValueType::Vector2, String::Format(TEXT("float2({0}.x, {0}.y * -1)"), raCosSin.Value), node); - const auto dotB2 = writeLocal(ValueType::Vector2, String::Format(TEXT("float2({0}.y, {0}.x)"), raCosSin.Value), node); + auto dotB1 = writeLocal(ValueType::Vector2, String::Format(TEXT("float2({0}.x, {0}.y * -1)"), raCosSin.Value), node); + auto dotB2 = writeLocal(ValueType::Vector2, String::Format(TEXT("float2({0}.y, {0}.x)"), raCosSin.Value), node); value = writeLocal(ValueType::Vector2, String::Format(TEXT("{3} + float2(dot({0},{1}), dot({0},{2}))"), x1.Value, dotB1.Value, dotB2.Value, center.Value), node); break; @@ -367,11 +367,11 @@ void MaterialGenerator::ProcessGroupMaterial(Box* box, Node* node, Value& value) // Sphere Mask case 28: { - Value a = tryGetValue(node->GetBox(0), 0, Value::Zero); - Value b = tryGetValue(node->GetBox(1), 1, Value::Zero).Cast(a.Type); - Value radius = tryGetValue(node->GetBox(2), node->Values[0]).AsFloat(); - Value hardness = tryGetValue(node->GetBox(3), node->Values[1]).AsFloat(); - Value invert = tryGetValue(node->GetBox(4), node->Values[2]).AsBool(); + const auto a = tryGetValue(node->GetBox(0), 0, Value::Zero); + const auto b = tryGetValue(node->GetBox(1), 1, Value::Zero).Cast(a.Type); + const auto radius = tryGetValue(node->GetBox(2), node->Values[0]).AsFloat(); + const auto hardness = tryGetValue(node->GetBox(3), node->Values[1]).AsFloat(); + const auto invert = tryGetValue(node->GetBox(4), node->Values[2]).AsBool(); // Get distance and apply radius auto x1 = writeLocal(ValueType::Float, String::Format(TEXT("distance({0},{1}) * (1 / {2})"), a.Value, b.Value, radius.Value), node); @@ -385,9 +385,9 @@ void MaterialGenerator::ProcessGroupMaterial(Box* box, Node* node, Value& value) // Tiling & Offset case 29: { - auto uv = tryGetValue(node->GetBox(0), getUVs).AsVector2(); - auto tiling = tryGetValue(node->GetBox(1), node->Values[0]).AsVector2(); - auto offset = tryGetValue(node->GetBox(2), node->Values[1]).AsVector2(); + const auto uv = tryGetValue(node->GetBox(0), getUVs).AsVector2(); + const auto tiling = tryGetValue(node->GetBox(1), node->Values[0]).AsVector2(); + const auto offset = tryGetValue(node->GetBox(2), node->Values[1]).AsVector2(); value = writeLocal(ValueType::Vector2, String::Format(TEXT("{0} * {1} + {2}"), uv.Value, tiling.Value, offset.Value), node); break; @@ -395,7 +395,7 @@ void MaterialGenerator::ProcessGroupMaterial(Box* box, Node* node, Value& value) // DDX case 30: { - auto inValue = tryGetValue(node->GetBox(0), 0, Value::Zero); + const auto inValue = tryGetValue(node->GetBox(0), 0, Value::Zero); value = writeLocal(inValue.Type, String::Format(TEXT("ddx({0})"), inValue.Value), node); break; @@ -403,10 +403,39 @@ void MaterialGenerator::ProcessGroupMaterial(Box* box, Node* node, Value& value) // DDY case 31: { - auto inValue = tryGetValue(node->GetBox(0), 0, Value::Zero); + const auto inValue = tryGetValue(node->GetBox(0), 0, Value::Zero); value = writeLocal(inValue.Type, String::Format(TEXT("ddy({0})"), inValue.Value), node); break; + } + // Sign + case 32: + { + const auto inValue = tryGetValue(node->GetBox(0), 0, Value::Zero); + + value = writeLocal(ValueType::Float, String::Format(TEXT("sign({0})"), inValue.Value), node); + break; + } + // Any + case 33: + { + const auto inValue = tryGetValue(node->GetBox(0), 0, Value::Zero); + + value = writeLocal(ValueType::Bool, String::Format(TEXT("any({0})"), inValue.Value), node); + break; + } + // All + case 34: + { + const auto inValue = tryGetValue(node->GetBox(0), 0, Value::Zero); + + value = writeLocal(ValueType::Bool, String::Format(TEXT("all({0})"), inValue.Value), node); + break; + } + // Blackbody + case 35: + { + break; } default: break; From e75ca148c0aaa5e5cd9f68b6ed96ecbb104d7806 Mon Sep 17 00:00:00 2001 From: "W2.Wizard" Date: Sat, 30 Jan 2021 23:21:41 +0100 Subject: [PATCH 2/5] Added blackbody --- Source/Editor/Surface/Archetypes/Material.cs | 6 +++++- .../MaterialGenerator.Material.cpp | 17 +++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/Source/Editor/Surface/Archetypes/Material.cs b/Source/Editor/Surface/Archetypes/Material.cs index f8fc025a8..ceb9921cb 100644 --- a/Source/Editor/Surface/Archetypes/Material.cs +++ b/Source/Editor/Surface/Archetypes/Material.cs @@ -805,9 +805,13 @@ namespace FlaxEditor.Surface.Archetypes Description = "Simulates black body radiation via a given temperature in kelvin", Flags = NodeFlags.MaterialGraph, Size = new Vector2(120, 25), + DefaultValues = new object[] + { + 0.0f, + }, Elements = new[] { - NodeElementArchetype.Factory.Input(0, "Temp", true, typeof(float), 0), + NodeElementArchetype.Factory.Input(0, "Temp", true, typeof(float), 0, 0), NodeElementArchetype.Factory.Output(0, string.Empty, typeof(Vector3), 1), } }, diff --git a/Source/Engine/Tools/MaterialGenerator/MaterialGenerator.Material.cpp b/Source/Engine/Tools/MaterialGenerator/MaterialGenerator.Material.cpp index d851e4b86..34ed16d9a 100644 --- a/Source/Engine/Tools/MaterialGenerator/MaterialGenerator.Material.cpp +++ b/Source/Engine/Tools/MaterialGenerator/MaterialGenerator.Material.cpp @@ -435,6 +435,23 @@ void MaterialGenerator::ProcessGroupMaterial(Box* box, Node* node, Value& value) // Blackbody case 35: { + const auto temperature = tryGetValue(node->GetBox(0), node->Values[0]).AsFloat(); + + // Value X + auto x = writeLocal(ValueType::Float, String::Format(TEXT("56100000.0f * pow({0}, (-3.0f / 2.0f)) + 148.0f"), temperature.Value), node); + + // Value Y + auto y = writeLocal(ValueType::Float, String::Format(TEXT("{0} > 6500.0f ? 35200000.0f * pow({0}, (-3.0f / 2.0f)) + 184.0f : 100.04f * log({0}) - 623.6f"), temperature.Value), node); + + // Value Z + auto z = writeLocal(ValueType::Float, String::Format(TEXT("194.18f * log({0}) - 1448.6f"), temperature.Value), node); + + // Final color + auto color = writeLocal(ValueType::Vector3, String::Format(TEXT("float3({0}, {1}, {2})"), x.Value, y.Value, z.Value), node); + color = writeLocal(ValueType::Vector3, String::Format(TEXT("clamp({0}, 0.0f, 255.0f) / 255.0f"), color.Value), node); + color = writeLocal(ValueType::Vector3, String::Format(TEXT("{1} < 1000.0f ? {0} * {1}/1000.0f : {0}"), color.Value, temperature.Value), node); + + value = color; break; } default: From bc6de6932b9f2b4fc7c23ebc688ac8717035a01b Mon Sep 17 00:00:00 2001 From: "W2.Wizard" Date: Sat, 30 Jan 2021 23:28:53 +0100 Subject: [PATCH 3/5] Update MaterialGenerator.Material.cpp Already evaluate calculation instead --- .../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 34ed16d9a..31d2ba36c 100644 --- a/Source/Engine/Tools/MaterialGenerator/MaterialGenerator.Material.cpp +++ b/Source/Engine/Tools/MaterialGenerator/MaterialGenerator.Material.cpp @@ -438,10 +438,10 @@ void MaterialGenerator::ProcessGroupMaterial(Box* box, Node* node, Value& value) const auto temperature = tryGetValue(node->GetBox(0), node->Values[0]).AsFloat(); // Value X - auto x = writeLocal(ValueType::Float, String::Format(TEXT("56100000.0f * pow({0}, (-3.0f / 2.0f)) + 148.0f"), temperature.Value), node); + auto x = writeLocal(ValueType::Float, String::Format(TEXT("56100000.0f * pow({0}, -1) + 148.0f"), temperature.Value), node); // Value Y - auto y = writeLocal(ValueType::Float, String::Format(TEXT("{0} > 6500.0f ? 35200000.0f * pow({0}, (-3.0f / 2.0f)) + 184.0f : 100.04f * log({0}) - 623.6f"), temperature.Value), node); + auto y = writeLocal(ValueType::Float, String::Format(TEXT("{0} > 6500.0f ? 35200000.0f * pow({0}, -1) + 184.0f : 100.04f * log({0}) - 623.6f"), temperature.Value), node); // Value Z auto z = writeLocal(ValueType::Float, String::Format(TEXT("194.18f * log({0}) - 1448.6f"), temperature.Value), node); From 28d16fd620aec1864ad1208a053b02cda2b18cc7 Mon Sep 17 00:00:00 2001 From: "W2.Wizard" Date: Sat, 30 Jan 2021 23:32:35 +0100 Subject: [PATCH 4/5] Add notice --- .../Tools/MaterialGenerator/MaterialGenerator.Material.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Source/Engine/Tools/MaterialGenerator/MaterialGenerator.Material.cpp b/Source/Engine/Tools/MaterialGenerator/MaterialGenerator.Material.cpp index 31d2ba36c..83251089f 100644 --- a/Source/Engine/Tools/MaterialGenerator/MaterialGenerator.Material.cpp +++ b/Source/Engine/Tools/MaterialGenerator/MaterialGenerator.Material.cpp @@ -435,6 +435,8 @@ void MaterialGenerator::ProcessGroupMaterial(Box* box, Node* node, Value& value) // Blackbody case 35: { + // Based on unity's implementation by using data gathered by Mitchell Charity. + const auto temperature = tryGetValue(node->GetBox(0), node->Values[0]).AsFloat(); // Value X From 4949ea4b3b48283c6585a3bb7194104f903256e9 Mon Sep 17 00:00:00 2001 From: "W2.Wizard" Date: Wed, 3 Feb 2021 22:24:08 +0100 Subject: [PATCH 5/5] Applied review changes --- .../Tools/MaterialGenerator/MaterialGenerator.Material.cpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/Source/Engine/Tools/MaterialGenerator/MaterialGenerator.Material.cpp b/Source/Engine/Tools/MaterialGenerator/MaterialGenerator.Material.cpp index 83251089f..16d9623ec 100644 --- a/Source/Engine/Tools/MaterialGenerator/MaterialGenerator.Material.cpp +++ b/Source/Engine/Tools/MaterialGenerator/MaterialGenerator.Material.cpp @@ -435,7 +435,7 @@ void MaterialGenerator::ProcessGroupMaterial(Box* box, Node* node, Value& value) // Blackbody case 35: { - // Based on unity's implementation by using data gathered by Mitchell Charity. + // Reference: Mitchell Charity, http://www.vendian.org/mncharity/dir3/blackbody/ const auto temperature = tryGetValue(node->GetBox(0), node->Values[0]).AsFloat(); @@ -451,9 +451,7 @@ void MaterialGenerator::ProcessGroupMaterial(Box* box, Node* node, Value& value) // Final color auto color = writeLocal(ValueType::Vector3, String::Format(TEXT("float3({0}, {1}, {2})"), x.Value, y.Value, z.Value), node); color = writeLocal(ValueType::Vector3, String::Format(TEXT("clamp({0}, 0.0f, 255.0f) / 255.0f"), color.Value), node); - color = writeLocal(ValueType::Vector3, String::Format(TEXT("{1} < 1000.0f ? {0} * {1}/1000.0f : {0}"), color.Value, temperature.Value), node); - - value = color; + value = writeLocal(ValueType::Vector3, String::Format(TEXT("{1} < 1000.0f ? {0} * {1}/1000.0f : {0}"), color.Value, temperature.Value), node); break; } default: