-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheightmap.js
68 lines (59 loc) · 1.98 KB
/
heightmap.js
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
import { writeFileSync } from 'fs';
import { SimplexNoise } from './noise.js';
import { generateTerrain } from './terrain.js';
const sharp = require('sharp');
// Define the size of the heightmap
const width = 2000;
const height = 2000;
const perlin = new SimplexNoise();
// fun: f=0.01, a=10, o=1 makes some cool abstract art :)
const frequency = 0.005;
const amplitude = 1;
const octaves = 4;
// // Generate the heightmap
// const heightmap = [];
// for (let y = 0; y < height; y++) {
// const row = [];
// for (let x = 0; x < width; x++) {
// // Calculate the noise value for this point
// // const noiseValue = perlin.noise(x / smoothing, y / smoothing);
// const noiseValue = perlin.simplex_noise(x, y, frequency, amplitude, octaves);
// // Map the noise value to a height value between 0 and 255
// let heightValue = Math.floor((noiseValue + 1) * 127.5);
// // limit noise between 0 and 255
// // console.log("noise", noiseValue, "height", heightValue)
// // Add the height value to the row
// row.push(heightValue);
// }
// // Add the row to the heightmap
// heightmap.push(row);
// }
const heightmap = generateTerrain(new SimplexNoise(), width, height, octaves, 0.501, 0.0008, -300, 300);
/**
* convert the heightmap to a png using the sharp library
*/
export function save_heightmap(heightmap, width, height) {
const buffer = Buffer.alloc(width * height * 4);
for (let y = 0; y < height; y++) {
const row = heightmap[y];
for (let x = 0; x < width; x++) {
const i = (y * width + x) * 4;
let heightValue = row[x];
heightValue = Math.min(Math.max(heightValue, 0), 255);
buffer[i] = heightValue;
buffer[i + 1] = heightValue;
buffer[i + 2] = heightValue;
buffer[i + 3] = 255;
}
}
sharp(buffer, {
raw: {
width: width,
height: height,
channels: 4
}
})
.png()
.toFile('heightmap.png');
}
save_heightmap(heightmap, width, height);