-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.js
37 lines (34 loc) · 1.22 KB
/
index.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
var noise = require('perlin').noise
module.exports = function(seed, floor, ceiling, divisor) {
floor = floor || 0
ceiling = ceiling || 20 // minecraft's limit
divisor = divisor || 50
noise.seed(seed)
return function generateChunk(position, width) {
var startX = position[0] * width
var startY = position[1] * width
var startZ = position[2] * width
var chunk = new Int8Array(width * width * width)
pointsInside(startX, startZ, width, function(x, z) {
var n = noise.simplex2(x / divisor , z / divisor)
var y = ~~scale(n, -1, 1, floor, ceiling)
if (y === floor || startY < y && y < startY + width) {
var xidx = Math.abs((width + x % width) % width)
var yidx = Math.abs((width + y % width) % width)
var zidx = Math.abs((width + z % width) % width)
var idx = xidx + yidx * width + zidx * width * width
chunk[idx] = 1
}
})
return chunk
}
}
function pointsInside(startX, startY, width, func) {
for (var x = startX; x < startX + width; x++)
for (var y = startY; y < startY + width; y++)
func(x, y)
}
function scale( x, fromLow, fromHigh, toLow, toHigh ) {
return ( x - fromLow ) * ( toHigh - toLow ) / ( fromHigh - fromLow ) + toLow
}
;