fix line preview

This commit is contained in:
casperlamboo 2018-02-12 11:07:06 +01:00
parent 45b0f541b1
commit dbc01167e5
4 changed files with 74 additions and 59 deletions

View File

@ -0,0 +1,24 @@
export function hslToRgb(h, s, l){
let r, g, b;
if (s === 0) {
r = g = b = lightness;
} else {
const q = l < 0.5 ? l * (1 + s) : l + s - l * s;
const p = 2 * l - q;
r = hueToRgb(p, q, h + 1 / 3);
g = hueToRgb(p, q, h);
b = hueToRgb(p, q, h - 1 / 3);
}
return [r, g, b];
}
function hueToRgb(p, q, t){
if (t < 0) t += 1;
if (t > 1) t -= 1;
if (t < 1 / 6) return p + (q - p) * 6 * t;
if (t < 1 / 2) return q;
if (t < 2 / 3) return p + (q - p) * (2 / 3 - t) * 6;
return p;
}

View File

@ -1,4 +1,3 @@
// import * as THREE from 'three';
import calculateLayersIntersections from './calculateLayersIntersections.js';
import createLines from './createLines.js';
import generateInfills from './generateInfills.js';
@ -12,6 +11,7 @@ import shapesToSlices from './shapesToSlices.js';
import slicesToGCode from './slicesToGCode.js';
import applyPrecision from './applyPrecision.js';
import { PRECISION } from '../constants.js';
import { hslToRgb } from './helpers/color.js';
// // import removePrecision from './removePrecision.js';
export default function(settings, geometry, openObjectIndexes, constructLinePreview, onProgress) {
@ -64,7 +64,7 @@ export default function(settings, geometry, openObjectIndexes, constructLinePrev
updateProgress('Finished');
// if (constructLinePreview) gcode.linePreview = createGcodeGeometry(gcode.gcode);
if (constructLinePreview) gcode.linePreview = createGcodeGeometry(gcode.gcode);
gcode.gcode = gcodeToString(gcode.gcode);
return gcode;
@ -99,39 +99,32 @@ function gcodeToString(gcode) {
}, '');
}
// const MAX_SPEED = 100 * 60;
// const COLOR = new THREE.Color();
// function createGcodeGeometry(gcode) {
// const positions = [];
// const colors = [];
//
// let lastPoint = [0, 0, 0];
// for (let i = 0; i < gcode.length; i ++) {
// const command = gcode[i];
// if (typeof command === 'string') continue;
//
// const { G, F, X, Y, Z } = command;
//
// if (X || Y || Z) {
// if (G === 1) {
// positions.push(lastPoint.Y, lastPoint.Z, lastPoint.X);
// positions.push(Y, Z, X);
//
// const color = (G === 0) ? COLOR.setHex(0x00ff00) : COLOR.setHSL(F / MAX_SPEED, 0.5, 0.5);
// colors.push(color.r, color.g, color.b);
// colors.push(color.r, color.g, color.b);
// }
// lastPoint = { X, Y, Z };
// }
// }
//
// const geometry = new THREE.BufferGeometry();
//
// geometry.addAttribute('position', new THREE.BufferAttribute(new Float32Array(positions), 3));
// geometry.addAttribute('color', new THREE.BufferAttribute(new Float32Array(colors), 3));
//
// const material = new THREE.LineBasicMaterial({ vertexColors: THREE.VertexColors });
// const linePreview = new THREE.LineSegments(geometry, material);
//
// return linePreview;
// }
const MAX_SPEED = 100 * 60;
function createGcodeGeometry(gcode) {
const positions = [];
const colors = [];
let lastPoint = [0, 0, 0];
for (let i = 0; i < gcode.length; i ++) {
const command = gcode[i];
if (typeof command === 'string') continue;
const { G, F, X, Y, Z } = command;
if (X || Y || Z) {
if (G === 1) {
positions.push(lastPoint.Y, lastPoint.Z, lastPoint.X);
positions.push(Y, Z, X);
const color = (G === 0) ? [0, 1, 0] : hslToRgb(F / MAX_SPEED, 0.5, 0.5);
colors.push(...color, ...color);
}
lastPoint = { X, Y, Z };
}
}
return {
positions: new Float32Array(positions),
colors: new Float32Array(colors)
};
}

View File

@ -76,7 +76,9 @@ export function sliceGeometry(settings, geometry, materials, matrix, sync = fals
}
function sliceSync(settings, geometry, openObjectIndexes, constructLinePreview, onProgress) {
return slice(settings, geometry, openObjectIndexes, constructLinePreview, onProgress);
const gcode = slice(settings, geometry, openObjectIndexes, constructLinePreview, onProgress);
if (gcode.linePreview) gcode.linePreview = constructLineGeometry(gcode.linePreview);
return gcode;
}
function sliceAsync(settings, geometry, openObjectIndexes, constructLinePreview, onProgress) {
@ -98,19 +100,7 @@ function sliceAsync(settings, geometry, openObjectIndexes, constructLinePreview,
const { gcode } = data;
gcode.gcode = typedArrayToString(gcode.gcode);
// if (data.gcode.linePreview) {
// const geometry = new THREE.BufferGeometry();
//
// const { position, color } = data.gcode.linePreview;
// geometry.addAttribute('position', new THREE.BufferAttribute(new Float32Array(position), 3));
// geometry.addAttribute('color', new THREE.BufferAttribute(new Float32Array(color), 3));
//
// const material = new THREE.LineBasicMaterial({ vertexColors: THREE.VertexColors });
// const linePreview = new THREE.LineSegments(geometry, material);
//
// data.gcode.linePreview = linePreview;
// }
if (gcode.linePreview) gcode.linePreview = constructLineGeometry(gcode.linePreview);
resolve(gcode);
break;
@ -133,3 +123,14 @@ function sliceAsync(settings, geometry, openObjectIndexes, constructLinePreview,
}, buffers);
});
}
function constructLineGeometry(linePreview) {
const geometry = new THREE.BufferGeometry();
geometry.addAttribute('position', new THREE.BufferAttribute(new Float32Array(linePreview.positions), 3));
geometry.addAttribute('color', new THREE.BufferAttribute(new Float32Array(linePreview.colors), 3));
const material = new THREE.LineBasicMaterial({ vertexColors: THREE.VertexColors });
const mesh = new THREE.LineSegments(geometry, material);
return mesh;
}

View File

@ -19,13 +19,10 @@ self.addEventListener('message', (event) => {
gcode.gcode = stringToTypedArray(gcode.gcode);
const buffers = [gcode.gcode.buffer];
// if (gcode.linePreview) {
// const position = gcode.linePreview.geometry.getAttribute('position').array;
// const color = gcode.linePreview.geometry.getAttribute('color').array;
// buffers.push(position.buffer, color.buffer);
// gcode.linePreview = { position, color };
// }
if (gcode.linePreview) {
buffers.push(gcode.linePreview.positions.buffer);
buffers.push(gcode.linePreview.colors.buffer);
}
self.postMessage({
message: 'SLICE',