Skip to content

Commit

Permalink
Add Vector node, considerably increase performance, fix bugs, improve…
Browse files Browse the repository at this point in the history
… IntegerUpDown again
  • Loading branch information
luca009 committed Aug 9, 2021
1 parent 161cbce commit 98eec14
Show file tree
Hide file tree
Showing 20 changed files with 451 additions and 94 deletions.
18 changes: 18 additions & 0 deletions Nodex/Classes/Debugger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Nodex.Resources.Windows;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Nodex.Classes
{
public static class Debugger
{
public static void AddValue(object obj)
{
DebugWindow debugWindow = ((MainWindow)App.Current.MainWindow).debugWindow;
debugWindow.listboxInfo.Items.Add(obj);
}
}
}
36 changes: 36 additions & 0 deletions Nodex/Classes/ExtraMath.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Nodex.Classes
{
public static class ExtraMath
{
public static int Step(double value, int factor)
{
int nearestMultiple = (int)Math.Round(
value / factor,
MidpointRounding.AwayFromZero
) * factor;
return nearestMultiple;
}

public static int FloorStep(double value, int factor)
{
int nearestMultiple = (int)Math.Floor(
value / factor
) * factor;
return nearestMultiple;
}

public static int CeilingStep(double value, int factor)
{
int nearestMultiple = (int)Math.Ceiling(
value / factor
) * factor;
return nearestMultiple;
}
}
}
25 changes: 19 additions & 6 deletions Nodex/Classes/NetworkSolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,11 @@ private static Dictionary<NodeControl, int> Index(NodeControl outputNode)
return nodeControlIndexes;
}

