-
-
Notifications
You must be signed in to change notification settings - Fork 35.5k
/
LDrawLoader.js
2357 lines (1516 loc) · 53.2 KB
/
LDrawLoader.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import {
BufferAttribute,
BufferGeometry,
Color,
FileLoader,
Group,
LineBasicMaterial,
LineSegments,
Loader,
Matrix4,
Mesh,
MeshStandardMaterial,
SRGBColorSpace,
Vector3,
Ray
} from 'three';
// Special surface finish tag types.
// Note: "MATERIAL" tag (e.g. GLITTER, SPECKLE) is not implemented
const FINISH_TYPE_DEFAULT = 0;
const FINISH_TYPE_CHROME = 1;
const FINISH_TYPE_PEARLESCENT = 2;
const FINISH_TYPE_RUBBER = 3;
const FINISH_TYPE_MATTE_METALLIC = 4;
const FINISH_TYPE_METAL = 5;
// State machine to search a subobject path.
// The LDraw standard establishes these various possible subfolders.
const FILE_LOCATION_TRY_PARTS = 0;
const FILE_LOCATION_TRY_P = 1;
const FILE_LOCATION_TRY_MODELS = 2;
const FILE_LOCATION_AS_IS = 3;
const FILE_LOCATION_TRY_RELATIVE = 4;
const FILE_LOCATION_TRY_ABSOLUTE = 5;
const FILE_LOCATION_NOT_FOUND = 6;
const MAIN_COLOUR_CODE = '16';
const MAIN_EDGE_COLOUR_CODE = '24';
const COLOR_SPACE_LDRAW = SRGBColorSpace;
const _tempVec0 = new Vector3();
const _tempVec1 = new Vector3();
class ConditionalLineSegments extends LineSegments {
constructor( geometry, material ) {
super( geometry, material );
this.isConditionalLine = true;
}
}
function generateFaceNormals( faces ) {
for ( let i = 0, l = faces.length; i < l; i ++ ) {
const face = faces[ i ];
const vertices = face.vertices;
const v0 = vertices[ 0 ];
const v1 = vertices[ 1 ];
const v2 = vertices[ 2 ];
_tempVec0.subVectors( v1, v0 );
_tempVec1.subVectors( v2, v1 );
face.faceNormal = new Vector3()
.crossVectors( _tempVec0, _tempVec1 )
.normalize();
}
}
const _ray = new Ray();
function smoothNormals( faces, lineSegments, checkSubSegments = false ) {
// NOTE: 1e2 is pretty coarse but was chosen to quantize the resulting value because
// it allows edges to be smoothed as expected (see minifig arms).
// --
// And the vector values are initialize multiplied by 1 + 1e-10 to account for floating
// point errors on vertices along quantization boundaries. Ie after matrix multiplication
// vertices that should be merged might be set to "1.7" and "1.6999..." meaning they won't
// get merged. This added epsilon attempts to push these error values to the same quantized
// value for the sake of hashing. See "AT-ST mini" dishes. See mrdoob/three#23169.
const hashMultiplier = ( 1 + 1e-10 ) * 1e2;
function hashVertex( v ) {
const x = ~ ~ ( v.x * hashMultiplier );
const y = ~ ~ ( v.y * hashMultiplier );
const z = ~ ~ ( v.z * hashMultiplier );
return `${ x },${ y },${ z }`;
}
function hashEdge( v0, v1 ) {
return `${ hashVertex( v0 ) }_${ hashVertex( v1 ) }`;
}
// converts the two vertices to a ray with a normalized direction and origin of 0, 0, 0 projected
// onto the original line.
function toNormalizedRay( v0, v1, targetRay ) {
targetRay.direction.subVectors( v1, v0 ).normalize();
const scalar = v0.dot( targetRay.direction );
targetRay.origin.copy( v0 ).addScaledVector( targetRay.direction, - scalar );
return targetRay;
}
function hashRay( ray ) {
return hashEdge( ray.origin, ray.direction );
}
const hardEdges = new Set();
const hardEdgeRays = new Map();
const halfEdgeList = {};
const normals = [];
// Save the list of hard edges by hash
for ( let i = 0, l = lineSegments.length; i < l; i ++ ) {
const ls = lineSegments[ i ];
const vertices = ls.vertices;
const v0 = vertices[ 0 ];
const v1 = vertices[ 1 ];
hardEdges.add( hashEdge( v0, v1 ) );
hardEdges.add( hashEdge( v1, v0 ) );
// only generate the hard edge ray map if we're checking subsegments because it's more expensive to check
// and requires more memory.
if ( checkSubSegments ) {
// add both ray directions to the map
const ray = toNormalizedRay( v0, v1, new Ray() );
const rh1 = hashRay( ray );
if ( ! hardEdgeRays.has( rh1 ) ) {
toNormalizedRay( v1, v0, ray );
const rh2 = hashRay( ray );
const info = {
ray,
distances: [],
};
hardEdgeRays.set( rh1, info );
hardEdgeRays.set( rh2, info );
}
// store both segments ends in min, max order in the distances array to check if a face edge is a
// subsegment later.
const info = hardEdgeRays.get( rh1 );
let d0 = info.ray.direction.dot( v0 );
let d1 = info.ray.direction.dot( v1 );
if ( d0 > d1 ) {
[ d0, d1 ] = [ d1, d0 ];
}
info.distances.push( d0, d1 );
}
}
// track the half edges associated with each triangle
for ( let i = 0, l = faces.length; i < l; i ++ ) {
const tri = faces[ i ];
const vertices = tri.vertices;
const vertCount = vertices.length;
for ( let i2 = 0; i2 < vertCount; i2 ++ ) {
const index = i2;
const next = ( i2 + 1 ) % vertCount;
const v0 = vertices[ index ];
const v1 = vertices[ next ];
const hash = hashEdge( v0, v1 );
// don't add the triangle if the edge is supposed to be hard
if ( hardEdges.has( hash ) ) {
continue;
}
// if checking subsegments then check to see if this edge lies on a hard edge ray and whether its within any ray bounds
if ( checkSubSegments ) {
toNormalizedRay( v0, v1, _ray );
const rayHash = hashRay( _ray );
if ( hardEdgeRays.has( rayHash ) ) {
const info = hardEdgeRays.get( rayHash );
const { ray, distances } = info;
let d0 = ray.direction.dot( v0 );
let d1 = ray.direction.dot( v1 );
if ( d0 > d1 ) {
[ d0, d1 ] = [ d1, d0 ];
}
// return early if the face edge is found to be a subsegment of a line edge meaning the edge will have "hard" normals
let found = false;
for ( let i = 0, l = distances.length; i < l; i += 2 ) {
if ( d0 >= distances[ i ] && d1 <= distances[ i + 1 ] ) {
found = true;
break;
}
}
if ( found ) {
continue;
}
}
}
const info = {
index: index,
tri: tri
};
halfEdgeList[ hash ] = info;
}
}
// Iterate until we've tried to connect all faces to share normals
while ( true ) {
// Stop if there are no more faces left
let halfEdge = null;
for ( const key in halfEdgeList ) {
halfEdge = halfEdgeList[ key ];
break;
}
if ( halfEdge === null ) {
break;
}
// Exhaustively find all connected faces
const queue = [ halfEdge ];
while ( queue.length > 0 ) {
// initialize all vertex normals in this triangle
const tri = queue.pop().tri;
const vertices = tri.vertices;
const vertNormals = tri.normals;
const faceNormal = tri.faceNormal;
// Check if any edge is connected to another triangle edge
const vertCount = vertices.length;
for ( let i2 = 0; i2 < vertCount; i2 ++ ) {
const index = i2;
const next = ( i2 + 1 ) % vertCount;
const v0 = vertices[ index ];
const v1 = vertices[ next ];
// delete this triangle from the list so it won't be found again
const hash = hashEdge( v0, v1 );
delete halfEdgeList[ hash ];
const reverseHash = hashEdge( v1, v0 );
const otherInfo = halfEdgeList[ reverseHash ];
if ( otherInfo ) {
const otherTri = otherInfo.tri;
const otherIndex = otherInfo.index;
const otherNormals = otherTri.normals;
const otherVertCount = otherNormals.length;
const otherFaceNormal = otherTri.faceNormal;
// NOTE: If the angle between faces is > 67.5 degrees then assume it's
// hard edge. There are some cases where the line segments do not line up exactly
// with or span multiple triangle edges (see Lunar Vehicle wheels).
if ( Math.abs( otherTri.faceNormal.dot( tri.faceNormal ) ) < 0.25 ) {
continue;
}
// if this triangle has already been traversed then it won't be in
// the halfEdgeList. If it has not then add it to the queue and delete
// it so it won't be found again.
if ( reverseHash in halfEdgeList ) {
queue.push( otherInfo );
delete halfEdgeList[ reverseHash ];
}
// share the first normal
const otherNext = ( otherIndex + 1 ) % otherVertCount;
if (
vertNormals[ index ] && otherNormals[ otherNext ] &&
vertNormals[ index ] !== otherNormals[ otherNext ]
) {
otherNormals[ otherNext ].norm.add( vertNormals[ index ].norm );
vertNormals[ index ].norm = otherNormals[ otherNext ].norm;
}
let sharedNormal1 = vertNormals[ index ] || otherNormals[ otherNext ];
if ( sharedNormal1 === null ) {
// it's possible to encounter an edge of a triangle that has already been traversed meaning
// both edges already have different normals defined and shared. To work around this we create
// a wrapper object so when those edges are merged the normals can be updated everywhere.
sharedNormal1 = { norm: new Vector3() };
normals.push( sharedNormal1.norm );
}
if ( vertNormals[ index ] === null ) {
vertNormals[ index ] = sharedNormal1;
sharedNormal1.norm.add( faceNormal );
}
if ( otherNormals[ otherNext ] === null ) {
otherNormals[ otherNext ] = sharedNormal1;
sharedNormal1.norm.add( otherFaceNormal );
}
// share the second normal
if (
vertNormals[ next ] && otherNormals[ otherIndex ] &&
vertNormals[ next ] !== otherNormals[ otherIndex ]
) {
otherNormals[ otherIndex ].norm.add( vertNormals[ next ].norm );
vertNormals[ next ].norm = otherNormals[ otherIndex ].norm;
}
let sharedNormal2 = vertNormals[ next ] || otherNormals[ otherIndex ];
if ( sharedNormal2 === null ) {
sharedNormal2 = { norm: new Vector3() };
normals.push( sharedNormal2.norm );
}
if ( vertNormals[ next ] === null ) {
vertNormals[ next ] = sharedNormal2;
sharedNormal2.norm.add( faceNormal );
}
if ( otherNormals[ otherIndex ] === null ) {
otherNormals[ otherIndex ] = sharedNormal2;
sharedNormal2.norm.add( otherFaceNormal );
}
}
}
}
}
// The normals of each face have been added up so now we average them by normalizing the vector.
for ( let i = 0, l = normals.length; i < l; i ++ ) {
normals[ i ].normalize();
}
}
function isPartType( type ) {
return type === 'Part' || type === 'Unofficial_Part';
}
function isPrimitiveType( type ) {
return /primitive/i.test( type ) || type === 'Subpart';
}
class LineParser {
constructor( line, lineNumber ) {
this.line = line;
this.lineLength = line.length;
this.currentCharIndex = 0;
this.currentChar = ' ';
this.lineNumber = lineNumber;
}
seekNonSpace() {
while ( this.currentCharIndex < this.lineLength ) {
this.currentChar = this.line.charAt( this.currentCharIndex );
if ( this.currentChar !== ' ' && this.currentChar !== '\t' ) {
return;
}
this.currentCharIndex ++;
}
}
getToken() {
const pos0 = this.currentCharIndex ++;
// Seek space
while ( this.currentCharIndex < this.lineLength ) {
this.currentChar = this.line.charAt( this.currentCharIndex );
if ( this.currentChar === ' ' || this.currentChar === '\t' ) {
break;
}
this.currentCharIndex ++;
}
const pos1 = this.currentCharIndex;
this.seekNonSpace();
return this.line.substring( pos0, pos1 );
}
getVector() {
return new Vector3( parseFloat( this.getToken() ), parseFloat( this.getToken() ), parseFloat( this.getToken() ) );
}
getRemainingString() {
return this.line.substring( this.currentCharIndex, this.lineLength );
}
isAtTheEnd() {
return this.currentCharIndex >= this.lineLength;
}
setToEnd() {
this.currentCharIndex = this.lineLength;
}
getLineNumberString() {
return this.lineNumber >= 0 ? ' at line ' + this.lineNumber : '';
}
}
// Fetches and parses an intermediate representation of LDraw parts files.
class LDrawParsedCache {
constructor( loader ) {
this.loader = loader;
this._cache = {};
}
cloneResult( original ) {
const result = {};
// vertices are transformed and normals computed before being converted to geometry
// so these pieces must be cloned.
result.faces = original.faces.map( face => {
return {
colorCode: face.colorCode,
material: face.material,
vertices: face.vertices.map( v => v.clone() ),
normals: face.normals.map( () => null ),
faceNormal: null
};
} );
result.conditionalSegments = original.conditionalSegments.map( face => {
return {
colorCode: face.colorCode,
material: face.material,
vertices: face.vertices.map( v => v.clone() ),
controlPoints: face.controlPoints.map( v => v.clone() )
};
} );
result.lineSegments = original.lineSegments.map( face => {
return {
colorCode: face.colorCode,
material: face.material,
vertices: face.vertices.map( v => v.clone() )
};
} );
// none if this is subsequently modified
result.type = original.type;
result.category = original.category;
result.keywords = original.keywords;
result.author = original.author;
result.subobjects = original.subobjects;
result.fileName = original.fileName;
result.totalFaces = original.totalFaces;
result.startingBuildingStep = original.startingBuildingStep;
result.materials = original.materials;
result.group = null;
return result;
}
async fetchData( fileName ) {
let triedLowerCase = false;
let locationState = FILE_LOCATION_TRY_PARTS;
while ( locationState !== FILE_LOCATION_NOT_FOUND ) {
let subobjectURL = fileName;
switch ( locationState ) {
case FILE_LOCATION_AS_IS:
locationState = locationState + 1;
break;
case FILE_LOCATION_TRY_PARTS:
subobjectURL = 'parts/' + subobjectURL;
locationState = locationState + 1;
break;
case FILE_LOCATION_TRY_P:
subobjectURL = 'p/' + subobjectURL;
locationState = locationState + 1;
break;
case FILE_LOCATION_TRY_MODELS:
subobjectURL = 'models/' + subobjectURL;
locationState = locationState + 1;
break;
case FILE_LOCATION_TRY_RELATIVE:
subobjectURL = fileName.substring( 0, fileName.lastIndexOf( '/' ) + 1 ) + subobjectURL;
locationState = locationState + 1;
break;
case FILE_LOCATION_TRY_ABSOLUTE:
if ( triedLowerCase ) {
// Try absolute path
locationState = FILE_LOCATION_NOT_FOUND;
} else {
// Next attempt is lower case
fileName = fileName.toLowerCase();
subobjectURL = fileName;
triedLowerCase = true;
locationState = FILE_LOCATION_TRY_PARTS;
}
break;
}
const loader = this.loader;
const fileLoader = new FileLoader( loader.manager );
fileLoader.setPath( loader.partsLibraryPath );
fileLoader.setRequestHeader( loader.requestHeader );
fileLoader.setWithCredentials( loader.withCredentials );
try {
const text = await fileLoader.loadAsync( subobjectURL );
return text;
} catch ( _ ) {
continue;
}
}
throw new Error( 'LDrawLoader: Subobject "' + fileName + '" could not be loaded.' );
}
parse( text, fileName = null ) {
const loader = this.loader;
// final results
const faces = [];
const lineSegments = [];
const conditionalSegments = [];
const subobjects = [];
const materials = {};
const getLocalMaterial = colorCode => {
return materials[ colorCode ] || null;
};
let type = 'Model';
let category = null;
let keywords = null;
let author = null;
let totalFaces = 0;
// split into lines
if ( text.indexOf( '\r\n' ) !== - 1 ) {
// This is faster than String.split with regex that splits on both
text = text.replace( /\r\n/g, '\n' );
}
const lines = text.split( '\n' );
const numLines = lines.length;
let parsingEmbeddedFiles = false;
let currentEmbeddedFileName = null;
let currentEmbeddedText = null;
let bfcCertified = false;
let bfcCCW = true;
let bfcInverted = false;
let bfcCull = true;
let startingBuildingStep = false;
// Parse all line commands
for ( let lineIndex = 0; lineIndex < numLines; lineIndex ++ ) {
const line = lines[ lineIndex ];
if ( line.length === 0 ) continue;
if ( parsingEmbeddedFiles ) {
if ( line.startsWith( '0 FILE ' ) ) {
// Save previous embedded file in the cache
this.setData( currentEmbeddedFileName, currentEmbeddedText );
// New embedded text file
currentEmbeddedFileName = line.substring( 7 );
currentEmbeddedText = '';
} else {
currentEmbeddedText += line + '\n';
}
continue;
}
const lp = new LineParser( line, lineIndex + 1 );
lp.seekNonSpace();
if ( lp.isAtTheEnd() ) {
// Empty line
continue;
}
// Parse the line type
const lineType = lp.getToken();
let material;
let colorCode;
let segment;
let ccw;
let doubleSided;
let v0, v1, v2, v3, c0, c1;
switch ( lineType ) {
// Line type 0: Comment or META
case '0':
// Parse meta directive
const meta = lp.getToken();
if ( meta ) {
switch ( meta ) {
case '!LDRAW_ORG':
type = lp.getToken();
break;
case '!COLOUR':
material = loader.parseColorMetaDirective( lp );
if ( material ) {
materials[ material.userData.code ] = material;
} else {
console.warn( 'LDrawLoader: Error parsing material' + lp.getLineNumberString() );
}
break;
case '!CATEGORY':
category = lp.getToken();
break;
case '!KEYWORDS':
const newKeywords = lp.getRemainingString().split( ',' );
if ( newKeywords.length > 0 ) {
if ( ! keywords ) {
keywords = [];
}
newKeywords.forEach( function ( keyword ) {
keywords.push( keyword.trim() );
} );
}
break;
case 'FILE':
if ( lineIndex > 0 ) {
// Start embedded text files parsing
parsingEmbeddedFiles = true;
currentEmbeddedFileName = lp.getRemainingString();
currentEmbeddedText = '';
bfcCertified = false;
bfcCCW = true;
}
break;
case 'BFC':
// Changes to the backface culling state
while ( ! lp.isAtTheEnd() ) {
const token = lp.getToken();
switch ( token ) {
case 'CERTIFY':
case 'NOCERTIFY':
bfcCertified = token === 'CERTIFY';
bfcCCW = true;
break;
case 'CW':
case 'CCW':
bfcCCW = token === 'CCW';
break;
case 'INVERTNEXT':
bfcInverted = true;
break;
case 'CLIP':
case 'NOCLIP':
bfcCull = token === 'CLIP';
break;
default:
console.warn( 'THREE.LDrawLoader: BFC directive "' + token + '" is unknown.' );
break;
}
}
break;
case 'STEP':
startingBuildingStep = true;
break;
case 'Author:':
author = lp.getToken();
break;
default:
// Other meta directives are not implemented
break;
}
}
break;
// Line type 1: Sub-object file
case '1':
colorCode = lp.getToken();
material = getLocalMaterial( colorCode );
const posX = parseFloat( lp.getToken() );
const posY = parseFloat( lp.getToken() );
const posZ = parseFloat( lp.getToken() );
const m0 = parseFloat( lp.getToken() );
const m1 = parseFloat( lp.getToken() );
const m2 = parseFloat( lp.getToken() );
const m3 = parseFloat( lp.getToken() );
const m4 = parseFloat( lp.getToken() );
const m5 = parseFloat( lp.getToken() );
const m6 = parseFloat( lp.getToken() );
const m7 = parseFloat( lp.getToken() );
const m8 = parseFloat( lp.getToken() );
const matrix = new Matrix4().set(
m0, m1, m2, posX,
m3, m4, m5, posY,
m6, m7, m8, posZ,
0, 0, 0, 1
);
let fileName = lp.getRemainingString().trim().replace( /\\/g, '/' );
if ( loader.fileMap[ fileName ] ) {
// Found the subobject path in the preloaded file path map
fileName = loader.fileMap[ fileName ];
} else {
// Standardized subfolders
if ( fileName.startsWith( 's/' ) ) {
fileName = 'parts/' + fileName;
} else if ( fileName.startsWith( '48/' ) ) {
fileName = 'p/' + fileName;
}
}
subobjects.push( {
material: material,
colorCode: colorCode,
matrix: matrix,
fileName: fileName,
inverted: bfcInverted,
startingBuildingStep: startingBuildingStep
} );
startingBuildingStep = false;
bfcInverted = false;
break;
// Line type 2: Line segment
case '2':
colorCode = lp.getToken();
material = getLocalMaterial( colorCode );
v0 = lp.getVector();
v1 = lp.getVector();
segment = {
material: material,
colorCode: colorCode,
vertices: [ v0, v1 ],
};
lineSegments.push( segment );
break;
// Line type 5: Conditional Line segment
case '5':
colorCode = lp.getToken();
material = getLocalMaterial( colorCode );
v0 = lp.getVector();
v1 = lp.getVector();
c0 = lp.getVector();
c1 = lp.getVector();
segment = {
material: material,
colorCode: colorCode,
vertices: [ v0, v1 ],
controlPoints: [ c0, c1 ],
};
conditionalSegments.push( segment );
break;
// Line type 3: Triangle
case '3':
colorCode = lp.getToken();
material = getLocalMaterial( colorCode );
ccw = bfcCCW;
doubleSided = ! bfcCertified || ! bfcCull;
if ( ccw === true ) {
v0 = lp.getVector();