Skip to content

Ndarray #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Feb 11, 2015
38 changes: 11 additions & 27 deletions chunker.js
Original file line number Diff line number Diff line change
@@ -10,6 +10,7 @@ module.exports.Chunker = Chunker
function Chunker(opts) {
this.distance = opts.chunkDistance || 2
this.chunkSize = opts.chunkSize || 32
this.chunkPad = opts.chunkPad !== undefined ? opts.chunkPad : 0
this.cubeSize = opts.cubeSize || 25
this.generateVoxelChunk = opts.generateVoxelChunk
this.chunks = {}
@@ -20,6 +21,8 @@ function Chunker(opts) {
var bits = 0;
for (var size = this.chunkSize; size > 0; size >>= 1) bits++;
this.chunkBits = bits - 1;
this.chunkMask = (1 << this.chunkBits) - 1
this.chunkPadHalf = this.chunkPad >> 1
}

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

Chunker.prototype.voxelIndexFromCoordinates = function(x, y, z) {
var bits = this.chunkBits
var mask = (1 << bits) - 1
var vidx = (x & mask) + ((y & mask) << bits) + ((z & mask) << bits * 2)
return vidx
}

Chunker.prototype.voxelIndexFromPosition = function(pos) {
var v = this.voxelVector(pos)
return this.voxelIndex(v)
throw new Error('Chunker.prototype.voxelIndexFromCoordinates removed, use voxelAtCoordinates')
}

Chunker.prototype.voxelAtCoordinates = function(x, y, z, val) {
var ckey = this.chunkAtCoordinates(x, y, z).join('|')
var chunk = this.chunks[ckey]
if (!chunk) return false
var vidx = this.voxelIndexFromCoordinates(x, y, z)
var v = chunk.voxels[vidx]
var mask = this.chunkMask
var h = this.chunkPadHalf
var mx = x & mask
var my = y & mask
var mz = z & mask
var v = chunk.get(mx+h, my+h, mz+h)
if (typeof val !== 'undefined') {
chunk.voxels[vidx] = val
chunk.set(mx+h, my+h, mz+h, val)
}
return v
}
@@ -118,18 +117,3 @@ Chunker.prototype.voxelAtPosition = function(pos, val) {
return v;
}

// deprecated
Chunker.prototype.voxelIndex = function(voxelVector) {
var vidx = this.voxelIndexFromCoordinates(voxelVector[0], voxelVector[1], voxelVector[2])
return vidx
}

// deprecated
Chunker.prototype.voxelVector = function(pos) {
var cubeSize = this.cubeSize
var mask = (1 << this.chunkBits) - 1
var vx = (Math.floor(pos[0] / cubeSize)) & mask
var vy = (Math.floor(pos[1] / cubeSize)) & mask
var vz = (Math.floor(pos[2] / cubeSize)) & mask
return [vx, vy, vz]
};
30 changes: 18 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var chunker = require('./chunker')
var ndarray = require('ndarray')

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

// from https://github.com/mikolalysenko/mikolalysenko.github.com/blob/master/MinecraftMeshes2/js/testdata.js#L4
function generate(l, h, f, game) {
var d = [ h[0]-l[0], h[1]-l[1], h[2]-l[2] ]
var v = new Int8Array(d[0]*d[1]*d[2])
var n = 0
for(var k=l[2]; k<h[2]; ++k)
for(var j=l[1]; j<h[1]; ++j)
for(var i=l[0]; i<h[0]; ++i, ++n) {
v[n] = f(i,j,k,n,game)
}
return {voxels:v, dims:d}
function generate(lo, hi, fn, game) {
// To fix the display gaps, we need to pad the bounds
lo[0]--
lo[1]--
lo[2]--
hi[0]++
hi[1]++
hi[2]++
var dims = [hi[2]-lo[2], hi[1]-lo[1], hi[0]-lo[0]]
var data = ndarray(new Uint16Array(dims[2] * dims[1] * dims[0]), dims)
for (var k = lo[2]; k < hi[2]; k++)
for (var j = lo[1]; j < hi[1]; j++)
for(var i = lo[0]; i < hi[0]; i++) {
data.set(k-lo[2], j-lo[1], i-lo[0], fn(i, j, k))
}
return data
}

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

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

module.exports.generator['Hilly Terrain'] = function(i,j,k) {
6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -4,7 +4,8 @@
"version": "0.4.0",
"main": "index.js",
"dependencies": {
"inherits": "1.0.0"
"inherits": "1.0.0",
"ndarray": "~1.0.5"
},
"repository": {
"type": "git",
@@ -24,8 +25,5 @@
"devDependencies": {
"microtime": "~0.3.3",
"tape": "0.2.2"
},
"peerDependencies": {
"voxel-engine": ">=0.9.0"
}
}
29 changes: 0 additions & 29 deletions test.js
Original file line number Diff line number Diff line change
@@ -32,35 +32,6 @@ test('chunkAtPosition', function (t) {
t.end()
})

test('voxelIndexFromCoordinates', function (t) {
var chunker = voxel({chunkDistance: 2, chunkSize: 32, cubeSize: 1})
t.equal(chunker.voxelIndexFromCoordinates(0, 0, 0), 0)
t.equal(chunker.voxelIndexFromCoordinates(1, 0, 0), 1)
t.equal(chunker.voxelIndexFromCoordinates(31, 0, 0), 31)
t.equal(chunker.voxelIndexFromCoordinates(32, 0, 0), 0)
t.equal(chunker.voxelIndexFromCoordinates(0, 1, 0), 32)
t.equal(chunker.voxelIndexFromCoordinates(0, 31, 0), 32*31)
t.equal(chunker.voxelIndexFromCoordinates(0, 32, 0), 0)
t.equal(chunker.voxelIndexFromCoordinates(0, 0, 1), 32*32)
t.equal(chunker.voxelIndexFromCoordinates(0, 0, 31), 32*32*31)
t.equal(chunker.voxelIndexFromCoordinates(0, 0, 32), 0)
t.equal(chunker.voxelIndexFromCoordinates(-1, 0, 0), 31)
t.equal(chunker.voxelIndexFromCoordinates(-31, 0, 0), 1)
t.equal(chunker.voxelIndexFromCoordinates(-32, 0, 0), 0)
t.end()
})

test('voxelIndexFromPosition', function (t) {
var chunker = voxel({chunkDistance: 2, chunkSize: 32, cubeSize: 1})
t.equal(chunker.voxelIndexFromPosition([0, 0, 0]), 0)
t.equal(chunker.voxelIndexFromPosition([0.9999, 0, 0]), 0)
t.equal(chunker.voxelIndexFromPosition([1.9999, 0, 0]), 1)
t.equal(chunker.voxelIndexFromPosition([-0.0001, 0, 0]), 31)
t.equal(chunker.voxelIndexFromPosition([-0.9999, 0, 0]), 31)
t.equal(chunker.voxelIndexFromPosition([-1, 0, 0]), 31)
t.end()
})

test('getBounds', function (t) {
var chunker = voxel({chunkDistance: 2, chunkSize: 32, cubeSize: 1})
t.deepEqual(chunker.getBounds(0, 0, 0), [[0, 0, 0], [32, 32, 32]])