Comparing version 0.0.1 to 0.0.2
@@ -14,16 +14,21 @@ var example1 = | ||
{ | ||
name: 'maybe tree', | ||
model: example1 | ||
}, | ||
{ | ||
name: 'double rainbow', | ||
model: example2 | ||
}, | ||
{ | ||
name: 'alien fighter', | ||
model: example3 | ||
}, | ||
{ | ||
name: 'unfinished pipe', | ||
model: example4 | ||
}, | ||
{ | ||
name: 'escheresque pipes', | ||
model: example5 | ||
} | ||
]; |
@@ -8,3 +8,4 @@ var cubeSize = 32; | ||
y: cubeSize, | ||
z: cubeSize | ||
z: cubeSize, | ||
slow: 10 | ||
}); | ||
@@ -74,7 +75,7 @@ | ||
for (var y = 0; y < 32; y++) { | ||
for (var x = 0; x < 32; x++) { | ||
for (var x = 32 - 1; x >= 0; x--) { | ||
color = { | ||
r: (z + 1) * 8, | ||
g: (y + 1) * 8, | ||
b: (x + 1) * 8 | ||
r: z * 8, | ||
g: y * 8, | ||
b: 255 - x * 8 | ||
}; | ||
@@ -116,11 +117,11 @@ | ||
// Tinytest.add('occlusion', function (test) { | ||
// test.equal(count, cubeSize * cubeSize + cubeSize * (cubeSize - 1) + (cubeSize - 1) * (cubeSize - 1)); | ||
// }); | ||
Tinytest.add('model: ' + this.data.name, function (test) { | ||
test.equal(count < model.length, true); | ||
}); | ||
var end = +new Date; | ||
console.log(count, 'cubes rendered in', end - start, 'ms'); | ||
console.log(count, 'cubes rendered in', end - start, 'ms, with', model.length - count, 'cubes not rendered'); | ||
return ''; | ||
}); |
181
cubes.js
@@ -23,29 +23,25 @@ Cubes = function (canvasNode, config) { | ||
this.rotationIndex = 0; | ||
this.slow = config.slow || 0; | ||
// Four trees, indexed from different directions | ||
this.sceneData = [ | ||
{ | ||
count: 0 | ||
}, // 0 | ||
{ | ||
count: 0 | ||
}, // 90 | ||
{ | ||
count: 0 | ||
}, // 180 | ||
{ | ||
count: 0 | ||
} // 270 | ||
]; | ||
this.sceneData = []; | ||
this.sceneDataLength = 0; | ||
this.faceIndices = {}; | ||
this.faceDistances = {}; | ||
this.renderData = []; | ||
this.iso = new this.Isomer(canvasNode, { | ||
scale: (config.scale || 10.0), | ||
originY: (config.originY || this.gridSizeZ * 2 * 10) | ||
originY: (config.originY || this.gridSizeZ * 2 * 10), | ||
lightPosition: new this.Isomer.Vector( | ||
config.lightX || 3, | ||
config.lightY || -5, | ||
config.lightZ || 1 | ||
) | ||
}); | ||
this.iso.colorDifference = config.colorDifference || 0.10; | ||
this.planeXY = config.planeXY || true; | ||
this._adds = 0; | ||
this._newTree(); | ||
}; | ||
@@ -86,3 +82,3 @@ | ||
this.iso.scene = []; | ||
// this.iso.scene = []; | ||
@@ -97,4 +93,4 @@ if (this.planeXY) { | ||
), | ||
new this.Color(200, 200, 200), | ||
true | ||
new this.Color(200, 200, 200) | ||
// , true | ||
); | ||
@@ -105,35 +101,19 @@ } | ||
// Breadth-first search | ||
var ts = +new Date; | ||
var queue = [this._index(gridX - 1, gridY - 1, 0)]; | ||
// Pull only front-facing cubes from faceIndices | ||
for (var i = 0, ii = this.renderData.length; i < ii; i++) { | ||
var rd = this.renderData[i]; | ||
renderQueue.push(this.sceneData[this.faceIndices[rd.index]]); | ||
} | ||
var arr = null; | ||
var index = null; | ||
while (queue.length) { | ||
index = queue.shift(); | ||
arr = this.sceneData[0][index]; | ||
// Sort cubes so they render in the right order | ||
renderQueue.sort(this._cubeSorter); | ||
if (arr && arr[0].ts !== ts) { | ||
var len = 0; | ||
if (arr[0]) len++; | ||
if (arr[1] >= 0) len++; | ||
if (arr[2] >= 0) len++; | ||
if (arr[3] >= 0) len++; | ||
// renderQueue.push(arr[0]); | ||
if (len === 3 || len === 2 || len === 1) renderQueue.push(arr[0]); | ||
this.sceneData[0][index][0].ts = ts; | ||
if (arr[1]) queue.push(arr[1]); | ||
if (arr[2]) queue.push(arr[2]); | ||
if (arr[3]) queue.push(arr[3]); | ||
} | ||
// Render cubes in queue, non-blocking | ||
if (this.slow) { | ||
this.slowRender(renderQueue, this.slow); | ||
} | ||
else { | ||
this.render(renderQueue); | ||
} | ||
// Render cubes in queue | ||
setTimeout(this.render, 0, this, renderQueue); | ||
// For next-generation Isomer. | ||
@@ -146,13 +126,41 @@ // this.iso.canvas.clear(); | ||
Cubes.prototype.render = function (that, rq) { | ||
Cubes.prototype._cubeSorter = function (a, b) { | ||
if (a.x > b.x) return -1; | ||
if (a.x < b.x) return 1; | ||
if (a.y > b.y) return -1; | ||
if (a.y < b.y) return 1; | ||
if (a.z < b.z) return -1; | ||
if (a.z > b.z) return 1; | ||
return 0; | ||
} | ||
Cubes.prototype.render = function (rq) { | ||
setTimeout(function (that, rq) { | ||
var cube = null; | ||
for (var j = 0, jj = rq.length; j < jj; j++) { | ||
cube = rq[j]; | ||
that.iso.add( | ||
that.Shape.Prism( | ||
new that.Point(cube.x, cube.y, cube.z) | ||
), | ||
cube.color ? that.isoColor(cube.color) : null | ||
// , true | ||
); | ||
} | ||
}, 0, this, rq); | ||
} | ||
Cubes.prototype.slowRender = function (rq, speed) { | ||
var cube = null; | ||
for (var j = 0, jj = rq.length; j < jj; j++) { | ||
cube = rq[j]; | ||
that.iso.add( | ||
that.Shape.Prism( | ||
new that.Point(cube.x, cube.y, cube.z) | ||
), | ||
cube.color ? that.isoColor(cube.color) : null | ||
// , true | ||
); | ||
setTimeout(function (that, rq, j) { | ||
cube = rq[j]; | ||
that.iso.add( | ||
that.Shape.Prism( | ||
new that.Point(cube.x, cube.y, cube.z) | ||
), | ||
cube.color ? that.isoColor(cube.color) : null | ||
// , true | ||
); | ||
}, j * speed, this, rq, j); | ||
} | ||
@@ -162,9 +170,33 @@ } | ||
Cubes.prototype.insert = function (cube) { | ||
var dist = Math.min(cube.x, cube.y, this.gridSizeZ - cube.z); | ||
var fx = cube.x - dist; | ||
var fy = cube.y - dist; | ||
var fz = this.gridSizeZ - cube.z - dist; | ||
var faceIndex = this._index(fx, fy, fz); | ||
if (!this.faceIndices[faceIndex]) { | ||
this.faceDistances[faceIndex] = Infinity; | ||
this.renderData.push({ | ||
x: fx, | ||
y: fy, | ||
z: fz, | ||
index: faceIndex | ||
}); | ||
} | ||
var index = this._index(cube.x, cube.y, cube.z); | ||
this.sceneData[0][index] = this.sceneData[0][index] || [{ts: 0}]; | ||
this.sceneData[0][index][0] = cube; | ||
this.sceneData[0].count++; | ||
if (dist <= this.faceDistances[faceIndex]) { | ||
this.faceIndices[faceIndex] = index; | ||
this.faceDistances[faceIndex] = dist; | ||
} | ||
cube.index = index; | ||
this.sceneData[index] = cube; | ||
this.sceneDataLength++; | ||
} | ||
// Private Methods | ||
Cubes.prototype._index = function (x, y, z) { | ||
@@ -174,27 +206,4 @@ return this.gridSizeZ * this.gridSizeZ * z + this.gridSizeY * y + x + 1; | ||
Cubes.prototype._addNode = function (parent, i, x, y, z) { | ||
this._adds++; | ||
var index = this._index(x, y, z); | ||
this.sceneData[0][index] = this.sceneData[0][index] || [{ts: 0}]; | ||
this.sceneData[0][index][i] = parent; | ||
} | ||
Cubes.prototype._newTree = function () { | ||
// Add additional nodes to scene tree | ||
var index = null; | ||
for (var z = 0, zz = this.gridSizeZ; z < zz; z++) { | ||
for (var y = this.gridSizeY - 1, yy = 0; y >= yy; y--) { | ||
for (var x = this.gridSizeX - 1, xx = 0; x >= xx; x--) { | ||
index = this._index(x, y, z); | ||
if (x + 1 < this.gridSizeX) this._addNode(index, 1, x + 1, y, z); | ||
if (y + 1 < this.gridSizeY) this._addNode(index, 2, x, y + 1, z); | ||
if (z - 1 >= 0) this._addNode(index, 3, x, y, z - 1); | ||
} | ||
} | ||
} | ||
} | ||
if (Cubes.commonJS) { | ||
module.exports = Cubes; | ||
} |
Package.describe({ | ||
name: 'cryptoquick:cubes', | ||
version: '0.0.1', | ||
version: '0.0.2', | ||
summary: 'An isometric graphics-rendering library.', | ||
@@ -13,3 +13,4 @@ git: 'https://github.com/cryptoquick/cubes', | ||
api.addFiles('cubes.js'); | ||
api.export('Cubes', 'client'); | ||
api.export('Cubes'); | ||
api.imply('cryptoquick:isomer@0.2.5'); | ||
}); | ||
@@ -16,0 +17,0 @@ |
{ | ||
"name": "cubes", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "An isometric graphics management library.", | ||
@@ -5,0 +5,0 @@ "main": "cubes.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
43151
9
435