Browse Source

Apply filter node and better previews

flabbet 10 months ago
parent
commit
9e85a08e87

+ 37 - 22
src/PixiEditor.ChangeableDocument/Changeables/Graph/Nodes/FilterNodes/ApplyFilterNode.cs

@@ -1,45 +1,60 @@
-using PixiEditor.ChangeableDocument.Helpers;
+using PixiEditor.ChangeableDocument.Changeables.Graph.Interfaces;
+using PixiEditor.ChangeableDocument.Helpers;
 using PixiEditor.ChangeableDocument.Rendering;
 using PixiEditor.DrawingApi.Core;
+using PixiEditor.DrawingApi.Core.Surfaces;
 using PixiEditor.DrawingApi.Core.Surfaces.PaintImpl;
+using PixiEditor.Numerics;
 
 namespace PixiEditor.ChangeableDocument.Changeables.Graph.Nodes.FilterNodes;
 
 [NodeInfo("ApplyFilter")]
-public class ApplyFilterNode : Node
+public class ApplyFilterNode : RenderNode, IRenderInput
 {
     private Paint _paint = new();
-    
-    
-    public OutputProperty<Texture?> Output { get; }
-
-    public InputProperty<Texture?> Input { get; }
-    
     public InputProperty<Filter?> Filter { get; }
-    
-    private Texture _workingSurface;
+
+    public RenderInputProperty Background { get; }
 
     public ApplyFilterNode()
     {
-        Output = CreateOutput<Texture>(nameof(Output), "IMAGE", null);
-        Input = CreateInput<Texture>(nameof(Input), "IMAGE", null);
-        Filter = CreateInput<Filter>(nameof(Filter), "FILTER", null);
+        Background = CreateRenderInput("Input", "IMAGE");
+        Filter = CreateInput<Filter>("Filter", "FILTER", null);
     }
-    
-    protected override void OnExecute(RenderContext context)
+
+    protected override void OnPaint(RenderContext context, DrawingSurface surface)
     {
-        if (Input.Value is not { } input)
+        _paint.SetFilters(Filter.Value);
+        var layer = surface.Canvas.SaveLayer(_paint);
+        
+        Background.Value.Paint(context, surface);
+        
+        surface.Canvas.RestoreToCount(layer);
+    }
+
+    public override RectD? GetPreviewBounds(int frame, string elementToRenderName = "")
+    {
+        if (Background.Connection != null && Background.Connection.Node is IPreviewRenderable previousPreview)
         {
-            return;
+            return previousPreview.GetPreviewBounds(frame, elementToRenderName);
         }
         
-        _paint.SetFilters(Filter.Value);
-        
-        _workingSurface = RequestTexture(0, input.Size, true);
+        return null;
+    }
+
+    public override bool RenderPreview(DrawingSurface renderOn, ChunkResolution resolution, int frame,
+        string elementToRenderName)
+    {
+        if(Background.Value == null)
+            return false;
+
+        using RenderContext context = new(renderOn, frame, ChunkResolution.Full, VecI.One);
         
-        _workingSurface.DrawingSurface.Canvas.DrawSurface(input.DrawingSurface, 0, 0, _paint);
+        int layer = renderOn.Canvas.SaveLayer(_paint);
+        Background.Value.Paint(context, renderOn);
+        renderOn.Canvas.RestoreToCount(layer);
 
-        Output.Value = _workingSurface;
+        return true;
     }
 
     public override Node CreateCopy() => new ApplyFilterNode();

+ 21 - 1
src/PixiEditor.ChangeableDocument/Changeables/Graph/Nodes/MergeNode.cs

@@ -76,8 +76,28 @@ public class MergeNode : RenderNode
         {
             return null;
         }
+
+        RectD? totalBounds = null; 
+        
+        if (Top.Connection != null && Top.Connection.Node is IPreviewRenderable topPreview)
+        {
+            var topBounds = topPreview.GetPreviewBounds(frame, elementToRenderName);
+            if (topBounds != null)
+            {
+                totalBounds = totalBounds?.Union(topBounds.Value) ?? topBounds;
+            }
+        }
+        
+        if (Bottom.Connection != null && Bottom.Connection.Node is IPreviewRenderable bottomPreview)
+        {
+            var bottomBounds = bottomPreview.GetPreviewBounds(frame, elementToRenderName);
+            if (bottomBounds != null)
+            {
+                totalBounds = totalBounds?.Union(bottomBounds.Value) ?? bottomBounds;
+            } 
+        }
         
-        return new RectD(VecI.Zero, new VecI(128, 128)); 
+        return totalBounds;
     }
 
     public override bool RenderPreview(DrawingSurface renderOn, ChunkResolution resolution, int frame, string elementToRenderName)

+ 7 - 4
src/PixiEditor/Views/Visuals/PreviewPainterControl.cs

@@ -80,19 +80,22 @@ internal class DrawPreviewOperation : SkiaDrawOperation
     public override void Render(ISkiaSharpApiLease lease)
     {
         RectD? previewBounds = PreviewPainter.PreviewRenderable.GetPreviewBounds(frame, PreviewPainter.ElementToRenderName);
-        if (PreviewPainter == null || previewBounds == null)
+        if (PreviewPainter == null)
         {
             return;
         }
 
         DrawingSurface target = DrawingSurface.FromNative(lease.SkSurface);
 
-        float x = (float)previewBounds.Value.Width; 
-        float y = (float)previewBounds.Value.Height; 
+        float x = (float)(previewBounds?.Width ?? 0); 
+        float y = (float)(previewBounds?.Height ?? 0);
 
         target.Canvas.Save();
 
-        UniformScale(x, y, target, previewBounds.Value);
+        if (previewBounds != null)
+        {
+            UniformScale(x, y, target, previewBounds.Value);
+        }
 
         // TODO: Implement ChunkResolution and frame
         PreviewPainter.Paint(target, ChunkResolution.Full, frame);