This repository has been archived by the owner on Jun 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
gcode.js
68 lines (55 loc) · 1.86 KB
/
gcode.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/**
* @fileoverview GCODE Parser
* Based on https://github.com/mrdoob/three.js/blob/dev/examples/js/loaders/GCodeLoader.js
* @author tentone
* @author joewalnes
* @author wakeful-cloud
*/
import
{
LineBasicMaterial,
BufferGeometry,
Float32BufferAttribute,
Line
} from 'three';
import {BlobWorker, spawn, Thread, Transfer} from 'threads';
import WorkerText from './gcode.worker';
//Export
export default async (file, transfer, theme, progress) =>
{
//Spawn the worker
const worker = await spawn(BlobWorker.fromText(WorkerText));
//Observe progress
worker.observeProgress().subscribe(progress);
//If the transfer option is true, convert the model to a ThreadJS transferable otherwise have ThreadsJS clone the arraybuffer
const bytes = transfer ? Transfer(file) : file;
//Parse
const {primaryVertices, secondaryVertices} = await worker.parse(bytes);
//Kill the worker
Thread.terminate(worker);
//Theming
const primaryMaterial = new LineBasicMaterial({color: theme.primary});
const secondaryMaterial = new LineBasicMaterial({color: theme.secondary});
//Line generation
const primaryGeometry = new BufferGeometry();
const secondaryGeometry = new BufferGeometry();
primaryGeometry.setAttribute(
'position',
new Float32BufferAttribute(primaryVertices, 3)
);
secondaryGeometry.setAttribute(
'position',
new Float32BufferAttribute(secondaryVertices, 3)
);
const primaryLine = new Line(primaryGeometry, primaryMaterial);
const secondaryLine = new Line(secondaryGeometry, secondaryMaterial);
//Metadata
primaryLine.metadata = {...primaryLine.metadata, color: 'primary'};
secondaryLine.metadata = {...secondaryLine.metadata, color: 'secondary'};
//Cleanup
primaryGeometry.dispose();
secondaryGeometry.dispose();
primaryMaterial.dispose();
secondaryMaterial.dispose();
return [primaryLine, secondaryLine];
};