Browse Source

Converted noise node to render node

flabbet 10 months ago
parent
commit
22fb09dec5

+ 34 - 25
src/PixiEditor.ChangeableDocument/Changeables/Graph/Nodes/NoiseNode.cs

@@ -4,13 +4,14 @@ using PixiEditor.DrawingApi.Core;
 using PixiEditor.DrawingApi.Core.ColorsImpl;
 using PixiEditor.DrawingApi.Core.ColorsImpl;
 using PixiEditor.DrawingApi.Core.Numerics;
 using PixiEditor.DrawingApi.Core.Numerics;
 using PixiEditor.DrawingApi.Core.Shaders;
 using PixiEditor.DrawingApi.Core.Shaders;
+using PixiEditor.DrawingApi.Core.Surfaces;
 using PixiEditor.DrawingApi.Core.Surfaces.PaintImpl;
 using PixiEditor.DrawingApi.Core.Surfaces.PaintImpl;
 using PixiEditor.Numerics;
 using PixiEditor.Numerics;
 
 
 namespace PixiEditor.ChangeableDocument.Changeables.Graph.Nodes;
 namespace PixiEditor.ChangeableDocument.Changeables.Graph.Nodes;
 
 
 [NodeInfo("Noise")]
 [NodeInfo("Noise")]
-public class NoiseNode : Node
+public class NoiseNode : RenderNode
 {
 {
     private double previousScale = double.NaN;
     private double previousScale = double.NaN;
     private double previousSeed = double.NaN;
     private double previousSeed = double.NaN;
@@ -23,10 +24,7 @@ public class NoiseNode : Node
     private static readonly ColorFilter grayscaleFilter = ColorFilter.CreateColorMatrix(
     private static readonly ColorFilter grayscaleFilter = ColorFilter.CreateColorMatrix(
         ColorMatrix.MapAlphaToRedGreenBlue + ColorMatrix.OpaqueAlphaOffset);
         ColorMatrix.MapAlphaToRedGreenBlue + ColorMatrix.OpaqueAlphaOffset);
 
 
-    public OutputProperty<Texture> Noise { get; }
-
     public InputProperty<NoiseType> NoiseType { get; }
     public InputProperty<NoiseType> NoiseType { get; }
-    public InputProperty<VecI> Size { get; }
 
 
     public InputProperty<VecD> Offset { get; }
     public InputProperty<VecD> Offset { get; }
     
     
@@ -38,11 +36,7 @@ public class NoiseNode : Node
 
 
     public NoiseNode()
     public NoiseNode()
     {
     {
-        Noise = CreateOutput<Texture>(nameof(Noise), "NOISE", null);
         NoiseType = CreateInput(nameof(NoiseType), "NOISE_TYPE", Nodes.NoiseType.FractalPerlin);
         NoiseType = CreateInput(nameof(NoiseType), "NOISE_TYPE", Nodes.NoiseType.FractalPerlin);
-        Size = CreateInput(nameof(Size), "SIZE", new VecI(64, 64))
-            .WithRules(v => v.Min(VecI.One)
-            );
 
 
         Offset = CreateInput(nameof(Offset), "OFFSET", new VecD(0d, 0d));
         Offset = CreateInput(nameof(Offset), "OFFSET", new VecD(0d, 0d));
         
         
@@ -53,7 +47,7 @@ public class NoiseNode : Node
         Seed = CreateInput(nameof(Seed), "SEED", 0d);
         Seed = CreateInput(nameof(Seed), "SEED", 0d);
     }
     }
 
 
-    protected override void OnExecute(RenderContext context)
+    protected override DrawingSurface? ExecuteRender(RenderContext context)
     {
     {
         if (Math.Abs(previousScale - Scale.Value) > 0.000001
         if (Math.Abs(previousScale - Scale.Value) > 0.000001
             || previousSeed != Seed.Value
             || previousSeed != Seed.Value
@@ -64,15 +58,13 @@ public class NoiseNode : Node
         {
         {
             if (Scale.Value < 0.000001)
             if (Scale.Value < 0.000001)
             {
             {
-                Noise.Value = null;
-                return;
+                return null;
             }
             }
 
 
             var shader = SelectShader();
             var shader = SelectShader();
             if (shader == null)
             if (shader == null)
             {
             {
-                Noise.Value = null;
-                return;
+                return null;
             }
             }
 
 
             paint.Shader = shader;
             paint.Shader = shader;
@@ -86,24 +78,41 @@ public class NoiseNode : Node
             previousNoiseType = NoiseType.Value;
             previousNoiseType = NoiseType.Value;
         }
         }
 
 
-        var size = Size.Value;
 
 
-        if (size.X < 1 || size.Y < 1)
-        {
-            Noise.Value = null;
-            return;
-        }
+        var workingSurface = Output.GetFirstRenderTarget(context);
+
+        RenderNoise(workingSurface);
+
+        return workingSurface;
+    }
+
+    private void RenderNoise(DrawingSurface workingSurface)
+    {
+        int saved = workingSurface.Canvas.Save();
+        workingSurface.Canvas.Translate(-(float)Offset.Value.X, -(float)Offset.Value.Y);
+        workingSurface.Canvas.DrawPaint(paint);
+        workingSurface.Canvas.RestoreToCount(saved);
+    }
 
 
-        var workingSurface = RequestTexture(0, size);
+    public override RectD? GetPreviewBounds(int frame, string elementToRenderName = "")
+    {
+        return new RectD(0, 0, 128, 128); 
+    }
 
 
-        workingSurface.DrawingSurface.Canvas.Save();
-        workingSurface.DrawingSurface.Canvas.Translate(-(float)Offset.Value.X, -(float)Offset.Value.Y);
+    public override bool RenderPreview(DrawingSurface renderOn, ChunkResolution resolution, int frame, string elementToRenderName)
+    {
+        var shader = SelectShader();
+        if (shader == null)
+        {
+            return false;
+        }
         
         
-        workingSurface.DrawingSurface.Canvas.DrawPaint(paint);
+        paint.Shader = shader;
+        paint.ColorFilter = grayscaleFilter;
         
         
-        workingSurface.DrawingSurface.Canvas.Restore();
+        RenderNoise(renderOn);
 
 
-        Noise.Value = workingSurface;
+        return true;
     }
     }
 
 
     private Shader SelectShader()
     private Shader SelectShader()

+ 1 - 0
src/PixiEditor/Views/Visuals/PreviewPainterImage.cs

@@ -23,6 +23,7 @@ public class PreviewPainterImage : IImage
     {
     {
         if (PreviewPainter.PreviewRenderable.GetPreviewBounds(FrameToRender) == null) return;
         if (PreviewPainter.PreviewRenderable.GetPreviewBounds(FrameToRender) == null) return;
         
         
+        context.PushClip(destRect);
         using DrawPreviewOperation drawPreviewOperation = new DrawPreviewOperation(destRect, PreviewPainter, FrameToRender); 
         using DrawPreviewOperation drawPreviewOperation = new DrawPreviewOperation(destRect, PreviewPainter, FrameToRender); 
         context.Custom(drawPreviewOperation);
         context.Custom(drawPreviewOperation);
     }
     }