Skip to content

Commit

Permalink
Added Zoxel Importing and Exporting, beginning to prepare examples
Browse files Browse the repository at this point in the history
  • Loading branch information
TriBlade9 committed Nov 26, 2014
1 parent 5fe9292 commit 709122f
Show file tree
Hide file tree
Showing 43 changed files with 5,651 additions and 29 deletions.
24 changes: 21 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Include CEWBS in your HTML file, after BabylonJS and its dependencies.
```html
<script src="CEWBS.min.js"></script>
```
Or, if using node-webkit, require the commonJS version. (The meshers directory must be in the same directory as CWEBS-commonjs.js.)
Or, if using node-webkit, require the commonJS version. (The meshers and helpers directories must be in the same directory as CWEBS-commonjs.js.)
```javascript
var CEWBS = require('CEWBS-commonjs.js');
```
Expand Down Expand Up @@ -92,7 +92,10 @@ window.addEventListener("click", function (evt) {
`under` and `over`, which are both arrays of x,y,and z voxel coordinates in the picked mesh.
`under` is the voxel that was picked, and `over` is the voxel adjacent to the face of the voxel that was picked.

**Exporting Voxels:**

Importing/Exporting Voxels:
---
**CEWBS Import/Export**
To copy voxels in an interchangable format that is not dependant on the dimensions, use `exportVoxelData()`, which
returns an object in the form of

Expand All @@ -105,8 +108,23 @@ returns an object in the form of
],
}
```
To import the format described above into a VoxelMesh, follow the procedure shown:

```javascript
voxMesh1.setDimensions(exportedData.dimensions);
voxMesh1.setVoxelBatch(exportedData.voxels, 1);
```

**Zoxel Import/Export**
*Note, transparency is not supported and is ignored.*
*Note, animations are not supported. Import/export only deals with frame 1.*

To export a Zoxel string, run `exportZoxel()` on the VoxelMesh that you wish to export. This returns a JSON string which can then be
written to a Zoxel (.zox) file.

To import Zoxel files (.zox), create your CEWBS Voxel Mesh, then run `importZoxel(zoxelData)` with Zoxel's JSON string as the argument.
This will set the voxels in your mesh, so don't use it on something you've already created.

This can then be converted to formats used by other programs.

Coloring Voxels:
---
Expand Down
3 changes: 2 additions & 1 deletion compile.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ sed -i -e "s/%VERSION%/$1/g" ../dist/CEWBS-$1-commonjs.js

#Copy meshers folder for CommonJS edition
cp -rf meshers ../dist/meshers
cp -rf helpers ../dist/helpers

##FINALISE##
#Finally copy new version to examples folder.
cp ../dist/CEWBS-$1.js ../examples/lib/CEWBS.js
cp ../dist/CEWBS-$1-debug.js ../example/lib/CEWBS.js

42 changes: 40 additions & 2 deletions dist/CEWBS-0.2-commonjs.js → dist/CEWBS-0.2.1-commonjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ var meshers = {
}

var CEWBS = {};
CEWBS.version = '0.2';
CEWBS.Util = require('./helpers/util.js');

CEWBS.version = '0.2.1';

CEWBS.VoxelMesh = function(name, scene) {
BABYLON.Mesh.call(this, name, scene);
Expand Down Expand Up @@ -179,7 +181,7 @@ format:
],
}
*/
CEWBS.VoxelMesh.prototype.exportVoxelData = function(raw) {
CEWBS.VoxelMesh.prototype.exportVoxelData = function() {
var convertedVoxels = [];
for (var i = 0; i < this.voxelData.voxels.length; i++) {
var voxel = this.voxelData.voxels[i];
Expand All @@ -193,7 +195,43 @@ CEWBS.VoxelMesh.prototype.exportVoxelData = function(raw) {
return {dimensions: this.voxelData.dimensions, voxels: convertedVoxels};
}

//Import a Zoxel file into a CEWBS VoxelMesh
CEWBS.VoxelMesh.prototype.importZoxel = function(zoxelData) {
var cewbsData = {};
cewbsData.dimensions = [zoxelData.width, zoxelData.height, zoxelData.depth];

cewbsData.voxels = zoxelData.frame1;

for(var i = 0; i < cewbsData.voxels.length; i++) {
cewbsData.voxels[i][3] = parseInt(cewbsData.voxels[i][3].toString(16).substring(0,6), 16);
}

this.setDimensions(cewbsData.dimensions)
this.setVoxelBatch(cewbsData.voxels, 0xFFFFFF);
}

//Export the contents of the mesh to Zoxel format.
CEWBS.VoxelMesh.prototype.exportZoxel = function() {
var cewbsData = this.exportVoxelData();
var zoxelData = {};
zoxelData.creator = "CEWBS Exporter";
zoxelData.width = cewbsData.dimensions[0];
zoxelData.height = cewbsData.dimensions[1];
zoxelData.depth = cewbsData.dimensions[2];

zoxelData.version = 1;
zoxelData.frames = 1;

zoxelData.frame1 = cewbsData.voxels;

for(var i = 0; i < zoxelData.frame1.length; i++) {
zoxelData.frame1[i][3] = parseInt(CEWBS.Util.rgb2hex(this.coloringFunction(zoxelData.frame1[i][3]))+'FF', 16);
}

return zoxelData;
}

//Handle Raycasting and picking to get the voxel coordinates
CEWBS.VoxelMesh.handlePick = function(pickResult) {
var mesh = pickResult.pickedMesh;
var point = pickResult.pickedPoint;
Expand Down
71 changes: 67 additions & 4 deletions dist/CEWBS-0.2-debug.js → dist/CEWBS-0.2.1-debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ var meshers = {
}

var CEWBS = window.CEWBS = {};
CEWBS.version = '0.2';
CEWBS.Util = require('./helpers/util.js');

CEWBS.version = '0.2.1';

CEWBS.VoxelMesh = function(name, scene) {
BABYLON.Mesh.call(this, name, scene);
Expand Down Expand Up @@ -180,7 +182,7 @@ format:
],
}
*/
CEWBS.VoxelMesh.prototype.exportVoxelData = function(raw) {
CEWBS.VoxelMesh.prototype.exportVoxelData = function() {
var convertedVoxels = [];
for (var i = 0; i < this.voxelData.voxels.length; i++) {
var voxel = this.voxelData.voxels[i];
Expand All @@ -194,7 +196,43 @@ CEWBS.VoxelMesh.prototype.exportVoxelData = function(raw) {
return {dimensions: this.voxelData.dimensions, voxels: convertedVoxels};
}

//Import a Zoxel file into a CEWBS VoxelMesh
CEWBS.VoxelMesh.prototype.importZoxel = function(zoxelData) {
var cewbsData = {};
cewbsData.dimensions = [zoxelData.width, zoxelData.height, zoxelData.depth];

cewbsData.voxels = zoxelData.frame1;

for(var i = 0; i < cewbsData.voxels.length; i++) {
cewbsData.voxels[i][3] = parseInt(cewbsData.voxels[i][3].toString(16).substring(0,6), 16);
}

this.setDimensions(cewbsData.dimensions)
this.setVoxelBatch(cewbsData.voxels, 0xFFFFFF);
}

//Export the contents of the mesh to Zoxel format.
CEWBS.VoxelMesh.prototype.exportZoxel = function() {
var cewbsData = this.exportVoxelData();
var zoxelData = {};
zoxelData.creator = "CEWBS Exporter";
zoxelData.width = cewbsData.dimensions[0];
zoxelData.height = cewbsData.dimensions[1];
zoxelData.depth = cewbsData.dimensions[2];

zoxelData.version = 1;
zoxelData.frames = 1;

zoxelData.frame1 = cewbsData.voxels;

for(var i = 0; i < zoxelData.frame1.length; i++) {
zoxelData.frame1[i][3] = parseInt(CEWBS.Util.rgb2hex(this.coloringFunction(zoxelData.frame1[i][3]))+'FF', 16);
}

return zoxelData;
}

//Handle Raycasting and picking to get the voxel coordinates
CEWBS.VoxelMesh.handlePick = function(pickResult) {
var mesh = pickResult.pickedMesh;
var point = pickResult.pickedPoint;
Expand Down Expand Up @@ -249,7 +287,32 @@ if(!window.CEWBS) {
module.exports = CEWBS;
}

},{"./meshers/greedy_tri.js":2,"./meshers/monotone.js":3}],2:[function(require,module,exports){
},{"./helpers/util.js":2,"./meshers/greedy_tri.js":3,"./meshers/monotone.js":4}],2:[function(require,module,exports){
var util = {};

util.toHex = function(n) {
n = parseInt(n,10);
if (isNaN(n)) {return "00"};

n = Math.max(0,Math.min(n,255));
return "0123456789ABCDEF".charAt((n-n%16)/16)
+ "0123456789ABCDEF".charAt(n%16);
}

util.rgb2hex = function(rgb) {
return util.toHex(rgb[0])+util.toHex(rgb[1])+util.toHex(rgb[2]);
}

util.hex2rgb = function(hexStr) {
R = parseInt((hexStr).substring(0,2),16);
G = parseInt((hexStr).substring(2,4),16);
B = parseInt((hexStr).substring(4,6),16);
return [R,G,B];
}

module.exports = util;

},{}],3:[function(require,module,exports){
var GreedyMesh = (function() {
//Cache buffer internally
var mask = new Int32Array(4096);
Expand Down Expand Up @@ -350,7 +413,7 @@ if(exports) {
exports.mesher = GreedyMesh;
}

},{}],3:[function(require,module,exports){
},{}],4:[function(require,module,exports){
"use strict";

var MonotoneMesh = (function(){
Expand Down
1 change: 1 addition & 0 deletions dist/CEWBS-0.2.1.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 709122f

Please sign in to comment.