@@ -28,10 +28,10 @@ const FINISH_TYPE_METAL = 5;
28
28
29
29
// State machine to search a subobject path.
30
30
// The LDraw standard establishes these various possible subfolders.
31
- const FILE_LOCATION_AS_IS = 0 ;
32
- const FILE_LOCATION_TRY_PARTS = 1 ;
33
- const FILE_LOCATION_TRY_P = 2 ;
34
- const FILE_LOCATION_TRY_MODELS = 3 ;
31
+ const FILE_LOCATION_TRY_PARTS = 0 ;
32
+ const FILE_LOCATION_TRY_P = 1 ;
33
+ const FILE_LOCATION_TRY_MODELS = 2 ;
34
+ const FILE_LOCATION_AS_IS = 3 ;
35
35
const FILE_LOCATION_TRY_RELATIVE = 4 ;
36
36
const FILE_LOCATION_TRY_ABSOLUTE = 5 ;
37
37
const FILE_LOCATION_NOT_FOUND = 6 ;
@@ -688,7 +688,9 @@ class LDrawParsedCache {
688
688
result . type = original . type ;
689
689
result . category = original . category ;
690
690
result . keywords = original . keywords ;
691
+ result . author = original . author ;
691
692
result . subobjects = original . subobjects ;
693
+ result . fileName = original . fileName ;
692
694
result . totalFaces = original . totalFaces ;
693
695
result . startingConstructionStep = original . startingConstructionStep ;
694
696
result . materials = original . materials ;
@@ -700,7 +702,7 @@ class LDrawParsedCache {
700
702
async fetchData ( fileName ) {
701
703
702
704
let triedLowerCase = false ;
703
- let locationState = FILE_LOCATION_AS_IS ;
705
+ let locationState = FILE_LOCATION_TRY_PARTS ;
704
706
while ( locationState !== FILE_LOCATION_NOT_FOUND ) {
705
707
706
708
let subobjectURL = fileName ;
@@ -743,7 +745,7 @@ class LDrawParsedCache {
743
745
fileName = fileName . toLowerCase ( ) ;
744
746
subobjectURL = fileName ;
745
747
triedLowerCase = true ;
746
- locationState = FILE_LOCATION_AS_IS ;
748
+ locationState = FILE_LOCATION_TRY_PARTS ;
747
749
748
750
}
749
751
@@ -794,6 +796,7 @@ class LDrawParsedCache {
794
796
let type = 'Model' ;
795
797
let category = null ;
796
798
let keywords = null ;
799
+ let author = null ;
797
800
let totalFaces = 0 ;
798
801
799
802
// split into lines
@@ -995,6 +998,12 @@ class LDrawParsedCache {
995
998
996
999
break ;
997
1000
1001
+ case 'Author:' :
1002
+
1003
+ author = lp . getToken ( ) ;
1004
+
1005
+ break ;
1006
+
998
1007
default :
999
1008
// Other meta directives are not implemented
1000
1009
break ;
@@ -1221,6 +1230,7 @@ class LDrawParsedCache {
1221
1230
type,
1222
1231
category,
1223
1232
keywords,
1233
+ author,
1224
1234
subobjects,
1225
1235
totalFaces,
1226
1236
startingConstructionStep,
@@ -1356,6 +1366,9 @@ class LDrawPartsGeometryCache {
1356
1366
const group = new Group ( ) ;
1357
1367
group . userData . category = info . category ;
1358
1368
group . userData . keywords = info . keywords ;
1369
+ group . userData . author = info . author ;
1370
+ group . userData . type = info . type ;
1371
+ group . userData . fileName = info . fileName ;
1359
1372
info . group = group ;
1360
1373
1361
1374
const subobjectInfos = await Promise . all ( promises ) ;
@@ -1380,6 +1393,7 @@ class LDrawPartsGeometryCache {
1380
1393
subobjectGroup . name = subobject . fileName ;
1381
1394
1382
1395
loader . applyMaterialsToMesh ( subobjectGroup , subobject . colorCode , info . materials ) ;
1396
+ subobjectGroup . userData . colorCode = subobject . colorCode ;
1383
1397
1384
1398
group . add ( subobjectGroup ) ;
1385
1399
continue ;
@@ -1473,6 +1487,7 @@ class LDrawPartsGeometryCache {
1473
1487
if ( subobject ) {
1474
1488
1475
1489
loader . applyMaterialsToMesh ( group , subobject . colorCode , info . materials ) ;
1490
+ group . userData . colorCode = subobject . colorCode ;
1476
1491
1477
1492
}
1478
1493
@@ -1883,6 +1898,16 @@ class LDrawLoader extends Loader {
1883
1898
// The path to load parts from the LDraw parts library from.
1884
1899
this . partsLibraryPath = '' ;
1885
1900
1901
+ // Material assigned to not available colors for meshes and edges
1902
+ this . missingColorMaterial = new MeshStandardMaterial ( { color : 0xFF00FF , roughness : 0.3 , metalness : 0 } ) ;
1903
+ this . missingColorMaterial . name = 'Missing material' ;
1904
+ this . missingEdgeColorMaterial = new LineBasicMaterial ( { color : 0xFF00FF } ) ;
1905
+ this . missingEdgeColorMaterial . name = 'Missing material - Edge' ;
1906
+ this . missingConditionalEdgeColorMaterial = new LDrawConditionalLineMaterial ( { fog : true , color : 0xFF00FF } ) ;
1907
+ this . missingConditionalEdgeColorMaterial . name = 'Missing material - Conditional Edge' ;
1908
+ this . missingColorMaterial . userData . edgeMaterial = this . missingEdgeColorMaterial ;
1909
+ this . missingEdgeColorMaterial . userData . conditionalEdgeMaterial = this . missingConditionalEdgeColorMaterial ;
1910
+
1886
1911
}
1887
1912
1888
1913
setPartsLibraryPath ( path ) {
@@ -1934,6 +1959,7 @@ class LDrawLoader extends Loader {
1934
1959
1935
1960
this . applyMaterialsToMesh ( group , MAIN_COLOUR_CODE , this . materialLibrary , true ) ;
1936
1961
this . computeConstructionSteps ( group ) ;
1962
+ group . userData . fileName = url ;
1937
1963
onLoad ( group ) ;
1938
1964
1939
1965
} )
@@ -1949,7 +1975,9 @@ class LDrawLoader extends Loader {
1949
1975
. parseModel ( text , this . materialLibrary )
1950
1976
. then ( group => {
1951
1977
1978
+ this . applyMaterialsToMesh ( group , MAIN_COLOUR_CODE , this . materialLibrary , true ) ;
1952
1979
this . computeConstructionSteps ( group ) ;
1980
+ group . userData . fileName = '' ;
1953
1981
onLoad ( group ) ;
1954
1982
1955
1983
} ) ;
@@ -2080,8 +2108,11 @@ class LDrawLoader extends Loader {
2080
2108
material = loader . getMaterial ( colorCode ) ;
2081
2109
if ( material === null ) {
2082
2110
2083
- // otherwise throw an error if this is final opportunity to set the material
2084
- throw new Error ( `LDrawLoader: Material properties for code ${ colorCode } not available.` ) ;
2111
+ // otherwise throw a warning if this is final opportunity to set the material
2112
+ console . warn ( `LDrawLoader: Material properties for code ${ colorCode } not available.` ) ;
2113
+
2114
+ // And return the 'missing color' material
2115
+ material = loader . missingColorMaterial ;
2085
2116
2086
2117
}
2087
2118
@@ -2118,8 +2149,8 @@ class LDrawLoader extends Loader {
2118
2149
2119
2150
getMainEdgeMaterial ( ) {
2120
2151
2121
- const mainMat = this . getMainMaterial ( ) ;
2122
- return mainMat && mainMat . userData ? mainMat . userData . edgeMaterial : null ;
2152
+ const mat = this . getMaterial ( MAIN_EDGE_COLOUR_CODE ) ;
2153
+ return mat ? mat . userData . edgeMaterial : null ;
2123
2154
2124
2155
}
2125
2156
@@ -2162,113 +2193,113 @@ class LDrawLoader extends Loader {
2162
2193
2163
2194
}
2164
2195
2165
- switch ( token . toUpperCase ( ) ) {
2196
+ if ( ! parseLuminance ( token ) ) {
2166
2197
2167
- case 'CODE' :
2198
+ switch ( token . toUpperCase ( ) ) {
2168
2199
2169
- code = lineParser . getToken ( ) ;
2170
- break ;
2200
+ case 'CODE' :
2171
2201
2172
- case 'VALUE' :
2202
+ code = lineParser . getToken ( ) ;
2203
+ break ;
2173
2204
2174
- color = lineParser . getToken ( ) ;
2175
- if ( color . startsWith ( '0x' ) ) {
2205
+ case 'VALUE' :
2176
2206
2177
- color = '#' + color . substring ( 2 ) ;
2207
+ color = lineParser . getToken ( ) ;
2208
+ if ( color . startsWith ( '0x' ) ) {
2178
2209
2179
- } else if ( ! color . startsWith ( '#' ) ) {
2210
+ color = '#' + color . substring ( 2 ) ;
2180
2211
2181
- throw new Error ( 'LDrawLoader: Invalid color while parsing material' + lineParser . getLineNumberString ( ) + '. ' ) ;
2212
+ } else if ( ! color . startsWith ( '# ' ) ) {
2182
2213
2183
- }
2214
+ throw new Error ( 'LDrawLoader: Invalid color while parsing material' + lineParser . getLineNumberString ( ) + '.' ) ;
2184
2215
2185
- break ;
2216
+ }
2186
2217
2187
- case 'EDGE' :
2218
+ break ;
2188
2219
2189
- edgeColor = lineParser . getToken ( ) ;
2190
- if ( edgeColor . startsWith ( '0x' ) ) {
2220
+ case 'EDGE' :
2191
2221
2192
- edgeColor = '#' + edgeColor . substring ( 2 ) ;
2222
+ edgeColor = lineParser . getToken ( ) ;
2223
+ if ( edgeColor . startsWith ( '0x' ) ) {
2193
2224
2194
- } else if ( ! edgeColor . startsWith ( '#' ) ) {
2225
+ edgeColor = '#' + edgeColor . substring ( 2 ) ;
2195
2226
2196
- // Try to see if edge color is a color code
2197
- edgeMaterial = this . getMaterial ( edgeColor ) ;
2198
- if ( ! edgeMaterial ) {
2227
+ } else if ( ! edgeColor . startsWith ( '#' ) ) {
2199
2228
2200
- throw new Error ( 'LDrawLoader: Invalid edge color while parsing material' + lineParser . getLineNumberString ( ) + '.' ) ;
2229
+ // Try to see if edge color is a color code
2230
+ edgeMaterial = this . getMaterial ( edgeColor ) ;
2231
+ if ( ! edgeMaterial ) {
2201
2232
2202
- }
2233
+ throw new Error ( 'LDrawLoader: Invalid edge color while parsing material' + lineParser . getLineNumberString ( ) + '.' ) ;
2203
2234
2204
- // Get the edge material for this triangle material
2205
- edgeMaterial = edgeMaterial . userData . edgeMaterial ;
2235
+ }
2206
2236
2207
- }
2237
+ // Get the edge material for this triangle material
2238
+ edgeMaterial = edgeMaterial . userData . edgeMaterial ;
2208
2239
2209
- break ;
2240
+ }
2210
2241
2211
- case 'ALPHA' :
2242
+ break ;
2212
2243
2213
- alpha = parseInt ( lineParser . getToken ( ) ) ;
2244
+ case 'ALPHA' :
2214
2245
2215
- if ( isNaN ( alpha ) ) {
2246
+ alpha = parseInt ( lineParser . getToken ( ) ) ;
2216
2247
2217
- throw new Error ( 'LDrawLoader: Invalid alpha value in material definition' + lineParser . getLineNumberString ( ) + '.' ) ;
2248
+ if ( isNaN ( alpha ) ) {
2218
2249
2219
- }
2250
+ throw new Error ( 'LDrawLoader: Invalid alpha value in material definition' + lineParser . getLineNumberString ( ) + '.' ) ;
2220
2251
2221
- alpha = Math . max ( 0 , Math . min ( 1 , alpha / 255 ) ) ;
2252
+ }
2222
2253
2223
- if ( alpha < 1 ) {
2254
+ alpha = Math . max ( 0 , Math . min ( 1 , alpha / 255 ) ) ;
2224
2255
2225
- isTransparent = true ;
2256
+ if ( alpha < 1 ) {
2226
2257
2227
- }
2258
+ isTransparent = true ;
2228
2259
2229
- break ;
2260
+ }
2230
2261
2231
- case 'LUMINANCE' :
2262
+ break ;
2232
2263
2233
- luminance = parseInt ( lineParser . getToken ( ) ) ;
2264
+ case 'LUMINANCE' :
2234
2265
2235
- if ( isNaN ( luminance ) ) {
2266
+ if ( ! parseLuminance ( lineParser . getToken ( ) ) ) {
2236
2267
2237
- throw new Error ( 'LDrawLoader: Invalid luminance value in material definition' + LineParser . getLineNumberString ( ) + '.' ) ;
2268
+ throw new Error ( 'LDrawLoader: Invalid luminance value in material definition' + LineParser . getLineNumberString ( ) + '.' ) ;
2238
2269
2239
- }
2270
+ }
2240
2271
2241
- luminance = Math . max ( 0 , Math . min ( 1 , luminance / 255 ) ) ;
2272
+ break ;
2242
2273
2243
- break ;
2274
+ case 'CHROME' :
2275
+ finishType = FINISH_TYPE_CHROME ;
2276
+ break ;
2244
2277
2245
- case 'CHROME ' :
2246
- finishType = FINISH_TYPE_CHROME ;
2247
- break ;
2278
+ case 'PEARLESCENT ' :
2279
+ finishType = FINISH_TYPE_PEARLESCENT ;
2280
+ break ;
2248
2281
2249
- case 'PEARLESCENT ' :
2250
- finishType = FINISH_TYPE_PEARLESCENT ;
2251
- break ;
2282
+ case 'RUBBER ' :
2283
+ finishType = FINISH_TYPE_RUBBER ;
2284
+ break ;
2252
2285
2253
- case 'RUBBER ' :
2254
- finishType = FINISH_TYPE_RUBBER ;
2255
- break ;
2286
+ case 'MATTE_METALLIC ' :
2287
+ finishType = FINISH_TYPE_MATTE_METALLIC ;
2288
+ break ;
2256
2289
2257
- case 'MATTE_METALLIC ' :
2258
- finishType = FINISH_TYPE_MATTE_METALLIC ;
2259
- break ;
2290
+ case 'METAL ' :
2291
+ finishType = FINISH_TYPE_METAL ;
2292
+ break ;
2260
2293
2261
- case 'METAL' :
2262
- finishType = FINISH_TYPE_METAL ;
2263
- break ;
2294
+ case 'MATERIAL' :
2295
+ // Not implemented
2296
+ lineParser . setToEnd ( ) ;
2297
+ break ;
2264
2298
2265
- case 'MATERIAL' :
2266
- // Not implemented
2267
- lineParser . setToEnd ( ) ;
2268
- break ;
2299
+ default :
2300
+ throw new Error ( 'LDrawLoader: Unknown token "' + token + '" while parsing material' + lineParser . getLineNumberString ( ) + '.' ) ;
2269
2301
2270
- default :
2271
- throw new Error ( 'LDrawLoader: Unknown token "' + token + '" while parsing material' + lineParser . getLineNumberString ( ) + '.' ) ;
2302
+ }
2272
2303
2273
2304
}
2274
2305
@@ -2358,6 +2389,8 @@ class LDrawLoader extends Loader {
2358
2389
2359
2390
} ) ;
2360
2391
edgeMaterial . userData . conditionalEdgeMaterial . color . convertSRGBToLinear ( ) ;
2392
+ edgeMaterial . userData . conditionalEdgeMaterial . userData . code = code ;
2393
+ edgeMaterial . userData . conditionalEdgeMaterial . name = name + ' - Conditional Edge' ;
2361
2394
2362
2395
}
2363
2396
@@ -2370,6 +2403,35 @@ class LDrawLoader extends Loader {
2370
2403
2371
2404
return material ;
2372
2405
2406
+ function parseLuminance ( token ) {
2407
+
2408
+ // Returns success
2409
+
2410
+ let lum ;
2411
+
2412
+ if ( token . startsWith ( 'LUMINANCE' ) ) {
2413
+
2414
+ lum = parseInt ( token . substring ( 9 ) ) ;
2415
+
2416
+ }
2417
+ else {
2418
+
2419
+ lum = parseInt ( token ) ;
2420
+
2421
+ }
2422
+
2423
+ if ( isNaN ( lum ) ) {
2424
+
2425
+ return false ;
2426
+
2427
+ }
2428
+
2429
+ luminance = Math . max ( 0 , Math . min ( 1 , lum / 255 ) ) ;
2430
+
2431
+ return true ;
2432
+
2433
+ }
2434
+
2373
2435
}
2374
2436
2375
2437
computeConstructionSteps ( model ) {
0 commit comments