@@ -167,6 +167,26 @@ class LDrawConditionalLineMaterial extends ShaderMaterial {
167167
168168}
169169
170+ function generateFaceNormals ( faces ) {
171+
172+ for ( let i = 0 , l = faces . length ; i < l ; i ++ ) {
173+
174+ const face = faces [ i ] ;
175+ const vertices = face . vertices ;
176+ const v0 = vertices [ 0 ] ;
177+ const v1 = vertices [ 1 ] ;
178+ const v2 = vertices [ 2 ] ;
179+
180+ _tempVec0 . subVectors ( v1 , v0 ) ;
181+ _tempVec1 . subVectors ( v2 , v1 ) ;
182+ face . faceNormal = new Vector3 ( )
183+ . crossVectors ( _tempVec0 , _tempVec1 )
184+ . normalize ( ) ;
185+
186+ }
187+
188+ }
189+
170190function smoothNormals ( faces , lineSegments ) {
171191
172192 function hashVertex ( v ) {
@@ -666,8 +686,22 @@ function createObject( elements, elementSize, isConditionalSegments = false, tot
666686
667687 }
668688
689+ // create the normals array if this is a set of faces
669690 if ( elementSize === 3 ) {
670691
692+ if ( ! elem . faceNormal ) {
693+
694+ const v0 = vertices [ 0 ] ;
695+ const v1 = vertices [ 1 ] ;
696+ const v2 = vertices [ 2 ] ;
697+ _tempVec0 . subVectors ( v1 , v0 ) ;
698+ _tempVec1 . subVectors ( v2 , v1 ) ;
699+ elem . faceNormal = new Vector3 ( )
700+ . crossVectors ( _tempVec0 , _tempVec1 )
701+ . normalize ( ) ;
702+
703+ }
704+
671705 let elemNormals = elem . normals ;
672706 if ( elemNormals . length === 4 ) {
673707
@@ -683,6 +717,7 @@ function createObject( elements, elementSize, isConditionalSegments = false, tot
683717
684718 for ( let j = 0 , l = elemNormals . length ; j < l ; j ++ ) {
685719
720+ // use face normal if a vertex normal is not provided
686721 let n = elem . faceNormal ;
687722 if ( elemNormals [ j ] ) {
688723
@@ -1321,8 +1356,7 @@ class LDrawLoader extends Loader {
13211356
13221357 let startingConstructionStep = false ;
13231358
1324- const scope = this ;
1325- function parseColourCode ( lineParser , forEdge ) {
1359+ const parseColourCode = ( lineParser , forEdge ) => {
13261360
13271361 // Parses next colour code and returns a THREE.Material
13281362
@@ -1340,7 +1374,7 @@ class LDrawLoader extends Loader {
13401374
13411375 }
13421376
1343- const material = scope . getMaterial ( colourCode , currentParseScope ) ;
1377+ const material = this . getMaterial ( colourCode , currentParseScope ) ;
13441378
13451379 if ( ! material ) {
13461380
@@ -1350,21 +1384,21 @@ class LDrawLoader extends Loader {
13501384
13511385 return material ;
13521386
1353- }
1387+ } ;
13541388
1355- function parseVector ( lp ) {
1389+ const parseVector = lp => {
13561390
13571391 const v = new Vector3 ( parseFloat ( lp . getToken ( ) ) , parseFloat ( lp . getToken ( ) ) , parseFloat ( lp . getToken ( ) ) ) ;
13581392
1359- if ( ! scope . separateObjects ) {
1393+ if ( ! this . separateObjects ) {
13601394
13611395 v . applyMatrix4 ( currentParseScope . currentMatrix ) ;
13621396
13631397 }
13641398
13651399 return v ;
13661400
1367- }
1401+ } ;
13681402
13691403 // Parse all line commands
13701404 for ( let lineIndex = 0 ; lineIndex < numLines ; lineIndex ++ ) {
@@ -1413,7 +1447,7 @@ class LDrawLoader extends Loader {
14131447 let inverted ;
14141448 let ccw ;
14151449 let doubleSided ;
1416- let v0 , v1 , v2 , v3 , c0 , c1 , faceNormal ;
1450+ let v0 , v1 , v2 , v3 , c0 , c1 ;
14171451
14181452 switch ( lineType ) {
14191453
@@ -1437,8 +1471,8 @@ class LDrawLoader extends Loader {
14371471 // needs to be flipped.
14381472 if (
14391473 currentParseScope . matrix . determinant ( ) < 0 && (
1440- scope . separateObjects && isPrimitiveType ( type ) ||
1441- ! scope . separateObjects
1474+ this . separateObjects && isPrimitiveType ( type ) ||
1475+ ! this . separateObjects
14421476 ) ) {
14431477
14441478 currentParseScope . inverted = ! currentParseScope . inverted ;
@@ -1600,10 +1634,10 @@ class LDrawLoader extends Loader {
16001634
16011635 let fileName = lp . getRemainingString ( ) . trim ( ) . replace ( / \\ / g, '/' ) ;
16021636
1603- if ( scope . fileMap [ fileName ] ) {
1637+ if ( this . fileMap [ fileName ] ) {
16041638
16051639 // Found the subobject path in the preloaded file path map
1606- fileName = scope . fileMap [ fileName ] ;
1640+ fileName = this . fileMap [ fileName ] ;
16071641
16081642 } else {
16091643
@@ -1695,16 +1729,10 @@ class LDrawLoader extends Loader {
16951729
16961730 }
16971731
1698- _tempVec0 . subVectors ( v1 , v0 ) ;
1699- _tempVec1 . subVectors ( v2 , v1 ) ;
1700- faceNormal = new Vector3 ( )
1701- . crossVectors ( _tempVec0 , _tempVec1 )
1702- . normalize ( ) ;
1703-
17041732 faces . push ( {
17051733 material : material ,
17061734 colourCode : material . userData . code ,
1707- faceNormal : faceNormal ,
1735+ faceNormal : null ,
17081736 vertices : [ v0 , v1 , v2 ] ,
17091737 normals : [ null , null , null ] ,
17101738 } ) ;
@@ -1715,7 +1743,7 @@ class LDrawLoader extends Loader {
17151743 faces . push ( {
17161744 material : material ,
17171745 colourCode : material . userData . code ,
1718- faceNormal : faceNormal ,
1746+ faceNormal : null ,
17191747 vertices : [ v2 , v1 , v0 ] ,
17201748 normals : [ null , null , null ] ,
17211749 } ) ;
@@ -1750,18 +1778,12 @@ class LDrawLoader extends Loader {
17501778
17511779 }
17521780
1753- _tempVec0 . subVectors ( v1 , v0 ) ;
1754- _tempVec1 . subVectors ( v2 , v1 ) ;
1755- faceNormal = new Vector3 ( )
1756- . crossVectors ( _tempVec0 , _tempVec1 )
1757- . normalize ( ) ;
1758-
17591781 // specifically place the triangle diagonal in the v0 and v1 slots so we can
17601782 // account for the doubling of vertices later when smoothing normals.
17611783 faces . push ( {
17621784 material : material ,
17631785 colourCode : material . userData . code ,
1764- faceNormal : faceNormal ,
1786+ faceNormal : null ,
17651787 vertices : [ v0 , v1 , v2 , v3 ] ,
17661788 normals : [ null , null , null , null ] ,
17671789 } ) ;
@@ -1772,7 +1794,7 @@ class LDrawLoader extends Loader {
17721794 faces . push ( {
17731795 material : material ,
17741796 colourCode : material . userData . code ,
1775- faceNormal : faceNormal ,
1797+ faceNormal : null ,
17761798 vertices : [ v3 , v2 , v1 , v0 ] ,
17771799 normals : [ null , null , null , null ] ,
17781800 } ) ;
@@ -1802,7 +1824,7 @@ class LDrawLoader extends Loader {
18021824 currentParseScope . subobjectIndex = 0 ;
18031825
18041826 const isRoot = ! parentParseScope . isFromParse ;
1805- if ( isRoot || scope . separateObjects && ! isPrimitiveType ( type ) ) {
1827+ if ( isRoot || this . separateObjects && ! isPrimitiveType ( type ) ) {
18061828
18071829 currentParseScope . groupObject = new Group ( ) ;
18081830 currentParseScope . groupObject . userData . startingConstructionStep = currentParseScope . startingConstructionStep ;
@@ -1860,6 +1882,7 @@ class LDrawLoader extends Loader {
18601882
18611883 if ( this . smoothNormals && doSmooth ) {
18621884
1885+ generateFaceNormals ( subobjectParseScope . faces ) ;
18631886 smoothNormals ( subobjectParseScope . faces , subobjectParseScope . lineSegments ) ;
18641887
18651888 }
@@ -1953,14 +1976,10 @@ class LDrawLoader extends Loader {
19531976 const vertices = tri . vertices ;
19541977 for ( let i = 0 , l = vertices . length ; i < l ; i ++ ) {
19551978
1956- vertices [ i ] = vertices [ i ] . clone ( ) . applyMatrix4 ( subobjectParseScope . matrix ) ;
1979+ vertices [ i ] . applyMatrix4 ( subobjectParseScope . matrix ) ;
19571980
19581981 }
19591982
1960- _tempVec0 . subVectors ( vertices [ 1 ] , vertices [ 0 ] ) ;
1961- _tempVec1 . subVectors ( vertices [ 2 ] , vertices [ 1 ] ) ;
1962- tri . faceNormal . crossVectors ( _tempVec0 , _tempVec1 ) . normalize ( ) ;
1963-
19641983 }
19651984
19661985 parentFaces . push ( tri ) ;
@@ -2031,9 +2050,10 @@ class LDrawLoader extends Loader {
20312050
20322051 return scope . processObject ( text , subobject , url , parseScope ) ;
20332052
2034- } ) . catch ( function ( ) {
2053+ } ) . catch ( function ( err ) {
20352054
2036- console . warn ( 'LDrawLoader: Subobject "' + subobject . fileName + '" could not be found.' ) ;
2055+ console . warn ( 'LDrawLoader: Subobject "' + subobject . fileName + '" could not be loaded.' ) ;
2056+ console . warn ( err ) ;
20372057 return null ;
20382058
20392059 } ) ;
0 commit comments