Browse Source

Conversions

flabbet 1 year ago
parent
commit
ed4cce466c

+ 11 - 1
src/PixiEditor.ChangeableDocument/Changeables/Graph/FuncInputProperty.cs

@@ -19,7 +19,17 @@ public class FuncInputProperty<T> : InputProperty<Func<FuncContext, T>>, IFuncIn
 
     protected override object FuncFactory(object toReturn)
     {
-        Func<FuncContext, T> func = _ => (T)toReturn;
+        Func<FuncContext, T> func = _ =>
+        {
+            if (typeof(T).IsAssignableTo(typeof(ShaderExpressionVariable)))
+            {
+                var shaderExpressionVariable = (ShaderExpressionVariable)Activator.CreateInstance(typeof(T), "");
+                shaderExpressionVariable.SetConstantValue(toReturn, ConversionTable.Convert);
+                return (T)(object)shaderExpressionVariable;
+            }
+            
+            return (T)toReturn;
+        };
         return func;
     }
 

+ 2 - 2
src/PixiEditor.ChangeableDocument/Changeables/Graph/Nodes/ModifyImageRightNode.cs

@@ -35,8 +35,8 @@ public class ModifyImageRightNode : Node, IPairNodeEnd
 
     public ModifyImageRightNode()
     {
-        Coordinate = CreateFuncInput(nameof(Coordinate), "UV", new Float2("coords", VecD.Zero));
-        Color = CreateFuncInput(nameof(Color), "COLOR", new Half4("", DrawingApi.Core.ColorsImpl.Color.Empty));
+        Coordinate = CreateFuncInput(nameof(Coordinate), "UV", new Float2("coords"));
+        Color = CreateFuncInput(nameof(Color), "COLOR", new Half4(""));
         Output = CreateOutput<Texture>(nameof(Output), "OUTPUT", null);
     }
 

+ 2 - 2
src/PixiEditor.DrawingApi.Core/Shaders/Generation/Expressions/Float1.cs

@@ -5,11 +5,11 @@
 /// </summary>
 /// <param name="name">Name of the variable in shader code</param>
 /// <param name="constant">Constant value of the variable.</param>
-public class Float1(string name, double constant) : ShaderExpressionVariable<double>(name, constant)
+public class Float1(string name) : ShaderExpressionVariable<double>(name)
 {
     public override string ConstantValueString => ConstantValue.ToString(System.Globalization.CultureInfo.InvariantCulture);
     
-    public static implicit operator Float1(double value) => new Float1("", value);
+    public static implicit operator Float1(double value) => new Float1("") { ConstantValue = value };
     
     public static explicit operator double(Float1 value) => value.ConstantValue;
 }

+ 4 - 8
src/PixiEditor.DrawingApi.Core/Shaders/Generation/Expressions/Float2.cs

@@ -2,12 +2,8 @@
 
 namespace PixiEditor.DrawingApi.Core.Shaders.Generation.Expressions;
 
