Skip to content

Commit 8deebdf

Browse files
committed
Merge branch 'develop' into alpha
2 parents f5e0d28 + 84c3655 commit 8deebdf

8 files changed

Lines changed: 38 additions & 53 deletions

File tree

.github/workflows/run-tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ jobs:
2424
- uses: actions/checkout@v4
2525
- uses: actions/setup-node@v4
2626
with:
27-
node-version: '20.x'
27+
node-version: '22.x'
2828
cache: "npm"
2929
- run: npm ci
3030
- run: npm run build

.nvmrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v18.19.0
1+
v22.12.0

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,6 @@ It would be great if you want to help! Maybe you'd like to help out with:
163163
- ❤️ Thank you @Sindarius for implementing G2/G3 arc support.
164164
- ❤️ Thank you @Zeng95 for providing a React & Typescript example.
165165

166-
## Changelog
167-
Jump to the [CHANGELOG](CHANGELOG.md)
168166
## Known issues
169167
### Preview doesn't render in Brave
170168
This is caused by the device recognition shield in Brave. By changing the setting for "Device Recognition" in Shield settings to "Allow all device recognition attemps" or "Only block cross-site device recognition attemps" you should not get this error.

demo/index.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<!DOCTYPE html>
22
<head>
3+
<script src="https://cdn.lrkt-in.com/LogRocket.min.js" crossorigin="anonymous"></script>
4+
<script>document.location.hostname.includes('gcode-preview.web.app') && window.LogRocket && window.LogRocket.init('x64tmd/gcode-preview-demo');</script>
35
<script>
46
function loadAnalytics() {
57
var script = document.createElement("script");

demo/js/app.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,9 @@ export const app = (window.app = createApp({
216216
watchEffect(() => {
217217
preview.startLayer = +settings.value.startLayer;
218218
preview.endLayer = +settings.value.endLayer;
219+
});
220+
221+
watchEffect(() => {
219222
preview.singleLayerMode = settings.value.singleLayerMode;
220223
});
221224
});

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "gcode-preview",
3-
"version": "3.0.0-alpha.1",
3+
"version": "3.0.0-alpha.2",
44
"description": "Preview a 3d print from a gcode file",
55
"author": "remcoder@gmail.com",
66
"license": "MIT",
@@ -51,7 +51,7 @@
5151
"version:patch": "npm version patch",
5252
"version:minor": "npm version minor",
5353
"predeploy": "npm run build && npm run copy-deps && npm run copy-dist",
54-
"copy-deps": "copyfiles -f node_modules/three/build/three.module.js demo/lib/three/build && copyfiles -f node_modules/lil-gui/dist/lil-gui.esm.min.js demo/lib/lil-gui/dist",
54+
"copy-deps": "copyfiles -f node_modules/three/build/three.module.js demo/lib/three/build && copyfiles -f node_modules/three/build/three.core.js demo/lib/three/build && copyfiles -f node_modules/lil-gui/dist/lil-gui.esm.min.js demo/lib/lil-gui/dist",
5555
"copy-dist": "copyfiles -f dist/gcode-preview.es.js demo/dist",
5656
"deploy:preview": "live-server demo",
5757
"typedoc": "typedoc",

rollup.config.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-env node */
2-
import pkg from './package.json' assert { type: 'json' };
2+
import pkg from './package.json' with { type: 'json' };
33
import esbuild from 'rollup-plugin-esbuild';
44
import dts from 'rollup-plugin-dts';
55
import { nodeResolve } from '@rollup/plugin-node-resolve';

src/webgl-preview.ts

Lines changed: 28 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ export type GCodePreviewOptions = {
3737
buildVolume?: BuildVolume;
3838
backgroundColor?: ColorRepresentation;
3939
canvas?: HTMLCanvasElement;
40-
debug?: boolean;
4140
endLayer?: number;
4241
extrusionColor?: ColorRepresentation | ColorRepresentation[];
4342
initialCameraPosition?: number[];
@@ -59,20 +58,11 @@ export type GCodePreviewOptions = {
5958
* @deprecated Please see the demo how to implement drag and drop.
6059
*/
6160
allowDragNDrop?: boolean;
62-
/**
63-
* @deprecated Please use the `canvas` param instead.
64-
*/
65-
targetId?: string;
66-
/** @experimental */
6761
devMode?: boolean | DevModeOptions;
6862
};
6963

7064
export class WebGLPreview {
7165
minLayerThreshold: number;
72-
/**
73-
* @deprecated Please use the `canvas` param instead.
74-
*/
75-
targetId?: string;
7666
scene: Scene;
7767
camera: PerspectiveCamera;
7868
renderer: WebGLRenderer;
@@ -89,10 +79,6 @@ export class WebGLPreview {
8979
_singleLayerMode = false;
9080
buildVolume?: BuildVolume;
9181
initialCameraPosition = [-100, 400, 450];
92-
/**
93-
* @deprecated Use the dev mode options instead.
94-
*/
95-
debug = false;
9682
inches = false;
9783
nonTravelmoves: string[] = [];
9884
disableGradient = false;
@@ -111,6 +97,7 @@ export class WebGLPreview {
11197
private minPlane = new Plane(new Vector3(0, 1, 0), 0.6);
11298
private maxPlane = new Plane(new Vector3(0, -1, 0), 0.1);
11399
private clippingPlanes: Plane[] = [];
100+
private prevStartLayer = 0;
114101

115102
// colors
116103
private _backgroundColor = new Color(0xe0e0e0);
@@ -119,7 +106,7 @@ export class WebGLPreview {
119106
private _lastSegmentColor?: Color;
120107
private _toolColors: Record<number, Color> = {};
121108

122-
// debug
109+
// dev mode
123110
private devMode?: boolean | DevModeOptions = false;
124111
private _lastRenderTime = 0;
125112
private _wireframe = false;
@@ -136,14 +123,12 @@ export class WebGLPreview {
136123
if (opts.backgroundColor !== undefined) {
137124
this.backgroundColor = new Color(opts.backgroundColor);
138125
}
139-
this.targetId = opts.targetId;
140126
this.endLayer = opts.endLayer;
141127
this.startLayer = opts.startLayer;
142128
this.lineWidth = opts.lineWidth ?? 1;
143129
this.lineHeight = opts.lineHeight;
144130
this.buildVolume = opts.buildVolume && new BuildVolume(opts.buildVolume.x, opts.buildVolume.y, opts.buildVolume.z);
145131
this.initialCameraPosition = opts.initialCameraPosition ?? this.initialCameraPosition;
146-
this.debug = opts.debug ?? this.debug;
147132
this.renderExtrusion = opts.renderExtrusion ?? this.renderExtrusion;
148133
this.renderTravel = opts.renderTravel ?? this.renderTravel;
149134
this.nonTravelmoves = opts.nonTravelMoves ?? this.nonTravelmoves;
@@ -152,6 +137,10 @@ export class WebGLPreview {
152137
this.devMode = opts.devMode ?? this.devMode;
153138
this.stats = this.devMode ? new Stats() : undefined;
154139

140+
if (!opts.canvas) {
141+
throw Error('Set either opts.canvas or opts.targetId');
142+
}
143+
155144
if (opts.extrusionColor !== undefined) {
156145
this.extrusionColor = opts.extrusionColor;
157146
}
@@ -178,28 +167,11 @@ export class WebGLPreview {
178167
console.info('Using THREE r' + REVISION);
179168
console.debug('opts', opts);
180169

181-
if (this.targetId) {
182-
console.warn('`targetId` is deprecated and will removed in the future. Use `canvas` instead.');
183-
}
184-
185-
if (!opts.canvas) {
186-
if (!this.targetId) {
187-
throw Error('Set either opts.canvas or opts.targetId');
188-
}
189-
const container = document.getElementById(this.targetId);
190-
if (!container) throw new Error('Unable to find element ' + this.targetId);
191-
192-
this.renderer = new WebGLRenderer({ preserveDrawingBuffer: this.preserveDrawingBuffer });
193-
this.canvas = this.renderer.domElement;
194-
195-
container.appendChild(this.canvas);
196-
} else {
197-
this.canvas = opts.canvas;
198-
this.renderer = new WebGLRenderer({
199-
canvas: this.canvas,
200-
preserveDrawingBuffer: this.preserveDrawingBuffer
201-
});
202-
}
170+
this.canvas = opts.canvas;
171+
this.renderer = new WebGLRenderer({
172+
canvas: this.canvas,
173+
preserveDrawingBuffer: this.preserveDrawingBuffer
174+
});
203175

204176
this.renderer.localClippingEnabled = true;
205177
this.camera = new PerspectiveCamera(25, this.canvas.offsetWidth / this.canvas.offsetHeight, 10, 5000);
@@ -275,7 +247,6 @@ export class WebGLPreview {
275247
if (this.countLayers > 1 && value > 0) {
276248
this._startLayer = value;
277249
if (value <= this.countLayers) {
278-
console.log(value);
279250
const layer = this.job.layers[value - 1];
280251
this.minPlane.constant = -this.minPlane.normal.y * layer.z;
281252
this.clippingPlanes = [this.minPlane, this.maxPlane];
@@ -293,7 +264,7 @@ export class WebGLPreview {
293264
if (this.countLayers > 1 && value > 0) {
294265
this._endLayer = value;
295266
if (this._singleLayerMode === true) {
296-
this.startLayer = this._endLayer;
267+
this.startLayer = this._endLayer - 1;
297268
}
298269
if (value <= this.countLayers) {
299270
const layer = this.job.layers[value - 1];
@@ -306,10 +277,16 @@ export class WebGLPreview {
306277
}
307278
}
308279

280+
get singleLayerMode(): boolean {
281+
return this._singleLayerMode;
282+
}
309283
set singleLayerMode(value: boolean) {
310284
this._singleLayerMode = value;
311285
if (value) {
312-
this.startLayer = this.endLayer - 1;
286+
this.prevStartLayer = this._startLayer;
287+
this.startLayer = this._endLayer - 1;
288+
} else {
289+
this.startLayer = this.prevStartLayer;
313290
}
314291
}
315292

@@ -481,7 +458,6 @@ export class WebGLPreview {
481458
}
482459

483460
private renderPaths(endPathNumber: number = Infinity): void {
484-
console.log('rendering paths');
485461
if (this.renderTravel) {
486462
this.renderPathsAsLines(this.job.travels.slice(this.renderPathIndex, endPathNumber), this._travelColor);
487463
}
@@ -499,18 +475,24 @@ export class WebGLPreview {
499475
}
500476

501477
private renderPathsAsLines(paths: Path[], color: Color): void {
502-
console.log(this.clippingPlanes);
503478
const material = new LineMaterial({
504479
color: Number(color.getHex()),
505480
linewidth: this.lineWidth,
506481
clippingPlanes: this.clippingPlanes
507482
});
508483

509484
const lineVertices: number[] = [];
485+
486+
// lines need to be offset.
487+
// The gcode specifies the nozzle height which is the top of the extrusion.
488+
// The line doesn't have a constant height in world coords so it should be rendered at horizontal midplane of the extrusion layer.
489+
// Otherwise the line will be clipped by the clipping plane.
490+
const offset = -this.lineHeight / 2;
491+
510492
paths.forEach((path) => {
511493
for (let i = 0; i < path.vertices.length - 3; i += 3) {
512-
lineVertices.push(path.vertices[i], path.vertices[i + 1], path.vertices[i + 2]);
513-
lineVertices.push(path.vertices[i + 3], path.vertices[i + 4], path.vertices[i + 5]);
494+
lineVertices.push(path.vertices[i], path.vertices[i + 1] - 0.1, path.vertices[i + 2] + offset);
495+
lineVertices.push(path.vertices[i + 3], path.vertices[i + 4] - 0.1, path.vertices[i + 5] + offset);
514496
}
515497
});
516498

0 commit comments

Comments
 (0)