Skip to content

Commit

Permalink
Add OpenSimplex Noise, multi-threading using tiles
Browse files Browse the repository at this point in the history
  • Loading branch information
luca009 committed Jul 24, 2021
1 parent 13fe156 commit 161cbce
Show file tree
Hide file tree
Showing 21 changed files with 1,169 additions and 71 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -363,3 +363,4 @@ MigrationBackup/
FodyWeavers.xsd
/calculating.pdn
/Nodex/MainWindow.xaml.cs.bak
/Nodex/Classes/ProgressDialogUpdater.cs
18 changes: 17 additions & 1 deletion Nodex/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Windows.Threading;

namespace Nodex
{
Expand All @@ -15,6 +17,20 @@ namespace Nodex
/// </summary>
public partial class App : Application
{

void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{
// Process unhandled exception
((MainWindow)App.Current.MainWindow).gridExceptions.Visibility = Visibility.Visible;
((MainWindow)App.Current.MainWindow).textException.Text = e.Exception.Message;
Thread thread = new Thread(() => {
Thread.Sleep(7500);
Dispatcher.Invoke(() => { ((MainWindow)App.Current.MainWindow).gridExceptions.Visibility = Visibility.Hidden; });
});
thread.Start();
thread.IsBackground = true;

// Prevent default unhandled exception processing
e.Handled = true;
}
}
}
16 changes: 16 additions & 0 deletions Nodex/Classes/ITexture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;

namespace Nodex.Classes
{
public interface ITexture
{
double Evaluate(double x, double y);
double Evaluate(double x, double y, double z);
double Evaluate(double x, double y, double z, double w);
}
}
36 changes: 28 additions & 8 deletions Nodex/Classes/NetworkSolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
Expand Down Expand Up @@ -186,17 +187,36 @@ private static Dictionary<NodeControl, int> IndexFromNodeControl(NodeControl sta
/// Loops through all nodes, populating all relevant outputs
/// </summary>
/// <param name="inputDictionary">Dictionary containing all NodeControls and their indexes</param>
private static void CalculateOutput(Dictionary<NodeControl, int> inputDictionary)
private static async void CalculateOutput(Dictionary<NodeControl, int> inputDictionary)
{
var tempList = inputDictionary.ToList();
tempList.Sort((pair1, pair2) => pair1.Value.CompareTo(pair2.Value));
tempList.Reverse();

foreach (KeyValuePair<NodeControl, int> keyValuePair in tempList)
if ((bool)App.Current.Properties["imageCalculatingThreadRunning"])
{
NodeControl nodeControl = keyValuePair.Key;
nodeControl.node.PopulateOutputs();
((Thread)App.Current.Properties["imageCalculatingThread"]).Abort();
App.Current.Properties["imageCalculatingThreadRunning"] = false;
}

var tempList = inputDictionary.ToList();
//Thread thread = new Thread(() => {
tempList.Sort((pair1, pair2) => pair1.Value.CompareTo(pair2.Value));
tempList.Reverse();

foreach (KeyValuePair<NodeControl, int> keyValuePair in tempList)
{
Node node = null;
App.Current.Dispatcher.Invoke(() => { node = keyValuePair.Key.node; });
node.PopulateOutputs();
}
//});
//App.Current.Properties["imageCalculatingThread"] = thread;
App.Current.Properties["imageCalculatingThreadRunning"] = true;
//thread.SetApartmentState(ApartmentState.STA); //Set the thread to STA
//thread.Start();
//await Task.Run(() =>
//{
//thread.Join();
//Thread.Sleep(2);
App.Current.Properties["imageCalculatingThreadRunning"] = false;
//});
}
}
}
45 changes: 28 additions & 17 deletions Nodex/Classes/Node.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,31 +36,42 @@ public Node(NodeCategory category, string label, NodeIO[] inputs, NodeIO[] outpu

public void PopulateOutputs()
{
foreach (NodeIO input in inputs)
NodeIO[] inputsLocal = null;
NodeProperty[] propertiesLocal = null;

App.Current.Dispatcher.Invoke(() =>
{
//Check if all non-optional inputs are connected, if not, output 0
if ((input.connectedNodeIOs == null || input.connectedNodeIOs.Count <= 0) && !input.optional)
foreach (NodeIO input in inputs)
{
foreach (NodeIO output in outputs)
//Check if all non-optional inputs are connected, if not, output 0
if ((input.connectedNodeIOs == null || input.connectedNodeIOs.Count <= 0) && !input.optional)
{
output.value = 0;
foreach (NodeIO output in outputs)
{
output.value = 0;
}
return;
}
return;
}
}

object[] objects = calculateOutputs(inputs, properties);
inputsLocal = inputs;
propertiesLocal = properties;
});

for (int i = 0; i < objects.Length; i++)
{
if (i > outputs.Length - 1)
throw new ArgumentException("Too many objects were returned.");
outputs[i].value = objects[i];
object[] objects = calculateOutputs(inputsLocal, propertiesLocal);

if (outputs[i].connectedNodeIOs != null)
foreach (NodeIO nodeIO in outputs[i].connectedNodeIOs)
nodeIO.value = objects[i];
}
App.Current.Dispatcher.Invoke(() => {
for (int i = 0; i < objects.Length; i++)
{
if (i > outputs.Length - 1)
throw new ArgumentException("Too many objects were returned.");
outputs[i].value = objects[i];

if (outputs[i].connectedNodeIOs != null)
foreach (NodeIO nodeIO in outputs[i].connectedNodeIOs)
nodeIO.value = objects[i];
}
});
}
}
}
3 changes: 2 additions & 1 deletion Nodex/Classes/NodeIO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ public enum NodeIOCategory
{
Undefined = 0,
Image = 1,
Number = 2
Number = 2,
Vector = 3
}

