Doodle3D-Slicer/src/slicerworker.js

96 lines
1.9 KiB
JavaScript
Raw Normal View History

2015-07-26 15:32:10 +02:00
import THREE from 'three.js';
import Settings from './settings.js';
export default class {
constructor () {
this.worker = new Worker('./worker.js');
var scope = this;
this.worker.addEventListener('message', function (event) {
switch (event.data['cmd']) {
case 'PROGRESS':
if (scope.onprogress !== undefined) {
var progress = event.data['progress'];
scope.onprogress(progress);
}
break;
case 'GCODE':
if (scope.onfinish !== undefined) {
var reader = new FileReader();
reader.addEventListener("loadend", function() {
var gcode = reader.result;
scope.onfinish(gcode);
});
reader.readAsBinaryString(event.data['gcode']);
}
break;
}
}, false);
this.worker.onerror = function (error) {
console.warn(error);
};
}
setMesh (mesh) {
mesh.updateMatrix();
2015-06-12 15:58:26 +02:00
2015-07-26 15:32:10 +02:00
this.setGeometry(mesh.geometry, mesh.matrix);
2015-07-26 15:32:10 +02:00
return this;
}
2015-06-15 10:21:05 +02:00
2015-07-26 15:32:10 +02:00
setGeometry (geometry, matrix) {
if (geometry.type === 'Geometry') {
geometry = new THREE.BufferGeometry().fromGeometry(geometry);
}
else if (geometry.type === 'BufferGeometry') {
geometry = geometry.clone();
}
else {
console.warn('Geometry is not an instance of BufferGeometry or Geometry');
return;
}
2015-07-26 15:32:10 +02:00
if (!(matrix instanceof THREE.Matrix4)) {
matrix = new THREE.Matrix4();
}
2015-07-10 12:59:50 +02:00
2015-07-26 15:32:10 +02:00
var buffers = [];
for (var i = 0; i < geometry.attributesKeys.length; i ++) {
var key = geometry.attributesKeys[i];
buffers.push(geometry.attributes[key].array.buffer);
}
2015-07-10 12:59:50 +02:00
2015-07-26 15:32:10 +02:00
this.worker.postMessage({
'cmd': 'SET_MESH',
'geometry': {
'attributes': geometry.attributes,
'attributesKeys': geometry.attributesKeys
},
'matrix': matrix.toArray()
}, buffers);
2015-07-10 12:59:50 +02:00
2015-07-26 15:32:10 +02:00
return this;
2015-05-29 10:41:44 +02:00
}
2015-07-26 15:32:10 +02:00
slice (settings) {
this.worker.postMessage({
'cmd': 'SLICE',
'settings': settings.config
});
2015-05-29 10:41:44 +02:00
2015-07-26 15:32:10 +02:00
return this;
}
2015-07-26 15:32:10 +02:00
close () {
this.worker.postMessage({
'cmd': 'CLOSE'
});
return this;
}
}