Browse Source

Added comparison math node modes

CPKreuz 9 months ago
parent
commit
36fc6d6fd1

+ 18 - 4
src/PixiEditor.ChangeableDocument/Changeables/Graph/Nodes/MathNode.cs

@@ -21,6 +21,8 @@ public class MathNode : Node
     
     public FuncInputProperty<Float1> Y { get; }
     
+    public FuncInputProperty<Float1> Z { get; }
+    
     public MathNode()
     {
         Result = CreateFuncOutput<Float1>(nameof(Result), "RESULT", Calculate);
@@ -28,11 +30,12 @@ public class MathNode : Node
         Clamp = CreateInput(nameof(Clamp), "CLAMP", false);
         X = CreateFuncInput<Float1>(nameof(X), "X", 0d);
         Y = CreateFuncInput<Float1>(nameof(Y), "Y", 0d);
+        Z = CreateFuncInput<Float1>(nameof(Z), "Z", 0d);
     }
 
     private Float1 Calculate(FuncContext context)
     {
-        var (x, y) = GetValues(context);
+        var (x, y, z) = GetValues(context);
 
         if (context.HasContext)
         {
@@ -45,6 +48,11 @@ public class MathNode : Node
                 MathNodeMode.Sin => ShaderMath.Sin(x),
                 MathNodeMode.Cos => ShaderMath.Cos(x),
                 MathNodeMode.Tan => ShaderMath.Tan(x),
+                MathNodeMode.GreaterThan => ShaderMath.GreaterThan(x, y),
+                MathNodeMode.GreaterThanOrEqual => ShaderMath.GreaterThanOrEqual(x, y),
+                MathNodeMode.LessThan => ShaderMath.LessThan(x, y),
+                MathNodeMode.LessThanOrEqual => ShaderMath.LessThanOrEqual(x, y),
+                MathNodeMode.Compare => ShaderMath.Compare(x, y, z)
             };
 
             if (Clamp.Value)
@@ -57,7 +65,8 @@ public class MathNode : Node
 
         var xConst = x.ConstantValue;
         var yConst = y.ConstantValue;
-            
+        var zConst = z.ConstantValue;
+        
         var constValue = Mode.Value switch
         {
             MathNodeMode.Add => xConst + yConst,
@@ -67,14 +76,19 @@ public class MathNode : Node
             MathNodeMode.Sin => Math.Sin(xConst),
             MathNodeMode.Cos => Math.Cos(xConst),
             MathNodeMode.Tan => Math.Tan(xConst),
+            MathNodeMode.GreaterThan => xConst > yConst ? 1 : 0,
+            MathNodeMode.GreaterThanOrEqual => xConst >= yConst ? 1 : 0,
+            MathNodeMode.LessThan => xConst < yConst ? 1 : 0,
+            MathNodeMode.LessThanOrEqual => xConst <= yConst ? 1 : 0,
+            MathNodeMode.Compare => Math.Abs(xConst - yConst) < zConst ? 1 : 0
         };
             
         return new Float1(string.Empty) { ConstantValue = constValue };
     }
 
-    private (Float1 xConst, Float1 y) GetValues(FuncContext context)
+    private (Float1 xConst, Float1 y, Float1 z) GetValues(FuncContext context)
     {
-        return (context.GetValue(X), context.GetValue(Y));
+        return (context.GetValue(X), context.GetValue(Y), context.GetValue(Z));
     }
 
 

+ 18 - 0
src/PixiEditor.ChangeableDocument/Enums/MathNodeMode.cs

@@ -18,9 +18,27 @@ public enum MathNodeMode
     Cos,
     [Description("TAN")]
     Tan,
+    [Description("GREATER_THAN")]
+    GreaterThan,
+    [Description("GREATER_THAN_OR_EQUAL")]
+    GreaterThanOrEqual,
+    [Description("LESS_THAN")]
+    LessThan,
+    [Description("LESS_THAN_OR_EQUAL")]
+    LessThanOrEqual,
+    [Description("COMPARE")]
+    Compare
 }
 
 public static class MathNodeModeExtensions
 {
     public static bool UsesYValue(this MathNodeMode mode) => !(mode is >= MathNodeMode.Sin and <= MathNodeMode.Tan);
+
+    public static bool UsesZValue(this MathNodeMode mode) => mode is MathNodeMode.Compare;
+
+    public static (string x, string y, string z) GetNaming(this MathNodeMode mode) => mode switch
+    {
+        MathNodeMode.Compare => ("VALUE", "TARGET", "EPSILON"),
+        _ => ("X", "Y", "Z")
+    };
 }

+ 17 - 0
src/PixiEditor/ViewModels/Document/Nodes/MathNodeViewModel.cs

@@ -14,12 +14,18 @@ internal class MathNodeViewModel : NodeViewModel<MathNode>
 {
     private GenericEnumPropertyViewModel Mode { get; set; }
     
+    private NodePropertyViewModel X { get; set; }
+    
     private NodePropertyViewModel Y { get; set; }
     
+    private NodePropertyViewModel Z { get; set; }
+    
     public override void OnInitialized()
     {
         Mode = FindInputProperty("Mode") as GenericEnumPropertyViewModel;
+        X = FindInputProperty("X");
         Y = FindInputProperty("Y");
+        Z = FindInputProperty("Z");
         
         Mode.ValueChanged += ModeChanged;
     }
@@ -31,5 +37,16 @@ internal class MathNodeViewModel : NodeViewModel<MathNode>
 
         DisplayName = mode.GetDescription();
         Y.IsVisible = mode.UsesYValue();
+        Z.IsVisible = mode.UsesZValue();
+
+        var (x, y, z) = mode.GetNaming();
+
+        x = new LocalizedString(x);
+        y = new LocalizedString(y);
+        z = new LocalizedString(z);
+
+        X.DisplayName = x;
+        Y.DisplayName = y;
+        Z.DisplayName = z;
     }
 }