send typed array instead of string across worker

This commit is contained in:
casperlamboo 2018-02-12 10:28:42 +01:00
parent 4352293e95
commit 45b0f541b1
3 changed files with 24 additions and 2 deletions

View File

@ -0,0 +1,15 @@
export function stringToTypedArray(string) {
const array = new Uint8Array(string.length);
for (let i = 0; i < string.length; i ++) {
array[i] = string.charCodeAt(i);
}
return array;
}
export function typedArrayToString(array) {
let string = '';
for (let i = 0; i < array.length; i ++) {
string += String.fromCharCode(array[i]);
}
return string;
}

View File

@ -1,6 +1,7 @@
import * as THREE from 'three'; import * as THREE from 'three';
import slice from './sliceActions/slice.js'; import slice from './sliceActions/slice.js';
import SlicerWorker from './slicer.worker.js'; import SlicerWorker from './slicer.worker.js';
import { typedArrayToString } from './sliceActions/helpers/binary.js';
export function sliceMesh(settings, mesh, sync = false, constructLinePreview = false, onProgress) { export function sliceMesh(settings, mesh, sync = false, constructLinePreview = false, onProgress) {
if (!mesh || !mesh.isMesh) { if (!mesh || !mesh.isMesh) {
@ -95,6 +96,9 @@ function sliceAsync(settings, geometry, openObjectIndexes, constructLinePreview,
case 'SLICE': { case 'SLICE': {
slicerWorker.terminate(); slicerWorker.terminate();
const { gcode } = data;
gcode.gcode = typedArrayToString(gcode.gcode);
// if (data.gcode.linePreview) { // if (data.gcode.linePreview) {
// const geometry = new THREE.BufferGeometry(); // const geometry = new THREE.BufferGeometry();
// //
@ -108,7 +112,7 @@ function sliceAsync(settings, geometry, openObjectIndexes, constructLinePreview,
// data.gcode.linePreview = linePreview; // data.gcode.linePreview = linePreview;
// } // }
resolve(data.gcode); resolve(gcode);
break; break;
} }
case 'PROGRESS': { case 'PROGRESS': {

View File

@ -1,5 +1,6 @@
import 'core-js'; // polyfills import 'core-js'; // polyfills
import slice from './sliceActions/slice.js'; import slice from './sliceActions/slice.js';
import { stringToTypedArray } from './sliceActions/helpers/binary.js';
const onProgress = progress => { const onProgress = progress => {
self.postMessage({ self.postMessage({
@ -15,8 +16,10 @@ self.addEventListener('message', (event) => {
const { settings, geometry, constructLinePreview, openObjectIndexes } = data; const { settings, geometry, constructLinePreview, openObjectIndexes } = data;
const gcode = slice(settings, geometry, openObjectIndexes, constructLinePreview, onProgress); const gcode = slice(settings, geometry, openObjectIndexes, constructLinePreview, onProgress);
gcode.gcode = stringToTypedArray(gcode.gcode);
const buffers = [gcode.gcode.buffer];
const buffers = [];
// if (gcode.linePreview) { // if (gcode.linePreview) {
// const position = gcode.linePreview.geometry.getAttribute('position').array; // const position = gcode.linePreview.geometry.getAttribute('position').array;
// const color = gcode.linePreview.geometry.getAttribute('color').array; // const color = gcode.linePreview.geometry.getAttribute('color').array;