-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathNoise2D.java
150 lines (124 loc) · 4.23 KB
/
Noise2D.java
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package com.remote.remote2d.engine.logic;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.util.Random;
public class Noise2D{
public static float[][] GeneratePerlinNoise(float[][] baseNoise, int octaveCount)
{
int width = baseNoise.length;
int height = baseNoise[0].length;
float[][][] smoothNoise = new float[octaveCount][][]; //an array of 2D arrays containing
float persistance = 0.5f;
//generate smooth noise
for (int i = 0; i < octaveCount; i++)
{
smoothNoise[i] = GenerateSmoothNoise(baseNoise, i);
}
float[][] perlinNoise = GetEmptyArray(width, height);
float amplitude = 1.0f;
float totalAmplitude = 0.0f;
//blend noise together
for (int octave = octaveCount - 1; octave >= 0; octave--)
{
amplitude *= persistance;
totalAmplitude += amplitude;
for (int i = 0; i < width; i++)
{
for (int j = 0; j < height; j++)
{
perlinNoise[i][j] += smoothNoise[octave][i][j] * amplitude;
}
}
}
//normalisation
for (int i = 0; i < width; i++)
{
for (int j = 0; j < height; j++)
{
perlinNoise[i][j] /= totalAmplitude;
}
}
return perlinNoise;
}
public static float Interpolate(float x0, float x1, float alpha)
{
return x0 * (1 - alpha) + alpha * x1;
}
public static float[][] GenerateSmoothNoise(float[][] baseNoise, int octave)
{
int width = baseNoise.length;
int height = baseNoise[0].length;
float[][] smoothNoise = GetEmptyArray(width, height);
int samplePeriod = 1 << octave; // calculates 2 ^ k
float sampleFrequency = 1.0f / samplePeriod;
for (int i = 0; i < width; i++)
{
//calculate the horizontal sampling indices
int sample_i0 = (i / samplePeriod) * samplePeriod;
int sample_i1 = (sample_i0 + samplePeriod) % width; //wrap around
float horizontal_blend = (i - sample_i0) * sampleFrequency;
for (int j = 0; j < height; j++)
{
//calculate the vertical sampling indices
int sample_j0 = (j / samplePeriod) * samplePeriod;
int sample_j1 = (sample_j0 + samplePeriod) % height; //wrap around
float vertical_blend = (j - sample_j0) * sampleFrequency;
//blend the top two corners
float top = Interpolate(baseNoise[sample_i0][sample_j0],
baseNoise[sample_i1][sample_j0], horizontal_blend);
//blend the bottom two corners
float bottom = Interpolate(baseNoise[sample_i0][sample_j1],
baseNoise[sample_i1][sample_j1], horizontal_blend);
//final blend
smoothNoise[i][j] = Interpolate(top, bottom, vertical_blend);
}
}
return smoothNoise;
}
public static float[][] GenerateWhiteNoise(int width, int height, int seed)
{
Random random = new Random(seed);
float[][] noise = GetEmptyArray(width, height);
for (int i = 0; i < width; i++)
{
for (int j = 0; j < height; j++)
{
int x = width+i;
int y = height+j;
random.setSeed(random.nextLong() ^ x);
random.setSeed(random.nextLong() ^ y);
//System.out.println("X:"+x+" Y:"+y+" Res:"+seedWithOffset);
//random.setSeed(seedWithOffset);
noise[i][j] = (float)random.nextDouble() % 1;
}
}
return noise;
}
public static float[][] GetEmptyArray(int x, int y)
{
return new float[x][y];
}
public static BufferedImage perlinImage(int width, int height,int octave)
{
BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_ARGB);
Graphics2D graphics = image.createGraphics();
float[][] perlin = GeneratePerlinNoise(GenerateWhiteNoise(width,height,(int) System.currentTimeMillis()),octave);
for(int x=0;x<width;x++)
{
for(int y=0;y<height;y++)
{
int grayscale = (int)(255f*perlin[x][y]);
if(grayscale<0)
grayscale = 0;
if(grayscale>255)
grayscale = 255;
//Log.debug("Grayscale ("+x+","+y+"):"+grayscale);
graphics.setColor(new Color(grayscale,grayscale,grayscale));
graphics.drawRect(x, y, 1, 1);
}
}
graphics.dispose();
return image;
}
}