Skip to content

Commit

Permalink
Everything done except cleaning up and commenting code for worely noise
Browse files Browse the repository at this point in the history
  • Loading branch information
richardklafter committed Oct 12, 2011
1 parent 05c1735 commit ed3ff0b
Show file tree
Hide file tree
Showing 9 changed files with 567 additions and 23 deletions.
2 changes: 2 additions & 0 deletions CellNoiseDemo/CellNoiseDemo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
<Compile Include="App.xaml.cs">
<DependentUpon>App.xaml</DependentUpon>
</Compile>
<Compile Include="FillBitmapBackgroundWorker.cs" />
<Compile Include="CellNoiseDemoDataObject.cs" />
<Compile Include="MainPage.xaml.cs">
<DependentUpon>MainPage.xaml</DependentUpon>
Expand All @@ -81,6 +82,7 @@
<Compile Include="Vector3.cs" />
<Compile Include="Vector4.cs" />
<Compile Include="WorleyNoise.cs" />
<Compile Include="WriteableBitmapExtensions.cs" />
</ItemGroup>
<ItemGroup>
<ApplicationDefinition Include="App.xaml">
Expand Down
70 changes: 68 additions & 2 deletions CellNoiseDemo/CellNoiseDemoDataObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,90 @@
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Windows.Media.Imaging;
using System.Threading;

