Skip to content

Commit 6d2fa79

Browse files
committed
Added SimplexNoise
Signed-off-by: Ralph Niemitz <ralph.niemitz@gmx.de>
1 parent e4827bd commit 6d2fa79

File tree

4 files changed

+577
-0
lines changed

4 files changed

+577
-0
lines changed
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
package src.main.java.com.generation;
2+
3+
import java.util.Random;
4+
5+
/**
6+
* Implementation of the simplex noise algorithm.
7+
* @see <a href="https://en.wikipedia.org/wiki/Simplex_noise">Wikipedia</a>
8+
*/
9+
public class SimplexNoise {
10+
11+
private SimplexNoiseOctave[] octaves;
12+
private double[] frequencys;
13+
private double[] amplitudes;
14+
private int largestFeature;
15+
private double persistance;
16+
private long seed;
17+
18+
/**
19+
* @param largestFeature size of the largest possible feature
20+
* @param persistence the persistence
21+
* @param seed the seed
22+
*/
23+
public SimplexNoise(int largestFeature, double persistence, long seed) {
24+
25+
this.largestFeature = largestFeature;
26+
this.persistance = persistence;
27+
this.seed = seed;
28+
29+
int octaveCount = (int)Math.ceil(Math.log10(largestFeature) / Math.log10(2.0D));
30+
this.octaves = new SimplexNoiseOctave[octaveCount];
31+
this.frequencys = new double[octaveCount];
32+
this.amplitudes = new double[octaveCount];
33+
34+
Random random = new Random(seed);
35+
36+
for(int index = 0; index < octaveCount; index++) {
37+
38+
this.octaves[index] = new SimplexNoiseOctave(random.nextInt());
39+
this.frequencys[index] = Math.pow(2, index);
40+
this.amplitudes[index] = Math.pow(persistence, octaveCount - index);
41+
}
42+
}
43+
44+
/**
45+
* Generates a height map.
46+
* @param x X coordinate
47+
* @param y Y coordinate
48+
* @param width width
49+
* @param height height
50+
* @return the generated height map
51+
*/
52+
public float[][] generateHeightMap(int x, int y, int width, int height) {
53+
54+
int xEnd = x + width;
55+
int yEnd = y + height;
56+
57+
float[][] result = new float[width][height];
58+
59+
for(int i = 0; i < width; i++) {
60+
61+
for(int j = 0; j < height; j++) {
62+
63+
int posX = x + i * ((xEnd - x) / width);
64+
int posY = y + j * ((yEnd - y) / height);
65+
result[i][j] = Math.min(1.0F, Math.max(0.0F, (float)(0.5D * (1 + this.getNoise(posX, posY)))));
66+
}
67+
}
68+
69+
return result;
70+
}
71+
72+
/**
73+
* Generates a two dimensional noise.
74+
* @param x X coordinate
75+
* @param y Y coordinate
76+
* @return the generated noise
77+
*/
78+
public double getNoise(int x, int y) {
79+
80+
double result = 0;
81+
82+
for(int index = 0; index < this.octaves.length; index++) {
83+
84+
result += this.octaves[index].noise(x / this.frequencys[index], y / this.frequencys[index]) * this.amplitudes[index];
85+
}
86+
87+
return result;
88+
}
89+
90+
/**
91+
* Generates a three dimensional noise.
92+
* @param x X coordinate
93+
* @param y Y coordinate
94+
* @param z Z coordinate
95+
* @return the generated noise
96+
*/
97+
public double getNoise(int x, int y, int z) {
98+
99+
double result = 0;
100+
101+
for(int index = 0; index < this.octaves.length; index++) {
102+
103+
double frequency = Math.pow(2, index);
104+
double amplitude = Math.pow(this.persistance, this.octaves.length - index);
105+
106+
result += this.octaves[index].noise(x / frequency, y / frequency, z / frequency) * amplitude;
107+
}
108+
109+
return result;
110+
}
111+
112+
/**
113+
* @return the largest possible feature
114+
*/
115+
public int getLargestFeature() {
116+
117+
return this.largestFeature;
118+
}
119+
120+
/**
121+
* @return the persistence
122+
*/
123+
public double getPersistance() {
124+
125+
return this.persistance;
126+
}
127+
128+
/**
129+
* @return the seed
130+
*/
131+
public long getSeed() {
132+
133+
return this.seed;
134+
}
135+
}

0 commit comments

Comments
 (0)