@@ -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
7064export 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