-public class Float2(string name, VecD constant) : ShaderExpressionVariable<VecD>(name, constant)
+public class Float2(string name) : ShaderExpressionVariable<VecD>(name)
 {
-    public Float2(string name) : this(name, VecD.Zero)
-    {
-    }
-
     public override string ConstantValueString
     {
         get
@@ -22,7 +18,7 @@ public class Float2(string name, VecD constant) : ShaderExpressionVariable<VecD>
     {
         get
         {
-            return new Float1($"{UniformName}.x", ConstantValue.X);
+            return new Float1($"{UniformName}.x") { ConstantValue = ConstantValue.X };
         }
     }
     
@@ -30,10 +26,10 @@ public class Float2(string name, VecD constant) : ShaderExpressionVariable<VecD>
     {
         get
         {
-            return new Float1($"{UniformName}.y", ConstantValue.Y);
+            return new Float1($"{UniformName}.y") { ConstantValue = ConstantValue.Y };
         }
     }
     
-    public static implicit operator Float2(VecD value) => new Float2("", value);
+    public static implicit operator Float2(VecD value) => new Float2("") { ConstantValue = value };
     public static explicit operator VecD(Float2 value) => value.ConstantValue;
 }

+ 6 - 10
src/PixiEditor.DrawingApi.Core/Shaders/Generation/Expressions/Half4.cs

@@ -2,19 +2,15 @@
 
 namespace PixiEditor.DrawingApi.Core.Shaders.Generation.Expressions;
 
-public class Half4(string name, Color constant) : ShaderExpressionVariable<Color>(name, constant)
+public class Half4(string name) : ShaderExpressionVariable<Color>(name)
 {
-    public Half4(string name) : this(name, Colors.Transparent)
-    {
-    }
-
     public override string ConstantValueString => $"half4({ConstantValue.R}, {ConstantValue.G}, {ConstantValue.B}, {ConstantValue.A})";
     
-    public Float1 R => new Float1($"{UniformName}.r", ConstantValue.R);
-    public Float1 G => new Float1($"{UniformName}.g", ConstantValue.G);
-    public Float1 B => new Float1($"{UniformName}.b", ConstantValue.B);
-    public Float1 A => new Float1($"{UniformName}.a", ConstantValue.A);
+    public Float1 R => new Float1($"{UniformName}.r") { ConstantValue = ConstantValue.R };
+    public Float1 G => new Float1($"{UniformName}.g") { ConstantValue = ConstantValue.G };
+    public Float1 B => new Float1($"{UniformName}.b") { ConstantValue = ConstantValue.B };
+    public Float1 A => new Float1($"{UniformName}.a") { ConstantValue = ConstantValue.A };
     
-    public static implicit operator Half4(Color value) => new Half4("", value);
+    public static implicit operator Half4(Color value) => new Half4("") { ConstantValue = value };
     public static explicit operator Color(Half4 value) => value.ConstantValue;
 }

+ 1 - 1
src/PixiEditor.DrawingApi.Core/Shaders/Generation/Expressions/TextureSampler.cs

@@ -2,7 +2,7 @@
 
 public class TextureSampler : ShaderExpressionVariable<Texture>
 {
-    public TextureSampler(string name) : base(name, null)
+    public TextureSampler(string name) : base(name)
     {
     }
 

+ 1 - 1
src/PixiEditor.DrawingApi.Core/Shaders/Generation/ShaderBuilder.cs

@@ -102,7 +102,7 @@ public class ShaderBuilder
     public Float1 ConstructFloat1(Expression assignment)
     {
         string name = $"float_{_variables.Count}";
-        Float1 result = new Float1(name, 0);
+        Float1 result = new Float1(name);
         _variables.Add(result);
         
         _bodyBuilder.AppendLine($"float {name} = {assignment.ExpressionValue};");

+ 2 - 2
src/PixiEditor.DrawingApi.Core/Shaders/Generation/ShaderExpressionVariable.cs

@@ -23,10 +23,10 @@ public abstract class ShaderExpressionVariable(string name) : Expression
     }
 }
 
-public abstract class ShaderExpressionVariable<TConstant>(string name, TConstant constant)
+public abstract class ShaderExpressionVariable<TConstant>(string name)
     : ShaderExpressionVariable(name)
 {
-    public TConstant? ConstantValue { get; set; } = constant;
+    public TConstant? ConstantValue { get; set; }
 
     public override void SetConstantValue(object? value, Func<object, Type, object> convertFunc)
     {

+ 1 - 1
src/PixiEditor.UI.Common/Accents/Base.axaml

@@ -42,7 +42,6 @@
             <Color x:Key="ImageSocketColor">#99c47a</Color>
             <Color x:Key="BoolSocketColor">#68abdf</Color>
             <Color x:Key="FloatSocketColor">#ffc66d</Color>
-            <!-- TODO: How do we wanna handle floats and doubles? -->
             <Color x:Key="DoubleSocketColor">#efb66d</Color>
             <Color x:Key="ColorSocketColor">#99e4aa</Color>
             <Color x:Key="VecDSocketColor">#c984ca</Color>
@@ -95,6 +94,7 @@
             <SolidColorBrush x:Key="BooleanSocketBrush" Color="{StaticResource BoolSocketColor}"/>
             <SolidColorBrush x:Key="SingleSocketBrush" Color="{StaticResource FloatSocketColor}"/>
             <SolidColorBrush x:Key="DoubleSocketBrush" Color="{StaticResource DoubleSocketColor}"/>
+            <SolidColorBrush x:Key="Float1SocketBrush" Color="{StaticResource DoubleSocketColor}"/>
             <SolidColorBrush x:Key="ColorSocketBrush" Color="{StaticResource ColorSocketColor}"/>
             <SolidColorBrush x:Key="Half4SocketBrush" Color="{StaticResource ColorSocketColor}"/>
             <SolidColorBrush x:Key="VecDSocketBrush" Color="{StaticResource VecDSocketColor}"/>