public enum NodeIOType
Expand Down
65 changes: 41 additions & 24 deletions Nodex/Classes/Nodes/OutputNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;
Expand Down Expand Up @@ -51,35 +52,51 @@ object[] INode.Calculate(NodeIO[] inputs, NodeProperty[] properties)

BitmapSource bitmapSource = null;

int width = widthIntegerUpDown.Value;
int height = heightIntegerUpDown.Value;
int width = 0;
int height = 0;

switch (temp)
App.Current.Dispatcher.Invoke(() => {
width = widthIntegerUpDown.Value;
height = heightIntegerUpDown.Value;
});

try
{
switch (temp)
{
case Bitmap bitmap:
//Resize Image if neccessary
if (bitmap.Width != width || bitmap.Height != height)
bitmap = new Bitmap(bitmap, width, height);

App.Current.Dispatcher.Invoke(() => { bitmapSource = bitmap.ConvertToBitmapSource(); });
break;
case int integer:
Bitmap bmp = new Bitmap(width, height);
using (Graphics gfx = Graphics.FromImage(bmp))
using (SolidBrush brush = new SolidBrush(System.Drawing.Color.FromArgb(integer, integer, integer)))
{
gfx.FillRectangle(brush, 0, 0, width, height);
}
bitmapSource = bmp.ConvertToBitmapSource();
break;
case Vector3 vector:
break;
case NodeIO.NodeIOCategory.Undefined:
break;
}
}
catch (Exception ex)
{
case Bitmap bitmap:
//Resize Image if neccessary
if (bitmap.Width != width || bitmap.Height != height)
bitmap = new Bitmap(bitmap, width, height);

bitmapSource = bitmap.ConvertToBitmapSource();
break;
case int integer:
Bitmap bmp = new Bitmap(width, height);
using (Graphics gfx = Graphics.FromImage(bmp))
using (SolidBrush brush = new SolidBrush(System.Drawing.Color.FromArgb(integer, integer, integer)))
{
gfx.FillRectangle(brush, 0, 0, width, height);
}
bitmapSource = bmp.ConvertToBitmapSource();
break;
case NodeIO.NodeIOCategory.Undefined:
break;
}

if (bitmapSource != null)
((MainWindow)App.Current.MainWindow).imagePreview.Source = bitmapSource;
else
((MainWindow)App.Current.MainWindow).imagePreview.Source = null;
App.Current.Dispatcher.Invoke(() => {
if (bitmapSource != null)
((MainWindow)App.Current.MainWindow).imagePreview.Source = bitmapSource;
else
((MainWindow)App.Current.MainWindow).imagePreview.Source = null;
});

return new object[0];
}
Expand Down
Loading

0 comments on commit 161cbce

Please sign in to comment.