Skip to content

Commit 0907e26

Browse files
authored
Add fill noise for array (#28)
1 parent 4ab9061 commit 0907e26

File tree

6 files changed

+49
-6
lines changed

6 files changed

+49
-6
lines changed

src/MineCase.Algorithm/MineCase.Algorithm.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
<ItemGroup>
1010
<PackageReference Include="StyleCop.Analyzers" Version="1.1.0-beta004" PrivateAssets="All" />
11+
<PackageReference Include="System.Numerics.Vectors" Version="4.4.0" />
1112
<PackageReference Include="System.ValueTuple" Version="4.4.0" />
1213
</ItemGroup>
1314

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Numerics;
34
using System.Text;
45

56
namespace MineCase.Algorithm.Noise
67
{
78
public interface INoise
89
{
910
double Noise(double x, double y, double z);
11+
12+
void Noise(double[,,] noise, Vector3 offset, Vector3 scale);
1013
}
1114
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Numerics;
4+
using System.Text;
5+
6+
namespace MineCase.Algorithm.Noise
7+
{
8+
public abstract class NoiseBase : INoise
9+
{
10+
public abstract double Noise(double x, double y, double z);
11+
12+
public virtual void Noise(double[,,] noise, Vector3 offset, Vector3 scale)
13+
{
14+
var xExtent = noise.GetUpperBound(0) + 1;
15+
var yExtent = noise.GetUpperBound(1) + 1;
16+
var zExtent = noise.GetUpperBound(2) + 1;
17+
18+
for (int z = 0; z < zExtent; z++)
19+
{
20+
var zOffset = offset.Z + z * scale.Z;
21+
for (int y = 0; y < yExtent; y++)
22+
{
23+
var yOffset = offset.Y + y * scale.Y;
24+
for (int x = 0; x < xExtent; x++)
25+
{
26+
var xOffset = offset.X + x * scale.X;
27+
noise[x, y, z] = Noise(xOffset, yOffset, zOffset);
28+
}
29+
}
30+
}
31+
}
32+
}
33+
}

src/MineCase.Algorithm/Noise/OctavedNoise.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace MineCase.Algorithm.Noise
66
{
7-
public class OctavedNoise<TNoise> : INoise
7+
public class OctavedNoise<TNoise> : NoiseBase, INoise
88
where TNoise : INoise
99
{
1010
private readonly TNoise _innerNoise;
@@ -18,7 +18,7 @@ public OctavedNoise(TNoise innerNoise, int octaves, double persistence)
1818
_persistence = persistence;
1919
}
2020

21-
public double Noise(double x, double y, double z)
21+
public override double Noise(double x, double y, double z)
2222
{
2323
double total = 0;
2424
double frequency = 1;

src/MineCase.Algorithm/Noise/PerlinNoise.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Numerics;
34
using System.Text;
45

56
namespace MineCase.Algorithm.Noise
67
{
78
/// <summary>
89
/// Implementation for Improved Perlin Noise (http://mrl.nyu.edu/~perlin/noise/)
910
/// </summary>
10-
public class PerlinNoise : INoise
11+
public class PerlinNoise : NoiseBase, INoise
1112
{
1213
/// <summary>
1314
/// Permutation
@@ -25,7 +26,7 @@ public PerlinNoise(int seed)
2526
_p[i + 256] = _p[i] = random.Next(0, 256);
2627
}
2728

28-
public double Noise(double x, double y, double z)
29+
public override double Noise(double x, double y, double z)
2930
{
3031
var xcoord = Split(x);
3132
var ycoord = Split(y);

tests/UnitTest/NoiseTest.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.IO;
4+
using System.Numerics;
45
using System.Reflection;
56
using System.Runtime.CompilerServices;
67
using System.Text;
@@ -32,11 +33,13 @@ public void TestPerlinNoise3D()
3233
using (var image = new Image<ImageSharp.PixelFormats.Rgb24>(xExtent, yExtent))
3334
{
3435
var noise = new PerlinNoise(100);
36+
var noiseValue = new double[xExtent, yExtent, 1];
37+
noise.Noise(noiseValue, Vector3.Zero, new Vector3(0.1f, 0.1f, 0));
3538
for (int x = 0; x < xExtent; x++)
3639
{
3740
for (int y = 0; y < yExtent; y++)
3841
{
39-
var color = (byte)(noise.Noise(x / 10.0, 0, y / 10.0) * 255);
42+
var color = (byte)(noiseValue[x, y, 0] * 255);
4043
image[x, y] = new ImageSharp.PixelFormats.Rgb24(color, color, color);
4144
}
4245
}
@@ -55,11 +58,13 @@ public void TestOctavedPerlinNoise3D()
5558
using (var image = new Image<ImageSharp.PixelFormats.Rgb24>(xExtent, yExtent))
5659
{
5760
var noise = new OctavedNoise<PerlinNoise>(new PerlinNoise(100), 8, 1);
61+
var noiseValue = new double[xExtent, yExtent, 1];
62+
noise.Noise(noiseValue, Vector3.Zero, new Vector3(0.1f, 0.1f, 0));
5863
for (int x = 0; x < xExtent; x++)
5964
{
6065
for (int y = 0; y < yExtent; y++)
6166
{
62-
var color = (byte)(noise.Noise(x / 10.0, 0, y / 10.0) * 255);
67+
var color = (byte)(noiseValue[x, y, 0] * 255);
6368
image[x, y] = new ImageSharp.PixelFormats.Rgb24(color, color, color);
6469
}
6570
}

0 commit comments

Comments
 (0)