private static Dictionary<NodeControl, int> IndexFromNodeControl(NodeControl startingNodeControl, int startingIndex, Dictionary<NodeControl, int> nodeControlIndexes = null)
private static Dictionary<NodeControl, int> IndexFromNodeControl(NodeControl startingNodeControl, int startingIndex, Dictionary<NodeControl, int> nodeControlIndexes = null, short continueFromIndex = -1)
{
if (continueFromIndex != -1)
startingNodeControl = startingNodeControl.inputs[continueFromIndex].connectedNodeOutput.parentNodeControl;

if (nodeControlIndexes == null)
nodeControlIndexes = new Dictionary<NodeControl, int>();

Expand All @@ -117,11 +120,19 @@ private static Dictionary<NodeControl, int> IndexFromNodeControl(NodeControl sta
startingNodeControl.index = startingIndex;
}

while (startingNodeControl.inputs.Count > 0)
bool continueLoop = true;
while (continueLoop)
{
switch (startingNodeControl.inputs.Count)
{
case 0: break;
case 0:
continueLoop = false;
if (startingNodeControl.index > 0)
nodeControlIndexes.Remove(startingNodeControl);
if (startingIndex > startingNodeControl.index)
startingNodeControl.index = startingIndex;
nodeControlIndexes.Add(startingNodeControl, startingIndex);
break;
case 1:
//There's only one input, continue from there
if (startingNodeControl.index > 0)
Expand All @@ -131,7 +142,9 @@ private static Dictionary<NodeControl, int> IndexFromNodeControl(NodeControl sta

nodeControlIndexes.Add(startingNodeControl, startingIndex);

startingNodeControl = startingNodeControl.inputs[0].parentNodeControl;
if (startingNodeControl.inputs[0].connectedNodeOutput == null)
return nodeControlIndexes;
startingNodeControl = startingNodeControl.inputs[0].connectedNodeOutput.parentNodeControl;
break;
case int n when n > 1:
//There are multiple inputs, recursively continue from each one
Expand All @@ -153,7 +166,7 @@ private static Dictionary<NodeControl, int> IndexFromNodeControl(NodeControl sta
nodeControlIndexes.Add(startingNodeControl, startingIndex);

int nulls = 0;
for (int i = 0; i < startingNodeControl.inputs.Count; i++)
for (short i = 0; i < startingNodeControl.inputs.Count; i++)
{
if (startingNodeControl.inputs[i].connectedNodeOutput == null)
{
Expand All @@ -163,7 +176,7 @@ private static Dictionary<NodeControl, int> IndexFromNodeControl(NodeControl sta
continue;
}

var temp = IndexFromNodeControl(startingNodeControl.inputs[i].connectedNodeOutput.parentNodeControl, startingIndex + 1, nodeControlIndexes);
var temp = IndexFromNodeControl(startingNodeControl, startingIndex + 1, nodeControlIndexes, i);
if (temp == null)
return null;
nodeControlIndexes = temp;
Expand Down
33 changes: 33 additions & 0 deletions Nodex/Classes/Nodes/Inputs/VectorNode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using Nodex.Resources.Controls;
using System;
using System.Drawing;
using System.Numerics;

namespace Nodex.Classes.Nodes.Inputs
{
public class VectorNode : INode
{
public NodeControl nodeControl { get; set; }

public VectorNode()
{
INode node = this;
nodeControl = new NodeControl(new Node(
Node.NodeCategory.Texture,
"Vector",
new NodeIO[0] ,
new NodeIO[] { new NodeIO(NodeIO.NodeIOCategory.Vector, "Vector", NodeIO.NodeIOType.Output) },
new NodeProperty[] { new NodeProperty(new Vector4Control() { Height = 96 }, "Vector") },
node.Calculate
), this.GetType())
{ Width = 80, Height = 70 };
}

object[] INode.Calculate(NodeIO[] inputs, NodeProperty[] properties)
{
Vector4 vector = ((Vector4Control)properties[0].propertyElement).Vector;
Debugger.AddValue("vector node: " + vector);
return new object[] { vector };
}
}
}
7 changes: 6 additions & 1 deletion Nodex/Classes/Nodes/OutputNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,12 @@ private void UpdateWidthAndHeight(object sender, EventArgs e)

object[] INode.Calculate(NodeIO[] inputs, NodeProperty[] properties)
{
object temp = inputs[0].value;
object temp;

if (inputs == null)
temp = 0;
else
temp = inputs[0].value;

if (temp == null)
temp = 0;
Expand Down
25 changes: 16 additions & 9 deletions Nodex/Classes/Nodes/Textures/OpenSimplexNoiseNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ namespace Nodex.Classes.Nodes.Textures
struct OpenSimplexNoiseCache
{
public OpenSimplexNoise OpenSimplexNoise;
public ParallelTextureCalculator ParallelTextureCalculator;
public long Seed;
public int Scale;
public Dimensions.Dimension Dimension;
Expand Down Expand Up @@ -43,7 +44,7 @@ public OpenSimplexNoiseNode()
nodeControl = new NodeControl(new Node(
Node.NodeCategory.Texture,
"OpenSimplex Noise",
new NodeIO[0],
new NodeIO[] { new NodeIO(NodeIO.NodeIOCategory.Vector, "Vector", NodeIO.NodeIOType.Input, true) },
new NodeIO[] { new NodeIO(NodeIO.NodeIOCategory.Image, "Image", NodeIO.NodeIOType.Output) },
new NodeProperty[] { new NodeProperty(comboBoxDimensions, "Dimensions (WIP)"), new NodeProperty(new IntegerUpDown(0, int.MaxValue) { Height = 24 }, "Seed"), new NodeProperty(new IntegerUpDown(128, 1024, 1, 4) { Height = 24 }, "Scale") },
node.Calculate
Expand All @@ -53,13 +54,15 @@ public OpenSimplexNoiseNode()
width = (int)App.Current.Properties["imageWidth"];
height = (int)App.Current.Properties["imageHeight"];

ParallelTextureCalculator parallelTextureCalculator = new ParallelTextureCalculator(new OpenSimplexNoise(0), width * 8, height * 8, 1024, 1024, 8);
ParallelTextureCalculator parallelTextureCalculator = new ParallelTextureCalculator(new OpenSimplexNoise(0), width, height, 8, new Vector4(0), new Size(128, 128));

noiseCache = new OpenSimplexNoiseCache() { OpenSimplexNoise = new OpenSimplexNoise(0),
ParallelTextureCalculator = parallelTextureCalculator,
Seed = 0,
Scale = 64,
Dimension = Dimensions.Dimension.TwoD,
Vector = new Vector4(0),
HighResCache = parallelTextureCalculator.Calculate()
HighResCache = parallelTextureCalculator.Calculate(new Vector4(0), new Rectangle(new Point(0, 0), new Size(64, 64)))
};
//HighResCache = CalculateNoiseTexture(width * 4, height * 4, new Vector4(0), 0)};
}
Expand Down Expand Up @@ -94,35 +97,39 @@ object[] INode.Calculate(NodeIO[] inputs, NodeProperty[] properties)
int seed = 0;
int scale = 0;
Dimensions.Dimension dimension = Dimensions.Dimension.TwoD;
Vector4 vector = new Vector4(0);

App.Current.Dispatcher.Invoke(() =>
{
seed = ((IntegerUpDown)properties[1].propertyElement).Value;
scale = ((IntegerUpDown)properties[2].propertyElement).Value;
dimension = (Dimensions.Dimension)((ComboBox)properties[0].propertyElement).SelectedIndex;
if (inputs[0].connectedNodeIOs != null && inputs[0].connectedNodeIOs[0].value != null)
vector = (Vector4)inputs[0].connectedNodeIOs[0].value;
Debugger.AddValue("vector: " + vector.ToString());
});

if (!(noiseCache.Seed != seed || noiseCache.Scale != scale || noiseCache.Dimension != dimension) && bitmap != null)
if (!(noiseCache.Seed != seed || noiseCache.Scale != scale || noiseCache.Dimension != dimension || noiseCache.Vector != vector) && bitmap != null)
{
return new object[] { bitmap };
}

width = (int)App.Current.Properties["imageWidth"];
height = (int)App.Current.Properties["imageHeight"];

Rectangle cropRect = new Rectangle((int)vector.X, (int)vector.Y, scale, scale);

if (noiseCache.Seed != seed)
{
ParallelTextureCalculator parallelTextureCalculator = new ParallelTextureCalculator(new OpenSimplexNoise(seed), width * 8, height * 8, 1024, 1024, 8);
noiseCache.HighResCache = parallelTextureCalculator.Calculate();
noiseCache.ParallelTextureCalculator = new ParallelTextureCalculator(new OpenSimplexNoise(seed), width, height, 8, vector, new Size(128, 128));
//noiseCache.Vector != vector
noiseCache.HighResCache = noiseCache.ParallelTextureCalculator.Calculate(vector, cropRect);
//noiseCache.HighResCache = CalculateNoiseTexture(width * 4, height * 4, new Vector4(0), seed);
}

noiseCache.Dimension = dimension;
noiseCache.Seed = seed;
noiseCache.Scale = scale;
double scaleSquared = scale ^ 2;

Rectangle cropRect = new Rectangle(0, 0, scale, scale);
//Bitmap sourceBitmap = noiseCache.HighResCache;
//Bitmap targetBitmap = new Bitmap(cropRect.Width, cropRect.Height);

Expand Down
24 changes: 19 additions & 5 deletions Nodex/Classes/Threading/Chunk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;

Expand All @@ -12,33 +13,46 @@ public class Chunk
public ITexture Texture { get; }
public int SizeX { get; }
public int SizeY { get; }
public Vector2 Position { get; }
public Bitmap Bitmap { get; private set; }
public Rectangle Boundary { get; }

public Chunk(ITexture texture, int size)
public Chunk(ITexture texture, int size, Vector2 position)
{
Texture = texture;
SizeX = size;
SizeY = size;
Position = position;
Boundary = new Rectangle((int)position.X, (int)position.Y, size, size);
}
public Chunk(ITexture texture, int xSize, int ySize)
public Chunk(ITexture texture, int xSize, int ySize, Vector2 position)
{
Texture = texture;
SizeX = xSize;
SizeY = ySize;
Position = position;
Boundary = new Rectangle((int)position.X, (int)position.Y, xSize, ySize);
}

public Bitmap Calculate(float scale, double offsetX, double offsetY)
public void Calculate(float scale, double offsetZ, double offsetW)
{
Bitmap bitmap = new Bitmap(SizeX, SizeY);
for (int x = 0; x < SizeX; x++)
{
for (int y = 0; y < SizeY; y++)
{
int pixelBrightness = (int)((Texture.Evaluate((x + offsetX) / scale, (y + offsetY) / scale) + 1) * 127.5);
int pixelBrightness = (int)((Texture.Evaluate((x + Position.X) / scale, (y + Position.Y) / scale, offsetZ, offsetW) + 1) * 127.5);
bitmap.SetPixel(x, y, Color.FromArgb(pixelBrightness, pixelBrightness, pixelBrightness));
//Console.WriteLine($"{{{x},{y}}}: {pixelBrightness}");
}
}

return bitmap;
Bitmap = bitmap;
}

public void ClearBitmap()
{
Bitmap = null;
}
}
}
Loading

0 comments on commit 98eec14

Please sign in to comment.