SHOW:
|
|
- or go back to the newest paste.
1 | private static final float AMPLITUDE = 8f; | |
2 | private static final int OCTAVES = 6; | |
3 | private static final float ROUGHNESS = 0.3f; | |
4 | ||
5 | private Random random = new Random(); | |
6 | private int seed; | |
7 | private int xOffset = 0; | |
8 | private int zOffset = 0; | |
9 | ||
10 | public TerrainPerlinNoise(Random rand, float roughness, float width, float height) { | |
11 | random = (rand == null) ? new Random() : rand; | |
12 | this.seed = random.nextInt(1000000000); | |
13 | } | |
14 | ||
15 | public float generateHeight(int x, int z) { | |
16 | float total = 0; | |
17 | float d = (float) Math.pow(2, OCTAVES-1); | |
18 | for(int i=0;i<OCTAVES;i++){ | |
19 | float freq = (float) (Math.pow(2, i) / d); | |
20 | float amp = (float) Math.pow(ROUGHNESS, i) * AMPLITUDE; | |
21 | total += getInterpolatedNoise((x+xOffset)*freq, (z + zOffset)*freq) * amp; | |
22 | } | |
23 | return total; | |
24 | } | |
25 | ||
26 | private float getInterpolatedNoise(float x, float z){ | |
27 | int intX = (int) x; | |
28 | int intZ = (int) z; | |
29 | float fracX = x - intX; | |
30 | float fracZ = z - intZ; | |
31 | ||
32 | float v1 = getSmoothNoise(intX, intZ); | |
33 | float v2 = getSmoothNoise(intX + 1, intZ); | |
34 | float v3 = getSmoothNoise(intX, intZ + 1); | |
35 | float v4 = getSmoothNoise(intX + 1, intZ + 1); | |
36 | float i1 = interpolate(v1, v2, fracX); | |
37 | float i2 = interpolate(v3, v4, fracX); | |
38 | return interpolate(i1, i2, fracZ); | |
39 | } | |
40 | ||
41 | private float interpolate(float a, float b, float blend){ | |
42 | double theta = blend * Math.PI; | |
43 | float f = (float)(1f - Math.cos(theta)) * 0.5f; | |
44 | return a * (1f - f) + b * f; | |
45 | } | |
46 | ||
47 | private float getSmoothNoise(int x, int z) { | |
48 | float corners = (getNoise(x - 1, z - 1) + getNoise(x + 1, z - 1) + getNoise(x - 1, z + 1) | |
49 | + getNoise(x + 1, z + 1)) / 16f; | |
50 | float sides = (getNoise(x - 1, z) + getNoise(x + 1, z) + getNoise(x, z - 1) | |
51 | + getNoise(x, z + 1)) / 8f; | |
52 | float center = getNoise(x, z) / 4f; | |
53 | return corners + sides + center; | |
54 | } | |
55 | ||
56 | private float getNoise(int x, int z) { | |
57 | random.setSeed(x * 49632 + z * 325176 + seed); | |
58 | return random.nextFloat() * 2f - 1f; | |
59 | - | } |
59 | + | |
60 | ||
61 | public void startNoise() { | |
62 | System.out.println("Generating noise map"); | |
63 | System.out.println("Width: " + Terrain.SIZE); | |
64 | System.out.println("Height: " + Terrain.SIZE); | |
65 | BufferedImage image = new BufferedImage((int) Terrain.SIZE, (int) Terrain.SIZE, BufferedImage.TYPE_INT_RGB); | |
66 | ||
67 | for(int x = 0; x < Terrain.SIZE; x++) { | |
68 | for(int y = 0; y < Terrain.SIZE; y++) { | |
69 | float c = generateHeight(x, y); | |
70 | ||
71 | c *= 128.0; | |
72 | c += 127.0; | |
73 | if(c > 255.0) { | |
74 | c = 255.0f; | |
75 | } | |
76 | if(c < 0.0) { | |
77 | c = 0.0f; | |
78 | } | |
79 | ||
80 | // image.setRGB(x, y, Biome.getColour(c).getRGB()); | |
81 | image.setRGB(x, y, new Color(c/255, c/255, c/255).getRGB()); | |
82 | } | |
83 | } | |
84 | ||
85 | File fileImage = new File("res/maps/noise.png"); | |
86 | // File fileImage = new File("res/maps/biomeMap.png"); | |
87 | if(!fileImage.exists()) { | |
88 | try { | |
89 | fileImage.mkdirs(); | |
90 | fileImage.createNewFile(); | |
91 | } | |
92 | catch (IOException e) { | |
93 | e.printStackTrace(); | |
94 | } | |
95 | } | |
96 | try { | |
97 | ImageIO.write(image, "png", fileImage); | |
98 | } | |
99 | catch (IOException e) { | |
100 | e.printStackTrace(); | |
101 | } | |
102 | System.out.println("Noise map generated"); | |
103 | } |