namespace CellNoiseDemo
{
public class CellNoiseDemoDataObject : INotifyPropertyChanged
{
private FillBitmapBackgroundWorker _worker = new FillBitmapBackgroundWorker();
public CellNoiseDemoDataObject()
{
_worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(_worker_RunWorkerCompleted);
PropertyChanged += new PropertyChangedEventHandler(CellNoiseDemoDataObject_PropertyChanged);
//CellNoiseDemoDataObject_PropertyChanged(this, null);
}

void _worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (!e.Cancelled && e.Error == null)
{
Bitmap.Invalidate();
PropertyChanged(this, new PropertyChangedEventArgs("Bitmap"));
}
}

private Vector4 bitmapFillFunc(Vector4 pos)
{
PointColor pc = new PointColor(pos, new Vector4());
float[] dists = new float[3];

Func<Vector3, Vector3, float> distanceFunction = null;
switch (DistanceMetric)
{
case DistanceMetrics.Euclidean:
distanceFunction = WorleyNoise.EuclidianDistanceFunc;
break;
case DistanceMetrics.Manhattan:
distanceFunction = WorleyNoise.ManhattanDistanceFunc;
break;
case DistanceMetrics.Chebyshev:
distanceFunction = WorleyNoise.ChebyshevDistanceFunc;
break;
}

Func<float[], float> combinerFunc = null;
switch (CombinationFunction)
{
case CombinationFunctions.D1:
combinerFunc = i => i[0];
break;
case CombinationFunctions.D2MinusD1:
combinerFunc = i => i[1] - i[0];
break;
case CombinationFunctions.D3MinusD1:
combinerFunc = i => i[2] - i[0];
break;
}

pc = WorleyNoise.WorleyFunc(pc, Seed, distanceFunction, ref dists, combinerFunc);
return pc.Color;
}

void CellNoiseDemoDataObject_PropertyChanged(object sender, PropertyChangedEventArgs e)
{

FillBitmap(ref _bitmap, true);
}

public void FillBitmap(ref WriteableBitmap bitmap, bool doAsync)
{
_worker.FillBitmapAsync((float)Zoom, bitmapFillFunc, _bitmap);
while (!doAsync && _worker.IsBusy)
{
System.Threading.Thread.Sleep(10);
}
}

private WriteableBitmap _bitmap = new WriteableBitmap(200, 200);
public WriteableBitmap Bitmap
{
get { return _bitmap; }
}

public event PropertyChangedEventHandler PropertyChanged;

private double _zoom = 0;
private double _zoom = 5;
public double Zoom
{
get { return _zoom; }
Expand Down
81 changes: 81 additions & 0 deletions CellNoiseDemo/FillBitmapBackgroundWorker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;
using System.Threading;
using System.ComponentModel;

namespace CellNoiseDemo
{
public class ImprovedBackgroundWorker : BackgroundWorker
{
public bool Cancel()
{
if (!this.IsBusy) return true;
if (!this.WorkerSupportsCancellation) return false;
this.CancelAsync();
while (this.IsBusy)
{
System.Threading.Thread.Sleep(0);
}
return !this.IsBusy;
}
}

public class FillBitmapBackgroundWorker : ImprovedBackgroundWorker
{
public FillBitmapBackgroundWorker()
{
this.WorkerReportsProgress = false;
this.WorkerSupportsCancellation = true;
}

protected override void OnDoWork(DoWorkEventArgs e)
{
float width = _writeableBitmap.PixelWidth;
float height = _writeableBitmap.PixelHeight;
int index = 0;
for (int y = 0; y < _writeableBitmap.PixelHeight; y++)
{
if (this.CancellationPending)
{
e.Cancel = true;
return;
}
for (int x = 0; x < _writeableBitmap.PixelWidth; x++)
{
Vector4 color = _fillFunction(new Vector4((float)x / width, (float)y / height, 0f, 1f) * _zoom);
_writeableBitmap.SetPixeli(index++, (byte)(color.W * 255), (byte)(color.X * 255), (byte)(color.Y * 255), (byte)(color.Z * 255));
}
}
}

public void FillBitmapAsync(float zoom, Func<Vector4, Vector4> fillFunction, WriteableBitmap writeableBitmap)
{
if (this.Cancel())
{
_zoom = zoom;
_fillFunction = fillFunction;
_writeableBitmap = writeableBitmap;
this.RunWorkerAsync();
}
}

//public void Cancel

protected override void OnRunWorkerCompleted(RunWorkerCompletedEventArgs e)
{
base.OnRunWorkerCompleted(e);
}
private float _zoom;
private Func<Vector4, Vector4> _fillFunction;
private WriteableBitmap _writeableBitmap;
}
}
9 changes: 4 additions & 5 deletions CellNoiseDemo/MainPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<TextBlock Text="Cell Noise" Style="{StaticResource TextBlockStyle}" FontSize="20" FontWeight="Bold" TextAlignment="Center"/>
<TextBlock Text="Demo" Style="{StaticResource TextBlockStyle}" FontSize="20" FontWeight="Bold" TextAlignment="Center"/>
<TextBlock Text="Zoom:" Style="{StaticResource TextBlockStyle}"/>
<Slider Height="25" Value="{Binding Zoom, Mode=TwoWay}" Minimum="-10" Maximum="10" />
<Slider Height="25" Value="{Binding Zoom, Mode=TwoWay}" Minimum=".5" Maximum="20" />
<TextBlock Text="Seed:" Style="{StaticResource TextBlockStyle}"/>
<StackPanel Orientation="Horizontal" Height="25">
<TextBox Name="txtSeed" Width="120" Text="{Binding Seed, Mode=TwoWay}" />
Expand All @@ -32,13 +32,12 @@
<TextBlock Text="Combination Function:" Style="{StaticResource TextBlockStyle}"/>
<ComboBox SelectionChanged="ComboBox_SelectionChanged_1">
<ComboBox.Items>
<ComboBoxItem IsSelected="True" Content="D1" Tag="D1"/>
<ComboBoxItem Content="D2 - D1" Tag="D2MinusD1"/>
<ComboBoxItem Content="D1" Tag="D1"/>
<ComboBoxItem IsSelected="True" Content="D2 - D1" Tag="D2MinusD1"/>
<ComboBoxItem Content="D3 - D1" Tag="D3MinusD1"/>
</ComboBox.Items>
</ComboBox>
<Button Content="Export Image" Margin="0,5,0,0" Style="{StaticResource ButtonStyle}" Click="Button_Click" />
</StackPanel>
<Image Grid.Column="1" Name="img" Stretch="UniformToFill" />
<Image Grid.Column="1" Name="img" Stretch="UniformToFill" Source="{Binding Bitmap}"/>
</Grid>
</UserControl>
6 changes: 1 addition & 5 deletions CellNoiseDemo/MainPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;

namespace CellNoiseDemo
{
Expand All @@ -35,10 +36,5 @@ private void ComboBox_SelectionChanged_1(object sender, SelectionChangedEventArg
{
dataObject.CombinationFunctionString = ((sender as ComboBox).SelectedItem as ComboBoxItem).Tag as string;
}

private void Button_Click(object sender, RoutedEventArgs e)
{

}
}
}
2 changes: 1 addition & 1 deletion CellNoiseDemo/Vector3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ public void Scale(ref Vector3 scale)
/// <summary>
/// Defines the size of the Vector3 struct in bytes.
/// </summary>
public static readonly int SizeInBytes = Marshal.SizeOf(new Vector3());
//public static readonly int SizeInBytes = Marshal.SizeOf(new Vector3());

#endregion

Expand Down
2 changes: 1 addition & 1 deletion CellNoiseDemo/Vector4.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public struct Vector4 : IEquatable<Vector4>
/// <summary>
/// Defines the size of the Vector4 struct in bytes.
/// </summary>
public static readonly int SizeInBytes = Marshal.SizeOf(new Vector4());
//public static readonly int SizeInBytes = Marshal.SizeOf(new Vector4());

#endregion

Expand Down
24 changes: 15 additions & 9 deletions CellNoiseDemo/WorleyNoise.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@

namespace CellNoiseDemo
{
public static class WorleyNoise
public struct PointColor
{
public struct PointColor
public PointColor(Vector4 point, Vector4 color)
{
public PointColor(Vector4 point, Vector4 color)
{
Point = point;
Color = color;
}
public Vector4 Point;
public Vector4 Color;
Point = point;
Color = color;
}
public Vector4 Point;
public Vector4 Color;
}

public static class WorleyNoise
{
public static float EuclidianDistanceFunc(Vector3 p1, Vector3 p2)
{
return (p1.X - p2.X) * (p1.X - p2.X) + (p1.Y - p2.Y) * (p1.Y - p2.Y) + (p1.Z - p2.Z) * (p1.Z - p2.Z);
Expand Down Expand Up @@ -110,6 +110,12 @@ private static uint probLookup(uint value)
return 9;
}

/// <summary>
/// Inserts value into array using insertion sort. If the value is greater than the largest value in the array
/// it will not be added to the array.
/// </summary>
/// <param name="arr">The array to insert the value into.</param>
/// <param name="value">The value to insert into the array.</param>
private static void insert(float[] arr, float value)
{
float temp;
Expand Down
Loading

0 comments on commit ed3ff0b

Please sign in to comment.