-
Notifications
You must be signed in to change notification settings - Fork 0
/
PerlinNoise.cs
88 lines (74 loc) · 2.67 KB
/
PerlinNoise.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DarkBoxEngine
{
class PerlinNoise
{
private int width;
private int height;
private int octave;
private double[,] Noise;
private double persistance = 0.5;
private double amplitude = 1.0;
private double totalAmplitude = 0.0;
public PerlinNoise(int width, int height, int octave)
{
this.width = width;
this.height = height;
this.Noise = new double[width, height];
this.octave = octave;
randomNoise();
}
private double[,] randomNoise()
{
Random random = new Random();
for (int i = 0; i < Noise.GetLength(0); i++)
{
for (int j = 0; j < Noise.GetLength(1); j++)
{
var noiseVal=random.NextDouble();
Noise[i, j] = noiseVal;
}
}
return Noise;
}
private double[][,] smoothNoise()
{
double[][,] smoothNoise = new double[octave][,];
for (int i = 0; i < octave; i++)
{
smoothNoise[i] = GenerateSmoothNoise(Noise) ;
}
return smoothNoise;
}
private double[,] GenerateSmoothNoise(double[,] inputNoise)
{
var samplePeriod = 1 << octave;
var sampleFrequency = 1.0 / samplePeriod;
double[,] smoothNoise= new double[width, height]; ;
for(int i = 0; i < width; i++)
{
var sample_i0 = (i / samplePeriod) * samplePeriod;
var sample_i1 = (sample_i0 + samplePeriod) % width;
var horizontal_blend = (i - sample_i0) * sampleFrequency;
for (int j = 0; j < height; j++)
{
var sample_j0 = (j / samplePeriod) * samplePeriod;
var sample_j1 = (sample_j0 + samplePeriod) % height;
var vertical_blend = (j - sample_j0) * sampleFrequency;
var top = Lerp(inputNoise[sample_i0, sample_j0], inputNoise[sample_i1,sample_j0], horizontal_blend);
var bottom = Lerp(inputNoise[sample_i0, sample_j1], inputNoise[sample_i1, sample_j1], horizontal_blend);
smoothNoise[i, i] = Lerp(top, bottom, vertical_blend);
}
}
return smoothNoise;
}
double Lerp(double firstFloat, double secondFloat, double by)
{
return firstFloat * by + secondFloat * (1 - by);
}
}
}