Skip to content

Commit dcee702

Browse files
committed
Merge pull request #18 from deathcap/ndarray
Ndarray
2 parents 7cfa103 + 2a92d91 commit dcee702

File tree

4 files changed

+31
-72
lines changed

4 files changed

+31
-72
lines changed

chunker.js

+11-27
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ module.exports.Chunker = Chunker
1010
function Chunker(opts) {
1111
this.distance = opts.chunkDistance || 2
1212
this.chunkSize = opts.chunkSize || 32
13+
this.chunkPad = opts.chunkPad !== undefined ? opts.chunkPad : 0
1314
this.cubeSize = opts.cubeSize || 25
1415
this.generateVoxelChunk = opts.generateVoxelChunk
1516
this.chunks = {}
@@ -20,6 +21,8 @@ function Chunker(opts) {
2021
var bits = 0;
2122
for (var size = this.chunkSize; size > 0; size >>= 1) bits++;
2223
this.chunkBits = bits - 1;
24+
this.chunkMask = (1 << this.chunkBits) - 1
25+
this.chunkPadHalf = this.chunkPad >> 1
2326
}
2427

2528
inherits(Chunker, events.EventEmitter)
@@ -86,25 +89,21 @@ Chunker.prototype.chunkAtPosition = function(position) {
8689
};
8790

8891
Chunker.prototype.voxelIndexFromCoordinates = function(x, y, z) {
89-
var bits = this.chunkBits
90-
var mask = (1 << bits) - 1
91-
var vidx = (x & mask) + ((y & mask) << bits) + ((z & mask) << bits * 2)
92-
return vidx
93-
}
94-
95-
Chunker.prototype.voxelIndexFromPosition = function(pos) {
96-
var v = this.voxelVector(pos)
97-
return this.voxelIndex(v)
92+
throw new Error('Chunker.prototype.voxelIndexFromCoordinates removed, use voxelAtCoordinates')
9893
}
9994

10095
Chunker.prototype.voxelAtCoordinates = function(x, y, z, val) {
10196
var ckey = this.chunkAtCoordinates(x, y, z).join('|')
10297
var chunk = this.chunks[ckey]
10398
if (!chunk) return false
104-
var vidx = this.voxelIndexFromCoordinates(x, y, z)
105-
var v = chunk.voxels[vidx]
99+
var mask = this.chunkMask
100+
var h = this.chunkPadHalf
101+
var mx = x & mask
102+
var my = y & mask
103+
var mz = z & mask
104+
var v = chunk.get(mx+h, my+h, mz+h)
106105
if (typeof val !== 'undefined') {
107-
chunk.voxels[vidx] = val
106+
chunk.set(mx+h, my+h, mz+h, val)
108107
}
109108
return v
110109
}
@@ -118,18 +117,3 @@ Chunker.prototype.voxelAtPosition = function(pos, val) {
118117
return v;
119118
}
120119

121-
// deprecated
122-
Chunker.prototype.voxelIndex = function(voxelVector) {
123-
var vidx = this.voxelIndexFromCoordinates(voxelVector[0], voxelVector[1], voxelVector[2])
124-
return vidx
125-
}
126-
127-
// deprecated
128-
Chunker.prototype.voxelVector = function(pos) {
129-
var cubeSize = this.cubeSize
130-
var mask = (1 << this.chunkBits) - 1
131-
var vx = (Math.floor(pos[0] / cubeSize)) & mask
132-
var vy = (Math.floor(pos[1] / cubeSize)) & mask
133-
var vz = (Math.floor(pos[2] / cubeSize)) & mask
134-
return [vx, vy, vz]
135-
};

index.js

+18-12
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var chunker = require('./chunker')
2+
var ndarray = require('ndarray')
23

34
module.exports = function(opts) {
45
if (!opts.generateVoxelChunk) opts.generateVoxelChunk = function(low, high) {
@@ -20,17 +21,22 @@ module.exports.geometry = {}
2021
module.exports.generator = {}
2122
module.exports.generate = generate
2223

23-
// from https://github.com/mikolalysenko/mikolalysenko.github.com/blob/master/MinecraftMeshes2/js/testdata.js#L4
24-
function generate(l, h, f, game) {
25-
var d = [ h[0]-l[0], h[1]-l[1], h[2]-l[2] ]
26-
var v = new Int8Array(d[0]*d[1]*d[2])
27-
var n = 0
28-
for(var k=l[2]; k<h[2]; ++k)
29-
for(var j=l[1]; j<h[1]; ++j)
30-
for(var i=l[0]; i<h[0]; ++i, ++n) {
31-
v[n] = f(i,j,k,n,game)
32-
}
33-
return {voxels:v, dims:d}
24+
function generate(lo, hi, fn, game) {
25+
// To fix the display gaps, we need to pad the bounds
26+
lo[0]--
27+
lo[1]--
28+
lo[2]--
29+
hi[0]++
30+
hi[1]++
31+
hi[2]++
32+
var dims = [hi[2]-lo[2], hi[1]-lo[1], hi[0]-lo[0]]
33+
var data = ndarray(new Uint16Array(dims[2] * dims[1] * dims[0]), dims)
34+
for (var k = lo[2]; k < hi[2]; k++)
35+
for (var j = lo[1]; j < hi[1]; j++)
36+
for(var i = lo[0]; i < hi[0]; i++) {
37+
data.set(k-lo[2], j-lo[1], i-lo[0], fn(i, j, k))
38+
}
39+
return data
3440
}
3541

3642
// shape and terrain generator functions
@@ -55,7 +61,7 @@ module.exports.generator['Hill'] = function(i,j,k) {
5561
}
5662

5763
module.exports.generator['Valley'] = function(i,j,k) {
58-
return j <= (i*i + k*k) * 31 / (32*32*2) + 1 ? 1 : 0;
64+
return j <= (i*i + k*k) * 31 / (32*32*2) + 1 ? 1 + (1<<15) : 0;
5965
}
6066

6167
module.exports.generator['Hilly Terrain'] = function(i,j,k) {

package.json

+2-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"version": "0.4.0",
55
"main": "index.js",
66
"dependencies": {
7-
"inherits": "1.0.0"
7+
"inherits": "1.0.0",
8+
"ndarray": "~1.0.5"
89
},
910
"repository": {
1011
"type": "git",
@@ -24,8 +25,5 @@
2425
"devDependencies": {
2526
"microtime": "~0.3.3",
2627
"tape": "0.2.2"
27-
},
28-
"peerDependencies": {
29-
"voxel-engine": ">=0.9.0"
3028
}
3129
}

test.js

-29
Original file line numberDiff line numberDiff line change
@@ -32,35 +32,6 @@ test('chunkAtPosition', function (t) {
3232
t.end()
3333
})
3434

35-
test('voxelIndexFromCoordinates', function (t) {
36-
var chunker = voxel({chunkDistance: 2, chunkSize: 32, cubeSize: 1})
37-
t.equal(chunker.voxelIndexFromCoordinates(0, 0, 0), 0)
38-
t.equal(chunker.voxelIndexFromCoordinates(1, 0, 0), 1)
39-
t.equal(chunker.voxelIndexFromCoordinates(31, 0, 0), 31)
40-
t.equal(chunker.voxelIndexFromCoordinates(32, 0, 0), 0)
41-
t.equal(chunker.voxelIndexFromCoordinates(0, 1, 0), 32)
42-
t.equal(chunker.voxelIndexFromCoordinates(0, 31, 0), 32*31)
43-
t.equal(chunker.voxelIndexFromCoordinates(0, 32, 0), 0)
44-
t.equal(chunker.voxelIndexFromCoordinates(0, 0, 1), 32*32)
45-
t.equal(chunker.voxelIndexFromCoordinates(0, 0, 31), 32*32*31)
46-
t.equal(chunker.voxelIndexFromCoordinates(0, 0, 32), 0)
47-
t.equal(chunker.voxelIndexFromCoordinates(-1, 0, 0), 31)
48-
t.equal(chunker.voxelIndexFromCoordinates(-31, 0, 0), 1)
49-
t.equal(chunker.voxelIndexFromCoordinates(-32, 0, 0), 0)
50-
t.end()
51-
})
52-
53-
test('voxelIndexFromPosition', function (t) {
54-
var chunker = voxel({chunkDistance: 2, chunkSize: 32, cubeSize: 1})
55-
t.equal(chunker.voxelIndexFromPosition([0, 0, 0]), 0)
56-
t.equal(chunker.voxelIndexFromPosition([0.9999, 0, 0]), 0)
57-
t.equal(chunker.voxelIndexFromPosition([1.9999, 0, 0]), 1)
58-
t.equal(chunker.voxelIndexFromPosition([-0.0001, 0, 0]), 31)
59-
t.equal(chunker.voxelIndexFromPosition([-0.9999, 0, 0]), 31)
60-
t.equal(chunker.voxelIndexFromPosition([-1, 0, 0]), 31)
61-
t.end()
62-
})
63-
6435
test('getBounds', function (t) {
6536
var chunker = voxel({chunkDistance: 2, chunkSize: 32, cubeSize: 1})
6637
t.deepEqual(chunker.getBounds(0, 0, 0), [[0, 0, 0], [32, 32, 32]])

0 commit comments

Comments
 (0)