Browse Source

Initial create image node

CPKreuz 1 year ago
parent
commit
8370ca64b9

+ 8 - 0
src/PixiEditor.ChangeableDocument/Changeables/Graph/Interfaces/IFieldContext.cs

@@ -0,0 +1,8 @@
+using PixiEditor.Numerics;
+
+namespace PixiEditor.ChangeableDocument.Changeables.Graph.Interfaces;
+
+public interface IFieldContext
+{
+    public VecD Position { get; }
+}

+ 47 - 0
src/PixiEditor.ChangeableDocument/Changeables/Graph/Nodes/CombineColor.cs

@@ -0,0 +1,47 @@
+using PixiEditor.ChangeableDocument.Changeables.Animations;
+using PixiEditor.ChangeableDocument.Changeables.Graph.Interfaces;
+using PixiEditor.DrawingApi.Core.ColorsImpl;
+
+namespace PixiEditor.ChangeableDocument.Changeables.Graph.Nodes;
+
+public class CombineColor : Node
+{
+    public OutputProperty<Func<IFieldContext, Color>> Color { get; }
+    
+    public InputProperty<Func<IFieldContext, double>> R { get; }
+    
+    public InputProperty<Func<IFieldContext, double>> G { get; }
+    
+    public InputProperty<Func<IFieldContext, double>> B { get; }
+    
+    public InputProperty<Func<IFieldContext, double>> A { get; }
+
+    public CombineColor()
+    {
+        Color = CreateOutput<Func<IFieldContext, Color>>(nameof(Color), "COLOR", GetColor);
+        
+        R = CreateInput<Func<IFieldContext, double>>("R", "R", _ => 0);
+        G = CreateInput<Func<IFieldContext, double>>("G", "G", _ => 0);
+        B = CreateInput<Func<IFieldContext, double>>("B", "B", _ => 0);
+        A = CreateInput<Func<IFieldContext, double>>("A", "A", _ => 1);
+    }
+
+    private Color GetColor(IFieldContext ctx)
+    {
+        var r = R.Value(ctx) * 255;
+        var g = G.Value(ctx) * 255;
+        var b = B.Value(ctx) * 255;
+        var a = A.Value(ctx) * 255;
+
+        return new Color((byte)r, (byte)g, (byte)b, (byte)a);
+    }
+    
+    protected override ChunkyImage? OnExecute(KeyFrameTime frameTime)
+    {
+        return null;
+    }
+
+    public override bool Validate() => true;
+
+    public override Node CreateCopy() => new SeparateVecI();
+}

+ 57 - 0
src/PixiEditor.ChangeableDocument/Changeables/Graph/Nodes/CreateImageNode.cs

@@ -0,0 +1,57 @@
+using PixiEditor.ChangeableDocument.Changeables.Animations;
+using PixiEditor.ChangeableDocument.Changeables.Graph.Interfaces;
+using PixiEditor.DrawingApi.Core.ColorsImpl;
+using PixiEditor.DrawingApi.Core.Surface;
+using PixiEditor.Numerics;
+
+namespace PixiEditor.ChangeableDocument.Changeables.Graph.Nodes;
+
+public class CreateImageNode : Node
+{
+    public InputProperty<int> Width { get; }
+    public InputProperty<int> Height { get; }
+    public InputProperty<Func<IFieldContext, Color>> Color { get; }
+    public OutputProperty<ChunkyImage> Output { get; }
+    
+    public CreateImageNode() 
+    {
+        Width = CreateInput<int>("Width", "WIDTH", 32);
+        Height = CreateInput<int>("Height", "HEIGHT", 32);
+        Color = CreateInput<Func<IFieldContext, Color>>("Color", "COLOR", _ => new Color(0, 0, 0, 255));
+        Output = CreateOutput<ChunkyImage>("Output", "OUTPUT", null);
+    }
+
+    protected override ChunkyImage? OnExecute(KeyFrameTime frameTime)
+    {
+        var width = Width.Value;
+        var height = Height.Value;
+        
+        Output.Value = new ChunkyImage(new VecI(Width.Value, Height.Value));
+
+        for (int y = 0; y < width; y++)
+        {
+            for (int x = 0; x < height; x++)
+            {
+                var context = new CreateImageContext(new VecD((double)x / width, (double)y / width));
+                var color = Color.Value(context);
+
+                Output.Value.EnqueueDrawPixel(new VecI(x, y), color, BlendMode.Src);
+            }
+        }
+
+        Output.Value.CommitChanges();
+
+        return Output.Value;
+    }
+
+    public override bool Validate()
+    {
+        return Width.Value > 0 && Height.Value > 0;
+    }
+
+    public override Node CreateCopy() => new CreateImageNode();
+
+    record CreateImageContext(VecD Position) : IFieldContext
+    {
+    }
+}

+ 24 - 0
src/PixiEditor.ChangeableDocument/Changeables/Graph/Nodes/ImageSpaceNode.cs

@@ -0,0 +1,24 @@
+using PixiEditor.ChangeableDocument.Changeables.Animations;
+using PixiEditor.ChangeableDocument.Changeables.Graph.Interfaces;
+using PixiEditor.Numerics;
+
+namespace PixiEditor.ChangeableDocument.Changeables.Graph.Nodes;
+
+public class ImageSpaceNode : Node
+{
+    public OutputProperty<Func<IFieldContext, VecD>> Position { get; }
+
+    public ImageSpaceNode()
+    {
+        Position = CreateOutput<Func<IFieldContext, VecD>>(nameof(Position), "PIXEL_COORDINATE", ctx => ctx.Position);
+    }
+    
+    protected override ChunkyImage? OnExecute(KeyFrameTime frameTime)
+    {
+        return null;
+    }
+
+    public override bool Validate() => true;
+
+    public override Node CreateCopy() => new ImageSpaceNode();
+}

+ 30 - 0
src/PixiEditor.ChangeableDocument/Changeables/Graph/Nodes/SeparateXY.cs

@@ -0,0 +1,30 @@
+using PixiEditor.ChangeableDocument.Changeables.Animations;
+using PixiEditor.ChangeableDocument.Changeables.Graph.Interfaces;
+using PixiEditor.Numerics;
+
+namespace PixiEditor.ChangeableDocument.Changeables.Graph.Nodes;
+
+public class SeparateVecI : Node
+{
+    public InputProperty<Func<IFieldContext, VecD>> Vector { get; }
+    
+    public OutputProperty<Func<IFieldContext, double>> X { get; }
+    
+    public OutputProperty<Func<IFieldContext, double>> Y { get; }
+
+    public SeparateVecI()
+    {
+        X = CreateOutput<Func<IFieldContext, double>>("X", "X", ctx => Vector.Value(ctx).X);
+        Y = CreateOutput<Func<IFieldContext, double>>("Y", "Y", ctx => Vector.Value(ctx).Y);
+        Vector = CreateInput<Func<IFieldContext, VecD>>("Vector", "VECTOR", _ => new VecD(0, 0));
+    }
+    
+    protected override ChunkyImage? OnExecute(KeyFrameTime frameTime)
+    {
+        return null;
+    }
+
+    public override bool Validate() => true;
+
+    public override Node CreateCopy() => new SeparateVecI();
+}